blob: fb0538358c348535ee57c163874844dc76aad79f [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"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037
Chris Wilson88241782011-01-07 17:09:48 +000038static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson05394f32010-11-08 19:18:58 +000039static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
40static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000041static __must_check int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
42 bool write);
43static __must_check int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
44 uint64_t offset,
45 uint64_t size);
Chris Wilson05394f32010-11-08 19:18:58 +000046static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000047static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
48 unsigned alignment,
49 bool map_and_fenceable);
Chris Wilsond9e86c02010-11-10 16:40:20 +000050static void i915_gem_clear_fence_reg(struct drm_device *dev,
51 struct drm_i915_fence_reg *reg);
Chris Wilson05394f32010-11-08 19:18:58 +000052static int i915_gem_phys_pwrite(struct drm_device *dev,
53 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100054 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000055 struct drm_file *file);
56static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070057
Chris Wilson17250b72010-10-28 12:51:39 +010058static int i915_gem_inactive_shrink(struct shrinker *shrinker,
Ying Han1495f232011-05-24 17:12:27 -070059 struct shrink_control *sc);
Chris Wilson31169712009-09-14 16:50:28 +010060
Chris Wilson73aa8082010-09-30 11:46:12 +010061/* some bookkeeping */
62static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
63 size_t size)
64{
65 dev_priv->mm.object_count++;
66 dev_priv->mm.object_memory += size;
67}
68
69static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
70 size_t size)
71{
72 dev_priv->mm.object_count--;
73 dev_priv->mm.object_memory -= size;
74}
75
Chris Wilson21dd3732011-01-26 15:55:56 +000076static int
77i915_gem_wait_for_error(struct drm_device *dev)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010078{
79 struct drm_i915_private *dev_priv = dev->dev_private;
80 struct completion *x = &dev_priv->error_completion;
81 unsigned long flags;
82 int ret;
83
84 if (!atomic_read(&dev_priv->mm.wedged))
85 return 0;
86
87 ret = wait_for_completion_interruptible(x);
88 if (ret)
89 return ret;
90
Chris Wilson21dd3732011-01-26 15:55:56 +000091 if (atomic_read(&dev_priv->mm.wedged)) {
92 /* GPU is hung, bump the completion count to account for
93 * the token we just consumed so that we never hit zero and
94 * end up waiting upon a subsequent completion event that
95 * will never happen.
96 */
97 spin_lock_irqsave(&x->wait.lock, flags);
98 x->done++;
99 spin_unlock_irqrestore(&x->wait.lock, flags);
100 }
101 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100102}
103
Chris Wilson54cf91d2010-11-25 18:00:26 +0000104int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100105{
Chris Wilson76c1dec2010-09-25 11:22:51 +0100106 int ret;
107
Chris Wilson21dd3732011-01-26 15:55:56 +0000108 ret = i915_gem_wait_for_error(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100109 if (ret)
110 return ret;
111
112 ret = mutex_lock_interruptible(&dev->struct_mutex);
113 if (ret)
114 return ret;
115
Chris Wilson23bc5982010-09-29 16:10:57 +0100116 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100117 return 0;
118}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100119
Chris Wilson7d1c4802010-08-07 21:45:03 +0100120static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000121i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100122{
Chris Wilson05394f32010-11-08 19:18:58 +0000123 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100124}
125
Chris Wilson20217462010-11-23 15:26:33 +0000126void i915_gem_do_init(struct drm_device *dev,
127 unsigned long start,
128 unsigned long mappable_end,
129 unsigned long end)
Jesse Barnes79e53942008-11-07 14:24:08 -0800130{
131 drm_i915_private_t *dev_priv = dev->dev_private;
132
Chris Wilsonbee4a182011-01-21 10:54:32 +0000133 drm_mm_init(&dev_priv->mm.gtt_space, start, end - start);
Jesse Barnes79e53942008-11-07 14:24:08 -0800134
Chris Wilsonbee4a182011-01-21 10:54:32 +0000135 dev_priv->mm.gtt_start = start;
136 dev_priv->mm.gtt_mappable_end = mappable_end;
137 dev_priv->mm.gtt_end = end;
Chris Wilson73aa8082010-09-30 11:46:12 +0100138 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200139 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Chris Wilsonbee4a182011-01-21 10:54:32 +0000140
141 /* Take over this portion of the GTT */
142 intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
Jesse Barnes79e53942008-11-07 14:24:08 -0800143}
Keith Packard6dbe2772008-10-14 21:41:13 -0700144
Eric Anholt673a3942008-07-30 12:06:12 -0700145int
146i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000147 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700148{
Eric Anholt673a3942008-07-30 12:06:12 -0700149 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000150
151 if (args->gtt_start >= args->gtt_end ||
152 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
153 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700154
155 mutex_lock(&dev->struct_mutex);
Chris Wilson20217462010-11-23 15:26:33 +0000156 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700157 mutex_unlock(&dev->struct_mutex);
158
Chris Wilson20217462010-11-23 15:26:33 +0000159 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700160}
161
Eric Anholt5a125c32008-10-22 21:40:13 -0700162int
163i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000164 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700165{
Chris Wilson73aa8082010-09-30 11:46:12 +0100166 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700167 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000168 struct drm_i915_gem_object *obj;
169 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700170
171 if (!(dev->driver->driver_features & DRIVER_GEM))
172 return -ENODEV;
173
Chris Wilson6299f992010-11-24 12:23:44 +0000174 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100175 mutex_lock(&dev->struct_mutex);
Chris Wilson6299f992010-11-24 12:23:44 +0000176 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
177 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100178 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700179
Chris Wilson6299f992010-11-24 12:23:44 +0000180 args->aper_size = dev_priv->mm.gtt_total;
181 args->aper_available_size = args->aper_size -pinned;
182
Eric Anholt5a125c32008-10-22 21:40:13 -0700183 return 0;
184}
185
Dave Airlieff72145b2011-02-07 12:16:14 +1000186static int
187i915_gem_create(struct drm_file *file,
188 struct drm_device *dev,
189 uint64_t size,
190 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700191{
Chris Wilson05394f32010-11-08 19:18:58 +0000192 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300193 int ret;
194 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700195
Dave Airlieff72145b2011-02-07 12:16:14 +1000196 size = roundup(size, PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -0700197
198 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000199 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700200 if (obj == NULL)
201 return -ENOMEM;
202
Chris Wilson05394f32010-11-08 19:18:58 +0000203 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100204 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000205 drm_gem_object_release(&obj->base);
206 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100207 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700208 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100209 }
210
Chris Wilson202f2fe2010-10-14 13:20:40 +0100211 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000212 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100213 trace_i915_gem_object_create(obj);
214
Dave Airlieff72145b2011-02-07 12:16:14 +1000215 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700216 return 0;
217}
218
Dave Airlieff72145b2011-02-07 12:16:14 +1000219int
220i915_gem_dumb_create(struct drm_file *file,
221 struct drm_device *dev,
222 struct drm_mode_create_dumb *args)
223{
224 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000225 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000226 args->size = args->pitch * args->height;
227 return i915_gem_create(file, dev,
228 args->size, &args->handle);
229}
230
231int i915_gem_dumb_destroy(struct drm_file *file,
232 struct drm_device *dev,
233 uint32_t handle)
234{
235 return drm_gem_handle_delete(file, handle);
236}
237
238/**
239 * Creates a new mm object and returns a handle to it.
240 */
241int
242i915_gem_create_ioctl(struct drm_device *dev, void *data,
243 struct drm_file *file)
244{
245 struct drm_i915_gem_create *args = data;
246 return i915_gem_create(file, dev,
247 args->size, &args->handle);
248}
249
Chris Wilson05394f32010-11-08 19:18:58 +0000250static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700251{
Chris Wilson05394f32010-11-08 19:18:58 +0000252 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700253
254 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000255 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700256}
257
Chris Wilson99a03df2010-05-27 14:15:34 +0100258static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700259slow_shmem_copy(struct page *dst_page,
260 int dst_offset,
261 struct page *src_page,
262 int src_offset,
263 int length)
264{
265 char *dst_vaddr, *src_vaddr;
266
Chris Wilson99a03df2010-05-27 14:15:34 +0100267 dst_vaddr = kmap(dst_page);
268 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700269
270 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
271
Chris Wilson99a03df2010-05-27 14:15:34 +0100272 kunmap(src_page);
273 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700274}
275
Chris Wilson99a03df2010-05-27 14:15:34 +0100276static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700277slow_shmem_bit17_copy(struct page *gpu_page,
278 int gpu_offset,
279 struct page *cpu_page,
280 int cpu_offset,
281 int length,
282 int is_read)
283{
284 char *gpu_vaddr, *cpu_vaddr;
285
286 /* Use the unswizzled path if this page isn't affected. */
287 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
288 if (is_read)
289 return slow_shmem_copy(cpu_page, cpu_offset,
290 gpu_page, gpu_offset, length);
291 else
292 return slow_shmem_copy(gpu_page, gpu_offset,
293 cpu_page, cpu_offset, length);
294 }
295
Chris Wilson99a03df2010-05-27 14:15:34 +0100296 gpu_vaddr = kmap(gpu_page);
297 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700298
299 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
300 * XORing with the other bits (A9 for Y, A9 and A10 for X)
301 */
302 while (length > 0) {
303 int cacheline_end = ALIGN(gpu_offset + 1, 64);
304 int this_length = min(cacheline_end - gpu_offset, length);
305 int swizzled_gpu_offset = gpu_offset ^ 64;
306
307 if (is_read) {
308 memcpy(cpu_vaddr + cpu_offset,
309 gpu_vaddr + swizzled_gpu_offset,
310 this_length);
311 } else {
312 memcpy(gpu_vaddr + swizzled_gpu_offset,
313 cpu_vaddr + cpu_offset,
314 this_length);
315 }
316 cpu_offset += this_length;
317 gpu_offset += this_length;
318 length -= this_length;
319 }
320
Chris Wilson99a03df2010-05-27 14:15:34 +0100321 kunmap(cpu_page);
322 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700323}
324
Eric Anholt673a3942008-07-30 12:06:12 -0700325/**
Eric Anholteb014592009-03-10 11:44:52 -0700326 * This is the fast shmem pread path, which attempts to copy_from_user directly
327 * from the backing pages of the object to the user's address space. On a
328 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
329 */
330static int
Chris Wilson05394f32010-11-08 19:18:58 +0000331i915_gem_shmem_pread_fast(struct drm_device *dev,
332 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700333 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000334 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700335{
Chris Wilson05394f32010-11-08 19:18:58 +0000336 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700337 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100338 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700339 char __user *user_data;
340 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700341
342 user_data = (char __user *) (uintptr_t) args->data_ptr;
343 remain = args->size;
344
Eric Anholteb014592009-03-10 11:44:52 -0700345 offset = args->offset;
346
347 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100348 struct page *page;
349 char *vaddr;
350 int ret;
351
Eric Anholteb014592009-03-10 11:44:52 -0700352 /* Operation in this page
353 *
Eric Anholteb014592009-03-10 11:44:52 -0700354 * page_offset = offset within page
355 * page_length = bytes to copy for this page
356 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100357 page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700358 page_length = remain;
359 if ((page_offset + remain) > PAGE_SIZE)
360 page_length = PAGE_SIZE - page_offset;
361
Chris Wilsone5281cc2010-10-28 13:45:36 +0100362 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
363 GFP_HIGHUSER | __GFP_RECLAIMABLE);
364 if (IS_ERR(page))
365 return PTR_ERR(page);
366
367 vaddr = kmap_atomic(page);
368 ret = __copy_to_user_inatomic(user_data,
369 vaddr + page_offset,
370 page_length);
371 kunmap_atomic(vaddr);
372
373 mark_page_accessed(page);
374 page_cache_release(page);
375 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100376 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700377
378 remain -= page_length;
379 user_data += page_length;
380 offset += page_length;
381 }
382
Chris Wilson4f27b752010-10-14 15:26:45 +0100383 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700384}
385
386/**
387 * This is the fallback shmem pread path, which allocates temporary storage
388 * in kernel space to copy_to_user into outside of the struct_mutex, so we
389 * can copy out of the object's backing pages while holding the struct mutex
390 * and not take page faults.
391 */
392static int
Chris Wilson05394f32010-11-08 19:18:58 +0000393i915_gem_shmem_pread_slow(struct drm_device *dev,
394 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700395 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000396 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700397{
Chris Wilson05394f32010-11-08 19:18:58 +0000398 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700399 struct mm_struct *mm = current->mm;
400 struct page **user_pages;
401 ssize_t remain;
402 loff_t offset, pinned_pages, i;
403 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100404 int shmem_page_offset;
405 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700406 int page_length;
407 int ret;
408 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700409 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700410
411 remain = args->size;
412
413 /* Pin the user pages containing the data. We can't fault while
414 * holding the struct mutex, yet we want to hold it while
415 * dereferencing the user data.
416 */
417 first_data_page = data_ptr / PAGE_SIZE;
418 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
419 num_pages = last_data_page - first_data_page + 1;
420
Chris Wilson4f27b752010-10-14 15:26:45 +0100421 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700422 if (user_pages == NULL)
423 return -ENOMEM;
424
Chris Wilson4f27b752010-10-14 15:26:45 +0100425 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700426 down_read(&mm->mmap_sem);
427 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700428 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700429 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100430 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700431 if (pinned_pages < num_pages) {
432 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100433 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700434 }
435
Chris Wilson4f27b752010-10-14 15:26:45 +0100436 ret = i915_gem_object_set_cpu_read_domain_range(obj,
437 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700438 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100439 if (ret)
440 goto out;
441
442 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700443
Eric Anholteb014592009-03-10 11:44:52 -0700444 offset = args->offset;
445
446 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100447 struct page *page;
448
Eric Anholteb014592009-03-10 11:44:52 -0700449 /* Operation in this page
450 *
Eric Anholteb014592009-03-10 11:44:52 -0700451 * shmem_page_offset = offset within page in shmem file
452 * data_page_index = page number in get_user_pages return
453 * data_page_offset = offset with data_page_index page.
454 * page_length = bytes to copy for this page
455 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100456 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700457 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100458 data_page_offset = offset_in_page(data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700459
460 page_length = remain;
461 if ((shmem_page_offset + page_length) > PAGE_SIZE)
462 page_length = PAGE_SIZE - shmem_page_offset;
463 if ((data_page_offset + page_length) > PAGE_SIZE)
464 page_length = PAGE_SIZE - data_page_offset;
465
Chris Wilsone5281cc2010-10-28 13:45:36 +0100466 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
467 GFP_HIGHUSER | __GFP_RECLAIMABLE);
468 if (IS_ERR(page))
469 return PTR_ERR(page);
470
Eric Anholt280b7132009-03-12 16:56:27 -0700471 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100472 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700473 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100474 user_pages[data_page_index],
475 data_page_offset,
476 page_length,
477 1);
478 } else {
479 slow_shmem_copy(user_pages[data_page_index],
480 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100481 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100482 shmem_page_offset,
483 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700484 }
Eric Anholteb014592009-03-10 11:44:52 -0700485
Chris Wilsone5281cc2010-10-28 13:45:36 +0100486 mark_page_accessed(page);
487 page_cache_release(page);
488
Eric Anholteb014592009-03-10 11:44:52 -0700489 remain -= page_length;
490 data_ptr += page_length;
491 offset += page_length;
492 }
493
Chris Wilson4f27b752010-10-14 15:26:45 +0100494out:
Eric Anholteb014592009-03-10 11:44:52 -0700495 for (i = 0; i < pinned_pages; i++) {
496 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100497 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700498 page_cache_release(user_pages[i]);
499 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700500 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700501
502 return ret;
503}
504
Eric Anholt673a3942008-07-30 12:06:12 -0700505/**
506 * Reads data from the object referenced by handle.
507 *
508 * On error, the contents of *data are undefined.
509 */
510int
511i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000512 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700513{
514 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000515 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100516 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700517
Chris Wilson51311d02010-11-17 09:10:42 +0000518 if (args->size == 0)
519 return 0;
520
521 if (!access_ok(VERIFY_WRITE,
522 (char __user *)(uintptr_t)args->data_ptr,
523 args->size))
524 return -EFAULT;
525
526 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
527 args->size);
528 if (ret)
529 return -EFAULT;
530
Chris Wilson4f27b752010-10-14 15:26:45 +0100531 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100532 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100533 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700534
Chris Wilson05394f32010-11-08 19:18:58 +0000535 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000536 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100537 ret = -ENOENT;
538 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100539 }
Eric Anholt673a3942008-07-30 12:06:12 -0700540
Chris Wilson7dcd2492010-09-26 20:21:44 +0100541 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000542 if (args->offset > obj->base.size ||
543 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100544 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100545 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100546 }
547
Chris Wilsondb53a302011-02-03 11:57:46 +0000548 trace_i915_gem_object_pread(obj, args->offset, args->size);
549
Chris Wilson4f27b752010-10-14 15:26:45 +0100550 ret = i915_gem_object_set_cpu_read_domain_range(obj,
551 args->offset,
552 args->size);
553 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100554 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100555
556 ret = -EFAULT;
557 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000558 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100559 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000560 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700561
Chris Wilson35b62a82010-09-26 20:23:38 +0100562out:
Chris Wilson05394f32010-11-08 19:18:58 +0000563 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100564unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100565 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700566 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700567}
568
Keith Packard0839ccb2008-10-30 19:38:48 -0700569/* This is the fast write path which cannot handle
570 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700571 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700572
Keith Packard0839ccb2008-10-30 19:38:48 -0700573static inline int
574fast_user_write(struct io_mapping *mapping,
575 loff_t page_base, int page_offset,
576 char __user *user_data,
577 int length)
578{
579 char *vaddr_atomic;
580 unsigned long unwritten;
581
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700582 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700583 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
584 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700585 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100586 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700587}
588
589/* Here's the write path which can sleep for
590 * page faults
591 */
592
Chris Wilsonab34c222010-05-27 14:15:35 +0100593static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700594slow_kernel_write(struct io_mapping *mapping,
595 loff_t gtt_base, int gtt_offset,
596 struct page *user_page, int user_offset,
597 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700598{
Chris Wilsonab34c222010-05-27 14:15:35 +0100599 char __iomem *dst_vaddr;
600 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700601
Chris Wilsonab34c222010-05-27 14:15:35 +0100602 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
603 src_vaddr = kmap(user_page);
604
605 memcpy_toio(dst_vaddr + gtt_offset,
606 src_vaddr + user_offset,
607 length);
608
609 kunmap(user_page);
610 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700611}
612
Eric Anholt3de09aa2009-03-09 09:42:23 -0700613/**
614 * This is the fast pwrite path, where we copy the data directly from the
615 * user into the GTT, uncached.
616 */
Eric Anholt673a3942008-07-30 12:06:12 -0700617static int
Chris Wilson05394f32010-11-08 19:18:58 +0000618i915_gem_gtt_pwrite_fast(struct drm_device *dev,
619 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700620 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000621 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700622{
Keith Packard0839ccb2008-10-30 19:38:48 -0700623 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700624 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700625 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700626 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700627 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700628
629 user_data = (char __user *) (uintptr_t) args->data_ptr;
630 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700631
Chris Wilson05394f32010-11-08 19:18:58 +0000632 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700633
634 while (remain > 0) {
635 /* Operation in this page
636 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700637 * page_base = page offset within aperture
638 * page_offset = offset within page
639 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700640 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100641 page_base = offset & PAGE_MASK;
642 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700643 page_length = remain;
644 if ((page_offset + remain) > PAGE_SIZE)
645 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700646
Keith Packard0839ccb2008-10-30 19:38:48 -0700647 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700648 * source page isn't available. Return the error and we'll
649 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700650 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100651 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
652 page_offset, user_data, page_length))
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100653 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700654
Keith Packard0839ccb2008-10-30 19:38:48 -0700655 remain -= page_length;
656 user_data += page_length;
657 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700658 }
Eric Anholt673a3942008-07-30 12:06:12 -0700659
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100660 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700661}
662
Eric Anholt3de09aa2009-03-09 09:42:23 -0700663/**
664 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
665 * the memory and maps it using kmap_atomic for copying.
666 *
667 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
668 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
669 */
Eric Anholt3043c602008-10-02 12:24:47 -0700670static int
Chris Wilson05394f32010-11-08 19:18:58 +0000671i915_gem_gtt_pwrite_slow(struct drm_device *dev,
672 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700673 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000674 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700675{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700676 drm_i915_private_t *dev_priv = dev->dev_private;
677 ssize_t remain;
678 loff_t gtt_page_base, offset;
679 loff_t first_data_page, last_data_page, num_pages;
680 loff_t pinned_pages, i;
681 struct page **user_pages;
682 struct mm_struct *mm = current->mm;
683 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700684 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700685 uint64_t data_ptr = args->data_ptr;
686
687 remain = args->size;
688
689 /* Pin the user pages containing the data. We can't fault while
690 * holding the struct mutex, and all of the pwrite implementations
691 * want to hold it while dereferencing the user data.
692 */
693 first_data_page = data_ptr / PAGE_SIZE;
694 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
695 num_pages = last_data_page - first_data_page + 1;
696
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100697 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700698 if (user_pages == NULL)
699 return -ENOMEM;
700
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100701 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700702 down_read(&mm->mmap_sem);
703 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
704 num_pages, 0, 0, user_pages, NULL);
705 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100706 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700707 if (pinned_pages < num_pages) {
708 ret = -EFAULT;
709 goto out_unpin_pages;
710 }
711
Chris Wilsond9e86c02010-11-10 16:40:20 +0000712 ret = i915_gem_object_set_to_gtt_domain(obj, true);
713 if (ret)
714 goto out_unpin_pages;
715
716 ret = i915_gem_object_put_fence(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700717 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100718 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700719
Chris Wilson05394f32010-11-08 19:18:58 +0000720 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700721
722 while (remain > 0) {
723 /* Operation in this page
724 *
725 * gtt_page_base = page offset within aperture
726 * gtt_page_offset = offset within page in aperture
727 * data_page_index = page number in get_user_pages return
728 * data_page_offset = offset with data_page_index page.
729 * page_length = bytes to copy for this page
730 */
731 gtt_page_base = offset & PAGE_MASK;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100732 gtt_page_offset = offset_in_page(offset);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700733 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100734 data_page_offset = offset_in_page(data_ptr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700735
736 page_length = remain;
737 if ((gtt_page_offset + page_length) > PAGE_SIZE)
738 page_length = PAGE_SIZE - gtt_page_offset;
739 if ((data_page_offset + page_length) > PAGE_SIZE)
740 page_length = PAGE_SIZE - data_page_offset;
741
Chris Wilsonab34c222010-05-27 14:15:35 +0100742 slow_kernel_write(dev_priv->mm.gtt_mapping,
743 gtt_page_base, gtt_page_offset,
744 user_pages[data_page_index],
745 data_page_offset,
746 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700747
748 remain -= page_length;
749 offset += page_length;
750 data_ptr += page_length;
751 }
752
Eric Anholt3de09aa2009-03-09 09:42:23 -0700753out_unpin_pages:
754 for (i = 0; i < pinned_pages; i++)
755 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700756 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700757
758 return ret;
759}
760
Eric Anholt40123c12009-03-09 13:42:30 -0700761/**
762 * This is the fast shmem pwrite path, which attempts to directly
763 * copy_from_user into the kmapped pages backing the object.
764 */
Eric Anholt673a3942008-07-30 12:06:12 -0700765static int
Chris Wilson05394f32010-11-08 19:18:58 +0000766i915_gem_shmem_pwrite_fast(struct drm_device *dev,
767 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700768 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000769 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700770{
Chris Wilson05394f32010-11-08 19:18:58 +0000771 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700772 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100773 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700774 char __user *user_data;
775 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700776
777 user_data = (char __user *) (uintptr_t) args->data_ptr;
778 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700779
Eric Anholt673a3942008-07-30 12:06:12 -0700780 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000781 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700782
Eric Anholt40123c12009-03-09 13:42:30 -0700783 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100784 struct page *page;
785 char *vaddr;
786 int ret;
787
Eric Anholt40123c12009-03-09 13:42:30 -0700788 /* Operation in this page
789 *
Eric Anholt40123c12009-03-09 13:42:30 -0700790 * page_offset = offset within page
791 * page_length = bytes to copy for this page
792 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100793 page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700794 page_length = remain;
795 if ((page_offset + remain) > PAGE_SIZE)
796 page_length = PAGE_SIZE - page_offset;
797
Chris Wilsone5281cc2010-10-28 13:45:36 +0100798 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
799 GFP_HIGHUSER | __GFP_RECLAIMABLE);
800 if (IS_ERR(page))
801 return PTR_ERR(page);
802
803 vaddr = kmap_atomic(page, KM_USER0);
804 ret = __copy_from_user_inatomic(vaddr + page_offset,
805 user_data,
806 page_length);
807 kunmap_atomic(vaddr, KM_USER0);
808
809 set_page_dirty(page);
810 mark_page_accessed(page);
811 page_cache_release(page);
812
813 /* If we get a fault while copying data, then (presumably) our
814 * source page isn't available. Return the error and we'll
815 * retry in the slow path.
816 */
817 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100818 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700819
820 remain -= page_length;
821 user_data += page_length;
822 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700823 }
824
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100825 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700826}
827
828/**
829 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
830 * the memory and maps it using kmap_atomic for copying.
831 *
832 * This avoids taking mmap_sem for faulting on the user's address while the
833 * struct_mutex is held.
834 */
835static int
Chris Wilson05394f32010-11-08 19:18:58 +0000836i915_gem_shmem_pwrite_slow(struct drm_device *dev,
837 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700838 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000839 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700840{
Chris Wilson05394f32010-11-08 19:18:58 +0000841 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700842 struct mm_struct *mm = current->mm;
843 struct page **user_pages;
844 ssize_t remain;
845 loff_t offset, pinned_pages, i;
846 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100847 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700848 int data_page_index, data_page_offset;
849 int page_length;
850 int ret;
851 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700852 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700853
854 remain = args->size;
855
856 /* Pin the user pages containing the data. We can't fault while
857 * holding the struct mutex, and all of the pwrite implementations
858 * want to hold it while dereferencing the user data.
859 */
860 first_data_page = data_ptr / PAGE_SIZE;
861 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
862 num_pages = last_data_page - first_data_page + 1;
863
Chris Wilson4f27b752010-10-14 15:26:45 +0100864 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700865 if (user_pages == NULL)
866 return -ENOMEM;
867
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100868 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700869 down_read(&mm->mmap_sem);
870 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
871 num_pages, 0, 0, user_pages, NULL);
872 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100873 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700874 if (pinned_pages < num_pages) {
875 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100876 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700877 }
878
Eric Anholt40123c12009-03-09 13:42:30 -0700879 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100880 if (ret)
881 goto out;
882
883 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700884
Eric Anholt40123c12009-03-09 13:42:30 -0700885 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000886 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700887
888 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100889 struct page *page;
890
Eric Anholt40123c12009-03-09 13:42:30 -0700891 /* Operation in this page
892 *
Eric Anholt40123c12009-03-09 13:42:30 -0700893 * shmem_page_offset = offset within page in shmem file
894 * data_page_index = page number in get_user_pages return
895 * data_page_offset = offset with data_page_index page.
896 * page_length = bytes to copy for this page
897 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100898 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700899 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100900 data_page_offset = offset_in_page(data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700901
902 page_length = remain;
903 if ((shmem_page_offset + page_length) > PAGE_SIZE)
904 page_length = PAGE_SIZE - shmem_page_offset;
905 if ((data_page_offset + page_length) > PAGE_SIZE)
906 page_length = PAGE_SIZE - data_page_offset;
907
Chris Wilsone5281cc2010-10-28 13:45:36 +0100908 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
909 GFP_HIGHUSER | __GFP_RECLAIMABLE);
910 if (IS_ERR(page)) {
911 ret = PTR_ERR(page);
912 goto out;
913 }
914
Eric Anholt280b7132009-03-12 16:56:27 -0700915 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100916 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700917 shmem_page_offset,
918 user_pages[data_page_index],
919 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100920 page_length,
921 0);
922 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100923 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100924 shmem_page_offset,
925 user_pages[data_page_index],
926 data_page_offset,
927 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700928 }
Eric Anholt40123c12009-03-09 13:42:30 -0700929
Chris Wilsone5281cc2010-10-28 13:45:36 +0100930 set_page_dirty(page);
931 mark_page_accessed(page);
932 page_cache_release(page);
933
Eric Anholt40123c12009-03-09 13:42:30 -0700934 remain -= page_length;
935 data_ptr += page_length;
936 offset += page_length;
937 }
938
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100939out:
Eric Anholt40123c12009-03-09 13:42:30 -0700940 for (i = 0; i < pinned_pages; i++)
941 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700942 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700943
944 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700945}
946
947/**
948 * Writes data to the object referenced by handle.
949 *
950 * On error, the contents of the buffer that were to be modified are undefined.
951 */
952int
953i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100954 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700955{
956 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000957 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000958 int ret;
959
960 if (args->size == 0)
961 return 0;
962
963 if (!access_ok(VERIFY_READ,
964 (char __user *)(uintptr_t)args->data_ptr,
965 args->size))
966 return -EFAULT;
967
968 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
969 args->size);
970 if (ret)
971 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700972
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100973 ret = i915_mutex_lock_interruptible(dev);
974 if (ret)
975 return ret;
976
Chris Wilson05394f32010-11-08 19:18:58 +0000977 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000978 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100979 ret = -ENOENT;
980 goto unlock;
981 }
Eric Anholt673a3942008-07-30 12:06:12 -0700982
Chris Wilson7dcd2492010-09-26 20:21:44 +0100983 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000984 if (args->offset > obj->base.size ||
985 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100986 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100987 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100988 }
989
Chris Wilsondb53a302011-02-03 11:57:46 +0000990 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
991
Eric Anholt673a3942008-07-30 12:06:12 -0700992 /* We can only do the GTT pwrite on untiled buffers, as otherwise
993 * it would end up going through the fenced access, and we'll get
994 * different detiling behavior between reading and writing.
995 * pread/pwrite currently are reading and writing from the CPU
996 * perspective, requiring manual detiling by the client.
997 */
Chris Wilson05394f32010-11-08 19:18:58 +0000998 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100999 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001000 else if (obj->gtt_space &&
Chris Wilson05394f32010-11-08 19:18:58 +00001001 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001002 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001003 if (ret)
1004 goto out;
1005
Chris Wilsond9e86c02010-11-10 16:40:20 +00001006 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1007 if (ret)
1008 goto out_unpin;
1009
1010 ret = i915_gem_object_put_fence(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001011 if (ret)
1012 goto out_unpin;
1013
1014 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1015 if (ret == -EFAULT)
1016 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1017
1018out_unpin:
1019 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001020 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001021 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1022 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001023 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001024
1025 ret = -EFAULT;
1026 if (!i915_gem_object_needs_bit17_swizzle(obj))
1027 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1028 if (ret == -EFAULT)
1029 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001030 }
Eric Anholt673a3942008-07-30 12:06:12 -07001031
Chris Wilson35b62a82010-09-26 20:23:38 +01001032out:
Chris Wilson05394f32010-11-08 19:18:58 +00001033 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001034unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001035 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001036 return ret;
1037}
1038
1039/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001040 * Called when user space prepares to use an object with the CPU, either
1041 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001042 */
1043int
1044i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001045 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001046{
1047 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001048 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001049 uint32_t read_domains = args->read_domains;
1050 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001051 int ret;
1052
1053 if (!(dev->driver->driver_features & DRIVER_GEM))
1054 return -ENODEV;
1055
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001056 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001057 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001058 return -EINVAL;
1059
Chris Wilson21d509e2009-06-06 09:46:02 +01001060 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001061 return -EINVAL;
1062
1063 /* Having something in the write domain implies it's in the read
1064 * domain, and only that read domain. Enforce that in the request.
1065 */
1066 if (write_domain != 0 && read_domains != write_domain)
1067 return -EINVAL;
1068
Chris Wilson76c1dec2010-09-25 11:22:51 +01001069 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001070 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001071 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001072
Chris Wilson05394f32010-11-08 19:18:58 +00001073 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001074 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001075 ret = -ENOENT;
1076 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001077 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001078
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001079 if (read_domains & I915_GEM_DOMAIN_GTT) {
1080 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001081
1082 /* Silently promote "you're not bound, there was nothing to do"
1083 * to success, since the client was just asking us to
1084 * make sure everything was done.
1085 */
1086 if (ret == -EINVAL)
1087 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001088 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001089 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001090 }
1091
Chris Wilson05394f32010-11-08 19:18:58 +00001092 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001093unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001094 mutex_unlock(&dev->struct_mutex);
1095 return ret;
1096}
1097
1098/**
1099 * Called when user space has done writes to this buffer
1100 */
1101int
1102i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001103 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001104{
1105 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001106 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001107 int ret = 0;
1108
1109 if (!(dev->driver->driver_features & DRIVER_GEM))
1110 return -ENODEV;
1111
Chris Wilson76c1dec2010-09-25 11:22:51 +01001112 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001113 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001114 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001115
Chris Wilson05394f32010-11-08 19:18:58 +00001116 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001117 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001118 ret = -ENOENT;
1119 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001120 }
1121
Eric Anholt673a3942008-07-30 12:06:12 -07001122 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001123 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001124 i915_gem_object_flush_cpu_write_domain(obj);
1125
Chris Wilson05394f32010-11-08 19:18:58 +00001126 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001127unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001128 mutex_unlock(&dev->struct_mutex);
1129 return ret;
1130}
1131
1132/**
1133 * Maps the contents of an object, returning the address it is mapped
1134 * into.
1135 *
1136 * While the mapping holds a reference on the contents of the object, it doesn't
1137 * imply a ref on the object itself.
1138 */
1139int
1140i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001141 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001142{
Chris Wilsonda761a62010-10-27 17:37:08 +01001143 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001144 struct drm_i915_gem_mmap *args = data;
1145 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001146 unsigned long addr;
1147
1148 if (!(dev->driver->driver_features & DRIVER_GEM))
1149 return -ENODEV;
1150
Chris Wilson05394f32010-11-08 19:18:58 +00001151 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001152 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001153 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001154
Chris Wilsonda761a62010-10-27 17:37:08 +01001155 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1156 drm_gem_object_unreference_unlocked(obj);
1157 return -E2BIG;
1158 }
1159
Eric Anholt673a3942008-07-30 12:06:12 -07001160 down_write(&current->mm->mmap_sem);
1161 addr = do_mmap(obj->filp, 0, args->size,
1162 PROT_READ | PROT_WRITE, MAP_SHARED,
1163 args->offset);
1164 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001165 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001166 if (IS_ERR((void *)addr))
1167 return addr;
1168
1169 args->addr_ptr = (uint64_t) addr;
1170
1171 return 0;
1172}
1173
Jesse Barnesde151cf2008-11-12 10:03:55 -08001174/**
1175 * i915_gem_fault - fault a page into the GTT
1176 * vma: VMA in question
1177 * vmf: fault info
1178 *
1179 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1180 * from userspace. The fault handler takes care of binding the object to
1181 * the GTT (if needed), allocating and programming a fence register (again,
1182 * only if needed based on whether the old reg is still valid or the object
1183 * is tiled) and inserting a new PTE into the faulting process.
1184 *
1185 * Note that the faulting process may involve evicting existing objects
1186 * from the GTT and/or fence registers to make room. So performance may
1187 * suffer if the GTT working set is large or there are few fence registers
1188 * left.
1189 */
1190int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1191{
Chris Wilson05394f32010-11-08 19:18:58 +00001192 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1193 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001194 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001195 pgoff_t page_offset;
1196 unsigned long pfn;
1197 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001198 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001199
1200 /* We don't use vmf->pgoff since that has the fake offset */
1201 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1202 PAGE_SHIFT;
1203
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001204 ret = i915_mutex_lock_interruptible(dev);
1205 if (ret)
1206 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001207
Chris Wilsondb53a302011-02-03 11:57:46 +00001208 trace_i915_gem_object_fault(obj, page_offset, true, write);
1209
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001210 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001211 if (!obj->map_and_fenceable) {
1212 ret = i915_gem_object_unbind(obj);
1213 if (ret)
1214 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001215 }
Chris Wilson05394f32010-11-08 19:18:58 +00001216 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001217 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001218 if (ret)
1219 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001220 }
1221
Chris Wilson4a684a42010-10-28 14:44:08 +01001222 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1223 if (ret)
1224 goto unlock;
1225
Chris Wilsond9e86c02010-11-10 16:40:20 +00001226 if (obj->tiling_mode == I915_TILING_NONE)
1227 ret = i915_gem_object_put_fence(obj);
1228 else
Chris Wilsonce453d82011-02-21 14:43:56 +00001229 ret = i915_gem_object_get_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001230 if (ret)
1231 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001232
Chris Wilson05394f32010-11-08 19:18:58 +00001233 if (i915_gem_object_is_inactive(obj))
1234 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001235
Chris Wilson6299f992010-11-24 12:23:44 +00001236 obj->fault_mappable = true;
1237
Chris Wilson05394f32010-11-08 19:18:58 +00001238 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001239 page_offset;
1240
1241 /* Finally, remap it using the new GTT offset */
1242 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001243unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001244 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001245out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001246 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001247 case -EIO:
Chris Wilson045e7692010-11-07 09:18:22 +00001248 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001249 /* Give the error handler a chance to run and move the
1250 * objects off the GPU active list. Next time we service the
1251 * fault, we should be able to transition the page into the
1252 * GTT without touching the GPU (and so avoid further
1253 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1254 * with coherency, just lost writes.
1255 */
Chris Wilson045e7692010-11-07 09:18:22 +00001256 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001257 case 0:
1258 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001259 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001260 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001261 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001262 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001264 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001265 }
1266}
1267
1268/**
1269 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1270 * @obj: obj in question
1271 *
1272 * GEM memory mapping works by handing back to userspace a fake mmap offset
1273 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1274 * up the object based on the offset and sets up the various memory mapping
1275 * structures.
1276 *
1277 * This routine allocates and attaches a fake offset for @obj.
1278 */
1279static int
Chris Wilson05394f32010-11-08 19:18:58 +00001280i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001281{
Chris Wilson05394f32010-11-08 19:18:58 +00001282 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001283 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001284 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001285 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001286 int ret = 0;
1287
1288 /* Set the object up for mmap'ing */
Chris Wilson05394f32010-11-08 19:18:58 +00001289 list = &obj->base.map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001290 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001291 if (!list->map)
1292 return -ENOMEM;
1293
1294 map = list->map;
1295 map->type = _DRM_GEM;
Chris Wilson05394f32010-11-08 19:18:58 +00001296 map->size = obj->base.size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001297 map->handle = obj;
1298
1299 /* Get a DRM GEM mmap offset allocated... */
1300 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
Chris Wilson05394f32010-11-08 19:18:58 +00001301 obj->base.size / PAGE_SIZE,
1302 0, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001303 if (!list->file_offset_node) {
Chris Wilson05394f32010-11-08 19:18:58 +00001304 DRM_ERROR("failed to allocate offset for bo %d\n",
1305 obj->base.name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001306 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001307 goto out_free_list;
1308 }
1309
1310 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
Chris Wilson05394f32010-11-08 19:18:58 +00001311 obj->base.size / PAGE_SIZE,
1312 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001313 if (!list->file_offset_node) {
1314 ret = -ENOMEM;
1315 goto out_free_list;
1316 }
1317
1318 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001319 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1320 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321 DRM_ERROR("failed to add to map hash\n");
1322 goto out_free_mm;
1323 }
1324
Jesse Barnesde151cf2008-11-12 10:03:55 -08001325 return 0;
1326
1327out_free_mm:
1328 drm_mm_put_block(list->file_offset_node);
1329out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001330 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001331 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001332
1333 return ret;
1334}
1335
Chris Wilson901782b2009-07-10 08:18:50 +01001336/**
1337 * i915_gem_release_mmap - remove physical page mappings
1338 * @obj: obj in question
1339 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001340 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001341 * relinquish ownership of the pages back to the system.
1342 *
1343 * It is vital that we remove the page mapping if we have mapped a tiled
1344 * object through the GTT and then lose the fence register due to
1345 * resource pressure. Similarly if the object has been moved out of the
1346 * aperture, than pages mapped into userspace must be revoked. Removing the
1347 * mapping will then trigger a page fault on the next user access, allowing
1348 * fixup by i915_gem_fault().
1349 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001350void
Chris Wilson05394f32010-11-08 19:18:58 +00001351i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001352{
Chris Wilson6299f992010-11-24 12:23:44 +00001353 if (!obj->fault_mappable)
1354 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001355
Chris Wilsonf6e47882011-03-20 21:09:12 +00001356 if (obj->base.dev->dev_mapping)
1357 unmap_mapping_range(obj->base.dev->dev_mapping,
1358 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1359 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001360
Chris Wilson6299f992010-11-24 12:23:44 +00001361 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001362}
1363
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001364static void
Chris Wilson05394f32010-11-08 19:18:58 +00001365i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001366{
Chris Wilson05394f32010-11-08 19:18:58 +00001367 struct drm_device *dev = obj->base.dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001368 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001369 struct drm_map_list *list = &obj->base.map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001370
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001371 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001372 drm_mm_put_block(list->file_offset_node);
1373 kfree(list->map);
1374 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001375}
1376
Chris Wilson92b88ae2010-11-09 11:47:32 +00001377static uint32_t
1378i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1379{
1380 struct drm_device *dev = obj->base.dev;
1381 uint32_t size;
1382
1383 if (INTEL_INFO(dev)->gen >= 4 ||
1384 obj->tiling_mode == I915_TILING_NONE)
1385 return obj->base.size;
1386
1387 /* Previous chips need a power-of-two fence region when tiling */
1388 if (INTEL_INFO(dev)->gen == 3)
1389 size = 1024*1024;
1390 else
1391 size = 512*1024;
1392
1393 while (size < obj->base.size)
1394 size <<= 1;
1395
1396 return size;
1397}
1398
Jesse Barnesde151cf2008-11-12 10:03:55 -08001399/**
1400 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1401 * @obj: object to check
1402 *
1403 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001404 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001405 */
1406static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001407i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001408{
Chris Wilson05394f32010-11-08 19:18:58 +00001409 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001410
1411 /*
1412 * Minimum alignment is 4k (GTT page size), but might be greater
1413 * if a fence register is needed for the object.
1414 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001415 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilson05394f32010-11-08 19:18:58 +00001416 obj->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001417 return 4096;
1418
1419 /*
1420 * Previous chips need to be aligned to the size of the smallest
1421 * fence register that can contain the object.
1422 */
Chris Wilson05394f32010-11-08 19:18:58 +00001423 return i915_gem_get_gtt_size(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001424}
1425
Daniel Vetter5e783302010-11-14 22:32:36 +01001426/**
1427 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1428 * unfenced object
1429 * @obj: object to check
1430 *
1431 * Return the required GTT alignment for an object, only taking into account
1432 * unfenced tiled surface requirements.
1433 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001434uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001435i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
Daniel Vetter5e783302010-11-14 22:32:36 +01001436{
Chris Wilson05394f32010-11-08 19:18:58 +00001437 struct drm_device *dev = obj->base.dev;
Daniel Vetter5e783302010-11-14 22:32:36 +01001438 int tile_height;
1439
1440 /*
1441 * Minimum alignment is 4k (GTT page size) for sane hw.
1442 */
1443 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001444 obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001445 return 4096;
1446
1447 /*
1448 * Older chips need unfenced tiled buffers to be aligned to the left
1449 * edge of an even tile row (where tile rows are counted as if the bo is
1450 * placed in a fenced gtt region).
1451 */
Daniel Vetterc8ebc2b2011-05-12 22:17:20 +01001452 if (IS_GEN2(dev))
1453 tile_height = 16;
1454 else if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Daniel Vetter5e783302010-11-14 22:32:36 +01001455 tile_height = 32;
1456 else
1457 tile_height = 8;
1458
Chris Wilson05394f32010-11-08 19:18:58 +00001459 return tile_height * obj->stride * 2;
Daniel Vetter5e783302010-11-14 22:32:36 +01001460}
1461
Jesse Barnesde151cf2008-11-12 10:03:55 -08001462int
Dave Airlieff72145b2011-02-07 12:16:14 +10001463i915_gem_mmap_gtt(struct drm_file *file,
1464 struct drm_device *dev,
1465 uint32_t handle,
1466 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001467{
Chris Wilsonda761a62010-10-27 17:37:08 +01001468 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001469 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001470 int ret;
1471
1472 if (!(dev->driver->driver_features & DRIVER_GEM))
1473 return -ENODEV;
1474
Chris Wilson76c1dec2010-09-25 11:22:51 +01001475 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001476 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001477 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001478
Dave Airlieff72145b2011-02-07 12:16:14 +10001479 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001480 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001481 ret = -ENOENT;
1482 goto unlock;
1483 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001484
Chris Wilson05394f32010-11-08 19:18:58 +00001485 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001486 ret = -E2BIG;
1487 goto unlock;
1488 }
1489
Chris Wilson05394f32010-11-08 19:18:58 +00001490 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001491 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001492 ret = -EINVAL;
1493 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001494 }
1495
Chris Wilson05394f32010-11-08 19:18:58 +00001496 if (!obj->base.map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001497 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001498 if (ret)
1499 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001500 }
1501
Dave Airlieff72145b2011-02-07 12:16:14 +10001502 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001503
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001504out:
Chris Wilson05394f32010-11-08 19:18:58 +00001505 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001506unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001507 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001508 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001509}
1510
Dave Airlieff72145b2011-02-07 12:16:14 +10001511/**
1512 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1513 * @dev: DRM device
1514 * @data: GTT mapping ioctl data
1515 * @file: GEM object info
1516 *
1517 * Simply returns the fake offset to userspace so it can mmap it.
1518 * The mmap call will end up in drm_gem_mmap(), which will set things
1519 * up so we can get faults in the handler above.
1520 *
1521 * The fault handler will take care of binding the object into the GTT
1522 * (since it may have been evicted to make room for something), allocating
1523 * a fence register, and mapping the appropriate aperture address into
1524 * userspace.
1525 */
1526int
1527i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1528 struct drm_file *file)
1529{
1530 struct drm_i915_gem_mmap_gtt *args = data;
1531
1532 if (!(dev->driver->driver_features & DRIVER_GEM))
1533 return -ENODEV;
1534
1535 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1536}
1537
1538
Chris Wilsone5281cc2010-10-28 13:45:36 +01001539static int
Chris Wilson05394f32010-11-08 19:18:58 +00001540i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001541 gfp_t gfpmask)
1542{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001543 int page_count, i;
1544 struct address_space *mapping;
1545 struct inode *inode;
1546 struct page *page;
1547
1548 /* Get the list of pages out of our struct file. They'll be pinned
1549 * at this point until we release them.
1550 */
Chris Wilson05394f32010-11-08 19:18:58 +00001551 page_count = obj->base.size / PAGE_SIZE;
1552 BUG_ON(obj->pages != NULL);
1553 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1554 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001555 return -ENOMEM;
1556
Chris Wilson05394f32010-11-08 19:18:58 +00001557 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001558 mapping = inode->i_mapping;
1559 for (i = 0; i < page_count; i++) {
1560 page = read_cache_page_gfp(mapping, i,
1561 GFP_HIGHUSER |
1562 __GFP_COLD |
1563 __GFP_RECLAIMABLE |
1564 gfpmask);
1565 if (IS_ERR(page))
1566 goto err_pages;
1567
Chris Wilson05394f32010-11-08 19:18:58 +00001568 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001569 }
1570
Chris Wilson05394f32010-11-08 19:18:58 +00001571 if (obj->tiling_mode != I915_TILING_NONE)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001572 i915_gem_object_do_bit_17_swizzle(obj);
1573
1574 return 0;
1575
1576err_pages:
1577 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001578 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001579
Chris Wilson05394f32010-11-08 19:18:58 +00001580 drm_free_large(obj->pages);
1581 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001582 return PTR_ERR(page);
1583}
1584
Chris Wilson5cdf5882010-09-27 15:51:07 +01001585static void
Chris Wilson05394f32010-11-08 19:18:58 +00001586i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001587{
Chris Wilson05394f32010-11-08 19:18:58 +00001588 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001589 int i;
1590
Chris Wilson05394f32010-11-08 19:18:58 +00001591 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001592
Chris Wilson05394f32010-11-08 19:18:58 +00001593 if (obj->tiling_mode != I915_TILING_NONE)
Eric Anholt280b7132009-03-12 16:56:27 -07001594 i915_gem_object_save_bit_17_swizzle(obj);
1595
Chris Wilson05394f32010-11-08 19:18:58 +00001596 if (obj->madv == I915_MADV_DONTNEED)
1597 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001598
1599 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001600 if (obj->dirty)
1601 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001602
Chris Wilson05394f32010-11-08 19:18:58 +00001603 if (obj->madv == I915_MADV_WILLNEED)
1604 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001605
Chris Wilson05394f32010-11-08 19:18:58 +00001606 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001607 }
Chris Wilson05394f32010-11-08 19:18:58 +00001608 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001609
Chris Wilson05394f32010-11-08 19:18:58 +00001610 drm_free_large(obj->pages);
1611 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001612}
1613
Chris Wilson54cf91d2010-11-25 18:00:26 +00001614void
Chris Wilson05394f32010-11-08 19:18:58 +00001615i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001616 struct intel_ring_buffer *ring,
1617 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001618{
Chris Wilson05394f32010-11-08 19:18:58 +00001619 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001620 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001621
Zou Nan hai852835f2010-05-21 09:08:56 +08001622 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001623 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001624
1625 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001626 if (!obj->active) {
1627 drm_gem_object_reference(&obj->base);
1628 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001629 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001630
Eric Anholt673a3942008-07-30 12:06:12 -07001631 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001632 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1633 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001634
Chris Wilson05394f32010-11-08 19:18:58 +00001635 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001636 if (obj->fenced_gpu_access) {
1637 struct drm_i915_fence_reg *reg;
1638
1639 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1640
1641 obj->last_fenced_seqno = seqno;
1642 obj->last_fenced_ring = ring;
1643
1644 reg = &dev_priv->fence_regs[obj->fence_reg];
1645 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1646 }
1647}
1648
1649static void
1650i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1651{
1652 list_del_init(&obj->ring_list);
1653 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001654}
1655
Eric Anholtce44b0e2008-11-06 16:00:31 -08001656static void
Chris Wilson05394f32010-11-08 19:18:58 +00001657i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001658{
Chris Wilson05394f32010-11-08 19:18:58 +00001659 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001660 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001661
Chris Wilson05394f32010-11-08 19:18:58 +00001662 BUG_ON(!obj->active);
1663 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001664
1665 i915_gem_object_move_off_active(obj);
1666}
1667
1668static void
1669i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1670{
1671 struct drm_device *dev = obj->base.dev;
1672 struct drm_i915_private *dev_priv = dev->dev_private;
1673
1674 if (obj->pin_count != 0)
1675 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1676 else
1677 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1678
1679 BUG_ON(!list_empty(&obj->gpu_write_list));
1680 BUG_ON(!obj->active);
1681 obj->ring = NULL;
1682
1683 i915_gem_object_move_off_active(obj);
1684 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001685
1686 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001687 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001688 drm_gem_object_unreference(&obj->base);
1689
1690 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001691}
Eric Anholt673a3942008-07-30 12:06:12 -07001692
Chris Wilson963b4832009-09-20 23:03:54 +01001693/* Immediately discard the backing storage */
1694static void
Chris Wilson05394f32010-11-08 19:18:58 +00001695i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001696{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001697 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001698
Chris Wilsonae9fed62010-08-07 11:01:30 +01001699 /* Our goal here is to return as much of the memory as
1700 * is possible back to the system as we are called from OOM.
1701 * To do this we must instruct the shmfs to drop all of its
1702 * backing pages, *now*. Here we mirror the actions taken
1703 * when by shmem_delete_inode() to release the backing store.
1704 */
Chris Wilson05394f32010-11-08 19:18:58 +00001705 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001706 truncate_inode_pages(inode->i_mapping, 0);
1707 if (inode->i_op->truncate_range)
1708 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001709
Chris Wilson05394f32010-11-08 19:18:58 +00001710 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001711}
1712
1713static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001714i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001715{
Chris Wilson05394f32010-11-08 19:18:58 +00001716 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001717}
1718
Eric Anholt673a3942008-07-30 12:06:12 -07001719static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001720i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1721 uint32_t flush_domains)
Daniel Vetter63560392010-02-19 11:51:59 +01001722{
Chris Wilson05394f32010-11-08 19:18:58 +00001723 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001724
Chris Wilson05394f32010-11-08 19:18:58 +00001725 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001726 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001727 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001728 if (obj->base.write_domain & flush_domains) {
1729 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001730
Chris Wilson05394f32010-11-08 19:18:58 +00001731 obj->base.write_domain = 0;
1732 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001733 i915_gem_object_move_to_active(obj, ring,
Chris Wilsondb53a302011-02-03 11:57:46 +00001734 i915_gem_next_request_seqno(ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001735
Daniel Vetter63560392010-02-19 11:51:59 +01001736 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001737 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001738 old_write_domain);
1739 }
1740 }
1741}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001742
Chris Wilson3cce4692010-10-27 16:11:02 +01001743int
Chris Wilsondb53a302011-02-03 11:57:46 +00001744i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001745 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001746 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001747{
Chris Wilsondb53a302011-02-03 11:57:46 +00001748 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001749 uint32_t seqno;
1750 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001751 int ret;
1752
1753 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001754
Chris Wilson3cce4692010-10-27 16:11:02 +01001755 ret = ring->add_request(ring, &seqno);
1756 if (ret)
1757 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001758
Chris Wilsondb53a302011-02-03 11:57:46 +00001759 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001760
1761 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001762 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001763 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001764 was_empty = list_empty(&ring->request_list);
1765 list_add_tail(&request->list, &ring->request_list);
1766
Chris Wilsondb53a302011-02-03 11:57:46 +00001767 if (file) {
1768 struct drm_i915_file_private *file_priv = file->driver_priv;
1769
Chris Wilson1c255952010-09-26 11:03:27 +01001770 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001771 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001772 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001773 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001774 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001775 }
Eric Anholt673a3942008-07-30 12:06:12 -07001776
Chris Wilsondb53a302011-02-03 11:57:46 +00001777 ring->outstanding_lazy_request = false;
1778
Ben Gamarif65d9422009-09-14 17:48:44 -04001779 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001780 mod_timer(&dev_priv->hangcheck_timer,
1781 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001782 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001783 queue_delayed_work(dev_priv->wq,
1784 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001785 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001786 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001787}
1788
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001789static inline void
1790i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001791{
Chris Wilson1c255952010-09-26 11:03:27 +01001792 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001793
Chris Wilson1c255952010-09-26 11:03:27 +01001794 if (!file_priv)
1795 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001796
Chris Wilson1c255952010-09-26 11:03:27 +01001797 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001798 if (request->file_priv) {
1799 list_del(&request->client_list);
1800 request->file_priv = NULL;
1801 }
Chris Wilson1c255952010-09-26 11:03:27 +01001802 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001803}
1804
Chris Wilsondfaae392010-09-22 10:31:52 +01001805static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1806 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001807{
Chris Wilsondfaae392010-09-22 10:31:52 +01001808 while (!list_empty(&ring->request_list)) {
1809 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001810
Chris Wilsondfaae392010-09-22 10:31:52 +01001811 request = list_first_entry(&ring->request_list,
1812 struct drm_i915_gem_request,
1813 list);
1814
1815 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001816 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001817 kfree(request);
1818 }
1819
1820 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001821 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001822
Chris Wilson05394f32010-11-08 19:18:58 +00001823 obj = list_first_entry(&ring->active_list,
1824 struct drm_i915_gem_object,
1825 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001826
Chris Wilson05394f32010-11-08 19:18:58 +00001827 obj->base.write_domain = 0;
1828 list_del_init(&obj->gpu_write_list);
1829 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001830 }
Eric Anholt673a3942008-07-30 12:06:12 -07001831}
1832
Chris Wilson312817a2010-11-22 11:50:11 +00001833static void i915_gem_reset_fences(struct drm_device *dev)
1834{
1835 struct drm_i915_private *dev_priv = dev->dev_private;
1836 int i;
1837
1838 for (i = 0; i < 16; i++) {
1839 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001840 struct drm_i915_gem_object *obj = reg->obj;
1841
1842 if (!obj)
1843 continue;
1844
1845 if (obj->tiling_mode)
1846 i915_gem_release_mmap(obj);
1847
Chris Wilsond9e86c02010-11-10 16:40:20 +00001848 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1849 reg->obj->fenced_gpu_access = false;
1850 reg->obj->last_fenced_seqno = 0;
1851 reg->obj->last_fenced_ring = NULL;
1852 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001853 }
1854}
1855
Chris Wilson069efc12010-09-30 16:53:18 +01001856void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001857{
Chris Wilsondfaae392010-09-22 10:31:52 +01001858 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001859 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001860 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001861
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001862 for (i = 0; i < I915_NUM_RINGS; i++)
1863 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001864
1865 /* Remove anything from the flushing lists. The GPU cache is likely
1866 * to be lost on reset along with the data, so simply move the
1867 * lost bo to the inactive list.
1868 */
1869 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001870 obj= list_first_entry(&dev_priv->mm.flushing_list,
1871 struct drm_i915_gem_object,
1872 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001873
Chris Wilson05394f32010-11-08 19:18:58 +00001874 obj->base.write_domain = 0;
1875 list_del_init(&obj->gpu_write_list);
1876 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001877 }
Chris Wilson9375e442010-09-19 12:21:28 +01001878
Chris Wilsondfaae392010-09-22 10:31:52 +01001879 /* Move everything out of the GPU domains to ensure we do any
1880 * necessary invalidation upon reuse.
1881 */
Chris Wilson05394f32010-11-08 19:18:58 +00001882 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001883 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001884 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001885 {
Chris Wilson05394f32010-11-08 19:18:58 +00001886 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001887 }
Chris Wilson069efc12010-09-30 16:53:18 +01001888
1889 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001890 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001891}
1892
1893/**
1894 * This function clears the request list as sequence numbers are passed.
1895 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001896static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001897i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001898{
Eric Anholt673a3942008-07-30 12:06:12 -07001899 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001900 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001901
Chris Wilsondb53a302011-02-03 11:57:46 +00001902 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001903 return;
1904
Chris Wilsondb53a302011-02-03 11:57:46 +00001905 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001906
Chris Wilson78501ea2010-10-27 12:18:21 +01001907 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001908
Chris Wilson076e2c02011-01-21 10:07:18 +00001909 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001910 if (seqno >= ring->sync_seqno[i])
1911 ring->sync_seqno[i] = 0;
1912
Zou Nan hai852835f2010-05-21 09:08:56 +08001913 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001914 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001915
Zou Nan hai852835f2010-05-21 09:08:56 +08001916 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001917 struct drm_i915_gem_request,
1918 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001919
Chris Wilsondfaae392010-09-22 10:31:52 +01001920 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001921 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001922
Chris Wilsondb53a302011-02-03 11:57:46 +00001923 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001924
1925 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001926 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001927 kfree(request);
1928 }
1929
1930 /* Move any buffers on the active list that are no longer referenced
1931 * by the ringbuffer to the flushing/inactive lists as appropriate.
1932 */
1933 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001934 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001935
Chris Wilson05394f32010-11-08 19:18:58 +00001936 obj= list_first_entry(&ring->active_list,
1937 struct drm_i915_gem_object,
1938 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001939
Chris Wilson05394f32010-11-08 19:18:58 +00001940 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001941 break;
1942
Chris Wilson05394f32010-11-08 19:18:58 +00001943 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001944 i915_gem_object_move_to_flushing(obj);
1945 else
1946 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001947 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001948
Chris Wilsondb53a302011-02-03 11:57:46 +00001949 if (unlikely(ring->trace_irq_seqno &&
1950 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001951 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001952 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001953 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001954
Chris Wilsondb53a302011-02-03 11:57:46 +00001955 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001956}
1957
1958void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001959i915_gem_retire_requests(struct drm_device *dev)
1960{
1961 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001962 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001963
Chris Wilsonbe726152010-07-23 23:18:50 +01001964 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001965 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001966
1967 /* We must be careful that during unbind() we do not
1968 * accidentally infinitely recurse into retire requests.
1969 * Currently:
1970 * retire -> free -> unbind -> wait -> retire_ring
1971 */
Chris Wilson05394f32010-11-08 19:18:58 +00001972 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001973 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001974 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001975 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001976 }
1977
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001978 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilsondb53a302011-02-03 11:57:46 +00001979 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001980}
1981
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001982static void
Eric Anholt673a3942008-07-30 12:06:12 -07001983i915_gem_retire_work_handler(struct work_struct *work)
1984{
1985 drm_i915_private_t *dev_priv;
1986 struct drm_device *dev;
Chris Wilson0a587052011-01-09 21:05:44 +00001987 bool idle;
1988 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001989
1990 dev_priv = container_of(work, drm_i915_private_t,
1991 mm.retire_work.work);
1992 dev = dev_priv->dev;
1993
Chris Wilson891b48c2010-09-29 12:26:37 +01001994 /* Come back later if the device is busy... */
1995 if (!mutex_trylock(&dev->struct_mutex)) {
1996 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1997 return;
1998 }
1999
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002000 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002001
Chris Wilson0a587052011-01-09 21:05:44 +00002002 /* Send a periodic flush down the ring so we don't hold onto GEM
2003 * objects indefinitely.
2004 */
2005 idle = true;
2006 for (i = 0; i < I915_NUM_RINGS; i++) {
2007 struct intel_ring_buffer *ring = &dev_priv->ring[i];
2008
2009 if (!list_empty(&ring->gpu_write_list)) {
2010 struct drm_i915_gem_request *request;
2011 int ret;
2012
Chris Wilsondb53a302011-02-03 11:57:46 +00002013 ret = i915_gem_flush_ring(ring,
2014 0, I915_GEM_GPU_DOMAINS);
Chris Wilson0a587052011-01-09 21:05:44 +00002015 request = kzalloc(sizeof(*request), GFP_KERNEL);
2016 if (ret || request == NULL ||
Chris Wilsondb53a302011-02-03 11:57:46 +00002017 i915_add_request(ring, NULL, request))
Chris Wilson0a587052011-01-09 21:05:44 +00002018 kfree(request);
2019 }
2020
2021 idle &= list_empty(&ring->request_list);
2022 }
2023
2024 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002025 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilson0a587052011-01-09 21:05:44 +00002026
Eric Anholt673a3942008-07-30 12:06:12 -07002027 mutex_unlock(&dev->struct_mutex);
2028}
2029
Chris Wilsondb53a302011-02-03 11:57:46 +00002030/**
2031 * Waits for a sequence number to be signaled, and cleans up the
2032 * request and object lists appropriately for that event.
2033 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002034int
Chris Wilsondb53a302011-02-03 11:57:46 +00002035i915_wait_request(struct intel_ring_buffer *ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002036 uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07002037{
Chris Wilsondb53a302011-02-03 11:57:46 +00002038 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002039 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002040 int ret = 0;
2041
2042 BUG_ON(seqno == 0);
2043
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002044 if (atomic_read(&dev_priv->mm.wedged)) {
2045 struct completion *x = &dev_priv->error_completion;
2046 bool recovery_complete;
2047 unsigned long flags;
2048
2049 /* Give the error handler a chance to run. */
2050 spin_lock_irqsave(&x->wait.lock, flags);
2051 recovery_complete = x->done > 0;
2052 spin_unlock_irqrestore(&x->wait.lock, flags);
2053
2054 return recovery_complete ? -EIO : -EAGAIN;
2055 }
Ben Gamariffed1d02009-09-14 17:48:41 -04002056
Chris Wilson5d97eb62010-11-10 20:40:02 +00002057 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002058 struct drm_i915_gem_request *request;
2059
2060 request = kzalloc(sizeof(*request), GFP_KERNEL);
2061 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002062 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002063
Chris Wilsondb53a302011-02-03 11:57:46 +00002064 ret = i915_add_request(ring, NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +01002065 if (ret) {
2066 kfree(request);
2067 return ret;
2068 }
2069
2070 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002071 }
2072
Chris Wilson78501ea2010-10-27 12:18:21 +01002073 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002074 if (HAS_PCH_SPLIT(ring->dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002075 ier = I915_READ(DEIER) | I915_READ(GTIER);
2076 else
2077 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002078 if (!ier) {
2079 DRM_ERROR("something (likely vbetool) disabled "
2080 "interrupts, re-enabling\n");
Chris Wilsondb53a302011-02-03 11:57:46 +00002081 i915_driver_irq_preinstall(ring->dev);
2082 i915_driver_irq_postinstall(ring->dev);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002083 }
2084
Chris Wilsondb53a302011-02-03 11:57:46 +00002085 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002086
Chris Wilsonb2223492010-10-27 15:27:33 +01002087 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002088 if (ring->irq_get(ring)) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002089 if (dev_priv->mm.interruptible)
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002090 ret = wait_event_interruptible(ring->irq_queue,
2091 i915_seqno_passed(ring->get_seqno(ring), seqno)
2092 || atomic_read(&dev_priv->mm.wedged));
2093 else
2094 wait_event(ring->irq_queue,
2095 i915_seqno_passed(ring->get_seqno(ring), seqno)
2096 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002097
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002098 ring->irq_put(ring);
Chris Wilsonb5ba1772010-12-14 12:17:15 +00002099 } else if (wait_for(i915_seqno_passed(ring->get_seqno(ring),
2100 seqno) ||
2101 atomic_read(&dev_priv->mm.wedged), 3000))
2102 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01002103 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002104
Chris Wilsondb53a302011-02-03 11:57:46 +00002105 trace_i915_gem_request_wait_end(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002106 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002107 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002108 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002109
2110 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002111 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002112 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002113 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002114
2115 /* Directly dispatch request retiring. While we have the work queue
2116 * to handle this, the waiter on a request often wants an associated
2117 * buffer to have made it to the inactive list, and we would need
2118 * a separate wait queue to handle that.
2119 */
2120 if (ret == 0)
Chris Wilsondb53a302011-02-03 11:57:46 +00002121 i915_gem_retire_requests_ring(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002122
2123 return ret;
2124}
2125
Daniel Vetter48764bf2009-09-15 22:57:32 +02002126/**
Eric Anholt673a3942008-07-30 12:06:12 -07002127 * Ensures that all rendering to the object has completed and the object is
2128 * safe to unbind from the GTT or access from the CPU.
2129 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00002130int
Chris Wilsonce453d82011-02-21 14:43:56 +00002131i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002132{
Eric Anholt673a3942008-07-30 12:06:12 -07002133 int ret;
2134
Eric Anholte47c68e2008-11-14 13:35:19 -08002135 /* This function only exists to support waiting for existing rendering,
2136 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002137 */
Chris Wilson05394f32010-11-08 19:18:58 +00002138 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002139
2140 /* If there is rendering queued on the buffer being evicted, wait for
2141 * it.
2142 */
Chris Wilson05394f32010-11-08 19:18:58 +00002143 if (obj->active) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002144 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002145 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002146 return ret;
2147 }
2148
2149 return 0;
2150}
2151
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002152static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2153{
2154 u32 old_write_domain, old_read_domains;
2155
2156 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2157 return;
2158
2159 /* Act a barrier for all accesses through the GTT */
2160 mb();
2161
2162 /* Force a pagefault for domain tracking on next user access */
2163 i915_gem_release_mmap(obj);
2164
2165 old_read_domains = obj->base.read_domains;
2166 old_write_domain = obj->base.write_domain;
2167
2168 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2169 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2170
2171 trace_i915_gem_object_change_domain(obj,
2172 old_read_domains,
2173 old_write_domain);
2174}
2175
Eric Anholt673a3942008-07-30 12:06:12 -07002176/**
2177 * Unbinds an object from the GTT aperture.
2178 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002179int
Chris Wilson05394f32010-11-08 19:18:58 +00002180i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002181{
Eric Anholt673a3942008-07-30 12:06:12 -07002182 int ret = 0;
2183
Chris Wilson05394f32010-11-08 19:18:58 +00002184 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002185 return 0;
2186
Chris Wilson05394f32010-11-08 19:18:58 +00002187 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002188 DRM_ERROR("Attempting to unbind pinned buffer\n");
2189 return -EINVAL;
2190 }
2191
Chris Wilsona8198ee2011-04-13 22:04:09 +01002192 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson8dc17752010-07-23 23:18:51 +01002193 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002194 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002195 /* Continue on if we fail due to EIO, the GPU is hung so we
2196 * should be safe and we need to cleanup or else we might
2197 * cause memory corruption through use-after-free.
2198 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002199
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002200 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002201
2202 /* Move the object to the CPU domain to ensure that
2203 * any possible CPU writes while it's not in the GTT
2204 * are flushed when we go to remap it.
2205 */
2206 if (ret == 0)
2207 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2208 if (ret == -ERESTARTSYS)
2209 return ret;
Chris Wilson812ed4922010-09-30 15:08:57 +01002210 if (ret) {
Chris Wilsona8198ee2011-04-13 22:04:09 +01002211 /* In the event of a disaster, abandon all caches and
2212 * hope for the best.
2213 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002214 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002215 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002216 }
Eric Anholt673a3942008-07-30 12:06:12 -07002217
Daniel Vetter96b47b62009-12-15 17:50:00 +01002218 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002219 ret = i915_gem_object_put_fence(obj);
2220 if (ret == -ERESTARTSYS)
2221 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002222
Chris Wilsondb53a302011-02-03 11:57:46 +00002223 trace_i915_gem_object_unbind(obj);
2224
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002225 i915_gem_gtt_unbind_object(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002226 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002227
Chris Wilson6299f992010-11-24 12:23:44 +00002228 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002229 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002230 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002231 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002232
Chris Wilson05394f32010-11-08 19:18:58 +00002233 drm_mm_put_block(obj->gtt_space);
2234 obj->gtt_space = NULL;
2235 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002236
Chris Wilson05394f32010-11-08 19:18:58 +00002237 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002238 i915_gem_object_truncate(obj);
2239
Chris Wilson8dc17752010-07-23 23:18:51 +01002240 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002241}
2242
Chris Wilson88241782011-01-07 17:09:48 +00002243int
Chris Wilsondb53a302011-02-03 11:57:46 +00002244i915_gem_flush_ring(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002245 uint32_t invalidate_domains,
2246 uint32_t flush_domains)
2247{
Chris Wilson88241782011-01-07 17:09:48 +00002248 int ret;
2249
Chris Wilson36d527d2011-03-19 22:26:49 +00002250 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2251 return 0;
2252
Chris Wilsondb53a302011-02-03 11:57:46 +00002253 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2254
Chris Wilson88241782011-01-07 17:09:48 +00002255 ret = ring->flush(ring, invalidate_domains, flush_domains);
2256 if (ret)
2257 return ret;
2258
Chris Wilson36d527d2011-03-19 22:26:49 +00002259 if (flush_domains & I915_GEM_GPU_DOMAINS)
2260 i915_gem_process_flushing_list(ring, flush_domains);
2261
Chris Wilson88241782011-01-07 17:09:48 +00002262 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002263}
2264
Chris Wilsondb53a302011-02-03 11:57:46 +00002265static int i915_ring_idle(struct intel_ring_buffer *ring)
Chris Wilsona56ba562010-09-28 10:07:56 +01002266{
Chris Wilson88241782011-01-07 17:09:48 +00002267 int ret;
2268
Chris Wilson395b70b2010-10-28 21:28:46 +01002269 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002270 return 0;
2271
Chris Wilson88241782011-01-07 17:09:48 +00002272 if (!list_empty(&ring->gpu_write_list)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002273 ret = i915_gem_flush_ring(ring,
Chris Wilson0ac74c62010-12-06 14:36:02 +00002274 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilson88241782011-01-07 17:09:48 +00002275 if (ret)
2276 return ret;
2277 }
2278
Chris Wilsonce453d82011-02-21 14:43:56 +00002279 return i915_wait_request(ring, i915_gem_next_request_seqno(ring));
Chris Wilsona56ba562010-09-28 10:07:56 +01002280}
2281
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002282int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002283i915_gpu_idle(struct drm_device *dev)
2284{
2285 drm_i915_private_t *dev_priv = dev->dev_private;
2286 bool lists_empty;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002287 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002288
Zou Nan haid1b851f2010-05-21 09:08:57 +08002289 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002290 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002291 if (lists_empty)
2292 return 0;
2293
2294 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002295 for (i = 0; i < I915_NUM_RINGS; i++) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002296 ret = i915_ring_idle(&dev_priv->ring[i]);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002297 if (ret)
2298 return ret;
2299 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002300
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002301 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002302}
2303
Daniel Vetterc6642782010-11-12 13:46:18 +00002304static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2305 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002306{
Chris Wilson05394f32010-11-08 19:18:58 +00002307 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002308 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002309 u32 size = obj->gtt_space->size;
2310 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002311 uint64_t val;
2312
Chris Wilson05394f32010-11-08 19:18:58 +00002313 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002314 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002315 val |= obj->gtt_offset & 0xfffff000;
2316 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002317 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2318
Chris Wilson05394f32010-11-08 19:18:58 +00002319 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002320 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2321 val |= I965_FENCE_REG_VALID;
2322
Daniel Vetterc6642782010-11-12 13:46:18 +00002323 if (pipelined) {
2324 int ret = intel_ring_begin(pipelined, 6);
2325 if (ret)
2326 return ret;
2327
2328 intel_ring_emit(pipelined, MI_NOOP);
2329 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2330 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2331 intel_ring_emit(pipelined, (u32)val);
2332 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2333 intel_ring_emit(pipelined, (u32)(val >> 32));
2334 intel_ring_advance(pipelined);
2335 } else
2336 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2337
2338 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002339}
2340
Daniel Vetterc6642782010-11-12 13:46:18 +00002341static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2342 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343{
Chris Wilson05394f32010-11-08 19:18:58 +00002344 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002345 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002346 u32 size = obj->gtt_space->size;
2347 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002348 uint64_t val;
2349
Chris Wilson05394f32010-11-08 19:18:58 +00002350 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002351 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002352 val |= obj->gtt_offset & 0xfffff000;
2353 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2354 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002355 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2356 val |= I965_FENCE_REG_VALID;
2357
Daniel Vetterc6642782010-11-12 13:46:18 +00002358 if (pipelined) {
2359 int ret = intel_ring_begin(pipelined, 6);
2360 if (ret)
2361 return ret;
2362
2363 intel_ring_emit(pipelined, MI_NOOP);
2364 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2365 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2366 intel_ring_emit(pipelined, (u32)val);
2367 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2368 intel_ring_emit(pipelined, (u32)(val >> 32));
2369 intel_ring_advance(pipelined);
2370 } else
2371 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2372
2373 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002374}
2375
Daniel Vetterc6642782010-11-12 13:46:18 +00002376static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2377 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002378{
Chris Wilson05394f32010-11-08 19:18:58 +00002379 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002380 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002381 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002382 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002383 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002384
Daniel Vetterc6642782010-11-12 13:46:18 +00002385 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2386 (size & -size) != size ||
2387 (obj->gtt_offset & (size - 1)),
2388 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2389 obj->gtt_offset, obj->map_and_fenceable, size))
2390 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002391
Daniel Vetterc6642782010-11-12 13:46:18 +00002392 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002393 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002394 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002395 tile_width = 512;
2396
2397 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002398 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002399 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002400
Chris Wilson05394f32010-11-08 19:18:58 +00002401 val = obj->gtt_offset;
2402 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002403 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002404 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002405 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2406 val |= I830_FENCE_REG_VALID;
2407
Chris Wilson05394f32010-11-08 19:18:58 +00002408 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002409 if (fence_reg < 8)
2410 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002411 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002412 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002413
2414 if (pipelined) {
2415 int ret = intel_ring_begin(pipelined, 4);
2416 if (ret)
2417 return ret;
2418
2419 intel_ring_emit(pipelined, MI_NOOP);
2420 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2421 intel_ring_emit(pipelined, fence_reg);
2422 intel_ring_emit(pipelined, val);
2423 intel_ring_advance(pipelined);
2424 } else
2425 I915_WRITE(fence_reg, val);
2426
2427 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002428}
2429
Daniel Vetterc6642782010-11-12 13:46:18 +00002430static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2431 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002432{
Chris Wilson05394f32010-11-08 19:18:58 +00002433 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002434 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002435 u32 size = obj->gtt_space->size;
2436 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002437 uint32_t val;
2438 uint32_t pitch_val;
2439
Daniel Vetterc6642782010-11-12 13:46:18 +00002440 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2441 (size & -size) != size ||
2442 (obj->gtt_offset & (size - 1)),
2443 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2444 obj->gtt_offset, size))
2445 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002446
Chris Wilson05394f32010-11-08 19:18:58 +00002447 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002448 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002449
Chris Wilson05394f32010-11-08 19:18:58 +00002450 val = obj->gtt_offset;
2451 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002452 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002453 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002454 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2455 val |= I830_FENCE_REG_VALID;
2456
Daniel Vetterc6642782010-11-12 13:46:18 +00002457 if (pipelined) {
2458 int ret = intel_ring_begin(pipelined, 4);
2459 if (ret)
2460 return ret;
2461
2462 intel_ring_emit(pipelined, MI_NOOP);
2463 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2464 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2465 intel_ring_emit(pipelined, val);
2466 intel_ring_advance(pipelined);
2467 } else
2468 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2469
2470 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002471}
2472
Chris Wilsond9e86c02010-11-10 16:40:20 +00002473static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2474{
2475 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2476}
2477
2478static int
2479i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002480 struct intel_ring_buffer *pipelined)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002481{
2482 int ret;
2483
2484 if (obj->fenced_gpu_access) {
Chris Wilson88241782011-01-07 17:09:48 +00002485 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002486 ret = i915_gem_flush_ring(obj->last_fenced_ring,
Chris Wilson88241782011-01-07 17:09:48 +00002487 0, obj->base.write_domain);
2488 if (ret)
2489 return ret;
2490 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002491
2492 obj->fenced_gpu_access = false;
2493 }
2494
2495 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2496 if (!ring_passed_seqno(obj->last_fenced_ring,
2497 obj->last_fenced_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002498 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002499 obj->last_fenced_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002500 if (ret)
2501 return ret;
2502 }
2503
2504 obj->last_fenced_seqno = 0;
2505 obj->last_fenced_ring = NULL;
2506 }
2507
Chris Wilson63256ec2011-01-04 18:42:07 +00002508 /* Ensure that all CPU reads are completed before installing a fence
2509 * and all writes before removing the fence.
2510 */
2511 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2512 mb();
2513
Chris Wilsond9e86c02010-11-10 16:40:20 +00002514 return 0;
2515}
2516
2517int
2518i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2519{
2520 int ret;
2521
2522 if (obj->tiling_mode)
2523 i915_gem_release_mmap(obj);
2524
Chris Wilsonce453d82011-02-21 14:43:56 +00002525 ret = i915_gem_object_flush_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002526 if (ret)
2527 return ret;
2528
2529 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2530 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2531 i915_gem_clear_fence_reg(obj->base.dev,
2532 &dev_priv->fence_regs[obj->fence_reg]);
2533
2534 obj->fence_reg = I915_FENCE_REG_NONE;
2535 }
2536
2537 return 0;
2538}
2539
2540static struct drm_i915_fence_reg *
2541i915_find_fence_reg(struct drm_device *dev,
2542 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002543{
Daniel Vetterae3db242010-02-19 11:51:58 +01002544 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002545 struct drm_i915_fence_reg *reg, *first, *avail;
2546 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002547
2548 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002549 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002550 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2551 reg = &dev_priv->fence_regs[i];
2552 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002553 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002554
Chris Wilson05394f32010-11-08 19:18:58 +00002555 if (!reg->obj->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002556 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002557 }
2558
Chris Wilsond9e86c02010-11-10 16:40:20 +00002559 if (avail == NULL)
2560 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002561
2562 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002563 avail = first = NULL;
2564 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2565 if (reg->obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002566 continue;
2567
Chris Wilsond9e86c02010-11-10 16:40:20 +00002568 if (first == NULL)
2569 first = reg;
2570
2571 if (!pipelined ||
2572 !reg->obj->last_fenced_ring ||
2573 reg->obj->last_fenced_ring == pipelined) {
2574 avail = reg;
2575 break;
2576 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002577 }
2578
Chris Wilsond9e86c02010-11-10 16:40:20 +00002579 if (avail == NULL)
2580 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002581
Chris Wilsona00b10c2010-09-24 21:15:47 +01002582 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002583}
2584
Jesse Barnesde151cf2008-11-12 10:03:55 -08002585/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002586 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002587 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002588 * @pipelined: ring on which to queue the change, or NULL for CPU access
2589 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002590 *
2591 * When mapping objects through the GTT, userspace wants to be able to write
2592 * to them without having to worry about swizzling if the object is tiled.
2593 *
2594 * This function walks the fence regs looking for a free one for @obj,
2595 * stealing one if it can't find any.
2596 *
2597 * It then sets up the reg based on the object's properties: address, pitch
2598 * and tiling format.
2599 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002600int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002601i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002602 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002603{
Chris Wilson05394f32010-11-08 19:18:58 +00002604 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002605 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002606 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002607 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002608
Chris Wilson6bda10d2010-12-05 21:04:18 +00002609 /* XXX disable pipelining. There are bugs. Shocking. */
2610 pipelined = NULL;
2611
Chris Wilsond9e86c02010-11-10 16:40:20 +00002612 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002613 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2614 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002615 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002616
Chris Wilson29c5a582011-03-17 15:23:22 +00002617 if (obj->tiling_changed) {
2618 ret = i915_gem_object_flush_fence(obj, pipelined);
2619 if (ret)
2620 return ret;
2621
2622 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2623 pipelined = NULL;
2624
2625 if (pipelined) {
2626 reg->setup_seqno =
2627 i915_gem_next_request_seqno(pipelined);
2628 obj->last_fenced_seqno = reg->setup_seqno;
2629 obj->last_fenced_ring = pipelined;
2630 }
2631
2632 goto update;
2633 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002634
2635 if (!pipelined) {
2636 if (reg->setup_seqno) {
2637 if (!ring_passed_seqno(obj->last_fenced_ring,
2638 reg->setup_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002639 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002640 reg->setup_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002641 if (ret)
2642 return ret;
2643 }
2644
2645 reg->setup_seqno = 0;
2646 }
2647 } else if (obj->last_fenced_ring &&
2648 obj->last_fenced_ring != pipelined) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002649 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002650 if (ret)
2651 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002652 }
2653
Eric Anholta09ba7f2009-08-29 12:49:51 -07002654 return 0;
2655 }
2656
Chris Wilsond9e86c02010-11-10 16:40:20 +00002657 reg = i915_find_fence_reg(dev, pipelined);
2658 if (reg == NULL)
2659 return -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002660
Chris Wilsonce453d82011-02-21 14:43:56 +00002661 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002662 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002663 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002664
Chris Wilsond9e86c02010-11-10 16:40:20 +00002665 if (reg->obj) {
2666 struct drm_i915_gem_object *old = reg->obj;
2667
2668 drm_gem_object_reference(&old->base);
2669
2670 if (old->tiling_mode)
2671 i915_gem_release_mmap(old);
2672
Chris Wilsonce453d82011-02-21 14:43:56 +00002673 ret = i915_gem_object_flush_fence(old, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002674 if (ret) {
2675 drm_gem_object_unreference(&old->base);
2676 return ret;
2677 }
2678
2679 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2680 pipelined = NULL;
2681
2682 old->fence_reg = I915_FENCE_REG_NONE;
2683 old->last_fenced_ring = pipelined;
2684 old->last_fenced_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002685 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002686
2687 drm_gem_object_unreference(&old->base);
2688 } else if (obj->last_fenced_seqno == 0)
2689 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002690
Jesse Barnesde151cf2008-11-12 10:03:55 -08002691 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002692 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2693 obj->fence_reg = reg - dev_priv->fence_regs;
2694 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002695
Chris Wilsond9e86c02010-11-10 16:40:20 +00002696 reg->setup_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002697 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002698 obj->last_fenced_seqno = reg->setup_seqno;
2699
2700update:
2701 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002702 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002703 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002704 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002705 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002706 break;
2707 case 5:
2708 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002709 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002710 break;
2711 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002712 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002713 break;
2714 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002715 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002716 break;
2717 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002718
Daniel Vetterc6642782010-11-12 13:46:18 +00002719 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002720}
2721
2722/**
2723 * i915_gem_clear_fence_reg - clear out fence register info
2724 * @obj: object to clear
2725 *
2726 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002727 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002728 */
2729static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002730i915_gem_clear_fence_reg(struct drm_device *dev,
2731 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002732{
Jesse Barnes79e53942008-11-07 14:24:08 -08002733 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002734 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002735
Chris Wilsone259bef2010-09-17 00:32:02 +01002736 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002737 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002738 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002739 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002740 break;
2741 case 5:
2742 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002743 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002744 break;
2745 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002746 if (fence_reg >= 8)
2747 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002748 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002749 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002750 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002751
2752 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002753 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002754 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002755
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002756 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002757 reg->obj = NULL;
2758 reg->setup_seqno = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002759}
2760
2761/**
Eric Anholt673a3942008-07-30 12:06:12 -07002762 * Finds free space in the GTT aperture and binds the object there.
2763 */
2764static int
Chris Wilson05394f32010-11-08 19:18:58 +00002765i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002766 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002767 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002768{
Chris Wilson05394f32010-11-08 19:18:58 +00002769 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002770 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002771 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002772 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002773 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002774 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002775 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002776
Chris Wilson05394f32010-11-08 19:18:58 +00002777 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002778 DRM_ERROR("Attempting to bind a purgeable object\n");
2779 return -EINVAL;
2780 }
2781
Chris Wilson05394f32010-11-08 19:18:58 +00002782 fence_size = i915_gem_get_gtt_size(obj);
2783 fence_alignment = i915_gem_get_gtt_alignment(obj);
2784 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002785
Eric Anholt673a3942008-07-30 12:06:12 -07002786 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002787 alignment = map_and_fenceable ? fence_alignment :
2788 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002789 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002790 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2791 return -EINVAL;
2792 }
2793
Chris Wilson05394f32010-11-08 19:18:58 +00002794 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002795
Chris Wilson654fc602010-05-27 13:18:21 +01002796 /* If the object is bigger than the entire aperture, reject it early
2797 * before evicting everything in a vain attempt to find space.
2798 */
Chris Wilson05394f32010-11-08 19:18:58 +00002799 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002800 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002801 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2802 return -E2BIG;
2803 }
2804
Eric Anholt673a3942008-07-30 12:06:12 -07002805 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002806 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002807 free_space =
2808 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002809 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002810 dev_priv->mm.gtt_mappable_end,
2811 0);
2812 else
2813 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002814 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002815
2816 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002817 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002818 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002819 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002820 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002821 dev_priv->mm.gtt_mappable_end,
2822 0);
2823 else
Chris Wilson05394f32010-11-08 19:18:58 +00002824 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002825 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002826 }
Chris Wilson05394f32010-11-08 19:18:58 +00002827 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002828 /* If the gtt is empty and we're still having trouble
2829 * fitting our object in, we're out of memory.
2830 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002831 ret = i915_gem_evict_something(dev, size, alignment,
2832 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002833 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002834 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002835
Eric Anholt673a3942008-07-30 12:06:12 -07002836 goto search_free;
2837 }
2838
Chris Wilsone5281cc2010-10-28 13:45:36 +01002839 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002840 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002841 drm_mm_put_block(obj->gtt_space);
2842 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002843
2844 if (ret == -ENOMEM) {
Chris Wilson809b6332011-01-10 17:33:15 +00002845 /* first try to reclaim some memory by clearing the GTT */
2846 ret = i915_gem_evict_everything(dev, false);
Chris Wilson07f73f62009-09-14 16:50:30 +01002847 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002848 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002849 if (gfpmask) {
2850 gfpmask = 0;
2851 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002852 }
2853
Chris Wilson809b6332011-01-10 17:33:15 +00002854 return -ENOMEM;
Chris Wilson07f73f62009-09-14 16:50:30 +01002855 }
2856
2857 goto search_free;
2858 }
2859
Eric Anholt673a3942008-07-30 12:06:12 -07002860 return ret;
2861 }
2862
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002863 ret = i915_gem_gtt_bind_object(obj);
2864 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002865 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002866 drm_mm_put_block(obj->gtt_space);
2867 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002868
Chris Wilson809b6332011-01-10 17:33:15 +00002869 if (i915_gem_evict_everything(dev, false))
Chris Wilson07f73f62009-09-14 16:50:30 +01002870 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002871
2872 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002873 }
Eric Anholt673a3942008-07-30 12:06:12 -07002874
Chris Wilson6299f992010-11-24 12:23:44 +00002875 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002876 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002877
Eric Anholt673a3942008-07-30 12:06:12 -07002878 /* Assert that the object is not currently in any GPU domain. As it
2879 * wasn't in the GTT, there shouldn't be any way it could have been in
2880 * a GPU cache
2881 */
Chris Wilson05394f32010-11-08 19:18:58 +00002882 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2883 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002884
Chris Wilson6299f992010-11-24 12:23:44 +00002885 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002886
Daniel Vetter75e9e912010-11-04 17:11:09 +01002887 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002888 obj->gtt_space->size == fence_size &&
2889 (obj->gtt_space->start & (fence_alignment -1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002890
Daniel Vetter75e9e912010-11-04 17:11:09 +01002891 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002892 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002893
Chris Wilson05394f32010-11-08 19:18:58 +00002894 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002895
Chris Wilsondb53a302011-02-03 11:57:46 +00002896 trace_i915_gem_object_bind(obj, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002897 return 0;
2898}
2899
2900void
Chris Wilson05394f32010-11-08 19:18:58 +00002901i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002902{
Eric Anholt673a3942008-07-30 12:06:12 -07002903 /* If we don't have a page list set up, then we're not pinned
2904 * to GPU, and we can ignore the cache flush because it'll happen
2905 * again at bind time.
2906 */
Chris Wilson05394f32010-11-08 19:18:58 +00002907 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002908 return;
2909
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002910 /* If the GPU is snooping the contents of the CPU cache,
2911 * we do not need to manually clear the CPU cache lines. However,
2912 * the caches are only snooped when the render cache is
2913 * flushed/invalidated. As we always have to emit invalidations
2914 * and flushes when moving into and out of the RENDER domain, correct
2915 * snooping behaviour occurs naturally as the result of our domain
2916 * tracking.
2917 */
2918 if (obj->cache_level != I915_CACHE_NONE)
2919 return;
2920
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002921 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002922
Chris Wilson05394f32010-11-08 19:18:58 +00002923 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002924}
2925
Eric Anholte47c68e2008-11-14 13:35:19 -08002926/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson88241782011-01-07 17:09:48 +00002927static int
Chris Wilson3619df02010-11-28 15:37:17 +00002928i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002929{
Chris Wilson05394f32010-11-08 19:18:58 +00002930 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson88241782011-01-07 17:09:48 +00002931 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002932
2933 /* Queue the GPU write cache flushing we need. */
Chris Wilsondb53a302011-02-03 11:57:46 +00002934 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002935}
2936
2937/** Flushes the GTT write domain for the object if it's dirty. */
2938static void
Chris Wilson05394f32010-11-08 19:18:58 +00002939i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002940{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002941 uint32_t old_write_domain;
2942
Chris Wilson05394f32010-11-08 19:18:58 +00002943 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002944 return;
2945
Chris Wilson63256ec2011-01-04 18:42:07 +00002946 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002947 * to it immediately go to main memory as far as we know, so there's
2948 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002949 *
2950 * However, we do have to enforce the order so that all writes through
2951 * the GTT land before any writes to the device, such as updates to
2952 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002953 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002954 wmb();
2955
Chris Wilson4a684a42010-10-28 14:44:08 +01002956 i915_gem_release_mmap(obj);
2957
Chris Wilson05394f32010-11-08 19:18:58 +00002958 old_write_domain = obj->base.write_domain;
2959 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002960
2961 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002962 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002963 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002964}
2965
2966/** Flushes the CPU write domain for the object if it's dirty. */
2967static void
Chris Wilson05394f32010-11-08 19:18:58 +00002968i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002969{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002970 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002971
Chris Wilson05394f32010-11-08 19:18:58 +00002972 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002973 return;
2974
2975 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002976 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002977 old_write_domain = obj->base.write_domain;
2978 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002979
2980 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002981 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002982 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002983}
2984
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002985/**
2986 * Moves a single object to the GTT read, and possibly write domain.
2987 *
2988 * This function returns when the move is complete, including waiting on
2989 * flushes to occur.
2990 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002991int
Chris Wilson20217462010-11-23 15:26:33 +00002992i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002993{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002994 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002995 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002996
Eric Anholt02354392008-11-26 13:58:13 -08002997 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002998 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002999 return -EINVAL;
3000
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003001 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3002 return 0;
3003
Chris Wilson88241782011-01-07 17:09:48 +00003004 ret = i915_gem_object_flush_gpu_write_domain(obj);
3005 if (ret)
3006 return ret;
3007
Chris Wilson87ca9c82010-12-02 09:42:56 +00003008 if (obj->pending_gpu_write || write) {
Chris Wilsonce453d82011-02-21 14:43:56 +00003009 ret = i915_gem_object_wait_rendering(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00003010 if (ret)
3011 return ret;
3012 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003013
Chris Wilson72133422010-09-13 23:56:38 +01003014 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003015
Chris Wilson05394f32010-11-08 19:18:58 +00003016 old_write_domain = obj->base.write_domain;
3017 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003018
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003019 /* It should now be out of any other write domains, and we can update
3020 * the domain values for our changes.
3021 */
Chris Wilson05394f32010-11-08 19:18:58 +00003022 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3023 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003024 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003025 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3026 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3027 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003028 }
3029
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003030 trace_i915_gem_object_change_domain(obj,
3031 old_read_domains,
3032 old_write_domain);
3033
Eric Anholte47c68e2008-11-14 13:35:19 -08003034 return 0;
3035}
3036
Chris Wilsone4ffd172011-04-04 09:44:39 +01003037int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3038 enum i915_cache_level cache_level)
3039{
3040 int ret;
3041
3042 if (obj->cache_level == cache_level)
3043 return 0;
3044
3045 if (obj->pin_count) {
3046 DRM_DEBUG("can not change the cache level of pinned objects\n");
3047 return -EBUSY;
3048 }
3049
3050 if (obj->gtt_space) {
3051 ret = i915_gem_object_finish_gpu(obj);
3052 if (ret)
3053 return ret;
3054
3055 i915_gem_object_finish_gtt(obj);
3056
3057 /* Before SandyBridge, you could not use tiling or fence
3058 * registers with snooped memory, so relinquish any fences
3059 * currently pointing to our region in the aperture.
3060 */
3061 if (INTEL_INFO(obj->base.dev)->gen < 6) {
3062 ret = i915_gem_object_put_fence(obj);
3063 if (ret)
3064 return ret;
3065 }
3066
3067 i915_gem_gtt_rebind_object(obj, cache_level);
3068 }
3069
3070 if (cache_level == I915_CACHE_NONE) {
3071 u32 old_read_domains, old_write_domain;
3072
3073 /* If we're coming from LLC cached, then we haven't
3074 * actually been tracking whether the data is in the
3075 * CPU cache or not, since we only allow one bit set
3076 * in obj->write_domain and have been skipping the clflushes.
3077 * Just set it to the CPU cache for now.
3078 */
3079 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3080 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3081
3082 old_read_domains = obj->base.read_domains;
3083 old_write_domain = obj->base.write_domain;
3084
3085 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3086 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3087
3088 trace_i915_gem_object_change_domain(obj,
3089 old_read_domains,
3090 old_write_domain);
3091 }
3092
3093 obj->cache_level = cache_level;
3094 return 0;
3095}
3096
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003097/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003098 * Prepare buffer for display plane (scanout, cursors, etc).
3099 * Can be called from an uninterruptible phase (modesetting) and allows
3100 * any flushes to be pipelined (for pageflips).
3101 *
3102 * For the display plane, we want to be in the GTT but out of any write
3103 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
3104 * ability to pipeline the waits, pinning and any additional subtleties
3105 * that may differentiate the display plane from ordinary buffers.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003106 */
3107int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003108i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3109 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003110 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003111{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003112 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003113 int ret;
3114
Chris Wilson88241782011-01-07 17:09:48 +00003115 ret = i915_gem_object_flush_gpu_write_domain(obj);
3116 if (ret)
3117 return ret;
3118
Chris Wilson0be73282010-12-06 14:36:27 +00003119 if (pipelined != obj->ring) {
Chris Wilsonce453d82011-02-21 14:43:56 +00003120 ret = i915_gem_object_wait_rendering(obj);
Chris Wilsonced270f2010-09-26 22:47:46 +01003121 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003122 return ret;
3123 }
3124
Eric Anholta7ef0642011-03-29 16:59:54 -07003125 /* The display engine is not coherent with the LLC cache on gen6. As
3126 * a result, we make sure that the pinning that is about to occur is
3127 * done with uncached PTEs. This is lowest common denominator for all
3128 * chipsets.
3129 *
3130 * However for gen6+, we could do better by using the GFDT bit instead
3131 * of uncaching, which would allow us to flush all the LLC-cached data
3132 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3133 */
3134 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3135 if (ret)
3136 return ret;
3137
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003138 /* As the user may map the buffer once pinned in the display plane
3139 * (e.g. libkms for the bootup splash), we have to ensure that we
3140 * always use map_and_fenceable for all scanout buffers.
3141 */
3142 ret = i915_gem_object_pin(obj, alignment, true);
3143 if (ret)
3144 return ret;
3145
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003146 i915_gem_object_flush_cpu_write_domain(obj);
3147
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003148 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003149 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003150
3151 /* It should now be out of any other write domains, and we can update
3152 * the domain values for our changes.
3153 */
3154 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003155 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003156
3157 trace_i915_gem_object_change_domain(obj,
3158 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003159 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003160
3161 return 0;
3162}
3163
Chris Wilson85345512010-11-13 09:49:11 +00003164int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003165i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003166{
Chris Wilson88241782011-01-07 17:09:48 +00003167 int ret;
3168
Chris Wilsona8198ee2011-04-13 22:04:09 +01003169 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003170 return 0;
3171
Chris Wilson88241782011-01-07 17:09:48 +00003172 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003173 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Chris Wilson88241782011-01-07 17:09:48 +00003174 if (ret)
3175 return ret;
3176 }
Chris Wilson85345512010-11-13 09:49:11 +00003177
Chris Wilsona8198ee2011-04-13 22:04:09 +01003178 /* Ensure that we invalidate the GPU's caches and TLBs. */
3179 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3180
Chris Wilsonce453d82011-02-21 14:43:56 +00003181 return i915_gem_object_wait_rendering(obj);
Chris Wilson85345512010-11-13 09:49:11 +00003182}
3183
Eric Anholte47c68e2008-11-14 13:35:19 -08003184/**
3185 * Moves a single object to the CPU read, and possibly write domain.
3186 *
3187 * This function returns when the move is complete, including waiting on
3188 * flushes to occur.
3189 */
3190static int
Chris Wilson919926a2010-11-12 13:42:53 +00003191i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003192{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003193 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003194 int ret;
3195
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003196 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3197 return 0;
3198
Chris Wilson88241782011-01-07 17:09:48 +00003199 ret = i915_gem_object_flush_gpu_write_domain(obj);
3200 if (ret)
3201 return ret;
3202
Chris Wilsonce453d82011-02-21 14:43:56 +00003203 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003204 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003205 return ret;
3206
3207 i915_gem_object_flush_gtt_write_domain(obj);
3208
3209 /* If we have a partially-valid cache of the object in the CPU,
3210 * finish invalidating it and free the per-page flags.
3211 */
3212 i915_gem_object_set_to_full_cpu_read_domain(obj);
3213
Chris Wilson05394f32010-11-08 19:18:58 +00003214 old_write_domain = obj->base.write_domain;
3215 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003216
Eric Anholte47c68e2008-11-14 13:35:19 -08003217 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003218 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003219 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003220
Chris Wilson05394f32010-11-08 19:18:58 +00003221 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003222 }
3223
3224 /* It should now be out of any other write domains, and we can update
3225 * the domain values for our changes.
3226 */
Chris Wilson05394f32010-11-08 19:18:58 +00003227 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003228
3229 /* If we're writing through the CPU, then the GPU read domains will
3230 * need to be invalidated at next use.
3231 */
3232 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003233 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3234 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003235 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003236
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003237 trace_i915_gem_object_change_domain(obj,
3238 old_read_domains,
3239 old_write_domain);
3240
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003241 return 0;
3242}
3243
Eric Anholt673a3942008-07-30 12:06:12 -07003244/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003245 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003246 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003247 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3248 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3249 */
3250static void
Chris Wilson05394f32010-11-08 19:18:58 +00003251i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003252{
Chris Wilson05394f32010-11-08 19:18:58 +00003253 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003254 return;
3255
3256 /* If we're partially in the CPU read domain, finish moving it in.
3257 */
Chris Wilson05394f32010-11-08 19:18:58 +00003258 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003259 int i;
3260
Chris Wilson05394f32010-11-08 19:18:58 +00003261 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3262 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003263 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003264 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003265 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003266 }
3267
3268 /* Free the page_cpu_valid mappings which are now stale, whether
3269 * or not we've got I915_GEM_DOMAIN_CPU.
3270 */
Chris Wilson05394f32010-11-08 19:18:58 +00003271 kfree(obj->page_cpu_valid);
3272 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003273}
3274
3275/**
3276 * Set the CPU read domain on a range of the object.
3277 *
3278 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3279 * not entirely valid. The page_cpu_valid member of the object flags which
3280 * pages have been flushed, and will be respected by
3281 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3282 * of the whole object.
3283 *
3284 * This function returns when the move is complete, including waiting on
3285 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003286 */
3287static int
Chris Wilson05394f32010-11-08 19:18:58 +00003288i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003289 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003290{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003291 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003292 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003293
Chris Wilson05394f32010-11-08 19:18:58 +00003294 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003295 return i915_gem_object_set_to_cpu_domain(obj, 0);
3296
Chris Wilson88241782011-01-07 17:09:48 +00003297 ret = i915_gem_object_flush_gpu_write_domain(obj);
3298 if (ret)
3299 return ret;
3300
Chris Wilsonce453d82011-02-21 14:43:56 +00003301 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003302 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003303 return ret;
Daniel Vetterde18a292010-11-27 22:30:41 +01003304
Eric Anholte47c68e2008-11-14 13:35:19 -08003305 i915_gem_object_flush_gtt_write_domain(obj);
3306
3307 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003308 if (obj->page_cpu_valid == NULL &&
3309 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003310 return 0;
3311
Eric Anholte47c68e2008-11-14 13:35:19 -08003312 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3313 * newly adding I915_GEM_DOMAIN_CPU
3314 */
Chris Wilson05394f32010-11-08 19:18:58 +00003315 if (obj->page_cpu_valid == NULL) {
3316 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3317 GFP_KERNEL);
3318 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003319 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003320 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3321 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003322
3323 /* Flush the cache on any pages that are still invalid from the CPU's
3324 * perspective.
3325 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003326 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3327 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003328 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003329 continue;
3330
Chris Wilson05394f32010-11-08 19:18:58 +00003331 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003332
Chris Wilson05394f32010-11-08 19:18:58 +00003333 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003334 }
3335
Eric Anholte47c68e2008-11-14 13:35:19 -08003336 /* It should now be out of any other write domains, and we can update
3337 * the domain values for our changes.
3338 */
Chris Wilson05394f32010-11-08 19:18:58 +00003339 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003340
Chris Wilson05394f32010-11-08 19:18:58 +00003341 old_read_domains = obj->base.read_domains;
3342 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003343
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003344 trace_i915_gem_object_change_domain(obj,
3345 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003346 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003347
Eric Anholt673a3942008-07-30 12:06:12 -07003348 return 0;
3349}
3350
Eric Anholt673a3942008-07-30 12:06:12 -07003351/* Throttle our rendering by waiting until the ring has completed our requests
3352 * emitted over 20 msec ago.
3353 *
Eric Anholtb9624422009-06-03 07:27:35 +00003354 * Note that if we were to use the current jiffies each time around the loop,
3355 * we wouldn't escape the function with any frames outstanding if the time to
3356 * render a frame was over 20ms.
3357 *
Eric Anholt673a3942008-07-30 12:06:12 -07003358 * This should get us reasonable parallelism between CPU and GPU but also
3359 * relatively low latency when blocking on a particular request to finish.
3360 */
3361static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003362i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003363{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003364 struct drm_i915_private *dev_priv = dev->dev_private;
3365 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003366 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003367 struct drm_i915_gem_request *request;
3368 struct intel_ring_buffer *ring = NULL;
3369 u32 seqno = 0;
3370 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003371
Chris Wilsone110e8d2011-01-26 15:39:14 +00003372 if (atomic_read(&dev_priv->mm.wedged))
3373 return -EIO;
3374
Chris Wilson1c255952010-09-26 11:03:27 +01003375 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003376 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003377 if (time_after_eq(request->emitted_jiffies, recent_enough))
3378 break;
3379
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003380 ring = request->ring;
3381 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003382 }
Chris Wilson1c255952010-09-26 11:03:27 +01003383 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003384
3385 if (seqno == 0)
3386 return 0;
3387
3388 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003389 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003390 /* And wait for the seqno passing without holding any locks and
3391 * causing extra latency for others. This is safe as the irq
3392 * generation is designed to be run atomically and so is
3393 * lockless.
3394 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003395 if (ring->irq_get(ring)) {
3396 ret = wait_event_interruptible(ring->irq_queue,
3397 i915_seqno_passed(ring->get_seqno(ring), seqno)
3398 || atomic_read(&dev_priv->mm.wedged));
3399 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003400
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003401 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3402 ret = -EIO;
3403 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003404 }
3405
3406 if (ret == 0)
3407 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003408
Eric Anholt673a3942008-07-30 12:06:12 -07003409 return ret;
3410}
3411
Eric Anholt673a3942008-07-30 12:06:12 -07003412int
Chris Wilson05394f32010-11-08 19:18:58 +00003413i915_gem_object_pin(struct drm_i915_gem_object *obj,
3414 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003415 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003416{
Chris Wilson05394f32010-11-08 19:18:58 +00003417 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003418 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003419 int ret;
3420
Chris Wilson05394f32010-11-08 19:18:58 +00003421 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003422 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003423
Chris Wilson05394f32010-11-08 19:18:58 +00003424 if (obj->gtt_space != NULL) {
3425 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3426 (map_and_fenceable && !obj->map_and_fenceable)) {
3427 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003428 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003429 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3430 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003431 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003432 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003433 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003434 ret = i915_gem_object_unbind(obj);
3435 if (ret)
3436 return ret;
3437 }
3438 }
3439
Chris Wilson05394f32010-11-08 19:18:58 +00003440 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003441 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003442 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003443 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003444 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003445 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003446
Chris Wilson05394f32010-11-08 19:18:58 +00003447 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003448 if (!obj->active)
3449 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003450 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003451 }
Chris Wilson6299f992010-11-24 12:23:44 +00003452 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003453
Chris Wilson23bc5982010-09-29 16:10:57 +01003454 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003455 return 0;
3456}
3457
3458void
Chris Wilson05394f32010-11-08 19:18:58 +00003459i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003460{
Chris Wilson05394f32010-11-08 19:18:58 +00003461 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003462 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003463
Chris Wilson23bc5982010-09-29 16:10:57 +01003464 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003465 BUG_ON(obj->pin_count == 0);
3466 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003467
Chris Wilson05394f32010-11-08 19:18:58 +00003468 if (--obj->pin_count == 0) {
3469 if (!obj->active)
3470 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003471 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003472 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003473 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003474 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003475}
3476
3477int
3478i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003479 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003480{
3481 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003482 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003483 int ret;
3484
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003485 ret = i915_mutex_lock_interruptible(dev);
3486 if (ret)
3487 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003488
Chris Wilson05394f32010-11-08 19:18:58 +00003489 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003490 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003491 ret = -ENOENT;
3492 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003493 }
Eric Anholt673a3942008-07-30 12:06:12 -07003494
Chris Wilson05394f32010-11-08 19:18:58 +00003495 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003496 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003497 ret = -EINVAL;
3498 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003499 }
3500
Chris Wilson05394f32010-11-08 19:18:58 +00003501 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003502 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3503 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003504 ret = -EINVAL;
3505 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003506 }
3507
Chris Wilson05394f32010-11-08 19:18:58 +00003508 obj->user_pin_count++;
3509 obj->pin_filp = file;
3510 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003511 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003512 if (ret)
3513 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003514 }
3515
3516 /* XXX - flush the CPU caches for pinned objects
3517 * as the X server doesn't manage domains yet
3518 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003519 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003520 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003521out:
Chris Wilson05394f32010-11-08 19:18:58 +00003522 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003523unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003524 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003525 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003526}
3527
3528int
3529i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003530 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003531{
3532 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003533 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003534 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003535
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003536 ret = i915_mutex_lock_interruptible(dev);
3537 if (ret)
3538 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003539
Chris Wilson05394f32010-11-08 19:18:58 +00003540 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003541 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003542 ret = -ENOENT;
3543 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003544 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003545
Chris Wilson05394f32010-11-08 19:18:58 +00003546 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003547 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3548 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003549 ret = -EINVAL;
3550 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003551 }
Chris Wilson05394f32010-11-08 19:18:58 +00003552 obj->user_pin_count--;
3553 if (obj->user_pin_count == 0) {
3554 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003555 i915_gem_object_unpin(obj);
3556 }
Eric Anholt673a3942008-07-30 12:06:12 -07003557
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003558out:
Chris Wilson05394f32010-11-08 19:18:58 +00003559 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003560unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003561 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003562 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003563}
3564
3565int
3566i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003567 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003568{
3569 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003570 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003571 int ret;
3572
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003573 ret = i915_mutex_lock_interruptible(dev);
3574 if (ret)
3575 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003576
Chris Wilson05394f32010-11-08 19:18:58 +00003577 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003578 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003579 ret = -ENOENT;
3580 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003581 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003582
Chris Wilson0be555b2010-08-04 15:36:30 +01003583 /* Count all active objects as busy, even if they are currently not used
3584 * by the gpu. Users of this interface expect objects to eventually
3585 * become non-busy without any further actions, therefore emit any
3586 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003587 */
Chris Wilson05394f32010-11-08 19:18:58 +00003588 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003589 if (args->busy) {
3590 /* Unconditionally flush objects, even when the gpu still uses this
3591 * object. Userspace calling this function indicates that it wants to
3592 * use this buffer rather sooner than later, so issuing the required
3593 * flush earlier is beneficial.
3594 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003595 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003596 ret = i915_gem_flush_ring(obj->ring,
Chris Wilson88241782011-01-07 17:09:48 +00003597 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003598 } else if (obj->ring->outstanding_lazy_request ==
3599 obj->last_rendering_seqno) {
3600 struct drm_i915_gem_request *request;
3601
Chris Wilson7a194872010-12-07 10:38:40 +00003602 /* This ring is not being cleared by active usage,
3603 * so emit a request to do so.
3604 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003605 request = kzalloc(sizeof(*request), GFP_KERNEL);
3606 if (request)
Chris Wilsondb53a302011-02-03 11:57:46 +00003607 ret = i915_add_request(obj->ring, NULL,request);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003608 else
Chris Wilson7a194872010-12-07 10:38:40 +00003609 ret = -ENOMEM;
3610 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003611
3612 /* Update the active list for the hardware's current position.
3613 * Otherwise this only updates on a delayed timer or when irqs
3614 * are actually unmasked, and our working set ends up being
3615 * larger than required.
3616 */
Chris Wilsondb53a302011-02-03 11:57:46 +00003617 i915_gem_retire_requests_ring(obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003618
Chris Wilson05394f32010-11-08 19:18:58 +00003619 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003620 }
Eric Anholt673a3942008-07-30 12:06:12 -07003621
Chris Wilson05394f32010-11-08 19:18:58 +00003622 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003623unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003624 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003625 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003626}
3627
3628int
3629i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3630 struct drm_file *file_priv)
3631{
3632 return i915_gem_ring_throttle(dev, file_priv);
3633}
3634
Chris Wilson3ef94da2009-09-14 16:50:29 +01003635int
3636i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3637 struct drm_file *file_priv)
3638{
3639 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003640 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003641 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003642
3643 switch (args->madv) {
3644 case I915_MADV_DONTNEED:
3645 case I915_MADV_WILLNEED:
3646 break;
3647 default:
3648 return -EINVAL;
3649 }
3650
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003651 ret = i915_mutex_lock_interruptible(dev);
3652 if (ret)
3653 return ret;
3654
Chris Wilson05394f32010-11-08 19:18:58 +00003655 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003656 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003657 ret = -ENOENT;
3658 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003659 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003660
Chris Wilson05394f32010-11-08 19:18:58 +00003661 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003662 ret = -EINVAL;
3663 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003664 }
3665
Chris Wilson05394f32010-11-08 19:18:58 +00003666 if (obj->madv != __I915_MADV_PURGED)
3667 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003668
Chris Wilson2d7ef392009-09-20 23:13:10 +01003669 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003670 if (i915_gem_object_is_purgeable(obj) &&
3671 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003672 i915_gem_object_truncate(obj);
3673
Chris Wilson05394f32010-11-08 19:18:58 +00003674 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003675
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003676out:
Chris Wilson05394f32010-11-08 19:18:58 +00003677 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003678unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003679 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003680 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003681}
3682
Chris Wilson05394f32010-11-08 19:18:58 +00003683struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3684 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003685{
Chris Wilson73aa8082010-09-30 11:46:12 +01003686 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003687 struct drm_i915_gem_object *obj;
3688
3689 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3690 if (obj == NULL)
3691 return NULL;
3692
3693 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3694 kfree(obj);
3695 return NULL;
3696 }
3697
Chris Wilson73aa8082010-09-30 11:46:12 +01003698 i915_gem_info_add_obj(dev_priv, size);
3699
Daniel Vetterc397b902010-04-09 19:05:07 +00003700 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3701 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3702
Chris Wilson93dfb402011-03-29 16:59:50 -07003703 obj->cache_level = I915_CACHE_NONE;
Daniel Vetter62b8b212010-04-09 19:05:08 +00003704 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003705 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003706 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003707 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003708 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003709 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003710 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003711 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003712 /* Avoid an unnecessary call to unbind on the first bind. */
3713 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003714
Chris Wilson05394f32010-11-08 19:18:58 +00003715 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003716}
3717
Eric Anholt673a3942008-07-30 12:06:12 -07003718int i915_gem_init_object(struct drm_gem_object *obj)
3719{
Daniel Vetterc397b902010-04-09 19:05:07 +00003720 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003721
Eric Anholt673a3942008-07-30 12:06:12 -07003722 return 0;
3723}
3724
Chris Wilson05394f32010-11-08 19:18:58 +00003725static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003726{
Chris Wilson05394f32010-11-08 19:18:58 +00003727 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003728 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003729 int ret;
3730
3731 ret = i915_gem_object_unbind(obj);
3732 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003733 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003734 &dev_priv->mm.deferred_free_list);
3735 return;
3736 }
3737
Chris Wilson26e12f892011-03-20 11:20:19 +00003738 trace_i915_gem_object_destroy(obj);
3739
Chris Wilson05394f32010-11-08 19:18:58 +00003740 if (obj->base.map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01003741 i915_gem_free_mmap_offset(obj);
3742
Chris Wilson05394f32010-11-08 19:18:58 +00003743 drm_gem_object_release(&obj->base);
3744 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003745
Chris Wilson05394f32010-11-08 19:18:58 +00003746 kfree(obj->page_cpu_valid);
3747 kfree(obj->bit_17);
3748 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003749}
3750
Chris Wilson05394f32010-11-08 19:18:58 +00003751void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003752{
Chris Wilson05394f32010-11-08 19:18:58 +00003753 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3754 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003755
Chris Wilson05394f32010-11-08 19:18:58 +00003756 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003757 i915_gem_object_unpin(obj);
3758
Chris Wilson05394f32010-11-08 19:18:58 +00003759 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003760 i915_gem_detach_phys_object(dev, obj);
3761
Chris Wilsonbe726152010-07-23 23:18:50 +01003762 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003763}
3764
Jesse Barnes5669fca2009-02-17 15:13:31 -08003765int
Eric Anholt673a3942008-07-30 12:06:12 -07003766i915_gem_idle(struct drm_device *dev)
3767{
3768 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003769 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003770
Keith Packard6dbe2772008-10-14 21:41:13 -07003771 mutex_lock(&dev->struct_mutex);
3772
Chris Wilson87acb0a2010-10-19 10:13:00 +01003773 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003774 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003775 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003776 }
Eric Anholt673a3942008-07-30 12:06:12 -07003777
Chris Wilson29105cc2010-01-07 10:39:13 +00003778 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003779 if (ret) {
3780 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003781 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003782 }
Eric Anholt673a3942008-07-30 12:06:12 -07003783
Chris Wilson29105cc2010-01-07 10:39:13 +00003784 /* Under UMS, be paranoid and evict. */
3785 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003786 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003787 if (ret) {
3788 mutex_unlock(&dev->struct_mutex);
3789 return ret;
3790 }
3791 }
3792
Chris Wilson312817a2010-11-22 11:50:11 +00003793 i915_gem_reset_fences(dev);
3794
Chris Wilson29105cc2010-01-07 10:39:13 +00003795 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3796 * We need to replace this with a semaphore, or something.
3797 * And not confound mm.suspended!
3798 */
3799 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003800 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003801
3802 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003803 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003804
Keith Packard6dbe2772008-10-14 21:41:13 -07003805 mutex_unlock(&dev->struct_mutex);
3806
Chris Wilson29105cc2010-01-07 10:39:13 +00003807 /* Cancel the retire work handler, which should be idle now. */
3808 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3809
Eric Anholt673a3942008-07-30 12:06:12 -07003810 return 0;
3811}
3812
Eric Anholt673a3942008-07-30 12:06:12 -07003813int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003814i915_gem_init_ringbuffer(struct drm_device *dev)
3815{
3816 drm_i915_private_t *dev_priv = dev->dev_private;
3817 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003818
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003819 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003820 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003821 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003822
3823 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003824 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003825 if (ret)
3826 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003827 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003828
Chris Wilson549f7362010-10-19 11:19:32 +01003829 if (HAS_BLT(dev)) {
3830 ret = intel_init_blt_ring_buffer(dev);
3831 if (ret)
3832 goto cleanup_bsd_ring;
3833 }
3834
Chris Wilson6f392d5482010-08-07 11:01:22 +01003835 dev_priv->next_seqno = 1;
3836
Chris Wilson68f95ba2010-05-27 13:18:22 +01003837 return 0;
3838
Chris Wilson549f7362010-10-19 11:19:32 +01003839cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003840 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003841cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003842 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003843 return ret;
3844}
3845
3846void
3847i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3848{
3849 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003850 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003851
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003852 for (i = 0; i < I915_NUM_RINGS; i++)
3853 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003854}
3855
3856int
Eric Anholt673a3942008-07-30 12:06:12 -07003857i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3858 struct drm_file *file_priv)
3859{
3860 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003861 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003862
Jesse Barnes79e53942008-11-07 14:24:08 -08003863 if (drm_core_check_feature(dev, DRIVER_MODESET))
3864 return 0;
3865
Ben Gamariba1234d2009-09-14 17:48:47 -04003866 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003867 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003868 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003869 }
3870
Eric Anholt673a3942008-07-30 12:06:12 -07003871 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003872 dev_priv->mm.suspended = 0;
3873
3874 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003875 if (ret != 0) {
3876 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003877 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003878 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003879
Chris Wilson69dc4982010-10-19 10:36:51 +01003880 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003881 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3882 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003883 for (i = 0; i < I915_NUM_RINGS; i++) {
3884 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3885 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3886 }
Eric Anholt673a3942008-07-30 12:06:12 -07003887 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003888
Chris Wilson5f353082010-06-07 14:03:03 +01003889 ret = drm_irq_install(dev);
3890 if (ret)
3891 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003892
Eric Anholt673a3942008-07-30 12:06:12 -07003893 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003894
3895cleanup_ringbuffer:
3896 mutex_lock(&dev->struct_mutex);
3897 i915_gem_cleanup_ringbuffer(dev);
3898 dev_priv->mm.suspended = 1;
3899 mutex_unlock(&dev->struct_mutex);
3900
3901 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003902}
3903
3904int
3905i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3906 struct drm_file *file_priv)
3907{
Jesse Barnes79e53942008-11-07 14:24:08 -08003908 if (drm_core_check_feature(dev, DRIVER_MODESET))
3909 return 0;
3910
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003911 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003912 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003913}
3914
3915void
3916i915_gem_lastclose(struct drm_device *dev)
3917{
3918 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003919
Eric Anholte806b492009-01-22 09:56:58 -08003920 if (drm_core_check_feature(dev, DRIVER_MODESET))
3921 return;
3922
Keith Packard6dbe2772008-10-14 21:41:13 -07003923 ret = i915_gem_idle(dev);
3924 if (ret)
3925 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003926}
3927
Chris Wilson64193402010-10-24 12:38:05 +01003928static void
3929init_ring_lists(struct intel_ring_buffer *ring)
3930{
3931 INIT_LIST_HEAD(&ring->active_list);
3932 INIT_LIST_HEAD(&ring->request_list);
3933 INIT_LIST_HEAD(&ring->gpu_write_list);
3934}
3935
Eric Anholt673a3942008-07-30 12:06:12 -07003936void
3937i915_gem_load(struct drm_device *dev)
3938{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003939 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003940 drm_i915_private_t *dev_priv = dev->dev_private;
3941
Chris Wilson69dc4982010-10-19 10:36:51 +01003942 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003943 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3944 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003945 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003946 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003947 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003948 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003949 for (i = 0; i < I915_NUM_RINGS; i++)
3950 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003951 for (i = 0; i < 16; i++)
3952 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003953 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3954 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003955 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003956
Dave Airlie94400122010-07-20 13:15:31 +10003957 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3958 if (IS_GEN3(dev)) {
3959 u32 tmp = I915_READ(MI_ARB_STATE);
3960 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3961 /* arb state is a masked write, so set bit + bit in mask */
3962 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3963 I915_WRITE(MI_ARB_STATE, tmp);
3964 }
3965 }
3966
Chris Wilson72bfa192010-12-19 11:42:05 +00003967 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3968
Jesse Barnesde151cf2008-11-12 10:03:55 -08003969 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003970 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3971 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003972
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003973 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003974 dev_priv->num_fence_regs = 16;
3975 else
3976 dev_priv->num_fence_regs = 8;
3977
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003978 /* Initialize fence registers to zero */
Eric Anholt10ed13e2011-05-06 13:53:49 -07003979 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3980 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003981 }
Eric Anholt10ed13e2011-05-06 13:53:49 -07003982
Eric Anholt673a3942008-07-30 12:06:12 -07003983 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003984 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01003985
Chris Wilsonce453d82011-02-21 14:43:56 +00003986 dev_priv->mm.interruptible = true;
3987
Chris Wilson17250b72010-10-28 12:51:39 +01003988 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3989 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3990 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07003991}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003992
3993/*
3994 * Create a physically contiguous memory object for this object
3995 * e.g. for cursor + overlay regs
3996 */
Chris Wilson995b6762010-08-20 13:23:26 +01003997static int i915_gem_init_phys_object(struct drm_device *dev,
3998 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003999{
4000 drm_i915_private_t *dev_priv = dev->dev_private;
4001 struct drm_i915_gem_phys_object *phys_obj;
4002 int ret;
4003
4004 if (dev_priv->mm.phys_objs[id - 1] || !size)
4005 return 0;
4006
Eric Anholt9a298b22009-03-24 12:23:04 -07004007 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004008 if (!phys_obj)
4009 return -ENOMEM;
4010
4011 phys_obj->id = id;
4012
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004013 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004014 if (!phys_obj->handle) {
4015 ret = -ENOMEM;
4016 goto kfree_obj;
4017 }
4018#ifdef CONFIG_X86
4019 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4020#endif
4021
4022 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4023
4024 return 0;
4025kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004026 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004027 return ret;
4028}
4029
Chris Wilson995b6762010-08-20 13:23:26 +01004030static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004031{
4032 drm_i915_private_t *dev_priv = dev->dev_private;
4033 struct drm_i915_gem_phys_object *phys_obj;
4034
4035 if (!dev_priv->mm.phys_objs[id - 1])
4036 return;
4037
4038 phys_obj = dev_priv->mm.phys_objs[id - 1];
4039 if (phys_obj->cur_obj) {
4040 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4041 }
4042
4043#ifdef CONFIG_X86
4044 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4045#endif
4046 drm_pci_free(dev, phys_obj->handle);
4047 kfree(phys_obj);
4048 dev_priv->mm.phys_objs[id - 1] = NULL;
4049}
4050
4051void i915_gem_free_all_phys_object(struct drm_device *dev)
4052{
4053 int i;
4054
Dave Airlie260883c2009-01-22 17:58:49 +10004055 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004056 i915_gem_free_phys_object(dev, i);
4057}
4058
4059void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004060 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004061{
Chris Wilson05394f32010-11-08 19:18:58 +00004062 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004063 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004064 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004065 int page_count;
4066
Chris Wilson05394f32010-11-08 19:18:58 +00004067 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004068 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004069 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004070
Chris Wilson05394f32010-11-08 19:18:58 +00004071 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004072 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004073 struct page *page = read_cache_page_gfp(mapping, i,
4074 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4075 if (!IS_ERR(page)) {
4076 char *dst = kmap_atomic(page);
4077 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4078 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004079
Chris Wilsone5281cc2010-10-28 13:45:36 +01004080 drm_clflush_pages(&page, 1);
4081
4082 set_page_dirty(page);
4083 mark_page_accessed(page);
4084 page_cache_release(page);
4085 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004086 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004087 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004088
Chris Wilson05394f32010-11-08 19:18:58 +00004089 obj->phys_obj->cur_obj = NULL;
4090 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004091}
4092
4093int
4094i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004095 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004096 int id,
4097 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004098{
Chris Wilson05394f32010-11-08 19:18:58 +00004099 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004100 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004101 int ret = 0;
4102 int page_count;
4103 int i;
4104
4105 if (id > I915_MAX_PHYS_OBJECT)
4106 return -EINVAL;
4107
Chris Wilson05394f32010-11-08 19:18:58 +00004108 if (obj->phys_obj) {
4109 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004110 return 0;
4111 i915_gem_detach_phys_object(dev, obj);
4112 }
4113
Dave Airlie71acb5e2008-12-30 20:31:46 +10004114 /* create a new object */
4115 if (!dev_priv->mm.phys_objs[id - 1]) {
4116 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004117 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004118 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004119 DRM_ERROR("failed to init phys object %d size: %zu\n",
4120 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004121 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004122 }
4123 }
4124
4125 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004126 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4127 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004128
Chris Wilson05394f32010-11-08 19:18:58 +00004129 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004130
4131 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004132 struct page *page;
4133 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004134
Chris Wilsone5281cc2010-10-28 13:45:36 +01004135 page = read_cache_page_gfp(mapping, i,
4136 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4137 if (IS_ERR(page))
4138 return PTR_ERR(page);
4139
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004140 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004141 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004142 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004143 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004144
4145 mark_page_accessed(page);
4146 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004147 }
4148
4149 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004150}
4151
4152static int
Chris Wilson05394f32010-11-08 19:18:58 +00004153i915_gem_phys_pwrite(struct drm_device *dev,
4154 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004155 struct drm_i915_gem_pwrite *args,
4156 struct drm_file *file_priv)
4157{
Chris Wilson05394f32010-11-08 19:18:58 +00004158 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004159 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004160
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004161 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4162 unsigned long unwritten;
4163
4164 /* The physical object once assigned is fixed for the lifetime
4165 * of the obj, so we can safely drop the lock and continue
4166 * to access vaddr.
4167 */
4168 mutex_unlock(&dev->struct_mutex);
4169 unwritten = copy_from_user(vaddr, user_data, args->size);
4170 mutex_lock(&dev->struct_mutex);
4171 if (unwritten)
4172 return -EFAULT;
4173 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004174
Daniel Vetter40ce6572010-11-05 18:12:18 +01004175 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004176 return 0;
4177}
Eric Anholtb9624422009-06-03 07:27:35 +00004178
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004179void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004180{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004181 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004182
4183 /* Clean up our request list when the client is going away, so that
4184 * later retire_requests won't dereference our soon-to-be-gone
4185 * file_priv.
4186 */
Chris Wilson1c255952010-09-26 11:03:27 +01004187 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004188 while (!list_empty(&file_priv->mm.request_list)) {
4189 struct drm_i915_gem_request *request;
4190
4191 request = list_first_entry(&file_priv->mm.request_list,
4192 struct drm_i915_gem_request,
4193 client_list);
4194 list_del(&request->client_list);
4195 request->file_priv = NULL;
4196 }
Chris Wilson1c255952010-09-26 11:03:27 +01004197 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004198}
Chris Wilson31169712009-09-14 16:50:28 +01004199
Chris Wilson31169712009-09-14 16:50:28 +01004200static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004201i915_gpu_is_active(struct drm_device *dev)
4202{
4203 drm_i915_private_t *dev_priv = dev->dev_private;
4204 int lists_empty;
4205
Chris Wilson1637ef42010-04-20 17:10:35 +01004206 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004207 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004208
4209 return !lists_empty;
4210}
4211
4212static int
Ying Han1495f232011-05-24 17:12:27 -07004213i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004214{
Chris Wilson17250b72010-10-28 12:51:39 +01004215 struct drm_i915_private *dev_priv =
4216 container_of(shrinker,
4217 struct drm_i915_private,
4218 mm.inactive_shrinker);
4219 struct drm_device *dev = dev_priv->dev;
4220 struct drm_i915_gem_object *obj, *next;
Ying Han1495f232011-05-24 17:12:27 -07004221 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01004222 int cnt;
4223
4224 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004225 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004226
4227 /* "fast-path" to count number of available objects */
4228 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01004229 cnt = 0;
4230 list_for_each_entry(obj,
4231 &dev_priv->mm.inactive_list,
4232 mm_list)
4233 cnt++;
4234 mutex_unlock(&dev->struct_mutex);
4235 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004236 }
4237
Chris Wilson1637ef42010-04-20 17:10:35 +01004238rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004239 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01004240 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01004241
Chris Wilson17250b72010-10-28 12:51:39 +01004242 list_for_each_entry_safe(obj, next,
4243 &dev_priv->mm.inactive_list,
4244 mm_list) {
4245 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00004246 if (i915_gem_object_unbind(obj) == 0 &&
4247 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004248 break;
Chris Wilson31169712009-09-14 16:50:28 +01004249 }
Chris Wilson31169712009-09-14 16:50:28 +01004250 }
4251
4252 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01004253 cnt = 0;
4254 list_for_each_entry_safe(obj, next,
4255 &dev_priv->mm.inactive_list,
4256 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00004257 if (nr_to_scan &&
4258 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004259 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00004260 else
Chris Wilson17250b72010-10-28 12:51:39 +01004261 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01004262 }
4263
Chris Wilson17250b72010-10-28 12:51:39 +01004264 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01004265 /*
4266 * We are desperate for pages, so as a last resort, wait
4267 * for the GPU to finish and discard whatever we can.
4268 * This has a dramatic impact to reduce the number of
4269 * OOM-killer events whilst running the GPU aggressively.
4270 */
Chris Wilson17250b72010-10-28 12:51:39 +01004271 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01004272 goto rescan;
4273 }
Chris Wilson17250b72010-10-28 12:51:39 +01004274 mutex_unlock(&dev->struct_mutex);
4275 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004276}