blob: 52b199da7d5daadc09c5d54bed76ce9b318160f0 [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);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200198 if (size == 0)
199 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700200
201 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000202 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700203 if (obj == NULL)
204 return -ENOMEM;
205
Chris Wilson05394f32010-11-08 19:18:58 +0000206 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100207 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000208 drm_gem_object_release(&obj->base);
209 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100210 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700211 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100212 }
213
Chris Wilson202f2fe2010-10-14 13:20:40 +0100214 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000215 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100216 trace_i915_gem_object_create(obj);
217
Dave Airlieff72145b2011-02-07 12:16:14 +1000218 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700219 return 0;
220}
221
Dave Airlieff72145b2011-02-07 12:16:14 +1000222int
223i915_gem_dumb_create(struct drm_file *file,
224 struct drm_device *dev,
225 struct drm_mode_create_dumb *args)
226{
227 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000228 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000229 args->size = args->pitch * args->height;
230 return i915_gem_create(file, dev,
231 args->size, &args->handle);
232}
233
234int i915_gem_dumb_destroy(struct drm_file *file,
235 struct drm_device *dev,
236 uint32_t handle)
237{
238 return drm_gem_handle_delete(file, handle);
239}
240
241/**
242 * Creates a new mm object and returns a handle to it.
243 */
244int
245i915_gem_create_ioctl(struct drm_device *dev, void *data,
246 struct drm_file *file)
247{
248 struct drm_i915_gem_create *args = data;
249 return i915_gem_create(file, dev,
250 args->size, &args->handle);
251}
252
Chris Wilson05394f32010-11-08 19:18:58 +0000253static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700254{
Chris Wilson05394f32010-11-08 19:18:58 +0000255 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700256
257 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000258 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700259}
260
Chris Wilson99a03df2010-05-27 14:15:34 +0100261static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700262slow_shmem_copy(struct page *dst_page,
263 int dst_offset,
264 struct page *src_page,
265 int src_offset,
266 int length)
267{
268 char *dst_vaddr, *src_vaddr;
269
Chris Wilson99a03df2010-05-27 14:15:34 +0100270 dst_vaddr = kmap(dst_page);
271 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700272
273 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
274
Chris Wilson99a03df2010-05-27 14:15:34 +0100275 kunmap(src_page);
276 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700277}
278
Chris Wilson99a03df2010-05-27 14:15:34 +0100279static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700280slow_shmem_bit17_copy(struct page *gpu_page,
281 int gpu_offset,
282 struct page *cpu_page,
283 int cpu_offset,
284 int length,
285 int is_read)
286{
287 char *gpu_vaddr, *cpu_vaddr;
288
289 /* Use the unswizzled path if this page isn't affected. */
290 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
291 if (is_read)
292 return slow_shmem_copy(cpu_page, cpu_offset,
293 gpu_page, gpu_offset, length);
294 else
295 return slow_shmem_copy(gpu_page, gpu_offset,
296 cpu_page, cpu_offset, length);
297 }
298
Chris Wilson99a03df2010-05-27 14:15:34 +0100299 gpu_vaddr = kmap(gpu_page);
300 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700301
302 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
303 * XORing with the other bits (A9 for Y, A9 and A10 for X)
304 */
305 while (length > 0) {
306 int cacheline_end = ALIGN(gpu_offset + 1, 64);
307 int this_length = min(cacheline_end - gpu_offset, length);
308 int swizzled_gpu_offset = gpu_offset ^ 64;
309
310 if (is_read) {
311 memcpy(cpu_vaddr + cpu_offset,
312 gpu_vaddr + swizzled_gpu_offset,
313 this_length);
314 } else {
315 memcpy(gpu_vaddr + swizzled_gpu_offset,
316 cpu_vaddr + cpu_offset,
317 this_length);
318 }
319 cpu_offset += this_length;
320 gpu_offset += this_length;
321 length -= this_length;
322 }
323
Chris Wilson99a03df2010-05-27 14:15:34 +0100324 kunmap(cpu_page);
325 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700326}
327
Eric Anholt673a3942008-07-30 12:06:12 -0700328/**
Eric Anholteb014592009-03-10 11:44:52 -0700329 * This is the fast shmem pread path, which attempts to copy_from_user directly
330 * from the backing pages of the object to the user's address space. On a
331 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
332 */
333static int
Chris Wilson05394f32010-11-08 19:18:58 +0000334i915_gem_shmem_pread_fast(struct drm_device *dev,
335 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700336 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000337 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700338{
Chris Wilson05394f32010-11-08 19:18:58 +0000339 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700340 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100341 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700342 char __user *user_data;
343 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700344
345 user_data = (char __user *) (uintptr_t) args->data_ptr;
346 remain = args->size;
347
Eric Anholteb014592009-03-10 11:44:52 -0700348 offset = args->offset;
349
350 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100351 struct page *page;
352 char *vaddr;
353 int ret;
354
Eric Anholteb014592009-03-10 11:44:52 -0700355 /* Operation in this page
356 *
Eric Anholteb014592009-03-10 11:44:52 -0700357 * page_offset = offset within page
358 * page_length = bytes to copy for this page
359 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100360 page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700361 page_length = remain;
362 if ((page_offset + remain) > PAGE_SIZE)
363 page_length = PAGE_SIZE - page_offset;
364
Hugh Dickins5949eac2011-06-27 16:18:18 -0700365 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100366 if (IS_ERR(page))
367 return PTR_ERR(page);
368
369 vaddr = kmap_atomic(page);
370 ret = __copy_to_user_inatomic(user_data,
371 vaddr + page_offset,
372 page_length);
373 kunmap_atomic(vaddr);
374
375 mark_page_accessed(page);
376 page_cache_release(page);
377 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100378 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700379
380 remain -= page_length;
381 user_data += page_length;
382 offset += page_length;
383 }
384
Chris Wilson4f27b752010-10-14 15:26:45 +0100385 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700386}
387
388/**
389 * This is the fallback shmem pread path, which allocates temporary storage
390 * in kernel space to copy_to_user into outside of the struct_mutex, so we
391 * can copy out of the object's backing pages while holding the struct mutex
392 * and not take page faults.
393 */
394static int
Chris Wilson05394f32010-11-08 19:18:58 +0000395i915_gem_shmem_pread_slow(struct drm_device *dev,
396 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700397 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000398 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700399{
Chris Wilson05394f32010-11-08 19:18:58 +0000400 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700401 struct mm_struct *mm = current->mm;
402 struct page **user_pages;
403 ssize_t remain;
404 loff_t offset, pinned_pages, i;
405 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100406 int shmem_page_offset;
407 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700408 int page_length;
409 int ret;
410 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700411 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700412
413 remain = args->size;
414
415 /* Pin the user pages containing the data. We can't fault while
416 * holding the struct mutex, yet we want to hold it while
417 * dereferencing the user data.
418 */
419 first_data_page = data_ptr / PAGE_SIZE;
420 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
421 num_pages = last_data_page - first_data_page + 1;
422
Chris Wilson4f27b752010-10-14 15:26:45 +0100423 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700424 if (user_pages == NULL)
425 return -ENOMEM;
426
Chris Wilson4f27b752010-10-14 15:26:45 +0100427 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700428 down_read(&mm->mmap_sem);
429 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700430 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700431 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100432 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700433 if (pinned_pages < num_pages) {
434 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100435 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700436 }
437
Chris Wilson4f27b752010-10-14 15:26:45 +0100438 ret = i915_gem_object_set_cpu_read_domain_range(obj,
439 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700440 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100441 if (ret)
442 goto out;
443
444 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700445
Eric Anholteb014592009-03-10 11:44:52 -0700446 offset = args->offset;
447
448 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100449 struct page *page;
450
Eric Anholteb014592009-03-10 11:44:52 -0700451 /* Operation in this page
452 *
Eric Anholteb014592009-03-10 11:44:52 -0700453 * shmem_page_offset = offset within page in shmem file
454 * data_page_index = page number in get_user_pages return
455 * data_page_offset = offset with data_page_index page.
456 * page_length = bytes to copy for this page
457 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100458 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700459 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100460 data_page_offset = offset_in_page(data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700461
462 page_length = remain;
463 if ((shmem_page_offset + page_length) > PAGE_SIZE)
464 page_length = PAGE_SIZE - shmem_page_offset;
465 if ((data_page_offset + page_length) > PAGE_SIZE)
466 page_length = PAGE_SIZE - data_page_offset;
467
Hugh Dickins5949eac2011-06-27 16:18:18 -0700468 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Jesper Juhlb65552f2011-06-12 20:53:44 +0000469 if (IS_ERR(page)) {
470 ret = PTR_ERR(page);
471 goto out;
472 }
Chris Wilsone5281cc2010-10-28 13:45:36 +0100473
Eric Anholt280b7132009-03-12 16:56:27 -0700474 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100475 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700476 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100477 user_pages[data_page_index],
478 data_page_offset,
479 page_length,
480 1);
481 } else {
482 slow_shmem_copy(user_pages[data_page_index],
483 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100484 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100485 shmem_page_offset,
486 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700487 }
Eric Anholteb014592009-03-10 11:44:52 -0700488
Chris Wilsone5281cc2010-10-28 13:45:36 +0100489 mark_page_accessed(page);
490 page_cache_release(page);
491
Eric Anholteb014592009-03-10 11:44:52 -0700492 remain -= page_length;
493 data_ptr += page_length;
494 offset += page_length;
495 }
496
Chris Wilson4f27b752010-10-14 15:26:45 +0100497out:
Eric Anholteb014592009-03-10 11:44:52 -0700498 for (i = 0; i < pinned_pages; i++) {
499 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100500 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700501 page_cache_release(user_pages[i]);
502 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700503 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700504
505 return ret;
506}
507
Eric Anholt673a3942008-07-30 12:06:12 -0700508/**
509 * Reads data from the object referenced by handle.
510 *
511 * On error, the contents of *data are undefined.
512 */
513int
514i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000515 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700516{
517 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000518 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100519 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700520
Chris Wilson51311d02010-11-17 09:10:42 +0000521 if (args->size == 0)
522 return 0;
523
524 if (!access_ok(VERIFY_WRITE,
525 (char __user *)(uintptr_t)args->data_ptr,
526 args->size))
527 return -EFAULT;
528
529 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
530 args->size);
531 if (ret)
532 return -EFAULT;
533
Chris Wilson4f27b752010-10-14 15:26:45 +0100534 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100535 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100536 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700537
Chris Wilson05394f32010-11-08 19:18:58 +0000538 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000539 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100540 ret = -ENOENT;
541 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100542 }
Eric Anholt673a3942008-07-30 12:06:12 -0700543
Chris Wilson7dcd2492010-09-26 20:21:44 +0100544 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000545 if (args->offset > obj->base.size ||
546 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100547 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100548 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100549 }
550
Chris Wilsondb53a302011-02-03 11:57:46 +0000551 trace_i915_gem_object_pread(obj, args->offset, args->size);
552
Chris Wilson4f27b752010-10-14 15:26:45 +0100553 ret = i915_gem_object_set_cpu_read_domain_range(obj,
554 args->offset,
555 args->size);
556 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100557 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100558
559 ret = -EFAULT;
560 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000561 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100562 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000563 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700564
Chris Wilson35b62a82010-09-26 20:23:38 +0100565out:
Chris Wilson05394f32010-11-08 19:18:58 +0000566 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100567unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100568 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700569 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700570}
571
Keith Packard0839ccb2008-10-30 19:38:48 -0700572/* This is the fast write path which cannot handle
573 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700574 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700575
Keith Packard0839ccb2008-10-30 19:38:48 -0700576static inline int
577fast_user_write(struct io_mapping *mapping,
578 loff_t page_base, int page_offset,
579 char __user *user_data,
580 int length)
581{
582 char *vaddr_atomic;
583 unsigned long unwritten;
584
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700585 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700586 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
587 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700588 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100589 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700590}
591
592/* Here's the write path which can sleep for
593 * page faults
594 */
595
Chris Wilsonab34c222010-05-27 14:15:35 +0100596static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700597slow_kernel_write(struct io_mapping *mapping,
598 loff_t gtt_base, int gtt_offset,
599 struct page *user_page, int user_offset,
600 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700601{
Chris Wilsonab34c222010-05-27 14:15:35 +0100602 char __iomem *dst_vaddr;
603 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700604
Chris Wilsonab34c222010-05-27 14:15:35 +0100605 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
606 src_vaddr = kmap(user_page);
607
608 memcpy_toio(dst_vaddr + gtt_offset,
609 src_vaddr + user_offset,
610 length);
611
612 kunmap(user_page);
613 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700614}
615
Eric Anholt3de09aa2009-03-09 09:42:23 -0700616/**
617 * This is the fast pwrite path, where we copy the data directly from the
618 * user into the GTT, uncached.
619 */
Eric Anholt673a3942008-07-30 12:06:12 -0700620static int
Chris Wilson05394f32010-11-08 19:18:58 +0000621i915_gem_gtt_pwrite_fast(struct drm_device *dev,
622 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700623 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000624 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700625{
Keith Packard0839ccb2008-10-30 19:38:48 -0700626 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700627 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700628 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700629 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700630 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700631
632 user_data = (char __user *) (uintptr_t) args->data_ptr;
633 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700634
Chris Wilson05394f32010-11-08 19:18:58 +0000635 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700636
637 while (remain > 0) {
638 /* Operation in this page
639 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700640 * page_base = page offset within aperture
641 * page_offset = offset within page
642 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700643 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100644 page_base = offset & PAGE_MASK;
645 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700646 page_length = remain;
647 if ((page_offset + remain) > PAGE_SIZE)
648 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700649
Keith Packard0839ccb2008-10-30 19:38:48 -0700650 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700651 * source page isn't available. Return the error and we'll
652 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700653 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100654 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
655 page_offset, user_data, page_length))
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100656 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700657
Keith Packard0839ccb2008-10-30 19:38:48 -0700658 remain -= page_length;
659 user_data += page_length;
660 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700661 }
Eric Anholt673a3942008-07-30 12:06:12 -0700662
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100663 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700664}
665
Eric Anholt3de09aa2009-03-09 09:42:23 -0700666/**
667 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
668 * the memory and maps it using kmap_atomic for copying.
669 *
670 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
671 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
672 */
Eric Anholt3043c602008-10-02 12:24:47 -0700673static int
Chris Wilson05394f32010-11-08 19:18:58 +0000674i915_gem_gtt_pwrite_slow(struct drm_device *dev,
675 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700676 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000677 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700678{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700679 drm_i915_private_t *dev_priv = dev->dev_private;
680 ssize_t remain;
681 loff_t gtt_page_base, offset;
682 loff_t first_data_page, last_data_page, num_pages;
683 loff_t pinned_pages, i;
684 struct page **user_pages;
685 struct mm_struct *mm = current->mm;
686 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700687 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700688 uint64_t data_ptr = args->data_ptr;
689
690 remain = args->size;
691
692 /* Pin the user pages containing the data. We can't fault while
693 * holding the struct mutex, and all of the pwrite implementations
694 * want to hold it while dereferencing the user data.
695 */
696 first_data_page = data_ptr / PAGE_SIZE;
697 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
698 num_pages = last_data_page - first_data_page + 1;
699
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100700 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700701 if (user_pages == NULL)
702 return -ENOMEM;
703
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100704 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700705 down_read(&mm->mmap_sem);
706 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
707 num_pages, 0, 0, user_pages, NULL);
708 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100709 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700710 if (pinned_pages < num_pages) {
711 ret = -EFAULT;
712 goto out_unpin_pages;
713 }
714
Chris Wilsond9e86c02010-11-10 16:40:20 +0000715 ret = i915_gem_object_set_to_gtt_domain(obj, true);
716 if (ret)
717 goto out_unpin_pages;
718
719 ret = i915_gem_object_put_fence(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700720 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100721 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700722
Chris Wilson05394f32010-11-08 19:18:58 +0000723 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700724
725 while (remain > 0) {
726 /* Operation in this page
727 *
728 * gtt_page_base = page offset within aperture
729 * gtt_page_offset = offset within page in aperture
730 * data_page_index = page number in get_user_pages return
731 * data_page_offset = offset with data_page_index page.
732 * page_length = bytes to copy for this page
733 */
734 gtt_page_base = offset & PAGE_MASK;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100735 gtt_page_offset = offset_in_page(offset);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700736 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100737 data_page_offset = offset_in_page(data_ptr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700738
739 page_length = remain;
740 if ((gtt_page_offset + page_length) > PAGE_SIZE)
741 page_length = PAGE_SIZE - gtt_page_offset;
742 if ((data_page_offset + page_length) > PAGE_SIZE)
743 page_length = PAGE_SIZE - data_page_offset;
744
Chris Wilsonab34c222010-05-27 14:15:35 +0100745 slow_kernel_write(dev_priv->mm.gtt_mapping,
746 gtt_page_base, gtt_page_offset,
747 user_pages[data_page_index],
748 data_page_offset,
749 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700750
751 remain -= page_length;
752 offset += page_length;
753 data_ptr += page_length;
754 }
755
Eric Anholt3de09aa2009-03-09 09:42:23 -0700756out_unpin_pages:
757 for (i = 0; i < pinned_pages; i++)
758 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700759 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700760
761 return ret;
762}
763
Eric Anholt40123c12009-03-09 13:42:30 -0700764/**
765 * This is the fast shmem pwrite path, which attempts to directly
766 * copy_from_user into the kmapped pages backing the object.
767 */
Eric Anholt673a3942008-07-30 12:06:12 -0700768static int
Chris Wilson05394f32010-11-08 19:18:58 +0000769i915_gem_shmem_pwrite_fast(struct drm_device *dev,
770 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700771 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000772 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700773{
Chris Wilson05394f32010-11-08 19:18:58 +0000774 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700775 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100776 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700777 char __user *user_data;
778 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700779
780 user_data = (char __user *) (uintptr_t) args->data_ptr;
781 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700782
Eric Anholt673a3942008-07-30 12:06:12 -0700783 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000784 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700785
Eric Anholt40123c12009-03-09 13:42:30 -0700786 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100787 struct page *page;
788 char *vaddr;
789 int ret;
790
Eric Anholt40123c12009-03-09 13:42:30 -0700791 /* Operation in this page
792 *
Eric Anholt40123c12009-03-09 13:42:30 -0700793 * page_offset = offset within page
794 * page_length = bytes to copy for this page
795 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100796 page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700797 page_length = remain;
798 if ((page_offset + remain) > PAGE_SIZE)
799 page_length = PAGE_SIZE - page_offset;
800
Hugh Dickins5949eac2011-06-27 16:18:18 -0700801 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100802 if (IS_ERR(page))
803 return PTR_ERR(page);
804
805 vaddr = kmap_atomic(page, KM_USER0);
806 ret = __copy_from_user_inatomic(vaddr + page_offset,
807 user_data,
808 page_length);
809 kunmap_atomic(vaddr, KM_USER0);
810
811 set_page_dirty(page);
812 mark_page_accessed(page);
813 page_cache_release(page);
814
815 /* If we get a fault while copying data, then (presumably) our
816 * source page isn't available. Return the error and we'll
817 * retry in the slow path.
818 */
819 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100820 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700821
822 remain -= page_length;
823 user_data += page_length;
824 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700825 }
826
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100827 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700828}
829
830/**
831 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
832 * the memory and maps it using kmap_atomic for copying.
833 *
834 * This avoids taking mmap_sem for faulting on the user's address while the
835 * struct_mutex is held.
836 */
837static int
Chris Wilson05394f32010-11-08 19:18:58 +0000838i915_gem_shmem_pwrite_slow(struct drm_device *dev,
839 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700840 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000841 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700842{
Chris Wilson05394f32010-11-08 19:18:58 +0000843 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700844 struct mm_struct *mm = current->mm;
845 struct page **user_pages;
846 ssize_t remain;
847 loff_t offset, pinned_pages, i;
848 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100849 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700850 int data_page_index, data_page_offset;
851 int page_length;
852 int ret;
853 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700854 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700855
856 remain = args->size;
857
858 /* Pin the user pages containing the data. We can't fault while
859 * holding the struct mutex, and all of the pwrite implementations
860 * want to hold it while dereferencing the user data.
861 */
862 first_data_page = data_ptr / PAGE_SIZE;
863 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
864 num_pages = last_data_page - first_data_page + 1;
865
Chris Wilson4f27b752010-10-14 15:26:45 +0100866 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700867 if (user_pages == NULL)
868 return -ENOMEM;
869
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100870 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700871 down_read(&mm->mmap_sem);
872 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
873 num_pages, 0, 0, user_pages, NULL);
874 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100875 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700876 if (pinned_pages < num_pages) {
877 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100878 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700879 }
880
Eric Anholt40123c12009-03-09 13:42:30 -0700881 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100882 if (ret)
883 goto out;
884
885 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700886
Eric Anholt40123c12009-03-09 13:42:30 -0700887 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000888 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700889
890 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100891 struct page *page;
892
Eric Anholt40123c12009-03-09 13:42:30 -0700893 /* Operation in this page
894 *
Eric Anholt40123c12009-03-09 13:42:30 -0700895 * shmem_page_offset = offset within page in shmem file
896 * data_page_index = page number in get_user_pages return
897 * data_page_offset = offset with data_page_index page.
898 * page_length = bytes to copy for this page
899 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100900 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700901 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100902 data_page_offset = offset_in_page(data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700903
904 page_length = remain;
905 if ((shmem_page_offset + page_length) > PAGE_SIZE)
906 page_length = PAGE_SIZE - shmem_page_offset;
907 if ((data_page_offset + page_length) > PAGE_SIZE)
908 page_length = PAGE_SIZE - data_page_offset;
909
Hugh Dickins5949eac2011-06-27 16:18:18 -0700910 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100911 if (IS_ERR(page)) {
912 ret = PTR_ERR(page);
913 goto out;
914 }
915
Eric Anholt280b7132009-03-12 16:56:27 -0700916 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100917 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700918 shmem_page_offset,
919 user_pages[data_page_index],
920 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100921 page_length,
922 0);
923 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100924 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100925 shmem_page_offset,
926 user_pages[data_page_index],
927 data_page_offset,
928 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700929 }
Eric Anholt40123c12009-03-09 13:42:30 -0700930
Chris Wilsone5281cc2010-10-28 13:45:36 +0100931 set_page_dirty(page);
932 mark_page_accessed(page);
933 page_cache_release(page);
934
Eric Anholt40123c12009-03-09 13:42:30 -0700935 remain -= page_length;
936 data_ptr += page_length;
937 offset += page_length;
938 }
939
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100940out:
Eric Anholt40123c12009-03-09 13:42:30 -0700941 for (i = 0; i < pinned_pages; i++)
942 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700943 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700944
945 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700946}
947
948/**
949 * Writes data to the object referenced by handle.
950 *
951 * On error, the contents of the buffer that were to be modified are undefined.
952 */
953int
954i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100955 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700956{
957 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000958 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000959 int ret;
960
961 if (args->size == 0)
962 return 0;
963
964 if (!access_ok(VERIFY_READ,
965 (char __user *)(uintptr_t)args->data_ptr,
966 args->size))
967 return -EFAULT;
968
969 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
970 args->size);
971 if (ret)
972 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700973
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100974 ret = i915_mutex_lock_interruptible(dev);
975 if (ret)
976 return ret;
977
Chris Wilson05394f32010-11-08 19:18:58 +0000978 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000979 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100980 ret = -ENOENT;
981 goto unlock;
982 }
Eric Anholt673a3942008-07-30 12:06:12 -0700983
Chris Wilson7dcd2492010-09-26 20:21:44 +0100984 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000985 if (args->offset > obj->base.size ||
986 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100987 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100988 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100989 }
990
Chris Wilsondb53a302011-02-03 11:57:46 +0000991 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
992
Eric Anholt673a3942008-07-30 12:06:12 -0700993 /* We can only do the GTT pwrite on untiled buffers, as otherwise
994 * it would end up going through the fenced access, and we'll get
995 * different detiling behavior between reading and writing.
996 * pread/pwrite currently are reading and writing from the CPU
997 * perspective, requiring manual detiling by the client.
998 */
Chris Wilson05394f32010-11-08 19:18:58 +0000999 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001000 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001001 else if (obj->gtt_space &&
Chris Wilson05394f32010-11-08 19:18:58 +00001002 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001003 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001004 if (ret)
1005 goto out;
1006
Chris Wilsond9e86c02010-11-10 16:40:20 +00001007 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1008 if (ret)
1009 goto out_unpin;
1010
1011 ret = i915_gem_object_put_fence(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001012 if (ret)
1013 goto out_unpin;
1014
1015 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1016 if (ret == -EFAULT)
1017 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1018
1019out_unpin:
1020 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001021 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001022 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1023 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001024 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001025
1026 ret = -EFAULT;
1027 if (!i915_gem_object_needs_bit17_swizzle(obj))
1028 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1029 if (ret == -EFAULT)
1030 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001031 }
Eric Anholt673a3942008-07-30 12:06:12 -07001032
Chris Wilson35b62a82010-09-26 20:23:38 +01001033out:
Chris Wilson05394f32010-11-08 19:18:58 +00001034 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001035unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001036 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001037 return ret;
1038}
1039
1040/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001041 * Called when user space prepares to use an object with the CPU, either
1042 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001043 */
1044int
1045i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001046 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001047{
1048 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001049 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001050 uint32_t read_domains = args->read_domains;
1051 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001052 int ret;
1053
1054 if (!(dev->driver->driver_features & DRIVER_GEM))
1055 return -ENODEV;
1056
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001057 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001058 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001059 return -EINVAL;
1060
Chris Wilson21d509e2009-06-06 09:46:02 +01001061 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001062 return -EINVAL;
1063
1064 /* Having something in the write domain implies it's in the read
1065 * domain, and only that read domain. Enforce that in the request.
1066 */
1067 if (write_domain != 0 && read_domains != write_domain)
1068 return -EINVAL;
1069
Chris Wilson76c1dec2010-09-25 11:22:51 +01001070 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001071 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001072 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001073
Chris Wilson05394f32010-11-08 19:18:58 +00001074 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001075 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001076 ret = -ENOENT;
1077 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001078 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001079
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001080 if (read_domains & I915_GEM_DOMAIN_GTT) {
1081 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001082
1083 /* Silently promote "you're not bound, there was nothing to do"
1084 * to success, since the client was just asking us to
1085 * make sure everything was done.
1086 */
1087 if (ret == -EINVAL)
1088 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001089 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001090 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001091 }
1092
Chris Wilson05394f32010-11-08 19:18:58 +00001093 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001094unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001095 mutex_unlock(&dev->struct_mutex);
1096 return ret;
1097}
1098
1099/**
1100 * Called when user space has done writes to this buffer
1101 */
1102int
1103i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001104 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001105{
1106 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001107 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001108 int ret = 0;
1109
1110 if (!(dev->driver->driver_features & DRIVER_GEM))
1111 return -ENODEV;
1112
Chris Wilson76c1dec2010-09-25 11:22:51 +01001113 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001114 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001115 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001116
Chris Wilson05394f32010-11-08 19:18:58 +00001117 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001118 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001119 ret = -ENOENT;
1120 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001121 }
1122
Eric Anholt673a3942008-07-30 12:06:12 -07001123 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001124 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001125 i915_gem_object_flush_cpu_write_domain(obj);
1126
Chris Wilson05394f32010-11-08 19:18:58 +00001127 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001128unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001129 mutex_unlock(&dev->struct_mutex);
1130 return ret;
1131}
1132
1133/**
1134 * Maps the contents of an object, returning the address it is mapped
1135 * into.
1136 *
1137 * While the mapping holds a reference on the contents of the object, it doesn't
1138 * imply a ref on the object itself.
1139 */
1140int
1141i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001142 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001143{
Chris Wilsonda761a62010-10-27 17:37:08 +01001144 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001145 struct drm_i915_gem_mmap *args = data;
1146 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001147 unsigned long addr;
1148
1149 if (!(dev->driver->driver_features & DRIVER_GEM))
1150 return -ENODEV;
1151
Chris Wilson05394f32010-11-08 19:18:58 +00001152 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001153 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001154 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001155
Chris Wilsonda761a62010-10-27 17:37:08 +01001156 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1157 drm_gem_object_unreference_unlocked(obj);
1158 return -E2BIG;
1159 }
1160
Eric Anholt673a3942008-07-30 12:06:12 -07001161 down_write(&current->mm->mmap_sem);
1162 addr = do_mmap(obj->filp, 0, args->size,
1163 PROT_READ | PROT_WRITE, MAP_SHARED,
1164 args->offset);
1165 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001166 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001167 if (IS_ERR((void *)addr))
1168 return addr;
1169
1170 args->addr_ptr = (uint64_t) addr;
1171
1172 return 0;
1173}
1174
Jesse Barnesde151cf2008-11-12 10:03:55 -08001175/**
1176 * i915_gem_fault - fault a page into the GTT
1177 * vma: VMA in question
1178 * vmf: fault info
1179 *
1180 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1181 * from userspace. The fault handler takes care of binding the object to
1182 * the GTT (if needed), allocating and programming a fence register (again,
1183 * only if needed based on whether the old reg is still valid or the object
1184 * is tiled) and inserting a new PTE into the faulting process.
1185 *
1186 * Note that the faulting process may involve evicting existing objects
1187 * from the GTT and/or fence registers to make room. So performance may
1188 * suffer if the GTT working set is large or there are few fence registers
1189 * left.
1190 */
1191int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1192{
Chris Wilson05394f32010-11-08 19:18:58 +00001193 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1194 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001195 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001196 pgoff_t page_offset;
1197 unsigned long pfn;
1198 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001199 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001200
1201 /* We don't use vmf->pgoff since that has the fake offset */
1202 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1203 PAGE_SHIFT;
1204
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001205 ret = i915_mutex_lock_interruptible(dev);
1206 if (ret)
1207 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001208
Chris Wilsondb53a302011-02-03 11:57:46 +00001209 trace_i915_gem_object_fault(obj, page_offset, true, write);
1210
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001211 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001212 if (!obj->map_and_fenceable) {
1213 ret = i915_gem_object_unbind(obj);
1214 if (ret)
1215 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001216 }
Chris Wilson05394f32010-11-08 19:18:58 +00001217 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001218 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001219 if (ret)
1220 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001221
Eric Anholte92d03b2011-06-14 16:43:09 -07001222 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1223 if (ret)
1224 goto unlock;
1225 }
Chris Wilson4a684a42010-10-28 14:44:08 +01001226
Chris Wilsond9e86c02010-11-10 16:40:20 +00001227 if (obj->tiling_mode == I915_TILING_NONE)
1228 ret = i915_gem_object_put_fence(obj);
1229 else
Chris Wilsonce453d82011-02-21 14:43:56 +00001230 ret = i915_gem_object_get_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001231 if (ret)
1232 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001233
Chris Wilson05394f32010-11-08 19:18:58 +00001234 if (i915_gem_object_is_inactive(obj))
1235 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001236
Chris Wilson6299f992010-11-24 12:23:44 +00001237 obj->fault_mappable = true;
1238
Chris Wilson05394f32010-11-08 19:18:58 +00001239 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001240 page_offset;
1241
1242 /* Finally, remap it using the new GTT offset */
1243 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001244unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001245 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001246out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001247 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001248 case -EIO:
Chris Wilson045e7692010-11-07 09:18:22 +00001249 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001250 /* Give the error handler a chance to run and move the
1251 * objects off the GPU active list. Next time we service the
1252 * fault, we should be able to transition the page into the
1253 * GTT without touching the GPU (and so avoid further
1254 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1255 * with coherency, just lost writes.
1256 */
Chris Wilson045e7692010-11-07 09:18:22 +00001257 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001258 case 0:
1259 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001260 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001261 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001262 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001264 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001265 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001266 }
1267}
1268
1269/**
Chris Wilson901782b2009-07-10 08:18:50 +01001270 * i915_gem_release_mmap - remove physical page mappings
1271 * @obj: obj in question
1272 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001273 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001274 * relinquish ownership of the pages back to the system.
1275 *
1276 * It is vital that we remove the page mapping if we have mapped a tiled
1277 * object through the GTT and then lose the fence register due to
1278 * resource pressure. Similarly if the object has been moved out of the
1279 * aperture, than pages mapped into userspace must be revoked. Removing the
1280 * mapping will then trigger a page fault on the next user access, allowing
1281 * fixup by i915_gem_fault().
1282 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001283void
Chris Wilson05394f32010-11-08 19:18:58 +00001284i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001285{
Chris Wilson6299f992010-11-24 12:23:44 +00001286 if (!obj->fault_mappable)
1287 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001288
Chris Wilsonf6e47882011-03-20 21:09:12 +00001289 if (obj->base.dev->dev_mapping)
1290 unmap_mapping_range(obj->base.dev->dev_mapping,
1291 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1292 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001293
Chris Wilson6299f992010-11-24 12:23:44 +00001294 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001295}
1296
Chris Wilson92b88ae2010-11-09 11:47:32 +00001297static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001298i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001299{
Chris Wilsone28f8712011-07-18 13:11:49 -07001300 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001301
1302 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001303 tiling_mode == I915_TILING_NONE)
1304 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001305
1306 /* Previous chips need a power-of-two fence region when tiling */
1307 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001308 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001309 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001310 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001311
Chris Wilsone28f8712011-07-18 13:11:49 -07001312 while (gtt_size < size)
1313 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001314
Chris Wilsone28f8712011-07-18 13:11:49 -07001315 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001316}
1317
Jesse Barnesde151cf2008-11-12 10:03:55 -08001318/**
1319 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1320 * @obj: object to check
1321 *
1322 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001323 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001324 */
1325static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001326i915_gem_get_gtt_alignment(struct drm_device *dev,
1327 uint32_t size,
1328 int tiling_mode)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001329{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001330 /*
1331 * Minimum alignment is 4k (GTT page size), but might be greater
1332 * if a fence register is needed for the object.
1333 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001334 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001335 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001336 return 4096;
1337
1338 /*
1339 * Previous chips need to be aligned to the size of the smallest
1340 * fence register that can contain the object.
1341 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001342 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001343}
1344
Daniel Vetter5e783302010-11-14 22:32:36 +01001345/**
1346 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1347 * unfenced object
Chris Wilsone28f8712011-07-18 13:11:49 -07001348 * @dev: the device
1349 * @size: size of the object
1350 * @tiling_mode: tiling mode of the object
Daniel Vetter5e783302010-11-14 22:32:36 +01001351 *
1352 * Return the required GTT alignment for an object, only taking into account
1353 * unfenced tiled surface requirements.
1354 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001355uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001356i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1357 uint32_t size,
1358 int tiling_mode)
Daniel Vetter5e783302010-11-14 22:32:36 +01001359{
Daniel Vetter5e783302010-11-14 22:32:36 +01001360 /*
1361 * Minimum alignment is 4k (GTT page size) for sane hw.
1362 */
1363 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001364 tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001365 return 4096;
1366
Chris Wilsone28f8712011-07-18 13:11:49 -07001367 /* Previous hardware however needs to be aligned to a power-of-two
1368 * tile height. The simplest method for determining this is to reuse
1369 * the power-of-tile object size.
Daniel Vetter5e783302010-11-14 22:32:36 +01001370 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001371 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Daniel Vetter5e783302010-11-14 22:32:36 +01001372}
1373
Jesse Barnesde151cf2008-11-12 10:03:55 -08001374int
Dave Airlieff72145b2011-02-07 12:16:14 +10001375i915_gem_mmap_gtt(struct drm_file *file,
1376 struct drm_device *dev,
1377 uint32_t handle,
1378 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001379{
Chris Wilsonda761a62010-10-27 17:37:08 +01001380 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001381 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001382 int ret;
1383
1384 if (!(dev->driver->driver_features & DRIVER_GEM))
1385 return -ENODEV;
1386
Chris Wilson76c1dec2010-09-25 11:22:51 +01001387 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001388 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001389 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001390
Dave Airlieff72145b2011-02-07 12:16:14 +10001391 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001392 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001393 ret = -ENOENT;
1394 goto unlock;
1395 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001396
Chris Wilson05394f32010-11-08 19:18:58 +00001397 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001398 ret = -E2BIG;
1399 goto unlock;
1400 }
1401
Chris Wilson05394f32010-11-08 19:18:58 +00001402 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001403 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001404 ret = -EINVAL;
1405 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001406 }
1407
Chris Wilson05394f32010-11-08 19:18:58 +00001408 if (!obj->base.map_list.map) {
Rob Clarkb464e9a2011-08-10 08:09:08 -05001409 ret = drm_gem_create_mmap_offset(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001410 if (ret)
1411 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001412 }
1413
Dave Airlieff72145b2011-02-07 12:16:14 +10001414 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001415
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001416out:
Chris Wilson05394f32010-11-08 19:18:58 +00001417 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001418unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001419 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001420 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001421}
1422
Dave Airlieff72145b2011-02-07 12:16:14 +10001423/**
1424 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1425 * @dev: DRM device
1426 * @data: GTT mapping ioctl data
1427 * @file: GEM object info
1428 *
1429 * Simply returns the fake offset to userspace so it can mmap it.
1430 * The mmap call will end up in drm_gem_mmap(), which will set things
1431 * up so we can get faults in the handler above.
1432 *
1433 * The fault handler will take care of binding the object into the GTT
1434 * (since it may have been evicted to make room for something), allocating
1435 * a fence register, and mapping the appropriate aperture address into
1436 * userspace.
1437 */
1438int
1439i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1440 struct drm_file *file)
1441{
1442 struct drm_i915_gem_mmap_gtt *args = data;
1443
1444 if (!(dev->driver->driver_features & DRIVER_GEM))
1445 return -ENODEV;
1446
1447 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1448}
1449
1450
Chris Wilsone5281cc2010-10-28 13:45:36 +01001451static int
Chris Wilson05394f32010-11-08 19:18:58 +00001452i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001453 gfp_t gfpmask)
1454{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001455 int page_count, i;
1456 struct address_space *mapping;
1457 struct inode *inode;
1458 struct page *page;
1459
1460 /* Get the list of pages out of our struct file. They'll be pinned
1461 * at this point until we release them.
1462 */
Chris Wilson05394f32010-11-08 19:18:58 +00001463 page_count = obj->base.size / PAGE_SIZE;
1464 BUG_ON(obj->pages != NULL);
1465 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1466 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001467 return -ENOMEM;
1468
Chris Wilson05394f32010-11-08 19:18:58 +00001469 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001470 mapping = inode->i_mapping;
Hugh Dickins5949eac2011-06-27 16:18:18 -07001471 gfpmask |= mapping_gfp_mask(mapping);
1472
Chris Wilsone5281cc2010-10-28 13:45:36 +01001473 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07001474 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001475 if (IS_ERR(page))
1476 goto err_pages;
1477
Chris Wilson05394f32010-11-08 19:18:58 +00001478 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001479 }
1480
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001481 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilsone5281cc2010-10-28 13:45:36 +01001482 i915_gem_object_do_bit_17_swizzle(obj);
1483
1484 return 0;
1485
1486err_pages:
1487 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001488 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001489
Chris Wilson05394f32010-11-08 19:18:58 +00001490 drm_free_large(obj->pages);
1491 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001492 return PTR_ERR(page);
1493}
1494
Chris Wilson5cdf5882010-09-27 15:51:07 +01001495static void
Chris Wilson05394f32010-11-08 19:18:58 +00001496i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001497{
Chris Wilson05394f32010-11-08 19:18:58 +00001498 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001499 int i;
1500
Chris Wilson05394f32010-11-08 19:18:58 +00001501 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001502
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001503 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07001504 i915_gem_object_save_bit_17_swizzle(obj);
1505
Chris Wilson05394f32010-11-08 19:18:58 +00001506 if (obj->madv == I915_MADV_DONTNEED)
1507 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001508
1509 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001510 if (obj->dirty)
1511 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001512
Chris Wilson05394f32010-11-08 19:18:58 +00001513 if (obj->madv == I915_MADV_WILLNEED)
1514 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001515
Chris Wilson05394f32010-11-08 19:18:58 +00001516 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001517 }
Chris Wilson05394f32010-11-08 19:18:58 +00001518 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001519
Chris Wilson05394f32010-11-08 19:18:58 +00001520 drm_free_large(obj->pages);
1521 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001522}
1523
Chris Wilson54cf91d2010-11-25 18:00:26 +00001524void
Chris Wilson05394f32010-11-08 19:18:58 +00001525i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001526 struct intel_ring_buffer *ring,
1527 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001528{
Chris Wilson05394f32010-11-08 19:18:58 +00001529 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001530 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001531
Zou Nan hai852835f2010-05-21 09:08:56 +08001532 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001533 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001534
1535 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001536 if (!obj->active) {
1537 drm_gem_object_reference(&obj->base);
1538 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001539 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001540
Eric Anholt673a3942008-07-30 12:06:12 -07001541 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001542 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1543 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001544
Chris Wilson05394f32010-11-08 19:18:58 +00001545 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001546 if (obj->fenced_gpu_access) {
1547 struct drm_i915_fence_reg *reg;
1548
1549 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1550
1551 obj->last_fenced_seqno = seqno;
1552 obj->last_fenced_ring = ring;
1553
1554 reg = &dev_priv->fence_regs[obj->fence_reg];
1555 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1556 }
1557}
1558
1559static void
1560i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1561{
1562 list_del_init(&obj->ring_list);
1563 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001564}
1565
Eric Anholtce44b0e2008-11-06 16:00:31 -08001566static void
Chris Wilson05394f32010-11-08 19:18:58 +00001567i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001568{
Chris Wilson05394f32010-11-08 19:18:58 +00001569 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001570 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001571
Chris Wilson05394f32010-11-08 19:18:58 +00001572 BUG_ON(!obj->active);
1573 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001574
1575 i915_gem_object_move_off_active(obj);
1576}
1577
1578static void
1579i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1580{
1581 struct drm_device *dev = obj->base.dev;
1582 struct drm_i915_private *dev_priv = dev->dev_private;
1583
1584 if (obj->pin_count != 0)
1585 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1586 else
1587 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1588
1589 BUG_ON(!list_empty(&obj->gpu_write_list));
1590 BUG_ON(!obj->active);
1591 obj->ring = NULL;
1592
1593 i915_gem_object_move_off_active(obj);
1594 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001595
1596 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001597 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001598 drm_gem_object_unreference(&obj->base);
1599
1600 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001601}
Eric Anholt673a3942008-07-30 12:06:12 -07001602
Chris Wilson963b4832009-09-20 23:03:54 +01001603/* Immediately discard the backing storage */
1604static void
Chris Wilson05394f32010-11-08 19:18:58 +00001605i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001606{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001607 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001608
Chris Wilsonae9fed62010-08-07 11:01:30 +01001609 /* Our goal here is to return as much of the memory as
1610 * is possible back to the system as we are called from OOM.
1611 * To do this we must instruct the shmfs to drop all of its
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001612 * backing pages, *now*.
Chris Wilsonae9fed62010-08-07 11:01:30 +01001613 */
Chris Wilson05394f32010-11-08 19:18:58 +00001614 inode = obj->base.filp->f_path.dentry->d_inode;
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001615 shmem_truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001616
Chris Wilson05394f32010-11-08 19:18:58 +00001617 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001618}
1619
1620static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001621i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001622{
Chris Wilson05394f32010-11-08 19:18:58 +00001623 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001624}
1625
Eric Anholt673a3942008-07-30 12:06:12 -07001626static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001627i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1628 uint32_t flush_domains)
Daniel Vetter63560392010-02-19 11:51:59 +01001629{
Chris Wilson05394f32010-11-08 19:18:58 +00001630 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001631
Chris Wilson05394f32010-11-08 19:18:58 +00001632 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001633 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001634 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001635 if (obj->base.write_domain & flush_domains) {
1636 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001637
Chris Wilson05394f32010-11-08 19:18:58 +00001638 obj->base.write_domain = 0;
1639 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001640 i915_gem_object_move_to_active(obj, ring,
Chris Wilsondb53a302011-02-03 11:57:46 +00001641 i915_gem_next_request_seqno(ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001642
Daniel Vetter63560392010-02-19 11:51:59 +01001643 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001644 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001645 old_write_domain);
1646 }
1647 }
1648}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001649
Chris Wilson3cce4692010-10-27 16:11:02 +01001650int
Chris Wilsondb53a302011-02-03 11:57:46 +00001651i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001652 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001653 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001654{
Chris Wilsondb53a302011-02-03 11:57:46 +00001655 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001656 uint32_t seqno;
1657 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001658 int ret;
1659
1660 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001661
Chris Wilson3cce4692010-10-27 16:11:02 +01001662 ret = ring->add_request(ring, &seqno);
1663 if (ret)
1664 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001665
Chris Wilsondb53a302011-02-03 11:57:46 +00001666 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001667
1668 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001669 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001670 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001671 was_empty = list_empty(&ring->request_list);
1672 list_add_tail(&request->list, &ring->request_list);
1673
Chris Wilsondb53a302011-02-03 11:57:46 +00001674 if (file) {
1675 struct drm_i915_file_private *file_priv = file->driver_priv;
1676
Chris Wilson1c255952010-09-26 11:03:27 +01001677 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001678 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001679 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001680 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001681 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001682 }
Eric Anholt673a3942008-07-30 12:06:12 -07001683
Chris Wilsondb53a302011-02-03 11:57:46 +00001684 ring->outstanding_lazy_request = false;
1685
Ben Gamarif65d9422009-09-14 17:48:44 -04001686 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001687 if (i915_enable_hangcheck) {
1688 mod_timer(&dev_priv->hangcheck_timer,
1689 jiffies +
1690 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1691 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001692 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001693 queue_delayed_work(dev_priv->wq,
1694 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001695 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001696 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001697}
1698
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001699static inline void
1700i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001701{
Chris Wilson1c255952010-09-26 11:03:27 +01001702 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001703
Chris Wilson1c255952010-09-26 11:03:27 +01001704 if (!file_priv)
1705 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001706
Chris Wilson1c255952010-09-26 11:03:27 +01001707 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001708 if (request->file_priv) {
1709 list_del(&request->client_list);
1710 request->file_priv = NULL;
1711 }
Chris Wilson1c255952010-09-26 11:03:27 +01001712 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001713}
1714
Chris Wilsondfaae392010-09-22 10:31:52 +01001715static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1716 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001717{
Chris Wilsondfaae392010-09-22 10:31:52 +01001718 while (!list_empty(&ring->request_list)) {
1719 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001720
Chris Wilsondfaae392010-09-22 10:31:52 +01001721 request = list_first_entry(&ring->request_list,
1722 struct drm_i915_gem_request,
1723 list);
1724
1725 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001726 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001727 kfree(request);
1728 }
1729
1730 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001731 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001732
Chris Wilson05394f32010-11-08 19:18:58 +00001733 obj = list_first_entry(&ring->active_list,
1734 struct drm_i915_gem_object,
1735 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001736
Chris Wilson05394f32010-11-08 19:18:58 +00001737 obj->base.write_domain = 0;
1738 list_del_init(&obj->gpu_write_list);
1739 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001740 }
Eric Anholt673a3942008-07-30 12:06:12 -07001741}
1742
Chris Wilson312817a2010-11-22 11:50:11 +00001743static void i915_gem_reset_fences(struct drm_device *dev)
1744{
1745 struct drm_i915_private *dev_priv = dev->dev_private;
1746 int i;
1747
1748 for (i = 0; i < 16; i++) {
1749 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001750 struct drm_i915_gem_object *obj = reg->obj;
1751
1752 if (!obj)
1753 continue;
1754
1755 if (obj->tiling_mode)
1756 i915_gem_release_mmap(obj);
1757
Chris Wilsond9e86c02010-11-10 16:40:20 +00001758 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1759 reg->obj->fenced_gpu_access = false;
1760 reg->obj->last_fenced_seqno = 0;
1761 reg->obj->last_fenced_ring = NULL;
1762 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001763 }
1764}
1765
Chris Wilson069efc12010-09-30 16:53:18 +01001766void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001767{
Chris Wilsondfaae392010-09-22 10:31:52 +01001768 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001769 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001770 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001771
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001772 for (i = 0; i < I915_NUM_RINGS; i++)
1773 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001774
1775 /* Remove anything from the flushing lists. The GPU cache is likely
1776 * to be lost on reset along with the data, so simply move the
1777 * lost bo to the inactive list.
1778 */
1779 while (!list_empty(&dev_priv->mm.flushing_list)) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001780 obj = list_first_entry(&dev_priv->mm.flushing_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001781 struct drm_i915_gem_object,
1782 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001783
Chris Wilson05394f32010-11-08 19:18:58 +00001784 obj->base.write_domain = 0;
1785 list_del_init(&obj->gpu_write_list);
1786 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001787 }
Chris Wilson9375e442010-09-19 12:21:28 +01001788
Chris Wilsondfaae392010-09-22 10:31:52 +01001789 /* Move everything out of the GPU domains to ensure we do any
1790 * necessary invalidation upon reuse.
1791 */
Chris Wilson05394f32010-11-08 19:18:58 +00001792 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001793 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001794 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001795 {
Chris Wilson05394f32010-11-08 19:18:58 +00001796 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001797 }
Chris Wilson069efc12010-09-30 16:53:18 +01001798
1799 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001800 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001801}
1802
1803/**
1804 * This function clears the request list as sequence numbers are passed.
1805 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001806static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001807i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001808{
Eric Anholt673a3942008-07-30 12:06:12 -07001809 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001810 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001811
Chris Wilsondb53a302011-02-03 11:57:46 +00001812 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001813 return;
1814
Chris Wilsondb53a302011-02-03 11:57:46 +00001815 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001816
Chris Wilson78501ea2010-10-27 12:18:21 +01001817 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001818
Chris Wilson076e2c02011-01-21 10:07:18 +00001819 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001820 if (seqno >= ring->sync_seqno[i])
1821 ring->sync_seqno[i] = 0;
1822
Zou Nan hai852835f2010-05-21 09:08:56 +08001823 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001824 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001825
Zou Nan hai852835f2010-05-21 09:08:56 +08001826 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001827 struct drm_i915_gem_request,
1828 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001829
Chris Wilsondfaae392010-09-22 10:31:52 +01001830 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001831 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001832
Chris Wilsondb53a302011-02-03 11:57:46 +00001833 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001834
1835 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001836 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001837 kfree(request);
1838 }
1839
1840 /* Move any buffers on the active list that are no longer referenced
1841 * by the ringbuffer to the flushing/inactive lists as appropriate.
1842 */
1843 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001844 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001845
Akshay Joshi0206e352011-08-16 15:34:10 -04001846 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001847 struct drm_i915_gem_object,
1848 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001849
Chris Wilson05394f32010-11-08 19:18:58 +00001850 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001851 break;
1852
Chris Wilson05394f32010-11-08 19:18:58 +00001853 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001854 i915_gem_object_move_to_flushing(obj);
1855 else
1856 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001857 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001858
Chris Wilsondb53a302011-02-03 11:57:46 +00001859 if (unlikely(ring->trace_irq_seqno &&
1860 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001861 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001862 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001863 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001864
Chris Wilsondb53a302011-02-03 11:57:46 +00001865 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001866}
1867
1868void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001869i915_gem_retire_requests(struct drm_device *dev)
1870{
1871 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001872 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001873
Chris Wilsonbe726152010-07-23 23:18:50 +01001874 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001875 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001876
1877 /* We must be careful that during unbind() we do not
1878 * accidentally infinitely recurse into retire requests.
1879 * Currently:
1880 * retire -> free -> unbind -> wait -> retire_ring
1881 */
Chris Wilson05394f32010-11-08 19:18:58 +00001882 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001883 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001884 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001885 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001886 }
1887
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001888 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilsondb53a302011-02-03 11:57:46 +00001889 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001890}
1891
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001892static void
Eric Anholt673a3942008-07-30 12:06:12 -07001893i915_gem_retire_work_handler(struct work_struct *work)
1894{
1895 drm_i915_private_t *dev_priv;
1896 struct drm_device *dev;
Chris Wilson0a587052011-01-09 21:05:44 +00001897 bool idle;
1898 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001899
1900 dev_priv = container_of(work, drm_i915_private_t,
1901 mm.retire_work.work);
1902 dev = dev_priv->dev;
1903
Chris Wilson891b48c2010-09-29 12:26:37 +01001904 /* Come back later if the device is busy... */
1905 if (!mutex_trylock(&dev->struct_mutex)) {
1906 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1907 return;
1908 }
1909
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001910 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001911
Chris Wilson0a587052011-01-09 21:05:44 +00001912 /* Send a periodic flush down the ring so we don't hold onto GEM
1913 * objects indefinitely.
1914 */
1915 idle = true;
1916 for (i = 0; i < I915_NUM_RINGS; i++) {
1917 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1918
1919 if (!list_empty(&ring->gpu_write_list)) {
1920 struct drm_i915_gem_request *request;
1921 int ret;
1922
Chris Wilsondb53a302011-02-03 11:57:46 +00001923 ret = i915_gem_flush_ring(ring,
1924 0, I915_GEM_GPU_DOMAINS);
Chris Wilson0a587052011-01-09 21:05:44 +00001925 request = kzalloc(sizeof(*request), GFP_KERNEL);
1926 if (ret || request == NULL ||
Chris Wilsondb53a302011-02-03 11:57:46 +00001927 i915_add_request(ring, NULL, request))
Chris Wilson0a587052011-01-09 21:05:44 +00001928 kfree(request);
1929 }
1930
1931 idle &= list_empty(&ring->request_list);
1932 }
1933
1934 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001935 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilson0a587052011-01-09 21:05:44 +00001936
Eric Anholt673a3942008-07-30 12:06:12 -07001937 mutex_unlock(&dev->struct_mutex);
1938}
1939
Chris Wilsondb53a302011-02-03 11:57:46 +00001940/**
1941 * Waits for a sequence number to be signaled, and cleans up the
1942 * request and object lists appropriately for that event.
1943 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001944int
Chris Wilsondb53a302011-02-03 11:57:46 +00001945i915_wait_request(struct intel_ring_buffer *ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00001946 uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001947{
Chris Wilsondb53a302011-02-03 11:57:46 +00001948 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001949 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001950 int ret = 0;
1951
1952 BUG_ON(seqno == 0);
1953
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001954 if (atomic_read(&dev_priv->mm.wedged)) {
1955 struct completion *x = &dev_priv->error_completion;
1956 bool recovery_complete;
1957 unsigned long flags;
1958
1959 /* Give the error handler a chance to run. */
1960 spin_lock_irqsave(&x->wait.lock, flags);
1961 recovery_complete = x->done > 0;
1962 spin_unlock_irqrestore(&x->wait.lock, flags);
1963
1964 return recovery_complete ? -EIO : -EAGAIN;
1965 }
Ben Gamariffed1d02009-09-14 17:48:41 -04001966
Chris Wilson5d97eb62010-11-10 20:40:02 +00001967 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01001968 struct drm_i915_gem_request *request;
1969
1970 request = kzalloc(sizeof(*request), GFP_KERNEL);
1971 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001972 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01001973
Chris Wilsondb53a302011-02-03 11:57:46 +00001974 ret = i915_add_request(ring, NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +01001975 if (ret) {
1976 kfree(request);
1977 return ret;
1978 }
1979
1980 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01001981 }
1982
Chris Wilson78501ea2010-10-27 12:18:21 +01001983 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001984 if (HAS_PCH_SPLIT(ring->dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001985 ier = I915_READ(DEIER) | I915_READ(GTIER);
1986 else
1987 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001988 if (!ier) {
1989 DRM_ERROR("something (likely vbetool) disabled "
1990 "interrupts, re-enabling\n");
Chris Wilsonf01c22f2011-06-28 11:48:51 +01001991 ring->dev->driver->irq_preinstall(ring->dev);
1992 ring->dev->driver->irq_postinstall(ring->dev);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001993 }
1994
Chris Wilsondb53a302011-02-03 11:57:46 +00001995 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001996
Chris Wilsonb2223492010-10-27 15:27:33 +01001997 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001998 if (ring->irq_get(ring)) {
Chris Wilsonce453d82011-02-21 14:43:56 +00001999 if (dev_priv->mm.interruptible)
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002000 ret = wait_event_interruptible(ring->irq_queue,
2001 i915_seqno_passed(ring->get_seqno(ring), seqno)
2002 || atomic_read(&dev_priv->mm.wedged));
2003 else
2004 wait_event(ring->irq_queue,
2005 i915_seqno_passed(ring->get_seqno(ring), seqno)
2006 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002007
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002008 ring->irq_put(ring);
Chris Wilsonb5ba1772010-12-14 12:17:15 +00002009 } else if (wait_for(i915_seqno_passed(ring->get_seqno(ring),
2010 seqno) ||
2011 atomic_read(&dev_priv->mm.wedged), 3000))
2012 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01002013 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002014
Chris Wilsondb53a302011-02-03 11:57:46 +00002015 trace_i915_gem_request_wait_end(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002016 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002017 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002018 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002019
2020 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002021 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002022 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002023 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002024
2025 /* Directly dispatch request retiring. While we have the work queue
2026 * to handle this, the waiter on a request often wants an associated
2027 * buffer to have made it to the inactive list, and we would need
2028 * a separate wait queue to handle that.
2029 */
2030 if (ret == 0)
Chris Wilsondb53a302011-02-03 11:57:46 +00002031 i915_gem_retire_requests_ring(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002032
2033 return ret;
2034}
2035
Daniel Vetter48764bf2009-09-15 22:57:32 +02002036/**
Eric Anholt673a3942008-07-30 12:06:12 -07002037 * Ensures that all rendering to the object has completed and the object is
2038 * safe to unbind from the GTT or access from the CPU.
2039 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00002040int
Chris Wilsonce453d82011-02-21 14:43:56 +00002041i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002042{
Eric Anholt673a3942008-07-30 12:06:12 -07002043 int ret;
2044
Eric Anholte47c68e2008-11-14 13:35:19 -08002045 /* This function only exists to support waiting for existing rendering,
2046 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002047 */
Chris Wilson05394f32010-11-08 19:18:58 +00002048 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002049
2050 /* If there is rendering queued on the buffer being evicted, wait for
2051 * it.
2052 */
Chris Wilson05394f32010-11-08 19:18:58 +00002053 if (obj->active) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002054 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002055 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002056 return ret;
2057 }
2058
2059 return 0;
2060}
2061
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002062static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2063{
2064 u32 old_write_domain, old_read_domains;
2065
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002066 /* Act a barrier for all accesses through the GTT */
2067 mb();
2068
2069 /* Force a pagefault for domain tracking on next user access */
2070 i915_gem_release_mmap(obj);
2071
Keith Packardb97c3d92011-06-24 21:02:59 -07002072 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2073 return;
2074
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002075 old_read_domains = obj->base.read_domains;
2076 old_write_domain = obj->base.write_domain;
2077
2078 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2079 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2080
2081 trace_i915_gem_object_change_domain(obj,
2082 old_read_domains,
2083 old_write_domain);
2084}
2085
Eric Anholt673a3942008-07-30 12:06:12 -07002086/**
2087 * Unbinds an object from the GTT aperture.
2088 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002089int
Chris Wilson05394f32010-11-08 19:18:58 +00002090i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002091{
Eric Anholt673a3942008-07-30 12:06:12 -07002092 int ret = 0;
2093
Chris Wilson05394f32010-11-08 19:18:58 +00002094 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002095 return 0;
2096
Chris Wilson05394f32010-11-08 19:18:58 +00002097 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002098 DRM_ERROR("Attempting to unbind pinned buffer\n");
2099 return -EINVAL;
2100 }
2101
Chris Wilsona8198ee2011-04-13 22:04:09 +01002102 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson8dc17752010-07-23 23:18:51 +01002103 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002104 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002105 /* Continue on if we fail due to EIO, the GPU is hung so we
2106 * should be safe and we need to cleanup or else we might
2107 * cause memory corruption through use-after-free.
2108 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002109
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002110 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002111
2112 /* Move the object to the CPU domain to ensure that
2113 * any possible CPU writes while it's not in the GTT
2114 * are flushed when we go to remap it.
2115 */
2116 if (ret == 0)
2117 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2118 if (ret == -ERESTARTSYS)
2119 return ret;
Chris Wilson812ed4922010-09-30 15:08:57 +01002120 if (ret) {
Chris Wilsona8198ee2011-04-13 22:04:09 +01002121 /* In the event of a disaster, abandon all caches and
2122 * hope for the best.
2123 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002124 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002125 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002126 }
Eric Anholt673a3942008-07-30 12:06:12 -07002127
Daniel Vetter96b47b62009-12-15 17:50:00 +01002128 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002129 ret = i915_gem_object_put_fence(obj);
2130 if (ret == -ERESTARTSYS)
2131 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002132
Chris Wilsondb53a302011-02-03 11:57:46 +00002133 trace_i915_gem_object_unbind(obj);
2134
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002135 i915_gem_gtt_unbind_object(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002136 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002137
Chris Wilson6299f992010-11-24 12:23:44 +00002138 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002139 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002140 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002141 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002142
Chris Wilson05394f32010-11-08 19:18:58 +00002143 drm_mm_put_block(obj->gtt_space);
2144 obj->gtt_space = NULL;
2145 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002146
Chris Wilson05394f32010-11-08 19:18:58 +00002147 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002148 i915_gem_object_truncate(obj);
2149
Chris Wilson8dc17752010-07-23 23:18:51 +01002150 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002151}
2152
Chris Wilson88241782011-01-07 17:09:48 +00002153int
Chris Wilsondb53a302011-02-03 11:57:46 +00002154i915_gem_flush_ring(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002155 uint32_t invalidate_domains,
2156 uint32_t flush_domains)
2157{
Chris Wilson88241782011-01-07 17:09:48 +00002158 int ret;
2159
Chris Wilson36d527d2011-03-19 22:26:49 +00002160 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2161 return 0;
2162
Chris Wilsondb53a302011-02-03 11:57:46 +00002163 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2164
Chris Wilson88241782011-01-07 17:09:48 +00002165 ret = ring->flush(ring, invalidate_domains, flush_domains);
2166 if (ret)
2167 return ret;
2168
Chris Wilson36d527d2011-03-19 22:26:49 +00002169 if (flush_domains & I915_GEM_GPU_DOMAINS)
2170 i915_gem_process_flushing_list(ring, flush_domains);
2171
Chris Wilson88241782011-01-07 17:09:48 +00002172 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002173}
2174
Chris Wilsondb53a302011-02-03 11:57:46 +00002175static int i915_ring_idle(struct intel_ring_buffer *ring)
Chris Wilsona56ba562010-09-28 10:07:56 +01002176{
Chris Wilson88241782011-01-07 17:09:48 +00002177 int ret;
2178
Chris Wilson395b70b2010-10-28 21:28:46 +01002179 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002180 return 0;
2181
Chris Wilson88241782011-01-07 17:09:48 +00002182 if (!list_empty(&ring->gpu_write_list)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002183 ret = i915_gem_flush_ring(ring,
Chris Wilson0ac74c62010-12-06 14:36:02 +00002184 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilson88241782011-01-07 17:09:48 +00002185 if (ret)
2186 return ret;
2187 }
2188
Chris Wilsonce453d82011-02-21 14:43:56 +00002189 return i915_wait_request(ring, i915_gem_next_request_seqno(ring));
Chris Wilsona56ba562010-09-28 10:07:56 +01002190}
2191
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002192int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002193i915_gpu_idle(struct drm_device *dev)
2194{
2195 drm_i915_private_t *dev_priv = dev->dev_private;
2196 bool lists_empty;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002197 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002198
Zou Nan haid1b851f2010-05-21 09:08:57 +08002199 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002200 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002201 if (lists_empty)
2202 return 0;
2203
2204 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002205 for (i = 0; i < I915_NUM_RINGS; i++) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002206 ret = i915_ring_idle(&dev_priv->ring[i]);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002207 if (ret)
2208 return ret;
2209 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002210
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002211 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002212}
2213
Daniel Vetterc6642782010-11-12 13:46:18 +00002214static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2215 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002216{
Chris Wilson05394f32010-11-08 19:18:58 +00002217 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002218 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002219 u32 size = obj->gtt_space->size;
2220 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002221 uint64_t val;
2222
Chris Wilson05394f32010-11-08 19:18:58 +00002223 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002224 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002225 val |= obj->gtt_offset & 0xfffff000;
2226 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002227 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2228
Chris Wilson05394f32010-11-08 19:18:58 +00002229 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002230 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2231 val |= I965_FENCE_REG_VALID;
2232
Daniel Vetterc6642782010-11-12 13:46:18 +00002233 if (pipelined) {
2234 int ret = intel_ring_begin(pipelined, 6);
2235 if (ret)
2236 return ret;
2237
2238 intel_ring_emit(pipelined, MI_NOOP);
2239 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2240 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2241 intel_ring_emit(pipelined, (u32)val);
2242 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2243 intel_ring_emit(pipelined, (u32)(val >> 32));
2244 intel_ring_advance(pipelined);
2245 } else
2246 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2247
2248 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002249}
2250
Daniel Vetterc6642782010-11-12 13:46:18 +00002251static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2252 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002253{
Chris Wilson05394f32010-11-08 19:18:58 +00002254 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002255 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002256 u32 size = obj->gtt_space->size;
2257 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002258 uint64_t val;
2259
Chris Wilson05394f32010-11-08 19:18:58 +00002260 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002261 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002262 val |= obj->gtt_offset & 0xfffff000;
2263 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2264 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002265 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2266 val |= I965_FENCE_REG_VALID;
2267
Daniel Vetterc6642782010-11-12 13:46:18 +00002268 if (pipelined) {
2269 int ret = intel_ring_begin(pipelined, 6);
2270 if (ret)
2271 return ret;
2272
2273 intel_ring_emit(pipelined, MI_NOOP);
2274 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2275 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2276 intel_ring_emit(pipelined, (u32)val);
2277 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2278 intel_ring_emit(pipelined, (u32)(val >> 32));
2279 intel_ring_advance(pipelined);
2280 } else
2281 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2282
2283 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002284}
2285
Daniel Vetterc6642782010-11-12 13:46:18 +00002286static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2287 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002288{
Chris Wilson05394f32010-11-08 19:18:58 +00002289 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002290 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002291 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002292 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002293 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002294
Daniel Vetterc6642782010-11-12 13:46:18 +00002295 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2296 (size & -size) != size ||
2297 (obj->gtt_offset & (size - 1)),
2298 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2299 obj->gtt_offset, obj->map_and_fenceable, size))
2300 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002301
Daniel Vetterc6642782010-11-12 13:46:18 +00002302 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002303 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002304 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002305 tile_width = 512;
2306
2307 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002308 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002309 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002310
Chris Wilson05394f32010-11-08 19:18:58 +00002311 val = obj->gtt_offset;
2312 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002313 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002314 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002315 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2316 val |= I830_FENCE_REG_VALID;
2317
Chris Wilson05394f32010-11-08 19:18:58 +00002318 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002319 if (fence_reg < 8)
2320 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002321 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002322 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002323
2324 if (pipelined) {
2325 int ret = intel_ring_begin(pipelined, 4);
2326 if (ret)
2327 return ret;
2328
2329 intel_ring_emit(pipelined, MI_NOOP);
2330 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2331 intel_ring_emit(pipelined, fence_reg);
2332 intel_ring_emit(pipelined, val);
2333 intel_ring_advance(pipelined);
2334 } else
2335 I915_WRITE(fence_reg, val);
2336
2337 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002338}
2339
Daniel Vetterc6642782010-11-12 13:46:18 +00002340static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2341 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002342{
Chris Wilson05394f32010-11-08 19:18:58 +00002343 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002344 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002345 u32 size = obj->gtt_space->size;
2346 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002347 uint32_t val;
2348 uint32_t pitch_val;
2349
Daniel Vetterc6642782010-11-12 13:46:18 +00002350 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2351 (size & -size) != size ||
2352 (obj->gtt_offset & (size - 1)),
2353 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2354 obj->gtt_offset, size))
2355 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356
Chris Wilson05394f32010-11-08 19:18:58 +00002357 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002358 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002359
Chris Wilson05394f32010-11-08 19:18:58 +00002360 val = obj->gtt_offset;
2361 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002363 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002364 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2365 val |= I830_FENCE_REG_VALID;
2366
Daniel Vetterc6642782010-11-12 13:46:18 +00002367 if (pipelined) {
2368 int ret = intel_ring_begin(pipelined, 4);
2369 if (ret)
2370 return ret;
2371
2372 intel_ring_emit(pipelined, MI_NOOP);
2373 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2374 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2375 intel_ring_emit(pipelined, val);
2376 intel_ring_advance(pipelined);
2377 } else
2378 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2379
2380 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002381}
2382
Chris Wilsond9e86c02010-11-10 16:40:20 +00002383static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2384{
2385 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2386}
2387
2388static int
2389i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002390 struct intel_ring_buffer *pipelined)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002391{
2392 int ret;
2393
2394 if (obj->fenced_gpu_access) {
Chris Wilson88241782011-01-07 17:09:48 +00002395 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002396 ret = i915_gem_flush_ring(obj->last_fenced_ring,
Chris Wilson88241782011-01-07 17:09:48 +00002397 0, obj->base.write_domain);
2398 if (ret)
2399 return ret;
2400 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002401
2402 obj->fenced_gpu_access = false;
2403 }
2404
2405 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2406 if (!ring_passed_seqno(obj->last_fenced_ring,
2407 obj->last_fenced_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002408 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002409 obj->last_fenced_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002410 if (ret)
2411 return ret;
2412 }
2413
2414 obj->last_fenced_seqno = 0;
2415 obj->last_fenced_ring = NULL;
2416 }
2417
Chris Wilson63256ec2011-01-04 18:42:07 +00002418 /* Ensure that all CPU reads are completed before installing a fence
2419 * and all writes before removing the fence.
2420 */
2421 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2422 mb();
2423
Chris Wilsond9e86c02010-11-10 16:40:20 +00002424 return 0;
2425}
2426
2427int
2428i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2429{
2430 int ret;
2431
2432 if (obj->tiling_mode)
2433 i915_gem_release_mmap(obj);
2434
Chris Wilsonce453d82011-02-21 14:43:56 +00002435 ret = i915_gem_object_flush_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002436 if (ret)
2437 return ret;
2438
2439 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2440 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2441 i915_gem_clear_fence_reg(obj->base.dev,
2442 &dev_priv->fence_regs[obj->fence_reg]);
2443
2444 obj->fence_reg = I915_FENCE_REG_NONE;
2445 }
2446
2447 return 0;
2448}
2449
2450static struct drm_i915_fence_reg *
2451i915_find_fence_reg(struct drm_device *dev,
2452 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002453{
Daniel Vetterae3db242010-02-19 11:51:58 +01002454 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002455 struct drm_i915_fence_reg *reg, *first, *avail;
2456 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002457
2458 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002459 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002460 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2461 reg = &dev_priv->fence_regs[i];
2462 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002463 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002464
Chris Wilson05394f32010-11-08 19:18:58 +00002465 if (!reg->obj->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002466 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002467 }
2468
Chris Wilsond9e86c02010-11-10 16:40:20 +00002469 if (avail == NULL)
2470 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002471
2472 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002473 avail = first = NULL;
2474 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2475 if (reg->obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002476 continue;
2477
Chris Wilsond9e86c02010-11-10 16:40:20 +00002478 if (first == NULL)
2479 first = reg;
2480
2481 if (!pipelined ||
2482 !reg->obj->last_fenced_ring ||
2483 reg->obj->last_fenced_ring == pipelined) {
2484 avail = reg;
2485 break;
2486 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002487 }
2488
Chris Wilsond9e86c02010-11-10 16:40:20 +00002489 if (avail == NULL)
2490 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002491
Chris Wilsona00b10c2010-09-24 21:15:47 +01002492 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002493}
2494
Jesse Barnesde151cf2008-11-12 10:03:55 -08002495/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002496 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002497 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002498 * @pipelined: ring on which to queue the change, or NULL for CPU access
2499 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002500 *
2501 * When mapping objects through the GTT, userspace wants to be able to write
2502 * to them without having to worry about swizzling if the object is tiled.
2503 *
2504 * This function walks the fence regs looking for a free one for @obj,
2505 * stealing one if it can't find any.
2506 *
2507 * It then sets up the reg based on the object's properties: address, pitch
2508 * and tiling format.
2509 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002510int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002511i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002512 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002513{
Chris Wilson05394f32010-11-08 19:18:58 +00002514 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002515 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002516 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002517 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002518
Chris Wilson6bda10d2010-12-05 21:04:18 +00002519 /* XXX disable pipelining. There are bugs. Shocking. */
2520 pipelined = NULL;
2521
Chris Wilsond9e86c02010-11-10 16:40:20 +00002522 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002523 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2524 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002525 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002526
Chris Wilson29c5a582011-03-17 15:23:22 +00002527 if (obj->tiling_changed) {
2528 ret = i915_gem_object_flush_fence(obj, pipelined);
2529 if (ret)
2530 return ret;
2531
2532 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2533 pipelined = NULL;
2534
2535 if (pipelined) {
2536 reg->setup_seqno =
2537 i915_gem_next_request_seqno(pipelined);
2538 obj->last_fenced_seqno = reg->setup_seqno;
2539 obj->last_fenced_ring = pipelined;
2540 }
2541
2542 goto update;
2543 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002544
2545 if (!pipelined) {
2546 if (reg->setup_seqno) {
2547 if (!ring_passed_seqno(obj->last_fenced_ring,
2548 reg->setup_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002549 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002550 reg->setup_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002551 if (ret)
2552 return ret;
2553 }
2554
2555 reg->setup_seqno = 0;
2556 }
2557 } else if (obj->last_fenced_ring &&
2558 obj->last_fenced_ring != pipelined) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002559 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002560 if (ret)
2561 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002562 }
2563
Eric Anholta09ba7f2009-08-29 12:49:51 -07002564 return 0;
2565 }
2566
Chris Wilsond9e86c02010-11-10 16:40:20 +00002567 reg = i915_find_fence_reg(dev, pipelined);
2568 if (reg == NULL)
2569 return -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002570
Chris Wilsonce453d82011-02-21 14:43:56 +00002571 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002572 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002573 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002574
Chris Wilsond9e86c02010-11-10 16:40:20 +00002575 if (reg->obj) {
2576 struct drm_i915_gem_object *old = reg->obj;
2577
2578 drm_gem_object_reference(&old->base);
2579
2580 if (old->tiling_mode)
2581 i915_gem_release_mmap(old);
2582
Chris Wilsonce453d82011-02-21 14:43:56 +00002583 ret = i915_gem_object_flush_fence(old, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002584 if (ret) {
2585 drm_gem_object_unreference(&old->base);
2586 return ret;
2587 }
2588
2589 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2590 pipelined = NULL;
2591
2592 old->fence_reg = I915_FENCE_REG_NONE;
2593 old->last_fenced_ring = pipelined;
2594 old->last_fenced_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002595 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002596
2597 drm_gem_object_unreference(&old->base);
2598 } else if (obj->last_fenced_seqno == 0)
2599 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002600
Jesse Barnesde151cf2008-11-12 10:03:55 -08002601 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002602 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2603 obj->fence_reg = reg - dev_priv->fence_regs;
2604 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002605
Chris Wilsond9e86c02010-11-10 16:40:20 +00002606 reg->setup_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002607 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002608 obj->last_fenced_seqno = reg->setup_seqno;
2609
2610update:
2611 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002612 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002613 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002614 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002615 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002616 break;
2617 case 5:
2618 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002619 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002620 break;
2621 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002622 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002623 break;
2624 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002625 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002626 break;
2627 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002628
Daniel Vetterc6642782010-11-12 13:46:18 +00002629 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002630}
2631
2632/**
2633 * i915_gem_clear_fence_reg - clear out fence register info
2634 * @obj: object to clear
2635 *
2636 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002637 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002638 */
2639static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002640i915_gem_clear_fence_reg(struct drm_device *dev,
2641 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002642{
Jesse Barnes79e53942008-11-07 14:24:08 -08002643 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002644 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002645
Chris Wilsone259bef2010-09-17 00:32:02 +01002646 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002647 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002648 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002649 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002650 break;
2651 case 5:
2652 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002653 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002654 break;
2655 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002656 if (fence_reg >= 8)
2657 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002658 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002659 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002660 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002661
2662 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002663 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002664 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002665
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002666 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002667 reg->obj = NULL;
2668 reg->setup_seqno = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002669}
2670
2671/**
Eric Anholt673a3942008-07-30 12:06:12 -07002672 * Finds free space in the GTT aperture and binds the object there.
2673 */
2674static int
Chris Wilson05394f32010-11-08 19:18:58 +00002675i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002676 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002677 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002678{
Chris Wilson05394f32010-11-08 19:18:58 +00002679 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002680 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002681 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002682 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002683 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002684 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002685 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002686
Chris Wilson05394f32010-11-08 19:18:58 +00002687 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002688 DRM_ERROR("Attempting to bind a purgeable object\n");
2689 return -EINVAL;
2690 }
2691
Chris Wilsone28f8712011-07-18 13:11:49 -07002692 fence_size = i915_gem_get_gtt_size(dev,
2693 obj->base.size,
2694 obj->tiling_mode);
2695 fence_alignment = i915_gem_get_gtt_alignment(dev,
2696 obj->base.size,
2697 obj->tiling_mode);
2698 unfenced_alignment =
2699 i915_gem_get_unfenced_gtt_alignment(dev,
2700 obj->base.size,
2701 obj->tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002702
Eric Anholt673a3942008-07-30 12:06:12 -07002703 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002704 alignment = map_and_fenceable ? fence_alignment :
2705 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002706 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002707 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2708 return -EINVAL;
2709 }
2710
Chris Wilson05394f32010-11-08 19:18:58 +00002711 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002712
Chris Wilson654fc602010-05-27 13:18:21 +01002713 /* If the object is bigger than the entire aperture, reject it early
2714 * before evicting everything in a vain attempt to find space.
2715 */
Chris Wilson05394f32010-11-08 19:18:58 +00002716 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002717 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002718 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2719 return -E2BIG;
2720 }
2721
Eric Anholt673a3942008-07-30 12:06:12 -07002722 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002723 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002724 free_space =
2725 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002726 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002727 dev_priv->mm.gtt_mappable_end,
2728 0);
2729 else
2730 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002731 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002732
2733 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002734 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002735 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002736 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002737 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002738 dev_priv->mm.gtt_mappable_end,
2739 0);
2740 else
Chris Wilson05394f32010-11-08 19:18:58 +00002741 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002742 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002743 }
Chris Wilson05394f32010-11-08 19:18:58 +00002744 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002745 /* If the gtt is empty and we're still having trouble
2746 * fitting our object in, we're out of memory.
2747 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002748 ret = i915_gem_evict_something(dev, size, alignment,
2749 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002750 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002751 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002752
Eric Anholt673a3942008-07-30 12:06:12 -07002753 goto search_free;
2754 }
2755
Chris Wilsone5281cc2010-10-28 13:45:36 +01002756 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002757 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002758 drm_mm_put_block(obj->gtt_space);
2759 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002760
2761 if (ret == -ENOMEM) {
Chris Wilson809b6332011-01-10 17:33:15 +00002762 /* first try to reclaim some memory by clearing the GTT */
2763 ret = i915_gem_evict_everything(dev, false);
Chris Wilson07f73f62009-09-14 16:50:30 +01002764 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002765 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002766 if (gfpmask) {
2767 gfpmask = 0;
2768 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002769 }
2770
Chris Wilson809b6332011-01-10 17:33:15 +00002771 return -ENOMEM;
Chris Wilson07f73f62009-09-14 16:50:30 +01002772 }
2773
2774 goto search_free;
2775 }
2776
Eric Anholt673a3942008-07-30 12:06:12 -07002777 return ret;
2778 }
2779
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002780 ret = i915_gem_gtt_bind_object(obj);
2781 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002782 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002783 drm_mm_put_block(obj->gtt_space);
2784 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002785
Chris Wilson809b6332011-01-10 17:33:15 +00002786 if (i915_gem_evict_everything(dev, false))
Chris Wilson07f73f62009-09-14 16:50:30 +01002787 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002788
2789 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002790 }
Eric Anholt673a3942008-07-30 12:06:12 -07002791
Chris Wilson6299f992010-11-24 12:23:44 +00002792 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002793 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002794
Eric Anholt673a3942008-07-30 12:06:12 -07002795 /* Assert that the object is not currently in any GPU domain. As it
2796 * wasn't in the GTT, there shouldn't be any way it could have been in
2797 * a GPU cache
2798 */
Chris Wilson05394f32010-11-08 19:18:58 +00002799 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2800 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002801
Chris Wilson6299f992010-11-24 12:23:44 +00002802 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002803
Daniel Vetter75e9e912010-11-04 17:11:09 +01002804 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002805 obj->gtt_space->size == fence_size &&
Akshay Joshi0206e352011-08-16 15:34:10 -04002806 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002807
Daniel Vetter75e9e912010-11-04 17:11:09 +01002808 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002809 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002810
Chris Wilson05394f32010-11-08 19:18:58 +00002811 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002812
Chris Wilsondb53a302011-02-03 11:57:46 +00002813 trace_i915_gem_object_bind(obj, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002814 return 0;
2815}
2816
2817void
Chris Wilson05394f32010-11-08 19:18:58 +00002818i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002819{
Eric Anholt673a3942008-07-30 12:06:12 -07002820 /* If we don't have a page list set up, then we're not pinned
2821 * to GPU, and we can ignore the cache flush because it'll happen
2822 * again at bind time.
2823 */
Chris Wilson05394f32010-11-08 19:18:58 +00002824 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002825 return;
2826
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002827 /* If the GPU is snooping the contents of the CPU cache,
2828 * we do not need to manually clear the CPU cache lines. However,
2829 * the caches are only snooped when the render cache is
2830 * flushed/invalidated. As we always have to emit invalidations
2831 * and flushes when moving into and out of the RENDER domain, correct
2832 * snooping behaviour occurs naturally as the result of our domain
2833 * tracking.
2834 */
2835 if (obj->cache_level != I915_CACHE_NONE)
2836 return;
2837
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002838 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002839
Chris Wilson05394f32010-11-08 19:18:58 +00002840 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002841}
2842
Eric Anholte47c68e2008-11-14 13:35:19 -08002843/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson88241782011-01-07 17:09:48 +00002844static int
Chris Wilson3619df02010-11-28 15:37:17 +00002845i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002846{
Chris Wilson05394f32010-11-08 19:18:58 +00002847 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson88241782011-01-07 17:09:48 +00002848 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002849
2850 /* Queue the GPU write cache flushing we need. */
Chris Wilsondb53a302011-02-03 11:57:46 +00002851 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002852}
2853
2854/** Flushes the GTT write domain for the object if it's dirty. */
2855static void
Chris Wilson05394f32010-11-08 19:18:58 +00002856i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002857{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002858 uint32_t old_write_domain;
2859
Chris Wilson05394f32010-11-08 19:18:58 +00002860 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002861 return;
2862
Chris Wilson63256ec2011-01-04 18:42:07 +00002863 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002864 * to it immediately go to main memory as far as we know, so there's
2865 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002866 *
2867 * However, we do have to enforce the order so that all writes through
2868 * the GTT land before any writes to the device, such as updates to
2869 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002870 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002871 wmb();
2872
Chris Wilson05394f32010-11-08 19:18:58 +00002873 old_write_domain = obj->base.write_domain;
2874 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002875
2876 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002877 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002878 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002879}
2880
2881/** Flushes the CPU write domain for the object if it's dirty. */
2882static void
Chris Wilson05394f32010-11-08 19:18:58 +00002883i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002884{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002885 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002886
Chris Wilson05394f32010-11-08 19:18:58 +00002887 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002888 return;
2889
2890 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002891 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002892 old_write_domain = obj->base.write_domain;
2893 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002894
2895 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002896 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002897 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002898}
2899
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002900/**
2901 * Moves a single object to the GTT read, and possibly write domain.
2902 *
2903 * This function returns when the move is complete, including waiting on
2904 * flushes to occur.
2905 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002906int
Chris Wilson20217462010-11-23 15:26:33 +00002907i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002908{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002909 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002910 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002911
Eric Anholt02354392008-11-26 13:58:13 -08002912 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002913 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002914 return -EINVAL;
2915
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002916 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2917 return 0;
2918
Chris Wilson88241782011-01-07 17:09:48 +00002919 ret = i915_gem_object_flush_gpu_write_domain(obj);
2920 if (ret)
2921 return ret;
2922
Chris Wilson87ca9c82010-12-02 09:42:56 +00002923 if (obj->pending_gpu_write || write) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002924 ret = i915_gem_object_wait_rendering(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00002925 if (ret)
2926 return ret;
2927 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002928
Chris Wilson72133422010-09-13 23:56:38 +01002929 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002930
Chris Wilson05394f32010-11-08 19:18:58 +00002931 old_write_domain = obj->base.write_domain;
2932 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002933
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002934 /* It should now be out of any other write domains, and we can update
2935 * the domain values for our changes.
2936 */
Chris Wilson05394f32010-11-08 19:18:58 +00002937 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2938 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002939 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002940 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2941 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2942 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002943 }
2944
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002945 trace_i915_gem_object_change_domain(obj,
2946 old_read_domains,
2947 old_write_domain);
2948
Eric Anholte47c68e2008-11-14 13:35:19 -08002949 return 0;
2950}
2951
Chris Wilsone4ffd172011-04-04 09:44:39 +01002952int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2953 enum i915_cache_level cache_level)
2954{
2955 int ret;
2956
2957 if (obj->cache_level == cache_level)
2958 return 0;
2959
2960 if (obj->pin_count) {
2961 DRM_DEBUG("can not change the cache level of pinned objects\n");
2962 return -EBUSY;
2963 }
2964
2965 if (obj->gtt_space) {
2966 ret = i915_gem_object_finish_gpu(obj);
2967 if (ret)
2968 return ret;
2969
2970 i915_gem_object_finish_gtt(obj);
2971
2972 /* Before SandyBridge, you could not use tiling or fence
2973 * registers with snooped memory, so relinquish any fences
2974 * currently pointing to our region in the aperture.
2975 */
2976 if (INTEL_INFO(obj->base.dev)->gen < 6) {
2977 ret = i915_gem_object_put_fence(obj);
2978 if (ret)
2979 return ret;
2980 }
2981
2982 i915_gem_gtt_rebind_object(obj, cache_level);
2983 }
2984
2985 if (cache_level == I915_CACHE_NONE) {
2986 u32 old_read_domains, old_write_domain;
2987
2988 /* If we're coming from LLC cached, then we haven't
2989 * actually been tracking whether the data is in the
2990 * CPU cache or not, since we only allow one bit set
2991 * in obj->write_domain and have been skipping the clflushes.
2992 * Just set it to the CPU cache for now.
2993 */
2994 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
2995 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
2996
2997 old_read_domains = obj->base.read_domains;
2998 old_write_domain = obj->base.write_domain;
2999
3000 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3001 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3002
3003 trace_i915_gem_object_change_domain(obj,
3004 old_read_domains,
3005 old_write_domain);
3006 }
3007
3008 obj->cache_level = cache_level;
3009 return 0;
3010}
3011
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003012/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003013 * Prepare buffer for display plane (scanout, cursors, etc).
3014 * Can be called from an uninterruptible phase (modesetting) and allows
3015 * any flushes to be pipelined (for pageflips).
3016 *
3017 * For the display plane, we want to be in the GTT but out of any write
3018 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
3019 * ability to pipeline the waits, pinning and any additional subtleties
3020 * that may differentiate the display plane from ordinary buffers.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003021 */
3022int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003023i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3024 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003025 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003026{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003027 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003028 int ret;
3029
Chris Wilson88241782011-01-07 17:09:48 +00003030 ret = i915_gem_object_flush_gpu_write_domain(obj);
3031 if (ret)
3032 return ret;
3033
Chris Wilson0be73282010-12-06 14:36:27 +00003034 if (pipelined != obj->ring) {
Chris Wilsonce453d82011-02-21 14:43:56 +00003035 ret = i915_gem_object_wait_rendering(obj);
Keith Packardf0b69ef2011-07-19 16:21:40 -07003036 if (ret == -ERESTARTSYS)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003037 return ret;
3038 }
3039
Eric Anholta7ef0642011-03-29 16:59:54 -07003040 /* The display engine is not coherent with the LLC cache on gen6. As
3041 * a result, we make sure that the pinning that is about to occur is
3042 * done with uncached PTEs. This is lowest common denominator for all
3043 * chipsets.
3044 *
3045 * However for gen6+, we could do better by using the GFDT bit instead
3046 * of uncaching, which would allow us to flush all the LLC-cached data
3047 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3048 */
3049 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3050 if (ret)
3051 return ret;
3052
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003053 /* As the user may map the buffer once pinned in the display plane
3054 * (e.g. libkms for the bootup splash), we have to ensure that we
3055 * always use map_and_fenceable for all scanout buffers.
3056 */
3057 ret = i915_gem_object_pin(obj, alignment, true);
3058 if (ret)
3059 return ret;
3060
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003061 i915_gem_object_flush_cpu_write_domain(obj);
3062
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003063 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003064 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003065
3066 /* It should now be out of any other write domains, and we can update
3067 * the domain values for our changes.
3068 */
3069 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003070 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003071
3072 trace_i915_gem_object_change_domain(obj,
3073 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003074 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003075
3076 return 0;
3077}
3078
Chris Wilson85345512010-11-13 09:49:11 +00003079int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003080i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003081{
Chris Wilson88241782011-01-07 17:09:48 +00003082 int ret;
3083
Chris Wilsona8198ee2011-04-13 22:04:09 +01003084 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003085 return 0;
3086
Chris Wilson88241782011-01-07 17:09:48 +00003087 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003088 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Chris Wilson88241782011-01-07 17:09:48 +00003089 if (ret)
3090 return ret;
3091 }
Chris Wilson85345512010-11-13 09:49:11 +00003092
Chris Wilsona8198ee2011-04-13 22:04:09 +01003093 /* Ensure that we invalidate the GPU's caches and TLBs. */
3094 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3095
Chris Wilsonce453d82011-02-21 14:43:56 +00003096 return i915_gem_object_wait_rendering(obj);
Chris Wilson85345512010-11-13 09:49:11 +00003097}
3098
Eric Anholte47c68e2008-11-14 13:35:19 -08003099/**
3100 * Moves a single object to the CPU read, and possibly write domain.
3101 *
3102 * This function returns when the move is complete, including waiting on
3103 * flushes to occur.
3104 */
3105static int
Chris Wilson919926a2010-11-12 13:42:53 +00003106i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003107{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003108 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003109 int ret;
3110
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003111 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3112 return 0;
3113
Chris Wilson88241782011-01-07 17:09:48 +00003114 ret = i915_gem_object_flush_gpu_write_domain(obj);
3115 if (ret)
3116 return ret;
3117
Chris Wilsonce453d82011-02-21 14:43:56 +00003118 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003119 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003120 return ret;
3121
3122 i915_gem_object_flush_gtt_write_domain(obj);
3123
3124 /* If we have a partially-valid cache of the object in the CPU,
3125 * finish invalidating it and free the per-page flags.
3126 */
3127 i915_gem_object_set_to_full_cpu_read_domain(obj);
3128
Chris Wilson05394f32010-11-08 19:18:58 +00003129 old_write_domain = obj->base.write_domain;
3130 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003131
Eric Anholte47c68e2008-11-14 13:35:19 -08003132 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003133 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003134 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003135
Chris Wilson05394f32010-11-08 19:18:58 +00003136 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003137 }
3138
3139 /* It should now be out of any other write domains, and we can update
3140 * the domain values for our changes.
3141 */
Chris Wilson05394f32010-11-08 19:18:58 +00003142 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003143
3144 /* If we're writing through the CPU, then the GPU read domains will
3145 * need to be invalidated at next use.
3146 */
3147 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003148 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3149 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003150 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003151
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003152 trace_i915_gem_object_change_domain(obj,
3153 old_read_domains,
3154 old_write_domain);
3155
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003156 return 0;
3157}
3158
Eric Anholt673a3942008-07-30 12:06:12 -07003159/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003160 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003161 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003162 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3163 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3164 */
3165static void
Chris Wilson05394f32010-11-08 19:18:58 +00003166i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003167{
Chris Wilson05394f32010-11-08 19:18:58 +00003168 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003169 return;
3170
3171 /* If we're partially in the CPU read domain, finish moving it in.
3172 */
Chris Wilson05394f32010-11-08 19:18:58 +00003173 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003174 int i;
3175
Chris Wilson05394f32010-11-08 19:18:58 +00003176 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3177 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003178 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003179 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003180 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003181 }
3182
3183 /* Free the page_cpu_valid mappings which are now stale, whether
3184 * or not we've got I915_GEM_DOMAIN_CPU.
3185 */
Chris Wilson05394f32010-11-08 19:18:58 +00003186 kfree(obj->page_cpu_valid);
3187 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003188}
3189
3190/**
3191 * Set the CPU read domain on a range of the object.
3192 *
3193 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3194 * not entirely valid. The page_cpu_valid member of the object flags which
3195 * pages have been flushed, and will be respected by
3196 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3197 * of the whole object.
3198 *
3199 * This function returns when the move is complete, including waiting on
3200 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003201 */
3202static int
Chris Wilson05394f32010-11-08 19:18:58 +00003203i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003204 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003205{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003206 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003207 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003208
Chris Wilson05394f32010-11-08 19:18:58 +00003209 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003210 return i915_gem_object_set_to_cpu_domain(obj, 0);
3211
Chris Wilson88241782011-01-07 17:09:48 +00003212 ret = i915_gem_object_flush_gpu_write_domain(obj);
3213 if (ret)
3214 return ret;
3215
Chris Wilsonce453d82011-02-21 14:43:56 +00003216 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003217 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003218 return ret;
Daniel Vetterde18a292010-11-27 22:30:41 +01003219
Eric Anholte47c68e2008-11-14 13:35:19 -08003220 i915_gem_object_flush_gtt_write_domain(obj);
3221
3222 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003223 if (obj->page_cpu_valid == NULL &&
3224 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003225 return 0;
3226
Eric Anholte47c68e2008-11-14 13:35:19 -08003227 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3228 * newly adding I915_GEM_DOMAIN_CPU
3229 */
Chris Wilson05394f32010-11-08 19:18:58 +00003230 if (obj->page_cpu_valid == NULL) {
3231 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3232 GFP_KERNEL);
3233 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003234 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003235 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3236 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003237
3238 /* Flush the cache on any pages that are still invalid from the CPU's
3239 * perspective.
3240 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003241 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3242 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003243 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003244 continue;
3245
Chris Wilson05394f32010-11-08 19:18:58 +00003246 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003247
Chris Wilson05394f32010-11-08 19:18:58 +00003248 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003249 }
3250
Eric Anholte47c68e2008-11-14 13:35:19 -08003251 /* It should now be out of any other write domains, and we can update
3252 * the domain values for our changes.
3253 */
Chris Wilson05394f32010-11-08 19:18:58 +00003254 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003255
Chris Wilson05394f32010-11-08 19:18:58 +00003256 old_read_domains = obj->base.read_domains;
3257 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003258
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003259 trace_i915_gem_object_change_domain(obj,
3260 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003261 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003262
Eric Anholt673a3942008-07-30 12:06:12 -07003263 return 0;
3264}
3265
Eric Anholt673a3942008-07-30 12:06:12 -07003266/* Throttle our rendering by waiting until the ring has completed our requests
3267 * emitted over 20 msec ago.
3268 *
Eric Anholtb9624422009-06-03 07:27:35 +00003269 * Note that if we were to use the current jiffies each time around the loop,
3270 * we wouldn't escape the function with any frames outstanding if the time to
3271 * render a frame was over 20ms.
3272 *
Eric Anholt673a3942008-07-30 12:06:12 -07003273 * This should get us reasonable parallelism between CPU and GPU but also
3274 * relatively low latency when blocking on a particular request to finish.
3275 */
3276static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003277i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003278{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003279 struct drm_i915_private *dev_priv = dev->dev_private;
3280 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003281 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003282 struct drm_i915_gem_request *request;
3283 struct intel_ring_buffer *ring = NULL;
3284 u32 seqno = 0;
3285 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003286
Chris Wilsone110e8d2011-01-26 15:39:14 +00003287 if (atomic_read(&dev_priv->mm.wedged))
3288 return -EIO;
3289
Chris Wilson1c255952010-09-26 11:03:27 +01003290 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003291 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003292 if (time_after_eq(request->emitted_jiffies, recent_enough))
3293 break;
3294
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003295 ring = request->ring;
3296 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003297 }
Chris Wilson1c255952010-09-26 11:03:27 +01003298 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003299
3300 if (seqno == 0)
3301 return 0;
3302
3303 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003304 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003305 /* And wait for the seqno passing without holding any locks and
3306 * causing extra latency for others. This is safe as the irq
3307 * generation is designed to be run atomically and so is
3308 * lockless.
3309 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003310 if (ring->irq_get(ring)) {
3311 ret = wait_event_interruptible(ring->irq_queue,
3312 i915_seqno_passed(ring->get_seqno(ring), seqno)
3313 || atomic_read(&dev_priv->mm.wedged));
3314 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003315
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003316 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3317 ret = -EIO;
3318 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003319 }
3320
3321 if (ret == 0)
3322 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003323
Eric Anholt673a3942008-07-30 12:06:12 -07003324 return ret;
3325}
3326
Eric Anholt673a3942008-07-30 12:06:12 -07003327int
Chris Wilson05394f32010-11-08 19:18:58 +00003328i915_gem_object_pin(struct drm_i915_gem_object *obj,
3329 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003330 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003331{
Chris Wilson05394f32010-11-08 19:18:58 +00003332 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003333 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003334 int ret;
3335
Chris Wilson05394f32010-11-08 19:18:58 +00003336 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003337 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003338
Chris Wilson05394f32010-11-08 19:18:58 +00003339 if (obj->gtt_space != NULL) {
3340 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3341 (map_and_fenceable && !obj->map_and_fenceable)) {
3342 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003343 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003344 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3345 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003346 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003347 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003348 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003349 ret = i915_gem_object_unbind(obj);
3350 if (ret)
3351 return ret;
3352 }
3353 }
3354
Chris Wilson05394f32010-11-08 19:18:58 +00003355 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003356 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003357 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003358 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003359 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003360 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003361
Chris Wilson05394f32010-11-08 19:18:58 +00003362 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003363 if (!obj->active)
3364 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003365 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003366 }
Chris Wilson6299f992010-11-24 12:23:44 +00003367 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003368
Chris Wilson23bc5982010-09-29 16:10:57 +01003369 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003370 return 0;
3371}
3372
3373void
Chris Wilson05394f32010-11-08 19:18:58 +00003374i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003375{
Chris Wilson05394f32010-11-08 19:18:58 +00003376 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003377 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003378
Chris Wilson23bc5982010-09-29 16:10:57 +01003379 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003380 BUG_ON(obj->pin_count == 0);
3381 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003382
Chris Wilson05394f32010-11-08 19:18:58 +00003383 if (--obj->pin_count == 0) {
3384 if (!obj->active)
3385 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003386 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003387 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003388 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003389 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003390}
3391
3392int
3393i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003394 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003395{
3396 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003397 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003398 int ret;
3399
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003400 ret = i915_mutex_lock_interruptible(dev);
3401 if (ret)
3402 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003403
Chris Wilson05394f32010-11-08 19:18:58 +00003404 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003405 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003406 ret = -ENOENT;
3407 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003408 }
Eric Anholt673a3942008-07-30 12:06:12 -07003409
Chris Wilson05394f32010-11-08 19:18:58 +00003410 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003411 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003412 ret = -EINVAL;
3413 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003414 }
3415
Chris Wilson05394f32010-11-08 19:18:58 +00003416 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003417 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3418 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003419 ret = -EINVAL;
3420 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003421 }
3422
Chris Wilson05394f32010-11-08 19:18:58 +00003423 obj->user_pin_count++;
3424 obj->pin_filp = file;
3425 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003426 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003427 if (ret)
3428 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003429 }
3430
3431 /* XXX - flush the CPU caches for pinned objects
3432 * as the X server doesn't manage domains yet
3433 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003434 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003435 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003436out:
Chris Wilson05394f32010-11-08 19:18:58 +00003437 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003438unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003439 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003440 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003441}
3442
3443int
3444i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003445 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003446{
3447 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003448 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003449 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003450
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003451 ret = i915_mutex_lock_interruptible(dev);
3452 if (ret)
3453 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003454
Chris Wilson05394f32010-11-08 19:18:58 +00003455 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003456 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003457 ret = -ENOENT;
3458 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003459 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003460
Chris Wilson05394f32010-11-08 19:18:58 +00003461 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003462 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3463 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003464 ret = -EINVAL;
3465 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003466 }
Chris Wilson05394f32010-11-08 19:18:58 +00003467 obj->user_pin_count--;
3468 if (obj->user_pin_count == 0) {
3469 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003470 i915_gem_object_unpin(obj);
3471 }
Eric Anholt673a3942008-07-30 12:06:12 -07003472
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003473out:
Chris Wilson05394f32010-11-08 19:18:58 +00003474 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003475unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003476 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003477 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003478}
3479
3480int
3481i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003482 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003483{
3484 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003485 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003486 int ret;
3487
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003488 ret = i915_mutex_lock_interruptible(dev);
3489 if (ret)
3490 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003491
Chris Wilson05394f32010-11-08 19:18:58 +00003492 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003493 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003494 ret = -ENOENT;
3495 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003496 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003497
Chris Wilson0be555b2010-08-04 15:36:30 +01003498 /* Count all active objects as busy, even if they are currently not used
3499 * by the gpu. Users of this interface expect objects to eventually
3500 * become non-busy without any further actions, therefore emit any
3501 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003502 */
Chris Wilson05394f32010-11-08 19:18:58 +00003503 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003504 if (args->busy) {
3505 /* Unconditionally flush objects, even when the gpu still uses this
3506 * object. Userspace calling this function indicates that it wants to
3507 * use this buffer rather sooner than later, so issuing the required
3508 * flush earlier is beneficial.
3509 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003510 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003511 ret = i915_gem_flush_ring(obj->ring,
Chris Wilson88241782011-01-07 17:09:48 +00003512 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003513 } else if (obj->ring->outstanding_lazy_request ==
3514 obj->last_rendering_seqno) {
3515 struct drm_i915_gem_request *request;
3516
Chris Wilson7a194872010-12-07 10:38:40 +00003517 /* This ring is not being cleared by active usage,
3518 * so emit a request to do so.
3519 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003520 request = kzalloc(sizeof(*request), GFP_KERNEL);
3521 if (request)
Akshay Joshi0206e352011-08-16 15:34:10 -04003522 ret = i915_add_request(obj->ring, NULL, request);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003523 else
Chris Wilson7a194872010-12-07 10:38:40 +00003524 ret = -ENOMEM;
3525 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003526
3527 /* Update the active list for the hardware's current position.
3528 * Otherwise this only updates on a delayed timer or when irqs
3529 * are actually unmasked, and our working set ends up being
3530 * larger than required.
3531 */
Chris Wilsondb53a302011-02-03 11:57:46 +00003532 i915_gem_retire_requests_ring(obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003533
Chris Wilson05394f32010-11-08 19:18:58 +00003534 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003535 }
Eric Anholt673a3942008-07-30 12:06:12 -07003536
Chris Wilson05394f32010-11-08 19:18:58 +00003537 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003538unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003539 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003540 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003541}
3542
3543int
3544i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3545 struct drm_file *file_priv)
3546{
Akshay Joshi0206e352011-08-16 15:34:10 -04003547 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003548}
3549
Chris Wilson3ef94da2009-09-14 16:50:29 +01003550int
3551i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3552 struct drm_file *file_priv)
3553{
3554 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003555 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003556 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003557
3558 switch (args->madv) {
3559 case I915_MADV_DONTNEED:
3560 case I915_MADV_WILLNEED:
3561 break;
3562 default:
3563 return -EINVAL;
3564 }
3565
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003566 ret = i915_mutex_lock_interruptible(dev);
3567 if (ret)
3568 return ret;
3569
Chris Wilson05394f32010-11-08 19:18:58 +00003570 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003571 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003572 ret = -ENOENT;
3573 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003574 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003575
Chris Wilson05394f32010-11-08 19:18:58 +00003576 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003577 ret = -EINVAL;
3578 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003579 }
3580
Chris Wilson05394f32010-11-08 19:18:58 +00003581 if (obj->madv != __I915_MADV_PURGED)
3582 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003583
Chris Wilson2d7ef392009-09-20 23:13:10 +01003584 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003585 if (i915_gem_object_is_purgeable(obj) &&
3586 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003587 i915_gem_object_truncate(obj);
3588
Chris Wilson05394f32010-11-08 19:18:58 +00003589 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003590
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003591out:
Chris Wilson05394f32010-11-08 19:18:58 +00003592 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003593unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003594 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003595 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003596}
3597
Chris Wilson05394f32010-11-08 19:18:58 +00003598struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3599 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003600{
Chris Wilson73aa8082010-09-30 11:46:12 +01003601 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003602 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003603 struct address_space *mapping;
Daniel Vetterc397b902010-04-09 19:05:07 +00003604
3605 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3606 if (obj == NULL)
3607 return NULL;
3608
3609 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3610 kfree(obj);
3611 return NULL;
3612 }
3613
Hugh Dickins5949eac2011-06-27 16:18:18 -07003614 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3615 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3616
Chris Wilson73aa8082010-09-30 11:46:12 +01003617 i915_gem_info_add_obj(dev_priv, size);
3618
Daniel Vetterc397b902010-04-09 19:05:07 +00003619 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3620 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3621
Eric Anholta1871112011-03-29 16:59:55 -07003622 if (IS_GEN6(dev)) {
3623 /* On Gen6, we can have the GPU use the LLC (the CPU
3624 * cache) for about a 10% performance improvement
3625 * compared to uncached. Graphics requests other than
3626 * display scanout are coherent with the CPU in
3627 * accessing this cache. This means in this mode we
3628 * don't need to clflush on the CPU side, and on the
3629 * GPU side we only need to flush internal caches to
3630 * get data visible to the CPU.
3631 *
3632 * However, we maintain the display planes as UC, and so
3633 * need to rebind when first used as such.
3634 */
3635 obj->cache_level = I915_CACHE_LLC;
3636 } else
3637 obj->cache_level = I915_CACHE_NONE;
3638
Daniel Vetter62b8b212010-04-09 19:05:08 +00003639 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003640 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003641 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003642 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003643 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003644 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003645 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003646 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003647 /* Avoid an unnecessary call to unbind on the first bind. */
3648 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003649
Chris Wilson05394f32010-11-08 19:18:58 +00003650 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003651}
3652
Eric Anholt673a3942008-07-30 12:06:12 -07003653int i915_gem_init_object(struct drm_gem_object *obj)
3654{
Daniel Vetterc397b902010-04-09 19:05:07 +00003655 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003656
Eric Anholt673a3942008-07-30 12:06:12 -07003657 return 0;
3658}
3659
Chris Wilson05394f32010-11-08 19:18:58 +00003660static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003661{
Chris Wilson05394f32010-11-08 19:18:58 +00003662 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003663 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003664 int ret;
3665
3666 ret = i915_gem_object_unbind(obj);
3667 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003668 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003669 &dev_priv->mm.deferred_free_list);
3670 return;
3671 }
3672
Chris Wilson26e12f892011-03-20 11:20:19 +00003673 trace_i915_gem_object_destroy(obj);
3674
Chris Wilson05394f32010-11-08 19:18:58 +00003675 if (obj->base.map_list.map)
Rob Clarkb464e9a2011-08-10 08:09:08 -05003676 drm_gem_free_mmap_offset(&obj->base);
Chris Wilsonbe726152010-07-23 23:18:50 +01003677
Chris Wilson05394f32010-11-08 19:18:58 +00003678 drm_gem_object_release(&obj->base);
3679 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003680
Chris Wilson05394f32010-11-08 19:18:58 +00003681 kfree(obj->page_cpu_valid);
3682 kfree(obj->bit_17);
3683 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003684}
3685
Chris Wilson05394f32010-11-08 19:18:58 +00003686void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003687{
Chris Wilson05394f32010-11-08 19:18:58 +00003688 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3689 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003690
Chris Wilson05394f32010-11-08 19:18:58 +00003691 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003692 i915_gem_object_unpin(obj);
3693
Chris Wilson05394f32010-11-08 19:18:58 +00003694 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003695 i915_gem_detach_phys_object(dev, obj);
3696
Chris Wilsonbe726152010-07-23 23:18:50 +01003697 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003698}
3699
Jesse Barnes5669fca2009-02-17 15:13:31 -08003700int
Eric Anholt673a3942008-07-30 12:06:12 -07003701i915_gem_idle(struct drm_device *dev)
3702{
3703 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003704 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003705
Keith Packard6dbe2772008-10-14 21:41:13 -07003706 mutex_lock(&dev->struct_mutex);
3707
Chris Wilson87acb0a2010-10-19 10:13:00 +01003708 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003709 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003710 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003711 }
Eric Anholt673a3942008-07-30 12:06:12 -07003712
Chris Wilson29105cc2010-01-07 10:39:13 +00003713 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003714 if (ret) {
3715 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003716 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003717 }
Eric Anholt673a3942008-07-30 12:06:12 -07003718
Chris Wilson29105cc2010-01-07 10:39:13 +00003719 /* Under UMS, be paranoid and evict. */
3720 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003721 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003722 if (ret) {
3723 mutex_unlock(&dev->struct_mutex);
3724 return ret;
3725 }
3726 }
3727
Chris Wilson312817a2010-11-22 11:50:11 +00003728 i915_gem_reset_fences(dev);
3729
Chris Wilson29105cc2010-01-07 10:39:13 +00003730 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3731 * We need to replace this with a semaphore, or something.
3732 * And not confound mm.suspended!
3733 */
3734 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003735 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003736
3737 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003738 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003739
Keith Packard6dbe2772008-10-14 21:41:13 -07003740 mutex_unlock(&dev->struct_mutex);
3741
Chris Wilson29105cc2010-01-07 10:39:13 +00003742 /* Cancel the retire work handler, which should be idle now. */
3743 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3744
Eric Anholt673a3942008-07-30 12:06:12 -07003745 return 0;
3746}
3747
Eric Anholt673a3942008-07-30 12:06:12 -07003748int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003749i915_gem_init_ringbuffer(struct drm_device *dev)
3750{
3751 drm_i915_private_t *dev_priv = dev->dev_private;
3752 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003753
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003754 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003755 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003756 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003757
3758 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003759 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003760 if (ret)
3761 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003762 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003763
Chris Wilson549f7362010-10-19 11:19:32 +01003764 if (HAS_BLT(dev)) {
3765 ret = intel_init_blt_ring_buffer(dev);
3766 if (ret)
3767 goto cleanup_bsd_ring;
3768 }
3769
Chris Wilson6f392d5482010-08-07 11:01:22 +01003770 dev_priv->next_seqno = 1;
3771
Chris Wilson68f95ba2010-05-27 13:18:22 +01003772 return 0;
3773
Chris Wilson549f7362010-10-19 11:19:32 +01003774cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003775 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003776cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003777 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003778 return ret;
3779}
3780
3781void
3782i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3783{
3784 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003785 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003786
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003787 for (i = 0; i < I915_NUM_RINGS; i++)
3788 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003789}
3790
3791int
Eric Anholt673a3942008-07-30 12:06:12 -07003792i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3793 struct drm_file *file_priv)
3794{
3795 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003796 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003797
Jesse Barnes79e53942008-11-07 14:24:08 -08003798 if (drm_core_check_feature(dev, DRIVER_MODESET))
3799 return 0;
3800
Ben Gamariba1234d2009-09-14 17:48:47 -04003801 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003802 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003803 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003804 }
3805
Eric Anholt673a3942008-07-30 12:06:12 -07003806 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003807 dev_priv->mm.suspended = 0;
3808
3809 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003810 if (ret != 0) {
3811 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003812 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003813 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003814
Chris Wilson69dc4982010-10-19 10:36:51 +01003815 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003816 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3817 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003818 for (i = 0; i < I915_NUM_RINGS; i++) {
3819 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3820 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3821 }
Eric Anholt673a3942008-07-30 12:06:12 -07003822 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003823
Chris Wilson5f353082010-06-07 14:03:03 +01003824 ret = drm_irq_install(dev);
3825 if (ret)
3826 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003827
Eric Anholt673a3942008-07-30 12:06:12 -07003828 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003829
3830cleanup_ringbuffer:
3831 mutex_lock(&dev->struct_mutex);
3832 i915_gem_cleanup_ringbuffer(dev);
3833 dev_priv->mm.suspended = 1;
3834 mutex_unlock(&dev->struct_mutex);
3835
3836 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003837}
3838
3839int
3840i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3841 struct drm_file *file_priv)
3842{
Jesse Barnes79e53942008-11-07 14:24:08 -08003843 if (drm_core_check_feature(dev, DRIVER_MODESET))
3844 return 0;
3845
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003846 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003847 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003848}
3849
3850void
3851i915_gem_lastclose(struct drm_device *dev)
3852{
3853 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003854
Eric Anholte806b492009-01-22 09:56:58 -08003855 if (drm_core_check_feature(dev, DRIVER_MODESET))
3856 return;
3857
Keith Packard6dbe2772008-10-14 21:41:13 -07003858 ret = i915_gem_idle(dev);
3859 if (ret)
3860 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003861}
3862
Chris Wilson64193402010-10-24 12:38:05 +01003863static void
3864init_ring_lists(struct intel_ring_buffer *ring)
3865{
3866 INIT_LIST_HEAD(&ring->active_list);
3867 INIT_LIST_HEAD(&ring->request_list);
3868 INIT_LIST_HEAD(&ring->gpu_write_list);
3869}
3870
Eric Anholt673a3942008-07-30 12:06:12 -07003871void
3872i915_gem_load(struct drm_device *dev)
3873{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003874 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003875 drm_i915_private_t *dev_priv = dev->dev_private;
3876
Chris Wilson69dc4982010-10-19 10:36:51 +01003877 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003878 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3879 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003880 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003881 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003882 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003883 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003884 for (i = 0; i < I915_NUM_RINGS; i++)
3885 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003886 for (i = 0; i < 16; i++)
3887 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003888 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3889 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003890 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003891
Dave Airlie94400122010-07-20 13:15:31 +10003892 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3893 if (IS_GEN3(dev)) {
3894 u32 tmp = I915_READ(MI_ARB_STATE);
3895 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3896 /* arb state is a masked write, so set bit + bit in mask */
3897 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3898 I915_WRITE(MI_ARB_STATE, tmp);
3899 }
3900 }
3901
Chris Wilson72bfa192010-12-19 11:42:05 +00003902 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3903
Jesse Barnesde151cf2008-11-12 10:03:55 -08003904 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003905 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3906 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003907
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003908 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003909 dev_priv->num_fence_regs = 16;
3910 else
3911 dev_priv->num_fence_regs = 8;
3912
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003913 /* Initialize fence registers to zero */
Eric Anholt10ed13e2011-05-06 13:53:49 -07003914 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3915 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003916 }
Eric Anholt10ed13e2011-05-06 13:53:49 -07003917
Eric Anholt673a3942008-07-30 12:06:12 -07003918 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003919 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01003920
Chris Wilsonce453d82011-02-21 14:43:56 +00003921 dev_priv->mm.interruptible = true;
3922
Chris Wilson17250b72010-10-28 12:51:39 +01003923 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3924 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3925 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07003926}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003927
3928/*
3929 * Create a physically contiguous memory object for this object
3930 * e.g. for cursor + overlay regs
3931 */
Chris Wilson995b6762010-08-20 13:23:26 +01003932static int i915_gem_init_phys_object(struct drm_device *dev,
3933 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003934{
3935 drm_i915_private_t *dev_priv = dev->dev_private;
3936 struct drm_i915_gem_phys_object *phys_obj;
3937 int ret;
3938
3939 if (dev_priv->mm.phys_objs[id - 1] || !size)
3940 return 0;
3941
Eric Anholt9a298b22009-03-24 12:23:04 -07003942 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003943 if (!phys_obj)
3944 return -ENOMEM;
3945
3946 phys_obj->id = id;
3947
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003948 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003949 if (!phys_obj->handle) {
3950 ret = -ENOMEM;
3951 goto kfree_obj;
3952 }
3953#ifdef CONFIG_X86
3954 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3955#endif
3956
3957 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3958
3959 return 0;
3960kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07003961 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003962 return ret;
3963}
3964
Chris Wilson995b6762010-08-20 13:23:26 +01003965static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003966{
3967 drm_i915_private_t *dev_priv = dev->dev_private;
3968 struct drm_i915_gem_phys_object *phys_obj;
3969
3970 if (!dev_priv->mm.phys_objs[id - 1])
3971 return;
3972
3973 phys_obj = dev_priv->mm.phys_objs[id - 1];
3974 if (phys_obj->cur_obj) {
3975 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3976 }
3977
3978#ifdef CONFIG_X86
3979 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3980#endif
3981 drm_pci_free(dev, phys_obj->handle);
3982 kfree(phys_obj);
3983 dev_priv->mm.phys_objs[id - 1] = NULL;
3984}
3985
3986void i915_gem_free_all_phys_object(struct drm_device *dev)
3987{
3988 int i;
3989
Dave Airlie260883c2009-01-22 17:58:49 +10003990 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003991 i915_gem_free_phys_object(dev, i);
3992}
3993
3994void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003995 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003996{
Chris Wilson05394f32010-11-08 19:18:58 +00003997 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01003998 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003999 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004000 int page_count;
4001
Chris Wilson05394f32010-11-08 19:18:58 +00004002 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004003 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004004 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004005
Chris Wilson05394f32010-11-08 19:18:58 +00004006 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004007 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07004008 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004009 if (!IS_ERR(page)) {
4010 char *dst = kmap_atomic(page);
4011 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4012 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004013
Chris Wilsone5281cc2010-10-28 13:45:36 +01004014 drm_clflush_pages(&page, 1);
4015
4016 set_page_dirty(page);
4017 mark_page_accessed(page);
4018 page_cache_release(page);
4019 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004020 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004021 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004022
Chris Wilson05394f32010-11-08 19:18:58 +00004023 obj->phys_obj->cur_obj = NULL;
4024 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004025}
4026
4027int
4028i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004029 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004030 int id,
4031 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004032{
Chris Wilson05394f32010-11-08 19:18:58 +00004033 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004034 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004035 int ret = 0;
4036 int page_count;
4037 int i;
4038
4039 if (id > I915_MAX_PHYS_OBJECT)
4040 return -EINVAL;
4041
Chris Wilson05394f32010-11-08 19:18:58 +00004042 if (obj->phys_obj) {
4043 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004044 return 0;
4045 i915_gem_detach_phys_object(dev, obj);
4046 }
4047
Dave Airlie71acb5e2008-12-30 20:31:46 +10004048 /* create a new object */
4049 if (!dev_priv->mm.phys_objs[id - 1]) {
4050 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004051 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004052 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004053 DRM_ERROR("failed to init phys object %d size: %zu\n",
4054 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004055 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004056 }
4057 }
4058
4059 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004060 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4061 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004062
Chris Wilson05394f32010-11-08 19:18:58 +00004063 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004064
4065 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004066 struct page *page;
4067 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004068
Hugh Dickins5949eac2011-06-27 16:18:18 -07004069 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004070 if (IS_ERR(page))
4071 return PTR_ERR(page);
4072
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004073 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004074 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004075 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004076 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004077
4078 mark_page_accessed(page);
4079 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004080 }
4081
4082 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004083}
4084
4085static int
Chris Wilson05394f32010-11-08 19:18:58 +00004086i915_gem_phys_pwrite(struct drm_device *dev,
4087 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004088 struct drm_i915_gem_pwrite *args,
4089 struct drm_file *file_priv)
4090{
Chris Wilson05394f32010-11-08 19:18:58 +00004091 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004092 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004093
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004094 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4095 unsigned long unwritten;
4096
4097 /* The physical object once assigned is fixed for the lifetime
4098 * of the obj, so we can safely drop the lock and continue
4099 * to access vaddr.
4100 */
4101 mutex_unlock(&dev->struct_mutex);
4102 unwritten = copy_from_user(vaddr, user_data, args->size);
4103 mutex_lock(&dev->struct_mutex);
4104 if (unwritten)
4105 return -EFAULT;
4106 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004107
Daniel Vetter40ce6572010-11-05 18:12:18 +01004108 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004109 return 0;
4110}
Eric Anholtb9624422009-06-03 07:27:35 +00004111
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004112void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004113{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004114 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004115
4116 /* Clean up our request list when the client is going away, so that
4117 * later retire_requests won't dereference our soon-to-be-gone
4118 * file_priv.
4119 */
Chris Wilson1c255952010-09-26 11:03:27 +01004120 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004121 while (!list_empty(&file_priv->mm.request_list)) {
4122 struct drm_i915_gem_request *request;
4123
4124 request = list_first_entry(&file_priv->mm.request_list,
4125 struct drm_i915_gem_request,
4126 client_list);
4127 list_del(&request->client_list);
4128 request->file_priv = NULL;
4129 }
Chris Wilson1c255952010-09-26 11:03:27 +01004130 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004131}
Chris Wilson31169712009-09-14 16:50:28 +01004132
Chris Wilson31169712009-09-14 16:50:28 +01004133static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004134i915_gpu_is_active(struct drm_device *dev)
4135{
4136 drm_i915_private_t *dev_priv = dev->dev_private;
4137 int lists_empty;
4138
Chris Wilson1637ef42010-04-20 17:10:35 +01004139 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004140 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004141
4142 return !lists_empty;
4143}
4144
4145static int
Ying Han1495f232011-05-24 17:12:27 -07004146i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004147{
Chris Wilson17250b72010-10-28 12:51:39 +01004148 struct drm_i915_private *dev_priv =
4149 container_of(shrinker,
4150 struct drm_i915_private,
4151 mm.inactive_shrinker);
4152 struct drm_device *dev = dev_priv->dev;
4153 struct drm_i915_gem_object *obj, *next;
Ying Han1495f232011-05-24 17:12:27 -07004154 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01004155 int cnt;
4156
4157 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004158 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004159
4160 /* "fast-path" to count number of available objects */
4161 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01004162 cnt = 0;
4163 list_for_each_entry(obj,
4164 &dev_priv->mm.inactive_list,
4165 mm_list)
4166 cnt++;
4167 mutex_unlock(&dev->struct_mutex);
4168 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004169 }
4170
Chris Wilson1637ef42010-04-20 17:10:35 +01004171rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004172 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01004173 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01004174
Chris Wilson17250b72010-10-28 12:51:39 +01004175 list_for_each_entry_safe(obj, next,
4176 &dev_priv->mm.inactive_list,
4177 mm_list) {
4178 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00004179 if (i915_gem_object_unbind(obj) == 0 &&
4180 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004181 break;
Chris Wilson31169712009-09-14 16:50:28 +01004182 }
Chris Wilson31169712009-09-14 16:50:28 +01004183 }
4184
4185 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01004186 cnt = 0;
4187 list_for_each_entry_safe(obj, next,
4188 &dev_priv->mm.inactive_list,
4189 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00004190 if (nr_to_scan &&
4191 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004192 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00004193 else
Chris Wilson17250b72010-10-28 12:51:39 +01004194 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01004195 }
4196
Chris Wilson17250b72010-10-28 12:51:39 +01004197 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01004198 /*
4199 * We are desperate for pages, so as a last resort, wait
4200 * for the GPU to finish and discard whatever we can.
4201 * This has a dramatic impact to reduce the number of
4202 * OOM-killer events whilst running the GPU aggressively.
4203 */
Chris Wilson17250b72010-10-28 12:51:39 +01004204 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01004205 goto rescan;
4206 }
Chris Wilson17250b72010-10-28 12:51:39 +01004207 mutex_unlock(&dev->struct_mutex);
4208 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004209}