blob: d8e0c15c874f4fbbbdc1cec977eb1aa13b00c234 [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);
Chris Wilson31169712009-09-14 16:50:28 +010061
Chris Wilson73aa8082010-09-30 11:46:12 +010062/* some bookkeeping */
63static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
64 size_t size)
65{
66 dev_priv->mm.object_count++;
67 dev_priv->mm.object_memory += size;
68}
69
70static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
71 size_t size)
72{
73 dev_priv->mm.object_count--;
74 dev_priv->mm.object_memory -= size;
75}
76
Chris Wilson21dd3732011-01-26 15:55:56 +000077static int
78i915_gem_wait_for_error(struct drm_device *dev)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010079{
80 struct drm_i915_private *dev_priv = dev->dev_private;
81 struct completion *x = &dev_priv->error_completion;
82 unsigned long flags;
83 int ret;
84
85 if (!atomic_read(&dev_priv->mm.wedged))
86 return 0;
87
88 ret = wait_for_completion_interruptible(x);
89 if (ret)
90 return ret;
91
Chris Wilson21dd3732011-01-26 15:55:56 +000092 if (atomic_read(&dev_priv->mm.wedged)) {
93 /* GPU is hung, bump the completion count to account for
94 * the token we just consumed so that we never hit zero and
95 * end up waiting upon a subsequent completion event that
96 * will never happen.
97 */
98 spin_lock_irqsave(&x->wait.lock, flags);
99 x->done++;
100 spin_unlock_irqrestore(&x->wait.lock, flags);
101 }
102 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100103}
104
Chris Wilson54cf91d2010-11-25 18:00:26 +0000105int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100106{
Chris Wilson76c1dec2010-09-25 11:22:51 +0100107 int ret;
108
Chris Wilson21dd3732011-01-26 15:55:56 +0000109 ret = i915_gem_wait_for_error(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100110 if (ret)
111 return ret;
112
113 ret = mutex_lock_interruptible(&dev->struct_mutex);
114 if (ret)
115 return ret;
116
Chris Wilson23bc5982010-09-29 16:10:57 +0100117 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100118 return 0;
119}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100120
Chris Wilson7d1c4802010-08-07 21:45:03 +0100121static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000122i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100123{
Chris Wilson05394f32010-11-08 19:18:58 +0000124 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100125}
126
Chris Wilson20217462010-11-23 15:26:33 +0000127void i915_gem_do_init(struct drm_device *dev,
128 unsigned long start,
129 unsigned long mappable_end,
130 unsigned long end)
Jesse Barnes79e53942008-11-07 14:24:08 -0800131{
132 drm_i915_private_t *dev_priv = dev->dev_private;
133
Chris Wilsonbee4a182011-01-21 10:54:32 +0000134 drm_mm_init(&dev_priv->mm.gtt_space, start, end - start);
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Chris Wilsonbee4a182011-01-21 10:54:32 +0000136 dev_priv->mm.gtt_start = start;
137 dev_priv->mm.gtt_mappable_end = mappable_end;
138 dev_priv->mm.gtt_end = end;
Chris Wilson73aa8082010-09-30 11:46:12 +0100139 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200140 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Chris Wilsonbee4a182011-01-21 10:54:32 +0000141
142 /* Take over this portion of the GTT */
143 intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
Jesse Barnes79e53942008-11-07 14:24:08 -0800144}
Keith Packard6dbe2772008-10-14 21:41:13 -0700145
Eric Anholt673a3942008-07-30 12:06:12 -0700146int
147i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000148 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700149{
Eric Anholt673a3942008-07-30 12:06:12 -0700150 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000151
152 if (args->gtt_start >= args->gtt_end ||
153 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
154 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700155
156 mutex_lock(&dev->struct_mutex);
Chris Wilson20217462010-11-23 15:26:33 +0000157 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700158 mutex_unlock(&dev->struct_mutex);
159
Chris Wilson20217462010-11-23 15:26:33 +0000160 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700161}
162
Eric Anholt5a125c32008-10-22 21:40:13 -0700163int
164i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000165 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700166{
Chris Wilson73aa8082010-09-30 11:46:12 +0100167 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700168 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000169 struct drm_i915_gem_object *obj;
170 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700171
172 if (!(dev->driver->driver_features & DRIVER_GEM))
173 return -ENODEV;
174
Chris Wilson6299f992010-11-24 12:23:44 +0000175 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100176 mutex_lock(&dev->struct_mutex);
Chris Wilson6299f992010-11-24 12:23:44 +0000177 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
178 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100179 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700180
Chris Wilson6299f992010-11-24 12:23:44 +0000181 args->aper_size = dev_priv->mm.gtt_total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400182 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000183
Eric Anholt5a125c32008-10-22 21:40:13 -0700184 return 0;
185}
186
Dave Airlieff72145b2011-02-07 12:16:14 +1000187static int
188i915_gem_create(struct drm_file *file,
189 struct drm_device *dev,
190 uint64_t size,
191 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700192{
Chris Wilson05394f32010-11-08 19:18:58 +0000193 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300194 int ret;
195 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700196
Dave Airlieff72145b2011-02-07 12:16:14 +1000197 size = roundup(size, PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -0700198
199 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000200 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700201 if (obj == NULL)
202 return -ENOMEM;
203
Chris Wilson05394f32010-11-08 19:18:58 +0000204 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100205 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000206 drm_gem_object_release(&obj->base);
207 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100208 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700209 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100210 }
211
Chris Wilson202f2fe2010-10-14 13:20:40 +0100212 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000213 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100214 trace_i915_gem_object_create(obj);
215
Dave Airlieff72145b2011-02-07 12:16:14 +1000216 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700217 return 0;
218}
219
Dave Airlieff72145b2011-02-07 12:16:14 +1000220int
221i915_gem_dumb_create(struct drm_file *file,
222 struct drm_device *dev,
223 struct drm_mode_create_dumb *args)
224{
225 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000226 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000227 args->size = args->pitch * args->height;
228 return i915_gem_create(file, dev,
229 args->size, &args->handle);
230}
231
232int i915_gem_dumb_destroy(struct drm_file *file,
233 struct drm_device *dev,
234 uint32_t handle)
235{
236 return drm_gem_handle_delete(file, handle);
237}
238
239/**
240 * Creates a new mm object and returns a handle to it.
241 */
242int
243i915_gem_create_ioctl(struct drm_device *dev, void *data,
244 struct drm_file *file)
245{
246 struct drm_i915_gem_create *args = data;
247 return i915_gem_create(file, dev,
248 args->size, &args->handle);
249}
250
Chris Wilson05394f32010-11-08 19:18:58 +0000251static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700252{
Chris Wilson05394f32010-11-08 19:18:58 +0000253 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700254
255 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000256 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700257}
258
Chris Wilson99a03df2010-05-27 14:15:34 +0100259static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700260slow_shmem_copy(struct page *dst_page,
261 int dst_offset,
262 struct page *src_page,
263 int src_offset,
264 int length)
265{
266 char *dst_vaddr, *src_vaddr;
267
Chris Wilson99a03df2010-05-27 14:15:34 +0100268 dst_vaddr = kmap(dst_page);
269 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700270
271 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
272
Chris Wilson99a03df2010-05-27 14:15:34 +0100273 kunmap(src_page);
274 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700275}
276
Chris Wilson99a03df2010-05-27 14:15:34 +0100277static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700278slow_shmem_bit17_copy(struct page *gpu_page,
279 int gpu_offset,
280 struct page *cpu_page,
281 int cpu_offset,
282 int length,
283 int is_read)
284{
285 char *gpu_vaddr, *cpu_vaddr;
286
287 /* Use the unswizzled path if this page isn't affected. */
288 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
289 if (is_read)
290 return slow_shmem_copy(cpu_page, cpu_offset,
291 gpu_page, gpu_offset, length);
292 else
293 return slow_shmem_copy(gpu_page, gpu_offset,
294 cpu_page, cpu_offset, length);
295 }
296
Chris Wilson99a03df2010-05-27 14:15:34 +0100297 gpu_vaddr = kmap(gpu_page);
298 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700299
300 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
301 * XORing with the other bits (A9 for Y, A9 and A10 for X)
302 */
303 while (length > 0) {
304 int cacheline_end = ALIGN(gpu_offset + 1, 64);
305 int this_length = min(cacheline_end - gpu_offset, length);
306 int swizzled_gpu_offset = gpu_offset ^ 64;
307
308 if (is_read) {
309 memcpy(cpu_vaddr + cpu_offset,
310 gpu_vaddr + swizzled_gpu_offset,
311 this_length);
312 } else {
313 memcpy(gpu_vaddr + swizzled_gpu_offset,
314 cpu_vaddr + cpu_offset,
315 this_length);
316 }
317 cpu_offset += this_length;
318 gpu_offset += this_length;
319 length -= this_length;
320 }
321
Chris Wilson99a03df2010-05-27 14:15:34 +0100322 kunmap(cpu_page);
323 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700324}
325
Eric Anholt673a3942008-07-30 12:06:12 -0700326/**
Eric Anholteb014592009-03-10 11:44:52 -0700327 * This is the fast shmem pread path, which attempts to copy_from_user directly
328 * from the backing pages of the object to the user's address space. On a
329 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
330 */
331static int
Chris Wilson05394f32010-11-08 19:18:58 +0000332i915_gem_shmem_pread_fast(struct drm_device *dev,
333 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700334 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000335 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700336{
Chris Wilson05394f32010-11-08 19:18:58 +0000337 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700338 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100339 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700340 char __user *user_data;
341 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700342
343 user_data = (char __user *) (uintptr_t) args->data_ptr;
344 remain = args->size;
345
Eric Anholteb014592009-03-10 11:44:52 -0700346 offset = args->offset;
347
348 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100349 struct page *page;
350 char *vaddr;
351 int ret;
352
Eric Anholteb014592009-03-10 11:44:52 -0700353 /* Operation in this page
354 *
Eric Anholteb014592009-03-10 11:44:52 -0700355 * page_offset = offset within page
356 * page_length = bytes to copy for this page
357 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100358 page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700359 page_length = remain;
360 if ((page_offset + remain) > PAGE_SIZE)
361 page_length = PAGE_SIZE - page_offset;
362
Hugh Dickins5949eac2011-06-27 16:18:18 -0700363 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100364 if (IS_ERR(page))
365 return PTR_ERR(page);
366
367 vaddr = kmap_atomic(page);
368 ret = __copy_to_user_inatomic(user_data,
369 vaddr + page_offset,
370 page_length);
371 kunmap_atomic(vaddr);
372
373 mark_page_accessed(page);
374 page_cache_release(page);
375 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100376 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700377
378 remain -= page_length;
379 user_data += page_length;
380 offset += page_length;
381 }
382
Chris Wilson4f27b752010-10-14 15:26:45 +0100383 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700384}
385
386/**
387 * This is the fallback shmem pread path, which allocates temporary storage
388 * in kernel space to copy_to_user into outside of the struct_mutex, so we
389 * can copy out of the object's backing pages while holding the struct mutex
390 * and not take page faults.
391 */
392static int
Chris Wilson05394f32010-11-08 19:18:58 +0000393i915_gem_shmem_pread_slow(struct drm_device *dev,
394 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700395 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000396 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700397{
Chris Wilson05394f32010-11-08 19:18:58 +0000398 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700399 struct mm_struct *mm = current->mm;
400 struct page **user_pages;
401 ssize_t remain;
402 loff_t offset, pinned_pages, i;
403 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100404 int shmem_page_offset;
405 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700406 int page_length;
407 int ret;
408 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700409 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700410
411 remain = args->size;
412
413 /* Pin the user pages containing the data. We can't fault while
414 * holding the struct mutex, yet we want to hold it while
415 * dereferencing the user data.
416 */
417 first_data_page = data_ptr / PAGE_SIZE;
418 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
419 num_pages = last_data_page - first_data_page + 1;
420
Chris Wilson4f27b752010-10-14 15:26:45 +0100421 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700422 if (user_pages == NULL)
423 return -ENOMEM;
424
Chris Wilson4f27b752010-10-14 15:26:45 +0100425 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700426 down_read(&mm->mmap_sem);
427 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700428 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700429 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100430 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700431 if (pinned_pages < num_pages) {
432 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100433 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700434 }
435
Chris Wilson4f27b752010-10-14 15:26:45 +0100436 ret = i915_gem_object_set_cpu_read_domain_range(obj,
437 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700438 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100439 if (ret)
440 goto out;
441
442 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700443
Eric Anholteb014592009-03-10 11:44:52 -0700444 offset = args->offset;
445
446 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100447 struct page *page;
448
Eric Anholteb014592009-03-10 11:44:52 -0700449 /* Operation in this page
450 *
Eric Anholteb014592009-03-10 11:44:52 -0700451 * shmem_page_offset = offset within page in shmem file
452 * data_page_index = page number in get_user_pages return
453 * data_page_offset = offset with data_page_index page.
454 * page_length = bytes to copy for this page
455 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100456 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700457 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100458 data_page_offset = offset_in_page(data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700459
460 page_length = remain;
461 if ((shmem_page_offset + page_length) > PAGE_SIZE)
462 page_length = PAGE_SIZE - shmem_page_offset;
463 if ((data_page_offset + page_length) > PAGE_SIZE)
464 page_length = PAGE_SIZE - data_page_offset;
465
Hugh Dickins5949eac2011-06-27 16:18:18 -0700466 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Jesper Juhlb65552f2011-06-12 20:53:44 +0000467 if (IS_ERR(page)) {
468 ret = PTR_ERR(page);
469 goto out;
470 }
Chris Wilsone5281cc2010-10-28 13:45:36 +0100471
Eric Anholt280b7132009-03-12 16:56:27 -0700472 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100473 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700474 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100475 user_pages[data_page_index],
476 data_page_offset,
477 page_length,
478 1);
479 } else {
480 slow_shmem_copy(user_pages[data_page_index],
481 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100482 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100483 shmem_page_offset,
484 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700485 }
Eric Anholteb014592009-03-10 11:44:52 -0700486
Chris Wilsone5281cc2010-10-28 13:45:36 +0100487 mark_page_accessed(page);
488 page_cache_release(page);
489
Eric Anholteb014592009-03-10 11:44:52 -0700490 remain -= page_length;
491 data_ptr += page_length;
492 offset += page_length;
493 }
494
Chris Wilson4f27b752010-10-14 15:26:45 +0100495out:
Eric Anholteb014592009-03-10 11:44:52 -0700496 for (i = 0; i < pinned_pages; i++) {
497 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100498 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700499 page_cache_release(user_pages[i]);
500 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700501 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700502
503 return ret;
504}
505
Eric Anholt673a3942008-07-30 12:06:12 -0700506/**
507 * Reads data from the object referenced by handle.
508 *
509 * On error, the contents of *data are undefined.
510 */
511int
512i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000513 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700514{
515 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000516 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100517 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700518
Chris Wilson51311d02010-11-17 09:10:42 +0000519 if (args->size == 0)
520 return 0;
521
522 if (!access_ok(VERIFY_WRITE,
523 (char __user *)(uintptr_t)args->data_ptr,
524 args->size))
525 return -EFAULT;
526
527 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
528 args->size);
529 if (ret)
530 return -EFAULT;
531
Chris Wilson4f27b752010-10-14 15:26:45 +0100532 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100533 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100534 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700535
Chris Wilson05394f32010-11-08 19:18:58 +0000536 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000537 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100538 ret = -ENOENT;
539 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100540 }
Eric Anholt673a3942008-07-30 12:06:12 -0700541
Chris Wilson7dcd2492010-09-26 20:21:44 +0100542 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000543 if (args->offset > obj->base.size ||
544 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100545 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100546 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100547 }
548
Chris Wilsondb53a302011-02-03 11:57:46 +0000549 trace_i915_gem_object_pread(obj, args->offset, args->size);
550
Chris Wilson4f27b752010-10-14 15:26:45 +0100551 ret = i915_gem_object_set_cpu_read_domain_range(obj,
552 args->offset,
553 args->size);
554 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100555 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100556
557 ret = -EFAULT;
558 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000559 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100560 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000561 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700562
Chris Wilson35b62a82010-09-26 20:23:38 +0100563out:
Chris Wilson05394f32010-11-08 19:18:58 +0000564 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100565unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100566 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700567 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700568}
569
Keith Packard0839ccb2008-10-30 19:38:48 -0700570/* This is the fast write path which cannot handle
571 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700572 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700573
Keith Packard0839ccb2008-10-30 19:38:48 -0700574static inline int
575fast_user_write(struct io_mapping *mapping,
576 loff_t page_base, int page_offset,
577 char __user *user_data,
578 int length)
579{
580 char *vaddr_atomic;
581 unsigned long unwritten;
582
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700583 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700584 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
585 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700586 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100587 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700588}
589
590/* Here's the write path which can sleep for
591 * page faults
592 */
593
Chris Wilsonab34c222010-05-27 14:15:35 +0100594static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700595slow_kernel_write(struct io_mapping *mapping,
596 loff_t gtt_base, int gtt_offset,
597 struct page *user_page, int user_offset,
598 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700599{
Chris Wilsonab34c222010-05-27 14:15:35 +0100600 char __iomem *dst_vaddr;
601 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700602
Chris Wilsonab34c222010-05-27 14:15:35 +0100603 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
604 src_vaddr = kmap(user_page);
605
606 memcpy_toio(dst_vaddr + gtt_offset,
607 src_vaddr + user_offset,
608 length);
609
610 kunmap(user_page);
611 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700612}
613
Eric Anholt3de09aa2009-03-09 09:42:23 -0700614/**
615 * This is the fast pwrite path, where we copy the data directly from the
616 * user into the GTT, uncached.
617 */
Eric Anholt673a3942008-07-30 12:06:12 -0700618static int
Chris Wilson05394f32010-11-08 19:18:58 +0000619i915_gem_gtt_pwrite_fast(struct drm_device *dev,
620 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700621 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000622 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700623{
Keith Packard0839ccb2008-10-30 19:38:48 -0700624 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700625 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700626 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700627 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700628 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700629
630 user_data = (char __user *) (uintptr_t) args->data_ptr;
631 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700632
Chris Wilson05394f32010-11-08 19:18:58 +0000633 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700634
635 while (remain > 0) {
636 /* Operation in this page
637 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700638 * page_base = page offset within aperture
639 * page_offset = offset within page
640 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700641 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100642 page_base = offset & PAGE_MASK;
643 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700644 page_length = remain;
645 if ((page_offset + remain) > PAGE_SIZE)
646 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700647
Keith Packard0839ccb2008-10-30 19:38:48 -0700648 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700649 * source page isn't available. Return the error and we'll
650 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700651 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100652 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
653 page_offset, user_data, page_length))
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100654 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700655
Keith Packard0839ccb2008-10-30 19:38:48 -0700656 remain -= page_length;
657 user_data += page_length;
658 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700659 }
Eric Anholt673a3942008-07-30 12:06:12 -0700660
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100661 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700662}
663
Eric Anholt3de09aa2009-03-09 09:42:23 -0700664/**
665 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
666 * the memory and maps it using kmap_atomic for copying.
667 *
668 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
669 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
670 */
Eric Anholt3043c602008-10-02 12:24:47 -0700671static int
Chris Wilson05394f32010-11-08 19:18:58 +0000672i915_gem_gtt_pwrite_slow(struct drm_device *dev,
673 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700674 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000675 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700676{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700677 drm_i915_private_t *dev_priv = dev->dev_private;
678 ssize_t remain;
679 loff_t gtt_page_base, offset;
680 loff_t first_data_page, last_data_page, num_pages;
681 loff_t pinned_pages, i;
682 struct page **user_pages;
683 struct mm_struct *mm = current->mm;
684 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700685 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700686 uint64_t data_ptr = args->data_ptr;
687
688 remain = args->size;
689
690 /* Pin the user pages containing the data. We can't fault while
691 * holding the struct mutex, and all of the pwrite implementations
692 * want to hold it while dereferencing the user data.
693 */
694 first_data_page = data_ptr / PAGE_SIZE;
695 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
696 num_pages = last_data_page - first_data_page + 1;
697
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100698 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700699 if (user_pages == NULL)
700 return -ENOMEM;
701
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100702 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700703 down_read(&mm->mmap_sem);
704 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
705 num_pages, 0, 0, user_pages, NULL);
706 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100707 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700708 if (pinned_pages < num_pages) {
709 ret = -EFAULT;
710 goto out_unpin_pages;
711 }
712
Chris Wilsond9e86c02010-11-10 16:40:20 +0000713 ret = i915_gem_object_set_to_gtt_domain(obj, true);
714 if (ret)
715 goto out_unpin_pages;
716
717 ret = i915_gem_object_put_fence(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700718 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100719 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700720
Chris Wilson05394f32010-11-08 19:18:58 +0000721 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700722
723 while (remain > 0) {
724 /* Operation in this page
725 *
726 * gtt_page_base = page offset within aperture
727 * gtt_page_offset = offset within page in aperture
728 * data_page_index = page number in get_user_pages return
729 * data_page_offset = offset with data_page_index page.
730 * page_length = bytes to copy for this page
731 */
732 gtt_page_base = offset & PAGE_MASK;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100733 gtt_page_offset = offset_in_page(offset);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700734 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100735 data_page_offset = offset_in_page(data_ptr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700736
737 page_length = remain;
738 if ((gtt_page_offset + page_length) > PAGE_SIZE)
739 page_length = PAGE_SIZE - gtt_page_offset;
740 if ((data_page_offset + page_length) > PAGE_SIZE)
741 page_length = PAGE_SIZE - data_page_offset;
742
Chris Wilsonab34c222010-05-27 14:15:35 +0100743 slow_kernel_write(dev_priv->mm.gtt_mapping,
744 gtt_page_base, gtt_page_offset,
745 user_pages[data_page_index],
746 data_page_offset,
747 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700748
749 remain -= page_length;
750 offset += page_length;
751 data_ptr += page_length;
752 }
753
Eric Anholt3de09aa2009-03-09 09:42:23 -0700754out_unpin_pages:
755 for (i = 0; i < pinned_pages; i++)
756 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700757 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700758
759 return ret;
760}
761
Eric Anholt40123c12009-03-09 13:42:30 -0700762/**
763 * This is the fast shmem pwrite path, which attempts to directly
764 * copy_from_user into the kmapped pages backing the object.
765 */
Eric Anholt673a3942008-07-30 12:06:12 -0700766static int
Chris Wilson05394f32010-11-08 19:18:58 +0000767i915_gem_shmem_pwrite_fast(struct drm_device *dev,
768 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700769 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000770 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700771{
Chris Wilson05394f32010-11-08 19:18:58 +0000772 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700773 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100774 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700775 char __user *user_data;
776 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700777
778 user_data = (char __user *) (uintptr_t) args->data_ptr;
779 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700780
Eric Anholt673a3942008-07-30 12:06:12 -0700781 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000782 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700783
Eric Anholt40123c12009-03-09 13:42:30 -0700784 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100785 struct page *page;
786 char *vaddr;
787 int ret;
788
Eric Anholt40123c12009-03-09 13:42:30 -0700789 /* Operation in this page
790 *
Eric Anholt40123c12009-03-09 13:42:30 -0700791 * page_offset = offset within page
792 * page_length = bytes to copy for this page
793 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100794 page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700795 page_length = remain;
796 if ((page_offset + remain) > PAGE_SIZE)
797 page_length = PAGE_SIZE - page_offset;
798
Hugh Dickins5949eac2011-06-27 16:18:18 -0700799 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100800 if (IS_ERR(page))
801 return PTR_ERR(page);
802
803 vaddr = kmap_atomic(page, KM_USER0);
804 ret = __copy_from_user_inatomic(vaddr + page_offset,
805 user_data,
806 page_length);
807 kunmap_atomic(vaddr, KM_USER0);
808
809 set_page_dirty(page);
810 mark_page_accessed(page);
811 page_cache_release(page);
812
813 /* If we get a fault while copying data, then (presumably) our
814 * source page isn't available. Return the error and we'll
815 * retry in the slow path.
816 */
817 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100818 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700819
820 remain -= page_length;
821 user_data += page_length;
822 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700823 }
824
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100825 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700826}
827
828/**
829 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
830 * the memory and maps it using kmap_atomic for copying.
831 *
832 * This avoids taking mmap_sem for faulting on the user's address while the
833 * struct_mutex is held.
834 */
835static int
Chris Wilson05394f32010-11-08 19:18:58 +0000836i915_gem_shmem_pwrite_slow(struct drm_device *dev,
837 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700838 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000839 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700840{
Chris Wilson05394f32010-11-08 19:18:58 +0000841 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700842 struct mm_struct *mm = current->mm;
843 struct page **user_pages;
844 ssize_t remain;
845 loff_t offset, pinned_pages, i;
846 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100847 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700848 int data_page_index, data_page_offset;
849 int page_length;
850 int ret;
851 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700852 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700853
854 remain = args->size;
855
856 /* Pin the user pages containing the data. We can't fault while
857 * holding the struct mutex, and all of the pwrite implementations
858 * want to hold it while dereferencing the user data.
859 */
860 first_data_page = data_ptr / PAGE_SIZE;
861 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
862 num_pages = last_data_page - first_data_page + 1;
863
Chris Wilson4f27b752010-10-14 15:26:45 +0100864 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700865 if (user_pages == NULL)
866 return -ENOMEM;
867
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100868 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700869 down_read(&mm->mmap_sem);
870 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
871 num_pages, 0, 0, user_pages, NULL);
872 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100873 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700874 if (pinned_pages < num_pages) {
875 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100876 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700877 }
878
Eric Anholt40123c12009-03-09 13:42:30 -0700879 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100880 if (ret)
881 goto out;
882
883 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700884
Eric Anholt40123c12009-03-09 13:42:30 -0700885 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000886 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700887
888 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100889 struct page *page;
890
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
894 * data_page_index = page number in get_user_pages return
895 * data_page_offset = offset with data_page_index page.
896 * page_length = bytes to copy for this page
897 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100898 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700899 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100900 data_page_offset = offset_in_page(data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700901
902 page_length = remain;
903 if ((shmem_page_offset + page_length) > PAGE_SIZE)
904 page_length = PAGE_SIZE - shmem_page_offset;
905 if ((data_page_offset + page_length) > PAGE_SIZE)
906 page_length = PAGE_SIZE - data_page_offset;
907
Hugh Dickins5949eac2011-06-27 16:18:18 -0700908 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100909 if (IS_ERR(page)) {
910 ret = PTR_ERR(page);
911 goto out;
912 }
913
Eric Anholt280b7132009-03-12 16:56:27 -0700914 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100915 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700916 shmem_page_offset,
917 user_pages[data_page_index],
918 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100919 page_length,
920 0);
921 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100922 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100923 shmem_page_offset,
924 user_pages[data_page_index],
925 data_page_offset,
926 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700927 }
Eric Anholt40123c12009-03-09 13:42:30 -0700928
Chris Wilsone5281cc2010-10-28 13:45:36 +0100929 set_page_dirty(page);
930 mark_page_accessed(page);
931 page_cache_release(page);
932
Eric Anholt40123c12009-03-09 13:42:30 -0700933 remain -= page_length;
934 data_ptr += page_length;
935 offset += page_length;
936 }
937
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100938out:
Eric Anholt40123c12009-03-09 13:42:30 -0700939 for (i = 0; i < pinned_pages; i++)
940 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700941 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700942
943 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700944}
945
946/**
947 * Writes data to the object referenced by handle.
948 *
949 * On error, the contents of the buffer that were to be modified are undefined.
950 */
951int
952i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100953 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700954{
955 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000956 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000957 int ret;
958
959 if (args->size == 0)
960 return 0;
961
962 if (!access_ok(VERIFY_READ,
963 (char __user *)(uintptr_t)args->data_ptr,
964 args->size))
965 return -EFAULT;
966
967 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
968 args->size);
969 if (ret)
970 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700971
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100972 ret = i915_mutex_lock_interruptible(dev);
973 if (ret)
974 return ret;
975
Chris Wilson05394f32010-11-08 19:18:58 +0000976 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000977 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100978 ret = -ENOENT;
979 goto unlock;
980 }
Eric Anholt673a3942008-07-30 12:06:12 -0700981
Chris Wilson7dcd2492010-09-26 20:21:44 +0100982 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000983 if (args->offset > obj->base.size ||
984 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100985 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100986 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100987 }
988
Chris Wilsondb53a302011-02-03 11:57:46 +0000989 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
990
Eric Anholt673a3942008-07-30 12:06:12 -0700991 /* We can only do the GTT pwrite on untiled buffers, as otherwise
992 * it would end up going through the fenced access, and we'll get
993 * different detiling behavior between reading and writing.
994 * pread/pwrite currently are reading and writing from the CPU
995 * perspective, requiring manual detiling by the client.
996 */
Chris Wilson05394f32010-11-08 19:18:58 +0000997 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100998 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilsond9e86c02010-11-10 16:40:20 +0000999 else if (obj->gtt_space &&
Chris Wilson05394f32010-11-08 19:18:58 +00001000 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001001 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001002 if (ret)
1003 goto out;
1004
Chris Wilsond9e86c02010-11-10 16:40:20 +00001005 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1006 if (ret)
1007 goto out_unpin;
1008
1009 ret = i915_gem_object_put_fence(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001010 if (ret)
1011 goto out_unpin;
1012
1013 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1014 if (ret == -EFAULT)
1015 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1016
1017out_unpin:
1018 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001019 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001020 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1021 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001022 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001023
1024 ret = -EFAULT;
1025 if (!i915_gem_object_needs_bit17_swizzle(obj))
1026 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1027 if (ret == -EFAULT)
1028 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001029 }
Eric Anholt673a3942008-07-30 12:06:12 -07001030
Chris Wilson35b62a82010-09-26 20:23:38 +01001031out:
Chris Wilson05394f32010-11-08 19:18:58 +00001032 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001033unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001034 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001035 return ret;
1036}
1037
1038/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001039 * Called when user space prepares to use an object with the CPU, either
1040 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001041 */
1042int
1043i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001044 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001045{
1046 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001047 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001048 uint32_t read_domains = args->read_domains;
1049 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001050 int ret;
1051
1052 if (!(dev->driver->driver_features & DRIVER_GEM))
1053 return -ENODEV;
1054
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001055 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001056 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001057 return -EINVAL;
1058
Chris Wilson21d509e2009-06-06 09:46:02 +01001059 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001060 return -EINVAL;
1061
1062 /* Having something in the write domain implies it's in the read
1063 * domain, and only that read domain. Enforce that in the request.
1064 */
1065 if (write_domain != 0 && read_domains != write_domain)
1066 return -EINVAL;
1067
Chris Wilson76c1dec2010-09-25 11:22:51 +01001068 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001069 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001070 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001071
Chris Wilson05394f32010-11-08 19:18:58 +00001072 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001073 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001074 ret = -ENOENT;
1075 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001076 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001077
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001078 if (read_domains & I915_GEM_DOMAIN_GTT) {
1079 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001080
1081 /* Silently promote "you're not bound, there was nothing to do"
1082 * to success, since the client was just asking us to
1083 * make sure everything was done.
1084 */
1085 if (ret == -EINVAL)
1086 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001087 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001088 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001089 }
1090
Chris Wilson05394f32010-11-08 19:18:58 +00001091 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001092unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001093 mutex_unlock(&dev->struct_mutex);
1094 return ret;
1095}
1096
1097/**
1098 * Called when user space has done writes to this buffer
1099 */
1100int
1101i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001102 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001103{
1104 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001105 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001106 int ret = 0;
1107
1108 if (!(dev->driver->driver_features & DRIVER_GEM))
1109 return -ENODEV;
1110
Chris Wilson76c1dec2010-09-25 11:22:51 +01001111 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001112 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001113 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001114
Chris Wilson05394f32010-11-08 19:18:58 +00001115 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001116 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001117 ret = -ENOENT;
1118 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001119 }
1120
Eric Anholt673a3942008-07-30 12:06:12 -07001121 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001122 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001123 i915_gem_object_flush_cpu_write_domain(obj);
1124
Chris Wilson05394f32010-11-08 19:18:58 +00001125 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001126unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001127 mutex_unlock(&dev->struct_mutex);
1128 return ret;
1129}
1130
1131/**
1132 * Maps the contents of an object, returning the address it is mapped
1133 * into.
1134 *
1135 * While the mapping holds a reference on the contents of the object, it doesn't
1136 * imply a ref on the object itself.
1137 */
1138int
1139i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001140 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001141{
Chris Wilsonda761a62010-10-27 17:37:08 +01001142 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001143 struct drm_i915_gem_mmap *args = data;
1144 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001145 unsigned long addr;
1146
1147 if (!(dev->driver->driver_features & DRIVER_GEM))
1148 return -ENODEV;
1149
Chris Wilson05394f32010-11-08 19:18:58 +00001150 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001151 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001152 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001153
Chris Wilsonda761a62010-10-27 17:37:08 +01001154 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1155 drm_gem_object_unreference_unlocked(obj);
1156 return -E2BIG;
1157 }
1158
Eric Anholt673a3942008-07-30 12:06:12 -07001159 down_write(&current->mm->mmap_sem);
1160 addr = do_mmap(obj->filp, 0, args->size,
1161 PROT_READ | PROT_WRITE, MAP_SHARED,
1162 args->offset);
1163 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001164 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001165 if (IS_ERR((void *)addr))
1166 return addr;
1167
1168 args->addr_ptr = (uint64_t) addr;
1169
1170 return 0;
1171}
1172
Jesse Barnesde151cf2008-11-12 10:03:55 -08001173/**
1174 * i915_gem_fault - fault a page into the GTT
1175 * vma: VMA in question
1176 * vmf: fault info
1177 *
1178 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1179 * from userspace. The fault handler takes care of binding the object to
1180 * the GTT (if needed), allocating and programming a fence register (again,
1181 * only if needed based on whether the old reg is still valid or the object
1182 * is tiled) and inserting a new PTE into the faulting process.
1183 *
1184 * Note that the faulting process may involve evicting existing objects
1185 * from the GTT and/or fence registers to make room. So performance may
1186 * suffer if the GTT working set is large or there are few fence registers
1187 * left.
1188 */
1189int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1190{
Chris Wilson05394f32010-11-08 19:18:58 +00001191 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1192 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001193 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001194 pgoff_t page_offset;
1195 unsigned long pfn;
1196 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001197 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001198
1199 /* We don't use vmf->pgoff since that has the fake offset */
1200 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1201 PAGE_SHIFT;
1202
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001203 ret = i915_mutex_lock_interruptible(dev);
1204 if (ret)
1205 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001206
Chris Wilsondb53a302011-02-03 11:57:46 +00001207 trace_i915_gem_object_fault(obj, page_offset, true, write);
1208
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001209 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001210 if (!obj->map_and_fenceable) {
1211 ret = i915_gem_object_unbind(obj);
1212 if (ret)
1213 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001214 }
Chris Wilson05394f32010-11-08 19:18:58 +00001215 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001216 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001217 if (ret)
1218 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001219
Eric Anholte92d03b2011-06-14 16:43:09 -07001220 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1221 if (ret)
1222 goto unlock;
1223 }
Chris Wilson4a684a42010-10-28 14:44:08 +01001224
Chris Wilsond9e86c02010-11-10 16:40:20 +00001225 if (obj->tiling_mode == I915_TILING_NONE)
1226 ret = i915_gem_object_put_fence(obj);
1227 else
Chris Wilsonce453d82011-02-21 14:43:56 +00001228 ret = i915_gem_object_get_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001229 if (ret)
1230 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001231
Chris Wilson05394f32010-11-08 19:18:58 +00001232 if (i915_gem_object_is_inactive(obj))
1233 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001234
Chris Wilson6299f992010-11-24 12:23:44 +00001235 obj->fault_mappable = true;
1236
Chris Wilson05394f32010-11-08 19:18:58 +00001237 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001238 page_offset;
1239
1240 /* Finally, remap it using the new GTT offset */
1241 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001242unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001243 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001244out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001245 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001246 case -EIO:
Chris Wilson045e7692010-11-07 09:18:22 +00001247 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001248 /* Give the error handler a chance to run and move the
1249 * objects off the GPU active list. Next time we service the
1250 * fault, we should be able to transition the page into the
1251 * GTT without touching the GPU (and so avoid further
1252 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1253 * with coherency, just lost writes.
1254 */
Chris Wilson045e7692010-11-07 09:18:22 +00001255 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001256 case 0:
1257 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001258 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001259 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001260 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001261 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001262 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001263 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001264 }
1265}
1266
1267/**
Chris Wilson901782b2009-07-10 08:18:50 +01001268 * i915_gem_release_mmap - remove physical page mappings
1269 * @obj: obj in question
1270 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001271 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001272 * relinquish ownership of the pages back to the system.
1273 *
1274 * It is vital that we remove the page mapping if we have mapped a tiled
1275 * object through the GTT and then lose the fence register due to
1276 * resource pressure. Similarly if the object has been moved out of the
1277 * aperture, than pages mapped into userspace must be revoked. Removing the
1278 * mapping will then trigger a page fault on the next user access, allowing
1279 * fixup by i915_gem_fault().
1280 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001281void
Chris Wilson05394f32010-11-08 19:18:58 +00001282i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001283{
Chris Wilson6299f992010-11-24 12:23:44 +00001284 if (!obj->fault_mappable)
1285 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001286
Chris Wilsonf6e47882011-03-20 21:09:12 +00001287 if (obj->base.dev->dev_mapping)
1288 unmap_mapping_range(obj->base.dev->dev_mapping,
1289 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1290 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001291
Chris Wilson6299f992010-11-24 12:23:44 +00001292 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001293}
1294
Chris Wilson92b88ae2010-11-09 11:47:32 +00001295static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001296i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001297{
Chris Wilsone28f8712011-07-18 13:11:49 -07001298 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001299
1300 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001301 tiling_mode == I915_TILING_NONE)
1302 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001303
1304 /* Previous chips need a power-of-two fence region when tiling */
1305 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001306 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001307 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001308 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001309
Chris Wilsone28f8712011-07-18 13:11:49 -07001310 while (gtt_size < size)
1311 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001312
Chris Wilsone28f8712011-07-18 13:11:49 -07001313 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001314}
1315
Jesse Barnesde151cf2008-11-12 10:03:55 -08001316/**
1317 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1318 * @obj: object to check
1319 *
1320 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001321 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001322 */
1323static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001324i915_gem_get_gtt_alignment(struct drm_device *dev,
1325 uint32_t size,
1326 int tiling_mode)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001327{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001328 /*
1329 * Minimum alignment is 4k (GTT page size), but might be greater
1330 * if a fence register is needed for the object.
1331 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001332 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001333 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001334 return 4096;
1335
1336 /*
1337 * Previous chips need to be aligned to the size of the smallest
1338 * fence register that can contain the object.
1339 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001340 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001341}
1342
Daniel Vetter5e783302010-11-14 22:32:36 +01001343/**
1344 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1345 * unfenced object
Chris Wilsone28f8712011-07-18 13:11:49 -07001346 * @dev: the device
1347 * @size: size of the object
1348 * @tiling_mode: tiling mode of the object
Daniel Vetter5e783302010-11-14 22:32:36 +01001349 *
1350 * Return the required GTT alignment for an object, only taking into account
1351 * unfenced tiled surface requirements.
1352 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001353uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001354i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1355 uint32_t size,
1356 int tiling_mode)
Daniel Vetter5e783302010-11-14 22:32:36 +01001357{
Daniel Vetter5e783302010-11-14 22:32:36 +01001358 /*
1359 * Minimum alignment is 4k (GTT page size) for sane hw.
1360 */
1361 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001362 tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001363 return 4096;
1364
Chris Wilsone28f8712011-07-18 13:11:49 -07001365 /* Previous hardware however needs to be aligned to a power-of-two
1366 * tile height. The simplest method for determining this is to reuse
1367 * the power-of-tile object size.
Daniel Vetter5e783302010-11-14 22:32:36 +01001368 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001369 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Daniel Vetter5e783302010-11-14 22:32:36 +01001370}
1371
Jesse Barnesde151cf2008-11-12 10:03:55 -08001372int
Dave Airlieff72145b2011-02-07 12:16:14 +10001373i915_gem_mmap_gtt(struct drm_file *file,
1374 struct drm_device *dev,
1375 uint32_t handle,
1376 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001377{
Chris Wilsonda761a62010-10-27 17:37:08 +01001378 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001379 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001380 int ret;
1381
1382 if (!(dev->driver->driver_features & DRIVER_GEM))
1383 return -ENODEV;
1384
Chris Wilson76c1dec2010-09-25 11:22:51 +01001385 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001386 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001387 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001388
Dave Airlieff72145b2011-02-07 12:16:14 +10001389 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001390 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001391 ret = -ENOENT;
1392 goto unlock;
1393 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001394
Chris Wilson05394f32010-11-08 19:18:58 +00001395 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001396 ret = -E2BIG;
1397 goto unlock;
1398 }
1399
Chris Wilson05394f32010-11-08 19:18:58 +00001400 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001401 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001402 ret = -EINVAL;
1403 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001404 }
1405
Chris Wilson05394f32010-11-08 19:18:58 +00001406 if (!obj->base.map_list.map) {
Rob Clarkb464e9a2011-08-10 08:09:08 -05001407 ret = drm_gem_create_mmap_offset(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001408 if (ret)
1409 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001410 }
1411
Dave Airlieff72145b2011-02-07 12:16:14 +10001412 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001413
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001414out:
Chris Wilson05394f32010-11-08 19:18:58 +00001415 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001416unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001417 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001418 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001419}
1420
Dave Airlieff72145b2011-02-07 12:16:14 +10001421/**
1422 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1423 * @dev: DRM device
1424 * @data: GTT mapping ioctl data
1425 * @file: GEM object info
1426 *
1427 * Simply returns the fake offset to userspace so it can mmap it.
1428 * The mmap call will end up in drm_gem_mmap(), which will set things
1429 * up so we can get faults in the handler above.
1430 *
1431 * The fault handler will take care of binding the object into the GTT
1432 * (since it may have been evicted to make room for something), allocating
1433 * a fence register, and mapping the appropriate aperture address into
1434 * userspace.
1435 */
1436int
1437i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1438 struct drm_file *file)
1439{
1440 struct drm_i915_gem_mmap_gtt *args = data;
1441
1442 if (!(dev->driver->driver_features & DRIVER_GEM))
1443 return -ENODEV;
1444
1445 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1446}
1447
1448
Chris Wilsone5281cc2010-10-28 13:45:36 +01001449static int
Chris Wilson05394f32010-11-08 19:18:58 +00001450i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001451 gfp_t gfpmask)
1452{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001453 int page_count, i;
1454 struct address_space *mapping;
1455 struct inode *inode;
1456 struct page *page;
1457
1458 /* Get the list of pages out of our struct file. They'll be pinned
1459 * at this point until we release them.
1460 */
Chris Wilson05394f32010-11-08 19:18:58 +00001461 page_count = obj->base.size / PAGE_SIZE;
1462 BUG_ON(obj->pages != NULL);
1463 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1464 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001465 return -ENOMEM;
1466
Chris Wilson05394f32010-11-08 19:18:58 +00001467 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001468 mapping = inode->i_mapping;
Hugh Dickins5949eac2011-06-27 16:18:18 -07001469 gfpmask |= mapping_gfp_mask(mapping);
1470
Chris Wilsone5281cc2010-10-28 13:45:36 +01001471 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07001472 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001473 if (IS_ERR(page))
1474 goto err_pages;
1475
Chris Wilson05394f32010-11-08 19:18:58 +00001476 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001477 }
1478
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001479 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilsone5281cc2010-10-28 13:45:36 +01001480 i915_gem_object_do_bit_17_swizzle(obj);
1481
1482 return 0;
1483
1484err_pages:
1485 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001486 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001487
Chris Wilson05394f32010-11-08 19:18:58 +00001488 drm_free_large(obj->pages);
1489 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001490 return PTR_ERR(page);
1491}
1492
Chris Wilson5cdf5882010-09-27 15:51:07 +01001493static void
Chris Wilson05394f32010-11-08 19:18:58 +00001494i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001495{
Chris Wilson05394f32010-11-08 19:18:58 +00001496 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001497 int i;
1498
Chris Wilson05394f32010-11-08 19:18:58 +00001499 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001500
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001501 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07001502 i915_gem_object_save_bit_17_swizzle(obj);
1503
Chris Wilson05394f32010-11-08 19:18:58 +00001504 if (obj->madv == I915_MADV_DONTNEED)
1505 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001506
1507 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001508 if (obj->dirty)
1509 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001510
Chris Wilson05394f32010-11-08 19:18:58 +00001511 if (obj->madv == I915_MADV_WILLNEED)
1512 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001513
Chris Wilson05394f32010-11-08 19:18:58 +00001514 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001515 }
Chris Wilson05394f32010-11-08 19:18:58 +00001516 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001517
Chris Wilson05394f32010-11-08 19:18:58 +00001518 drm_free_large(obj->pages);
1519 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001520}
1521
Chris Wilson54cf91d2010-11-25 18:00:26 +00001522void
Chris Wilson05394f32010-11-08 19:18:58 +00001523i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001524 struct intel_ring_buffer *ring,
1525 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001526{
Chris Wilson05394f32010-11-08 19:18:58 +00001527 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001528 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001529
Zou Nan hai852835f2010-05-21 09:08:56 +08001530 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001531 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001532
1533 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001534 if (!obj->active) {
1535 drm_gem_object_reference(&obj->base);
1536 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001537 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001538
Eric Anholt673a3942008-07-30 12:06:12 -07001539 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001540 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1541 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001542
Chris Wilson05394f32010-11-08 19:18:58 +00001543 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001544 if (obj->fenced_gpu_access) {
1545 struct drm_i915_fence_reg *reg;
1546
1547 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1548
1549 obj->last_fenced_seqno = seqno;
1550 obj->last_fenced_ring = ring;
1551
1552 reg = &dev_priv->fence_regs[obj->fence_reg];
1553 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1554 }
1555}
1556
1557static void
1558i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1559{
1560 list_del_init(&obj->ring_list);
1561 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001562}
1563
Eric Anholtce44b0e2008-11-06 16:00:31 -08001564static void
Chris Wilson05394f32010-11-08 19:18:58 +00001565i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001566{
Chris Wilson05394f32010-11-08 19:18:58 +00001567 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001568 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001569
Chris Wilson05394f32010-11-08 19:18:58 +00001570 BUG_ON(!obj->active);
1571 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001572
1573 i915_gem_object_move_off_active(obj);
1574}
1575
1576static void
1577i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1578{
1579 struct drm_device *dev = obj->base.dev;
1580 struct drm_i915_private *dev_priv = dev->dev_private;
1581
1582 if (obj->pin_count != 0)
1583 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1584 else
1585 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1586
1587 BUG_ON(!list_empty(&obj->gpu_write_list));
1588 BUG_ON(!obj->active);
1589 obj->ring = NULL;
1590
1591 i915_gem_object_move_off_active(obj);
1592 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001593
1594 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001595 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001596 drm_gem_object_unreference(&obj->base);
1597
1598 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001599}
Eric Anholt673a3942008-07-30 12:06:12 -07001600
Chris Wilson963b4832009-09-20 23:03:54 +01001601/* Immediately discard the backing storage */
1602static void
Chris Wilson05394f32010-11-08 19:18:58 +00001603i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001604{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001605 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001606
Chris Wilsonae9fed62010-08-07 11:01:30 +01001607 /* Our goal here is to return as much of the memory as
1608 * is possible back to the system as we are called from OOM.
1609 * To do this we must instruct the shmfs to drop all of its
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001610 * backing pages, *now*.
Chris Wilsonae9fed62010-08-07 11:01:30 +01001611 */
Chris Wilson05394f32010-11-08 19:18:58 +00001612 inode = obj->base.filp->f_path.dentry->d_inode;
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001613 shmem_truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001614
Chris Wilson05394f32010-11-08 19:18:58 +00001615 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001616}
1617
1618static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001619i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001620{
Chris Wilson05394f32010-11-08 19:18:58 +00001621 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001622}
1623
Eric Anholt673a3942008-07-30 12:06:12 -07001624static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001625i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1626 uint32_t flush_domains)
Daniel Vetter63560392010-02-19 11:51:59 +01001627{
Chris Wilson05394f32010-11-08 19:18:58 +00001628 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001629
Chris Wilson05394f32010-11-08 19:18:58 +00001630 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001631 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001632 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001633 if (obj->base.write_domain & flush_domains) {
1634 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001635
Chris Wilson05394f32010-11-08 19:18:58 +00001636 obj->base.write_domain = 0;
1637 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001638 i915_gem_object_move_to_active(obj, ring,
Chris Wilsondb53a302011-02-03 11:57:46 +00001639 i915_gem_next_request_seqno(ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001640
Daniel Vetter63560392010-02-19 11:51:59 +01001641 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001642 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001643 old_write_domain);
1644 }
1645 }
1646}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001647
Chris Wilson3cce4692010-10-27 16:11:02 +01001648int
Chris Wilsondb53a302011-02-03 11:57:46 +00001649i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001650 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001651 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001652{
Chris Wilsondb53a302011-02-03 11:57:46 +00001653 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001654 uint32_t seqno;
1655 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001656 int ret;
1657
1658 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001659
Chris Wilson3cce4692010-10-27 16:11:02 +01001660 ret = ring->add_request(ring, &seqno);
1661 if (ret)
1662 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001663
Chris Wilsondb53a302011-02-03 11:57:46 +00001664 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001665
1666 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001667 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001668 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001669 was_empty = list_empty(&ring->request_list);
1670 list_add_tail(&request->list, &ring->request_list);
1671
Chris Wilsondb53a302011-02-03 11:57:46 +00001672 if (file) {
1673 struct drm_i915_file_private *file_priv = file->driver_priv;
1674
Chris Wilson1c255952010-09-26 11:03:27 +01001675 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001676 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001677 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001678 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001679 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001680 }
Eric Anholt673a3942008-07-30 12:06:12 -07001681
Chris Wilsondb53a302011-02-03 11:57:46 +00001682 ring->outstanding_lazy_request = false;
1683
Ben Gamarif65d9422009-09-14 17:48:44 -04001684 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001685 if (i915_enable_hangcheck) {
1686 mod_timer(&dev_priv->hangcheck_timer,
1687 jiffies +
1688 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1689 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001690 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001691 queue_delayed_work(dev_priv->wq,
1692 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001693 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001694 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001695}
1696
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001697static inline void
1698i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001699{
Chris Wilson1c255952010-09-26 11:03:27 +01001700 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001701
Chris Wilson1c255952010-09-26 11:03:27 +01001702 if (!file_priv)
1703 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001704
Chris Wilson1c255952010-09-26 11:03:27 +01001705 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001706 if (request->file_priv) {
1707 list_del(&request->client_list);
1708 request->file_priv = NULL;
1709 }
Chris Wilson1c255952010-09-26 11:03:27 +01001710 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001711}
1712
Chris Wilsondfaae392010-09-22 10:31:52 +01001713static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1714 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001715{
Chris Wilsondfaae392010-09-22 10:31:52 +01001716 while (!list_empty(&ring->request_list)) {
1717 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001718
Chris Wilsondfaae392010-09-22 10:31:52 +01001719 request = list_first_entry(&ring->request_list,
1720 struct drm_i915_gem_request,
1721 list);
1722
1723 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001724 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001725 kfree(request);
1726 }
1727
1728 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001729 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001730
Chris Wilson05394f32010-11-08 19:18:58 +00001731 obj = list_first_entry(&ring->active_list,
1732 struct drm_i915_gem_object,
1733 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001734
Chris Wilson05394f32010-11-08 19:18:58 +00001735 obj->base.write_domain = 0;
1736 list_del_init(&obj->gpu_write_list);
1737 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001738 }
Eric Anholt673a3942008-07-30 12:06:12 -07001739}
1740
Chris Wilson312817a2010-11-22 11:50:11 +00001741static void i915_gem_reset_fences(struct drm_device *dev)
1742{
1743 struct drm_i915_private *dev_priv = dev->dev_private;
1744 int i;
1745
1746 for (i = 0; i < 16; i++) {
1747 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001748 struct drm_i915_gem_object *obj = reg->obj;
1749
1750 if (!obj)
1751 continue;
1752
1753 if (obj->tiling_mode)
1754 i915_gem_release_mmap(obj);
1755
Chris Wilsond9e86c02010-11-10 16:40:20 +00001756 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1757 reg->obj->fenced_gpu_access = false;
1758 reg->obj->last_fenced_seqno = 0;
1759 reg->obj->last_fenced_ring = NULL;
1760 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001761 }
1762}
1763
Chris Wilson069efc12010-09-30 16:53:18 +01001764void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001765{
Chris Wilsondfaae392010-09-22 10:31:52 +01001766 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001767 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001768 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001769
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001770 for (i = 0; i < I915_NUM_RINGS; i++)
1771 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001772
1773 /* Remove anything from the flushing lists. The GPU cache is likely
1774 * to be lost on reset along with the data, so simply move the
1775 * lost bo to the inactive list.
1776 */
1777 while (!list_empty(&dev_priv->mm.flushing_list)) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001778 obj = list_first_entry(&dev_priv->mm.flushing_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001779 struct drm_i915_gem_object,
1780 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001781
Chris Wilson05394f32010-11-08 19:18:58 +00001782 obj->base.write_domain = 0;
1783 list_del_init(&obj->gpu_write_list);
1784 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001785 }
Chris Wilson9375e442010-09-19 12:21:28 +01001786
Chris Wilsondfaae392010-09-22 10:31:52 +01001787 /* Move everything out of the GPU domains to ensure we do any
1788 * necessary invalidation upon reuse.
1789 */
Chris Wilson05394f32010-11-08 19:18:58 +00001790 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001791 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001792 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001793 {
Chris Wilson05394f32010-11-08 19:18:58 +00001794 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001795 }
Chris Wilson069efc12010-09-30 16:53:18 +01001796
1797 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001798 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001799}
1800
1801/**
1802 * This function clears the request list as sequence numbers are passed.
1803 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001804static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001805i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001806{
Eric Anholt673a3942008-07-30 12:06:12 -07001807 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001808 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001809
Chris Wilsondb53a302011-02-03 11:57:46 +00001810 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001811 return;
1812
Chris Wilsondb53a302011-02-03 11:57:46 +00001813 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001814
Chris Wilson78501ea2010-10-27 12:18:21 +01001815 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001816
Chris Wilson076e2c02011-01-21 10:07:18 +00001817 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001818 if (seqno >= ring->sync_seqno[i])
1819 ring->sync_seqno[i] = 0;
1820
Zou Nan hai852835f2010-05-21 09:08:56 +08001821 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001822 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001823
Zou Nan hai852835f2010-05-21 09:08:56 +08001824 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001825 struct drm_i915_gem_request,
1826 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001827
Chris Wilsondfaae392010-09-22 10:31:52 +01001828 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001829 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001830
Chris Wilsondb53a302011-02-03 11:57:46 +00001831 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001832
1833 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001834 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001835 kfree(request);
1836 }
1837
1838 /* Move any buffers on the active list that are no longer referenced
1839 * by the ringbuffer to the flushing/inactive lists as appropriate.
1840 */
1841 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001842 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001843
Akshay Joshi0206e352011-08-16 15:34:10 -04001844 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001845 struct drm_i915_gem_object,
1846 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001847
Chris Wilson05394f32010-11-08 19:18:58 +00001848 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001849 break;
1850
Chris Wilson05394f32010-11-08 19:18:58 +00001851 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001852 i915_gem_object_move_to_flushing(obj);
1853 else
1854 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001855 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001856
Chris Wilsondb53a302011-02-03 11:57:46 +00001857 if (unlikely(ring->trace_irq_seqno &&
1858 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001859 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001860 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001861 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001862
Chris Wilsondb53a302011-02-03 11:57:46 +00001863 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001864}
1865
1866void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001867i915_gem_retire_requests(struct drm_device *dev)
1868{
1869 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001870 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001871
Chris Wilsonbe726152010-07-23 23:18:50 +01001872 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001873 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001874
1875 /* We must be careful that during unbind() we do not
1876 * accidentally infinitely recurse into retire requests.
1877 * Currently:
1878 * retire -> free -> unbind -> wait -> retire_ring
1879 */
Chris Wilson05394f32010-11-08 19:18:58 +00001880 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001881 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001882 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001883 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001884 }
1885
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001886 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilsondb53a302011-02-03 11:57:46 +00001887 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001888}
1889
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001890static void
Eric Anholt673a3942008-07-30 12:06:12 -07001891i915_gem_retire_work_handler(struct work_struct *work)
1892{
1893 drm_i915_private_t *dev_priv;
1894 struct drm_device *dev;
Chris Wilson0a587052011-01-09 21:05:44 +00001895 bool idle;
1896 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001897
1898 dev_priv = container_of(work, drm_i915_private_t,
1899 mm.retire_work.work);
1900 dev = dev_priv->dev;
1901
Chris Wilson891b48c2010-09-29 12:26:37 +01001902 /* Come back later if the device is busy... */
1903 if (!mutex_trylock(&dev->struct_mutex)) {
1904 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1905 return;
1906 }
1907
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001908 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001909
Chris Wilson0a587052011-01-09 21:05:44 +00001910 /* Send a periodic flush down the ring so we don't hold onto GEM
1911 * objects indefinitely.
1912 */
1913 idle = true;
1914 for (i = 0; i < I915_NUM_RINGS; i++) {
1915 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1916
1917 if (!list_empty(&ring->gpu_write_list)) {
1918 struct drm_i915_gem_request *request;
1919 int ret;
1920
Chris Wilsondb53a302011-02-03 11:57:46 +00001921 ret = i915_gem_flush_ring(ring,
1922 0, I915_GEM_GPU_DOMAINS);
Chris Wilson0a587052011-01-09 21:05:44 +00001923 request = kzalloc(sizeof(*request), GFP_KERNEL);
1924 if (ret || request == NULL ||
Chris Wilsondb53a302011-02-03 11:57:46 +00001925 i915_add_request(ring, NULL, request))
Chris Wilson0a587052011-01-09 21:05:44 +00001926 kfree(request);
1927 }
1928
1929 idle &= list_empty(&ring->request_list);
1930 }
1931
1932 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001933 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilson0a587052011-01-09 21:05:44 +00001934
Eric Anholt673a3942008-07-30 12:06:12 -07001935 mutex_unlock(&dev->struct_mutex);
1936}
1937
Chris Wilsondb53a302011-02-03 11:57:46 +00001938/**
1939 * Waits for a sequence number to be signaled, and cleans up the
1940 * request and object lists appropriately for that event.
1941 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001942int
Chris Wilsondb53a302011-02-03 11:57:46 +00001943i915_wait_request(struct intel_ring_buffer *ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00001944 uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001945{
Chris Wilsondb53a302011-02-03 11:57:46 +00001946 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001947 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001948 int ret = 0;
1949
1950 BUG_ON(seqno == 0);
1951
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001952 if (atomic_read(&dev_priv->mm.wedged)) {
1953 struct completion *x = &dev_priv->error_completion;
1954 bool recovery_complete;
1955 unsigned long flags;
1956
1957 /* Give the error handler a chance to run. */
1958 spin_lock_irqsave(&x->wait.lock, flags);
1959 recovery_complete = x->done > 0;
1960 spin_unlock_irqrestore(&x->wait.lock, flags);
1961
1962 return recovery_complete ? -EIO : -EAGAIN;
1963 }
Ben Gamariffed1d02009-09-14 17:48:41 -04001964
Chris Wilson5d97eb62010-11-10 20:40:02 +00001965 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01001966 struct drm_i915_gem_request *request;
1967
1968 request = kzalloc(sizeof(*request), GFP_KERNEL);
1969 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001970 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01001971
Chris Wilsondb53a302011-02-03 11:57:46 +00001972 ret = i915_add_request(ring, NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +01001973 if (ret) {
1974 kfree(request);
1975 return ret;
1976 }
1977
1978 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01001979 }
1980
Chris Wilson78501ea2010-10-27 12:18:21 +01001981 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001982 if (HAS_PCH_SPLIT(ring->dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001983 ier = I915_READ(DEIER) | I915_READ(GTIER);
1984 else
1985 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001986 if (!ier) {
1987 DRM_ERROR("something (likely vbetool) disabled "
1988 "interrupts, re-enabling\n");
Chris Wilsonf01c22f2011-06-28 11:48:51 +01001989 ring->dev->driver->irq_preinstall(ring->dev);
1990 ring->dev->driver->irq_postinstall(ring->dev);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001991 }
1992
Chris Wilsondb53a302011-02-03 11:57:46 +00001993 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001994
Chris Wilsonb2223492010-10-27 15:27:33 +01001995 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001996 if (ring->irq_get(ring)) {
Chris Wilsonce453d82011-02-21 14:43:56 +00001997 if (dev_priv->mm.interruptible)
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001998 ret = wait_event_interruptible(ring->irq_queue,
1999 i915_seqno_passed(ring->get_seqno(ring), seqno)
2000 || atomic_read(&dev_priv->mm.wedged));
2001 else
2002 wait_event(ring->irq_queue,
2003 i915_seqno_passed(ring->get_seqno(ring), seqno)
2004 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002005
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002006 ring->irq_put(ring);
Chris Wilsonb5ba1772010-12-14 12:17:15 +00002007 } else if (wait_for(i915_seqno_passed(ring->get_seqno(ring),
2008 seqno) ||
2009 atomic_read(&dev_priv->mm.wedged), 3000))
2010 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01002011 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002012
Chris Wilsondb53a302011-02-03 11:57:46 +00002013 trace_i915_gem_request_wait_end(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002014 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002015 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002016 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002017
2018 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002019 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002020 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002021 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002022
2023 /* Directly dispatch request retiring. While we have the work queue
2024 * to handle this, the waiter on a request often wants an associated
2025 * buffer to have made it to the inactive list, and we would need
2026 * a separate wait queue to handle that.
2027 */
2028 if (ret == 0)
Chris Wilsondb53a302011-02-03 11:57:46 +00002029 i915_gem_retire_requests_ring(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002030
2031 return ret;
2032}
2033
Daniel Vetter48764bf2009-09-15 22:57:32 +02002034/**
Eric Anholt673a3942008-07-30 12:06:12 -07002035 * Ensures that all rendering to the object has completed and the object is
2036 * safe to unbind from the GTT or access from the CPU.
2037 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00002038int
Chris Wilsonce453d82011-02-21 14:43:56 +00002039i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002040{
Eric Anholt673a3942008-07-30 12:06:12 -07002041 int ret;
2042
Eric Anholte47c68e2008-11-14 13:35:19 -08002043 /* This function only exists to support waiting for existing rendering,
2044 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002045 */
Chris Wilson05394f32010-11-08 19:18:58 +00002046 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002047
2048 /* If there is rendering queued on the buffer being evicted, wait for
2049 * it.
2050 */
Chris Wilson05394f32010-11-08 19:18:58 +00002051 if (obj->active) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002052 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002053 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002054 return ret;
2055 }
2056
2057 return 0;
2058}
2059
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002060static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2061{
2062 u32 old_write_domain, old_read_domains;
2063
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002064 /* Act a barrier for all accesses through the GTT */
2065 mb();
2066
2067 /* Force a pagefault for domain tracking on next user access */
2068 i915_gem_release_mmap(obj);
2069
Keith Packardb97c3d92011-06-24 21:02:59 -07002070 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2071 return;
2072
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002073 old_read_domains = obj->base.read_domains;
2074 old_write_domain = obj->base.write_domain;
2075
2076 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2077 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2078
2079 trace_i915_gem_object_change_domain(obj,
2080 old_read_domains,
2081 old_write_domain);
2082}
2083
Eric Anholt673a3942008-07-30 12:06:12 -07002084/**
2085 * Unbinds an object from the GTT aperture.
2086 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002087int
Chris Wilson05394f32010-11-08 19:18:58 +00002088i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002089{
Eric Anholt673a3942008-07-30 12:06:12 -07002090 int ret = 0;
2091
Chris Wilson05394f32010-11-08 19:18:58 +00002092 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002093 return 0;
2094
Chris Wilson05394f32010-11-08 19:18:58 +00002095 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002096 DRM_ERROR("Attempting to unbind pinned buffer\n");
2097 return -EINVAL;
2098 }
2099
Chris Wilsona8198ee2011-04-13 22:04:09 +01002100 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson8dc17752010-07-23 23:18:51 +01002101 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002102 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002103 /* Continue on if we fail due to EIO, the GPU is hung so we
2104 * should be safe and we need to cleanup or else we might
2105 * cause memory corruption through use-after-free.
2106 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002107
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002108 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002109
2110 /* Move the object to the CPU domain to ensure that
2111 * any possible CPU writes while it's not in the GTT
2112 * are flushed when we go to remap it.
2113 */
2114 if (ret == 0)
2115 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2116 if (ret == -ERESTARTSYS)
2117 return ret;
Chris Wilson812ed4922010-09-30 15:08:57 +01002118 if (ret) {
Chris Wilsona8198ee2011-04-13 22:04:09 +01002119 /* In the event of a disaster, abandon all caches and
2120 * hope for the best.
2121 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002122 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002123 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002124 }
Eric Anholt673a3942008-07-30 12:06:12 -07002125
Daniel Vetter96b47b62009-12-15 17:50:00 +01002126 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002127 ret = i915_gem_object_put_fence(obj);
2128 if (ret == -ERESTARTSYS)
2129 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002130
Chris Wilsondb53a302011-02-03 11:57:46 +00002131 trace_i915_gem_object_unbind(obj);
2132
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002133 i915_gem_gtt_unbind_object(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002134 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002135
Chris Wilson6299f992010-11-24 12:23:44 +00002136 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002137 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002138 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002139 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002140
Chris Wilson05394f32010-11-08 19:18:58 +00002141 drm_mm_put_block(obj->gtt_space);
2142 obj->gtt_space = NULL;
2143 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002144
Chris Wilson05394f32010-11-08 19:18:58 +00002145 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002146 i915_gem_object_truncate(obj);
2147
Chris Wilson8dc17752010-07-23 23:18:51 +01002148 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002149}
2150
Chris Wilson88241782011-01-07 17:09:48 +00002151int
Chris Wilsondb53a302011-02-03 11:57:46 +00002152i915_gem_flush_ring(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002153 uint32_t invalidate_domains,
2154 uint32_t flush_domains)
2155{
Chris Wilson88241782011-01-07 17:09:48 +00002156 int ret;
2157
Chris Wilson36d527d2011-03-19 22:26:49 +00002158 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2159 return 0;
2160
Chris Wilsondb53a302011-02-03 11:57:46 +00002161 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2162
Chris Wilson88241782011-01-07 17:09:48 +00002163 ret = ring->flush(ring, invalidate_domains, flush_domains);
2164 if (ret)
2165 return ret;
2166
Chris Wilson36d527d2011-03-19 22:26:49 +00002167 if (flush_domains & I915_GEM_GPU_DOMAINS)
2168 i915_gem_process_flushing_list(ring, flush_domains);
2169
Chris Wilson88241782011-01-07 17:09:48 +00002170 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002171}
2172
Chris Wilsondb53a302011-02-03 11:57:46 +00002173static int i915_ring_idle(struct intel_ring_buffer *ring)
Chris Wilsona56ba562010-09-28 10:07:56 +01002174{
Chris Wilson88241782011-01-07 17:09:48 +00002175 int ret;
2176
Chris Wilson395b70b2010-10-28 21:28:46 +01002177 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002178 return 0;
2179
Chris Wilson88241782011-01-07 17:09:48 +00002180 if (!list_empty(&ring->gpu_write_list)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002181 ret = i915_gem_flush_ring(ring,
Chris Wilson0ac74c62010-12-06 14:36:02 +00002182 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilson88241782011-01-07 17:09:48 +00002183 if (ret)
2184 return ret;
2185 }
2186
Chris Wilsonce453d82011-02-21 14:43:56 +00002187 return i915_wait_request(ring, i915_gem_next_request_seqno(ring));
Chris Wilsona56ba562010-09-28 10:07:56 +01002188}
2189
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002190int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002191i915_gpu_idle(struct drm_device *dev)
2192{
2193 drm_i915_private_t *dev_priv = dev->dev_private;
2194 bool lists_empty;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002195 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002196
Zou Nan haid1b851f2010-05-21 09:08:57 +08002197 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002198 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002199 if (lists_empty)
2200 return 0;
2201
2202 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002203 for (i = 0; i < I915_NUM_RINGS; i++) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002204 ret = i915_ring_idle(&dev_priv->ring[i]);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002205 if (ret)
2206 return ret;
2207 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002208
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002209 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002210}
2211
Daniel Vetterc6642782010-11-12 13:46:18 +00002212static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2213 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002214{
Chris Wilson05394f32010-11-08 19:18:58 +00002215 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002216 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002217 u32 size = obj->gtt_space->size;
2218 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002219 uint64_t val;
2220
Chris Wilson05394f32010-11-08 19:18:58 +00002221 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002222 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002223 val |= obj->gtt_offset & 0xfffff000;
2224 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002225 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2226
Chris Wilson05394f32010-11-08 19:18:58 +00002227 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002228 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2229 val |= I965_FENCE_REG_VALID;
2230
Daniel Vetterc6642782010-11-12 13:46:18 +00002231 if (pipelined) {
2232 int ret = intel_ring_begin(pipelined, 6);
2233 if (ret)
2234 return ret;
2235
2236 intel_ring_emit(pipelined, MI_NOOP);
2237 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2238 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2239 intel_ring_emit(pipelined, (u32)val);
2240 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2241 intel_ring_emit(pipelined, (u32)(val >> 32));
2242 intel_ring_advance(pipelined);
2243 } else
2244 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2245
2246 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002247}
2248
Daniel Vetterc6642782010-11-12 13:46:18 +00002249static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2250 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002251{
Chris Wilson05394f32010-11-08 19:18:58 +00002252 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002253 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002254 u32 size = obj->gtt_space->size;
2255 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002256 uint64_t val;
2257
Chris Wilson05394f32010-11-08 19:18:58 +00002258 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002259 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002260 val |= obj->gtt_offset & 0xfffff000;
2261 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2262 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002263 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2264 val |= I965_FENCE_REG_VALID;
2265
Daniel Vetterc6642782010-11-12 13:46:18 +00002266 if (pipelined) {
2267 int ret = intel_ring_begin(pipelined, 6);
2268 if (ret)
2269 return ret;
2270
2271 intel_ring_emit(pipelined, MI_NOOP);
2272 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2273 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2274 intel_ring_emit(pipelined, (u32)val);
2275 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2276 intel_ring_emit(pipelined, (u32)(val >> 32));
2277 intel_ring_advance(pipelined);
2278 } else
2279 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2280
2281 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002282}
2283
Daniel Vetterc6642782010-11-12 13:46:18 +00002284static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2285 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002286{
Chris Wilson05394f32010-11-08 19:18:58 +00002287 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002288 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002289 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002290 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002291 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002292
Daniel Vetterc6642782010-11-12 13:46:18 +00002293 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2294 (size & -size) != size ||
2295 (obj->gtt_offset & (size - 1)),
2296 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2297 obj->gtt_offset, obj->map_and_fenceable, size))
2298 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002299
Daniel Vetterc6642782010-11-12 13:46:18 +00002300 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002301 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002302 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002303 tile_width = 512;
2304
2305 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002306 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002307 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308
Chris Wilson05394f32010-11-08 19:18:58 +00002309 val = obj->gtt_offset;
2310 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002311 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002312 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002313 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2314 val |= I830_FENCE_REG_VALID;
2315
Chris Wilson05394f32010-11-08 19:18:58 +00002316 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002317 if (fence_reg < 8)
2318 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002319 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002320 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002321
2322 if (pipelined) {
2323 int ret = intel_ring_begin(pipelined, 4);
2324 if (ret)
2325 return ret;
2326
2327 intel_ring_emit(pipelined, MI_NOOP);
2328 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2329 intel_ring_emit(pipelined, fence_reg);
2330 intel_ring_emit(pipelined, val);
2331 intel_ring_advance(pipelined);
2332 } else
2333 I915_WRITE(fence_reg, val);
2334
2335 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002336}
2337
Daniel Vetterc6642782010-11-12 13:46:18 +00002338static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2339 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002340{
Chris Wilson05394f32010-11-08 19:18:58 +00002341 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002342 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002343 u32 size = obj->gtt_space->size;
2344 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002345 uint32_t val;
2346 uint32_t pitch_val;
2347
Daniel Vetterc6642782010-11-12 13:46:18 +00002348 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2349 (size & -size) != size ||
2350 (obj->gtt_offset & (size - 1)),
2351 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2352 obj->gtt_offset, size))
2353 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002354
Chris Wilson05394f32010-11-08 19:18:58 +00002355 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002356 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002357
Chris Wilson05394f32010-11-08 19:18:58 +00002358 val = obj->gtt_offset;
2359 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002360 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002361 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2363 val |= I830_FENCE_REG_VALID;
2364
Daniel Vetterc6642782010-11-12 13:46:18 +00002365 if (pipelined) {
2366 int ret = intel_ring_begin(pipelined, 4);
2367 if (ret)
2368 return ret;
2369
2370 intel_ring_emit(pipelined, MI_NOOP);
2371 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2372 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2373 intel_ring_emit(pipelined, val);
2374 intel_ring_advance(pipelined);
2375 } else
2376 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2377
2378 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002379}
2380
Chris Wilsond9e86c02010-11-10 16:40:20 +00002381static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2382{
2383 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2384}
2385
2386static int
2387i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002388 struct intel_ring_buffer *pipelined)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002389{
2390 int ret;
2391
2392 if (obj->fenced_gpu_access) {
Chris Wilson88241782011-01-07 17:09:48 +00002393 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002394 ret = i915_gem_flush_ring(obj->last_fenced_ring,
Chris Wilson88241782011-01-07 17:09:48 +00002395 0, obj->base.write_domain);
2396 if (ret)
2397 return ret;
2398 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002399
2400 obj->fenced_gpu_access = false;
2401 }
2402
2403 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2404 if (!ring_passed_seqno(obj->last_fenced_ring,
2405 obj->last_fenced_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002406 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002407 obj->last_fenced_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002408 if (ret)
2409 return ret;
2410 }
2411
2412 obj->last_fenced_seqno = 0;
2413 obj->last_fenced_ring = NULL;
2414 }
2415
Chris Wilson63256ec2011-01-04 18:42:07 +00002416 /* Ensure that all CPU reads are completed before installing a fence
2417 * and all writes before removing the fence.
2418 */
2419 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2420 mb();
2421
Chris Wilsond9e86c02010-11-10 16:40:20 +00002422 return 0;
2423}
2424
2425int
2426i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2427{
2428 int ret;
2429
2430 if (obj->tiling_mode)
2431 i915_gem_release_mmap(obj);
2432
Chris Wilsonce453d82011-02-21 14:43:56 +00002433 ret = i915_gem_object_flush_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002434 if (ret)
2435 return ret;
2436
2437 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2438 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2439 i915_gem_clear_fence_reg(obj->base.dev,
2440 &dev_priv->fence_regs[obj->fence_reg]);
2441
2442 obj->fence_reg = I915_FENCE_REG_NONE;
2443 }
2444
2445 return 0;
2446}
2447
2448static struct drm_i915_fence_reg *
2449i915_find_fence_reg(struct drm_device *dev,
2450 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002451{
Daniel Vetterae3db242010-02-19 11:51:58 +01002452 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002453 struct drm_i915_fence_reg *reg, *first, *avail;
2454 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002455
2456 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002457 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002458 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2459 reg = &dev_priv->fence_regs[i];
2460 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002461 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002462
Chris Wilson05394f32010-11-08 19:18:58 +00002463 if (!reg->obj->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002464 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002465 }
2466
Chris Wilsond9e86c02010-11-10 16:40:20 +00002467 if (avail == NULL)
2468 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002469
2470 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002471 avail = first = NULL;
2472 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2473 if (reg->obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002474 continue;
2475
Chris Wilsond9e86c02010-11-10 16:40:20 +00002476 if (first == NULL)
2477 first = reg;
2478
2479 if (!pipelined ||
2480 !reg->obj->last_fenced_ring ||
2481 reg->obj->last_fenced_ring == pipelined) {
2482 avail = reg;
2483 break;
2484 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002485 }
2486
Chris Wilsond9e86c02010-11-10 16:40:20 +00002487 if (avail == NULL)
2488 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002489
Chris Wilsona00b10c2010-09-24 21:15:47 +01002490 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002491}
2492
Jesse Barnesde151cf2008-11-12 10:03:55 -08002493/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002494 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002495 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002496 * @pipelined: ring on which to queue the change, or NULL for CPU access
2497 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002498 *
2499 * When mapping objects through the GTT, userspace wants to be able to write
2500 * to them without having to worry about swizzling if the object is tiled.
2501 *
2502 * This function walks the fence regs looking for a free one for @obj,
2503 * stealing one if it can't find any.
2504 *
2505 * It then sets up the reg based on the object's properties: address, pitch
2506 * and tiling format.
2507 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002508int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002509i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002510 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002511{
Chris Wilson05394f32010-11-08 19:18:58 +00002512 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002513 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002514 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002515 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002516
Chris Wilson6bda10d2010-12-05 21:04:18 +00002517 /* XXX disable pipelining. There are bugs. Shocking. */
2518 pipelined = NULL;
2519
Chris Wilsond9e86c02010-11-10 16:40:20 +00002520 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002521 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2522 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002523 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002524
Chris Wilson29c5a582011-03-17 15:23:22 +00002525 if (obj->tiling_changed) {
2526 ret = i915_gem_object_flush_fence(obj, pipelined);
2527 if (ret)
2528 return ret;
2529
2530 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2531 pipelined = NULL;
2532
2533 if (pipelined) {
2534 reg->setup_seqno =
2535 i915_gem_next_request_seqno(pipelined);
2536 obj->last_fenced_seqno = reg->setup_seqno;
2537 obj->last_fenced_ring = pipelined;
2538 }
2539
2540 goto update;
2541 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002542
2543 if (!pipelined) {
2544 if (reg->setup_seqno) {
2545 if (!ring_passed_seqno(obj->last_fenced_ring,
2546 reg->setup_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002547 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002548 reg->setup_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002549 if (ret)
2550 return ret;
2551 }
2552
2553 reg->setup_seqno = 0;
2554 }
2555 } else if (obj->last_fenced_ring &&
2556 obj->last_fenced_ring != pipelined) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002557 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002558 if (ret)
2559 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002560 }
2561
Eric Anholta09ba7f2009-08-29 12:49:51 -07002562 return 0;
2563 }
2564
Chris Wilsond9e86c02010-11-10 16:40:20 +00002565 reg = i915_find_fence_reg(dev, pipelined);
2566 if (reg == NULL)
2567 return -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002568
Chris Wilsonce453d82011-02-21 14:43:56 +00002569 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002570 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002571 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002572
Chris Wilsond9e86c02010-11-10 16:40:20 +00002573 if (reg->obj) {
2574 struct drm_i915_gem_object *old = reg->obj;
2575
2576 drm_gem_object_reference(&old->base);
2577
2578 if (old->tiling_mode)
2579 i915_gem_release_mmap(old);
2580
Chris Wilsonce453d82011-02-21 14:43:56 +00002581 ret = i915_gem_object_flush_fence(old, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002582 if (ret) {
2583 drm_gem_object_unreference(&old->base);
2584 return ret;
2585 }
2586
2587 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2588 pipelined = NULL;
2589
2590 old->fence_reg = I915_FENCE_REG_NONE;
2591 old->last_fenced_ring = pipelined;
2592 old->last_fenced_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002593 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002594
2595 drm_gem_object_unreference(&old->base);
2596 } else if (obj->last_fenced_seqno == 0)
2597 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002598
Jesse Barnesde151cf2008-11-12 10:03:55 -08002599 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002600 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2601 obj->fence_reg = reg - dev_priv->fence_regs;
2602 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002603
Chris Wilsond9e86c02010-11-10 16:40:20 +00002604 reg->setup_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002605 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002606 obj->last_fenced_seqno = reg->setup_seqno;
2607
2608update:
2609 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002610 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002611 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002612 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002613 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002614 break;
2615 case 5:
2616 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002617 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002618 break;
2619 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002620 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002621 break;
2622 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002623 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002624 break;
2625 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002626
Daniel Vetterc6642782010-11-12 13:46:18 +00002627 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002628}
2629
2630/**
2631 * i915_gem_clear_fence_reg - clear out fence register info
2632 * @obj: object to clear
2633 *
2634 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002635 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002636 */
2637static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002638i915_gem_clear_fence_reg(struct drm_device *dev,
2639 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002640{
Jesse Barnes79e53942008-11-07 14:24:08 -08002641 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002642 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002643
Chris Wilsone259bef2010-09-17 00:32:02 +01002644 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002645 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002646 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002647 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002648 break;
2649 case 5:
2650 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002651 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002652 break;
2653 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002654 if (fence_reg >= 8)
2655 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002656 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002657 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002658 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002659
2660 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002661 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002662 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002663
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002664 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002665 reg->obj = NULL;
2666 reg->setup_seqno = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002667}
2668
2669/**
Eric Anholt673a3942008-07-30 12:06:12 -07002670 * Finds free space in the GTT aperture and binds the object there.
2671 */
2672static int
Chris Wilson05394f32010-11-08 19:18:58 +00002673i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002674 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002675 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002676{
Chris Wilson05394f32010-11-08 19:18:58 +00002677 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002678 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002679 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002680 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002681 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002682 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002683 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002684
Chris Wilson05394f32010-11-08 19:18:58 +00002685 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002686 DRM_ERROR("Attempting to bind a purgeable object\n");
2687 return -EINVAL;
2688 }
2689
Chris Wilsone28f8712011-07-18 13:11:49 -07002690 fence_size = i915_gem_get_gtt_size(dev,
2691 obj->base.size,
2692 obj->tiling_mode);
2693 fence_alignment = i915_gem_get_gtt_alignment(dev,
2694 obj->base.size,
2695 obj->tiling_mode);
2696 unfenced_alignment =
2697 i915_gem_get_unfenced_gtt_alignment(dev,
2698 obj->base.size,
2699 obj->tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002700
Eric Anholt673a3942008-07-30 12:06:12 -07002701 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002702 alignment = map_and_fenceable ? fence_alignment :
2703 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002704 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002705 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2706 return -EINVAL;
2707 }
2708
Chris Wilson05394f32010-11-08 19:18:58 +00002709 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002710
Chris Wilson654fc602010-05-27 13:18:21 +01002711 /* If the object is bigger than the entire aperture, reject it early
2712 * before evicting everything in a vain attempt to find space.
2713 */
Chris Wilson05394f32010-11-08 19:18:58 +00002714 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002715 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002716 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2717 return -E2BIG;
2718 }
2719
Eric Anholt673a3942008-07-30 12:06:12 -07002720 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002721 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002722 free_space =
2723 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002724 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002725 dev_priv->mm.gtt_mappable_end,
2726 0);
2727 else
2728 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002729 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002730
2731 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002732 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002733 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002734 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002735 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002736 dev_priv->mm.gtt_mappable_end,
2737 0);
2738 else
Chris Wilson05394f32010-11-08 19:18:58 +00002739 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002740 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002741 }
Chris Wilson05394f32010-11-08 19:18:58 +00002742 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002743 /* If the gtt is empty and we're still having trouble
2744 * fitting our object in, we're out of memory.
2745 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002746 ret = i915_gem_evict_something(dev, size, alignment,
2747 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002748 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002749 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002750
Eric Anholt673a3942008-07-30 12:06:12 -07002751 goto search_free;
2752 }
2753
Chris Wilsone5281cc2010-10-28 13:45:36 +01002754 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002755 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002756 drm_mm_put_block(obj->gtt_space);
2757 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002758
2759 if (ret == -ENOMEM) {
Chris Wilson809b6332011-01-10 17:33:15 +00002760 /* first try to reclaim some memory by clearing the GTT */
2761 ret = i915_gem_evict_everything(dev, false);
Chris Wilson07f73f62009-09-14 16:50:30 +01002762 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002763 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002764 if (gfpmask) {
2765 gfpmask = 0;
2766 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002767 }
2768
Chris Wilson809b6332011-01-10 17:33:15 +00002769 return -ENOMEM;
Chris Wilson07f73f62009-09-14 16:50:30 +01002770 }
2771
2772 goto search_free;
2773 }
2774
Eric Anholt673a3942008-07-30 12:06:12 -07002775 return ret;
2776 }
2777
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002778 ret = i915_gem_gtt_bind_object(obj);
2779 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002780 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002781 drm_mm_put_block(obj->gtt_space);
2782 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002783
Chris Wilson809b6332011-01-10 17:33:15 +00002784 if (i915_gem_evict_everything(dev, false))
Chris Wilson07f73f62009-09-14 16:50:30 +01002785 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002786
2787 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002788 }
Eric Anholt673a3942008-07-30 12:06:12 -07002789
Chris Wilson6299f992010-11-24 12:23:44 +00002790 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002791 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002792
Eric Anholt673a3942008-07-30 12:06:12 -07002793 /* Assert that the object is not currently in any GPU domain. As it
2794 * wasn't in the GTT, there shouldn't be any way it could have been in
2795 * a GPU cache
2796 */
Chris Wilson05394f32010-11-08 19:18:58 +00002797 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2798 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002799
Chris Wilson6299f992010-11-24 12:23:44 +00002800 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002801
Daniel Vetter75e9e912010-11-04 17:11:09 +01002802 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002803 obj->gtt_space->size == fence_size &&
Akshay Joshi0206e352011-08-16 15:34:10 -04002804 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002805
Daniel Vetter75e9e912010-11-04 17:11:09 +01002806 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002807 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002808
Chris Wilson05394f32010-11-08 19:18:58 +00002809 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002810
Chris Wilsondb53a302011-02-03 11:57:46 +00002811 trace_i915_gem_object_bind(obj, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002812 return 0;
2813}
2814
2815void
Chris Wilson05394f32010-11-08 19:18:58 +00002816i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002817{
Eric Anholt673a3942008-07-30 12:06:12 -07002818 /* If we don't have a page list set up, then we're not pinned
2819 * to GPU, and we can ignore the cache flush because it'll happen
2820 * again at bind time.
2821 */
Chris Wilson05394f32010-11-08 19:18:58 +00002822 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002823 return;
2824
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002825 /* If the GPU is snooping the contents of the CPU cache,
2826 * we do not need to manually clear the CPU cache lines. However,
2827 * the caches are only snooped when the render cache is
2828 * flushed/invalidated. As we always have to emit invalidations
2829 * and flushes when moving into and out of the RENDER domain, correct
2830 * snooping behaviour occurs naturally as the result of our domain
2831 * tracking.
2832 */
2833 if (obj->cache_level != I915_CACHE_NONE)
2834 return;
2835
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002836 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002837
Chris Wilson05394f32010-11-08 19:18:58 +00002838 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002839}
2840
Eric Anholte47c68e2008-11-14 13:35:19 -08002841/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson88241782011-01-07 17:09:48 +00002842static int
Chris Wilson3619df02010-11-28 15:37:17 +00002843i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002844{
Chris Wilson05394f32010-11-08 19:18:58 +00002845 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson88241782011-01-07 17:09:48 +00002846 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002847
2848 /* Queue the GPU write cache flushing we need. */
Chris Wilsondb53a302011-02-03 11:57:46 +00002849 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002850}
2851
2852/** Flushes the GTT write domain for the object if it's dirty. */
2853static void
Chris Wilson05394f32010-11-08 19:18:58 +00002854i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002855{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002856 uint32_t old_write_domain;
2857
Chris Wilson05394f32010-11-08 19:18:58 +00002858 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002859 return;
2860
Chris Wilson63256ec2011-01-04 18:42:07 +00002861 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002862 * to it immediately go to main memory as far as we know, so there's
2863 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002864 *
2865 * However, we do have to enforce the order so that all writes through
2866 * the GTT land before any writes to the device, such as updates to
2867 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002868 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002869 wmb();
2870
Chris Wilson05394f32010-11-08 19:18:58 +00002871 old_write_domain = obj->base.write_domain;
2872 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002873
2874 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002875 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002876 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002877}
2878
2879/** Flushes the CPU write domain for the object if it's dirty. */
2880static void
Chris Wilson05394f32010-11-08 19:18:58 +00002881i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002882{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002883 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002884
Chris Wilson05394f32010-11-08 19:18:58 +00002885 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002886 return;
2887
2888 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002889 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002890 old_write_domain = obj->base.write_domain;
2891 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002892
2893 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002894 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002895 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002896}
2897
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002898/**
2899 * Moves a single object to the GTT read, and possibly write domain.
2900 *
2901 * This function returns when the move is complete, including waiting on
2902 * flushes to occur.
2903 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002904int
Chris Wilson20217462010-11-23 15:26:33 +00002905i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002906{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002907 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002908 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002909
Eric Anholt02354392008-11-26 13:58:13 -08002910 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002911 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002912 return -EINVAL;
2913
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002914 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2915 return 0;
2916
Chris Wilson88241782011-01-07 17:09:48 +00002917 ret = i915_gem_object_flush_gpu_write_domain(obj);
2918 if (ret)
2919 return ret;
2920
Chris Wilson87ca9c82010-12-02 09:42:56 +00002921 if (obj->pending_gpu_write || write) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002922 ret = i915_gem_object_wait_rendering(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00002923 if (ret)
2924 return ret;
2925 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002926
Chris Wilson72133422010-09-13 23:56:38 +01002927 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002928
Chris Wilson05394f32010-11-08 19:18:58 +00002929 old_write_domain = obj->base.write_domain;
2930 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002931
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002932 /* It should now be out of any other write domains, and we can update
2933 * the domain values for our changes.
2934 */
Chris Wilson05394f32010-11-08 19:18:58 +00002935 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2936 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002937 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002938 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2939 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2940 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002941 }
2942
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002943 trace_i915_gem_object_change_domain(obj,
2944 old_read_domains,
2945 old_write_domain);
2946
Eric Anholte47c68e2008-11-14 13:35:19 -08002947 return 0;
2948}
2949
Chris Wilsone4ffd172011-04-04 09:44:39 +01002950int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2951 enum i915_cache_level cache_level)
2952{
2953 int ret;
2954
2955 if (obj->cache_level == cache_level)
2956 return 0;
2957
2958 if (obj->pin_count) {
2959 DRM_DEBUG("can not change the cache level of pinned objects\n");
2960 return -EBUSY;
2961 }
2962
2963 if (obj->gtt_space) {
2964 ret = i915_gem_object_finish_gpu(obj);
2965 if (ret)
2966 return ret;
2967
2968 i915_gem_object_finish_gtt(obj);
2969
2970 /* Before SandyBridge, you could not use tiling or fence
2971 * registers with snooped memory, so relinquish any fences
2972 * currently pointing to our region in the aperture.
2973 */
2974 if (INTEL_INFO(obj->base.dev)->gen < 6) {
2975 ret = i915_gem_object_put_fence(obj);
2976 if (ret)
2977 return ret;
2978 }
2979
2980 i915_gem_gtt_rebind_object(obj, cache_level);
2981 }
2982
2983 if (cache_level == I915_CACHE_NONE) {
2984 u32 old_read_domains, old_write_domain;
2985
2986 /* If we're coming from LLC cached, then we haven't
2987 * actually been tracking whether the data is in the
2988 * CPU cache or not, since we only allow one bit set
2989 * in obj->write_domain and have been skipping the clflushes.
2990 * Just set it to the CPU cache for now.
2991 */
2992 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
2993 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
2994
2995 old_read_domains = obj->base.read_domains;
2996 old_write_domain = obj->base.write_domain;
2997
2998 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
2999 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3000
3001 trace_i915_gem_object_change_domain(obj,
3002 old_read_domains,
3003 old_write_domain);
3004 }
3005
3006 obj->cache_level = cache_level;
3007 return 0;
3008}
3009
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003010/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003011 * Prepare buffer for display plane (scanout, cursors, etc).
3012 * Can be called from an uninterruptible phase (modesetting) and allows
3013 * any flushes to be pipelined (for pageflips).
3014 *
3015 * For the display plane, we want to be in the GTT but out of any write
3016 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
3017 * ability to pipeline the waits, pinning and any additional subtleties
3018 * that may differentiate the display plane from ordinary buffers.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003019 */
3020int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003021i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3022 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003023 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003024{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003025 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003026 int ret;
3027
Chris Wilson88241782011-01-07 17:09:48 +00003028 ret = i915_gem_object_flush_gpu_write_domain(obj);
3029 if (ret)
3030 return ret;
3031
Chris Wilson0be73282010-12-06 14:36:27 +00003032 if (pipelined != obj->ring) {
Chris Wilsonce453d82011-02-21 14:43:56 +00003033 ret = i915_gem_object_wait_rendering(obj);
Keith Packardf0b69ef2011-07-19 16:21:40 -07003034 if (ret == -ERESTARTSYS)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003035 return ret;
3036 }
3037
Eric Anholta7ef0642011-03-29 16:59:54 -07003038 /* The display engine is not coherent with the LLC cache on gen6. As
3039 * a result, we make sure that the pinning that is about to occur is
3040 * done with uncached PTEs. This is lowest common denominator for all
3041 * chipsets.
3042 *
3043 * However for gen6+, we could do better by using the GFDT bit instead
3044 * of uncaching, which would allow us to flush all the LLC-cached data
3045 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3046 */
3047 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3048 if (ret)
3049 return ret;
3050
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003051 /* As the user may map the buffer once pinned in the display plane
3052 * (e.g. libkms for the bootup splash), we have to ensure that we
3053 * always use map_and_fenceable for all scanout buffers.
3054 */
3055 ret = i915_gem_object_pin(obj, alignment, true);
3056 if (ret)
3057 return ret;
3058
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003059 i915_gem_object_flush_cpu_write_domain(obj);
3060
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003061 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003062 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003063
3064 /* It should now be out of any other write domains, and we can update
3065 * the domain values for our changes.
3066 */
3067 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003068 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003069
3070 trace_i915_gem_object_change_domain(obj,
3071 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003072 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003073
3074 return 0;
3075}
3076
Chris Wilson85345512010-11-13 09:49:11 +00003077int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003078i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003079{
Chris Wilson88241782011-01-07 17:09:48 +00003080 int ret;
3081
Chris Wilsona8198ee2011-04-13 22:04:09 +01003082 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003083 return 0;
3084
Chris Wilson88241782011-01-07 17:09:48 +00003085 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003086 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Chris Wilson88241782011-01-07 17:09:48 +00003087 if (ret)
3088 return ret;
3089 }
Chris Wilson85345512010-11-13 09:49:11 +00003090
Chris Wilsona8198ee2011-04-13 22:04:09 +01003091 /* Ensure that we invalidate the GPU's caches and TLBs. */
3092 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3093
Chris Wilsonce453d82011-02-21 14:43:56 +00003094 return i915_gem_object_wait_rendering(obj);
Chris Wilson85345512010-11-13 09:49:11 +00003095}
3096
Eric Anholte47c68e2008-11-14 13:35:19 -08003097/**
3098 * Moves a single object to the CPU read, and possibly write domain.
3099 *
3100 * This function returns when the move is complete, including waiting on
3101 * flushes to occur.
3102 */
3103static int
Chris Wilson919926a2010-11-12 13:42:53 +00003104i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003105{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003106 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003107 int ret;
3108
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003109 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3110 return 0;
3111
Chris Wilson88241782011-01-07 17:09:48 +00003112 ret = i915_gem_object_flush_gpu_write_domain(obj);
3113 if (ret)
3114 return ret;
3115
Chris Wilsonce453d82011-02-21 14:43:56 +00003116 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003117 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003118 return ret;
3119
3120 i915_gem_object_flush_gtt_write_domain(obj);
3121
3122 /* If we have a partially-valid cache of the object in the CPU,
3123 * finish invalidating it and free the per-page flags.
3124 */
3125 i915_gem_object_set_to_full_cpu_read_domain(obj);
3126
Chris Wilson05394f32010-11-08 19:18:58 +00003127 old_write_domain = obj->base.write_domain;
3128 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003129
Eric Anholte47c68e2008-11-14 13:35:19 -08003130 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003131 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003132 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003133
Chris Wilson05394f32010-11-08 19:18:58 +00003134 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003135 }
3136
3137 /* It should now be out of any other write domains, and we can update
3138 * the domain values for our changes.
3139 */
Chris Wilson05394f32010-11-08 19:18:58 +00003140 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003141
3142 /* If we're writing through the CPU, then the GPU read domains will
3143 * need to be invalidated at next use.
3144 */
3145 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003146 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3147 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003148 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003149
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003150 trace_i915_gem_object_change_domain(obj,
3151 old_read_domains,
3152 old_write_domain);
3153
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003154 return 0;
3155}
3156
Eric Anholt673a3942008-07-30 12:06:12 -07003157/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003158 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003159 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003160 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3161 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3162 */
3163static void
Chris Wilson05394f32010-11-08 19:18:58 +00003164i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003165{
Chris Wilson05394f32010-11-08 19:18:58 +00003166 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003167 return;
3168
3169 /* If we're partially in the CPU read domain, finish moving it in.
3170 */
Chris Wilson05394f32010-11-08 19:18:58 +00003171 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003172 int i;
3173
Chris Wilson05394f32010-11-08 19:18:58 +00003174 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3175 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003176 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003177 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003178 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003179 }
3180
3181 /* Free the page_cpu_valid mappings which are now stale, whether
3182 * or not we've got I915_GEM_DOMAIN_CPU.
3183 */
Chris Wilson05394f32010-11-08 19:18:58 +00003184 kfree(obj->page_cpu_valid);
3185 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003186}
3187
3188/**
3189 * Set the CPU read domain on a range of the object.
3190 *
3191 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3192 * not entirely valid. The page_cpu_valid member of the object flags which
3193 * pages have been flushed, and will be respected by
3194 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3195 * of the whole object.
3196 *
3197 * This function returns when the move is complete, including waiting on
3198 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003199 */
3200static int
Chris Wilson05394f32010-11-08 19:18:58 +00003201i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003202 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003203{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003204 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003205 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003206
Chris Wilson05394f32010-11-08 19:18:58 +00003207 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003208 return i915_gem_object_set_to_cpu_domain(obj, 0);
3209
Chris Wilson88241782011-01-07 17:09:48 +00003210 ret = i915_gem_object_flush_gpu_write_domain(obj);
3211 if (ret)
3212 return ret;
3213
Chris Wilsonce453d82011-02-21 14:43:56 +00003214 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003215 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003216 return ret;
Daniel Vetterde18a292010-11-27 22:30:41 +01003217
Eric Anholte47c68e2008-11-14 13:35:19 -08003218 i915_gem_object_flush_gtt_write_domain(obj);
3219
3220 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003221 if (obj->page_cpu_valid == NULL &&
3222 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003223 return 0;
3224
Eric Anholte47c68e2008-11-14 13:35:19 -08003225 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3226 * newly adding I915_GEM_DOMAIN_CPU
3227 */
Chris Wilson05394f32010-11-08 19:18:58 +00003228 if (obj->page_cpu_valid == NULL) {
3229 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3230 GFP_KERNEL);
3231 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003232 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003233 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3234 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003235
3236 /* Flush the cache on any pages that are still invalid from the CPU's
3237 * perspective.
3238 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003239 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3240 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003241 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003242 continue;
3243
Chris Wilson05394f32010-11-08 19:18:58 +00003244 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003245
Chris Wilson05394f32010-11-08 19:18:58 +00003246 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003247 }
3248
Eric Anholte47c68e2008-11-14 13:35:19 -08003249 /* It should now be out of any other write domains, and we can update
3250 * the domain values for our changes.
3251 */
Chris Wilson05394f32010-11-08 19:18:58 +00003252 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003253
Chris Wilson05394f32010-11-08 19:18:58 +00003254 old_read_domains = obj->base.read_domains;
3255 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003256
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003257 trace_i915_gem_object_change_domain(obj,
3258 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003259 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003260
Eric Anholt673a3942008-07-30 12:06:12 -07003261 return 0;
3262}
3263
Eric Anholt673a3942008-07-30 12:06:12 -07003264/* Throttle our rendering by waiting until the ring has completed our requests
3265 * emitted over 20 msec ago.
3266 *
Eric Anholtb9624422009-06-03 07:27:35 +00003267 * Note that if we were to use the current jiffies each time around the loop,
3268 * we wouldn't escape the function with any frames outstanding if the time to
3269 * render a frame was over 20ms.
3270 *
Eric Anholt673a3942008-07-30 12:06:12 -07003271 * This should get us reasonable parallelism between CPU and GPU but also
3272 * relatively low latency when blocking on a particular request to finish.
3273 */
3274static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003275i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003276{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003277 struct drm_i915_private *dev_priv = dev->dev_private;
3278 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003279 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003280 struct drm_i915_gem_request *request;
3281 struct intel_ring_buffer *ring = NULL;
3282 u32 seqno = 0;
3283 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003284
Chris Wilsone110e8d2011-01-26 15:39:14 +00003285 if (atomic_read(&dev_priv->mm.wedged))
3286 return -EIO;
3287
Chris Wilson1c255952010-09-26 11:03:27 +01003288 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003289 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003290 if (time_after_eq(request->emitted_jiffies, recent_enough))
3291 break;
3292
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003293 ring = request->ring;
3294 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003295 }
Chris Wilson1c255952010-09-26 11:03:27 +01003296 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003297
3298 if (seqno == 0)
3299 return 0;
3300
3301 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003302 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003303 /* And wait for the seqno passing without holding any locks and
3304 * causing extra latency for others. This is safe as the irq
3305 * generation is designed to be run atomically and so is
3306 * lockless.
3307 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003308 if (ring->irq_get(ring)) {
3309 ret = wait_event_interruptible(ring->irq_queue,
3310 i915_seqno_passed(ring->get_seqno(ring), seqno)
3311 || atomic_read(&dev_priv->mm.wedged));
3312 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003313
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003314 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3315 ret = -EIO;
3316 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003317 }
3318
3319 if (ret == 0)
3320 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003321
Eric Anholt673a3942008-07-30 12:06:12 -07003322 return ret;
3323}
3324
Eric Anholt673a3942008-07-30 12:06:12 -07003325int
Chris Wilson05394f32010-11-08 19:18:58 +00003326i915_gem_object_pin(struct drm_i915_gem_object *obj,
3327 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003328 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003329{
Chris Wilson05394f32010-11-08 19:18:58 +00003330 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003331 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003332 int ret;
3333
Chris Wilson05394f32010-11-08 19:18:58 +00003334 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003335 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003336
Chris Wilson05394f32010-11-08 19:18:58 +00003337 if (obj->gtt_space != NULL) {
3338 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3339 (map_and_fenceable && !obj->map_and_fenceable)) {
3340 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003341 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003342 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3343 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003344 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003345 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003346 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003347 ret = i915_gem_object_unbind(obj);
3348 if (ret)
3349 return ret;
3350 }
3351 }
3352
Chris Wilson05394f32010-11-08 19:18:58 +00003353 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003354 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003355 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003356 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003357 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003358 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003359
Chris Wilson05394f32010-11-08 19:18:58 +00003360 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003361 if (!obj->active)
3362 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003363 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003364 }
Chris Wilson6299f992010-11-24 12:23:44 +00003365 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003366
Chris Wilson23bc5982010-09-29 16:10:57 +01003367 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003368 return 0;
3369}
3370
3371void
Chris Wilson05394f32010-11-08 19:18:58 +00003372i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003373{
Chris Wilson05394f32010-11-08 19:18:58 +00003374 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003375 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003376
Chris Wilson23bc5982010-09-29 16:10:57 +01003377 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003378 BUG_ON(obj->pin_count == 0);
3379 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003380
Chris Wilson05394f32010-11-08 19:18:58 +00003381 if (--obj->pin_count == 0) {
3382 if (!obj->active)
3383 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003384 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003385 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003386 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003387 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003388}
3389
3390int
3391i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003392 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003393{
3394 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003395 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003396 int ret;
3397
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003398 ret = i915_mutex_lock_interruptible(dev);
3399 if (ret)
3400 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003401
Chris Wilson05394f32010-11-08 19:18:58 +00003402 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003403 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003404 ret = -ENOENT;
3405 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003406 }
Eric Anholt673a3942008-07-30 12:06:12 -07003407
Chris Wilson05394f32010-11-08 19:18:58 +00003408 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003409 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003410 ret = -EINVAL;
3411 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003412 }
3413
Chris Wilson05394f32010-11-08 19:18:58 +00003414 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003415 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3416 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003417 ret = -EINVAL;
3418 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003419 }
3420
Chris Wilson05394f32010-11-08 19:18:58 +00003421 obj->user_pin_count++;
3422 obj->pin_filp = file;
3423 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003424 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003425 if (ret)
3426 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003427 }
3428
3429 /* XXX - flush the CPU caches for pinned objects
3430 * as the X server doesn't manage domains yet
3431 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003432 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003433 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003434out:
Chris Wilson05394f32010-11-08 19:18:58 +00003435 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003436unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003437 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003438 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003439}
3440
3441int
3442i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003443 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003444{
3445 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003446 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003447 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003448
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003449 ret = i915_mutex_lock_interruptible(dev);
3450 if (ret)
3451 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003452
Chris Wilson05394f32010-11-08 19:18:58 +00003453 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003454 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003455 ret = -ENOENT;
3456 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003457 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003458
Chris Wilson05394f32010-11-08 19:18:58 +00003459 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003460 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3461 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003462 ret = -EINVAL;
3463 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003464 }
Chris Wilson05394f32010-11-08 19:18:58 +00003465 obj->user_pin_count--;
3466 if (obj->user_pin_count == 0) {
3467 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003468 i915_gem_object_unpin(obj);
3469 }
Eric Anholt673a3942008-07-30 12:06:12 -07003470
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003471out:
Chris Wilson05394f32010-11-08 19:18:58 +00003472 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003473unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003474 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003475 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003476}
3477
3478int
3479i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003480 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003481{
3482 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003483 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003484 int ret;
3485
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003486 ret = i915_mutex_lock_interruptible(dev);
3487 if (ret)
3488 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003489
Chris Wilson05394f32010-11-08 19:18:58 +00003490 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003491 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003492 ret = -ENOENT;
3493 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003494 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003495
Chris Wilson0be555b2010-08-04 15:36:30 +01003496 /* Count all active objects as busy, even if they are currently not used
3497 * by the gpu. Users of this interface expect objects to eventually
3498 * become non-busy without any further actions, therefore emit any
3499 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003500 */
Chris Wilson05394f32010-11-08 19:18:58 +00003501 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003502 if (args->busy) {
3503 /* Unconditionally flush objects, even when the gpu still uses this
3504 * object. Userspace calling this function indicates that it wants to
3505 * use this buffer rather sooner than later, so issuing the required
3506 * flush earlier is beneficial.
3507 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003508 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003509 ret = i915_gem_flush_ring(obj->ring,
Chris Wilson88241782011-01-07 17:09:48 +00003510 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003511 } else if (obj->ring->outstanding_lazy_request ==
3512 obj->last_rendering_seqno) {
3513 struct drm_i915_gem_request *request;
3514
Chris Wilson7a194872010-12-07 10:38:40 +00003515 /* This ring is not being cleared by active usage,
3516 * so emit a request to do so.
3517 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003518 request = kzalloc(sizeof(*request), GFP_KERNEL);
3519 if (request)
Akshay Joshi0206e352011-08-16 15:34:10 -04003520 ret = i915_add_request(obj->ring, NULL, request);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003521 else
Chris Wilson7a194872010-12-07 10:38:40 +00003522 ret = -ENOMEM;
3523 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003524
3525 /* Update the active list for the hardware's current position.
3526 * Otherwise this only updates on a delayed timer or when irqs
3527 * are actually unmasked, and our working set ends up being
3528 * larger than required.
3529 */
Chris Wilsondb53a302011-02-03 11:57:46 +00003530 i915_gem_retire_requests_ring(obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003531
Chris Wilson05394f32010-11-08 19:18:58 +00003532 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003533 }
Eric Anholt673a3942008-07-30 12:06:12 -07003534
Chris Wilson05394f32010-11-08 19:18:58 +00003535 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003536unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003537 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003538 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003539}
3540
3541int
3542i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3543 struct drm_file *file_priv)
3544{
Akshay Joshi0206e352011-08-16 15:34:10 -04003545 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003546}
3547
Chris Wilson3ef94da2009-09-14 16:50:29 +01003548int
3549i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3550 struct drm_file *file_priv)
3551{
3552 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003553 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003554 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003555
3556 switch (args->madv) {
3557 case I915_MADV_DONTNEED:
3558 case I915_MADV_WILLNEED:
3559 break;
3560 default:
3561 return -EINVAL;
3562 }
3563
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003564 ret = i915_mutex_lock_interruptible(dev);
3565 if (ret)
3566 return ret;
3567
Chris Wilson05394f32010-11-08 19:18:58 +00003568 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003569 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003570 ret = -ENOENT;
3571 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003572 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003573
Chris Wilson05394f32010-11-08 19:18:58 +00003574 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003575 ret = -EINVAL;
3576 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003577 }
3578
Chris Wilson05394f32010-11-08 19:18:58 +00003579 if (obj->madv != __I915_MADV_PURGED)
3580 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003581
Chris Wilson2d7ef392009-09-20 23:13:10 +01003582 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003583 if (i915_gem_object_is_purgeable(obj) &&
3584 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003585 i915_gem_object_truncate(obj);
3586
Chris Wilson05394f32010-11-08 19:18:58 +00003587 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003588
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003589out:
Chris Wilson05394f32010-11-08 19:18:58 +00003590 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003591unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003592 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003593 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003594}
3595
Chris Wilson05394f32010-11-08 19:18:58 +00003596struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3597 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003598{
Chris Wilson73aa8082010-09-30 11:46:12 +01003599 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003600 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003601 struct address_space *mapping;
Daniel Vetterc397b902010-04-09 19:05:07 +00003602
3603 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3604 if (obj == NULL)
3605 return NULL;
3606
3607 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3608 kfree(obj);
3609 return NULL;
3610 }
3611
Hugh Dickins5949eac2011-06-27 16:18:18 -07003612 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3613 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3614
Chris Wilson73aa8082010-09-30 11:46:12 +01003615 i915_gem_info_add_obj(dev_priv, size);
3616
Daniel Vetterc397b902010-04-09 19:05:07 +00003617 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3618 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3619
Eric Anholta1871112011-03-29 16:59:55 -07003620 if (IS_GEN6(dev)) {
3621 /* On Gen6, we can have the GPU use the LLC (the CPU
3622 * cache) for about a 10% performance improvement
3623 * compared to uncached. Graphics requests other than
3624 * display scanout are coherent with the CPU in
3625 * accessing this cache. This means in this mode we
3626 * don't need to clflush on the CPU side, and on the
3627 * GPU side we only need to flush internal caches to
3628 * get data visible to the CPU.
3629 *
3630 * However, we maintain the display planes as UC, and so
3631 * need to rebind when first used as such.
3632 */
3633 obj->cache_level = I915_CACHE_LLC;
3634 } else
3635 obj->cache_level = I915_CACHE_NONE;
3636
Daniel Vetter62b8b212010-04-09 19:05:08 +00003637 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003638 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003639 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003640 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003641 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003642 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003643 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003644 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003645 /* Avoid an unnecessary call to unbind on the first bind. */
3646 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003647
Chris Wilson05394f32010-11-08 19:18:58 +00003648 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003649}
3650
Eric Anholt673a3942008-07-30 12:06:12 -07003651int i915_gem_init_object(struct drm_gem_object *obj)
3652{
Daniel Vetterc397b902010-04-09 19:05:07 +00003653 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003654
Eric Anholt673a3942008-07-30 12:06:12 -07003655 return 0;
3656}
3657
Chris Wilson05394f32010-11-08 19:18:58 +00003658static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003659{
Chris Wilson05394f32010-11-08 19:18:58 +00003660 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003661 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003662 int ret;
3663
3664 ret = i915_gem_object_unbind(obj);
3665 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003666 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003667 &dev_priv->mm.deferred_free_list);
3668 return;
3669 }
3670
Chris Wilson26e12f892011-03-20 11:20:19 +00003671 trace_i915_gem_object_destroy(obj);
3672
Chris Wilson05394f32010-11-08 19:18:58 +00003673 if (obj->base.map_list.map)
Rob Clarkb464e9a2011-08-10 08:09:08 -05003674 drm_gem_free_mmap_offset(&obj->base);
Chris Wilsonbe726152010-07-23 23:18:50 +01003675
Chris Wilson05394f32010-11-08 19:18:58 +00003676 drm_gem_object_release(&obj->base);
3677 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003678
Chris Wilson05394f32010-11-08 19:18:58 +00003679 kfree(obj->page_cpu_valid);
3680 kfree(obj->bit_17);
3681 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003682}
3683
Chris Wilson05394f32010-11-08 19:18:58 +00003684void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003685{
Chris Wilson05394f32010-11-08 19:18:58 +00003686 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3687 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003688
Chris Wilson05394f32010-11-08 19:18:58 +00003689 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003690 i915_gem_object_unpin(obj);
3691
Chris Wilson05394f32010-11-08 19:18:58 +00003692 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003693 i915_gem_detach_phys_object(dev, obj);
3694
Chris Wilsonbe726152010-07-23 23:18:50 +01003695 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003696}
3697
Jesse Barnes5669fca2009-02-17 15:13:31 -08003698int
Eric Anholt673a3942008-07-30 12:06:12 -07003699i915_gem_idle(struct drm_device *dev)
3700{
3701 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003702 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003703
Keith Packard6dbe2772008-10-14 21:41:13 -07003704 mutex_lock(&dev->struct_mutex);
3705
Chris Wilson87acb0a2010-10-19 10:13:00 +01003706 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003707 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003708 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003709 }
Eric Anholt673a3942008-07-30 12:06:12 -07003710
Chris Wilson29105cc2010-01-07 10:39:13 +00003711 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003712 if (ret) {
3713 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003714 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003715 }
Eric Anholt673a3942008-07-30 12:06:12 -07003716
Chris Wilson29105cc2010-01-07 10:39:13 +00003717 /* Under UMS, be paranoid and evict. */
3718 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003719 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003720 if (ret) {
3721 mutex_unlock(&dev->struct_mutex);
3722 return ret;
3723 }
3724 }
3725
Chris Wilson312817a2010-11-22 11:50:11 +00003726 i915_gem_reset_fences(dev);
3727
Chris Wilson29105cc2010-01-07 10:39:13 +00003728 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3729 * We need to replace this with a semaphore, or something.
3730 * And not confound mm.suspended!
3731 */
3732 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003733 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003734
3735 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003736 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003737
Keith Packard6dbe2772008-10-14 21:41:13 -07003738 mutex_unlock(&dev->struct_mutex);
3739
Chris Wilson29105cc2010-01-07 10:39:13 +00003740 /* Cancel the retire work handler, which should be idle now. */
3741 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3742
Eric Anholt673a3942008-07-30 12:06:12 -07003743 return 0;
3744}
3745
Eric Anholt673a3942008-07-30 12:06:12 -07003746int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003747i915_gem_init_ringbuffer(struct drm_device *dev)
3748{
3749 drm_i915_private_t *dev_priv = dev->dev_private;
3750 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003751
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003752 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003753 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003754 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003755
3756 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003757 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003758 if (ret)
3759 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003760 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003761
Chris Wilson549f7362010-10-19 11:19:32 +01003762 if (HAS_BLT(dev)) {
3763 ret = intel_init_blt_ring_buffer(dev);
3764 if (ret)
3765 goto cleanup_bsd_ring;
3766 }
3767
Chris Wilson6f392d5482010-08-07 11:01:22 +01003768 dev_priv->next_seqno = 1;
3769
Chris Wilson68f95ba2010-05-27 13:18:22 +01003770 return 0;
3771
Chris Wilson549f7362010-10-19 11:19:32 +01003772cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003773 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003774cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003775 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003776 return ret;
3777}
3778
3779void
3780i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3781{
3782 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003783 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003784
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003785 for (i = 0; i < I915_NUM_RINGS; i++)
3786 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003787}
3788
3789int
Eric Anholt673a3942008-07-30 12:06:12 -07003790i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3791 struct drm_file *file_priv)
3792{
3793 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003794 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003795
Jesse Barnes79e53942008-11-07 14:24:08 -08003796 if (drm_core_check_feature(dev, DRIVER_MODESET))
3797 return 0;
3798
Ben Gamariba1234d2009-09-14 17:48:47 -04003799 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003800 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003801 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003802 }
3803
Eric Anholt673a3942008-07-30 12:06:12 -07003804 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003805 dev_priv->mm.suspended = 0;
3806
3807 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003808 if (ret != 0) {
3809 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003810 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003811 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003812
Chris Wilson69dc4982010-10-19 10:36:51 +01003813 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003814 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3815 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003816 for (i = 0; i < I915_NUM_RINGS; i++) {
3817 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3818 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3819 }
Eric Anholt673a3942008-07-30 12:06:12 -07003820 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003821
Chris Wilson5f353082010-06-07 14:03:03 +01003822 ret = drm_irq_install(dev);
3823 if (ret)
3824 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003825
Eric Anholt673a3942008-07-30 12:06:12 -07003826 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003827
3828cleanup_ringbuffer:
3829 mutex_lock(&dev->struct_mutex);
3830 i915_gem_cleanup_ringbuffer(dev);
3831 dev_priv->mm.suspended = 1;
3832 mutex_unlock(&dev->struct_mutex);
3833
3834 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003835}
3836
3837int
3838i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3839 struct drm_file *file_priv)
3840{
Jesse Barnes79e53942008-11-07 14:24:08 -08003841 if (drm_core_check_feature(dev, DRIVER_MODESET))
3842 return 0;
3843
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003844 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003845 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003846}
3847
3848void
3849i915_gem_lastclose(struct drm_device *dev)
3850{
3851 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003852
Eric Anholte806b492009-01-22 09:56:58 -08003853 if (drm_core_check_feature(dev, DRIVER_MODESET))
3854 return;
3855
Keith Packard6dbe2772008-10-14 21:41:13 -07003856 ret = i915_gem_idle(dev);
3857 if (ret)
3858 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003859}
3860
Chris Wilson64193402010-10-24 12:38:05 +01003861static void
3862init_ring_lists(struct intel_ring_buffer *ring)
3863{
3864 INIT_LIST_HEAD(&ring->active_list);
3865 INIT_LIST_HEAD(&ring->request_list);
3866 INIT_LIST_HEAD(&ring->gpu_write_list);
3867}
3868
Eric Anholt673a3942008-07-30 12:06:12 -07003869void
3870i915_gem_load(struct drm_device *dev)
3871{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003872 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003873 drm_i915_private_t *dev_priv = dev->dev_private;
3874
Chris Wilson69dc4982010-10-19 10:36:51 +01003875 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003876 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3877 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003878 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003879 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003880 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003881 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003882 for (i = 0; i < I915_NUM_RINGS; i++)
3883 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003884 for (i = 0; i < 16; i++)
3885 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003886 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3887 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003888 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003889
Dave Airlie94400122010-07-20 13:15:31 +10003890 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3891 if (IS_GEN3(dev)) {
3892 u32 tmp = I915_READ(MI_ARB_STATE);
3893 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3894 /* arb state is a masked write, so set bit + bit in mask */
3895 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3896 I915_WRITE(MI_ARB_STATE, tmp);
3897 }
3898 }
3899
Chris Wilson72bfa192010-12-19 11:42:05 +00003900 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3901
Jesse Barnesde151cf2008-11-12 10:03:55 -08003902 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003903 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3904 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003905
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003906 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003907 dev_priv->num_fence_regs = 16;
3908 else
3909 dev_priv->num_fence_regs = 8;
3910
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003911 /* Initialize fence registers to zero */
Eric Anholt10ed13e2011-05-06 13:53:49 -07003912 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3913 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003914 }
Eric Anholt10ed13e2011-05-06 13:53:49 -07003915
Eric Anholt673a3942008-07-30 12:06:12 -07003916 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003917 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01003918
Chris Wilsonce453d82011-02-21 14:43:56 +00003919 dev_priv->mm.interruptible = true;
3920
Chris Wilson17250b72010-10-28 12:51:39 +01003921 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3922 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3923 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07003924}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003925
3926/*
3927 * Create a physically contiguous memory object for this object
3928 * e.g. for cursor + overlay regs
3929 */
Chris Wilson995b6762010-08-20 13:23:26 +01003930static int i915_gem_init_phys_object(struct drm_device *dev,
3931 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003932{
3933 drm_i915_private_t *dev_priv = dev->dev_private;
3934 struct drm_i915_gem_phys_object *phys_obj;
3935 int ret;
3936
3937 if (dev_priv->mm.phys_objs[id - 1] || !size)
3938 return 0;
3939
Eric Anholt9a298b22009-03-24 12:23:04 -07003940 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003941 if (!phys_obj)
3942 return -ENOMEM;
3943
3944 phys_obj->id = id;
3945
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003946 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003947 if (!phys_obj->handle) {
3948 ret = -ENOMEM;
3949 goto kfree_obj;
3950 }
3951#ifdef CONFIG_X86
3952 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3953#endif
3954
3955 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3956
3957 return 0;
3958kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07003959 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003960 return ret;
3961}
3962
Chris Wilson995b6762010-08-20 13:23:26 +01003963static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003964{
3965 drm_i915_private_t *dev_priv = dev->dev_private;
3966 struct drm_i915_gem_phys_object *phys_obj;
3967
3968 if (!dev_priv->mm.phys_objs[id - 1])
3969 return;
3970
3971 phys_obj = dev_priv->mm.phys_objs[id - 1];
3972 if (phys_obj->cur_obj) {
3973 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3974 }
3975
3976#ifdef CONFIG_X86
3977 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3978#endif
3979 drm_pci_free(dev, phys_obj->handle);
3980 kfree(phys_obj);
3981 dev_priv->mm.phys_objs[id - 1] = NULL;
3982}
3983
3984void i915_gem_free_all_phys_object(struct drm_device *dev)
3985{
3986 int i;
3987
Dave Airlie260883c2009-01-22 17:58:49 +10003988 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003989 i915_gem_free_phys_object(dev, i);
3990}
3991
3992void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003993 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003994{
Chris Wilson05394f32010-11-08 19:18:58 +00003995 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01003996 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003997 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003998 int page_count;
3999
Chris Wilson05394f32010-11-08 19:18:58 +00004000 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004001 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004002 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004003
Chris Wilson05394f32010-11-08 19:18:58 +00004004 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004005 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07004006 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004007 if (!IS_ERR(page)) {
4008 char *dst = kmap_atomic(page);
4009 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4010 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004011
Chris Wilsone5281cc2010-10-28 13:45:36 +01004012 drm_clflush_pages(&page, 1);
4013
4014 set_page_dirty(page);
4015 mark_page_accessed(page);
4016 page_cache_release(page);
4017 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004018 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004019 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004020
Chris Wilson05394f32010-11-08 19:18:58 +00004021 obj->phys_obj->cur_obj = NULL;
4022 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004023}
4024
4025int
4026i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004027 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004028 int id,
4029 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004030{
Chris Wilson05394f32010-11-08 19:18:58 +00004031 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004032 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004033 int ret = 0;
4034 int page_count;
4035 int i;
4036
4037 if (id > I915_MAX_PHYS_OBJECT)
4038 return -EINVAL;
4039
Chris Wilson05394f32010-11-08 19:18:58 +00004040 if (obj->phys_obj) {
4041 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004042 return 0;
4043 i915_gem_detach_phys_object(dev, obj);
4044 }
4045
Dave Airlie71acb5e2008-12-30 20:31:46 +10004046 /* create a new object */
4047 if (!dev_priv->mm.phys_objs[id - 1]) {
4048 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004049 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004050 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004051 DRM_ERROR("failed to init phys object %d size: %zu\n",
4052 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004053 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004054 }
4055 }
4056
4057 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004058 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4059 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004060
Chris Wilson05394f32010-11-08 19:18:58 +00004061 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004062
4063 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004064 struct page *page;
4065 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004066
Hugh Dickins5949eac2011-06-27 16:18:18 -07004067 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004068 if (IS_ERR(page))
4069 return PTR_ERR(page);
4070
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004071 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004072 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004073 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004074 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004075
4076 mark_page_accessed(page);
4077 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004078 }
4079
4080 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004081}
4082
4083static int
Chris Wilson05394f32010-11-08 19:18:58 +00004084i915_gem_phys_pwrite(struct drm_device *dev,
4085 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004086 struct drm_i915_gem_pwrite *args,
4087 struct drm_file *file_priv)
4088{
Chris Wilson05394f32010-11-08 19:18:58 +00004089 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004090 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004091
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004092 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4093 unsigned long unwritten;
4094
4095 /* The physical object once assigned is fixed for the lifetime
4096 * of the obj, so we can safely drop the lock and continue
4097 * to access vaddr.
4098 */
4099 mutex_unlock(&dev->struct_mutex);
4100 unwritten = copy_from_user(vaddr, user_data, args->size);
4101 mutex_lock(&dev->struct_mutex);
4102 if (unwritten)
4103 return -EFAULT;
4104 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004105
Daniel Vetter40ce6572010-11-05 18:12:18 +01004106 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004107 return 0;
4108}
Eric Anholtb9624422009-06-03 07:27:35 +00004109
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004110void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004111{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004112 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004113
4114 /* Clean up our request list when the client is going away, so that
4115 * later retire_requests won't dereference our soon-to-be-gone
4116 * file_priv.
4117 */
Chris Wilson1c255952010-09-26 11:03:27 +01004118 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004119 while (!list_empty(&file_priv->mm.request_list)) {
4120 struct drm_i915_gem_request *request;
4121
4122 request = list_first_entry(&file_priv->mm.request_list,
4123 struct drm_i915_gem_request,
4124 client_list);
4125 list_del(&request->client_list);
4126 request->file_priv = NULL;
4127 }
Chris Wilson1c255952010-09-26 11:03:27 +01004128 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004129}
Chris Wilson31169712009-09-14 16:50:28 +01004130
Chris Wilson31169712009-09-14 16:50:28 +01004131static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004132i915_gpu_is_active(struct drm_device *dev)
4133{
4134 drm_i915_private_t *dev_priv = dev->dev_private;
4135 int lists_empty;
4136
Chris Wilson1637ef42010-04-20 17:10:35 +01004137 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004138 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004139
4140 return !lists_empty;
4141}
4142
4143static int
Ying Han1495f232011-05-24 17:12:27 -07004144i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004145{
Chris Wilson17250b72010-10-28 12:51:39 +01004146 struct drm_i915_private *dev_priv =
4147 container_of(shrinker,
4148 struct drm_i915_private,
4149 mm.inactive_shrinker);
4150 struct drm_device *dev = dev_priv->dev;
4151 struct drm_i915_gem_object *obj, *next;
Ying Han1495f232011-05-24 17:12:27 -07004152 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01004153 int cnt;
4154
4155 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004156 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004157
4158 /* "fast-path" to count number of available objects */
4159 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01004160 cnt = 0;
4161 list_for_each_entry(obj,
4162 &dev_priv->mm.inactive_list,
4163 mm_list)
4164 cnt++;
4165 mutex_unlock(&dev->struct_mutex);
4166 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004167 }
4168
Chris Wilson1637ef42010-04-20 17:10:35 +01004169rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004170 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01004171 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01004172
Chris Wilson17250b72010-10-28 12:51:39 +01004173 list_for_each_entry_safe(obj, next,
4174 &dev_priv->mm.inactive_list,
4175 mm_list) {
4176 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00004177 if (i915_gem_object_unbind(obj) == 0 &&
4178 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004179 break;
Chris Wilson31169712009-09-14 16:50:28 +01004180 }
Chris Wilson31169712009-09-14 16:50:28 +01004181 }
4182
4183 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01004184 cnt = 0;
4185 list_for_each_entry_safe(obj, next,
4186 &dev_priv->mm.inactive_list,
4187 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00004188 if (nr_to_scan &&
4189 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004190 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00004191 else
Chris Wilson17250b72010-10-28 12:51:39 +01004192 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01004193 }
4194
Chris Wilson17250b72010-10-28 12:51:39 +01004195 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01004196 /*
4197 * We are desperate for pages, so as a last resort, wait
4198 * for the GPU to finish and discard whatever we can.
4199 * This has a dramatic impact to reduce the number of
4200 * OOM-killer events whilst running the GPU aggressively.
4201 */
Chris Wilson17250b72010-10-28 12:51:39 +01004202 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01004203 goto rescan;
4204 }
Chris Wilson17250b72010-10-28 12:51:39 +01004205 mutex_unlock(&dev->struct_mutex);
4206 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004207}