blob: 8b670e7ee4046b6668b755bffab62a1f42cbcf04 [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);
Jesper Juhlb65552f2011-06-12 20:53:44 +0000468 if (IS_ERR(page)) {
469 ret = PTR_ERR(page);
470 goto out;
471 }
Chris Wilsone5281cc2010-10-28 13:45:36 +0100472
Eric Anholt280b7132009-03-12 16:56:27 -0700473 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100474 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700475 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100476 user_pages[data_page_index],
477 data_page_offset,
478 page_length,
479 1);
480 } else {
481 slow_shmem_copy(user_pages[data_page_index],
482 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100483 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100484 shmem_page_offset,
485 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700486 }
Eric Anholteb014592009-03-10 11:44:52 -0700487
Chris Wilsone5281cc2010-10-28 13:45:36 +0100488 mark_page_accessed(page);
489 page_cache_release(page);
490
Eric Anholteb014592009-03-10 11:44:52 -0700491 remain -= page_length;
492 data_ptr += page_length;
493 offset += page_length;
494 }
495
Chris Wilson4f27b752010-10-14 15:26:45 +0100496out:
Eric Anholteb014592009-03-10 11:44:52 -0700497 for (i = 0; i < pinned_pages; i++) {
498 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100499 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700500 page_cache_release(user_pages[i]);
501 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700502 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700503
504 return ret;
505}
506
Eric Anholt673a3942008-07-30 12:06:12 -0700507/**
508 * Reads data from the object referenced by handle.
509 *
510 * On error, the contents of *data are undefined.
511 */
512int
513i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000514 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700515{
516 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000517 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100518 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700519
Chris Wilson51311d02010-11-17 09:10:42 +0000520 if (args->size == 0)
521 return 0;
522
523 if (!access_ok(VERIFY_WRITE,
524 (char __user *)(uintptr_t)args->data_ptr,
525 args->size))
526 return -EFAULT;
527
528 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
529 args->size);
530 if (ret)
531 return -EFAULT;
532
Chris Wilson4f27b752010-10-14 15:26:45 +0100533 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100534 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100535 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700536
Chris Wilson05394f32010-11-08 19:18:58 +0000537 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000538 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100539 ret = -ENOENT;
540 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100541 }
Eric Anholt673a3942008-07-30 12:06:12 -0700542
Chris Wilson7dcd2492010-09-26 20:21:44 +0100543 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000544 if (args->offset > obj->base.size ||
545 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100546 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100547 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100548 }
549
Chris Wilsondb53a302011-02-03 11:57:46 +0000550 trace_i915_gem_object_pread(obj, args->offset, args->size);
551
Chris Wilson4f27b752010-10-14 15:26:45 +0100552 ret = i915_gem_object_set_cpu_read_domain_range(obj,
553 args->offset,
554 args->size);
555 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100556 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100557
558 ret = -EFAULT;
559 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000560 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100561 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000562 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700563
Chris Wilson35b62a82010-09-26 20:23:38 +0100564out:
Chris Wilson05394f32010-11-08 19:18:58 +0000565 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100566unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100567 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700568 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700569}
570
Keith Packard0839ccb2008-10-30 19:38:48 -0700571/* This is the fast write path which cannot handle
572 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700573 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700574
Keith Packard0839ccb2008-10-30 19:38:48 -0700575static inline int
576fast_user_write(struct io_mapping *mapping,
577 loff_t page_base, int page_offset,
578 char __user *user_data,
579 int length)
580{
581 char *vaddr_atomic;
582 unsigned long unwritten;
583
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700584 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700585 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
586 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700587 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100588 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700589}
590
591/* Here's the write path which can sleep for
592 * page faults
593 */
594
Chris Wilsonab34c222010-05-27 14:15:35 +0100595static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700596slow_kernel_write(struct io_mapping *mapping,
597 loff_t gtt_base, int gtt_offset,
598 struct page *user_page, int user_offset,
599 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700600{
Chris Wilsonab34c222010-05-27 14:15:35 +0100601 char __iomem *dst_vaddr;
602 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700603
Chris Wilsonab34c222010-05-27 14:15:35 +0100604 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
605 src_vaddr = kmap(user_page);
606
607 memcpy_toio(dst_vaddr + gtt_offset,
608 src_vaddr + user_offset,
609 length);
610
611 kunmap(user_page);
612 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700613}
614
Eric Anholt3de09aa2009-03-09 09:42:23 -0700615/**
616 * This is the fast pwrite path, where we copy the data directly from the
617 * user into the GTT, uncached.
618 */
Eric Anholt673a3942008-07-30 12:06:12 -0700619static int
Chris Wilson05394f32010-11-08 19:18:58 +0000620i915_gem_gtt_pwrite_fast(struct drm_device *dev,
621 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700622 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000623 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700624{
Keith Packard0839ccb2008-10-30 19:38:48 -0700625 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700626 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700627 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700628 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700629 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700630
631 user_data = (char __user *) (uintptr_t) args->data_ptr;
632 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700633
Chris Wilson05394f32010-11-08 19:18:58 +0000634 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700635
636 while (remain > 0) {
637 /* Operation in this page
638 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700639 * page_base = page offset within aperture
640 * page_offset = offset within page
641 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700642 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100643 page_base = offset & PAGE_MASK;
644 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700645 page_length = remain;
646 if ((page_offset + remain) > PAGE_SIZE)
647 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700648
Keith Packard0839ccb2008-10-30 19:38:48 -0700649 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700650 * source page isn't available. Return the error and we'll
651 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700652 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100653 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
654 page_offset, user_data, page_length))
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100655 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700656
Keith Packard0839ccb2008-10-30 19:38:48 -0700657 remain -= page_length;
658 user_data += page_length;
659 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700660 }
Eric Anholt673a3942008-07-30 12:06:12 -0700661
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100662 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700663}
664
Eric Anholt3de09aa2009-03-09 09:42:23 -0700665/**
666 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
667 * the memory and maps it using kmap_atomic for copying.
668 *
669 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
670 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
671 */
Eric Anholt3043c602008-10-02 12:24:47 -0700672static int
Chris Wilson05394f32010-11-08 19:18:58 +0000673i915_gem_gtt_pwrite_slow(struct drm_device *dev,
674 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700675 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000676 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700677{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700678 drm_i915_private_t *dev_priv = dev->dev_private;
679 ssize_t remain;
680 loff_t gtt_page_base, offset;
681 loff_t first_data_page, last_data_page, num_pages;
682 loff_t pinned_pages, i;
683 struct page **user_pages;
684 struct mm_struct *mm = current->mm;
685 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700686 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700687 uint64_t data_ptr = args->data_ptr;
688
689 remain = args->size;
690
691 /* Pin the user pages containing the data. We can't fault while
692 * holding the struct mutex, and all of the pwrite implementations
693 * want to hold it while dereferencing the user data.
694 */
695 first_data_page = data_ptr / PAGE_SIZE;
696 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
697 num_pages = last_data_page - first_data_page + 1;
698
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100699 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700700 if (user_pages == NULL)
701 return -ENOMEM;
702
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100703 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700704 down_read(&mm->mmap_sem);
705 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
706 num_pages, 0, 0, user_pages, NULL);
707 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100708 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700709 if (pinned_pages < num_pages) {
710 ret = -EFAULT;
711 goto out_unpin_pages;
712 }
713
Chris Wilsond9e86c02010-11-10 16:40:20 +0000714 ret = i915_gem_object_set_to_gtt_domain(obj, true);
715 if (ret)
716 goto out_unpin_pages;
717
718 ret = i915_gem_object_put_fence(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700719 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100720 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700721
Chris Wilson05394f32010-11-08 19:18:58 +0000722 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700723
724 while (remain > 0) {
725 /* Operation in this page
726 *
727 * gtt_page_base = page offset within aperture
728 * gtt_page_offset = offset within page in aperture
729 * data_page_index = page number in get_user_pages return
730 * data_page_offset = offset with data_page_index page.
731 * page_length = bytes to copy for this page
732 */
733 gtt_page_base = offset & PAGE_MASK;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100734 gtt_page_offset = offset_in_page(offset);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700735 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100736 data_page_offset = offset_in_page(data_ptr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700737
738 page_length = remain;
739 if ((gtt_page_offset + page_length) > PAGE_SIZE)
740 page_length = PAGE_SIZE - gtt_page_offset;
741 if ((data_page_offset + page_length) > PAGE_SIZE)
742 page_length = PAGE_SIZE - data_page_offset;
743
Chris Wilsonab34c222010-05-27 14:15:35 +0100744 slow_kernel_write(dev_priv->mm.gtt_mapping,
745 gtt_page_base, gtt_page_offset,
746 user_pages[data_page_index],
747 data_page_offset,
748 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700749
750 remain -= page_length;
751 offset += page_length;
752 data_ptr += page_length;
753 }
754
Eric Anholt3de09aa2009-03-09 09:42:23 -0700755out_unpin_pages:
756 for (i = 0; i < pinned_pages; i++)
757 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700758 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700759
760 return ret;
761}
762
Eric Anholt40123c12009-03-09 13:42:30 -0700763/**
764 * This is the fast shmem pwrite path, which attempts to directly
765 * copy_from_user into the kmapped pages backing the object.
766 */
Eric Anholt673a3942008-07-30 12:06:12 -0700767static int
Chris Wilson05394f32010-11-08 19:18:58 +0000768i915_gem_shmem_pwrite_fast(struct drm_device *dev,
769 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700770 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000771 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700772{
Chris Wilson05394f32010-11-08 19:18:58 +0000773 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700774 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100775 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700776 char __user *user_data;
777 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700778
779 user_data = (char __user *) (uintptr_t) args->data_ptr;
780 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700781
Eric Anholt673a3942008-07-30 12:06:12 -0700782 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000783 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700784
Eric Anholt40123c12009-03-09 13:42:30 -0700785 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100786 struct page *page;
787 char *vaddr;
788 int ret;
789
Eric Anholt40123c12009-03-09 13:42:30 -0700790 /* Operation in this page
791 *
Eric Anholt40123c12009-03-09 13:42:30 -0700792 * page_offset = offset within page
793 * page_length = bytes to copy for this page
794 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100795 page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700796 page_length = remain;
797 if ((page_offset + remain) > PAGE_SIZE)
798 page_length = PAGE_SIZE - page_offset;
799
Chris Wilsone5281cc2010-10-28 13:45:36 +0100800 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
801 GFP_HIGHUSER | __GFP_RECLAIMABLE);
802 if (IS_ERR(page))
803 return PTR_ERR(page);
804
805 vaddr = kmap_atomic(page, KM_USER0);
806 ret = __copy_from_user_inatomic(vaddr + page_offset,
807 user_data,
808 page_length);
809 kunmap_atomic(vaddr, KM_USER0);
810
811 set_page_dirty(page);
812 mark_page_accessed(page);
813 page_cache_release(page);
814
815 /* If we get a fault while copying data, then (presumably) our
816 * source page isn't available. Return the error and we'll
817 * retry in the slow path.
818 */
819 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100820 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700821
822 remain -= page_length;
823 user_data += page_length;
824 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700825 }
826
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100827 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700828}
829
830/**
831 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
832 * the memory and maps it using kmap_atomic for copying.
833 *
834 * This avoids taking mmap_sem for faulting on the user's address while the
835 * struct_mutex is held.
836 */
837static int
Chris Wilson05394f32010-11-08 19:18:58 +0000838i915_gem_shmem_pwrite_slow(struct drm_device *dev,
839 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700840 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000841 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700842{
Chris Wilson05394f32010-11-08 19:18:58 +0000843 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700844 struct mm_struct *mm = current->mm;
845 struct page **user_pages;
846 ssize_t remain;
847 loff_t offset, pinned_pages, i;
848 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100849 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700850 int data_page_index, data_page_offset;
851 int page_length;
852 int ret;
853 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700854 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700855
856 remain = args->size;
857
858 /* Pin the user pages containing the data. We can't fault while
859 * holding the struct mutex, and all of the pwrite implementations
860 * want to hold it while dereferencing the user data.
861 */
862 first_data_page = data_ptr / PAGE_SIZE;
863 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
864 num_pages = last_data_page - first_data_page + 1;
865
Chris Wilson4f27b752010-10-14 15:26:45 +0100866 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700867 if (user_pages == NULL)
868 return -ENOMEM;
869
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100870 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700871 down_read(&mm->mmap_sem);
872 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
873 num_pages, 0, 0, user_pages, NULL);
874 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100875 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700876 if (pinned_pages < num_pages) {
877 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100878 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700879 }
880
Eric Anholt40123c12009-03-09 13:42:30 -0700881 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100882 if (ret)
883 goto out;
884
885 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700886
Eric Anholt40123c12009-03-09 13:42:30 -0700887 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000888 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700889
890 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100891 struct page *page;
892
Eric Anholt40123c12009-03-09 13:42:30 -0700893 /* Operation in this page
894 *
Eric Anholt40123c12009-03-09 13:42:30 -0700895 * shmem_page_offset = offset within page in shmem file
896 * data_page_index = page number in get_user_pages return
897 * data_page_offset = offset with data_page_index page.
898 * page_length = bytes to copy for this page
899 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100900 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700901 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100902 data_page_offset = offset_in_page(data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700903
904 page_length = remain;
905 if ((shmem_page_offset + page_length) > PAGE_SIZE)
906 page_length = PAGE_SIZE - shmem_page_offset;
907 if ((data_page_offset + page_length) > PAGE_SIZE)
908 page_length = PAGE_SIZE - data_page_offset;
909
Chris Wilsone5281cc2010-10-28 13:45:36 +0100910 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
911 GFP_HIGHUSER | __GFP_RECLAIMABLE);
912 if (IS_ERR(page)) {
913 ret = PTR_ERR(page);
914 goto out;
915 }
916
Eric Anholt280b7132009-03-12 16:56:27 -0700917 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100918 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700919 shmem_page_offset,
920 user_pages[data_page_index],
921 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100922 page_length,
923 0);
924 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100925 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100926 shmem_page_offset,
927 user_pages[data_page_index],
928 data_page_offset,
929 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700930 }
Eric Anholt40123c12009-03-09 13:42:30 -0700931
Chris Wilsone5281cc2010-10-28 13:45:36 +0100932 set_page_dirty(page);
933 mark_page_accessed(page);
934 page_cache_release(page);
935
Eric Anholt40123c12009-03-09 13:42:30 -0700936 remain -= page_length;
937 data_ptr += page_length;
938 offset += page_length;
939 }
940
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100941out:
Eric Anholt40123c12009-03-09 13:42:30 -0700942 for (i = 0; i < pinned_pages; i++)
943 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700944 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700945
946 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700947}
948
949/**
950 * Writes data to the object referenced by handle.
951 *
952 * On error, the contents of the buffer that were to be modified are undefined.
953 */
954int
955i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100956 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700957{
958 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000959 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000960 int ret;
961
962 if (args->size == 0)
963 return 0;
964
965 if (!access_ok(VERIFY_READ,
966 (char __user *)(uintptr_t)args->data_ptr,
967 args->size))
968 return -EFAULT;
969
970 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
971 args->size);
972 if (ret)
973 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700974
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100975 ret = i915_mutex_lock_interruptible(dev);
976 if (ret)
977 return ret;
978
Chris Wilson05394f32010-11-08 19:18:58 +0000979 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000980 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100981 ret = -ENOENT;
982 goto unlock;
983 }
Eric Anholt673a3942008-07-30 12:06:12 -0700984
Chris Wilson7dcd2492010-09-26 20:21:44 +0100985 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000986 if (args->offset > obj->base.size ||
987 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100988 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100989 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100990 }
991
Chris Wilsondb53a302011-02-03 11:57:46 +0000992 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
993
Eric Anholt673a3942008-07-30 12:06:12 -0700994 /* We can only do the GTT pwrite on untiled buffers, as otherwise
995 * it would end up going through the fenced access, and we'll get
996 * different detiling behavior between reading and writing.
997 * pread/pwrite currently are reading and writing from the CPU
998 * perspective, requiring manual detiling by the client.
999 */
Chris Wilson05394f32010-11-08 19:18:58 +00001000 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001001 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001002 else if (obj->gtt_space &&
Chris Wilson05394f32010-11-08 19:18:58 +00001003 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001004 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001005 if (ret)
1006 goto out;
1007
Chris Wilsond9e86c02010-11-10 16:40:20 +00001008 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1009 if (ret)
1010 goto out_unpin;
1011
1012 ret = i915_gem_object_put_fence(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001013 if (ret)
1014 goto out_unpin;
1015
1016 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1017 if (ret == -EFAULT)
1018 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1019
1020out_unpin:
1021 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001022 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001023 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1024 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001025 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001026
1027 ret = -EFAULT;
1028 if (!i915_gem_object_needs_bit17_swizzle(obj))
1029 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1030 if (ret == -EFAULT)
1031 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001032 }
Eric Anholt673a3942008-07-30 12:06:12 -07001033
Chris Wilson35b62a82010-09-26 20:23:38 +01001034out:
Chris Wilson05394f32010-11-08 19:18:58 +00001035 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001036unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001037 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001038 return ret;
1039}
1040
1041/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001042 * Called when user space prepares to use an object with the CPU, either
1043 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001044 */
1045int
1046i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001047 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001048{
1049 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001050 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001051 uint32_t read_domains = args->read_domains;
1052 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001053 int ret;
1054
1055 if (!(dev->driver->driver_features & DRIVER_GEM))
1056 return -ENODEV;
1057
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001058 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001059 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001060 return -EINVAL;
1061
Chris Wilson21d509e2009-06-06 09:46:02 +01001062 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001063 return -EINVAL;
1064
1065 /* Having something in the write domain implies it's in the read
1066 * domain, and only that read domain. Enforce that in the request.
1067 */
1068 if (write_domain != 0 && read_domains != write_domain)
1069 return -EINVAL;
1070
Chris Wilson76c1dec2010-09-25 11:22:51 +01001071 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001072 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001073 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001074
Chris Wilson05394f32010-11-08 19:18:58 +00001075 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001076 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001077 ret = -ENOENT;
1078 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001079 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001080
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001081 if (read_domains & I915_GEM_DOMAIN_GTT) {
1082 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001083
1084 /* Silently promote "you're not bound, there was nothing to do"
1085 * to success, since the client was just asking us to
1086 * make sure everything was done.
1087 */
1088 if (ret == -EINVAL)
1089 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001090 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001091 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001092 }
1093
Chris Wilson05394f32010-11-08 19:18:58 +00001094 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001095unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001096 mutex_unlock(&dev->struct_mutex);
1097 return ret;
1098}
1099
1100/**
1101 * Called when user space has done writes to this buffer
1102 */
1103int
1104i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001105 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001106{
1107 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001108 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001109 int ret = 0;
1110
1111 if (!(dev->driver->driver_features & DRIVER_GEM))
1112 return -ENODEV;
1113
Chris Wilson76c1dec2010-09-25 11:22:51 +01001114 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001115 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001116 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001117
Chris Wilson05394f32010-11-08 19:18:58 +00001118 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001119 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001120 ret = -ENOENT;
1121 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001122 }
1123
Eric Anholt673a3942008-07-30 12:06:12 -07001124 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001125 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001126 i915_gem_object_flush_cpu_write_domain(obj);
1127
Chris Wilson05394f32010-11-08 19:18:58 +00001128 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001129unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001130 mutex_unlock(&dev->struct_mutex);
1131 return ret;
1132}
1133
1134/**
1135 * Maps the contents of an object, returning the address it is mapped
1136 * into.
1137 *
1138 * While the mapping holds a reference on the contents of the object, it doesn't
1139 * imply a ref on the object itself.
1140 */
1141int
1142i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001143 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001144{
Chris Wilsonda761a62010-10-27 17:37:08 +01001145 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001146 struct drm_i915_gem_mmap *args = data;
1147 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001148 unsigned long addr;
1149
1150 if (!(dev->driver->driver_features & DRIVER_GEM))
1151 return -ENODEV;
1152
Chris Wilson05394f32010-11-08 19:18:58 +00001153 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001154 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001155 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001156
Chris Wilsonda761a62010-10-27 17:37:08 +01001157 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1158 drm_gem_object_unreference_unlocked(obj);
1159 return -E2BIG;
1160 }
1161
Eric Anholt673a3942008-07-30 12:06:12 -07001162 down_write(&current->mm->mmap_sem);
1163 addr = do_mmap(obj->filp, 0, args->size,
1164 PROT_READ | PROT_WRITE, MAP_SHARED,
1165 args->offset);
1166 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001167 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001168 if (IS_ERR((void *)addr))
1169 return addr;
1170
1171 args->addr_ptr = (uint64_t) addr;
1172
1173 return 0;
1174}
1175
Jesse Barnesde151cf2008-11-12 10:03:55 -08001176/**
1177 * i915_gem_fault - fault a page into the GTT
1178 * vma: VMA in question
1179 * vmf: fault info
1180 *
1181 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1182 * from userspace. The fault handler takes care of binding the object to
1183 * the GTT (if needed), allocating and programming a fence register (again,
1184 * only if needed based on whether the old reg is still valid or the object
1185 * is tiled) and inserting a new PTE into the faulting process.
1186 *
1187 * Note that the faulting process may involve evicting existing objects
1188 * from the GTT and/or fence registers to make room. So performance may
1189 * suffer if the GTT working set is large or there are few fence registers
1190 * left.
1191 */
1192int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1193{
Chris Wilson05394f32010-11-08 19:18:58 +00001194 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1195 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001196 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001197 pgoff_t page_offset;
1198 unsigned long pfn;
1199 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001200 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001201
1202 /* We don't use vmf->pgoff since that has the fake offset */
1203 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1204 PAGE_SHIFT;
1205
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001206 ret = i915_mutex_lock_interruptible(dev);
1207 if (ret)
1208 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001209
Chris Wilsondb53a302011-02-03 11:57:46 +00001210 trace_i915_gem_object_fault(obj, page_offset, true, write);
1211
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001212 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001213 if (!obj->map_and_fenceable) {
1214 ret = i915_gem_object_unbind(obj);
1215 if (ret)
1216 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001217 }
Chris Wilson05394f32010-11-08 19:18:58 +00001218 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001219 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001220 if (ret)
1221 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001222
Eric Anholte92d03b2011-06-14 16:43:09 -07001223 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1224 if (ret)
1225 goto unlock;
1226 }
Chris Wilson4a684a42010-10-28 14:44:08 +01001227
Chris Wilsond9e86c02010-11-10 16:40:20 +00001228 if (obj->tiling_mode == I915_TILING_NONE)
1229 ret = i915_gem_object_put_fence(obj);
1230 else
Chris Wilsonce453d82011-02-21 14:43:56 +00001231 ret = i915_gem_object_get_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001232 if (ret)
1233 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001234
Chris Wilson05394f32010-11-08 19:18:58 +00001235 if (i915_gem_object_is_inactive(obj))
1236 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001237
Chris Wilson6299f992010-11-24 12:23:44 +00001238 obj->fault_mappable = true;
1239
Chris Wilson05394f32010-11-08 19:18:58 +00001240 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001241 page_offset;
1242
1243 /* Finally, remap it using the new GTT offset */
1244 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001245unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001246 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001247out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001248 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001249 case -EIO:
Chris Wilson045e7692010-11-07 09:18:22 +00001250 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001251 /* Give the error handler a chance to run and move the
1252 * objects off the GPU active list. Next time we service the
1253 * fault, we should be able to transition the page into the
1254 * GTT without touching the GPU (and so avoid further
1255 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1256 * with coherency, just lost writes.
1257 */
Chris Wilson045e7692010-11-07 09:18:22 +00001258 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001259 case 0:
1260 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001261 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001262 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001264 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001265 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001266 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001267 }
1268}
1269
1270/**
1271 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1272 * @obj: obj in question
1273 *
1274 * GEM memory mapping works by handing back to userspace a fake mmap offset
1275 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1276 * up the object based on the offset and sets up the various memory mapping
1277 * structures.
1278 *
1279 * This routine allocates and attaches a fake offset for @obj.
1280 */
1281static int
Chris Wilson05394f32010-11-08 19:18:58 +00001282i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001283{
Chris Wilson05394f32010-11-08 19:18:58 +00001284 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001285 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001286 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001287 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001288 int ret = 0;
1289
1290 /* Set the object up for mmap'ing */
Chris Wilson05394f32010-11-08 19:18:58 +00001291 list = &obj->base.map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001292 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001293 if (!list->map)
1294 return -ENOMEM;
1295
1296 map = list->map;
1297 map->type = _DRM_GEM;
Chris Wilson05394f32010-11-08 19:18:58 +00001298 map->size = obj->base.size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001299 map->handle = obj;
1300
1301 /* Get a DRM GEM mmap offset allocated... */
1302 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
Chris Wilson05394f32010-11-08 19:18:58 +00001303 obj->base.size / PAGE_SIZE,
1304 0, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001305 if (!list->file_offset_node) {
Chris Wilson05394f32010-11-08 19:18:58 +00001306 DRM_ERROR("failed to allocate offset for bo %d\n",
1307 obj->base.name);
Chris Wilson9e0ae532010-09-21 15:05:24 +01001308 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001309 goto out_free_list;
1310 }
1311
1312 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
Chris Wilson05394f32010-11-08 19:18:58 +00001313 obj->base.size / PAGE_SIZE,
1314 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001315 if (!list->file_offset_node) {
1316 ret = -ENOMEM;
1317 goto out_free_list;
1318 }
1319
1320 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae532010-09-21 15:05:24 +01001321 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1322 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001323 DRM_ERROR("failed to add to map hash\n");
1324 goto out_free_mm;
1325 }
1326
Jesse Barnesde151cf2008-11-12 10:03:55 -08001327 return 0;
1328
1329out_free_mm:
1330 drm_mm_put_block(list->file_offset_node);
1331out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001332 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001333 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001334
1335 return ret;
1336}
1337
Chris Wilson901782b2009-07-10 08:18:50 +01001338/**
1339 * i915_gem_release_mmap - remove physical page mappings
1340 * @obj: obj in question
1341 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001342 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001343 * relinquish ownership of the pages back to the system.
1344 *
1345 * It is vital that we remove the page mapping if we have mapped a tiled
1346 * object through the GTT and then lose the fence register due to
1347 * resource pressure. Similarly if the object has been moved out of the
1348 * aperture, than pages mapped into userspace must be revoked. Removing the
1349 * mapping will then trigger a page fault on the next user access, allowing
1350 * fixup by i915_gem_fault().
1351 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001352void
Chris Wilson05394f32010-11-08 19:18:58 +00001353i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001354{
Chris Wilson6299f992010-11-24 12:23:44 +00001355 if (!obj->fault_mappable)
1356 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001357
Chris Wilsonf6e47882011-03-20 21:09:12 +00001358 if (obj->base.dev->dev_mapping)
1359 unmap_mapping_range(obj->base.dev->dev_mapping,
1360 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1361 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001362
Chris Wilson6299f992010-11-24 12:23:44 +00001363 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001364}
1365
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001366static void
Chris Wilson05394f32010-11-08 19:18:58 +00001367i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001368{
Chris Wilson05394f32010-11-08 19:18:58 +00001369 struct drm_device *dev = obj->base.dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001370 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001371 struct drm_map_list *list = &obj->base.map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001372
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001373 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001374 drm_mm_put_block(list->file_offset_node);
1375 kfree(list->map);
1376 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001377}
1378
Chris Wilson92b88ae2010-11-09 11:47:32 +00001379static uint32_t
1380i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1381{
1382 struct drm_device *dev = obj->base.dev;
1383 uint32_t size;
1384
1385 if (INTEL_INFO(dev)->gen >= 4 ||
1386 obj->tiling_mode == I915_TILING_NONE)
1387 return obj->base.size;
1388
1389 /* Previous chips need a power-of-two fence region when tiling */
1390 if (INTEL_INFO(dev)->gen == 3)
1391 size = 1024*1024;
1392 else
1393 size = 512*1024;
1394
1395 while (size < obj->base.size)
1396 size <<= 1;
1397
1398 return size;
1399}
1400
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401/**
1402 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1403 * @obj: object to check
1404 *
1405 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001406 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001407 */
1408static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001409i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001410{
Chris Wilson05394f32010-11-08 19:18:58 +00001411 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001412
1413 /*
1414 * Minimum alignment is 4k (GTT page size), but might be greater
1415 * if a fence register is needed for the object.
1416 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001417 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilson05394f32010-11-08 19:18:58 +00001418 obj->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001419 return 4096;
1420
1421 /*
1422 * Previous chips need to be aligned to the size of the smallest
1423 * fence register that can contain the object.
1424 */
Chris Wilson05394f32010-11-08 19:18:58 +00001425 return i915_gem_get_gtt_size(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001426}
1427
Daniel Vetter5e783302010-11-14 22:32:36 +01001428/**
1429 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1430 * unfenced object
1431 * @obj: object to check
1432 *
1433 * Return the required GTT alignment for an object, only taking into account
1434 * unfenced tiled surface requirements.
1435 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001436uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001437i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
Daniel Vetter5e783302010-11-14 22:32:36 +01001438{
Chris Wilson05394f32010-11-08 19:18:58 +00001439 struct drm_device *dev = obj->base.dev;
Daniel Vetter5e783302010-11-14 22:32:36 +01001440 int tile_height;
1441
1442 /*
1443 * Minimum alignment is 4k (GTT page size) for sane hw.
1444 */
1445 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001446 obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001447 return 4096;
1448
1449 /*
1450 * Older chips need unfenced tiled buffers to be aligned to the left
1451 * edge of an even tile row (where tile rows are counted as if the bo is
1452 * placed in a fenced gtt region).
1453 */
Daniel Vetterc8ebc2b2011-05-12 22:17:20 +01001454 if (IS_GEN2(dev))
1455 tile_height = 16;
1456 else if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Daniel Vetter5e783302010-11-14 22:32:36 +01001457 tile_height = 32;
1458 else
1459 tile_height = 8;
1460
Chris Wilson05394f32010-11-08 19:18:58 +00001461 return tile_height * obj->stride * 2;
Daniel Vetter5e783302010-11-14 22:32:36 +01001462}
1463
Jesse Barnesde151cf2008-11-12 10:03:55 -08001464int
Dave Airlieff72145b2011-02-07 12:16:14 +10001465i915_gem_mmap_gtt(struct drm_file *file,
1466 struct drm_device *dev,
1467 uint32_t handle,
1468 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001469{
Chris Wilsonda761a62010-10-27 17:37:08 +01001470 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001471 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001472 int ret;
1473
1474 if (!(dev->driver->driver_features & DRIVER_GEM))
1475 return -ENODEV;
1476
Chris Wilson76c1dec2010-09-25 11:22:51 +01001477 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001478 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001479 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001480
Dave Airlieff72145b2011-02-07 12:16:14 +10001481 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001482 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001483 ret = -ENOENT;
1484 goto unlock;
1485 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001486
Chris Wilson05394f32010-11-08 19:18:58 +00001487 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001488 ret = -E2BIG;
1489 goto unlock;
1490 }
1491
Chris Wilson05394f32010-11-08 19:18:58 +00001492 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001493 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001494 ret = -EINVAL;
1495 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001496 }
1497
Chris Wilson05394f32010-11-08 19:18:58 +00001498 if (!obj->base.map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001499 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001500 if (ret)
1501 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001502 }
1503
Dave Airlieff72145b2011-02-07 12:16:14 +10001504 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001505
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001506out:
Chris Wilson05394f32010-11-08 19:18:58 +00001507 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001508unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001509 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001510 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001511}
1512
Dave Airlieff72145b2011-02-07 12:16:14 +10001513/**
1514 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1515 * @dev: DRM device
1516 * @data: GTT mapping ioctl data
1517 * @file: GEM object info
1518 *
1519 * Simply returns the fake offset to userspace so it can mmap it.
1520 * The mmap call will end up in drm_gem_mmap(), which will set things
1521 * up so we can get faults in the handler above.
1522 *
1523 * The fault handler will take care of binding the object into the GTT
1524 * (since it may have been evicted to make room for something), allocating
1525 * a fence register, and mapping the appropriate aperture address into
1526 * userspace.
1527 */
1528int
1529i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1530 struct drm_file *file)
1531{
1532 struct drm_i915_gem_mmap_gtt *args = data;
1533
1534 if (!(dev->driver->driver_features & DRIVER_GEM))
1535 return -ENODEV;
1536
1537 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1538}
1539
1540
Chris Wilsone5281cc2010-10-28 13:45:36 +01001541static int
Chris Wilson05394f32010-11-08 19:18:58 +00001542i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001543 gfp_t gfpmask)
1544{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001545 int page_count, i;
1546 struct address_space *mapping;
1547 struct inode *inode;
1548 struct page *page;
1549
1550 /* Get the list of pages out of our struct file. They'll be pinned
1551 * at this point until we release them.
1552 */
Chris Wilson05394f32010-11-08 19:18:58 +00001553 page_count = obj->base.size / PAGE_SIZE;
1554 BUG_ON(obj->pages != NULL);
1555 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1556 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001557 return -ENOMEM;
1558
Chris Wilson05394f32010-11-08 19:18:58 +00001559 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001560 mapping = inode->i_mapping;
1561 for (i = 0; i < page_count; i++) {
1562 page = read_cache_page_gfp(mapping, i,
1563 GFP_HIGHUSER |
1564 __GFP_COLD |
1565 __GFP_RECLAIMABLE |
1566 gfpmask);
1567 if (IS_ERR(page))
1568 goto err_pages;
1569
Chris Wilson05394f32010-11-08 19:18:58 +00001570 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001571 }
1572
Chris Wilson05394f32010-11-08 19:18:58 +00001573 if (obj->tiling_mode != I915_TILING_NONE)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001574 i915_gem_object_do_bit_17_swizzle(obj);
1575
1576 return 0;
1577
1578err_pages:
1579 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001580 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001581
Chris Wilson05394f32010-11-08 19:18:58 +00001582 drm_free_large(obj->pages);
1583 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001584 return PTR_ERR(page);
1585}
1586
Chris Wilson5cdf5882010-09-27 15:51:07 +01001587static void
Chris Wilson05394f32010-11-08 19:18:58 +00001588i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001589{
Chris Wilson05394f32010-11-08 19:18:58 +00001590 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001591 int i;
1592
Chris Wilson05394f32010-11-08 19:18:58 +00001593 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001594
Chris Wilson05394f32010-11-08 19:18:58 +00001595 if (obj->tiling_mode != I915_TILING_NONE)
Eric Anholt280b7132009-03-12 16:56:27 -07001596 i915_gem_object_save_bit_17_swizzle(obj);
1597
Chris Wilson05394f32010-11-08 19:18:58 +00001598 if (obj->madv == I915_MADV_DONTNEED)
1599 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001600
1601 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001602 if (obj->dirty)
1603 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001604
Chris Wilson05394f32010-11-08 19:18:58 +00001605 if (obj->madv == I915_MADV_WILLNEED)
1606 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001607
Chris Wilson05394f32010-11-08 19:18:58 +00001608 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001609 }
Chris Wilson05394f32010-11-08 19:18:58 +00001610 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001611
Chris Wilson05394f32010-11-08 19:18:58 +00001612 drm_free_large(obj->pages);
1613 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001614}
1615
Chris Wilson54cf91d2010-11-25 18:00:26 +00001616void
Chris Wilson05394f32010-11-08 19:18:58 +00001617i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001618 struct intel_ring_buffer *ring,
1619 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001620{
Chris Wilson05394f32010-11-08 19:18:58 +00001621 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001622 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001623
Zou Nan hai852835f2010-05-21 09:08:56 +08001624 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001625 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001626
1627 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001628 if (!obj->active) {
1629 drm_gem_object_reference(&obj->base);
1630 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001631 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001632
Eric Anholt673a3942008-07-30 12:06:12 -07001633 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001634 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1635 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001636
Chris Wilson05394f32010-11-08 19:18:58 +00001637 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001638 if (obj->fenced_gpu_access) {
1639 struct drm_i915_fence_reg *reg;
1640
1641 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1642
1643 obj->last_fenced_seqno = seqno;
1644 obj->last_fenced_ring = ring;
1645
1646 reg = &dev_priv->fence_regs[obj->fence_reg];
1647 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1648 }
1649}
1650
1651static void
1652i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1653{
1654 list_del_init(&obj->ring_list);
1655 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001656}
1657
Eric Anholtce44b0e2008-11-06 16:00:31 -08001658static void
Chris Wilson05394f32010-11-08 19:18:58 +00001659i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001660{
Chris Wilson05394f32010-11-08 19:18:58 +00001661 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001662 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001663
Chris Wilson05394f32010-11-08 19:18:58 +00001664 BUG_ON(!obj->active);
1665 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001666
1667 i915_gem_object_move_off_active(obj);
1668}
1669
1670static void
1671i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1672{
1673 struct drm_device *dev = obj->base.dev;
1674 struct drm_i915_private *dev_priv = dev->dev_private;
1675
1676 if (obj->pin_count != 0)
1677 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1678 else
1679 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1680
1681 BUG_ON(!list_empty(&obj->gpu_write_list));
1682 BUG_ON(!obj->active);
1683 obj->ring = NULL;
1684
1685 i915_gem_object_move_off_active(obj);
1686 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001687
1688 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001689 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001690 drm_gem_object_unreference(&obj->base);
1691
1692 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001693}
Eric Anholt673a3942008-07-30 12:06:12 -07001694
Chris Wilson963b4832009-09-20 23:03:54 +01001695/* Immediately discard the backing storage */
1696static void
Chris Wilson05394f32010-11-08 19:18:58 +00001697i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001698{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001699 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001700
Chris Wilsonae9fed62010-08-07 11:01:30 +01001701 /* Our goal here is to return as much of the memory as
1702 * is possible back to the system as we are called from OOM.
1703 * To do this we must instruct the shmfs to drop all of its
1704 * backing pages, *now*. Here we mirror the actions taken
1705 * when by shmem_delete_inode() to release the backing store.
1706 */
Chris Wilson05394f32010-11-08 19:18:58 +00001707 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001708 truncate_inode_pages(inode->i_mapping, 0);
1709 if (inode->i_op->truncate_range)
1710 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001711
Chris Wilson05394f32010-11-08 19:18:58 +00001712 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001713}
1714
1715static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001716i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001717{
Chris Wilson05394f32010-11-08 19:18:58 +00001718 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001719}
1720
Eric Anholt673a3942008-07-30 12:06:12 -07001721static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001722i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1723 uint32_t flush_domains)
Daniel Vetter63560392010-02-19 11:51:59 +01001724{
Chris Wilson05394f32010-11-08 19:18:58 +00001725 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001726
Chris Wilson05394f32010-11-08 19:18:58 +00001727 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001728 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001729 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001730 if (obj->base.write_domain & flush_domains) {
1731 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001732
Chris Wilson05394f32010-11-08 19:18:58 +00001733 obj->base.write_domain = 0;
1734 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001735 i915_gem_object_move_to_active(obj, ring,
Chris Wilsondb53a302011-02-03 11:57:46 +00001736 i915_gem_next_request_seqno(ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001737
Daniel Vetter63560392010-02-19 11:51:59 +01001738 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001739 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001740 old_write_domain);
1741 }
1742 }
1743}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001744
Chris Wilson3cce4692010-10-27 16:11:02 +01001745int
Chris Wilsondb53a302011-02-03 11:57:46 +00001746i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001747 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001748 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001749{
Chris Wilsondb53a302011-02-03 11:57:46 +00001750 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001751 uint32_t seqno;
1752 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001753 int ret;
1754
1755 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001756
Chris Wilson3cce4692010-10-27 16:11:02 +01001757 ret = ring->add_request(ring, &seqno);
1758 if (ret)
1759 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001760
Chris Wilsondb53a302011-02-03 11:57:46 +00001761 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001762
1763 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001764 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001765 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001766 was_empty = list_empty(&ring->request_list);
1767 list_add_tail(&request->list, &ring->request_list);
1768
Chris Wilsondb53a302011-02-03 11:57:46 +00001769 if (file) {
1770 struct drm_i915_file_private *file_priv = file->driver_priv;
1771
Chris Wilson1c255952010-09-26 11:03:27 +01001772 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001773 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001774 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001775 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001776 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001777 }
Eric Anholt673a3942008-07-30 12:06:12 -07001778
Chris Wilsondb53a302011-02-03 11:57:46 +00001779 ring->outstanding_lazy_request = false;
1780
Ben Gamarif65d9422009-09-14 17:48:44 -04001781 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001782 if (i915_enable_hangcheck) {
1783 mod_timer(&dev_priv->hangcheck_timer,
1784 jiffies +
1785 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1786 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001787 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001788 queue_delayed_work(dev_priv->wq,
1789 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001790 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001791 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001792}
1793
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001794static inline void
1795i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001796{
Chris Wilson1c255952010-09-26 11:03:27 +01001797 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001798
Chris Wilson1c255952010-09-26 11:03:27 +01001799 if (!file_priv)
1800 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001801
Chris Wilson1c255952010-09-26 11:03:27 +01001802 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001803 if (request->file_priv) {
1804 list_del(&request->client_list);
1805 request->file_priv = NULL;
1806 }
Chris Wilson1c255952010-09-26 11:03:27 +01001807 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001808}
1809
Chris Wilsondfaae392010-09-22 10:31:52 +01001810static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1811 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001812{
Chris Wilsondfaae392010-09-22 10:31:52 +01001813 while (!list_empty(&ring->request_list)) {
1814 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001815
Chris Wilsondfaae392010-09-22 10:31:52 +01001816 request = list_first_entry(&ring->request_list,
1817 struct drm_i915_gem_request,
1818 list);
1819
1820 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001821 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001822 kfree(request);
1823 }
1824
1825 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001826 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001827
Chris Wilson05394f32010-11-08 19:18:58 +00001828 obj = list_first_entry(&ring->active_list,
1829 struct drm_i915_gem_object,
1830 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001831
Chris Wilson05394f32010-11-08 19:18:58 +00001832 obj->base.write_domain = 0;
1833 list_del_init(&obj->gpu_write_list);
1834 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001835 }
Eric Anholt673a3942008-07-30 12:06:12 -07001836}
1837
Chris Wilson312817a2010-11-22 11:50:11 +00001838static void i915_gem_reset_fences(struct drm_device *dev)
1839{
1840 struct drm_i915_private *dev_priv = dev->dev_private;
1841 int i;
1842
1843 for (i = 0; i < 16; i++) {
1844 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001845 struct drm_i915_gem_object *obj = reg->obj;
1846
1847 if (!obj)
1848 continue;
1849
1850 if (obj->tiling_mode)
1851 i915_gem_release_mmap(obj);
1852
Chris Wilsond9e86c02010-11-10 16:40:20 +00001853 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1854 reg->obj->fenced_gpu_access = false;
1855 reg->obj->last_fenced_seqno = 0;
1856 reg->obj->last_fenced_ring = NULL;
1857 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001858 }
1859}
1860
Chris Wilson069efc12010-09-30 16:53:18 +01001861void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001862{
Chris Wilsondfaae392010-09-22 10:31:52 +01001863 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001864 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001865 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001866
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001867 for (i = 0; i < I915_NUM_RINGS; i++)
1868 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001869
1870 /* Remove anything from the flushing lists. The GPU cache is likely
1871 * to be lost on reset along with the data, so simply move the
1872 * lost bo to the inactive list.
1873 */
1874 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001875 obj= list_first_entry(&dev_priv->mm.flushing_list,
1876 struct drm_i915_gem_object,
1877 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001878
Chris Wilson05394f32010-11-08 19:18:58 +00001879 obj->base.write_domain = 0;
1880 list_del_init(&obj->gpu_write_list);
1881 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001882 }
Chris Wilson9375e442010-09-19 12:21:28 +01001883
Chris Wilsondfaae392010-09-22 10:31:52 +01001884 /* Move everything out of the GPU domains to ensure we do any
1885 * necessary invalidation upon reuse.
1886 */
Chris Wilson05394f32010-11-08 19:18:58 +00001887 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001888 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001889 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001890 {
Chris Wilson05394f32010-11-08 19:18:58 +00001891 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001892 }
Chris Wilson069efc12010-09-30 16:53:18 +01001893
1894 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001895 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001896}
1897
1898/**
1899 * This function clears the request list as sequence numbers are passed.
1900 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001901static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001902i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001903{
Eric Anholt673a3942008-07-30 12:06:12 -07001904 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001905 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001906
Chris Wilsondb53a302011-02-03 11:57:46 +00001907 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001908 return;
1909
Chris Wilsondb53a302011-02-03 11:57:46 +00001910 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001911
Chris Wilson78501ea2010-10-27 12:18:21 +01001912 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001913
Chris Wilson076e2c02011-01-21 10:07:18 +00001914 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001915 if (seqno >= ring->sync_seqno[i])
1916 ring->sync_seqno[i] = 0;
1917
Zou Nan hai852835f2010-05-21 09:08:56 +08001918 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001919 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001920
Zou Nan hai852835f2010-05-21 09:08:56 +08001921 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001922 struct drm_i915_gem_request,
1923 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001924
Chris Wilsondfaae392010-09-22 10:31:52 +01001925 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001926 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001927
Chris Wilsondb53a302011-02-03 11:57:46 +00001928 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001929
1930 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001931 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001932 kfree(request);
1933 }
1934
1935 /* Move any buffers on the active list that are no longer referenced
1936 * by the ringbuffer to the flushing/inactive lists as appropriate.
1937 */
1938 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001939 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001940
Chris Wilson05394f32010-11-08 19:18:58 +00001941 obj= list_first_entry(&ring->active_list,
1942 struct drm_i915_gem_object,
1943 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001944
Chris Wilson05394f32010-11-08 19:18:58 +00001945 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001946 break;
1947
Chris Wilson05394f32010-11-08 19:18:58 +00001948 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001949 i915_gem_object_move_to_flushing(obj);
1950 else
1951 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001952 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001953
Chris Wilsondb53a302011-02-03 11:57:46 +00001954 if (unlikely(ring->trace_irq_seqno &&
1955 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001956 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001957 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001958 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001959
Chris Wilsondb53a302011-02-03 11:57:46 +00001960 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001961}
1962
1963void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001964i915_gem_retire_requests(struct drm_device *dev)
1965{
1966 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001967 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001968
Chris Wilsonbe726152010-07-23 23:18:50 +01001969 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001970 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001971
1972 /* We must be careful that during unbind() we do not
1973 * accidentally infinitely recurse into retire requests.
1974 * Currently:
1975 * retire -> free -> unbind -> wait -> retire_ring
1976 */
Chris Wilson05394f32010-11-08 19:18:58 +00001977 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001978 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001979 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001980 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001981 }
1982
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001983 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilsondb53a302011-02-03 11:57:46 +00001984 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001985}
1986
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001987static void
Eric Anholt673a3942008-07-30 12:06:12 -07001988i915_gem_retire_work_handler(struct work_struct *work)
1989{
1990 drm_i915_private_t *dev_priv;
1991 struct drm_device *dev;
Chris Wilson0a587052011-01-09 21:05:44 +00001992 bool idle;
1993 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001994
1995 dev_priv = container_of(work, drm_i915_private_t,
1996 mm.retire_work.work);
1997 dev = dev_priv->dev;
1998
Chris Wilson891b48c2010-09-29 12:26:37 +01001999 /* Come back later if the device is busy... */
2000 if (!mutex_trylock(&dev->struct_mutex)) {
2001 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2002 return;
2003 }
2004
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002005 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002006
Chris Wilson0a587052011-01-09 21:05:44 +00002007 /* Send a periodic flush down the ring so we don't hold onto GEM
2008 * objects indefinitely.
2009 */
2010 idle = true;
2011 for (i = 0; i < I915_NUM_RINGS; i++) {
2012 struct intel_ring_buffer *ring = &dev_priv->ring[i];
2013
2014 if (!list_empty(&ring->gpu_write_list)) {
2015 struct drm_i915_gem_request *request;
2016 int ret;
2017
Chris Wilsondb53a302011-02-03 11:57:46 +00002018 ret = i915_gem_flush_ring(ring,
2019 0, I915_GEM_GPU_DOMAINS);
Chris Wilson0a587052011-01-09 21:05:44 +00002020 request = kzalloc(sizeof(*request), GFP_KERNEL);
2021 if (ret || request == NULL ||
Chris Wilsondb53a302011-02-03 11:57:46 +00002022 i915_add_request(ring, NULL, request))
Chris Wilson0a587052011-01-09 21:05:44 +00002023 kfree(request);
2024 }
2025
2026 idle &= list_empty(&ring->request_list);
2027 }
2028
2029 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002030 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilson0a587052011-01-09 21:05:44 +00002031
Eric Anholt673a3942008-07-30 12:06:12 -07002032 mutex_unlock(&dev->struct_mutex);
2033}
2034
Chris Wilsondb53a302011-02-03 11:57:46 +00002035/**
2036 * Waits for a sequence number to be signaled, and cleans up the
2037 * request and object lists appropriately for that event.
2038 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002039int
Chris Wilsondb53a302011-02-03 11:57:46 +00002040i915_wait_request(struct intel_ring_buffer *ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002041 uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07002042{
Chris Wilsondb53a302011-02-03 11:57:46 +00002043 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002044 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002045 int ret = 0;
2046
2047 BUG_ON(seqno == 0);
2048
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002049 if (atomic_read(&dev_priv->mm.wedged)) {
2050 struct completion *x = &dev_priv->error_completion;
2051 bool recovery_complete;
2052 unsigned long flags;
2053
2054 /* Give the error handler a chance to run. */
2055 spin_lock_irqsave(&x->wait.lock, flags);
2056 recovery_complete = x->done > 0;
2057 spin_unlock_irqrestore(&x->wait.lock, flags);
2058
2059 return recovery_complete ? -EIO : -EAGAIN;
2060 }
Ben Gamariffed1d02009-09-14 17:48:41 -04002061
Chris Wilson5d97eb62010-11-10 20:40:02 +00002062 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002063 struct drm_i915_gem_request *request;
2064
2065 request = kzalloc(sizeof(*request), GFP_KERNEL);
2066 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002067 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002068
Chris Wilsondb53a302011-02-03 11:57:46 +00002069 ret = i915_add_request(ring, NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +01002070 if (ret) {
2071 kfree(request);
2072 return ret;
2073 }
2074
2075 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002076 }
2077
Chris Wilson78501ea2010-10-27 12:18:21 +01002078 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002079 if (HAS_PCH_SPLIT(ring->dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002080 ier = I915_READ(DEIER) | I915_READ(GTIER);
2081 else
2082 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002083 if (!ier) {
2084 DRM_ERROR("something (likely vbetool) disabled "
2085 "interrupts, re-enabling\n");
Chris Wilsonf01c22f2011-06-28 11:48:51 +01002086 ring->dev->driver->irq_preinstall(ring->dev);
2087 ring->dev->driver->irq_postinstall(ring->dev);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002088 }
2089
Chris Wilsondb53a302011-02-03 11:57:46 +00002090 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002091
Chris Wilsonb2223492010-10-27 15:27:33 +01002092 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002093 if (ring->irq_get(ring)) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002094 if (dev_priv->mm.interruptible)
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002095 ret = wait_event_interruptible(ring->irq_queue,
2096 i915_seqno_passed(ring->get_seqno(ring), seqno)
2097 || atomic_read(&dev_priv->mm.wedged));
2098 else
2099 wait_event(ring->irq_queue,
2100 i915_seqno_passed(ring->get_seqno(ring), seqno)
2101 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002102
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002103 ring->irq_put(ring);
Chris Wilsonb5ba1772010-12-14 12:17:15 +00002104 } else if (wait_for(i915_seqno_passed(ring->get_seqno(ring),
2105 seqno) ||
2106 atomic_read(&dev_priv->mm.wedged), 3000))
2107 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01002108 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002109
Chris Wilsondb53a302011-02-03 11:57:46 +00002110 trace_i915_gem_request_wait_end(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002111 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002112 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002113 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002114
2115 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002116 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002117 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002118 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002119
2120 /* Directly dispatch request retiring. While we have the work queue
2121 * to handle this, the waiter on a request often wants an associated
2122 * buffer to have made it to the inactive list, and we would need
2123 * a separate wait queue to handle that.
2124 */
2125 if (ret == 0)
Chris Wilsondb53a302011-02-03 11:57:46 +00002126 i915_gem_retire_requests_ring(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002127
2128 return ret;
2129}
2130
Daniel Vetter48764bf2009-09-15 22:57:32 +02002131/**
Eric Anholt673a3942008-07-30 12:06:12 -07002132 * Ensures that all rendering to the object has completed and the object is
2133 * safe to unbind from the GTT or access from the CPU.
2134 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00002135int
Chris Wilsonce453d82011-02-21 14:43:56 +00002136i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002137{
Eric Anholt673a3942008-07-30 12:06:12 -07002138 int ret;
2139
Eric Anholte47c68e2008-11-14 13:35:19 -08002140 /* This function only exists to support waiting for existing rendering,
2141 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002142 */
Chris Wilson05394f32010-11-08 19:18:58 +00002143 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002144
2145 /* If there is rendering queued on the buffer being evicted, wait for
2146 * it.
2147 */
Chris Wilson05394f32010-11-08 19:18:58 +00002148 if (obj->active) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002149 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002150 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002151 return ret;
2152 }
2153
2154 return 0;
2155}
2156
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002157static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2158{
2159 u32 old_write_domain, old_read_domains;
2160
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002161 /* Act a barrier for all accesses through the GTT */
2162 mb();
2163
2164 /* Force a pagefault for domain tracking on next user access */
2165 i915_gem_release_mmap(obj);
2166
Keith Packardb97c3d92011-06-24 21:02:59 -07002167 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2168 return;
2169
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002170 old_read_domains = obj->base.read_domains;
2171 old_write_domain = obj->base.write_domain;
2172
2173 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2174 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2175
2176 trace_i915_gem_object_change_domain(obj,
2177 old_read_domains,
2178 old_write_domain);
2179}
2180
Eric Anholt673a3942008-07-30 12:06:12 -07002181/**
2182 * Unbinds an object from the GTT aperture.
2183 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002184int
Chris Wilson05394f32010-11-08 19:18:58 +00002185i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002186{
Eric Anholt673a3942008-07-30 12:06:12 -07002187 int ret = 0;
2188
Chris Wilson05394f32010-11-08 19:18:58 +00002189 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002190 return 0;
2191
Chris Wilson05394f32010-11-08 19:18:58 +00002192 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002193 DRM_ERROR("Attempting to unbind pinned buffer\n");
2194 return -EINVAL;
2195 }
2196
Chris Wilsona8198ee2011-04-13 22:04:09 +01002197 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson8dc17752010-07-23 23:18:51 +01002198 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002199 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002200 /* Continue on if we fail due to EIO, the GPU is hung so we
2201 * should be safe and we need to cleanup or else we might
2202 * cause memory corruption through use-after-free.
2203 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002204
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002205 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002206
2207 /* Move the object to the CPU domain to ensure that
2208 * any possible CPU writes while it's not in the GTT
2209 * are flushed when we go to remap it.
2210 */
2211 if (ret == 0)
2212 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2213 if (ret == -ERESTARTSYS)
2214 return ret;
Chris Wilson812ed492010-09-30 15:08:57 +01002215 if (ret) {
Chris Wilsona8198ee2011-04-13 22:04:09 +01002216 /* In the event of a disaster, abandon all caches and
2217 * hope for the best.
2218 */
Chris Wilson812ed492010-09-30 15:08:57 +01002219 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002220 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed492010-09-30 15:08:57 +01002221 }
Eric Anholt673a3942008-07-30 12:06:12 -07002222
Daniel Vetter96b47b62009-12-15 17:50:00 +01002223 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002224 ret = i915_gem_object_put_fence(obj);
2225 if (ret == -ERESTARTSYS)
2226 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002227
Chris Wilsondb53a302011-02-03 11:57:46 +00002228 trace_i915_gem_object_unbind(obj);
2229
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002230 i915_gem_gtt_unbind_object(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002231 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002232
Chris Wilson6299f992010-11-24 12:23:44 +00002233 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002234 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002235 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002236 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002237
Chris Wilson05394f32010-11-08 19:18:58 +00002238 drm_mm_put_block(obj->gtt_space);
2239 obj->gtt_space = NULL;
2240 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002241
Chris Wilson05394f32010-11-08 19:18:58 +00002242 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002243 i915_gem_object_truncate(obj);
2244
Chris Wilson8dc17752010-07-23 23:18:51 +01002245 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002246}
2247
Chris Wilson88241782011-01-07 17:09:48 +00002248int
Chris Wilsondb53a302011-02-03 11:57:46 +00002249i915_gem_flush_ring(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002250 uint32_t invalidate_domains,
2251 uint32_t flush_domains)
2252{
Chris Wilson88241782011-01-07 17:09:48 +00002253 int ret;
2254
Chris Wilson36d527d2011-03-19 22:26:49 +00002255 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2256 return 0;
2257
Chris Wilsondb53a302011-02-03 11:57:46 +00002258 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2259
Chris Wilson88241782011-01-07 17:09:48 +00002260 ret = ring->flush(ring, invalidate_domains, flush_domains);
2261 if (ret)
2262 return ret;
2263
Chris Wilson36d527d2011-03-19 22:26:49 +00002264 if (flush_domains & I915_GEM_GPU_DOMAINS)
2265 i915_gem_process_flushing_list(ring, flush_domains);
2266
Chris Wilson88241782011-01-07 17:09:48 +00002267 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002268}
2269
Chris Wilsondb53a302011-02-03 11:57:46 +00002270static int i915_ring_idle(struct intel_ring_buffer *ring)
Chris Wilsona56ba562010-09-28 10:07:56 +01002271{
Chris Wilson88241782011-01-07 17:09:48 +00002272 int ret;
2273
Chris Wilson395b70b2010-10-28 21:28:46 +01002274 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002275 return 0;
2276
Chris Wilson88241782011-01-07 17:09:48 +00002277 if (!list_empty(&ring->gpu_write_list)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002278 ret = i915_gem_flush_ring(ring,
Chris Wilson0ac74c62010-12-06 14:36:02 +00002279 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilson88241782011-01-07 17:09:48 +00002280 if (ret)
2281 return ret;
2282 }
2283
Chris Wilsonce453d82011-02-21 14:43:56 +00002284 return i915_wait_request(ring, i915_gem_next_request_seqno(ring));
Chris Wilsona56ba562010-09-28 10:07:56 +01002285}
2286
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002287int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002288i915_gpu_idle(struct drm_device *dev)
2289{
2290 drm_i915_private_t *dev_priv = dev->dev_private;
2291 bool lists_empty;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002292 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002293
Zou Nan haid1b851f2010-05-21 09:08:57 +08002294 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002295 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002296 if (lists_empty)
2297 return 0;
2298
2299 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002300 for (i = 0; i < I915_NUM_RINGS; i++) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002301 ret = i915_ring_idle(&dev_priv->ring[i]);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002302 if (ret)
2303 return ret;
2304 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002305
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002306 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002307}
2308
Daniel Vetterc6642782010-11-12 13:46:18 +00002309static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2310 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002311{
Chris Wilson05394f32010-11-08 19:18:58 +00002312 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002313 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002314 u32 size = obj->gtt_space->size;
2315 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002316 uint64_t val;
2317
Chris Wilson05394f32010-11-08 19:18:58 +00002318 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002319 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002320 val |= obj->gtt_offset & 0xfffff000;
2321 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002322 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2323
Chris Wilson05394f32010-11-08 19:18:58 +00002324 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002325 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2326 val |= I965_FENCE_REG_VALID;
2327
Daniel Vetterc6642782010-11-12 13:46:18 +00002328 if (pipelined) {
2329 int ret = intel_ring_begin(pipelined, 6);
2330 if (ret)
2331 return ret;
2332
2333 intel_ring_emit(pipelined, MI_NOOP);
2334 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2335 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2336 intel_ring_emit(pipelined, (u32)val);
2337 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2338 intel_ring_emit(pipelined, (u32)(val >> 32));
2339 intel_ring_advance(pipelined);
2340 } else
2341 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2342
2343 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002344}
2345
Daniel Vetterc6642782010-11-12 13:46:18 +00002346static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2347 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002348{
Chris Wilson05394f32010-11-08 19:18:58 +00002349 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002350 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002351 u32 size = obj->gtt_space->size;
2352 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002353 uint64_t val;
2354
Chris Wilson05394f32010-11-08 19:18:58 +00002355 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002357 val |= obj->gtt_offset & 0xfffff000;
2358 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2359 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002360 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2361 val |= I965_FENCE_REG_VALID;
2362
Daniel Vetterc6642782010-11-12 13:46:18 +00002363 if (pipelined) {
2364 int ret = intel_ring_begin(pipelined, 6);
2365 if (ret)
2366 return ret;
2367
2368 intel_ring_emit(pipelined, MI_NOOP);
2369 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2370 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2371 intel_ring_emit(pipelined, (u32)val);
2372 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2373 intel_ring_emit(pipelined, (u32)(val >> 32));
2374 intel_ring_advance(pipelined);
2375 } else
2376 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2377
2378 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002379}
2380
Daniel Vetterc6642782010-11-12 13:46:18 +00002381static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2382 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002383{
Chris Wilson05394f32010-11-08 19:18:58 +00002384 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002385 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002386 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002387 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002388 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002389
Daniel Vetterc6642782010-11-12 13:46:18 +00002390 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2391 (size & -size) != size ||
2392 (obj->gtt_offset & (size - 1)),
2393 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2394 obj->gtt_offset, obj->map_and_fenceable, size))
2395 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002396
Daniel Vetterc6642782010-11-12 13:46:18 +00002397 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002398 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002399 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002400 tile_width = 512;
2401
2402 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002403 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002404 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002405
Chris Wilson05394f32010-11-08 19:18:58 +00002406 val = obj->gtt_offset;
2407 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002408 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002409 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002410 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2411 val |= I830_FENCE_REG_VALID;
2412
Chris Wilson05394f32010-11-08 19:18:58 +00002413 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002414 if (fence_reg < 8)
2415 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002416 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002417 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002418
2419 if (pipelined) {
2420 int ret = intel_ring_begin(pipelined, 4);
2421 if (ret)
2422 return ret;
2423
2424 intel_ring_emit(pipelined, MI_NOOP);
2425 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2426 intel_ring_emit(pipelined, fence_reg);
2427 intel_ring_emit(pipelined, val);
2428 intel_ring_advance(pipelined);
2429 } else
2430 I915_WRITE(fence_reg, val);
2431
2432 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002433}
2434
Daniel Vetterc6642782010-11-12 13:46:18 +00002435static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2436 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002437{
Chris Wilson05394f32010-11-08 19:18:58 +00002438 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002439 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002440 u32 size = obj->gtt_space->size;
2441 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002442 uint32_t val;
2443 uint32_t pitch_val;
2444
Daniel Vetterc6642782010-11-12 13:46:18 +00002445 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2446 (size & -size) != size ||
2447 (obj->gtt_offset & (size - 1)),
2448 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2449 obj->gtt_offset, size))
2450 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002451
Chris Wilson05394f32010-11-08 19:18:58 +00002452 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002453 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002454
Chris Wilson05394f32010-11-08 19:18:58 +00002455 val = obj->gtt_offset;
2456 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002457 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002458 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002459 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2460 val |= I830_FENCE_REG_VALID;
2461
Daniel Vetterc6642782010-11-12 13:46:18 +00002462 if (pipelined) {
2463 int ret = intel_ring_begin(pipelined, 4);
2464 if (ret)
2465 return ret;
2466
2467 intel_ring_emit(pipelined, MI_NOOP);
2468 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2469 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2470 intel_ring_emit(pipelined, val);
2471 intel_ring_advance(pipelined);
2472 } else
2473 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2474
2475 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002476}
2477
Chris Wilsond9e86c02010-11-10 16:40:20 +00002478static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2479{
2480 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2481}
2482
2483static int
2484i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002485 struct intel_ring_buffer *pipelined)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002486{
2487 int ret;
2488
2489 if (obj->fenced_gpu_access) {
Chris Wilson88241782011-01-07 17:09:48 +00002490 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002491 ret = i915_gem_flush_ring(obj->last_fenced_ring,
Chris Wilson88241782011-01-07 17:09:48 +00002492 0, obj->base.write_domain);
2493 if (ret)
2494 return ret;
2495 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002496
2497 obj->fenced_gpu_access = false;
2498 }
2499
2500 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2501 if (!ring_passed_seqno(obj->last_fenced_ring,
2502 obj->last_fenced_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002503 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002504 obj->last_fenced_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002505 if (ret)
2506 return ret;
2507 }
2508
2509 obj->last_fenced_seqno = 0;
2510 obj->last_fenced_ring = NULL;
2511 }
2512
Chris Wilson63256ec2011-01-04 18:42:07 +00002513 /* Ensure that all CPU reads are completed before installing a fence
2514 * and all writes before removing the fence.
2515 */
2516 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2517 mb();
2518
Chris Wilsond9e86c02010-11-10 16:40:20 +00002519 return 0;
2520}
2521
2522int
2523i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2524{
2525 int ret;
2526
2527 if (obj->tiling_mode)
2528 i915_gem_release_mmap(obj);
2529
Chris Wilsonce453d82011-02-21 14:43:56 +00002530 ret = i915_gem_object_flush_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002531 if (ret)
2532 return ret;
2533
2534 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2535 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2536 i915_gem_clear_fence_reg(obj->base.dev,
2537 &dev_priv->fence_regs[obj->fence_reg]);
2538
2539 obj->fence_reg = I915_FENCE_REG_NONE;
2540 }
2541
2542 return 0;
2543}
2544
2545static struct drm_i915_fence_reg *
2546i915_find_fence_reg(struct drm_device *dev,
2547 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002548{
Daniel Vetterae3db242010-02-19 11:51:58 +01002549 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002550 struct drm_i915_fence_reg *reg, *first, *avail;
2551 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002552
2553 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002554 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002555 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2556 reg = &dev_priv->fence_regs[i];
2557 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002558 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002559
Chris Wilson05394f32010-11-08 19:18:58 +00002560 if (!reg->obj->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002561 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002562 }
2563
Chris Wilsond9e86c02010-11-10 16:40:20 +00002564 if (avail == NULL)
2565 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002566
2567 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002568 avail = first = NULL;
2569 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2570 if (reg->obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002571 continue;
2572
Chris Wilsond9e86c02010-11-10 16:40:20 +00002573 if (first == NULL)
2574 first = reg;
2575
2576 if (!pipelined ||
2577 !reg->obj->last_fenced_ring ||
2578 reg->obj->last_fenced_ring == pipelined) {
2579 avail = reg;
2580 break;
2581 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002582 }
2583
Chris Wilsond9e86c02010-11-10 16:40:20 +00002584 if (avail == NULL)
2585 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002586
Chris Wilsona00b10c2010-09-24 21:15:47 +01002587 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002588}
2589
Jesse Barnesde151cf2008-11-12 10:03:55 -08002590/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002591 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002592 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002593 * @pipelined: ring on which to queue the change, or NULL for CPU access
2594 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002595 *
2596 * When mapping objects through the GTT, userspace wants to be able to write
2597 * to them without having to worry about swizzling if the object is tiled.
2598 *
2599 * This function walks the fence regs looking for a free one for @obj,
2600 * stealing one if it can't find any.
2601 *
2602 * It then sets up the reg based on the object's properties: address, pitch
2603 * and tiling format.
2604 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002605int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002606i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002607 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002608{
Chris Wilson05394f32010-11-08 19:18:58 +00002609 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002610 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002611 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002612 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002613
Chris Wilson6bda10d2010-12-05 21:04:18 +00002614 /* XXX disable pipelining. There are bugs. Shocking. */
2615 pipelined = NULL;
2616
Chris Wilsond9e86c02010-11-10 16:40:20 +00002617 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002618 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2619 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002620 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002621
Chris Wilson29c5a582011-03-17 15:23:22 +00002622 if (obj->tiling_changed) {
2623 ret = i915_gem_object_flush_fence(obj, pipelined);
2624 if (ret)
2625 return ret;
2626
2627 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2628 pipelined = NULL;
2629
2630 if (pipelined) {
2631 reg->setup_seqno =
2632 i915_gem_next_request_seqno(pipelined);
2633 obj->last_fenced_seqno = reg->setup_seqno;
2634 obj->last_fenced_ring = pipelined;
2635 }
2636
2637 goto update;
2638 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002639
2640 if (!pipelined) {
2641 if (reg->setup_seqno) {
2642 if (!ring_passed_seqno(obj->last_fenced_ring,
2643 reg->setup_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002644 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002645 reg->setup_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002646 if (ret)
2647 return ret;
2648 }
2649
2650 reg->setup_seqno = 0;
2651 }
2652 } else if (obj->last_fenced_ring &&
2653 obj->last_fenced_ring != pipelined) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002654 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002655 if (ret)
2656 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002657 }
2658
Eric Anholta09ba7f2009-08-29 12:49:51 -07002659 return 0;
2660 }
2661
Chris Wilsond9e86c02010-11-10 16:40:20 +00002662 reg = i915_find_fence_reg(dev, pipelined);
2663 if (reg == NULL)
2664 return -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002665
Chris Wilsonce453d82011-02-21 14:43:56 +00002666 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002667 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002668 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002669
Chris Wilsond9e86c02010-11-10 16:40:20 +00002670 if (reg->obj) {
2671 struct drm_i915_gem_object *old = reg->obj;
2672
2673 drm_gem_object_reference(&old->base);
2674
2675 if (old->tiling_mode)
2676 i915_gem_release_mmap(old);
2677
Chris Wilsonce453d82011-02-21 14:43:56 +00002678 ret = i915_gem_object_flush_fence(old, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002679 if (ret) {
2680 drm_gem_object_unreference(&old->base);
2681 return ret;
2682 }
2683
2684 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2685 pipelined = NULL;
2686
2687 old->fence_reg = I915_FENCE_REG_NONE;
2688 old->last_fenced_ring = pipelined;
2689 old->last_fenced_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002690 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002691
2692 drm_gem_object_unreference(&old->base);
2693 } else if (obj->last_fenced_seqno == 0)
2694 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002695
Jesse Barnesde151cf2008-11-12 10:03:55 -08002696 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002697 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2698 obj->fence_reg = reg - dev_priv->fence_regs;
2699 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002700
Chris Wilsond9e86c02010-11-10 16:40:20 +00002701 reg->setup_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002702 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002703 obj->last_fenced_seqno = reg->setup_seqno;
2704
2705update:
2706 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002707 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc2011-05-06 13:55:53 -07002708 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002709 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002710 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002711 break;
2712 case 5:
2713 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002714 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002715 break;
2716 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002717 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002718 break;
2719 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002720 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002721 break;
2722 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002723
Daniel Vetterc6642782010-11-12 13:46:18 +00002724 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002725}
2726
2727/**
2728 * i915_gem_clear_fence_reg - clear out fence register info
2729 * @obj: object to clear
2730 *
2731 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002732 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002733 */
2734static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002735i915_gem_clear_fence_reg(struct drm_device *dev,
2736 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002737{
Jesse Barnes79e53942008-11-07 14:24:08 -08002738 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002739 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002740
Chris Wilsone259bef2010-09-17 00:32:02 +01002741 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc2011-05-06 13:55:53 -07002742 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002743 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002744 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002745 break;
2746 case 5:
2747 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002748 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002749 break;
2750 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002751 if (fence_reg >= 8)
2752 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002753 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002754 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002755 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002756
2757 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002758 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002759 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002760
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002761 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002762 reg->obj = NULL;
2763 reg->setup_seqno = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002764}
2765
2766/**
Eric Anholt673a3942008-07-30 12:06:12 -07002767 * Finds free space in the GTT aperture and binds the object there.
2768 */
2769static int
Chris Wilson05394f32010-11-08 19:18:58 +00002770i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002771 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002772 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002773{
Chris Wilson05394f32010-11-08 19:18:58 +00002774 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002775 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002776 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002777 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002778 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002779 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002780 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002781
Chris Wilson05394f32010-11-08 19:18:58 +00002782 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002783 DRM_ERROR("Attempting to bind a purgeable object\n");
2784 return -EINVAL;
2785 }
2786
Chris Wilson05394f32010-11-08 19:18:58 +00002787 fence_size = i915_gem_get_gtt_size(obj);
2788 fence_alignment = i915_gem_get_gtt_alignment(obj);
2789 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002790
Eric Anholt673a3942008-07-30 12:06:12 -07002791 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002792 alignment = map_and_fenceable ? fence_alignment :
2793 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002794 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002795 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2796 return -EINVAL;
2797 }
2798
Chris Wilson05394f32010-11-08 19:18:58 +00002799 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002800
Chris Wilson654fc602010-05-27 13:18:21 +01002801 /* If the object is bigger than the entire aperture, reject it early
2802 * before evicting everything in a vain attempt to find space.
2803 */
Chris Wilson05394f32010-11-08 19:18:58 +00002804 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002805 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002806 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2807 return -E2BIG;
2808 }
2809
Eric Anholt673a3942008-07-30 12:06:12 -07002810 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002811 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002812 free_space =
2813 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002814 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002815 dev_priv->mm.gtt_mappable_end,
2816 0);
2817 else
2818 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002819 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002820
2821 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002822 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002823 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002824 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002825 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002826 dev_priv->mm.gtt_mappable_end,
2827 0);
2828 else
Chris Wilson05394f32010-11-08 19:18:58 +00002829 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002830 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002831 }
Chris Wilson05394f32010-11-08 19:18:58 +00002832 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002833 /* If the gtt is empty and we're still having trouble
2834 * fitting our object in, we're out of memory.
2835 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002836 ret = i915_gem_evict_something(dev, size, alignment,
2837 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002838 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002839 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002840
Eric Anholt673a3942008-07-30 12:06:12 -07002841 goto search_free;
2842 }
2843
Chris Wilsone5281cc2010-10-28 13:45:36 +01002844 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002845 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002846 drm_mm_put_block(obj->gtt_space);
2847 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002848
2849 if (ret == -ENOMEM) {
Chris Wilson809b6332011-01-10 17:33:15 +00002850 /* first try to reclaim some memory by clearing the GTT */
2851 ret = i915_gem_evict_everything(dev, false);
Chris Wilson07f73f62009-09-14 16:50:30 +01002852 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002853 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002854 if (gfpmask) {
2855 gfpmask = 0;
2856 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002857 }
2858
Chris Wilson809b6332011-01-10 17:33:15 +00002859 return -ENOMEM;
Chris Wilson07f73f62009-09-14 16:50:30 +01002860 }
2861
2862 goto search_free;
2863 }
2864
Eric Anholt673a3942008-07-30 12:06:12 -07002865 return ret;
2866 }
2867
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002868 ret = i915_gem_gtt_bind_object(obj);
2869 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002870 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002871 drm_mm_put_block(obj->gtt_space);
2872 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002873
Chris Wilson809b6332011-01-10 17:33:15 +00002874 if (i915_gem_evict_everything(dev, false))
Chris Wilson07f73f62009-09-14 16:50:30 +01002875 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002876
2877 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002878 }
Eric Anholt673a3942008-07-30 12:06:12 -07002879
Chris Wilson6299f992010-11-24 12:23:44 +00002880 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002881 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002882
Eric Anholt673a3942008-07-30 12:06:12 -07002883 /* Assert that the object is not currently in any GPU domain. As it
2884 * wasn't in the GTT, there shouldn't be any way it could have been in
2885 * a GPU cache
2886 */
Chris Wilson05394f32010-11-08 19:18:58 +00002887 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2888 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002889
Chris Wilson6299f992010-11-24 12:23:44 +00002890 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002891
Daniel Vetter75e9e912010-11-04 17:11:09 +01002892 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002893 obj->gtt_space->size == fence_size &&
2894 (obj->gtt_space->start & (fence_alignment -1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002895
Daniel Vetter75e9e912010-11-04 17:11:09 +01002896 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002897 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002898
Chris Wilson05394f32010-11-08 19:18:58 +00002899 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002900
Chris Wilsondb53a302011-02-03 11:57:46 +00002901 trace_i915_gem_object_bind(obj, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002902 return 0;
2903}
2904
2905void
Chris Wilson05394f32010-11-08 19:18:58 +00002906i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002907{
Eric Anholt673a3942008-07-30 12:06:12 -07002908 /* If we don't have a page list set up, then we're not pinned
2909 * to GPU, and we can ignore the cache flush because it'll happen
2910 * again at bind time.
2911 */
Chris Wilson05394f32010-11-08 19:18:58 +00002912 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002913 return;
2914
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002915 /* If the GPU is snooping the contents of the CPU cache,
2916 * we do not need to manually clear the CPU cache lines. However,
2917 * the caches are only snooped when the render cache is
2918 * flushed/invalidated. As we always have to emit invalidations
2919 * and flushes when moving into and out of the RENDER domain, correct
2920 * snooping behaviour occurs naturally as the result of our domain
2921 * tracking.
2922 */
2923 if (obj->cache_level != I915_CACHE_NONE)
2924 return;
2925
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002926 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002927
Chris Wilson05394f32010-11-08 19:18:58 +00002928 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002929}
2930
Eric Anholte47c68e2008-11-14 13:35:19 -08002931/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson88241782011-01-07 17:09:48 +00002932static int
Chris Wilson3619df02010-11-28 15:37:17 +00002933i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002934{
Chris Wilson05394f32010-11-08 19:18:58 +00002935 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson88241782011-01-07 17:09:48 +00002936 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002937
2938 /* Queue the GPU write cache flushing we need. */
Chris Wilsondb53a302011-02-03 11:57:46 +00002939 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002940}
2941
2942/** Flushes the GTT write domain for the object if it's dirty. */
2943static void
Chris Wilson05394f32010-11-08 19:18:58 +00002944i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002945{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002946 uint32_t old_write_domain;
2947
Chris Wilson05394f32010-11-08 19:18:58 +00002948 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002949 return;
2950
Chris Wilson63256ec2011-01-04 18:42:07 +00002951 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002952 * to it immediately go to main memory as far as we know, so there's
2953 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002954 *
2955 * However, we do have to enforce the order so that all writes through
2956 * the GTT land before any writes to the device, such as updates to
2957 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002958 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002959 wmb();
2960
Chris Wilson05394f32010-11-08 19:18:58 +00002961 old_write_domain = obj->base.write_domain;
2962 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002963
2964 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002965 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002966 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002967}
2968
2969/** Flushes the CPU write domain for the object if it's dirty. */
2970static void
Chris Wilson05394f32010-11-08 19:18:58 +00002971i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002972{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002973 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002974
Chris Wilson05394f32010-11-08 19:18:58 +00002975 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002976 return;
2977
2978 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002979 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002980 old_write_domain = obj->base.write_domain;
2981 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002982
2983 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002984 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002985 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002986}
2987
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002988/**
2989 * Moves a single object to the GTT read, and possibly write domain.
2990 *
2991 * This function returns when the move is complete, including waiting on
2992 * flushes to occur.
2993 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002994int
Chris Wilson20217462010-11-23 15:26:33 +00002995i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002996{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002997 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002998 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002999
Eric Anholt02354392008-11-26 13:58:13 -08003000 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00003001 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08003002 return -EINVAL;
3003
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003004 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3005 return 0;
3006
Chris Wilson88241782011-01-07 17:09:48 +00003007 ret = i915_gem_object_flush_gpu_write_domain(obj);
3008 if (ret)
3009 return ret;
3010
Chris Wilson87ca9c82010-12-02 09:42:56 +00003011 if (obj->pending_gpu_write || write) {
Chris Wilsonce453d82011-02-21 14:43:56 +00003012 ret = i915_gem_object_wait_rendering(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00003013 if (ret)
3014 return ret;
3015 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003016
Chris Wilson72133422010-09-13 23:56:38 +01003017 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003018
Chris Wilson05394f32010-11-08 19:18:58 +00003019 old_write_domain = obj->base.write_domain;
3020 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003021
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003022 /* It should now be out of any other write domains, and we can update
3023 * the domain values for our changes.
3024 */
Chris Wilson05394f32010-11-08 19:18:58 +00003025 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3026 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003027 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003028 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3029 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3030 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003031 }
3032
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003033 trace_i915_gem_object_change_domain(obj,
3034 old_read_domains,
3035 old_write_domain);
3036
Eric Anholte47c68e2008-11-14 13:35:19 -08003037 return 0;
3038}
3039
Chris Wilsone4ffd172011-04-04 09:44:39 +01003040int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3041 enum i915_cache_level cache_level)
3042{
3043 int ret;
3044
3045 if (obj->cache_level == cache_level)
3046 return 0;
3047
3048 if (obj->pin_count) {
3049 DRM_DEBUG("can not change the cache level of pinned objects\n");
3050 return -EBUSY;
3051 }
3052
3053 if (obj->gtt_space) {
3054 ret = i915_gem_object_finish_gpu(obj);
3055 if (ret)
3056 return ret;
3057
3058 i915_gem_object_finish_gtt(obj);
3059
3060 /* Before SandyBridge, you could not use tiling or fence
3061 * registers with snooped memory, so relinquish any fences
3062 * currently pointing to our region in the aperture.
3063 */
3064 if (INTEL_INFO(obj->base.dev)->gen < 6) {
3065 ret = i915_gem_object_put_fence(obj);
3066 if (ret)
3067 return ret;
3068 }
3069
3070 i915_gem_gtt_rebind_object(obj, cache_level);
3071 }
3072
3073 if (cache_level == I915_CACHE_NONE) {
3074 u32 old_read_domains, old_write_domain;
3075
3076 /* If we're coming from LLC cached, then we haven't
3077 * actually been tracking whether the data is in the
3078 * CPU cache or not, since we only allow one bit set
3079 * in obj->write_domain and have been skipping the clflushes.
3080 * Just set it to the CPU cache for now.
3081 */
3082 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3083 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3084
3085 old_read_domains = obj->base.read_domains;
3086 old_write_domain = obj->base.write_domain;
3087
3088 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3089 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3090
3091 trace_i915_gem_object_change_domain(obj,
3092 old_read_domains,
3093 old_write_domain);
3094 }
3095
3096 obj->cache_level = cache_level;
3097 return 0;
3098}
3099
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003100/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003101 * Prepare buffer for display plane (scanout, cursors, etc).
3102 * Can be called from an uninterruptible phase (modesetting) and allows
3103 * any flushes to be pipelined (for pageflips).
3104 *
3105 * For the display plane, we want to be in the GTT but out of any write
3106 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
3107 * ability to pipeline the waits, pinning and any additional subtleties
3108 * that may differentiate the display plane from ordinary buffers.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003109 */
3110int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003111i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3112 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003113 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003114{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003115 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003116 int ret;
3117
Chris Wilson88241782011-01-07 17:09:48 +00003118 ret = i915_gem_object_flush_gpu_write_domain(obj);
3119 if (ret)
3120 return ret;
3121
Chris Wilson0be73282010-12-06 14:36:27 +00003122 if (pipelined != obj->ring) {
Chris Wilsonce453d82011-02-21 14:43:56 +00003123 ret = i915_gem_object_wait_rendering(obj);
Chris Wilsonced270f2010-09-26 22:47:46 +01003124 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003125 return ret;
3126 }
3127
Eric Anholta7ef0642011-03-29 16:59:54 -07003128 /* The display engine is not coherent with the LLC cache on gen6. As
3129 * a result, we make sure that the pinning that is about to occur is
3130 * done with uncached PTEs. This is lowest common denominator for all
3131 * chipsets.
3132 *
3133 * However for gen6+, we could do better by using the GFDT bit instead
3134 * of uncaching, which would allow us to flush all the LLC-cached data
3135 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3136 */
3137 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3138 if (ret)
3139 return ret;
3140
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003141 /* As the user may map the buffer once pinned in the display plane
3142 * (e.g. libkms for the bootup splash), we have to ensure that we
3143 * always use map_and_fenceable for all scanout buffers.
3144 */
3145 ret = i915_gem_object_pin(obj, alignment, true);
3146 if (ret)
3147 return ret;
3148
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003149 i915_gem_object_flush_cpu_write_domain(obj);
3150
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003151 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003152 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003153
3154 /* It should now be out of any other write domains, and we can update
3155 * the domain values for our changes.
3156 */
3157 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003158 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003159
3160 trace_i915_gem_object_change_domain(obj,
3161 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003162 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003163
3164 return 0;
3165}
3166
Chris Wilson85345512010-11-13 09:49:11 +00003167int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003168i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003169{
Chris Wilson88241782011-01-07 17:09:48 +00003170 int ret;
3171
Chris Wilsona8198ee2011-04-13 22:04:09 +01003172 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003173 return 0;
3174
Chris Wilson88241782011-01-07 17:09:48 +00003175 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003176 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Chris Wilson88241782011-01-07 17:09:48 +00003177 if (ret)
3178 return ret;
3179 }
Chris Wilson85345512010-11-13 09:49:11 +00003180
Chris Wilsona8198ee2011-04-13 22:04:09 +01003181 /* Ensure that we invalidate the GPU's caches and TLBs. */
3182 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3183
Chris Wilsonce453d82011-02-21 14:43:56 +00003184 return i915_gem_object_wait_rendering(obj);
Chris Wilson85345512010-11-13 09:49:11 +00003185}
3186
Eric Anholte47c68e2008-11-14 13:35:19 -08003187/**
3188 * Moves a single object to the CPU read, and possibly write domain.
3189 *
3190 * This function returns when the move is complete, including waiting on
3191 * flushes to occur.
3192 */
3193static int
Chris Wilson919926a2010-11-12 13:42:53 +00003194i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003195{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003196 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003197 int ret;
3198
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003199 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3200 return 0;
3201
Chris Wilson88241782011-01-07 17:09:48 +00003202 ret = i915_gem_object_flush_gpu_write_domain(obj);
3203 if (ret)
3204 return ret;
3205
Chris Wilsonce453d82011-02-21 14:43:56 +00003206 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003207 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003208 return ret;
3209
3210 i915_gem_object_flush_gtt_write_domain(obj);
3211
3212 /* If we have a partially-valid cache of the object in the CPU,
3213 * finish invalidating it and free the per-page flags.
3214 */
3215 i915_gem_object_set_to_full_cpu_read_domain(obj);
3216
Chris Wilson05394f32010-11-08 19:18:58 +00003217 old_write_domain = obj->base.write_domain;
3218 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003219
Eric Anholte47c68e2008-11-14 13:35:19 -08003220 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003221 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003222 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003223
Chris Wilson05394f32010-11-08 19:18:58 +00003224 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003225 }
3226
3227 /* It should now be out of any other write domains, and we can update
3228 * the domain values for our changes.
3229 */
Chris Wilson05394f32010-11-08 19:18:58 +00003230 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003231
3232 /* If we're writing through the CPU, then the GPU read domains will
3233 * need to be invalidated at next use.
3234 */
3235 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003236 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3237 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003238 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003239
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003240 trace_i915_gem_object_change_domain(obj,
3241 old_read_domains,
3242 old_write_domain);
3243
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003244 return 0;
3245}
3246
Eric Anholt673a3942008-07-30 12:06:12 -07003247/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003248 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003249 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003250 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3251 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3252 */
3253static void
Chris Wilson05394f32010-11-08 19:18:58 +00003254i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003255{
Chris Wilson05394f32010-11-08 19:18:58 +00003256 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003257 return;
3258
3259 /* If we're partially in the CPU read domain, finish moving it in.
3260 */
Chris Wilson05394f32010-11-08 19:18:58 +00003261 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003262 int i;
3263
Chris Wilson05394f32010-11-08 19:18:58 +00003264 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3265 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003266 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003267 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003268 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003269 }
3270
3271 /* Free the page_cpu_valid mappings which are now stale, whether
3272 * or not we've got I915_GEM_DOMAIN_CPU.
3273 */
Chris Wilson05394f32010-11-08 19:18:58 +00003274 kfree(obj->page_cpu_valid);
3275 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003276}
3277
3278/**
3279 * Set the CPU read domain on a range of the object.
3280 *
3281 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3282 * not entirely valid. The page_cpu_valid member of the object flags which
3283 * pages have been flushed, and will be respected by
3284 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3285 * of the whole object.
3286 *
3287 * This function returns when the move is complete, including waiting on
3288 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003289 */
3290static int
Chris Wilson05394f32010-11-08 19:18:58 +00003291i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003292 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003293{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003294 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003295 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003296
Chris Wilson05394f32010-11-08 19:18:58 +00003297 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003298 return i915_gem_object_set_to_cpu_domain(obj, 0);
3299
Chris Wilson88241782011-01-07 17:09:48 +00003300 ret = i915_gem_object_flush_gpu_write_domain(obj);
3301 if (ret)
3302 return ret;
3303
Chris Wilsonce453d82011-02-21 14:43:56 +00003304 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003305 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003306 return ret;
Daniel Vetterde18a292010-11-27 22:30:41 +01003307
Eric Anholte47c68e2008-11-14 13:35:19 -08003308 i915_gem_object_flush_gtt_write_domain(obj);
3309
3310 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003311 if (obj->page_cpu_valid == NULL &&
3312 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003313 return 0;
3314
Eric Anholte47c68e2008-11-14 13:35:19 -08003315 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3316 * newly adding I915_GEM_DOMAIN_CPU
3317 */
Chris Wilson05394f32010-11-08 19:18:58 +00003318 if (obj->page_cpu_valid == NULL) {
3319 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3320 GFP_KERNEL);
3321 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003322 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003323 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3324 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003325
3326 /* Flush the cache on any pages that are still invalid from the CPU's
3327 * perspective.
3328 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003329 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3330 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003331 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003332 continue;
3333
Chris Wilson05394f32010-11-08 19:18:58 +00003334 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003335
Chris Wilson05394f32010-11-08 19:18:58 +00003336 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003337 }
3338
Eric Anholte47c68e2008-11-14 13:35:19 -08003339 /* It should now be out of any other write domains, and we can update
3340 * the domain values for our changes.
3341 */
Chris Wilson05394f32010-11-08 19:18:58 +00003342 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003343
Chris Wilson05394f32010-11-08 19:18:58 +00003344 old_read_domains = obj->base.read_domains;
3345 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003346
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003347 trace_i915_gem_object_change_domain(obj,
3348 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003349 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003350
Eric Anholt673a3942008-07-30 12:06:12 -07003351 return 0;
3352}
3353
Eric Anholt673a3942008-07-30 12:06:12 -07003354/* Throttle our rendering by waiting until the ring has completed our requests
3355 * emitted over 20 msec ago.
3356 *
Eric Anholtb9624422009-06-03 07:27:35 +00003357 * Note that if we were to use the current jiffies each time around the loop,
3358 * we wouldn't escape the function with any frames outstanding if the time to
3359 * render a frame was over 20ms.
3360 *
Eric Anholt673a3942008-07-30 12:06:12 -07003361 * This should get us reasonable parallelism between CPU and GPU but also
3362 * relatively low latency when blocking on a particular request to finish.
3363 */
3364static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003365i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003366{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003367 struct drm_i915_private *dev_priv = dev->dev_private;
3368 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003369 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003370 struct drm_i915_gem_request *request;
3371 struct intel_ring_buffer *ring = NULL;
3372 u32 seqno = 0;
3373 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003374
Chris Wilsone110e8d2011-01-26 15:39:14 +00003375 if (atomic_read(&dev_priv->mm.wedged))
3376 return -EIO;
3377
Chris Wilson1c255952010-09-26 11:03:27 +01003378 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003379 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003380 if (time_after_eq(request->emitted_jiffies, recent_enough))
3381 break;
3382
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003383 ring = request->ring;
3384 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003385 }
Chris Wilson1c255952010-09-26 11:03:27 +01003386 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003387
3388 if (seqno == 0)
3389 return 0;
3390
3391 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003392 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003393 /* And wait for the seqno passing without holding any locks and
3394 * causing extra latency for others. This is safe as the irq
3395 * generation is designed to be run atomically and so is
3396 * lockless.
3397 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003398 if (ring->irq_get(ring)) {
3399 ret = wait_event_interruptible(ring->irq_queue,
3400 i915_seqno_passed(ring->get_seqno(ring), seqno)
3401 || atomic_read(&dev_priv->mm.wedged));
3402 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003403
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003404 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3405 ret = -EIO;
3406 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003407 }
3408
3409 if (ret == 0)
3410 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003411
Eric Anholt673a3942008-07-30 12:06:12 -07003412 return ret;
3413}
3414
Eric Anholt673a3942008-07-30 12:06:12 -07003415int
Chris Wilson05394f32010-11-08 19:18:58 +00003416i915_gem_object_pin(struct drm_i915_gem_object *obj,
3417 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003418 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003419{
Chris Wilson05394f32010-11-08 19:18:58 +00003420 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003421 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003422 int ret;
3423
Chris Wilson05394f32010-11-08 19:18:58 +00003424 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003425 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003426
Chris Wilson05394f32010-11-08 19:18:58 +00003427 if (obj->gtt_space != NULL) {
3428 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3429 (map_and_fenceable && !obj->map_and_fenceable)) {
3430 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003431 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003432 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3433 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003434 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003435 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003436 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003437 ret = i915_gem_object_unbind(obj);
3438 if (ret)
3439 return ret;
3440 }
3441 }
3442
Chris Wilson05394f32010-11-08 19:18:58 +00003443 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003444 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003445 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003446 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003447 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003448 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003449
Chris Wilson05394f32010-11-08 19:18:58 +00003450 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003451 if (!obj->active)
3452 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003453 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003454 }
Chris Wilson6299f992010-11-24 12:23:44 +00003455 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003456
Chris Wilson23bc5982010-09-29 16:10:57 +01003457 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003458 return 0;
3459}
3460
3461void
Chris Wilson05394f32010-11-08 19:18:58 +00003462i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003463{
Chris Wilson05394f32010-11-08 19:18:58 +00003464 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003465 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003466
Chris Wilson23bc5982010-09-29 16:10:57 +01003467 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003468 BUG_ON(obj->pin_count == 0);
3469 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003470
Chris Wilson05394f32010-11-08 19:18:58 +00003471 if (--obj->pin_count == 0) {
3472 if (!obj->active)
3473 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003474 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003475 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003476 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003477 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003478}
3479
3480int
3481i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003482 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003483{
3484 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003485 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003486 int ret;
3487
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003488 ret = i915_mutex_lock_interruptible(dev);
3489 if (ret)
3490 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003491
Chris Wilson05394f32010-11-08 19:18:58 +00003492 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003493 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003494 ret = -ENOENT;
3495 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003496 }
Eric Anholt673a3942008-07-30 12:06:12 -07003497
Chris Wilson05394f32010-11-08 19:18:58 +00003498 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003499 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003500 ret = -EINVAL;
3501 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003502 }
3503
Chris Wilson05394f32010-11-08 19:18:58 +00003504 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003505 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3506 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003507 ret = -EINVAL;
3508 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003509 }
3510
Chris Wilson05394f32010-11-08 19:18:58 +00003511 obj->user_pin_count++;
3512 obj->pin_filp = file;
3513 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003514 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003515 if (ret)
3516 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003517 }
3518
3519 /* XXX - flush the CPU caches for pinned objects
3520 * as the X server doesn't manage domains yet
3521 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003522 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003523 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003524out:
Chris Wilson05394f32010-11-08 19:18:58 +00003525 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003526unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003527 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003528 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003529}
3530
3531int
3532i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003533 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003534{
3535 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003536 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003537 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003538
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003539 ret = i915_mutex_lock_interruptible(dev);
3540 if (ret)
3541 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003542
Chris Wilson05394f32010-11-08 19:18:58 +00003543 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003544 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003545 ret = -ENOENT;
3546 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003547 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003548
Chris Wilson05394f32010-11-08 19:18:58 +00003549 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003550 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3551 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003552 ret = -EINVAL;
3553 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003554 }
Chris Wilson05394f32010-11-08 19:18:58 +00003555 obj->user_pin_count--;
3556 if (obj->user_pin_count == 0) {
3557 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003558 i915_gem_object_unpin(obj);
3559 }
Eric Anholt673a3942008-07-30 12:06:12 -07003560
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003561out:
Chris Wilson05394f32010-11-08 19:18:58 +00003562 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003563unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003564 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003565 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003566}
3567
3568int
3569i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003570 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003571{
3572 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003573 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003574 int ret;
3575
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003576 ret = i915_mutex_lock_interruptible(dev);
3577 if (ret)
3578 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003579
Chris Wilson05394f32010-11-08 19:18:58 +00003580 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003581 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003582 ret = -ENOENT;
3583 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003584 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003585
Chris Wilson0be555b2010-08-04 15:36:30 +01003586 /* Count all active objects as busy, even if they are currently not used
3587 * by the gpu. Users of this interface expect objects to eventually
3588 * become non-busy without any further actions, therefore emit any
3589 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003590 */
Chris Wilson05394f32010-11-08 19:18:58 +00003591 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003592 if (args->busy) {
3593 /* Unconditionally flush objects, even when the gpu still uses this
3594 * object. Userspace calling this function indicates that it wants to
3595 * use this buffer rather sooner than later, so issuing the required
3596 * flush earlier is beneficial.
3597 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003598 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003599 ret = i915_gem_flush_ring(obj->ring,
Chris Wilson88241782011-01-07 17:09:48 +00003600 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003601 } else if (obj->ring->outstanding_lazy_request ==
3602 obj->last_rendering_seqno) {
3603 struct drm_i915_gem_request *request;
3604
Chris Wilson7a194872010-12-07 10:38:40 +00003605 /* This ring is not being cleared by active usage,
3606 * so emit a request to do so.
3607 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003608 request = kzalloc(sizeof(*request), GFP_KERNEL);
3609 if (request)
Chris Wilsondb53a302011-02-03 11:57:46 +00003610 ret = i915_add_request(obj->ring, NULL,request);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003611 else
Chris Wilson7a194872010-12-07 10:38:40 +00003612 ret = -ENOMEM;
3613 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003614
3615 /* Update the active list for the hardware's current position.
3616 * Otherwise this only updates on a delayed timer or when irqs
3617 * are actually unmasked, and our working set ends up being
3618 * larger than required.
3619 */
Chris Wilsondb53a302011-02-03 11:57:46 +00003620 i915_gem_retire_requests_ring(obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003621
Chris Wilson05394f32010-11-08 19:18:58 +00003622 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003623 }
Eric Anholt673a3942008-07-30 12:06:12 -07003624
Chris Wilson05394f32010-11-08 19:18:58 +00003625 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003626unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003627 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003628 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003629}
3630
3631int
3632i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3633 struct drm_file *file_priv)
3634{
3635 return i915_gem_ring_throttle(dev, file_priv);
3636}
3637
Chris Wilson3ef94da2009-09-14 16:50:29 +01003638int
3639i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3640 struct drm_file *file_priv)
3641{
3642 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003643 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003644 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003645
3646 switch (args->madv) {
3647 case I915_MADV_DONTNEED:
3648 case I915_MADV_WILLNEED:
3649 break;
3650 default:
3651 return -EINVAL;
3652 }
3653
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003654 ret = i915_mutex_lock_interruptible(dev);
3655 if (ret)
3656 return ret;
3657
Chris Wilson05394f32010-11-08 19:18:58 +00003658 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003659 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003660 ret = -ENOENT;
3661 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003662 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003663
Chris Wilson05394f32010-11-08 19:18:58 +00003664 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003665 ret = -EINVAL;
3666 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003667 }
3668
Chris Wilson05394f32010-11-08 19:18:58 +00003669 if (obj->madv != __I915_MADV_PURGED)
3670 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003671
Chris Wilson2d7ef392009-09-20 23:13:10 +01003672 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003673 if (i915_gem_object_is_purgeable(obj) &&
3674 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003675 i915_gem_object_truncate(obj);
3676
Chris Wilson05394f32010-11-08 19:18:58 +00003677 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003678
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003679out:
Chris Wilson05394f32010-11-08 19:18:58 +00003680 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003681unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003682 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003683 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003684}
3685
Chris Wilson05394f32010-11-08 19:18:58 +00003686struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3687 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003688{
Chris Wilson73aa8082010-09-30 11:46:12 +01003689 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003690 struct drm_i915_gem_object *obj;
3691
3692 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3693 if (obj == NULL)
3694 return NULL;
3695
3696 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3697 kfree(obj);
3698 return NULL;
3699 }
3700
Chris Wilson73aa8082010-09-30 11:46:12 +01003701 i915_gem_info_add_obj(dev_priv, size);
3702
Daniel Vetterc397b902010-04-09 19:05:07 +00003703 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3704 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3705
Eric Anholta1871112011-03-29 16:59:55 -07003706 if (IS_GEN6(dev)) {
3707 /* On Gen6, we can have the GPU use the LLC (the CPU
3708 * cache) for about a 10% performance improvement
3709 * compared to uncached. Graphics requests other than
3710 * display scanout are coherent with the CPU in
3711 * accessing this cache. This means in this mode we
3712 * don't need to clflush on the CPU side, and on the
3713 * GPU side we only need to flush internal caches to
3714 * get data visible to the CPU.
3715 *
3716 * However, we maintain the display planes as UC, and so
3717 * need to rebind when first used as such.
3718 */
3719 obj->cache_level = I915_CACHE_LLC;
3720 } else
3721 obj->cache_level = I915_CACHE_NONE;
3722
Daniel Vetter62b8b212010-04-09 19:05:08 +00003723 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003724 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003725 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003726 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003727 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003728 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003729 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003730 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003731 /* Avoid an unnecessary call to unbind on the first bind. */
3732 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003733
Chris Wilson05394f32010-11-08 19:18:58 +00003734 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003735}
3736
Eric Anholt673a3942008-07-30 12:06:12 -07003737int i915_gem_init_object(struct drm_gem_object *obj)
3738{
Daniel Vetterc397b902010-04-09 19:05:07 +00003739 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003740
Eric Anholt673a3942008-07-30 12:06:12 -07003741 return 0;
3742}
3743
Chris Wilson05394f32010-11-08 19:18:58 +00003744static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003745{
Chris Wilson05394f32010-11-08 19:18:58 +00003746 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003747 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003748 int ret;
3749
3750 ret = i915_gem_object_unbind(obj);
3751 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003752 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003753 &dev_priv->mm.deferred_free_list);
3754 return;
3755 }
3756
Chris Wilson26e12f82011-03-20 11:20:19 +00003757 trace_i915_gem_object_destroy(obj);
3758
Chris Wilson05394f32010-11-08 19:18:58 +00003759 if (obj->base.map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01003760 i915_gem_free_mmap_offset(obj);
3761
Chris Wilson05394f32010-11-08 19:18:58 +00003762 drm_gem_object_release(&obj->base);
3763 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003764
Chris Wilson05394f32010-11-08 19:18:58 +00003765 kfree(obj->page_cpu_valid);
3766 kfree(obj->bit_17);
3767 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003768}
3769
Chris Wilson05394f32010-11-08 19:18:58 +00003770void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003771{
Chris Wilson05394f32010-11-08 19:18:58 +00003772 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3773 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003774
Chris Wilson05394f32010-11-08 19:18:58 +00003775 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003776 i915_gem_object_unpin(obj);
3777
Chris Wilson05394f32010-11-08 19:18:58 +00003778 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003779 i915_gem_detach_phys_object(dev, obj);
3780
Chris Wilsonbe726152010-07-23 23:18:50 +01003781 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003782}
3783
Jesse Barnes5669fca2009-02-17 15:13:31 -08003784int
Eric Anholt673a3942008-07-30 12:06:12 -07003785i915_gem_idle(struct drm_device *dev)
3786{
3787 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003788 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003789
Keith Packard6dbe2772008-10-14 21:41:13 -07003790 mutex_lock(&dev->struct_mutex);
3791
Chris Wilson87acb0a2010-10-19 10:13:00 +01003792 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003793 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003794 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003795 }
Eric Anholt673a3942008-07-30 12:06:12 -07003796
Chris Wilson29105cc2010-01-07 10:39:13 +00003797 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003798 if (ret) {
3799 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003800 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003801 }
Eric Anholt673a3942008-07-30 12:06:12 -07003802
Chris Wilson29105cc2010-01-07 10:39:13 +00003803 /* Under UMS, be paranoid and evict. */
3804 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003805 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003806 if (ret) {
3807 mutex_unlock(&dev->struct_mutex);
3808 return ret;
3809 }
3810 }
3811
Chris Wilson312817a2010-11-22 11:50:11 +00003812 i915_gem_reset_fences(dev);
3813
Chris Wilson29105cc2010-01-07 10:39:13 +00003814 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3815 * We need to replace this with a semaphore, or something.
3816 * And not confound mm.suspended!
3817 */
3818 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003819 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003820
3821 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003822 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003823
Keith Packard6dbe2772008-10-14 21:41:13 -07003824 mutex_unlock(&dev->struct_mutex);
3825
Chris Wilson29105cc2010-01-07 10:39:13 +00003826 /* Cancel the retire work handler, which should be idle now. */
3827 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3828
Eric Anholt673a3942008-07-30 12:06:12 -07003829 return 0;
3830}
3831
Eric Anholt673a3942008-07-30 12:06:12 -07003832int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003833i915_gem_init_ringbuffer(struct drm_device *dev)
3834{
3835 drm_i915_private_t *dev_priv = dev->dev_private;
3836 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003837
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003838 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003839 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003840 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003841
3842 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003843 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003844 if (ret)
3845 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003846 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003847
Chris Wilson549f7362010-10-19 11:19:32 +01003848 if (HAS_BLT(dev)) {
3849 ret = intel_init_blt_ring_buffer(dev);
3850 if (ret)
3851 goto cleanup_bsd_ring;
3852 }
3853
Chris Wilson6f392d52010-08-07 11:01:22 +01003854 dev_priv->next_seqno = 1;
3855
Chris Wilson68f95ba2010-05-27 13:18:22 +01003856 return 0;
3857
Chris Wilson549f7362010-10-19 11:19:32 +01003858cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003859 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003860cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003861 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003862 return ret;
3863}
3864
3865void
3866i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3867{
3868 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003869 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003870
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003871 for (i = 0; i < I915_NUM_RINGS; i++)
3872 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003873}
3874
3875int
Eric Anholt673a3942008-07-30 12:06:12 -07003876i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3877 struct drm_file *file_priv)
3878{
3879 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003880 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003881
Jesse Barnes79e53942008-11-07 14:24:08 -08003882 if (drm_core_check_feature(dev, DRIVER_MODESET))
3883 return 0;
3884
Ben Gamariba1234d2009-09-14 17:48:47 -04003885 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003886 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003887 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003888 }
3889
Eric Anholt673a3942008-07-30 12:06:12 -07003890 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003891 dev_priv->mm.suspended = 0;
3892
3893 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003894 if (ret != 0) {
3895 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003896 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003897 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003898
Chris Wilson69dc4982010-10-19 10:36:51 +01003899 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003900 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3901 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003902 for (i = 0; i < I915_NUM_RINGS; i++) {
3903 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3904 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3905 }
Eric Anholt673a3942008-07-30 12:06:12 -07003906 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003907
Chris Wilson5f353082010-06-07 14:03:03 +01003908 ret = drm_irq_install(dev);
3909 if (ret)
3910 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003911
Eric Anholt673a3942008-07-30 12:06:12 -07003912 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003913
3914cleanup_ringbuffer:
3915 mutex_lock(&dev->struct_mutex);
3916 i915_gem_cleanup_ringbuffer(dev);
3917 dev_priv->mm.suspended = 1;
3918 mutex_unlock(&dev->struct_mutex);
3919
3920 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003921}
3922
3923int
3924i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3925 struct drm_file *file_priv)
3926{
Jesse Barnes79e53942008-11-07 14:24:08 -08003927 if (drm_core_check_feature(dev, DRIVER_MODESET))
3928 return 0;
3929
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003930 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003931 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003932}
3933
3934void
3935i915_gem_lastclose(struct drm_device *dev)
3936{
3937 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003938
Eric Anholte806b492009-01-22 09:56:58 -08003939 if (drm_core_check_feature(dev, DRIVER_MODESET))
3940 return;
3941
Keith Packard6dbe2772008-10-14 21:41:13 -07003942 ret = i915_gem_idle(dev);
3943 if (ret)
3944 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003945}
3946
Chris Wilson64193402010-10-24 12:38:05 +01003947static void
3948init_ring_lists(struct intel_ring_buffer *ring)
3949{
3950 INIT_LIST_HEAD(&ring->active_list);
3951 INIT_LIST_HEAD(&ring->request_list);
3952 INIT_LIST_HEAD(&ring->gpu_write_list);
3953}
3954
Eric Anholt673a3942008-07-30 12:06:12 -07003955void
3956i915_gem_load(struct drm_device *dev)
3957{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003958 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003959 drm_i915_private_t *dev_priv = dev->dev_private;
3960
Chris Wilson69dc4982010-10-19 10:36:51 +01003961 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003962 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3963 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003964 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003965 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003966 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003967 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003968 for (i = 0; i < I915_NUM_RINGS; i++)
3969 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003970 for (i = 0; i < 16; i++)
3971 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003972 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3973 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003974 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003975
Dave Airlie94400122010-07-20 13:15:31 +10003976 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3977 if (IS_GEN3(dev)) {
3978 u32 tmp = I915_READ(MI_ARB_STATE);
3979 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3980 /* arb state is a masked write, so set bit + bit in mask */
3981 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3982 I915_WRITE(MI_ARB_STATE, tmp);
3983 }
3984 }
3985
Chris Wilson72bfa192010-12-19 11:42:05 +00003986 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3987
Jesse Barnesde151cf2008-11-12 10:03:55 -08003988 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003989 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3990 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003991
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003992 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003993 dev_priv->num_fence_regs = 16;
3994 else
3995 dev_priv->num_fence_regs = 8;
3996
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003997 /* Initialize fence registers to zero */
Eric Anholt10ed13e2011-05-06 13:53:49 -07003998 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3999 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004000 }
Eric Anholt10ed13e2011-05-06 13:53:49 -07004001
Eric Anholt673a3942008-07-30 12:06:12 -07004002 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004003 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004004
Chris Wilsonce453d82011-02-21 14:43:56 +00004005 dev_priv->mm.interruptible = true;
4006
Chris Wilson17250b72010-10-28 12:51:39 +01004007 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4008 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4009 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004010}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004011
4012/*
4013 * Create a physically contiguous memory object for this object
4014 * e.g. for cursor + overlay regs
4015 */
Chris Wilson995b6762010-08-20 13:23:26 +01004016static int i915_gem_init_phys_object(struct drm_device *dev,
4017 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004018{
4019 drm_i915_private_t *dev_priv = dev->dev_private;
4020 struct drm_i915_gem_phys_object *phys_obj;
4021 int ret;
4022
4023 if (dev_priv->mm.phys_objs[id - 1] || !size)
4024 return 0;
4025
Eric Anholt9a298b22009-03-24 12:23:04 -07004026 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004027 if (!phys_obj)
4028 return -ENOMEM;
4029
4030 phys_obj->id = id;
4031
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004032 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004033 if (!phys_obj->handle) {
4034 ret = -ENOMEM;
4035 goto kfree_obj;
4036 }
4037#ifdef CONFIG_X86
4038 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4039#endif
4040
4041 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4042
4043 return 0;
4044kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004045 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004046 return ret;
4047}
4048
Chris Wilson995b6762010-08-20 13:23:26 +01004049static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004050{
4051 drm_i915_private_t *dev_priv = dev->dev_private;
4052 struct drm_i915_gem_phys_object *phys_obj;
4053
4054 if (!dev_priv->mm.phys_objs[id - 1])
4055 return;
4056
4057 phys_obj = dev_priv->mm.phys_objs[id - 1];
4058 if (phys_obj->cur_obj) {
4059 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4060 }
4061
4062#ifdef CONFIG_X86
4063 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4064#endif
4065 drm_pci_free(dev, phys_obj->handle);
4066 kfree(phys_obj);
4067 dev_priv->mm.phys_objs[id - 1] = NULL;
4068}
4069
4070void i915_gem_free_all_phys_object(struct drm_device *dev)
4071{
4072 int i;
4073
Dave Airlie260883c2009-01-22 17:58:49 +10004074 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004075 i915_gem_free_phys_object(dev, i);
4076}
4077
4078void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004079 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004080{
Chris Wilson05394f32010-11-08 19:18:58 +00004081 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004082 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004083 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004084 int page_count;
4085
Chris Wilson05394f32010-11-08 19:18:58 +00004086 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004087 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004088 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004089
Chris Wilson05394f32010-11-08 19:18:58 +00004090 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004091 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004092 struct page *page = read_cache_page_gfp(mapping, i,
4093 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4094 if (!IS_ERR(page)) {
4095 char *dst = kmap_atomic(page);
4096 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4097 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004098
Chris Wilsone5281cc2010-10-28 13:45:36 +01004099 drm_clflush_pages(&page, 1);
4100
4101 set_page_dirty(page);
4102 mark_page_accessed(page);
4103 page_cache_release(page);
4104 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004105 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004106 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004107
Chris Wilson05394f32010-11-08 19:18:58 +00004108 obj->phys_obj->cur_obj = NULL;
4109 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004110}
4111
4112int
4113i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004114 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004115 int id,
4116 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004117{
Chris Wilson05394f32010-11-08 19:18:58 +00004118 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004119 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004120 int ret = 0;
4121 int page_count;
4122 int i;
4123
4124 if (id > I915_MAX_PHYS_OBJECT)
4125 return -EINVAL;
4126
Chris Wilson05394f32010-11-08 19:18:58 +00004127 if (obj->phys_obj) {
4128 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004129 return 0;
4130 i915_gem_detach_phys_object(dev, obj);
4131 }
4132
Dave Airlie71acb5e2008-12-30 20:31:46 +10004133 /* create a new object */
4134 if (!dev_priv->mm.phys_objs[id - 1]) {
4135 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004136 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004137 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004138 DRM_ERROR("failed to init phys object %d size: %zu\n",
4139 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004140 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004141 }
4142 }
4143
4144 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004145 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4146 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004147
Chris Wilson05394f32010-11-08 19:18:58 +00004148 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004149
4150 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004151 struct page *page;
4152 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004153
Chris Wilsone5281cc2010-10-28 13:45:36 +01004154 page = read_cache_page_gfp(mapping, i,
4155 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4156 if (IS_ERR(page))
4157 return PTR_ERR(page);
4158
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004159 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004160 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004161 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004162 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004163
4164 mark_page_accessed(page);
4165 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004166 }
4167
4168 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004169}
4170
4171static int
Chris Wilson05394f32010-11-08 19:18:58 +00004172i915_gem_phys_pwrite(struct drm_device *dev,
4173 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004174 struct drm_i915_gem_pwrite *args,
4175 struct drm_file *file_priv)
4176{
Chris Wilson05394f32010-11-08 19:18:58 +00004177 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004178 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004179
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004180 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4181 unsigned long unwritten;
4182
4183 /* The physical object once assigned is fixed for the lifetime
4184 * of the obj, so we can safely drop the lock and continue
4185 * to access vaddr.
4186 */
4187 mutex_unlock(&dev->struct_mutex);
4188 unwritten = copy_from_user(vaddr, user_data, args->size);
4189 mutex_lock(&dev->struct_mutex);
4190 if (unwritten)
4191 return -EFAULT;
4192 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004193
Daniel Vetter40ce6572010-11-05 18:12:18 +01004194 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004195 return 0;
4196}
Eric Anholtb9624422009-06-03 07:27:35 +00004197
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004198void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004199{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004200 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004201
4202 /* Clean up our request list when the client is going away, so that
4203 * later retire_requests won't dereference our soon-to-be-gone
4204 * file_priv.
4205 */
Chris Wilson1c255952010-09-26 11:03:27 +01004206 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004207 while (!list_empty(&file_priv->mm.request_list)) {
4208 struct drm_i915_gem_request *request;
4209
4210 request = list_first_entry(&file_priv->mm.request_list,
4211 struct drm_i915_gem_request,
4212 client_list);
4213 list_del(&request->client_list);
4214 request->file_priv = NULL;
4215 }
Chris Wilson1c255952010-09-26 11:03:27 +01004216 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004217}
Chris Wilson31169712009-09-14 16:50:28 +01004218
Chris Wilson31169712009-09-14 16:50:28 +01004219static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004220i915_gpu_is_active(struct drm_device *dev)
4221{
4222 drm_i915_private_t *dev_priv = dev->dev_private;
4223 int lists_empty;
4224
Chris Wilson1637ef42010-04-20 17:10:35 +01004225 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004226 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004227
4228 return !lists_empty;
4229}
4230
4231static int
Ying Han1495f232011-05-24 17:12:27 -07004232i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004233{
Chris Wilson17250b72010-10-28 12:51:39 +01004234 struct drm_i915_private *dev_priv =
4235 container_of(shrinker,
4236 struct drm_i915_private,
4237 mm.inactive_shrinker);
4238 struct drm_device *dev = dev_priv->dev;
4239 struct drm_i915_gem_object *obj, *next;
Ying Han1495f232011-05-24 17:12:27 -07004240 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01004241 int cnt;
4242
4243 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004244 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004245
4246 /* "fast-path" to count number of available objects */
4247 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01004248 cnt = 0;
4249 list_for_each_entry(obj,
4250 &dev_priv->mm.inactive_list,
4251 mm_list)
4252 cnt++;
4253 mutex_unlock(&dev->struct_mutex);
4254 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004255 }
4256
Chris Wilson1637ef42010-04-20 17:10:35 +01004257rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004258 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01004259 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01004260
Chris Wilson17250b72010-10-28 12:51:39 +01004261 list_for_each_entry_safe(obj, next,
4262 &dev_priv->mm.inactive_list,
4263 mm_list) {
4264 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00004265 if (i915_gem_object_unbind(obj) == 0 &&
4266 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004267 break;
Chris Wilson31169712009-09-14 16:50:28 +01004268 }
Chris Wilson31169712009-09-14 16:50:28 +01004269 }
4270
4271 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01004272 cnt = 0;
4273 list_for_each_entry_safe(obj, next,
4274 &dev_priv->mm.inactive_list,
4275 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00004276 if (nr_to_scan &&
4277 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004278 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00004279 else
Chris Wilson17250b72010-10-28 12:51:39 +01004280 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01004281 }
4282
Chris Wilson17250b72010-10-28 12:51:39 +01004283 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01004284 /*
4285 * We are desperate for pages, so as a last resort, wait
4286 * for the GPU to finish and discard whatever we can.
4287 * This has a dramatic impact to reduce the number of
4288 * OOM-killer events whilst running the GPU aggressively.
4289 */
Chris Wilson17250b72010-10-28 12:51:39 +01004290 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01004291 goto rescan;
4292 }
Chris Wilson17250b72010-10-28 12:51:39 +01004293 mutex_unlock(&dev->struct_mutex);
4294 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004295}