blob: 3f391aa76d31129bb99293992ef863190110df87 [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
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#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>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Chris Wilson88241782011-01-07 17:09:48 +000039static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
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_set_to_cpu_domain(struct drm_i915_gem_object *obj,
43 bool write);
44static __must_check int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
45 uint64_t offset,
46 uint64_t size);
Chris Wilson05394f32010-11-08 19:18:58 +000047static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000048static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
49 unsigned alignment,
50 bool map_and_fenceable);
Chris Wilsond9e86c02010-11-10 16:40:20 +000051static void i915_gem_clear_fence_reg(struct drm_device *dev,
52 struct drm_i915_fence_reg *reg);
Chris Wilson05394f32010-11-08 19:18:58 +000053static int i915_gem_phys_pwrite(struct drm_device *dev,
54 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100055 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000056 struct drm_file *file);
57static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070058
Chris Wilson17250b72010-10-28 12:51:39 +010059static int i915_gem_inactive_shrink(struct shrinker *shrinker,
Ying Han1495f232011-05-24 17:12:27 -070060 struct shrink_control *sc);
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 Wilson73aa8082010-09-30 11:46:12 +010063/* some bookkeeping */
64static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
65 size_t size)
66{
67 dev_priv->mm.object_count++;
68 dev_priv->mm.object_memory += size;
69}
70
71static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
72 size_t size)
73{
74 dev_priv->mm.object_count--;
75 dev_priv->mm.object_memory -= size;
76}
77
Chris Wilson21dd3732011-01-26 15:55:56 +000078static int
79i915_gem_wait_for_error(struct drm_device *dev)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010080{
81 struct drm_i915_private *dev_priv = dev->dev_private;
82 struct completion *x = &dev_priv->error_completion;
83 unsigned long flags;
84 int ret;
85
86 if (!atomic_read(&dev_priv->mm.wedged))
87 return 0;
88
89 ret = wait_for_completion_interruptible(x);
90 if (ret)
91 return ret;
92
Chris Wilson21dd3732011-01-26 15:55:56 +000093 if (atomic_read(&dev_priv->mm.wedged)) {
94 /* GPU is hung, bump the completion count to account for
95 * the token we just consumed so that we never hit zero and
96 * end up waiting upon a subsequent completion event that
97 * will never happen.
98 */
99 spin_lock_irqsave(&x->wait.lock, flags);
100 x->done++;
101 spin_unlock_irqrestore(&x->wait.lock, flags);
102 }
103 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100104}
105
Chris Wilson54cf91d2010-11-25 18:00:26 +0000106int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100107{
Chris Wilson76c1dec2010-09-25 11:22:51 +0100108 int ret;
109
Chris Wilson21dd3732011-01-26 15:55:56 +0000110 ret = i915_gem_wait_for_error(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100111 if (ret)
112 return ret;
113
114 ret = mutex_lock_interruptible(&dev->struct_mutex);
115 if (ret)
116 return ret;
117
Chris Wilson23bc5982010-09-29 16:10:57 +0100118 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100119 return 0;
120}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100121
Chris Wilson7d1c4802010-08-07 21:45:03 +0100122static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000123i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100124{
Chris Wilson05394f32010-11-08 19:18:58 +0000125 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100126}
127
Chris Wilson20217462010-11-23 15:26:33 +0000128void i915_gem_do_init(struct drm_device *dev,
129 unsigned long start,
130 unsigned long mappable_end,
131 unsigned long end)
Jesse Barnes79e53942008-11-07 14:24:08 -0800132{
133 drm_i915_private_t *dev_priv = dev->dev_private;
134
Chris Wilsonbee4a182011-01-21 10:54:32 +0000135 drm_mm_init(&dev_priv->mm.gtt_space, start, end - start);
Jesse Barnes79e53942008-11-07 14:24:08 -0800136
Chris Wilsonbee4a182011-01-21 10:54:32 +0000137 dev_priv->mm.gtt_start = start;
138 dev_priv->mm.gtt_mappable_end = mappable_end;
139 dev_priv->mm.gtt_end = end;
Chris Wilson73aa8082010-09-30 11:46:12 +0100140 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200141 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Chris Wilsonbee4a182011-01-21 10:54:32 +0000142
143 /* Take over this portion of the GTT */
144 intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
Jesse Barnes79e53942008-11-07 14:24:08 -0800145}
Keith Packard6dbe2772008-10-14 21:41:13 -0700146
Eric Anholt673a3942008-07-30 12:06:12 -0700147int
148i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000149 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700150{
Eric Anholt673a3942008-07-30 12:06:12 -0700151 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000152
153 if (args->gtt_start >= args->gtt_end ||
154 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
155 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700156
157 mutex_lock(&dev->struct_mutex);
Chris Wilson20217462010-11-23 15:26:33 +0000158 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700159 mutex_unlock(&dev->struct_mutex);
160
Chris Wilson20217462010-11-23 15:26:33 +0000161 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700162}
163
Eric Anholt5a125c32008-10-22 21:40:13 -0700164int
165i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000166 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700167{
Chris Wilson73aa8082010-09-30 11:46:12 +0100168 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700169 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000170 struct drm_i915_gem_object *obj;
171 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700172
173 if (!(dev->driver->driver_features & DRIVER_GEM))
174 return -ENODEV;
175
Chris Wilson6299f992010-11-24 12:23:44 +0000176 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100177 mutex_lock(&dev->struct_mutex);
Chris Wilson6299f992010-11-24 12:23:44 +0000178 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
179 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100180 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700181
Chris Wilson6299f992010-11-24 12:23:44 +0000182 args->aper_size = dev_priv->mm.gtt_total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400183 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000184
Eric Anholt5a125c32008-10-22 21:40:13 -0700185 return 0;
186}
187
Dave Airlieff72145b2011-02-07 12:16:14 +1000188static int
189i915_gem_create(struct drm_file *file,
190 struct drm_device *dev,
191 uint64_t size,
192 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700193{
Chris Wilson05394f32010-11-08 19:18:58 +0000194 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300195 int ret;
196 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700197
Dave Airlieff72145b2011-02-07 12:16:14 +1000198 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200199 if (size == 0)
200 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700201
202 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000203 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700204 if (obj == NULL)
205 return -ENOMEM;
206
Chris Wilson05394f32010-11-08 19:18:58 +0000207 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100208 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000209 drm_gem_object_release(&obj->base);
210 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100211 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700212 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100213 }
214
Chris Wilson202f2fe2010-10-14 13:20:40 +0100215 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000216 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100217 trace_i915_gem_object_create(obj);
218
Dave Airlieff72145b2011-02-07 12:16:14 +1000219 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700220 return 0;
221}
222
Dave Airlieff72145b2011-02-07 12:16:14 +1000223int
224i915_gem_dumb_create(struct drm_file *file,
225 struct drm_device *dev,
226 struct drm_mode_create_dumb *args)
227{
228 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000229 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000230 args->size = args->pitch * args->height;
231 return i915_gem_create(file, dev,
232 args->size, &args->handle);
233}
234
235int i915_gem_dumb_destroy(struct drm_file *file,
236 struct drm_device *dev,
237 uint32_t handle)
238{
239 return drm_gem_handle_delete(file, handle);
240}
241
242/**
243 * Creates a new mm object and returns a handle to it.
244 */
245int
246i915_gem_create_ioctl(struct drm_device *dev, void *data,
247 struct drm_file *file)
248{
249 struct drm_i915_gem_create *args = data;
250 return i915_gem_create(file, dev,
251 args->size, &args->handle);
252}
253
Chris Wilson05394f32010-11-08 19:18:58 +0000254static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700255{
Chris Wilson05394f32010-11-08 19:18:58 +0000256 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700257
258 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000259 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700260}
261
Chris Wilson99a03df2010-05-27 14:15:34 +0100262static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700263slow_shmem_copy(struct page *dst_page,
264 int dst_offset,
265 struct page *src_page,
266 int src_offset,
267 int length)
268{
269 char *dst_vaddr, *src_vaddr;
270
Chris Wilson99a03df2010-05-27 14:15:34 +0100271 dst_vaddr = kmap(dst_page);
272 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700273
274 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
275
Chris Wilson99a03df2010-05-27 14:15:34 +0100276 kunmap(src_page);
277 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700278}
279
Chris Wilson99a03df2010-05-27 14:15:34 +0100280static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700281slow_shmem_bit17_copy(struct page *gpu_page,
282 int gpu_offset,
283 struct page *cpu_page,
284 int cpu_offset,
285 int length,
286 int is_read)
287{
288 char *gpu_vaddr, *cpu_vaddr;
289
290 /* Use the unswizzled path if this page isn't affected. */
291 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
292 if (is_read)
293 return slow_shmem_copy(cpu_page, cpu_offset,
294 gpu_page, gpu_offset, length);
295 else
296 return slow_shmem_copy(gpu_page, gpu_offset,
297 cpu_page, cpu_offset, length);
298 }
299
Chris Wilson99a03df2010-05-27 14:15:34 +0100300 gpu_vaddr = kmap(gpu_page);
301 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700302
303 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
304 * XORing with the other bits (A9 for Y, A9 and A10 for X)
305 */
306 while (length > 0) {
307 int cacheline_end = ALIGN(gpu_offset + 1, 64);
308 int this_length = min(cacheline_end - gpu_offset, length);
309 int swizzled_gpu_offset = gpu_offset ^ 64;
310
311 if (is_read) {
312 memcpy(cpu_vaddr + cpu_offset,
313 gpu_vaddr + swizzled_gpu_offset,
314 this_length);
315 } else {
316 memcpy(gpu_vaddr + swizzled_gpu_offset,
317 cpu_vaddr + cpu_offset,
318 this_length);
319 }
320 cpu_offset += this_length;
321 gpu_offset += this_length;
322 length -= this_length;
323 }
324
Chris Wilson99a03df2010-05-27 14:15:34 +0100325 kunmap(cpu_page);
326 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700327}
328
Eric Anholt673a3942008-07-30 12:06:12 -0700329/**
Eric Anholteb014592009-03-10 11:44:52 -0700330 * This is the fast shmem pread path, which attempts to copy_from_user directly
331 * from the backing pages of the object to the user's address space. On a
332 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
333 */
334static int
Chris Wilson05394f32010-11-08 19:18:58 +0000335i915_gem_shmem_pread_fast(struct drm_device *dev,
336 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700337 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000338 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700339{
Chris Wilson05394f32010-11-08 19:18:58 +0000340 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700341 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100342 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700343 char __user *user_data;
344 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700345
346 user_data = (char __user *) (uintptr_t) args->data_ptr;
347 remain = args->size;
348
Eric Anholteb014592009-03-10 11:44:52 -0700349 offset = args->offset;
350
351 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100352 struct page *page;
353 char *vaddr;
354 int ret;
355
Eric Anholteb014592009-03-10 11:44:52 -0700356 /* Operation in this page
357 *
Eric Anholteb014592009-03-10 11:44:52 -0700358 * page_offset = offset within page
359 * page_length = bytes to copy for this page
360 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100361 page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700362 page_length = remain;
363 if ((page_offset + remain) > PAGE_SIZE)
364 page_length = PAGE_SIZE - page_offset;
365
Hugh Dickins5949eac2011-06-27 16:18:18 -0700366 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100367 if (IS_ERR(page))
368 return PTR_ERR(page);
369
370 vaddr = kmap_atomic(page);
371 ret = __copy_to_user_inatomic(user_data,
372 vaddr + page_offset,
373 page_length);
374 kunmap_atomic(vaddr);
375
376 mark_page_accessed(page);
377 page_cache_release(page);
378 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100379 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700380
381 remain -= page_length;
382 user_data += page_length;
383 offset += page_length;
384 }
385
Chris Wilson4f27b752010-10-14 15:26:45 +0100386 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700387}
388
Daniel Vetter8c599672011-12-14 13:57:31 +0100389static inline int
390__copy_from_user_swizzled(char __user *gpu_vaddr, int gpu_offset,
391 const char *cpu_vaddr,
392 int length)
393{
394 int ret, cpu_offset = 0;
395
396 while (length > 0) {
397 int cacheline_end = ALIGN(gpu_offset + 1, 64);
398 int this_length = min(cacheline_end - gpu_offset, length);
399 int swizzled_gpu_offset = gpu_offset ^ 64;
400
401 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
402 cpu_vaddr + cpu_offset,
403 this_length);
404 if (ret)
405 return ret + length;
406
407 cpu_offset += this_length;
408 gpu_offset += this_length;
409 length -= this_length;
410 }
411
412 return 0;
413}
414
Eric Anholteb014592009-03-10 11:44:52 -0700415/**
416 * This is the fallback shmem pread path, which allocates temporary storage
417 * in kernel space to copy_to_user into outside of the struct_mutex, so we
418 * can copy out of the object's backing pages while holding the struct mutex
419 * and not take page faults.
420 */
421static int
Chris Wilson05394f32010-11-08 19:18:58 +0000422i915_gem_shmem_pread_slow(struct drm_device *dev,
423 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700424 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000425 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700426{
Chris Wilson05394f32010-11-08 19:18:58 +0000427 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700428 struct mm_struct *mm = current->mm;
429 struct page **user_pages;
430 ssize_t remain;
431 loff_t offset, pinned_pages, i;
432 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100433 int shmem_page_offset;
434 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700435 int page_length;
436 int ret;
437 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700438 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700439
440 remain = args->size;
441
442 /* Pin the user pages containing the data. We can't fault while
443 * holding the struct mutex, yet we want to hold it while
444 * dereferencing the user data.
445 */
446 first_data_page = data_ptr / PAGE_SIZE;
447 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
448 num_pages = last_data_page - first_data_page + 1;
449
Chris Wilson4f27b752010-10-14 15:26:45 +0100450 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700451 if (user_pages == NULL)
452 return -ENOMEM;
453
Chris Wilson4f27b752010-10-14 15:26:45 +0100454 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700455 down_read(&mm->mmap_sem);
456 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700457 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700458 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100459 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700460 if (pinned_pages < num_pages) {
461 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100462 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700463 }
464
Chris Wilson4f27b752010-10-14 15:26:45 +0100465 ret = i915_gem_object_set_cpu_read_domain_range(obj,
466 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700467 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100468 if (ret)
469 goto out;
470
471 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700472
Eric Anholteb014592009-03-10 11:44:52 -0700473 offset = args->offset;
474
475 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100476 struct page *page;
477
Eric Anholteb014592009-03-10 11:44:52 -0700478 /* Operation in this page
479 *
Eric Anholteb014592009-03-10 11:44:52 -0700480 * shmem_page_offset = offset within page in shmem file
481 * data_page_index = page number in get_user_pages return
482 * data_page_offset = offset with data_page_index page.
483 * page_length = bytes to copy for this page
484 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100485 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700486 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100487 data_page_offset = offset_in_page(data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700488
489 page_length = remain;
490 if ((shmem_page_offset + page_length) > PAGE_SIZE)
491 page_length = PAGE_SIZE - shmem_page_offset;
492 if ((data_page_offset + page_length) > PAGE_SIZE)
493 page_length = PAGE_SIZE - data_page_offset;
494
Hugh Dickins5949eac2011-06-27 16:18:18 -0700495 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Jesper Juhlb65552f2011-06-12 20:53:44 +0000496 if (IS_ERR(page)) {
497 ret = PTR_ERR(page);
498 goto out;
499 }
Chris Wilsone5281cc2010-10-28 13:45:36 +0100500
Eric Anholt280b7132009-03-12 16:56:27 -0700501 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100502 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700503 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100504 user_pages[data_page_index],
505 data_page_offset,
506 page_length,
507 1);
508 } else {
509 slow_shmem_copy(user_pages[data_page_index],
510 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100511 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100512 shmem_page_offset,
513 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700514 }
Eric Anholteb014592009-03-10 11:44:52 -0700515
Chris Wilsone5281cc2010-10-28 13:45:36 +0100516 mark_page_accessed(page);
517 page_cache_release(page);
518
Eric Anholteb014592009-03-10 11:44:52 -0700519 remain -= page_length;
520 data_ptr += page_length;
521 offset += page_length;
522 }
523
Chris Wilson4f27b752010-10-14 15:26:45 +0100524out:
Eric Anholteb014592009-03-10 11:44:52 -0700525 for (i = 0; i < pinned_pages; i++) {
526 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100527 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700528 page_cache_release(user_pages[i]);
529 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700530 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700531
532 return ret;
533}
534
Eric Anholt673a3942008-07-30 12:06:12 -0700535/**
536 * Reads data from the object referenced by handle.
537 *
538 * On error, the contents of *data are undefined.
539 */
540int
541i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000542 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700543{
544 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000545 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100546 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700547
Chris Wilson51311d02010-11-17 09:10:42 +0000548 if (args->size == 0)
549 return 0;
550
551 if (!access_ok(VERIFY_WRITE,
552 (char __user *)(uintptr_t)args->data_ptr,
553 args->size))
554 return -EFAULT;
555
556 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
557 args->size);
558 if (ret)
559 return -EFAULT;
560
Chris Wilson4f27b752010-10-14 15:26:45 +0100561 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100562 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100563 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700564
Chris Wilson05394f32010-11-08 19:18:58 +0000565 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000566 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100567 ret = -ENOENT;
568 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100569 }
Eric Anholt673a3942008-07-30 12:06:12 -0700570
Chris Wilson7dcd2492010-09-26 20:21:44 +0100571 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000572 if (args->offset > obj->base.size ||
573 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100574 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100575 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100576 }
577
Chris Wilsondb53a302011-02-03 11:57:46 +0000578 trace_i915_gem_object_pread(obj, args->offset, args->size);
579
Chris Wilson4f27b752010-10-14 15:26:45 +0100580 ret = i915_gem_object_set_cpu_read_domain_range(obj,
581 args->offset,
582 args->size);
583 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100584 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100585
586 ret = -EFAULT;
587 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000588 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100589 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000590 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700591
Chris Wilson35b62a82010-09-26 20:23:38 +0100592out:
Chris Wilson05394f32010-11-08 19:18:58 +0000593 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100594unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100595 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700596 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700597}
598
Keith Packard0839ccb2008-10-30 19:38:48 -0700599/* This is the fast write path which cannot handle
600 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700601 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700602
Keith Packard0839ccb2008-10-30 19:38:48 -0700603static inline int
604fast_user_write(struct io_mapping *mapping,
605 loff_t page_base, int page_offset,
606 char __user *user_data,
607 int length)
608{
609 char *vaddr_atomic;
610 unsigned long unwritten;
611
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700612 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700613 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
614 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700615 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100616 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700617}
618
619/* Here's the write path which can sleep for
620 * page faults
621 */
622
Chris Wilsonab34c222010-05-27 14:15:35 +0100623static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700624slow_kernel_write(struct io_mapping *mapping,
625 loff_t gtt_base, int gtt_offset,
626 struct page *user_page, int user_offset,
627 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700628{
Chris Wilsonab34c222010-05-27 14:15:35 +0100629 char __iomem *dst_vaddr;
630 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700631
Chris Wilsonab34c222010-05-27 14:15:35 +0100632 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
633 src_vaddr = kmap(user_page);
634
635 memcpy_toio(dst_vaddr + gtt_offset,
636 src_vaddr + user_offset,
637 length);
638
639 kunmap(user_page);
640 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700641}
642
Eric Anholt3de09aa2009-03-09 09:42:23 -0700643/**
644 * This is the fast pwrite path, where we copy the data directly from the
645 * user into the GTT, uncached.
646 */
Eric Anholt673a3942008-07-30 12:06:12 -0700647static int
Chris Wilson05394f32010-11-08 19:18:58 +0000648i915_gem_gtt_pwrite_fast(struct drm_device *dev,
649 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700650 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000651 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700652{
Keith Packard0839ccb2008-10-30 19:38:48 -0700653 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700654 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700655 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700656 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700657 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700658
659 user_data = (char __user *) (uintptr_t) args->data_ptr;
660 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700661
Chris Wilson05394f32010-11-08 19:18:58 +0000662 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700663
664 while (remain > 0) {
665 /* Operation in this page
666 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700667 * page_base = page offset within aperture
668 * page_offset = offset within page
669 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700670 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100671 page_base = offset & PAGE_MASK;
672 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700673 page_length = remain;
674 if ((page_offset + remain) > PAGE_SIZE)
675 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700676
Keith Packard0839ccb2008-10-30 19:38:48 -0700677 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700678 * source page isn't available. Return the error and we'll
679 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700680 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100681 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
682 page_offset, user_data, page_length))
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100683 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700684
Keith Packard0839ccb2008-10-30 19:38:48 -0700685 remain -= page_length;
686 user_data += page_length;
687 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700688 }
Eric Anholt673a3942008-07-30 12:06:12 -0700689
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100690 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700691}
692
Eric Anholt3de09aa2009-03-09 09:42:23 -0700693/**
694 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
695 * the memory and maps it using kmap_atomic for copying.
696 *
697 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
698 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
699 */
Eric Anholt3043c602008-10-02 12:24:47 -0700700static int
Chris Wilson05394f32010-11-08 19:18:58 +0000701i915_gem_gtt_pwrite_slow(struct drm_device *dev,
702 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700703 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000704 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700705{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700706 drm_i915_private_t *dev_priv = dev->dev_private;
707 ssize_t remain;
708 loff_t gtt_page_base, offset;
709 loff_t first_data_page, last_data_page, num_pages;
710 loff_t pinned_pages, i;
711 struct page **user_pages;
712 struct mm_struct *mm = current->mm;
713 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700714 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700715 uint64_t data_ptr = args->data_ptr;
716
717 remain = args->size;
718
719 /* Pin the user pages containing the data. We can't fault while
720 * holding the struct mutex, and all of the pwrite implementations
721 * want to hold it while dereferencing the user data.
722 */
723 first_data_page = data_ptr / PAGE_SIZE;
724 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
725 num_pages = last_data_page - first_data_page + 1;
726
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100727 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700728 if (user_pages == NULL)
729 return -ENOMEM;
730
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100731 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700732 down_read(&mm->mmap_sem);
733 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
734 num_pages, 0, 0, user_pages, NULL);
735 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100736 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700737 if (pinned_pages < num_pages) {
738 ret = -EFAULT;
739 goto out_unpin_pages;
740 }
741
Chris Wilsond9e86c02010-11-10 16:40:20 +0000742 ret = i915_gem_object_set_to_gtt_domain(obj, true);
743 if (ret)
744 goto out_unpin_pages;
745
746 ret = i915_gem_object_put_fence(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700747 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100748 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700749
Chris Wilson05394f32010-11-08 19:18:58 +0000750 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700751
752 while (remain > 0) {
753 /* Operation in this page
754 *
755 * gtt_page_base = page offset within aperture
756 * gtt_page_offset = offset within page in aperture
757 * data_page_index = page number in get_user_pages return
758 * data_page_offset = offset with data_page_index page.
759 * page_length = bytes to copy for this page
760 */
761 gtt_page_base = offset & PAGE_MASK;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100762 gtt_page_offset = offset_in_page(offset);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700763 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100764 data_page_offset = offset_in_page(data_ptr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700765
766 page_length = remain;
767 if ((gtt_page_offset + page_length) > PAGE_SIZE)
768 page_length = PAGE_SIZE - gtt_page_offset;
769 if ((data_page_offset + page_length) > PAGE_SIZE)
770 page_length = PAGE_SIZE - data_page_offset;
771
Chris Wilsonab34c222010-05-27 14:15:35 +0100772 slow_kernel_write(dev_priv->mm.gtt_mapping,
773 gtt_page_base, gtt_page_offset,
774 user_pages[data_page_index],
775 data_page_offset,
776 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700777
778 remain -= page_length;
779 offset += page_length;
780 data_ptr += page_length;
781 }
782
Eric Anholt3de09aa2009-03-09 09:42:23 -0700783out_unpin_pages:
784 for (i = 0; i < pinned_pages; i++)
785 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700786 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700787
788 return ret;
789}
790
Eric Anholt40123c12009-03-09 13:42:30 -0700791/**
792 * This is the fast shmem pwrite path, which attempts to directly
793 * copy_from_user into the kmapped pages backing the object.
794 */
Eric Anholt673a3942008-07-30 12:06:12 -0700795static int
Chris Wilson05394f32010-11-08 19:18:58 +0000796i915_gem_shmem_pwrite_fast(struct drm_device *dev,
797 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700798 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000799 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700800{
Chris Wilson05394f32010-11-08 19:18:58 +0000801 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700802 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100803 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700804 char __user *user_data;
805 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700806
807 user_data = (char __user *) (uintptr_t) args->data_ptr;
808 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700809
Eric Anholt673a3942008-07-30 12:06:12 -0700810 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000811 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700812
Eric Anholt40123c12009-03-09 13:42:30 -0700813 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100814 struct page *page;
815 char *vaddr;
816 int ret;
817
Eric Anholt40123c12009-03-09 13:42:30 -0700818 /* Operation in this page
819 *
Eric Anholt40123c12009-03-09 13:42:30 -0700820 * page_offset = offset within page
821 * page_length = bytes to copy for this page
822 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100823 page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700824 page_length = remain;
825 if ((page_offset + remain) > PAGE_SIZE)
826 page_length = PAGE_SIZE - page_offset;
827
Hugh Dickins5949eac2011-06-27 16:18:18 -0700828 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100829 if (IS_ERR(page))
830 return PTR_ERR(page);
831
Daniel Vetter130c2562011-09-17 20:55:46 +0200832 vaddr = kmap_atomic(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100833 ret = __copy_from_user_inatomic(vaddr + page_offset,
834 user_data,
835 page_length);
Daniel Vetter130c2562011-09-17 20:55:46 +0200836 kunmap_atomic(vaddr);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100837
838 set_page_dirty(page);
839 mark_page_accessed(page);
840 page_cache_release(page);
841
842 /* If we get a fault while copying data, then (presumably) our
843 * source page isn't available. Return the error and we'll
844 * retry in the slow path.
845 */
846 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100847 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700848
849 remain -= page_length;
850 user_data += page_length;
851 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700852 }
853
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100854 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700855}
856
857/**
858 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
859 * the memory and maps it using kmap_atomic for copying.
860 *
861 * This avoids taking mmap_sem for faulting on the user's address while the
862 * struct_mutex is held.
863 */
864static int
Chris Wilson05394f32010-11-08 19:18:58 +0000865i915_gem_shmem_pwrite_slow(struct drm_device *dev,
866 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700867 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000868 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700869{
Chris Wilson05394f32010-11-08 19:18:58 +0000870 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700871 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100872 loff_t offset;
873 char __user *user_data;
874 int shmem_page_offset, page_length, ret;
875 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700876
Daniel Vetter8c599672011-12-14 13:57:31 +0100877 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholt40123c12009-03-09 13:42:30 -0700878 remain = args->size;
879
Daniel Vetter8c599672011-12-14 13:57:31 +0100880 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700881
Eric Anholt40123c12009-03-09 13:42:30 -0700882 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000883 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700884
Daniel Vetter8c599672011-12-14 13:57:31 +0100885 mutex_unlock(&dev->struct_mutex);
886
Eric Anholt40123c12009-03-09 13:42:30 -0700887 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100888 struct page *page;
Daniel Vetter8c599672011-12-14 13:57:31 +0100889 char *vaddr;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100890
Eric Anholt40123c12009-03-09 13:42:30 -0700891 /* Operation in this page
892 *
Eric Anholt40123c12009-03-09 13:42:30 -0700893 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700894 * page_length = bytes to copy for this page
895 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100896 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700897
898 page_length = remain;
899 if ((shmem_page_offset + page_length) > PAGE_SIZE)
900 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700901
Hugh Dickins5949eac2011-06-27 16:18:18 -0700902 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100903 if (IS_ERR(page)) {
904 ret = PTR_ERR(page);
905 goto out;
906 }
907
Daniel Vetter8c599672011-12-14 13:57:31 +0100908 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
909 (page_to_phys(page) & (1 << 17)) != 0;
910
911 vaddr = kmap(page);
912 if (page_do_bit17_swizzling)
913 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
914 user_data,
915 page_length);
916 else
917 ret = __copy_from_user(vaddr + shmem_page_offset,
918 user_data,
919 page_length);
920 kunmap(page);
Eric Anholt40123c12009-03-09 13:42:30 -0700921
Chris Wilsone5281cc2010-10-28 13:45:36 +0100922 set_page_dirty(page);
923 mark_page_accessed(page);
924 page_cache_release(page);
925
Daniel Vetter8c599672011-12-14 13:57:31 +0100926 if (ret) {
927 ret = -EFAULT;
928 goto out;
929 }
930
Eric Anholt40123c12009-03-09 13:42:30 -0700931 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100932 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700933 offset += page_length;
934 }
935
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100936out:
Daniel Vetter8c599672011-12-14 13:57:31 +0100937 mutex_lock(&dev->struct_mutex);
938 /* Fixup: Kill any reinstated backing storage pages */
939 if (obj->madv == __I915_MADV_PURGED)
940 i915_gem_object_truncate(obj);
941 /* and flush dirty cachelines in case the object isn't in the cpu write
942 * domain anymore. */
943 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
944 i915_gem_clflush_object(obj);
945 intel_gtt_chipset_flush();
946 }
Eric Anholt40123c12009-03-09 13:42:30 -0700947
948 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700949}
950
951/**
952 * Writes data to the object referenced by handle.
953 *
954 * On error, the contents of the buffer that were to be modified are undefined.
955 */
956int
957i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100958 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700959{
960 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000961 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000962 int ret;
963
964 if (args->size == 0)
965 return 0;
966
967 if (!access_ok(VERIFY_READ,
968 (char __user *)(uintptr_t)args->data_ptr,
969 args->size))
970 return -EFAULT;
971
972 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
973 args->size);
974 if (ret)
975 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700976
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100977 ret = i915_mutex_lock_interruptible(dev);
978 if (ret)
979 return ret;
980
Chris Wilson05394f32010-11-08 19:18:58 +0000981 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000982 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100983 ret = -ENOENT;
984 goto unlock;
985 }
Eric Anholt673a3942008-07-30 12:06:12 -0700986
Chris Wilson7dcd2492010-09-26 20:21:44 +0100987 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000988 if (args->offset > obj->base.size ||
989 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100990 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100991 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100992 }
993
Chris Wilsondb53a302011-02-03 11:57:46 +0000994 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
995
Eric Anholt673a3942008-07-30 12:06:12 -0700996 /* We can only do the GTT pwrite on untiled buffers, as otherwise
997 * it would end up going through the fenced access, and we'll get
998 * different detiling behavior between reading and writing.
999 * pread/pwrite currently are reading and writing from the CPU
1000 * perspective, requiring manual detiling by the client.
1001 */
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001002 if (obj->phys_obj) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001003 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001004 goto out;
1005 }
1006
1007 if (obj->gtt_space &&
1008 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001009 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001010 if (ret)
1011 goto out;
1012
Chris Wilsond9e86c02010-11-10 16:40:20 +00001013 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1014 if (ret)
1015 goto out_unpin;
1016
1017 ret = i915_gem_object_put_fence(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001018 if (ret)
1019 goto out_unpin;
1020
1021 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1022 if (ret == -EFAULT)
1023 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1024
1025out_unpin:
1026 i915_gem_object_unpin(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001027
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001028 if (ret != -EFAULT)
1029 goto out;
1030 /* Fall through to the shmfs paths because the gtt paths might
1031 * fail with non-page-backed user pointers (e.g. gtt mappings
1032 * when moving data between textures). */
Eric Anholt40123c12009-03-09 13:42:30 -07001033 }
Eric Anholt673a3942008-07-30 12:06:12 -07001034
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001035 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1036 if (ret)
1037 goto out;
1038
1039 ret = -EFAULT;
1040 if (!i915_gem_object_needs_bit17_swizzle(obj))
1041 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1042 if (ret == -EFAULT)
1043 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
1044
Chris Wilson35b62a82010-09-26 20:23:38 +01001045out:
Chris Wilson05394f32010-11-08 19:18:58 +00001046 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001047unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001048 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001049 return ret;
1050}
1051
1052/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001053 * Called when user space prepares to use an object with the CPU, either
1054 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001055 */
1056int
1057i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001058 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001059{
1060 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001061 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001062 uint32_t read_domains = args->read_domains;
1063 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001064 int ret;
1065
1066 if (!(dev->driver->driver_features & DRIVER_GEM))
1067 return -ENODEV;
1068
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001069 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001070 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001071 return -EINVAL;
1072
Chris Wilson21d509e2009-06-06 09:46:02 +01001073 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001074 return -EINVAL;
1075
1076 /* Having something in the write domain implies it's in the read
1077 * domain, and only that read domain. Enforce that in the request.
1078 */
1079 if (write_domain != 0 && read_domains != write_domain)
1080 return -EINVAL;
1081
Chris Wilson76c1dec2010-09-25 11:22:51 +01001082 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001083 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001084 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001085
Chris Wilson05394f32010-11-08 19:18:58 +00001086 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001087 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001088 ret = -ENOENT;
1089 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001090 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001091
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001092 if (read_domains & I915_GEM_DOMAIN_GTT) {
1093 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001094
1095 /* Silently promote "you're not bound, there was nothing to do"
1096 * to success, since the client was just asking us to
1097 * make sure everything was done.
1098 */
1099 if (ret == -EINVAL)
1100 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001101 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001102 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001103 }
1104
Chris Wilson05394f32010-11-08 19:18:58 +00001105 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001106unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001107 mutex_unlock(&dev->struct_mutex);
1108 return ret;
1109}
1110
1111/**
1112 * Called when user space has done writes to this buffer
1113 */
1114int
1115i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001116 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001117{
1118 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001119 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001120 int ret = 0;
1121
1122 if (!(dev->driver->driver_features & DRIVER_GEM))
1123 return -ENODEV;
1124
Chris Wilson76c1dec2010-09-25 11:22:51 +01001125 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001126 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001127 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001128
Chris Wilson05394f32010-11-08 19:18:58 +00001129 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001130 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001131 ret = -ENOENT;
1132 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001133 }
1134
Eric Anholt673a3942008-07-30 12:06:12 -07001135 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001136 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001137 i915_gem_object_flush_cpu_write_domain(obj);
1138
Chris Wilson05394f32010-11-08 19:18:58 +00001139 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001140unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001141 mutex_unlock(&dev->struct_mutex);
1142 return ret;
1143}
1144
1145/**
1146 * Maps the contents of an object, returning the address it is mapped
1147 * into.
1148 *
1149 * While the mapping holds a reference on the contents of the object, it doesn't
1150 * imply a ref on the object itself.
1151 */
1152int
1153i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001154 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001155{
1156 struct drm_i915_gem_mmap *args = data;
1157 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001158 unsigned long addr;
1159
1160 if (!(dev->driver->driver_features & DRIVER_GEM))
1161 return -ENODEV;
1162
Chris Wilson05394f32010-11-08 19:18:58 +00001163 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001164 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001165 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001166
Eric Anholt673a3942008-07-30 12:06:12 -07001167 down_write(&current->mm->mmap_sem);
1168 addr = do_mmap(obj->filp, 0, args->size,
1169 PROT_READ | PROT_WRITE, MAP_SHARED,
1170 args->offset);
1171 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001172 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001173 if (IS_ERR((void *)addr))
1174 return addr;
1175
1176 args->addr_ptr = (uint64_t) addr;
1177
1178 return 0;
1179}
1180
Jesse Barnesde151cf2008-11-12 10:03:55 -08001181/**
1182 * i915_gem_fault - fault a page into the GTT
1183 * vma: VMA in question
1184 * vmf: fault info
1185 *
1186 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1187 * from userspace. The fault handler takes care of binding the object to
1188 * the GTT (if needed), allocating and programming a fence register (again,
1189 * only if needed based on whether the old reg is still valid or the object
1190 * is tiled) and inserting a new PTE into the faulting process.
1191 *
1192 * Note that the faulting process may involve evicting existing objects
1193 * from the GTT and/or fence registers to make room. So performance may
1194 * suffer if the GTT working set is large or there are few fence registers
1195 * left.
1196 */
1197int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1198{
Chris Wilson05394f32010-11-08 19:18:58 +00001199 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1200 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001201 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001202 pgoff_t page_offset;
1203 unsigned long pfn;
1204 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001205 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001206
1207 /* We don't use vmf->pgoff since that has the fake offset */
1208 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1209 PAGE_SHIFT;
1210
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001211 ret = i915_mutex_lock_interruptible(dev);
1212 if (ret)
1213 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001214
Chris Wilsondb53a302011-02-03 11:57:46 +00001215 trace_i915_gem_object_fault(obj, page_offset, true, write);
1216
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001217 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001218 if (!obj->map_and_fenceable) {
1219 ret = i915_gem_object_unbind(obj);
1220 if (ret)
1221 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001222 }
Chris Wilson05394f32010-11-08 19:18:58 +00001223 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001224 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001225 if (ret)
1226 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001227
Eric Anholte92d03b2011-06-14 16:43:09 -07001228 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1229 if (ret)
1230 goto unlock;
1231 }
Chris Wilson4a684a42010-10-28 14:44:08 +01001232
Chris Wilsond9e86c02010-11-10 16:40:20 +00001233 if (obj->tiling_mode == I915_TILING_NONE)
1234 ret = i915_gem_object_put_fence(obj);
1235 else
Chris Wilsonce453d82011-02-21 14:43:56 +00001236 ret = i915_gem_object_get_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001237 if (ret)
1238 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001239
Chris Wilson05394f32010-11-08 19:18:58 +00001240 if (i915_gem_object_is_inactive(obj))
1241 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001242
Chris Wilson6299f992010-11-24 12:23:44 +00001243 obj->fault_mappable = true;
1244
Chris Wilson05394f32010-11-08 19:18:58 +00001245 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001246 page_offset;
1247
1248 /* Finally, remap it using the new GTT offset */
1249 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001250unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001251 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001252out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001253 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001254 case -EIO:
Chris Wilson045e7692010-11-07 09:18:22 +00001255 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001256 /* Give the error handler a chance to run and move the
1257 * objects off the GPU active list. Next time we service the
1258 * fault, we should be able to transition the page into the
1259 * GTT without touching the GPU (and so avoid further
1260 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1261 * with coherency, just lost writes.
1262 */
Chris Wilson045e7692010-11-07 09:18:22 +00001263 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001264 case 0:
1265 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001266 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001267 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001268 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001269 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001270 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001271 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001272 }
1273}
1274
1275/**
Chris Wilson901782b2009-07-10 08:18:50 +01001276 * i915_gem_release_mmap - remove physical page mappings
1277 * @obj: obj in question
1278 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001279 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001280 * relinquish ownership of the pages back to the system.
1281 *
1282 * It is vital that we remove the page mapping if we have mapped a tiled
1283 * object through the GTT and then lose the fence register due to
1284 * resource pressure. Similarly if the object has been moved out of the
1285 * aperture, than pages mapped into userspace must be revoked. Removing the
1286 * mapping will then trigger a page fault on the next user access, allowing
1287 * fixup by i915_gem_fault().
1288 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001289void
Chris Wilson05394f32010-11-08 19:18:58 +00001290i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001291{
Chris Wilson6299f992010-11-24 12:23:44 +00001292 if (!obj->fault_mappable)
1293 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001294
Chris Wilsonf6e47882011-03-20 21:09:12 +00001295 if (obj->base.dev->dev_mapping)
1296 unmap_mapping_range(obj->base.dev->dev_mapping,
1297 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1298 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001299
Chris Wilson6299f992010-11-24 12:23:44 +00001300 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001301}
1302
Chris Wilson92b88ae2010-11-09 11:47:32 +00001303static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001304i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001305{
Chris Wilsone28f8712011-07-18 13:11:49 -07001306 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001307
1308 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001309 tiling_mode == I915_TILING_NONE)
1310 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001311
1312 /* Previous chips need a power-of-two fence region when tiling */
1313 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001314 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001315 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001316 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001317
Chris Wilsone28f8712011-07-18 13:11:49 -07001318 while (gtt_size < size)
1319 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001320
Chris Wilsone28f8712011-07-18 13:11:49 -07001321 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001322}
1323
Jesse Barnesde151cf2008-11-12 10:03:55 -08001324/**
1325 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1326 * @obj: object to check
1327 *
1328 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001329 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001330 */
1331static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001332i915_gem_get_gtt_alignment(struct drm_device *dev,
1333 uint32_t size,
1334 int tiling_mode)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001335{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001336 /*
1337 * Minimum alignment is 4k (GTT page size), but might be greater
1338 * if a fence register is needed for the object.
1339 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001340 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001341 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001342 return 4096;
1343
1344 /*
1345 * Previous chips need to be aligned to the size of the smallest
1346 * fence register that can contain the object.
1347 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001348 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001349}
1350
Daniel Vetter5e783302010-11-14 22:32:36 +01001351/**
1352 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1353 * unfenced object
Chris Wilsone28f8712011-07-18 13:11:49 -07001354 * @dev: the device
1355 * @size: size of the object
1356 * @tiling_mode: tiling mode of the object
Daniel Vetter5e783302010-11-14 22:32:36 +01001357 *
1358 * Return the required GTT alignment for an object, only taking into account
1359 * unfenced tiled surface requirements.
1360 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001361uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001362i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1363 uint32_t size,
1364 int tiling_mode)
Daniel Vetter5e783302010-11-14 22:32:36 +01001365{
Daniel Vetter5e783302010-11-14 22:32:36 +01001366 /*
1367 * Minimum alignment is 4k (GTT page size) for sane hw.
1368 */
1369 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001370 tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001371 return 4096;
1372
Chris Wilsone28f8712011-07-18 13:11:49 -07001373 /* Previous hardware however needs to be aligned to a power-of-two
1374 * tile height. The simplest method for determining this is to reuse
1375 * the power-of-tile object size.
Daniel Vetter5e783302010-11-14 22:32:36 +01001376 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001377 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Daniel Vetter5e783302010-11-14 22:32:36 +01001378}
1379
Jesse Barnesde151cf2008-11-12 10:03:55 -08001380int
Dave Airlieff72145b2011-02-07 12:16:14 +10001381i915_gem_mmap_gtt(struct drm_file *file,
1382 struct drm_device *dev,
1383 uint32_t handle,
1384 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001385{
Chris Wilsonda761a62010-10-27 17:37:08 +01001386 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001387 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001388 int ret;
1389
1390 if (!(dev->driver->driver_features & DRIVER_GEM))
1391 return -ENODEV;
1392
Chris Wilson76c1dec2010-09-25 11:22:51 +01001393 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001394 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001395 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001396
Dave Airlieff72145b2011-02-07 12:16:14 +10001397 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001398 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001399 ret = -ENOENT;
1400 goto unlock;
1401 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001402
Chris Wilson05394f32010-11-08 19:18:58 +00001403 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001404 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001405 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001406 }
1407
Chris Wilson05394f32010-11-08 19:18:58 +00001408 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001409 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001410 ret = -EINVAL;
1411 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001412 }
1413
Chris Wilson05394f32010-11-08 19:18:58 +00001414 if (!obj->base.map_list.map) {
Rob Clarkb464e9a2011-08-10 08:09:08 -05001415 ret = drm_gem_create_mmap_offset(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001416 if (ret)
1417 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001418 }
1419
Dave Airlieff72145b2011-02-07 12:16:14 +10001420 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001421
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001422out:
Chris Wilson05394f32010-11-08 19:18:58 +00001423 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001424unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001425 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001426 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001427}
1428
Dave Airlieff72145b2011-02-07 12:16:14 +10001429/**
1430 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1431 * @dev: DRM device
1432 * @data: GTT mapping ioctl data
1433 * @file: GEM object info
1434 *
1435 * Simply returns the fake offset to userspace so it can mmap it.
1436 * The mmap call will end up in drm_gem_mmap(), which will set things
1437 * up so we can get faults in the handler above.
1438 *
1439 * The fault handler will take care of binding the object into the GTT
1440 * (since it may have been evicted to make room for something), allocating
1441 * a fence register, and mapping the appropriate aperture address into
1442 * userspace.
1443 */
1444int
1445i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1446 struct drm_file *file)
1447{
1448 struct drm_i915_gem_mmap_gtt *args = data;
1449
1450 if (!(dev->driver->driver_features & DRIVER_GEM))
1451 return -ENODEV;
1452
1453 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1454}
1455
1456
Chris Wilsone5281cc2010-10-28 13:45:36 +01001457static int
Chris Wilson05394f32010-11-08 19:18:58 +00001458i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001459 gfp_t gfpmask)
1460{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001461 int page_count, i;
1462 struct address_space *mapping;
1463 struct inode *inode;
1464 struct page *page;
1465
1466 /* Get the list of pages out of our struct file. They'll be pinned
1467 * at this point until we release them.
1468 */
Chris Wilson05394f32010-11-08 19:18:58 +00001469 page_count = obj->base.size / PAGE_SIZE;
1470 BUG_ON(obj->pages != NULL);
1471 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1472 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001473 return -ENOMEM;
1474
Chris Wilson05394f32010-11-08 19:18:58 +00001475 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001476 mapping = inode->i_mapping;
Hugh Dickins5949eac2011-06-27 16:18:18 -07001477 gfpmask |= mapping_gfp_mask(mapping);
1478
Chris Wilsone5281cc2010-10-28 13:45:36 +01001479 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07001480 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001481 if (IS_ERR(page))
1482 goto err_pages;
1483
Chris Wilson05394f32010-11-08 19:18:58 +00001484 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001485 }
1486
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001487 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilsone5281cc2010-10-28 13:45:36 +01001488 i915_gem_object_do_bit_17_swizzle(obj);
1489
1490 return 0;
1491
1492err_pages:
1493 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001494 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001495
Chris Wilson05394f32010-11-08 19:18:58 +00001496 drm_free_large(obj->pages);
1497 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001498 return PTR_ERR(page);
1499}
1500
Chris Wilson5cdf5882010-09-27 15:51:07 +01001501static void
Chris Wilson05394f32010-11-08 19:18:58 +00001502i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001503{
Chris Wilson05394f32010-11-08 19:18:58 +00001504 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001505 int i;
1506
Chris Wilson05394f32010-11-08 19:18:58 +00001507 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001508
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001509 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07001510 i915_gem_object_save_bit_17_swizzle(obj);
1511
Chris Wilson05394f32010-11-08 19:18:58 +00001512 if (obj->madv == I915_MADV_DONTNEED)
1513 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001514
1515 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001516 if (obj->dirty)
1517 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001518
Chris Wilson05394f32010-11-08 19:18:58 +00001519 if (obj->madv == I915_MADV_WILLNEED)
1520 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001521
Chris Wilson05394f32010-11-08 19:18:58 +00001522 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001523 }
Chris Wilson05394f32010-11-08 19:18:58 +00001524 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001525
Chris Wilson05394f32010-11-08 19:18:58 +00001526 drm_free_large(obj->pages);
1527 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001528}
1529
Chris Wilson54cf91d2010-11-25 18:00:26 +00001530void
Chris Wilson05394f32010-11-08 19:18:58 +00001531i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001532 struct intel_ring_buffer *ring,
1533 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001534{
Chris Wilson05394f32010-11-08 19:18:58 +00001535 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001536 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001537
Zou Nan hai852835f2010-05-21 09:08:56 +08001538 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001539 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001540
1541 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001542 if (!obj->active) {
1543 drm_gem_object_reference(&obj->base);
1544 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001545 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001546
Eric Anholt673a3942008-07-30 12:06:12 -07001547 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001548 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1549 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001550
Chris Wilson05394f32010-11-08 19:18:58 +00001551 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001552 if (obj->fenced_gpu_access) {
1553 struct drm_i915_fence_reg *reg;
1554
1555 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1556
1557 obj->last_fenced_seqno = seqno;
1558 obj->last_fenced_ring = ring;
1559
1560 reg = &dev_priv->fence_regs[obj->fence_reg];
1561 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1562 }
1563}
1564
1565static void
1566i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1567{
1568 list_del_init(&obj->ring_list);
1569 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001570}
1571
Eric Anholtce44b0e2008-11-06 16:00:31 -08001572static void
Chris Wilson05394f32010-11-08 19:18:58 +00001573i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001574{
Chris Wilson05394f32010-11-08 19:18:58 +00001575 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001576 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001577
Chris Wilson05394f32010-11-08 19:18:58 +00001578 BUG_ON(!obj->active);
1579 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001580
1581 i915_gem_object_move_off_active(obj);
1582}
1583
1584static void
1585i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1586{
1587 struct drm_device *dev = obj->base.dev;
1588 struct drm_i915_private *dev_priv = dev->dev_private;
1589
1590 if (obj->pin_count != 0)
1591 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1592 else
1593 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1594
1595 BUG_ON(!list_empty(&obj->gpu_write_list));
1596 BUG_ON(!obj->active);
1597 obj->ring = NULL;
1598
1599 i915_gem_object_move_off_active(obj);
1600 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001601
1602 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001603 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001604 drm_gem_object_unreference(&obj->base);
1605
1606 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001607}
Eric Anholt673a3942008-07-30 12:06:12 -07001608
Chris Wilson963b4832009-09-20 23:03:54 +01001609/* Immediately discard the backing storage */
1610static void
Chris Wilson05394f32010-11-08 19:18:58 +00001611i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001612{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001613 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001614
Chris Wilsonae9fed62010-08-07 11:01:30 +01001615 /* Our goal here is to return as much of the memory as
1616 * is possible back to the system as we are called from OOM.
1617 * To do this we must instruct the shmfs to drop all of its
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001618 * backing pages, *now*.
Chris Wilsonae9fed62010-08-07 11:01:30 +01001619 */
Chris Wilson05394f32010-11-08 19:18:58 +00001620 inode = obj->base.filp->f_path.dentry->d_inode;
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001621 shmem_truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001622
Chris Wilson05394f32010-11-08 19:18:58 +00001623 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001624}
1625
1626static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001627i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001628{
Chris Wilson05394f32010-11-08 19:18:58 +00001629 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001630}
1631
Eric Anholt673a3942008-07-30 12:06:12 -07001632static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001633i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1634 uint32_t flush_domains)
Daniel Vetter63560392010-02-19 11:51:59 +01001635{
Chris Wilson05394f32010-11-08 19:18:58 +00001636 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001637
Chris Wilson05394f32010-11-08 19:18:58 +00001638 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001639 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001640 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001641 if (obj->base.write_domain & flush_domains) {
1642 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001643
Chris Wilson05394f32010-11-08 19:18:58 +00001644 obj->base.write_domain = 0;
1645 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001646 i915_gem_object_move_to_active(obj, ring,
Chris Wilsondb53a302011-02-03 11:57:46 +00001647 i915_gem_next_request_seqno(ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001648
Daniel Vetter63560392010-02-19 11:51:59 +01001649 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001650 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001651 old_write_domain);
1652 }
1653 }
1654}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001655
Chris Wilson3cce4692010-10-27 16:11:02 +01001656int
Chris Wilsondb53a302011-02-03 11:57:46 +00001657i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001658 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001659 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001660{
Chris Wilsondb53a302011-02-03 11:57:46 +00001661 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001662 uint32_t seqno;
1663 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001664 int ret;
1665
1666 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001667
Chris Wilson3cce4692010-10-27 16:11:02 +01001668 ret = ring->add_request(ring, &seqno);
1669 if (ret)
1670 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001671
Chris Wilsondb53a302011-02-03 11:57:46 +00001672 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001673
1674 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001675 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001676 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001677 was_empty = list_empty(&ring->request_list);
1678 list_add_tail(&request->list, &ring->request_list);
1679
Chris Wilsondb53a302011-02-03 11:57:46 +00001680 if (file) {
1681 struct drm_i915_file_private *file_priv = file->driver_priv;
1682
Chris Wilson1c255952010-09-26 11:03:27 +01001683 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001684 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001685 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001686 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001687 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001688 }
Eric Anholt673a3942008-07-30 12:06:12 -07001689
Chris Wilsondb53a302011-02-03 11:57:46 +00001690 ring->outstanding_lazy_request = false;
1691
Ben Gamarif65d9422009-09-14 17:48:44 -04001692 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001693 if (i915_enable_hangcheck) {
1694 mod_timer(&dev_priv->hangcheck_timer,
1695 jiffies +
1696 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1697 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001698 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001699 queue_delayed_work(dev_priv->wq,
1700 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001701 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001702 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001703}
1704
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001705static inline void
1706i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001707{
Chris Wilson1c255952010-09-26 11:03:27 +01001708 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001709
Chris Wilson1c255952010-09-26 11:03:27 +01001710 if (!file_priv)
1711 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001712
Chris Wilson1c255952010-09-26 11:03:27 +01001713 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001714 if (request->file_priv) {
1715 list_del(&request->client_list);
1716 request->file_priv = NULL;
1717 }
Chris Wilson1c255952010-09-26 11:03:27 +01001718 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001719}
1720
Chris Wilsondfaae392010-09-22 10:31:52 +01001721static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1722 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001723{
Chris Wilsondfaae392010-09-22 10:31:52 +01001724 while (!list_empty(&ring->request_list)) {
1725 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001726
Chris Wilsondfaae392010-09-22 10:31:52 +01001727 request = list_first_entry(&ring->request_list,
1728 struct drm_i915_gem_request,
1729 list);
1730
1731 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001732 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001733 kfree(request);
1734 }
1735
1736 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001737 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001738
Chris Wilson05394f32010-11-08 19:18:58 +00001739 obj = list_first_entry(&ring->active_list,
1740 struct drm_i915_gem_object,
1741 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001742
Chris Wilson05394f32010-11-08 19:18:58 +00001743 obj->base.write_domain = 0;
1744 list_del_init(&obj->gpu_write_list);
1745 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001746 }
Eric Anholt673a3942008-07-30 12:06:12 -07001747}
1748
Chris Wilson312817a2010-11-22 11:50:11 +00001749static void i915_gem_reset_fences(struct drm_device *dev)
1750{
1751 struct drm_i915_private *dev_priv = dev->dev_private;
1752 int i;
1753
Daniel Vetter4b9de732011-10-09 21:52:02 +02001754 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00001755 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001756 struct drm_i915_gem_object *obj = reg->obj;
1757
1758 if (!obj)
1759 continue;
1760
1761 if (obj->tiling_mode)
1762 i915_gem_release_mmap(obj);
1763
Chris Wilsond9e86c02010-11-10 16:40:20 +00001764 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1765 reg->obj->fenced_gpu_access = false;
1766 reg->obj->last_fenced_seqno = 0;
1767 reg->obj->last_fenced_ring = NULL;
1768 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001769 }
1770}
1771
Chris Wilson069efc12010-09-30 16:53:18 +01001772void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001773{
Chris Wilsondfaae392010-09-22 10:31:52 +01001774 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001775 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001776 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001777
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001778 for (i = 0; i < I915_NUM_RINGS; i++)
1779 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001780
1781 /* Remove anything from the flushing lists. The GPU cache is likely
1782 * to be lost on reset along with the data, so simply move the
1783 * lost bo to the inactive list.
1784 */
1785 while (!list_empty(&dev_priv->mm.flushing_list)) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001786 obj = list_first_entry(&dev_priv->mm.flushing_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001787 struct drm_i915_gem_object,
1788 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001789
Chris Wilson05394f32010-11-08 19:18:58 +00001790 obj->base.write_domain = 0;
1791 list_del_init(&obj->gpu_write_list);
1792 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001793 }
Chris Wilson9375e442010-09-19 12:21:28 +01001794
Chris Wilsondfaae392010-09-22 10:31:52 +01001795 /* Move everything out of the GPU domains to ensure we do any
1796 * necessary invalidation upon reuse.
1797 */
Chris Wilson05394f32010-11-08 19:18:58 +00001798 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001799 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001800 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001801 {
Chris Wilson05394f32010-11-08 19:18:58 +00001802 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001803 }
Chris Wilson069efc12010-09-30 16:53:18 +01001804
1805 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001806 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001807}
1808
1809/**
1810 * This function clears the request list as sequence numbers are passed.
1811 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001812static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001813i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001814{
Eric Anholt673a3942008-07-30 12:06:12 -07001815 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001816 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001817
Chris Wilsondb53a302011-02-03 11:57:46 +00001818 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001819 return;
1820
Chris Wilsondb53a302011-02-03 11:57:46 +00001821 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001822
Chris Wilson78501ea2010-10-27 12:18:21 +01001823 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001824
Chris Wilson076e2c02011-01-21 10:07:18 +00001825 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001826 if (seqno >= ring->sync_seqno[i])
1827 ring->sync_seqno[i] = 0;
1828
Zou Nan hai852835f2010-05-21 09:08:56 +08001829 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001830 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001831
Zou Nan hai852835f2010-05-21 09:08:56 +08001832 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001833 struct drm_i915_gem_request,
1834 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001835
Chris Wilsondfaae392010-09-22 10:31:52 +01001836 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001837 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001838
Chris Wilsondb53a302011-02-03 11:57:46 +00001839 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001840
1841 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001842 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001843 kfree(request);
1844 }
1845
1846 /* Move any buffers on the active list that are no longer referenced
1847 * by the ringbuffer to the flushing/inactive lists as appropriate.
1848 */
1849 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001850 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001851
Akshay Joshi0206e352011-08-16 15:34:10 -04001852 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001853 struct drm_i915_gem_object,
1854 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001855
Chris Wilson05394f32010-11-08 19:18:58 +00001856 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001857 break;
1858
Chris Wilson05394f32010-11-08 19:18:58 +00001859 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001860 i915_gem_object_move_to_flushing(obj);
1861 else
1862 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001863 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001864
Chris Wilsondb53a302011-02-03 11:57:46 +00001865 if (unlikely(ring->trace_irq_seqno &&
1866 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001867 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001868 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001869 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001870
Chris Wilsondb53a302011-02-03 11:57:46 +00001871 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001872}
1873
1874void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001875i915_gem_retire_requests(struct drm_device *dev)
1876{
1877 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001878 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001879
Chris Wilsonbe726152010-07-23 23:18:50 +01001880 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001881 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001882
1883 /* We must be careful that during unbind() we do not
1884 * accidentally infinitely recurse into retire requests.
1885 * Currently:
1886 * retire -> free -> unbind -> wait -> retire_ring
1887 */
Chris Wilson05394f32010-11-08 19:18:58 +00001888 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001889 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001890 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001891 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001892 }
1893
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001894 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilsondb53a302011-02-03 11:57:46 +00001895 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001896}
1897
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001898static void
Eric Anholt673a3942008-07-30 12:06:12 -07001899i915_gem_retire_work_handler(struct work_struct *work)
1900{
1901 drm_i915_private_t *dev_priv;
1902 struct drm_device *dev;
Chris Wilson0a587052011-01-09 21:05:44 +00001903 bool idle;
1904 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001905
1906 dev_priv = container_of(work, drm_i915_private_t,
1907 mm.retire_work.work);
1908 dev = dev_priv->dev;
1909
Chris Wilson891b48c2010-09-29 12:26:37 +01001910 /* Come back later if the device is busy... */
1911 if (!mutex_trylock(&dev->struct_mutex)) {
1912 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1913 return;
1914 }
1915
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001916 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001917
Chris Wilson0a587052011-01-09 21:05:44 +00001918 /* Send a periodic flush down the ring so we don't hold onto GEM
1919 * objects indefinitely.
1920 */
1921 idle = true;
1922 for (i = 0; i < I915_NUM_RINGS; i++) {
1923 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1924
1925 if (!list_empty(&ring->gpu_write_list)) {
1926 struct drm_i915_gem_request *request;
1927 int ret;
1928
Chris Wilsondb53a302011-02-03 11:57:46 +00001929 ret = i915_gem_flush_ring(ring,
1930 0, I915_GEM_GPU_DOMAINS);
Chris Wilson0a587052011-01-09 21:05:44 +00001931 request = kzalloc(sizeof(*request), GFP_KERNEL);
1932 if (ret || request == NULL ||
Chris Wilsondb53a302011-02-03 11:57:46 +00001933 i915_add_request(ring, NULL, request))
Chris Wilson0a587052011-01-09 21:05:44 +00001934 kfree(request);
1935 }
1936
1937 idle &= list_empty(&ring->request_list);
1938 }
1939
1940 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001941 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilson0a587052011-01-09 21:05:44 +00001942
Eric Anholt673a3942008-07-30 12:06:12 -07001943 mutex_unlock(&dev->struct_mutex);
1944}
1945
Chris Wilsondb53a302011-02-03 11:57:46 +00001946/**
1947 * Waits for a sequence number to be signaled, and cleans up the
1948 * request and object lists appropriately for that event.
1949 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001950int
Chris Wilsondb53a302011-02-03 11:57:46 +00001951i915_wait_request(struct intel_ring_buffer *ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001952 uint32_t seqno,
1953 bool do_retire)
Eric Anholt673a3942008-07-30 12:06:12 -07001954{
Chris Wilsondb53a302011-02-03 11:57:46 +00001955 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001956 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001957 int ret = 0;
1958
1959 BUG_ON(seqno == 0);
1960
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001961 if (atomic_read(&dev_priv->mm.wedged)) {
1962 struct completion *x = &dev_priv->error_completion;
1963 bool recovery_complete;
1964 unsigned long flags;
1965
1966 /* Give the error handler a chance to run. */
1967 spin_lock_irqsave(&x->wait.lock, flags);
1968 recovery_complete = x->done > 0;
1969 spin_unlock_irqrestore(&x->wait.lock, flags);
1970
1971 return recovery_complete ? -EIO : -EAGAIN;
1972 }
Ben Gamariffed1d02009-09-14 17:48:41 -04001973
Chris Wilson5d97eb62010-11-10 20:40:02 +00001974 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01001975 struct drm_i915_gem_request *request;
1976
1977 request = kzalloc(sizeof(*request), GFP_KERNEL);
1978 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001979 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01001980
Chris Wilsondb53a302011-02-03 11:57:46 +00001981 ret = i915_add_request(ring, NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +01001982 if (ret) {
1983 kfree(request);
1984 return ret;
1985 }
1986
1987 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01001988 }
1989
Chris Wilson78501ea2010-10-27 12:18:21 +01001990 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001991 if (HAS_PCH_SPLIT(ring->dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001992 ier = I915_READ(DEIER) | I915_READ(GTIER);
1993 else
1994 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001995 if (!ier) {
1996 DRM_ERROR("something (likely vbetool) disabled "
1997 "interrupts, re-enabling\n");
Chris Wilsonf01c22f2011-06-28 11:48:51 +01001998 ring->dev->driver->irq_preinstall(ring->dev);
1999 ring->dev->driver->irq_postinstall(ring->dev);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002000 }
2001
Chris Wilsondb53a302011-02-03 11:57:46 +00002002 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002003
Chris Wilsonb2223492010-10-27 15:27:33 +01002004 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002005 if (ring->irq_get(ring)) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002006 if (dev_priv->mm.interruptible)
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002007 ret = wait_event_interruptible(ring->irq_queue,
2008 i915_seqno_passed(ring->get_seqno(ring), seqno)
2009 || atomic_read(&dev_priv->mm.wedged));
2010 else
2011 wait_event(ring->irq_queue,
2012 i915_seqno_passed(ring->get_seqno(ring), seqno)
2013 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002014
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002015 ring->irq_put(ring);
Eric Anholte959b5d2011-12-22 14:55:01 -08002016 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
2017 seqno) ||
2018 atomic_read(&dev_priv->mm.wedged), 3000))
Chris Wilsonb5ba1772010-12-14 12:17:15 +00002019 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01002020 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002021
Chris Wilsondb53a302011-02-03 11:57:46 +00002022 trace_i915_gem_request_wait_end(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002023 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002024 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002025 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002026
2027 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002028 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002029 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002030 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002031
2032 /* Directly dispatch request retiring. While we have the work queue
2033 * to handle this, the waiter on a request often wants an associated
2034 * buffer to have made it to the inactive list, and we would need
2035 * a separate wait queue to handle that.
2036 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002037 if (ret == 0 && do_retire)
Chris Wilsondb53a302011-02-03 11:57:46 +00002038 i915_gem_retire_requests_ring(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002039
2040 return ret;
2041}
2042
Daniel Vetter48764bf2009-09-15 22:57:32 +02002043/**
Eric Anholt673a3942008-07-30 12:06:12 -07002044 * Ensures that all rendering to the object has completed and the object is
2045 * safe to unbind from the GTT or access from the CPU.
2046 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00002047int
Chris Wilsonce453d82011-02-21 14:43:56 +00002048i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002049{
Eric Anholt673a3942008-07-30 12:06:12 -07002050 int ret;
2051
Eric Anholte47c68e2008-11-14 13:35:19 -08002052 /* This function only exists to support waiting for existing rendering,
2053 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002054 */
Chris Wilson05394f32010-11-08 19:18:58 +00002055 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002056
2057 /* If there is rendering queued on the buffer being evicted, wait for
2058 * it.
2059 */
Chris Wilson05394f32010-11-08 19:18:58 +00002060 if (obj->active) {
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002061 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno,
2062 true);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002063 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002064 return ret;
2065 }
2066
2067 return 0;
2068}
2069
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002070static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2071{
2072 u32 old_write_domain, old_read_domains;
2073
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002074 /* Act a barrier for all accesses through the GTT */
2075 mb();
2076
2077 /* Force a pagefault for domain tracking on next user access */
2078 i915_gem_release_mmap(obj);
2079
Keith Packardb97c3d92011-06-24 21:02:59 -07002080 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2081 return;
2082
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002083 old_read_domains = obj->base.read_domains;
2084 old_write_domain = obj->base.write_domain;
2085
2086 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2087 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2088
2089 trace_i915_gem_object_change_domain(obj,
2090 old_read_domains,
2091 old_write_domain);
2092}
2093
Eric Anholt673a3942008-07-30 12:06:12 -07002094/**
2095 * Unbinds an object from the GTT aperture.
2096 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002097int
Chris Wilson05394f32010-11-08 19:18:58 +00002098i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002099{
Eric Anholt673a3942008-07-30 12:06:12 -07002100 int ret = 0;
2101
Chris Wilson05394f32010-11-08 19:18:58 +00002102 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002103 return 0;
2104
Chris Wilson05394f32010-11-08 19:18:58 +00002105 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002106 DRM_ERROR("Attempting to unbind pinned buffer\n");
2107 return -EINVAL;
2108 }
2109
Chris Wilsona8198ee2011-04-13 22:04:09 +01002110 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson8dc17752010-07-23 23:18:51 +01002111 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002112 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002113 /* Continue on if we fail due to EIO, the GPU is hung so we
2114 * should be safe and we need to cleanup or else we might
2115 * cause memory corruption through use-after-free.
2116 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002117
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002118 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002119
2120 /* Move the object to the CPU domain to ensure that
2121 * any possible CPU writes while it's not in the GTT
2122 * are flushed when we go to remap it.
2123 */
2124 if (ret == 0)
2125 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2126 if (ret == -ERESTARTSYS)
2127 return ret;
Chris Wilson812ed4922010-09-30 15:08:57 +01002128 if (ret) {
Chris Wilsona8198ee2011-04-13 22:04:09 +01002129 /* In the event of a disaster, abandon all caches and
2130 * hope for the best.
2131 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002132 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002133 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002134 }
Eric Anholt673a3942008-07-30 12:06:12 -07002135
Daniel Vetter96b47b62009-12-15 17:50:00 +01002136 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002137 ret = i915_gem_object_put_fence(obj);
2138 if (ret == -ERESTARTSYS)
2139 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002140
Chris Wilsondb53a302011-02-03 11:57:46 +00002141 trace_i915_gem_object_unbind(obj);
2142
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002143 i915_gem_gtt_unbind_object(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002144 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002145
Chris Wilson6299f992010-11-24 12:23:44 +00002146 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002147 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002148 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002149 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002150
Chris Wilson05394f32010-11-08 19:18:58 +00002151 drm_mm_put_block(obj->gtt_space);
2152 obj->gtt_space = NULL;
2153 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002154
Chris Wilson05394f32010-11-08 19:18:58 +00002155 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002156 i915_gem_object_truncate(obj);
2157
Chris Wilson8dc17752010-07-23 23:18:51 +01002158 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002159}
2160
Chris Wilson88241782011-01-07 17:09:48 +00002161int
Chris Wilsondb53a302011-02-03 11:57:46 +00002162i915_gem_flush_ring(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002163 uint32_t invalidate_domains,
2164 uint32_t flush_domains)
2165{
Chris Wilson88241782011-01-07 17:09:48 +00002166 int ret;
2167
Chris Wilson36d527d2011-03-19 22:26:49 +00002168 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2169 return 0;
2170
Chris Wilsondb53a302011-02-03 11:57:46 +00002171 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2172
Chris Wilson88241782011-01-07 17:09:48 +00002173 ret = ring->flush(ring, invalidate_domains, flush_domains);
2174 if (ret)
2175 return ret;
2176
Chris Wilson36d527d2011-03-19 22:26:49 +00002177 if (flush_domains & I915_GEM_GPU_DOMAINS)
2178 i915_gem_process_flushing_list(ring, flush_domains);
2179
Chris Wilson88241782011-01-07 17:09:48 +00002180 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002181}
2182
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002183static int i915_ring_idle(struct intel_ring_buffer *ring, bool do_retire)
Chris Wilsona56ba562010-09-28 10:07:56 +01002184{
Chris Wilson88241782011-01-07 17:09:48 +00002185 int ret;
2186
Chris Wilson395b70b2010-10-28 21:28:46 +01002187 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002188 return 0;
2189
Chris Wilson88241782011-01-07 17:09:48 +00002190 if (!list_empty(&ring->gpu_write_list)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002191 ret = i915_gem_flush_ring(ring,
Chris Wilson0ac74c62010-12-06 14:36:02 +00002192 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilson88241782011-01-07 17:09:48 +00002193 if (ret)
2194 return ret;
2195 }
2196
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002197 return i915_wait_request(ring, i915_gem_next_request_seqno(ring),
2198 do_retire);
Chris Wilsona56ba562010-09-28 10:07:56 +01002199}
2200
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002201int i915_gpu_idle(struct drm_device *dev, bool do_retire)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002202{
2203 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002204 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002205
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002206 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002207 for (i = 0; i < I915_NUM_RINGS; i++) {
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002208 ret = i915_ring_idle(&dev_priv->ring[i], do_retire);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002209 if (ret)
2210 return ret;
2211 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002212
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002213 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002214}
2215
Daniel Vetterc6642782010-11-12 13:46:18 +00002216static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2217 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002218{
Chris Wilson05394f32010-11-08 19:18:58 +00002219 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002220 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002221 u32 size = obj->gtt_space->size;
2222 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002223 uint64_t val;
2224
Chris Wilson05394f32010-11-08 19:18:58 +00002225 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002226 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002227 val |= obj->gtt_offset & 0xfffff000;
2228 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002229 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2230
Chris Wilson05394f32010-11-08 19:18:58 +00002231 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002232 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2233 val |= I965_FENCE_REG_VALID;
2234
Daniel Vetterc6642782010-11-12 13:46:18 +00002235 if (pipelined) {
2236 int ret = intel_ring_begin(pipelined, 6);
2237 if (ret)
2238 return ret;
2239
2240 intel_ring_emit(pipelined, MI_NOOP);
2241 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2242 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2243 intel_ring_emit(pipelined, (u32)val);
2244 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2245 intel_ring_emit(pipelined, (u32)(val >> 32));
2246 intel_ring_advance(pipelined);
2247 } else
2248 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2249
2250 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002251}
2252
Daniel Vetterc6642782010-11-12 13:46:18 +00002253static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2254 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002255{
Chris Wilson05394f32010-11-08 19:18:58 +00002256 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002257 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002258 u32 size = obj->gtt_space->size;
2259 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002260 uint64_t val;
2261
Chris Wilson05394f32010-11-08 19:18:58 +00002262 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002263 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002264 val |= obj->gtt_offset & 0xfffff000;
2265 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2266 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002267 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2268 val |= I965_FENCE_REG_VALID;
2269
Daniel Vetterc6642782010-11-12 13:46:18 +00002270 if (pipelined) {
2271 int ret = intel_ring_begin(pipelined, 6);
2272 if (ret)
2273 return ret;
2274
2275 intel_ring_emit(pipelined, MI_NOOP);
2276 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2277 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2278 intel_ring_emit(pipelined, (u32)val);
2279 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2280 intel_ring_emit(pipelined, (u32)(val >> 32));
2281 intel_ring_advance(pipelined);
2282 } else
2283 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2284
2285 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002286}
2287
Daniel Vetterc6642782010-11-12 13:46:18 +00002288static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2289 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002290{
Chris Wilson05394f32010-11-08 19:18:58 +00002291 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002292 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002293 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002294 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002295 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002296
Daniel Vetterc6642782010-11-12 13:46:18 +00002297 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2298 (size & -size) != size ||
2299 (obj->gtt_offset & (size - 1)),
2300 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2301 obj->gtt_offset, obj->map_and_fenceable, size))
2302 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002303
Daniel Vetterc6642782010-11-12 13:46:18 +00002304 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002305 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002306 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002307 tile_width = 512;
2308
2309 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002310 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002311 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002312
Chris Wilson05394f32010-11-08 19:18:58 +00002313 val = obj->gtt_offset;
2314 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002315 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002316 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2318 val |= I830_FENCE_REG_VALID;
2319
Chris Wilson05394f32010-11-08 19:18:58 +00002320 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002321 if (fence_reg < 8)
2322 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002323 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002324 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002325
2326 if (pipelined) {
2327 int ret = intel_ring_begin(pipelined, 4);
2328 if (ret)
2329 return ret;
2330
2331 intel_ring_emit(pipelined, MI_NOOP);
2332 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2333 intel_ring_emit(pipelined, fence_reg);
2334 intel_ring_emit(pipelined, val);
2335 intel_ring_advance(pipelined);
2336 } else
2337 I915_WRITE(fence_reg, val);
2338
2339 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002340}
2341
Daniel Vetterc6642782010-11-12 13:46:18 +00002342static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2343 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002344{
Chris Wilson05394f32010-11-08 19:18:58 +00002345 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002346 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002347 u32 size = obj->gtt_space->size;
2348 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002349 uint32_t val;
2350 uint32_t pitch_val;
2351
Daniel Vetterc6642782010-11-12 13:46:18 +00002352 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2353 (size & -size) != size ||
2354 (obj->gtt_offset & (size - 1)),
2355 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2356 obj->gtt_offset, size))
2357 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002358
Chris Wilson05394f32010-11-08 19:18:58 +00002359 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002360 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002361
Chris Wilson05394f32010-11-08 19:18:58 +00002362 val = obj->gtt_offset;
2363 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002364 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002365 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002366 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2367 val |= I830_FENCE_REG_VALID;
2368
Daniel Vetterc6642782010-11-12 13:46:18 +00002369 if (pipelined) {
2370 int ret = intel_ring_begin(pipelined, 4);
2371 if (ret)
2372 return ret;
2373
2374 intel_ring_emit(pipelined, MI_NOOP);
2375 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2376 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2377 intel_ring_emit(pipelined, val);
2378 intel_ring_advance(pipelined);
2379 } else
2380 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2381
2382 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002383}
2384
Chris Wilsond9e86c02010-11-10 16:40:20 +00002385static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2386{
2387 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2388}
2389
2390static int
2391i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002392 struct intel_ring_buffer *pipelined)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002393{
2394 int ret;
2395
2396 if (obj->fenced_gpu_access) {
Chris Wilson88241782011-01-07 17:09:48 +00002397 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002398 ret = i915_gem_flush_ring(obj->last_fenced_ring,
Chris Wilson88241782011-01-07 17:09:48 +00002399 0, obj->base.write_domain);
2400 if (ret)
2401 return ret;
2402 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002403
2404 obj->fenced_gpu_access = false;
2405 }
2406
2407 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2408 if (!ring_passed_seqno(obj->last_fenced_ring,
2409 obj->last_fenced_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002410 ret = i915_wait_request(obj->last_fenced_ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002411 obj->last_fenced_seqno,
2412 true);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002413 if (ret)
2414 return ret;
2415 }
2416
2417 obj->last_fenced_seqno = 0;
2418 obj->last_fenced_ring = NULL;
2419 }
2420
Chris Wilson63256ec2011-01-04 18:42:07 +00002421 /* Ensure that all CPU reads are completed before installing a fence
2422 * and all writes before removing the fence.
2423 */
2424 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2425 mb();
2426
Chris Wilsond9e86c02010-11-10 16:40:20 +00002427 return 0;
2428}
2429
2430int
2431i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2432{
2433 int ret;
2434
2435 if (obj->tiling_mode)
2436 i915_gem_release_mmap(obj);
2437
Chris Wilsonce453d82011-02-21 14:43:56 +00002438 ret = i915_gem_object_flush_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002439 if (ret)
2440 return ret;
2441
2442 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2443 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002444
2445 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002446 i915_gem_clear_fence_reg(obj->base.dev,
2447 &dev_priv->fence_regs[obj->fence_reg]);
2448
2449 obj->fence_reg = I915_FENCE_REG_NONE;
2450 }
2451
2452 return 0;
2453}
2454
2455static struct drm_i915_fence_reg *
2456i915_find_fence_reg(struct drm_device *dev,
2457 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002458{
Daniel Vetterae3db242010-02-19 11:51:58 +01002459 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002460 struct drm_i915_fence_reg *reg, *first, *avail;
2461 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002462
2463 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002464 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002465 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2466 reg = &dev_priv->fence_regs[i];
2467 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002468 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002469
Chris Wilson1690e1e2011-12-14 13:57:08 +01002470 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002471 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002472 }
2473
Chris Wilsond9e86c02010-11-10 16:40:20 +00002474 if (avail == NULL)
2475 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002476
2477 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002478 avail = first = NULL;
2479 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002480 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002481 continue;
2482
Chris Wilsond9e86c02010-11-10 16:40:20 +00002483 if (first == NULL)
2484 first = reg;
2485
2486 if (!pipelined ||
2487 !reg->obj->last_fenced_ring ||
2488 reg->obj->last_fenced_ring == pipelined) {
2489 avail = reg;
2490 break;
2491 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002492 }
2493
Chris Wilsond9e86c02010-11-10 16:40:20 +00002494 if (avail == NULL)
2495 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002496
Chris Wilsona00b10c2010-09-24 21:15:47 +01002497 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002498}
2499
Jesse Barnesde151cf2008-11-12 10:03:55 -08002500/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002501 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002502 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002503 * @pipelined: ring on which to queue the change, or NULL for CPU access
2504 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002505 *
2506 * When mapping objects through the GTT, userspace wants to be able to write
2507 * to them without having to worry about swizzling if the object is tiled.
2508 *
2509 * This function walks the fence regs looking for a free one for @obj,
2510 * stealing one if it can't find any.
2511 *
2512 * It then sets up the reg based on the object's properties: address, pitch
2513 * and tiling format.
2514 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002515int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002516i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002517 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002518{
Chris Wilson05394f32010-11-08 19:18:58 +00002519 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002520 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002521 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002522 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002523
Chris Wilson6bda10d2010-12-05 21:04:18 +00002524 /* XXX disable pipelining. There are bugs. Shocking. */
2525 pipelined = NULL;
2526
Chris Wilsond9e86c02010-11-10 16:40:20 +00002527 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002528 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2529 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002530 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002531
Chris Wilson29c5a582011-03-17 15:23:22 +00002532 if (obj->tiling_changed) {
2533 ret = i915_gem_object_flush_fence(obj, pipelined);
2534 if (ret)
2535 return ret;
2536
2537 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2538 pipelined = NULL;
2539
2540 if (pipelined) {
2541 reg->setup_seqno =
2542 i915_gem_next_request_seqno(pipelined);
2543 obj->last_fenced_seqno = reg->setup_seqno;
2544 obj->last_fenced_ring = pipelined;
2545 }
2546
2547 goto update;
2548 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002549
2550 if (!pipelined) {
2551 if (reg->setup_seqno) {
2552 if (!ring_passed_seqno(obj->last_fenced_ring,
2553 reg->setup_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002554 ret = i915_wait_request(obj->last_fenced_ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002555 reg->setup_seqno,
2556 true);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002557 if (ret)
2558 return ret;
2559 }
2560
2561 reg->setup_seqno = 0;
2562 }
2563 } else if (obj->last_fenced_ring &&
2564 obj->last_fenced_ring != pipelined) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002565 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002566 if (ret)
2567 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002568 }
2569
Eric Anholta09ba7f2009-08-29 12:49:51 -07002570 return 0;
2571 }
2572
Chris Wilsond9e86c02010-11-10 16:40:20 +00002573 reg = i915_find_fence_reg(dev, pipelined);
2574 if (reg == NULL)
Daniel Vetter39965b32011-12-14 13:57:09 +01002575 return -EDEADLK;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002576
Chris Wilsonce453d82011-02-21 14:43:56 +00002577 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002578 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002579 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002580
Chris Wilsond9e86c02010-11-10 16:40:20 +00002581 if (reg->obj) {
2582 struct drm_i915_gem_object *old = reg->obj;
2583
2584 drm_gem_object_reference(&old->base);
2585
2586 if (old->tiling_mode)
2587 i915_gem_release_mmap(old);
2588
Chris Wilsonce453d82011-02-21 14:43:56 +00002589 ret = i915_gem_object_flush_fence(old, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002590 if (ret) {
2591 drm_gem_object_unreference(&old->base);
2592 return ret;
2593 }
2594
2595 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2596 pipelined = NULL;
2597
2598 old->fence_reg = I915_FENCE_REG_NONE;
2599 old->last_fenced_ring = pipelined;
2600 old->last_fenced_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002601 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002602
2603 drm_gem_object_unreference(&old->base);
2604 } else if (obj->last_fenced_seqno == 0)
2605 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002606
Jesse Barnesde151cf2008-11-12 10:03:55 -08002607 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002608 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2609 obj->fence_reg = reg - dev_priv->fence_regs;
2610 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002611
Chris Wilsond9e86c02010-11-10 16:40:20 +00002612 reg->setup_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002613 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002614 obj->last_fenced_seqno = reg->setup_seqno;
2615
2616update:
2617 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002618 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002619 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002620 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002621 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002622 break;
2623 case 5:
2624 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002625 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002626 break;
2627 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002628 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002629 break;
2630 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002631 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002632 break;
2633 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002634
Daniel Vetterc6642782010-11-12 13:46:18 +00002635 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002636}
2637
2638/**
2639 * i915_gem_clear_fence_reg - clear out fence register info
2640 * @obj: object to clear
2641 *
2642 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002643 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002644 */
2645static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002646i915_gem_clear_fence_reg(struct drm_device *dev,
2647 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002648{
Jesse Barnes79e53942008-11-07 14:24:08 -08002649 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002650 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002651
Chris Wilsone259bef2010-09-17 00:32:02 +01002652 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002653 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002654 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002655 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002656 break;
2657 case 5:
2658 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002659 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002660 break;
2661 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002662 if (fence_reg >= 8)
2663 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002664 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002665 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002666 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002667
2668 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002669 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002670 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002671
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002672 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002673 reg->obj = NULL;
2674 reg->setup_seqno = 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002675 reg->pin_count = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002676}
2677
2678/**
Eric Anholt673a3942008-07-30 12:06:12 -07002679 * Finds free space in the GTT aperture and binds the object there.
2680 */
2681static int
Chris Wilson05394f32010-11-08 19:18:58 +00002682i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002683 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002684 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002685{
Chris Wilson05394f32010-11-08 19:18:58 +00002686 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002687 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002688 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002689 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002690 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002691 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002692 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002693
Chris Wilson05394f32010-11-08 19:18:58 +00002694 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002695 DRM_ERROR("Attempting to bind a purgeable object\n");
2696 return -EINVAL;
2697 }
2698
Chris Wilsone28f8712011-07-18 13:11:49 -07002699 fence_size = i915_gem_get_gtt_size(dev,
2700 obj->base.size,
2701 obj->tiling_mode);
2702 fence_alignment = i915_gem_get_gtt_alignment(dev,
2703 obj->base.size,
2704 obj->tiling_mode);
2705 unfenced_alignment =
2706 i915_gem_get_unfenced_gtt_alignment(dev,
2707 obj->base.size,
2708 obj->tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002709
Eric Anholt673a3942008-07-30 12:06:12 -07002710 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002711 alignment = map_and_fenceable ? fence_alignment :
2712 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002713 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002714 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2715 return -EINVAL;
2716 }
2717
Chris Wilson05394f32010-11-08 19:18:58 +00002718 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002719
Chris Wilson654fc602010-05-27 13:18:21 +01002720 /* If the object is bigger than the entire aperture, reject it early
2721 * before evicting everything in a vain attempt to find space.
2722 */
Chris Wilson05394f32010-11-08 19:18:58 +00002723 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002724 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002725 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2726 return -E2BIG;
2727 }
2728
Eric Anholt673a3942008-07-30 12:06:12 -07002729 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002730 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002731 free_space =
2732 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002733 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002734 dev_priv->mm.gtt_mappable_end,
2735 0);
2736 else
2737 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002738 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002739
2740 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002741 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002742 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002743 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002744 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002745 dev_priv->mm.gtt_mappable_end,
2746 0);
2747 else
Chris Wilson05394f32010-11-08 19:18:58 +00002748 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002749 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002750 }
Chris Wilson05394f32010-11-08 19:18:58 +00002751 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002752 /* If the gtt is empty and we're still having trouble
2753 * fitting our object in, we're out of memory.
2754 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002755 ret = i915_gem_evict_something(dev, size, alignment,
2756 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002757 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002758 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002759
Eric Anholt673a3942008-07-30 12:06:12 -07002760 goto search_free;
2761 }
2762
Chris Wilsone5281cc2010-10-28 13:45:36 +01002763 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002764 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002765 drm_mm_put_block(obj->gtt_space);
2766 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002767
2768 if (ret == -ENOMEM) {
Chris Wilson809b6332011-01-10 17:33:15 +00002769 /* first try to reclaim some memory by clearing the GTT */
2770 ret = i915_gem_evict_everything(dev, false);
Chris Wilson07f73f62009-09-14 16:50:30 +01002771 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002772 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002773 if (gfpmask) {
2774 gfpmask = 0;
2775 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002776 }
2777
Chris Wilson809b6332011-01-10 17:33:15 +00002778 return -ENOMEM;
Chris Wilson07f73f62009-09-14 16:50:30 +01002779 }
2780
2781 goto search_free;
2782 }
2783
Eric Anholt673a3942008-07-30 12:06:12 -07002784 return ret;
2785 }
2786
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002787 ret = i915_gem_gtt_bind_object(obj);
2788 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002789 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002790 drm_mm_put_block(obj->gtt_space);
2791 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002792
Chris Wilson809b6332011-01-10 17:33:15 +00002793 if (i915_gem_evict_everything(dev, false))
Chris Wilson07f73f62009-09-14 16:50:30 +01002794 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002795
2796 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002797 }
Eric Anholt673a3942008-07-30 12:06:12 -07002798
Chris Wilson6299f992010-11-24 12:23:44 +00002799 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002800 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002801
Eric Anholt673a3942008-07-30 12:06:12 -07002802 /* Assert that the object is not currently in any GPU domain. As it
2803 * wasn't in the GTT, there shouldn't be any way it could have been in
2804 * a GPU cache
2805 */
Chris Wilson05394f32010-11-08 19:18:58 +00002806 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2807 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002808
Chris Wilson6299f992010-11-24 12:23:44 +00002809 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002810
Daniel Vetter75e9e912010-11-04 17:11:09 +01002811 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002812 obj->gtt_space->size == fence_size &&
Akshay Joshi0206e352011-08-16 15:34:10 -04002813 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002814
Daniel Vetter75e9e912010-11-04 17:11:09 +01002815 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002816 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002817
Chris Wilson05394f32010-11-08 19:18:58 +00002818 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002819
Chris Wilsondb53a302011-02-03 11:57:46 +00002820 trace_i915_gem_object_bind(obj, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002821 return 0;
2822}
2823
2824void
Chris Wilson05394f32010-11-08 19:18:58 +00002825i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002826{
Eric Anholt673a3942008-07-30 12:06:12 -07002827 /* If we don't have a page list set up, then we're not pinned
2828 * to GPU, and we can ignore the cache flush because it'll happen
2829 * again at bind time.
2830 */
Chris Wilson05394f32010-11-08 19:18:58 +00002831 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002832 return;
2833
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002834 /* If the GPU is snooping the contents of the CPU cache,
2835 * we do not need to manually clear the CPU cache lines. However,
2836 * the caches are only snooped when the render cache is
2837 * flushed/invalidated. As we always have to emit invalidations
2838 * and flushes when moving into and out of the RENDER domain, correct
2839 * snooping behaviour occurs naturally as the result of our domain
2840 * tracking.
2841 */
2842 if (obj->cache_level != I915_CACHE_NONE)
2843 return;
2844
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002845 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002846
Chris Wilson05394f32010-11-08 19:18:58 +00002847 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002848}
2849
Eric Anholte47c68e2008-11-14 13:35:19 -08002850/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson88241782011-01-07 17:09:48 +00002851static int
Chris Wilson3619df02010-11-28 15:37:17 +00002852i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002853{
Chris Wilson05394f32010-11-08 19:18:58 +00002854 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson88241782011-01-07 17:09:48 +00002855 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002856
2857 /* Queue the GPU write cache flushing we need. */
Chris Wilsondb53a302011-02-03 11:57:46 +00002858 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002859}
2860
2861/** Flushes the GTT write domain for the object if it's dirty. */
2862static void
Chris Wilson05394f32010-11-08 19:18:58 +00002863i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002864{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002865 uint32_t old_write_domain;
2866
Chris Wilson05394f32010-11-08 19:18:58 +00002867 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002868 return;
2869
Chris Wilson63256ec2011-01-04 18:42:07 +00002870 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002871 * to it immediately go to main memory as far as we know, so there's
2872 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002873 *
2874 * However, we do have to enforce the order so that all writes through
2875 * the GTT land before any writes to the device, such as updates to
2876 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002877 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002878 wmb();
2879
Chris Wilson05394f32010-11-08 19:18:58 +00002880 old_write_domain = obj->base.write_domain;
2881 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002882
2883 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002884 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002885 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002886}
2887
2888/** Flushes the CPU write domain for the object if it's dirty. */
2889static void
Chris Wilson05394f32010-11-08 19:18:58 +00002890i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002891{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002892 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002893
Chris Wilson05394f32010-11-08 19:18:58 +00002894 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002895 return;
2896
2897 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002898 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002899 old_write_domain = obj->base.write_domain;
2900 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002901
2902 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002903 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002904 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002905}
2906
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002907/**
2908 * Moves a single object to the GTT read, and possibly write domain.
2909 *
2910 * This function returns when the move is complete, including waiting on
2911 * flushes to occur.
2912 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002913int
Chris Wilson20217462010-11-23 15:26:33 +00002914i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002915{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002916 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002917 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002918
Eric Anholt02354392008-11-26 13:58:13 -08002919 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002920 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002921 return -EINVAL;
2922
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002923 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2924 return 0;
2925
Chris Wilson88241782011-01-07 17:09:48 +00002926 ret = i915_gem_object_flush_gpu_write_domain(obj);
2927 if (ret)
2928 return ret;
2929
Chris Wilson87ca9c82010-12-02 09:42:56 +00002930 if (obj->pending_gpu_write || write) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002931 ret = i915_gem_object_wait_rendering(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00002932 if (ret)
2933 return ret;
2934 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002935
Chris Wilson72133422010-09-13 23:56:38 +01002936 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002937
Chris Wilson05394f32010-11-08 19:18:58 +00002938 old_write_domain = obj->base.write_domain;
2939 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002940
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002941 /* It should now be out of any other write domains, and we can update
2942 * the domain values for our changes.
2943 */
Chris Wilson05394f32010-11-08 19:18:58 +00002944 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2945 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002946 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002947 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2948 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2949 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002950 }
2951
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002952 trace_i915_gem_object_change_domain(obj,
2953 old_read_domains,
2954 old_write_domain);
2955
Eric Anholte47c68e2008-11-14 13:35:19 -08002956 return 0;
2957}
2958
Chris Wilsone4ffd172011-04-04 09:44:39 +01002959int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2960 enum i915_cache_level cache_level)
2961{
2962 int ret;
2963
2964 if (obj->cache_level == cache_level)
2965 return 0;
2966
2967 if (obj->pin_count) {
2968 DRM_DEBUG("can not change the cache level of pinned objects\n");
2969 return -EBUSY;
2970 }
2971
2972 if (obj->gtt_space) {
2973 ret = i915_gem_object_finish_gpu(obj);
2974 if (ret)
2975 return ret;
2976
2977 i915_gem_object_finish_gtt(obj);
2978
2979 /* Before SandyBridge, you could not use tiling or fence
2980 * registers with snooped memory, so relinquish any fences
2981 * currently pointing to our region in the aperture.
2982 */
2983 if (INTEL_INFO(obj->base.dev)->gen < 6) {
2984 ret = i915_gem_object_put_fence(obj);
2985 if (ret)
2986 return ret;
2987 }
2988
2989 i915_gem_gtt_rebind_object(obj, cache_level);
2990 }
2991
2992 if (cache_level == I915_CACHE_NONE) {
2993 u32 old_read_domains, old_write_domain;
2994
2995 /* If we're coming from LLC cached, then we haven't
2996 * actually been tracking whether the data is in the
2997 * CPU cache or not, since we only allow one bit set
2998 * in obj->write_domain and have been skipping the clflushes.
2999 * Just set it to the CPU cache for now.
3000 */
3001 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3002 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3003
3004 old_read_domains = obj->base.read_domains;
3005 old_write_domain = obj->base.write_domain;
3006
3007 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3008 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3009
3010 trace_i915_gem_object_change_domain(obj,
3011 old_read_domains,
3012 old_write_domain);
3013 }
3014
3015 obj->cache_level = cache_level;
3016 return 0;
3017}
3018
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003019/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003020 * Prepare buffer for display plane (scanout, cursors, etc).
3021 * Can be called from an uninterruptible phase (modesetting) and allows
3022 * any flushes to be pipelined (for pageflips).
3023 *
3024 * For the display plane, we want to be in the GTT but out of any write
3025 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
3026 * ability to pipeline the waits, pinning and any additional subtleties
3027 * that may differentiate the display plane from ordinary buffers.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003028 */
3029int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003030i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3031 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003032 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003033{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003034 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003035 int ret;
3036
Chris Wilson88241782011-01-07 17:09:48 +00003037 ret = i915_gem_object_flush_gpu_write_domain(obj);
3038 if (ret)
3039 return ret;
3040
Chris Wilson0be73282010-12-06 14:36:27 +00003041 if (pipelined != obj->ring) {
Chris Wilsonce453d82011-02-21 14:43:56 +00003042 ret = i915_gem_object_wait_rendering(obj);
Keith Packardf0b69ef2011-07-19 16:21:40 -07003043 if (ret == -ERESTARTSYS)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003044 return ret;
3045 }
3046
Eric Anholta7ef0642011-03-29 16:59:54 -07003047 /* The display engine is not coherent with the LLC cache on gen6. As
3048 * a result, we make sure that the pinning that is about to occur is
3049 * done with uncached PTEs. This is lowest common denominator for all
3050 * chipsets.
3051 *
3052 * However for gen6+, we could do better by using the GFDT bit instead
3053 * of uncaching, which would allow us to flush all the LLC-cached data
3054 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3055 */
3056 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3057 if (ret)
3058 return ret;
3059
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003060 /* As the user may map the buffer once pinned in the display plane
3061 * (e.g. libkms for the bootup splash), we have to ensure that we
3062 * always use map_and_fenceable for all scanout buffers.
3063 */
3064 ret = i915_gem_object_pin(obj, alignment, true);
3065 if (ret)
3066 return ret;
3067
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003068 i915_gem_object_flush_cpu_write_domain(obj);
3069
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003070 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003071 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003072
3073 /* It should now be out of any other write domains, and we can update
3074 * the domain values for our changes.
3075 */
3076 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003077 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003078
3079 trace_i915_gem_object_change_domain(obj,
3080 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003081 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003082
3083 return 0;
3084}
3085
Chris Wilson85345512010-11-13 09:49:11 +00003086int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003087i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003088{
Chris Wilson88241782011-01-07 17:09:48 +00003089 int ret;
3090
Chris Wilsona8198ee2011-04-13 22:04:09 +01003091 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003092 return 0;
3093
Chris Wilson88241782011-01-07 17:09:48 +00003094 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003095 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Chris Wilson88241782011-01-07 17:09:48 +00003096 if (ret)
3097 return ret;
3098 }
Chris Wilson85345512010-11-13 09:49:11 +00003099
Chris Wilsona8198ee2011-04-13 22:04:09 +01003100 /* Ensure that we invalidate the GPU's caches and TLBs. */
3101 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3102
Chris Wilsonce453d82011-02-21 14:43:56 +00003103 return i915_gem_object_wait_rendering(obj);
Chris Wilson85345512010-11-13 09:49:11 +00003104}
3105
Eric Anholte47c68e2008-11-14 13:35:19 -08003106/**
3107 * Moves a single object to the CPU read, and possibly write domain.
3108 *
3109 * This function returns when the move is complete, including waiting on
3110 * flushes to occur.
3111 */
3112static int
Chris Wilson919926a2010-11-12 13:42:53 +00003113i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003114{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003115 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003116 int ret;
3117
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003118 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3119 return 0;
3120
Chris Wilson88241782011-01-07 17:09:48 +00003121 ret = i915_gem_object_flush_gpu_write_domain(obj);
3122 if (ret)
3123 return ret;
3124
Chris Wilsonce453d82011-02-21 14:43:56 +00003125 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003126 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003127 return ret;
3128
3129 i915_gem_object_flush_gtt_write_domain(obj);
3130
3131 /* If we have a partially-valid cache of the object in the CPU,
3132 * finish invalidating it and free the per-page flags.
3133 */
3134 i915_gem_object_set_to_full_cpu_read_domain(obj);
3135
Chris Wilson05394f32010-11-08 19:18:58 +00003136 old_write_domain = obj->base.write_domain;
3137 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003138
Eric Anholte47c68e2008-11-14 13:35:19 -08003139 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003140 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003141 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003142
Chris Wilson05394f32010-11-08 19:18:58 +00003143 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003144 }
3145
3146 /* It should now be out of any other write domains, and we can update
3147 * the domain values for our changes.
3148 */
Chris Wilson05394f32010-11-08 19:18:58 +00003149 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003150
3151 /* If we're writing through the CPU, then the GPU read domains will
3152 * need to be invalidated at next use.
3153 */
3154 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003155 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3156 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003157 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003158
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003159 trace_i915_gem_object_change_domain(obj,
3160 old_read_domains,
3161 old_write_domain);
3162
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003163 return 0;
3164}
3165
Eric Anholt673a3942008-07-30 12:06:12 -07003166/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003167 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003168 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003169 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3170 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3171 */
3172static void
Chris Wilson05394f32010-11-08 19:18:58 +00003173i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003174{
Chris Wilson05394f32010-11-08 19:18:58 +00003175 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003176 return;
3177
3178 /* If we're partially in the CPU read domain, finish moving it in.
3179 */
Chris Wilson05394f32010-11-08 19:18:58 +00003180 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003181 int i;
3182
Chris Wilson05394f32010-11-08 19:18:58 +00003183 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3184 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003185 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003186 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003187 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003188 }
3189
3190 /* Free the page_cpu_valid mappings which are now stale, whether
3191 * or not we've got I915_GEM_DOMAIN_CPU.
3192 */
Chris Wilson05394f32010-11-08 19:18:58 +00003193 kfree(obj->page_cpu_valid);
3194 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003195}
3196
3197/**
3198 * Set the CPU read domain on a range of the object.
3199 *
3200 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3201 * not entirely valid. The page_cpu_valid member of the object flags which
3202 * pages have been flushed, and will be respected by
3203 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3204 * of the whole object.
3205 *
3206 * This function returns when the move is complete, including waiting on
3207 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003208 */
3209static int
Chris Wilson05394f32010-11-08 19:18:58 +00003210i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003211 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003212{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003213 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003214 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003215
Chris Wilson05394f32010-11-08 19:18:58 +00003216 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003217 return i915_gem_object_set_to_cpu_domain(obj, 0);
3218
Chris Wilson88241782011-01-07 17:09:48 +00003219 ret = i915_gem_object_flush_gpu_write_domain(obj);
3220 if (ret)
3221 return ret;
3222
Chris Wilsonce453d82011-02-21 14:43:56 +00003223 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003224 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003225 return ret;
Daniel Vetterde18a292010-11-27 22:30:41 +01003226
Eric Anholte47c68e2008-11-14 13:35:19 -08003227 i915_gem_object_flush_gtt_write_domain(obj);
3228
3229 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003230 if (obj->page_cpu_valid == NULL &&
3231 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003232 return 0;
3233
Eric Anholte47c68e2008-11-14 13:35:19 -08003234 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3235 * newly adding I915_GEM_DOMAIN_CPU
3236 */
Chris Wilson05394f32010-11-08 19:18:58 +00003237 if (obj->page_cpu_valid == NULL) {
3238 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3239 GFP_KERNEL);
3240 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003241 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003242 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3243 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003244
3245 /* Flush the cache on any pages that are still invalid from the CPU's
3246 * perspective.
3247 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003248 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3249 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003250 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003251 continue;
3252
Chris Wilson05394f32010-11-08 19:18:58 +00003253 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003254
Chris Wilson05394f32010-11-08 19:18:58 +00003255 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003256 }
3257
Eric Anholte47c68e2008-11-14 13:35:19 -08003258 /* It should now be out of any other write domains, and we can update
3259 * the domain values for our changes.
3260 */
Chris Wilson05394f32010-11-08 19:18:58 +00003261 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003262
Chris Wilson05394f32010-11-08 19:18:58 +00003263 old_read_domains = obj->base.read_domains;
3264 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003265
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003266 trace_i915_gem_object_change_domain(obj,
3267 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003268 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003269
Eric Anholt673a3942008-07-30 12:06:12 -07003270 return 0;
3271}
3272
Eric Anholt673a3942008-07-30 12:06:12 -07003273/* Throttle our rendering by waiting until the ring has completed our requests
3274 * emitted over 20 msec ago.
3275 *
Eric Anholtb9624422009-06-03 07:27:35 +00003276 * Note that if we were to use the current jiffies each time around the loop,
3277 * we wouldn't escape the function with any frames outstanding if the time to
3278 * render a frame was over 20ms.
3279 *
Eric Anholt673a3942008-07-30 12:06:12 -07003280 * This should get us reasonable parallelism between CPU and GPU but also
3281 * relatively low latency when blocking on a particular request to finish.
3282 */
3283static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003284i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003285{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003286 struct drm_i915_private *dev_priv = dev->dev_private;
3287 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003288 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003289 struct drm_i915_gem_request *request;
3290 struct intel_ring_buffer *ring = NULL;
3291 u32 seqno = 0;
3292 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003293
Chris Wilsone110e8d2011-01-26 15:39:14 +00003294 if (atomic_read(&dev_priv->mm.wedged))
3295 return -EIO;
3296
Chris Wilson1c255952010-09-26 11:03:27 +01003297 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003298 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003299 if (time_after_eq(request->emitted_jiffies, recent_enough))
3300 break;
3301
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003302 ring = request->ring;
3303 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003304 }
Chris Wilson1c255952010-09-26 11:03:27 +01003305 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003306
3307 if (seqno == 0)
3308 return 0;
3309
3310 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003311 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003312 /* And wait for the seqno passing without holding any locks and
3313 * causing extra latency for others. This is safe as the irq
3314 * generation is designed to be run atomically and so is
3315 * lockless.
3316 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003317 if (ring->irq_get(ring)) {
3318 ret = wait_event_interruptible(ring->irq_queue,
3319 i915_seqno_passed(ring->get_seqno(ring), seqno)
3320 || atomic_read(&dev_priv->mm.wedged));
3321 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003322
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003323 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3324 ret = -EIO;
Eric Anholte959b5d2011-12-22 14:55:01 -08003325 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
3326 seqno) ||
Eric Anholt7ea29b12011-12-22 14:54:59 -08003327 atomic_read(&dev_priv->mm.wedged), 3000)) {
3328 ret = -EBUSY;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003329 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003330 }
3331
3332 if (ret == 0)
3333 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003334
Eric Anholt673a3942008-07-30 12:06:12 -07003335 return ret;
3336}
3337
Eric Anholt673a3942008-07-30 12:06:12 -07003338int
Chris Wilson05394f32010-11-08 19:18:58 +00003339i915_gem_object_pin(struct drm_i915_gem_object *obj,
3340 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003341 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003342{
Chris Wilson05394f32010-11-08 19:18:58 +00003343 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003344 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003345 int ret;
3346
Chris Wilson05394f32010-11-08 19:18:58 +00003347 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003348 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003349
Chris Wilson05394f32010-11-08 19:18:58 +00003350 if (obj->gtt_space != NULL) {
3351 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3352 (map_and_fenceable && !obj->map_and_fenceable)) {
3353 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003354 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003355 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3356 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003357 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003358 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003359 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003360 ret = i915_gem_object_unbind(obj);
3361 if (ret)
3362 return ret;
3363 }
3364 }
3365
Chris Wilson05394f32010-11-08 19:18:58 +00003366 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003367 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003368 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003369 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003370 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003371 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003372
Chris Wilson05394f32010-11-08 19:18:58 +00003373 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003374 if (!obj->active)
3375 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003376 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003377 }
Chris Wilson6299f992010-11-24 12:23:44 +00003378 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003379
Chris Wilson23bc5982010-09-29 16:10:57 +01003380 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003381 return 0;
3382}
3383
3384void
Chris Wilson05394f32010-11-08 19:18:58 +00003385i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003386{
Chris Wilson05394f32010-11-08 19:18:58 +00003387 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003388 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003389
Chris Wilson23bc5982010-09-29 16:10:57 +01003390 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003391 BUG_ON(obj->pin_count == 0);
3392 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003393
Chris Wilson05394f32010-11-08 19:18:58 +00003394 if (--obj->pin_count == 0) {
3395 if (!obj->active)
3396 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003397 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003398 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003399 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003400 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003401}
3402
3403int
3404i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003405 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003406{
3407 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003408 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003409 int ret;
3410
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003411 ret = i915_mutex_lock_interruptible(dev);
3412 if (ret)
3413 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003414
Chris Wilson05394f32010-11-08 19:18:58 +00003415 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003416 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003417 ret = -ENOENT;
3418 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003419 }
Eric Anholt673a3942008-07-30 12:06:12 -07003420
Chris Wilson05394f32010-11-08 19:18:58 +00003421 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003422 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003423 ret = -EINVAL;
3424 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003425 }
3426
Chris Wilson05394f32010-11-08 19:18:58 +00003427 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003428 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3429 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003430 ret = -EINVAL;
3431 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003432 }
3433
Chris Wilson05394f32010-11-08 19:18:58 +00003434 obj->user_pin_count++;
3435 obj->pin_filp = file;
3436 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003437 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003438 if (ret)
3439 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003440 }
3441
3442 /* XXX - flush the CPU caches for pinned objects
3443 * as the X server doesn't manage domains yet
3444 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003445 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003446 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003447out:
Chris Wilson05394f32010-11-08 19:18:58 +00003448 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003449unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003450 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003451 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003452}
3453
3454int
3455i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003456 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003457{
3458 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003459 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003460 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003461
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003462 ret = i915_mutex_lock_interruptible(dev);
3463 if (ret)
3464 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003465
Chris Wilson05394f32010-11-08 19:18:58 +00003466 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003467 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003468 ret = -ENOENT;
3469 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003470 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003471
Chris Wilson05394f32010-11-08 19:18:58 +00003472 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003473 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3474 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003475 ret = -EINVAL;
3476 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003477 }
Chris Wilson05394f32010-11-08 19:18:58 +00003478 obj->user_pin_count--;
3479 if (obj->user_pin_count == 0) {
3480 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003481 i915_gem_object_unpin(obj);
3482 }
Eric Anholt673a3942008-07-30 12:06:12 -07003483
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003484out:
Chris Wilson05394f32010-11-08 19:18:58 +00003485 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003486unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003487 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003488 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003489}
3490
3491int
3492i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003493 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003494{
3495 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003496 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003497 int ret;
3498
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003499 ret = i915_mutex_lock_interruptible(dev);
3500 if (ret)
3501 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003502
Chris Wilson05394f32010-11-08 19:18:58 +00003503 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003504 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003505 ret = -ENOENT;
3506 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003507 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003508
Chris Wilson0be555b2010-08-04 15:36:30 +01003509 /* Count all active objects as busy, even if they are currently not used
3510 * by the gpu. Users of this interface expect objects to eventually
3511 * become non-busy without any further actions, therefore emit any
3512 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003513 */
Chris Wilson05394f32010-11-08 19:18:58 +00003514 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003515 if (args->busy) {
3516 /* Unconditionally flush objects, even when the gpu still uses this
3517 * object. Userspace calling this function indicates that it wants to
3518 * use this buffer rather sooner than later, so issuing the required
3519 * flush earlier is beneficial.
3520 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003521 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003522 ret = i915_gem_flush_ring(obj->ring,
Chris Wilson88241782011-01-07 17:09:48 +00003523 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003524 } else if (obj->ring->outstanding_lazy_request ==
3525 obj->last_rendering_seqno) {
3526 struct drm_i915_gem_request *request;
3527
Chris Wilson7a194872010-12-07 10:38:40 +00003528 /* This ring is not being cleared by active usage,
3529 * so emit a request to do so.
3530 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003531 request = kzalloc(sizeof(*request), GFP_KERNEL);
Rakib Mullick457eafc2011-11-16 00:49:28 +06003532 if (request) {
Akshay Joshi0206e352011-08-16 15:34:10 -04003533 ret = i915_add_request(obj->ring, NULL, request);
Rakib Mullick457eafc2011-11-16 00:49:28 +06003534 if (ret)
3535 kfree(request);
3536 } else
Chris Wilson7a194872010-12-07 10:38:40 +00003537 ret = -ENOMEM;
3538 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003539
3540 /* Update the active list for the hardware's current position.
3541 * Otherwise this only updates on a delayed timer or when irqs
3542 * are actually unmasked, and our working set ends up being
3543 * larger than required.
3544 */
Chris Wilsondb53a302011-02-03 11:57:46 +00003545 i915_gem_retire_requests_ring(obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003546
Chris Wilson05394f32010-11-08 19:18:58 +00003547 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003548 }
Eric Anholt673a3942008-07-30 12:06:12 -07003549
Chris Wilson05394f32010-11-08 19:18:58 +00003550 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003551unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003552 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003553 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003554}
3555
3556int
3557i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3558 struct drm_file *file_priv)
3559{
Akshay Joshi0206e352011-08-16 15:34:10 -04003560 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003561}
3562
Chris Wilson3ef94da2009-09-14 16:50:29 +01003563int
3564i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3565 struct drm_file *file_priv)
3566{
3567 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003568 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003569 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003570
3571 switch (args->madv) {
3572 case I915_MADV_DONTNEED:
3573 case I915_MADV_WILLNEED:
3574 break;
3575 default:
3576 return -EINVAL;
3577 }
3578
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003579 ret = i915_mutex_lock_interruptible(dev);
3580 if (ret)
3581 return ret;
3582
Chris Wilson05394f32010-11-08 19:18:58 +00003583 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003584 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003585 ret = -ENOENT;
3586 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003587 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003588
Chris Wilson05394f32010-11-08 19:18:58 +00003589 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003590 ret = -EINVAL;
3591 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003592 }
3593
Chris Wilson05394f32010-11-08 19:18:58 +00003594 if (obj->madv != __I915_MADV_PURGED)
3595 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003596
Chris Wilson2d7ef392009-09-20 23:13:10 +01003597 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003598 if (i915_gem_object_is_purgeable(obj) &&
3599 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003600 i915_gem_object_truncate(obj);
3601
Chris Wilson05394f32010-11-08 19:18:58 +00003602 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003603
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003604out:
Chris Wilson05394f32010-11-08 19:18:58 +00003605 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003606unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003607 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003608 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003609}
3610
Chris Wilson05394f32010-11-08 19:18:58 +00003611struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3612 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003613{
Chris Wilson73aa8082010-09-30 11:46:12 +01003614 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003615 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003616 struct address_space *mapping;
Daniel Vetterc397b902010-04-09 19:05:07 +00003617
3618 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3619 if (obj == NULL)
3620 return NULL;
3621
3622 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3623 kfree(obj);
3624 return NULL;
3625 }
3626
Hugh Dickins5949eac2011-06-27 16:18:18 -07003627 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3628 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3629
Chris Wilson73aa8082010-09-30 11:46:12 +01003630 i915_gem_info_add_obj(dev_priv, size);
3631
Daniel Vetterc397b902010-04-09 19:05:07 +00003632 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3633 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3634
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02003635 if (HAS_LLC(dev)) {
3636 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07003637 * cache) for about a 10% performance improvement
3638 * compared to uncached. Graphics requests other than
3639 * display scanout are coherent with the CPU in
3640 * accessing this cache. This means in this mode we
3641 * don't need to clflush on the CPU side, and on the
3642 * GPU side we only need to flush internal caches to
3643 * get data visible to the CPU.
3644 *
3645 * However, we maintain the display planes as UC, and so
3646 * need to rebind when first used as such.
3647 */
3648 obj->cache_level = I915_CACHE_LLC;
3649 } else
3650 obj->cache_level = I915_CACHE_NONE;
3651
Daniel Vetter62b8b212010-04-09 19:05:08 +00003652 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003653 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003654 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003655 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003656 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003657 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003658 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003659 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003660 /* Avoid an unnecessary call to unbind on the first bind. */
3661 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003662
Chris Wilson05394f32010-11-08 19:18:58 +00003663 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003664}
3665
Eric Anholt673a3942008-07-30 12:06:12 -07003666int i915_gem_init_object(struct drm_gem_object *obj)
3667{
Daniel Vetterc397b902010-04-09 19:05:07 +00003668 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003669
Eric Anholt673a3942008-07-30 12:06:12 -07003670 return 0;
3671}
3672
Chris Wilson05394f32010-11-08 19:18:58 +00003673static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003674{
Chris Wilson05394f32010-11-08 19:18:58 +00003675 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003676 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003677 int ret;
3678
3679 ret = i915_gem_object_unbind(obj);
3680 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003681 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003682 &dev_priv->mm.deferred_free_list);
3683 return;
3684 }
3685
Chris Wilson26e12f892011-03-20 11:20:19 +00003686 trace_i915_gem_object_destroy(obj);
3687
Chris Wilson05394f32010-11-08 19:18:58 +00003688 if (obj->base.map_list.map)
Rob Clarkb464e9a2011-08-10 08:09:08 -05003689 drm_gem_free_mmap_offset(&obj->base);
Chris Wilsonbe726152010-07-23 23:18:50 +01003690
Chris Wilson05394f32010-11-08 19:18:58 +00003691 drm_gem_object_release(&obj->base);
3692 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003693
Chris Wilson05394f32010-11-08 19:18:58 +00003694 kfree(obj->page_cpu_valid);
3695 kfree(obj->bit_17);
3696 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003697}
3698
Chris Wilson05394f32010-11-08 19:18:58 +00003699void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003700{
Chris Wilson05394f32010-11-08 19:18:58 +00003701 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3702 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003703
Chris Wilson05394f32010-11-08 19:18:58 +00003704 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003705 i915_gem_object_unpin(obj);
3706
Chris Wilson05394f32010-11-08 19:18:58 +00003707 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003708 i915_gem_detach_phys_object(dev, obj);
3709
Chris Wilsonbe726152010-07-23 23:18:50 +01003710 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003711}
3712
Jesse Barnes5669fca2009-02-17 15:13:31 -08003713int
Eric Anholt673a3942008-07-30 12:06:12 -07003714i915_gem_idle(struct drm_device *dev)
3715{
3716 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003717 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003718
Keith Packard6dbe2772008-10-14 21:41:13 -07003719 mutex_lock(&dev->struct_mutex);
3720
Chris Wilson87acb0a2010-10-19 10:13:00 +01003721 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003722 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003723 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003724 }
Eric Anholt673a3942008-07-30 12:06:12 -07003725
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08003726 ret = i915_gpu_idle(dev, true);
Keith Packard6dbe2772008-10-14 21:41:13 -07003727 if (ret) {
3728 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003729 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003730 }
Eric Anholt673a3942008-07-30 12:06:12 -07003731
Chris Wilson29105cc2010-01-07 10:39:13 +00003732 /* Under UMS, be paranoid and evict. */
3733 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003734 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003735 if (ret) {
3736 mutex_unlock(&dev->struct_mutex);
3737 return ret;
3738 }
3739 }
3740
Chris Wilson312817a2010-11-22 11:50:11 +00003741 i915_gem_reset_fences(dev);
3742
Chris Wilson29105cc2010-01-07 10:39:13 +00003743 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3744 * We need to replace this with a semaphore, or something.
3745 * And not confound mm.suspended!
3746 */
3747 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003748 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003749
3750 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003751 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003752
Keith Packard6dbe2772008-10-14 21:41:13 -07003753 mutex_unlock(&dev->struct_mutex);
3754
Chris Wilson29105cc2010-01-07 10:39:13 +00003755 /* Cancel the retire work handler, which should be idle now. */
3756 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3757
Eric Anholt673a3942008-07-30 12:06:12 -07003758 return 0;
3759}
3760
Eric Anholt673a3942008-07-30 12:06:12 -07003761int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003762i915_gem_init_ringbuffer(struct drm_device *dev)
3763{
3764 drm_i915_private_t *dev_priv = dev->dev_private;
3765 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003766
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003767 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003768 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003769 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003770
3771 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003772 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003773 if (ret)
3774 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003775 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003776
Chris Wilson549f7362010-10-19 11:19:32 +01003777 if (HAS_BLT(dev)) {
3778 ret = intel_init_blt_ring_buffer(dev);
3779 if (ret)
3780 goto cleanup_bsd_ring;
3781 }
3782
Chris Wilson6f392d5482010-08-07 11:01:22 +01003783 dev_priv->next_seqno = 1;
3784
Chris Wilson68f95ba2010-05-27 13:18:22 +01003785 return 0;
3786
Chris Wilson549f7362010-10-19 11:19:32 +01003787cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003788 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003789cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003790 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003791 return ret;
3792}
3793
3794void
3795i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3796{
3797 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003798 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003799
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003800 for (i = 0; i < I915_NUM_RINGS; i++)
3801 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003802}
3803
3804int
Eric Anholt673a3942008-07-30 12:06:12 -07003805i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3806 struct drm_file *file_priv)
3807{
3808 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003809 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003810
Jesse Barnes79e53942008-11-07 14:24:08 -08003811 if (drm_core_check_feature(dev, DRIVER_MODESET))
3812 return 0;
3813
Ben Gamariba1234d2009-09-14 17:48:47 -04003814 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003815 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003816 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003817 }
3818
Eric Anholt673a3942008-07-30 12:06:12 -07003819 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003820 dev_priv->mm.suspended = 0;
3821
3822 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003823 if (ret != 0) {
3824 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003825 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003826 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003827
Chris Wilson69dc4982010-10-19 10:36:51 +01003828 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003829 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3830 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003831 for (i = 0; i < I915_NUM_RINGS; i++) {
3832 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3833 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3834 }
Eric Anholt673a3942008-07-30 12:06:12 -07003835 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003836
Chris Wilson5f353082010-06-07 14:03:03 +01003837 ret = drm_irq_install(dev);
3838 if (ret)
3839 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003840
Eric Anholt673a3942008-07-30 12:06:12 -07003841 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003842
3843cleanup_ringbuffer:
3844 mutex_lock(&dev->struct_mutex);
3845 i915_gem_cleanup_ringbuffer(dev);
3846 dev_priv->mm.suspended = 1;
3847 mutex_unlock(&dev->struct_mutex);
3848
3849 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003850}
3851
3852int
3853i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3854 struct drm_file *file_priv)
3855{
Jesse Barnes79e53942008-11-07 14:24:08 -08003856 if (drm_core_check_feature(dev, DRIVER_MODESET))
3857 return 0;
3858
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003859 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003860 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003861}
3862
3863void
3864i915_gem_lastclose(struct drm_device *dev)
3865{
3866 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003867
Eric Anholte806b492009-01-22 09:56:58 -08003868 if (drm_core_check_feature(dev, DRIVER_MODESET))
3869 return;
3870
Keith Packard6dbe2772008-10-14 21:41:13 -07003871 ret = i915_gem_idle(dev);
3872 if (ret)
3873 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003874}
3875
Chris Wilson64193402010-10-24 12:38:05 +01003876static void
3877init_ring_lists(struct intel_ring_buffer *ring)
3878{
3879 INIT_LIST_HEAD(&ring->active_list);
3880 INIT_LIST_HEAD(&ring->request_list);
3881 INIT_LIST_HEAD(&ring->gpu_write_list);
3882}
3883
Eric Anholt673a3942008-07-30 12:06:12 -07003884void
3885i915_gem_load(struct drm_device *dev)
3886{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003887 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003888 drm_i915_private_t *dev_priv = dev->dev_private;
3889
Chris Wilson69dc4982010-10-19 10:36:51 +01003890 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003891 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3892 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003893 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003894 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003895 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003896 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003897 for (i = 0; i < I915_NUM_RINGS; i++)
3898 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02003899 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003900 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003901 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3902 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003903 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003904
Dave Airlie94400122010-07-20 13:15:31 +10003905 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3906 if (IS_GEN3(dev)) {
3907 u32 tmp = I915_READ(MI_ARB_STATE);
3908 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3909 /* arb state is a masked write, so set bit + bit in mask */
3910 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3911 I915_WRITE(MI_ARB_STATE, tmp);
3912 }
3913 }
3914
Chris Wilson72bfa192010-12-19 11:42:05 +00003915 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3916
Jesse Barnesde151cf2008-11-12 10:03:55 -08003917 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003918 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3919 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003920
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003921 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003922 dev_priv->num_fence_regs = 16;
3923 else
3924 dev_priv->num_fence_regs = 8;
3925
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003926 /* Initialize fence registers to zero */
Eric Anholt10ed13e2011-05-06 13:53:49 -07003927 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3928 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003929 }
Eric Anholt10ed13e2011-05-06 13:53:49 -07003930
Eric Anholt673a3942008-07-30 12:06:12 -07003931 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003932 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01003933
Chris Wilsonce453d82011-02-21 14:43:56 +00003934 dev_priv->mm.interruptible = true;
3935
Chris Wilson17250b72010-10-28 12:51:39 +01003936 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3937 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3938 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07003939}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003940
3941/*
3942 * Create a physically contiguous memory object for this object
3943 * e.g. for cursor + overlay regs
3944 */
Chris Wilson995b6762010-08-20 13:23:26 +01003945static int i915_gem_init_phys_object(struct drm_device *dev,
3946 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003947{
3948 drm_i915_private_t *dev_priv = dev->dev_private;
3949 struct drm_i915_gem_phys_object *phys_obj;
3950 int ret;
3951
3952 if (dev_priv->mm.phys_objs[id - 1] || !size)
3953 return 0;
3954
Eric Anholt9a298b22009-03-24 12:23:04 -07003955 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003956 if (!phys_obj)
3957 return -ENOMEM;
3958
3959 phys_obj->id = id;
3960
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003961 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003962 if (!phys_obj->handle) {
3963 ret = -ENOMEM;
3964 goto kfree_obj;
3965 }
3966#ifdef CONFIG_X86
3967 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3968#endif
3969
3970 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3971
3972 return 0;
3973kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07003974 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003975 return ret;
3976}
3977
Chris Wilson995b6762010-08-20 13:23:26 +01003978static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003979{
3980 drm_i915_private_t *dev_priv = dev->dev_private;
3981 struct drm_i915_gem_phys_object *phys_obj;
3982
3983 if (!dev_priv->mm.phys_objs[id - 1])
3984 return;
3985
3986 phys_obj = dev_priv->mm.phys_objs[id - 1];
3987 if (phys_obj->cur_obj) {
3988 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3989 }
3990
3991#ifdef CONFIG_X86
3992 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3993#endif
3994 drm_pci_free(dev, phys_obj->handle);
3995 kfree(phys_obj);
3996 dev_priv->mm.phys_objs[id - 1] = NULL;
3997}
3998
3999void i915_gem_free_all_phys_object(struct drm_device *dev)
4000{
4001 int i;
4002
Dave Airlie260883c2009-01-22 17:58:49 +10004003 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004004 i915_gem_free_phys_object(dev, i);
4005}
4006
4007void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004008 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004009{
Chris Wilson05394f32010-11-08 19:18:58 +00004010 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004011 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004012 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004013 int page_count;
4014
Chris Wilson05394f32010-11-08 19:18:58 +00004015 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004016 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004017 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004018
Chris Wilson05394f32010-11-08 19:18:58 +00004019 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004020 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07004021 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004022 if (!IS_ERR(page)) {
4023 char *dst = kmap_atomic(page);
4024 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4025 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004026
Chris Wilsone5281cc2010-10-28 13:45:36 +01004027 drm_clflush_pages(&page, 1);
4028
4029 set_page_dirty(page);
4030 mark_page_accessed(page);
4031 page_cache_release(page);
4032 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004033 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004034 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004035
Chris Wilson05394f32010-11-08 19:18:58 +00004036 obj->phys_obj->cur_obj = NULL;
4037 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004038}
4039
4040int
4041i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004042 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004043 int id,
4044 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004045{
Chris Wilson05394f32010-11-08 19:18:58 +00004046 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004047 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004048 int ret = 0;
4049 int page_count;
4050 int i;
4051
4052 if (id > I915_MAX_PHYS_OBJECT)
4053 return -EINVAL;
4054
Chris Wilson05394f32010-11-08 19:18:58 +00004055 if (obj->phys_obj) {
4056 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004057 return 0;
4058 i915_gem_detach_phys_object(dev, obj);
4059 }
4060
Dave Airlie71acb5e2008-12-30 20:31:46 +10004061 /* create a new object */
4062 if (!dev_priv->mm.phys_objs[id - 1]) {
4063 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004064 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004065 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004066 DRM_ERROR("failed to init phys object %d size: %zu\n",
4067 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004068 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004069 }
4070 }
4071
4072 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004073 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4074 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004075
Chris Wilson05394f32010-11-08 19:18:58 +00004076 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004077
4078 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004079 struct page *page;
4080 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004081
Hugh Dickins5949eac2011-06-27 16:18:18 -07004082 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004083 if (IS_ERR(page))
4084 return PTR_ERR(page);
4085
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004086 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004087 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004088 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004089 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004090
4091 mark_page_accessed(page);
4092 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004093 }
4094
4095 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004096}
4097
4098static int
Chris Wilson05394f32010-11-08 19:18:58 +00004099i915_gem_phys_pwrite(struct drm_device *dev,
4100 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004101 struct drm_i915_gem_pwrite *args,
4102 struct drm_file *file_priv)
4103{
Chris Wilson05394f32010-11-08 19:18:58 +00004104 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004105 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004106
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004107 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4108 unsigned long unwritten;
4109
4110 /* The physical object once assigned is fixed for the lifetime
4111 * of the obj, so we can safely drop the lock and continue
4112 * to access vaddr.
4113 */
4114 mutex_unlock(&dev->struct_mutex);
4115 unwritten = copy_from_user(vaddr, user_data, args->size);
4116 mutex_lock(&dev->struct_mutex);
4117 if (unwritten)
4118 return -EFAULT;
4119 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004120
Daniel Vetter40ce6572010-11-05 18:12:18 +01004121 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004122 return 0;
4123}
Eric Anholtb9624422009-06-03 07:27:35 +00004124
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004125void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004126{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004127 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004128
4129 /* Clean up our request list when the client is going away, so that
4130 * later retire_requests won't dereference our soon-to-be-gone
4131 * file_priv.
4132 */
Chris Wilson1c255952010-09-26 11:03:27 +01004133 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004134 while (!list_empty(&file_priv->mm.request_list)) {
4135 struct drm_i915_gem_request *request;
4136
4137 request = list_first_entry(&file_priv->mm.request_list,
4138 struct drm_i915_gem_request,
4139 client_list);
4140 list_del(&request->client_list);
4141 request->file_priv = NULL;
4142 }
Chris Wilson1c255952010-09-26 11:03:27 +01004143 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004144}
Chris Wilson31169712009-09-14 16:50:28 +01004145
Chris Wilson31169712009-09-14 16:50:28 +01004146static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004147i915_gpu_is_active(struct drm_device *dev)
4148{
4149 drm_i915_private_t *dev_priv = dev->dev_private;
4150 int lists_empty;
4151
Chris Wilson1637ef42010-04-20 17:10:35 +01004152 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004153 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004154
4155 return !lists_empty;
4156}
4157
4158static int
Ying Han1495f232011-05-24 17:12:27 -07004159i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004160{
Chris Wilson17250b72010-10-28 12:51:39 +01004161 struct drm_i915_private *dev_priv =
4162 container_of(shrinker,
4163 struct drm_i915_private,
4164 mm.inactive_shrinker);
4165 struct drm_device *dev = dev_priv->dev;
4166 struct drm_i915_gem_object *obj, *next;
Ying Han1495f232011-05-24 17:12:27 -07004167 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01004168 int cnt;
4169
4170 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004171 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004172
4173 /* "fast-path" to count number of available objects */
4174 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01004175 cnt = 0;
4176 list_for_each_entry(obj,
4177 &dev_priv->mm.inactive_list,
4178 mm_list)
4179 cnt++;
4180 mutex_unlock(&dev->struct_mutex);
4181 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004182 }
4183
Chris Wilson1637ef42010-04-20 17:10:35 +01004184rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004185 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01004186 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01004187
Chris Wilson17250b72010-10-28 12:51:39 +01004188 list_for_each_entry_safe(obj, next,
4189 &dev_priv->mm.inactive_list,
4190 mm_list) {
4191 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00004192 if (i915_gem_object_unbind(obj) == 0 &&
4193 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004194 break;
Chris Wilson31169712009-09-14 16:50:28 +01004195 }
Chris Wilson31169712009-09-14 16:50:28 +01004196 }
4197
4198 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01004199 cnt = 0;
4200 list_for_each_entry_safe(obj, next,
4201 &dev_priv->mm.inactive_list,
4202 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00004203 if (nr_to_scan &&
4204 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004205 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00004206 else
Chris Wilson17250b72010-10-28 12:51:39 +01004207 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01004208 }
4209
Chris Wilson17250b72010-10-28 12:51:39 +01004210 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01004211 /*
4212 * We are desperate for pages, so as a last resort, wait
4213 * for the GPU to finish and discard whatever we can.
4214 * This has a dramatic impact to reduce the number of
4215 * OOM-killer events whilst running the GPU aggressively.
4216 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08004217 if (i915_gpu_idle(dev, true) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01004218 goto rescan;
4219 }
Chris Wilson17250b72010-10-28 12:51:39 +01004220 mutex_unlock(&dev->struct_mutex);
4221 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004222}