blob: e46f2734acc5861753711b4cf72bb1b3f6d28e77 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Hugh Dickins5949eac2011-06-27 16:18:18 -070034#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070036#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Chris Wilson88241782011-01-07 17:09:48 +000039static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson05394f32010-11-08 19:18:58 +000040static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
41static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000042static __must_check int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
43 bool write);
44static __must_check int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
45 uint64_t offset,
46 uint64_t size);
Chris Wilson05394f32010-11-08 19:18:58 +000047static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000048static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
49 unsigned alignment,
50 bool map_and_fenceable);
Chris Wilsond9e86c02010-11-10 16:40:20 +000051static void i915_gem_clear_fence_reg(struct drm_device *dev,
52 struct drm_i915_fence_reg *reg);
Chris Wilson05394f32010-11-08 19:18:58 +000053static int i915_gem_phys_pwrite(struct drm_device *dev,
54 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100055 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000056 struct drm_file *file);
57static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070058
Chris Wilson17250b72010-10-28 12:51:39 +010059static int i915_gem_inactive_shrink(struct shrinker *shrinker,
Ying Han1495f232011-05-24 17:12:27 -070060 struct shrink_control *sc);
Chris Wilson31169712009-09-14 16:50:28 +010061
Chris Wilson73aa8082010-09-30 11:46:12 +010062/* some bookkeeping */
63static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
64 size_t size)
65{
66 dev_priv->mm.object_count++;
67 dev_priv->mm.object_memory += size;
68}
69
70static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
71 size_t size)
72{
73 dev_priv->mm.object_count--;
74 dev_priv->mm.object_memory -= size;
75}
76
Chris Wilson21dd3732011-01-26 15:55:56 +000077static int
78i915_gem_wait_for_error(struct drm_device *dev)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010079{
80 struct drm_i915_private *dev_priv = dev->dev_private;
81 struct completion *x = &dev_priv->error_completion;
82 unsigned long flags;
83 int ret;
84
85 if (!atomic_read(&dev_priv->mm.wedged))
86 return 0;
87
88 ret = wait_for_completion_interruptible(x);
89 if (ret)
90 return ret;
91
Chris Wilson21dd3732011-01-26 15:55:56 +000092 if (atomic_read(&dev_priv->mm.wedged)) {
93 /* GPU is hung, bump the completion count to account for
94 * the token we just consumed so that we never hit zero and
95 * end up waiting upon a subsequent completion event that
96 * will never happen.
97 */
98 spin_lock_irqsave(&x->wait.lock, flags);
99 x->done++;
100 spin_unlock_irqrestore(&x->wait.lock, flags);
101 }
102 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100103}
104
Chris Wilson54cf91d2010-11-25 18:00:26 +0000105int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100106{
Chris Wilson76c1dec2010-09-25 11:22:51 +0100107 int ret;
108
Chris Wilson21dd3732011-01-26 15:55:56 +0000109 ret = i915_gem_wait_for_error(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100110 if (ret)
111 return ret;
112
113 ret = mutex_lock_interruptible(&dev->struct_mutex);
114 if (ret)
115 return ret;
116
Chris Wilson23bc5982010-09-29 16:10:57 +0100117 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100118 return 0;
119}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100120
Chris Wilson7d1c4802010-08-07 21:45:03 +0100121static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000122i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100123{
Chris Wilson05394f32010-11-08 19:18:58 +0000124 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100125}
126
Chris Wilson20217462010-11-23 15:26:33 +0000127void i915_gem_do_init(struct drm_device *dev,
128 unsigned long start,
129 unsigned long mappable_end,
130 unsigned long end)
Jesse Barnes79e53942008-11-07 14:24:08 -0800131{
132 drm_i915_private_t *dev_priv = dev->dev_private;
133
Chris Wilsonbee4a182011-01-21 10:54:32 +0000134 drm_mm_init(&dev_priv->mm.gtt_space, start, end - start);
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Chris Wilsonbee4a182011-01-21 10:54:32 +0000136 dev_priv->mm.gtt_start = start;
137 dev_priv->mm.gtt_mappable_end = mappable_end;
138 dev_priv->mm.gtt_end = end;
Chris Wilson73aa8082010-09-30 11:46:12 +0100139 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200140 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Chris Wilsonbee4a182011-01-21 10:54:32 +0000141
142 /* Take over this portion of the GTT */
143 intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
Jesse Barnes79e53942008-11-07 14:24:08 -0800144}
Keith Packard6dbe2772008-10-14 21:41:13 -0700145
Eric Anholt673a3942008-07-30 12:06:12 -0700146int
147i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000148 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700149{
Eric Anholt673a3942008-07-30 12:06:12 -0700150 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000151
152 if (args->gtt_start >= args->gtt_end ||
153 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
154 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700155
156 mutex_lock(&dev->struct_mutex);
Chris Wilson20217462010-11-23 15:26:33 +0000157 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700158 mutex_unlock(&dev->struct_mutex);
159
Chris Wilson20217462010-11-23 15:26:33 +0000160 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700161}
162
Eric Anholt5a125c32008-10-22 21:40:13 -0700163int
164i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000165 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700166{
Chris Wilson73aa8082010-09-30 11:46:12 +0100167 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700168 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000169 struct drm_i915_gem_object *obj;
170 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700171
172 if (!(dev->driver->driver_features & DRIVER_GEM))
173 return -ENODEV;
174
Chris Wilson6299f992010-11-24 12:23:44 +0000175 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100176 mutex_lock(&dev->struct_mutex);
Chris Wilson6299f992010-11-24 12:23:44 +0000177 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
178 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100179 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700180
Chris Wilson6299f992010-11-24 12:23:44 +0000181 args->aper_size = dev_priv->mm.gtt_total;
182 args->aper_available_size = args->aper_size -pinned;
183
Eric Anholt5a125c32008-10-22 21:40:13 -0700184 return 0;
185}
186
Dave Airlieff72145b2011-02-07 12:16:14 +1000187static int
188i915_gem_create(struct drm_file *file,
189 struct drm_device *dev,
190 uint64_t size,
191 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700192{
Chris Wilson05394f32010-11-08 19:18:58 +0000193 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300194 int ret;
195 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700196
Dave Airlieff72145b2011-02-07 12:16:14 +1000197 size = roundup(size, PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -0700198
199 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000200 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700201 if (obj == NULL)
202 return -ENOMEM;
203
Chris Wilson05394f32010-11-08 19:18:58 +0000204 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100205 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000206 drm_gem_object_release(&obj->base);
207 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100208 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700209 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100210 }
211
Chris Wilson202f2fe2010-10-14 13:20:40 +0100212 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000213 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100214 trace_i915_gem_object_create(obj);
215
Dave Airlieff72145b2011-02-07 12:16:14 +1000216 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700217 return 0;
218}
219
Dave Airlieff72145b2011-02-07 12:16:14 +1000220int
221i915_gem_dumb_create(struct drm_file *file,
222 struct drm_device *dev,
223 struct drm_mode_create_dumb *args)
224{
225 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000226 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000227 args->size = args->pitch * args->height;
228 return i915_gem_create(file, dev,
229 args->size, &args->handle);
230}
231
232int i915_gem_dumb_destroy(struct drm_file *file,
233 struct drm_device *dev,
234 uint32_t handle)
235{
236 return drm_gem_handle_delete(file, handle);
237}
238
239/**
240 * Creates a new mm object and returns a handle to it.
241 */
242int
243i915_gem_create_ioctl(struct drm_device *dev, void *data,
244 struct drm_file *file)
245{
246 struct drm_i915_gem_create *args = data;
247 return i915_gem_create(file, dev,
248 args->size, &args->handle);
249}
250
Chris Wilson05394f32010-11-08 19:18:58 +0000251static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700252{
Chris Wilson05394f32010-11-08 19:18:58 +0000253 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700254
255 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000256 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700257}
258
Chris Wilson99a03df2010-05-27 14:15:34 +0100259static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700260slow_shmem_copy(struct page *dst_page,
261 int dst_offset,
262 struct page *src_page,
263 int src_offset,
264 int length)
265{
266 char *dst_vaddr, *src_vaddr;
267
Chris Wilson99a03df2010-05-27 14:15:34 +0100268 dst_vaddr = kmap(dst_page);
269 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700270
271 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
272
Chris Wilson99a03df2010-05-27 14:15:34 +0100273 kunmap(src_page);
274 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700275}
276
Chris Wilson99a03df2010-05-27 14:15:34 +0100277static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700278slow_shmem_bit17_copy(struct page *gpu_page,
279 int gpu_offset,
280 struct page *cpu_page,
281 int cpu_offset,
282 int length,
283 int is_read)
284{
285 char *gpu_vaddr, *cpu_vaddr;
286
287 /* Use the unswizzled path if this page isn't affected. */
288 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
289 if (is_read)
290 return slow_shmem_copy(cpu_page, cpu_offset,
291 gpu_page, gpu_offset, length);
292 else
293 return slow_shmem_copy(gpu_page, gpu_offset,
294 cpu_page, cpu_offset, length);
295 }
296
Chris Wilson99a03df2010-05-27 14:15:34 +0100297 gpu_vaddr = kmap(gpu_page);
298 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700299
300 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
301 * XORing with the other bits (A9 for Y, A9 and A10 for X)
302 */
303 while (length > 0) {
304 int cacheline_end = ALIGN(gpu_offset + 1, 64);
305 int this_length = min(cacheline_end - gpu_offset, length);
306 int swizzled_gpu_offset = gpu_offset ^ 64;
307
308 if (is_read) {
309 memcpy(cpu_vaddr + cpu_offset,
310 gpu_vaddr + swizzled_gpu_offset,
311 this_length);
312 } else {
313 memcpy(gpu_vaddr + swizzled_gpu_offset,
314 cpu_vaddr + cpu_offset,
315 this_length);
316 }
317 cpu_offset += this_length;
318 gpu_offset += this_length;
319 length -= this_length;
320 }
321
Chris Wilson99a03df2010-05-27 14:15:34 +0100322 kunmap(cpu_page);
323 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700324}
325
Eric Anholt673a3942008-07-30 12:06:12 -0700326/**
Eric Anholteb014592009-03-10 11:44:52 -0700327 * This is the fast shmem pread path, which attempts to copy_from_user directly
328 * from the backing pages of the object to the user's address space. On a
329 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
330 */
331static int
Chris Wilson05394f32010-11-08 19:18:58 +0000332i915_gem_shmem_pread_fast(struct drm_device *dev,
333 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700334 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000335 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700336{
Chris Wilson05394f32010-11-08 19:18:58 +0000337 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700338 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100339 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700340 char __user *user_data;
341 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700342
343 user_data = (char __user *) (uintptr_t) args->data_ptr;
344 remain = args->size;
345
Eric Anholteb014592009-03-10 11:44:52 -0700346 offset = args->offset;
347
348 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100349 struct page *page;
350 char *vaddr;
351 int ret;
352
Eric Anholteb014592009-03-10 11:44:52 -0700353 /* Operation in this page
354 *
Eric Anholteb014592009-03-10 11:44:52 -0700355 * page_offset = offset within page
356 * page_length = bytes to copy for this page
357 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100358 page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700359 page_length = remain;
360 if ((page_offset + remain) > PAGE_SIZE)
361 page_length = PAGE_SIZE - page_offset;
362
Hugh Dickins5949eac2011-06-27 16:18:18 -0700363 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100364 if (IS_ERR(page))
365 return PTR_ERR(page);
366
367 vaddr = kmap_atomic(page);
368 ret = __copy_to_user_inatomic(user_data,
369 vaddr + page_offset,
370 page_length);
371 kunmap_atomic(vaddr);
372
373 mark_page_accessed(page);
374 page_cache_release(page);
375 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100376 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700377
378 remain -= page_length;
379 user_data += page_length;
380 offset += page_length;
381 }
382
Chris Wilson4f27b752010-10-14 15:26:45 +0100383 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700384}
385
386/**
387 * This is the fallback shmem pread path, which allocates temporary storage
388 * in kernel space to copy_to_user into outside of the struct_mutex, so we
389 * can copy out of the object's backing pages while holding the struct mutex
390 * and not take page faults.
391 */
392static int
Chris Wilson05394f32010-11-08 19:18:58 +0000393i915_gem_shmem_pread_slow(struct drm_device *dev,
394 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700395 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000396 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700397{
Chris Wilson05394f32010-11-08 19:18:58 +0000398 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700399 struct mm_struct *mm = current->mm;
400 struct page **user_pages;
401 ssize_t remain;
402 loff_t offset, pinned_pages, i;
403 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100404 int shmem_page_offset;
405 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700406 int page_length;
407 int ret;
408 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700409 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700410
411 remain = args->size;
412
413 /* Pin the user pages containing the data. We can't fault while
414 * holding the struct mutex, yet we want to hold it while
415 * dereferencing the user data.
416 */
417 first_data_page = data_ptr / PAGE_SIZE;
418 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
419 num_pages = last_data_page - first_data_page + 1;
420
Chris Wilson4f27b752010-10-14 15:26:45 +0100421 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700422 if (user_pages == NULL)
423 return -ENOMEM;
424
Chris Wilson4f27b752010-10-14 15:26:45 +0100425 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700426 down_read(&mm->mmap_sem);
427 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700428 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700429 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100430 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700431 if (pinned_pages < num_pages) {
432 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100433 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700434 }
435
Chris Wilson4f27b752010-10-14 15:26:45 +0100436 ret = i915_gem_object_set_cpu_read_domain_range(obj,
437 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700438 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100439 if (ret)
440 goto out;
441
442 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700443
Eric Anholteb014592009-03-10 11:44:52 -0700444 offset = args->offset;
445
446 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100447 struct page *page;
448
Eric Anholteb014592009-03-10 11:44:52 -0700449 /* Operation in this page
450 *
Eric Anholteb014592009-03-10 11:44:52 -0700451 * shmem_page_offset = offset within page in shmem file
452 * data_page_index = page number in get_user_pages return
453 * data_page_offset = offset with data_page_index page.
454 * page_length = bytes to copy for this page
455 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100456 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700457 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100458 data_page_offset = offset_in_page(data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700459
460 page_length = remain;
461 if ((shmem_page_offset + page_length) > PAGE_SIZE)
462 page_length = PAGE_SIZE - shmem_page_offset;
463 if ((data_page_offset + page_length) > PAGE_SIZE)
464 page_length = PAGE_SIZE - data_page_offset;
465
Hugh Dickins5949eac2011-06-27 16:18:18 -0700466 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Jesper Juhlb65552f2011-06-12 20:53:44 +0000467 if (IS_ERR(page)) {
468 ret = PTR_ERR(page);
469 goto out;
470 }
Chris Wilsone5281cc2010-10-28 13:45:36 +0100471
Eric Anholt280b7132009-03-12 16:56:27 -0700472 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100473 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700474 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100475 user_pages[data_page_index],
476 data_page_offset,
477 page_length,
478 1);
479 } else {
480 slow_shmem_copy(user_pages[data_page_index],
481 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100482 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100483 shmem_page_offset,
484 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700485 }
Eric Anholteb014592009-03-10 11:44:52 -0700486
Chris Wilsone5281cc2010-10-28 13:45:36 +0100487 mark_page_accessed(page);
488 page_cache_release(page);
489
Eric Anholteb014592009-03-10 11:44:52 -0700490 remain -= page_length;
491 data_ptr += page_length;
492 offset += page_length;
493 }
494
Chris Wilson4f27b752010-10-14 15:26:45 +0100495out:
Eric Anholteb014592009-03-10 11:44:52 -0700496 for (i = 0; i < pinned_pages; i++) {
497 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100498 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700499 page_cache_release(user_pages[i]);
500 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700501 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700502
503 return ret;
504}
505
Eric Anholt673a3942008-07-30 12:06:12 -0700506/**
507 * Reads data from the object referenced by handle.
508 *
509 * On error, the contents of *data are undefined.
510 */
511int
512i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000513 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700514{
515 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000516 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100517 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700518
Chris Wilson51311d02010-11-17 09:10:42 +0000519 if (args->size == 0)
520 return 0;
521
522 if (!access_ok(VERIFY_WRITE,
523 (char __user *)(uintptr_t)args->data_ptr,
524 args->size))
525 return -EFAULT;
526
527 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
528 args->size);
529 if (ret)
530 return -EFAULT;
531
Chris Wilson4f27b752010-10-14 15:26:45 +0100532 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100533 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100534 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700535
Chris Wilson05394f32010-11-08 19:18:58 +0000536 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000537 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100538 ret = -ENOENT;
539 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100540 }
Eric Anholt673a3942008-07-30 12:06:12 -0700541
Chris Wilson7dcd2492010-09-26 20:21:44 +0100542 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000543 if (args->offset > obj->base.size ||
544 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100545 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100546 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100547 }
548
Chris Wilsondb53a302011-02-03 11:57:46 +0000549 trace_i915_gem_object_pread(obj, args->offset, args->size);
550
Chris Wilson4f27b752010-10-14 15:26:45 +0100551 ret = i915_gem_object_set_cpu_read_domain_range(obj,
552 args->offset,
553 args->size);
554 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100555 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100556
557 ret = -EFAULT;
558 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000559 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100560 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000561 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700562
Chris Wilson35b62a82010-09-26 20:23:38 +0100563out:
Chris Wilson05394f32010-11-08 19:18:58 +0000564 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100565unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100566 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700567 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700568}
569
Keith Packard0839ccb2008-10-30 19:38:48 -0700570/* This is the fast write path which cannot handle
571 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700572 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700573
Keith Packard0839ccb2008-10-30 19:38:48 -0700574static inline int
575fast_user_write(struct io_mapping *mapping,
576 loff_t page_base, int page_offset,
577 char __user *user_data,
578 int length)
579{
580 char *vaddr_atomic;
581 unsigned long unwritten;
582
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700583 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700584 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
585 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700586 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100587 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700588}
589
590/* Here's the write path which can sleep for
591 * page faults
592 */
593
Chris Wilsonab34c222010-05-27 14:15:35 +0100594static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700595slow_kernel_write(struct io_mapping *mapping,
596 loff_t gtt_base, int gtt_offset,
597 struct page *user_page, int user_offset,
598 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700599{
Chris Wilsonab34c222010-05-27 14:15:35 +0100600 char __iomem *dst_vaddr;
601 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700602
Chris Wilsonab34c222010-05-27 14:15:35 +0100603 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
604 src_vaddr = kmap(user_page);
605
606 memcpy_toio(dst_vaddr + gtt_offset,
607 src_vaddr + user_offset,
608 length);
609
610 kunmap(user_page);
611 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700612}
613
Eric Anholt3de09aa2009-03-09 09:42:23 -0700614/**
615 * This is the fast pwrite path, where we copy the data directly from the
616 * user into the GTT, uncached.
617 */
Eric Anholt673a3942008-07-30 12:06:12 -0700618static int
Chris Wilson05394f32010-11-08 19:18:58 +0000619i915_gem_gtt_pwrite_fast(struct drm_device *dev,
620 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700621 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000622 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700623{
Keith Packard0839ccb2008-10-30 19:38:48 -0700624 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700625 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700626 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700627 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700628 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700629
630 user_data = (char __user *) (uintptr_t) args->data_ptr;
631 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700632
Chris Wilson05394f32010-11-08 19:18:58 +0000633 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700634
635 while (remain > 0) {
636 /* Operation in this page
637 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700638 * page_base = page offset within aperture
639 * page_offset = offset within page
640 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700641 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100642 page_base = offset & PAGE_MASK;
643 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700644 page_length = remain;
645 if ((page_offset + remain) > PAGE_SIZE)
646 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700647
Keith Packard0839ccb2008-10-30 19:38:48 -0700648 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700649 * source page isn't available. Return the error and we'll
650 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700651 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100652 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
653 page_offset, user_data, page_length))
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100654 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700655
Keith Packard0839ccb2008-10-30 19:38:48 -0700656 remain -= page_length;
657 user_data += page_length;
658 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700659 }
Eric Anholt673a3942008-07-30 12:06:12 -0700660
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100661 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700662}
663
Eric Anholt3de09aa2009-03-09 09:42:23 -0700664/**
665 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
666 * the memory and maps it using kmap_atomic for copying.
667 *
668 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
669 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
670 */
Eric Anholt3043c602008-10-02 12:24:47 -0700671static int
Chris Wilson05394f32010-11-08 19:18:58 +0000672i915_gem_gtt_pwrite_slow(struct drm_device *dev,
673 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700674 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000675 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700676{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700677 drm_i915_private_t *dev_priv = dev->dev_private;
678 ssize_t remain;
679 loff_t gtt_page_base, offset;
680 loff_t first_data_page, last_data_page, num_pages;
681 loff_t pinned_pages, i;
682 struct page **user_pages;
683 struct mm_struct *mm = current->mm;
684 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700685 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700686 uint64_t data_ptr = args->data_ptr;
687
688 remain = args->size;
689
690 /* Pin the user pages containing the data. We can't fault while
691 * holding the struct mutex, and all of the pwrite implementations
692 * want to hold it while dereferencing the user data.
693 */
694 first_data_page = data_ptr / PAGE_SIZE;
695 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
696 num_pages = last_data_page - first_data_page + 1;
697
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100698 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700699 if (user_pages == NULL)
700 return -ENOMEM;
701
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100702 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700703 down_read(&mm->mmap_sem);
704 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
705 num_pages, 0, 0, user_pages, NULL);
706 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100707 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700708 if (pinned_pages < num_pages) {
709 ret = -EFAULT;
710 goto out_unpin_pages;
711 }
712
Chris Wilsond9e86c02010-11-10 16:40:20 +0000713 ret = i915_gem_object_set_to_gtt_domain(obj, true);
714 if (ret)
715 goto out_unpin_pages;
716
717 ret = i915_gem_object_put_fence(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700718 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100719 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700720
Chris Wilson05394f32010-11-08 19:18:58 +0000721 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700722
723 while (remain > 0) {
724 /* Operation in this page
725 *
726 * gtt_page_base = page offset within aperture
727 * gtt_page_offset = offset within page in aperture
728 * data_page_index = page number in get_user_pages return
729 * data_page_offset = offset with data_page_index page.
730 * page_length = bytes to copy for this page
731 */
732 gtt_page_base = offset & PAGE_MASK;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100733 gtt_page_offset = offset_in_page(offset);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700734 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100735 data_page_offset = offset_in_page(data_ptr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700736
737 page_length = remain;
738 if ((gtt_page_offset + page_length) > PAGE_SIZE)
739 page_length = PAGE_SIZE - gtt_page_offset;
740 if ((data_page_offset + page_length) > PAGE_SIZE)
741 page_length = PAGE_SIZE - data_page_offset;
742
Chris Wilsonab34c222010-05-27 14:15:35 +0100743 slow_kernel_write(dev_priv->mm.gtt_mapping,
744 gtt_page_base, gtt_page_offset,
745 user_pages[data_page_index],
746 data_page_offset,
747 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700748
749 remain -= page_length;
750 offset += page_length;
751 data_ptr += page_length;
752 }
753
Eric Anholt3de09aa2009-03-09 09:42:23 -0700754out_unpin_pages:
755 for (i = 0; i < pinned_pages; i++)
756 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700757 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700758
759 return ret;
760}
761
Eric Anholt40123c12009-03-09 13:42:30 -0700762/**
763 * This is the fast shmem pwrite path, which attempts to directly
764 * copy_from_user into the kmapped pages backing the object.
765 */
Eric Anholt673a3942008-07-30 12:06:12 -0700766static int
Chris Wilson05394f32010-11-08 19:18:58 +0000767i915_gem_shmem_pwrite_fast(struct drm_device *dev,
768 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700769 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000770 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700771{
Chris Wilson05394f32010-11-08 19:18:58 +0000772 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700773 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100774 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700775 char __user *user_data;
776 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700777
778 user_data = (char __user *) (uintptr_t) args->data_ptr;
779 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700780
Eric Anholt673a3942008-07-30 12:06:12 -0700781 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000782 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700783
Eric Anholt40123c12009-03-09 13:42:30 -0700784 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100785 struct page *page;
786 char *vaddr;
787 int ret;
788
Eric Anholt40123c12009-03-09 13:42:30 -0700789 /* Operation in this page
790 *
Eric Anholt40123c12009-03-09 13:42:30 -0700791 * page_offset = offset within page
792 * page_length = bytes to copy for this page
793 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100794 page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700795 page_length = remain;
796 if ((page_offset + remain) > PAGE_SIZE)
797 page_length = PAGE_SIZE - page_offset;
798
Hugh Dickins5949eac2011-06-27 16:18:18 -0700799 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100800 if (IS_ERR(page))
801 return PTR_ERR(page);
802
803 vaddr = kmap_atomic(page, KM_USER0);
804 ret = __copy_from_user_inatomic(vaddr + page_offset,
805 user_data,
806 page_length);
807 kunmap_atomic(vaddr, KM_USER0);
808
809 set_page_dirty(page);
810 mark_page_accessed(page);
811 page_cache_release(page);
812
813 /* If we get a fault while copying data, then (presumably) our
814 * source page isn't available. Return the error and we'll
815 * retry in the slow path.
816 */
817 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100818 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700819
820 remain -= page_length;
821 user_data += page_length;
822 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700823 }
824
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100825 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700826}
827
828/**
829 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
830 * the memory and maps it using kmap_atomic for copying.
831 *
832 * This avoids taking mmap_sem for faulting on the user's address while the
833 * struct_mutex is held.
834 */
835static int
Chris Wilson05394f32010-11-08 19:18:58 +0000836i915_gem_shmem_pwrite_slow(struct drm_device *dev,
837 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700838 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000839 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700840{
Chris Wilson05394f32010-11-08 19:18:58 +0000841 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700842 struct mm_struct *mm = current->mm;
843 struct page **user_pages;
844 ssize_t remain;
845 loff_t offset, pinned_pages, i;
846 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100847 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700848 int data_page_index, data_page_offset;
849 int page_length;
850 int ret;
851 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700852 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700853
854 remain = args->size;
855
856 /* Pin the user pages containing the data. We can't fault while
857 * holding the struct mutex, and all of the pwrite implementations
858 * want to hold it while dereferencing the user data.
859 */
860 first_data_page = data_ptr / PAGE_SIZE;
861 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
862 num_pages = last_data_page - first_data_page + 1;
863
Chris Wilson4f27b752010-10-14 15:26:45 +0100864 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700865 if (user_pages == NULL)
866 return -ENOMEM;
867
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100868 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700869 down_read(&mm->mmap_sem);
870 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
871 num_pages, 0, 0, user_pages, NULL);
872 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100873 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700874 if (pinned_pages < num_pages) {
875 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100876 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700877 }
878
Eric Anholt40123c12009-03-09 13:42:30 -0700879 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100880 if (ret)
881 goto out;
882
883 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700884
Eric Anholt40123c12009-03-09 13:42:30 -0700885 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000886 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700887
888 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100889 struct page *page;
890
Eric Anholt40123c12009-03-09 13:42:30 -0700891 /* Operation in this page
892 *
Eric Anholt40123c12009-03-09 13:42:30 -0700893 * shmem_page_offset = offset within page in shmem file
894 * data_page_index = page number in get_user_pages return
895 * data_page_offset = offset with data_page_index page.
896 * page_length = bytes to copy for this page
897 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100898 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700899 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100900 data_page_offset = offset_in_page(data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700901
902 page_length = remain;
903 if ((shmem_page_offset + page_length) > PAGE_SIZE)
904 page_length = PAGE_SIZE - shmem_page_offset;
905 if ((data_page_offset + page_length) > PAGE_SIZE)
906 page_length = PAGE_SIZE - data_page_offset;
907
Hugh Dickins5949eac2011-06-27 16:18:18 -0700908 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100909 if (IS_ERR(page)) {
910 ret = PTR_ERR(page);
911 goto out;
912 }
913
Eric Anholt280b7132009-03-12 16:56:27 -0700914 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100915 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700916 shmem_page_offset,
917 user_pages[data_page_index],
918 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100919 page_length,
920 0);
921 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100922 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100923 shmem_page_offset,
924 user_pages[data_page_index],
925 data_page_offset,
926 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700927 }
Eric Anholt40123c12009-03-09 13:42:30 -0700928
Chris Wilsone5281cc2010-10-28 13:45:36 +0100929 set_page_dirty(page);
930 mark_page_accessed(page);
931 page_cache_release(page);
932
Eric Anholt40123c12009-03-09 13:42:30 -0700933 remain -= page_length;
934 data_ptr += page_length;
935 offset += page_length;
936 }
937
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100938out:
Eric Anholt40123c12009-03-09 13:42:30 -0700939 for (i = 0; i < pinned_pages; i++)
940 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700941 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700942
943 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700944}
945
946/**
947 * Writes data to the object referenced by handle.
948 *
949 * On error, the contents of the buffer that were to be modified are undefined.
950 */
951int
952i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100953 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700954{
955 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000956 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000957 int ret;
958
959 if (args->size == 0)
960 return 0;
961
962 if (!access_ok(VERIFY_READ,
963 (char __user *)(uintptr_t)args->data_ptr,
964 args->size))
965 return -EFAULT;
966
967 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
968 args->size);
969 if (ret)
970 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700971
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100972 ret = i915_mutex_lock_interruptible(dev);
973 if (ret)
974 return ret;
975
Chris Wilson05394f32010-11-08 19:18:58 +0000976 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000977 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100978 ret = -ENOENT;
979 goto unlock;
980 }
Eric Anholt673a3942008-07-30 12:06:12 -0700981
Chris Wilson7dcd2492010-09-26 20:21:44 +0100982 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000983 if (args->offset > obj->base.size ||
984 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100985 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100986 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100987 }
988
Chris Wilsondb53a302011-02-03 11:57:46 +0000989 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
990
Eric Anholt673a3942008-07-30 12:06:12 -0700991 /* We can only do the GTT pwrite on untiled buffers, as otherwise
992 * it would end up going through the fenced access, and we'll get
993 * different detiling behavior between reading and writing.
994 * pread/pwrite currently are reading and writing from the CPU
995 * perspective, requiring manual detiling by the client.
996 */
Chris Wilson05394f32010-11-08 19:18:58 +0000997 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100998 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilsond9e86c02010-11-10 16:40:20 +0000999 else if (obj->gtt_space &&
Chris Wilson05394f32010-11-08 19:18:58 +00001000 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001001 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001002 if (ret)
1003 goto out;
1004
Chris Wilsond9e86c02010-11-10 16:40:20 +00001005 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1006 if (ret)
1007 goto out_unpin;
1008
1009 ret = i915_gem_object_put_fence(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001010 if (ret)
1011 goto out_unpin;
1012
1013 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1014 if (ret == -EFAULT)
1015 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1016
1017out_unpin:
1018 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001019 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001020 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1021 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001022 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001023
1024 ret = -EFAULT;
1025 if (!i915_gem_object_needs_bit17_swizzle(obj))
1026 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1027 if (ret == -EFAULT)
1028 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001029 }
Eric Anholt673a3942008-07-30 12:06:12 -07001030
Chris Wilson35b62a82010-09-26 20:23:38 +01001031out:
Chris Wilson05394f32010-11-08 19:18:58 +00001032 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001033unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001034 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001035 return ret;
1036}
1037
1038/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001039 * Called when user space prepares to use an object with the CPU, either
1040 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001041 */
1042int
1043i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001044 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001045{
1046 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001047 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001048 uint32_t read_domains = args->read_domains;
1049 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001050 int ret;
1051
1052 if (!(dev->driver->driver_features & DRIVER_GEM))
1053 return -ENODEV;
1054
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001055 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001056 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001057 return -EINVAL;
1058
Chris Wilson21d509e2009-06-06 09:46:02 +01001059 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001060 return -EINVAL;
1061
1062 /* Having something in the write domain implies it's in the read
1063 * domain, and only that read domain. Enforce that in the request.
1064 */
1065 if (write_domain != 0 && read_domains != write_domain)
1066 return -EINVAL;
1067
Chris Wilson76c1dec2010-09-25 11:22:51 +01001068 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001069 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001070 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001071
Chris Wilson05394f32010-11-08 19:18:58 +00001072 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001073 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001074 ret = -ENOENT;
1075 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001076 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001077
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001078 if (read_domains & I915_GEM_DOMAIN_GTT) {
1079 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001080
1081 /* Silently promote "you're not bound, there was nothing to do"
1082 * to success, since the client was just asking us to
1083 * make sure everything was done.
1084 */
1085 if (ret == -EINVAL)
1086 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001087 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001088 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001089 }
1090
Chris Wilson05394f32010-11-08 19:18:58 +00001091 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001092unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001093 mutex_unlock(&dev->struct_mutex);
1094 return ret;
1095}
1096
1097/**
1098 * Called when user space has done writes to this buffer
1099 */
1100int
1101i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001102 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001103{
1104 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001105 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001106 int ret = 0;
1107
1108 if (!(dev->driver->driver_features & DRIVER_GEM))
1109 return -ENODEV;
1110
Chris Wilson76c1dec2010-09-25 11:22:51 +01001111 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001112 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001113 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001114
Chris Wilson05394f32010-11-08 19:18:58 +00001115 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001116 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001117 ret = -ENOENT;
1118 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001119 }
1120
Eric Anholt673a3942008-07-30 12:06:12 -07001121 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001122 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001123 i915_gem_object_flush_cpu_write_domain(obj);
1124
Chris Wilson05394f32010-11-08 19:18:58 +00001125 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001126unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001127 mutex_unlock(&dev->struct_mutex);
1128 return ret;
1129}
1130
1131/**
1132 * Maps the contents of an object, returning the address it is mapped
1133 * into.
1134 *
1135 * While the mapping holds a reference on the contents of the object, it doesn't
1136 * imply a ref on the object itself.
1137 */
1138int
1139i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001140 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001141{
Chris Wilsonda761a62010-10-27 17:37:08 +01001142 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001143 struct drm_i915_gem_mmap *args = data;
1144 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001145 unsigned long addr;
1146
1147 if (!(dev->driver->driver_features & DRIVER_GEM))
1148 return -ENODEV;
1149
Chris Wilson05394f32010-11-08 19:18:58 +00001150 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001151 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001152 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001153
Chris Wilsonda761a62010-10-27 17:37:08 +01001154 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1155 drm_gem_object_unreference_unlocked(obj);
1156 return -E2BIG;
1157 }
1158
Eric Anholt673a3942008-07-30 12:06:12 -07001159 down_write(&current->mm->mmap_sem);
1160 addr = do_mmap(obj->filp, 0, args->size,
1161 PROT_READ | PROT_WRITE, MAP_SHARED,
1162 args->offset);
1163 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001164 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001165 if (IS_ERR((void *)addr))
1166 return addr;
1167
1168 args->addr_ptr = (uint64_t) addr;
1169
1170 return 0;
1171}
1172
Jesse Barnesde151cf2008-11-12 10:03:55 -08001173/**
1174 * i915_gem_fault - fault a page into the GTT
1175 * vma: VMA in question
1176 * vmf: fault info
1177 *
1178 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1179 * from userspace. The fault handler takes care of binding the object to
1180 * the GTT (if needed), allocating and programming a fence register (again,
1181 * only if needed based on whether the old reg is still valid or the object
1182 * is tiled) and inserting a new PTE into the faulting process.
1183 *
1184 * Note that the faulting process may involve evicting existing objects
1185 * from the GTT and/or fence registers to make room. So performance may
1186 * suffer if the GTT working set is large or there are few fence registers
1187 * left.
1188 */
1189int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1190{
Chris Wilson05394f32010-11-08 19:18:58 +00001191 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1192 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001193 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001194 pgoff_t page_offset;
1195 unsigned long pfn;
1196 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001197 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001198
1199 /* We don't use vmf->pgoff since that has the fake offset */
1200 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1201 PAGE_SHIFT;
1202
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001203 ret = i915_mutex_lock_interruptible(dev);
1204 if (ret)
1205 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001206
Chris Wilsondb53a302011-02-03 11:57:46 +00001207 trace_i915_gem_object_fault(obj, page_offset, true, write);
1208
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001209 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001210 if (!obj->map_and_fenceable) {
1211 ret = i915_gem_object_unbind(obj);
1212 if (ret)
1213 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001214 }
Chris Wilson05394f32010-11-08 19:18:58 +00001215 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001216 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001217 if (ret)
1218 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001219
Eric Anholte92d03b2011-06-14 16:43:09 -07001220 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1221 if (ret)
1222 goto unlock;
1223 }
Chris Wilson4a684a42010-10-28 14:44:08 +01001224
Chris Wilsond9e86c02010-11-10 16:40:20 +00001225 if (obj->tiling_mode == I915_TILING_NONE)
1226 ret = i915_gem_object_put_fence(obj);
1227 else
Chris Wilsonce453d82011-02-21 14:43:56 +00001228 ret = i915_gem_object_get_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001229 if (ret)
1230 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001231
Chris Wilson05394f32010-11-08 19:18:58 +00001232 if (i915_gem_object_is_inactive(obj))
1233 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001234
Chris Wilson6299f992010-11-24 12:23:44 +00001235 obj->fault_mappable = true;
1236
Chris Wilson05394f32010-11-08 19:18:58 +00001237 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001238 page_offset;
1239
1240 /* Finally, remap it using the new GTT offset */
1241 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001242unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001243 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001244out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001245 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001246 case -EIO:
Chris Wilson045e7692010-11-07 09:18:22 +00001247 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001248 /* Give the error handler a chance to run and move the
1249 * objects off the GPU active list. Next time we service the
1250 * fault, we should be able to transition the page into the
1251 * GTT without touching the GPU (and so avoid further
1252 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1253 * with coherency, just lost writes.
1254 */
Chris Wilson045e7692010-11-07 09:18:22 +00001255 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001256 case 0:
1257 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001258 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001259 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001260 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001261 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001262 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001263 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001264 }
1265}
1266
1267/**
1268 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1269 * @obj: obj in question
1270 *
1271 * GEM memory mapping works by handing back to userspace a fake mmap offset
1272 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1273 * up the object based on the offset and sets up the various memory mapping
1274 * structures.
1275 *
1276 * This routine allocates and attaches a fake offset for @obj.
1277 */
1278static int
Chris Wilson05394f32010-11-08 19:18:58 +00001279i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001280{
Chris Wilson05394f32010-11-08 19:18:58 +00001281 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001282 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001283 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001284 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001285 int ret = 0;
1286
1287 /* Set the object up for mmap'ing */
Chris Wilson05394f32010-11-08 19:18:58 +00001288 list = &obj->base.map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001289 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001290 if (!list->map)
1291 return -ENOMEM;
1292
1293 map = list->map;
1294 map->type = _DRM_GEM;
Chris Wilson05394f32010-11-08 19:18:58 +00001295 map->size = obj->base.size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001296 map->handle = obj;
1297
1298 /* Get a DRM GEM mmap offset allocated... */
1299 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
Chris Wilson05394f32010-11-08 19:18:58 +00001300 obj->base.size / PAGE_SIZE,
1301 0, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001302 if (!list->file_offset_node) {
Chris Wilson05394f32010-11-08 19:18:58 +00001303 DRM_ERROR("failed to allocate offset for bo %d\n",
1304 obj->base.name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001305 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001306 goto out_free_list;
1307 }
1308
1309 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
Chris Wilson05394f32010-11-08 19:18:58 +00001310 obj->base.size / PAGE_SIZE,
1311 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001312 if (!list->file_offset_node) {
1313 ret = -ENOMEM;
1314 goto out_free_list;
1315 }
1316
1317 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001318 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1319 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001320 DRM_ERROR("failed to add to map hash\n");
1321 goto out_free_mm;
1322 }
1323
Jesse Barnesde151cf2008-11-12 10:03:55 -08001324 return 0;
1325
1326out_free_mm:
1327 drm_mm_put_block(list->file_offset_node);
1328out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001329 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001330 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001331
1332 return ret;
1333}
1334
Chris Wilson901782b2009-07-10 08:18:50 +01001335/**
1336 * i915_gem_release_mmap - remove physical page mappings
1337 * @obj: obj in question
1338 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001339 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001340 * relinquish ownership of the pages back to the system.
1341 *
1342 * It is vital that we remove the page mapping if we have mapped a tiled
1343 * object through the GTT and then lose the fence register due to
1344 * resource pressure. Similarly if the object has been moved out of the
1345 * aperture, than pages mapped into userspace must be revoked. Removing the
1346 * mapping will then trigger a page fault on the next user access, allowing
1347 * fixup by i915_gem_fault().
1348 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001349void
Chris Wilson05394f32010-11-08 19:18:58 +00001350i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001351{
Chris Wilson6299f992010-11-24 12:23:44 +00001352 if (!obj->fault_mappable)
1353 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001354
Chris Wilsonf6e47882011-03-20 21:09:12 +00001355 if (obj->base.dev->dev_mapping)
1356 unmap_mapping_range(obj->base.dev->dev_mapping,
1357 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1358 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001359
Chris Wilson6299f992010-11-24 12:23:44 +00001360 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001361}
1362
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001363static void
Chris Wilson05394f32010-11-08 19:18:58 +00001364i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001365{
Chris Wilson05394f32010-11-08 19:18:58 +00001366 struct drm_device *dev = obj->base.dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001367 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001368 struct drm_map_list *list = &obj->base.map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001369
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001370 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001371 drm_mm_put_block(list->file_offset_node);
1372 kfree(list->map);
1373 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001374}
1375
Chris Wilson92b88ae2010-11-09 11:47:32 +00001376static uint32_t
1377i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1378{
1379 struct drm_device *dev = obj->base.dev;
1380 uint32_t size;
1381
1382 if (INTEL_INFO(dev)->gen >= 4 ||
1383 obj->tiling_mode == I915_TILING_NONE)
1384 return obj->base.size;
1385
1386 /* Previous chips need a power-of-two fence region when tiling */
1387 if (INTEL_INFO(dev)->gen == 3)
1388 size = 1024*1024;
1389 else
1390 size = 512*1024;
1391
1392 while (size < obj->base.size)
1393 size <<= 1;
1394
1395 return size;
1396}
1397
Jesse Barnesde151cf2008-11-12 10:03:55 -08001398/**
1399 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1400 * @obj: object to check
1401 *
1402 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001403 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001404 */
1405static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001406i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001407{
Chris Wilson05394f32010-11-08 19:18:58 +00001408 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001409
1410 /*
1411 * Minimum alignment is 4k (GTT page size), but might be greater
1412 * if a fence register is needed for the object.
1413 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001414 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilson05394f32010-11-08 19:18:58 +00001415 obj->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001416 return 4096;
1417
1418 /*
1419 * Previous chips need to be aligned to the size of the smallest
1420 * fence register that can contain the object.
1421 */
Chris Wilson05394f32010-11-08 19:18:58 +00001422 return i915_gem_get_gtt_size(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001423}
1424
Daniel Vetter5e783302010-11-14 22:32:36 +01001425/**
1426 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1427 * unfenced object
1428 * @obj: object to check
1429 *
1430 * Return the required GTT alignment for an object, only taking into account
1431 * unfenced tiled surface requirements.
1432 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001433uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001434i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
Daniel Vetter5e783302010-11-14 22:32:36 +01001435{
Chris Wilson05394f32010-11-08 19:18:58 +00001436 struct drm_device *dev = obj->base.dev;
Daniel Vetter5e783302010-11-14 22:32:36 +01001437 int tile_height;
1438
1439 /*
1440 * Minimum alignment is 4k (GTT page size) for sane hw.
1441 */
1442 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001443 obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001444 return 4096;
1445
1446 /*
1447 * Older chips need unfenced tiled buffers to be aligned to the left
1448 * edge of an even tile row (where tile rows are counted as if the bo is
1449 * placed in a fenced gtt region).
1450 */
Daniel Vetterc8ebc2b2011-05-12 22:17:20 +01001451 if (IS_GEN2(dev))
1452 tile_height = 16;
1453 else if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Daniel Vetter5e783302010-11-14 22:32:36 +01001454 tile_height = 32;
1455 else
1456 tile_height = 8;
1457
Chris Wilson05394f32010-11-08 19:18:58 +00001458 return tile_height * obj->stride * 2;
Daniel Vetter5e783302010-11-14 22:32:36 +01001459}
1460
Jesse Barnesde151cf2008-11-12 10:03:55 -08001461int
Dave Airlieff72145b2011-02-07 12:16:14 +10001462i915_gem_mmap_gtt(struct drm_file *file,
1463 struct drm_device *dev,
1464 uint32_t handle,
1465 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001466{
Chris Wilsonda761a62010-10-27 17:37:08 +01001467 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001468 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001469 int ret;
1470
1471 if (!(dev->driver->driver_features & DRIVER_GEM))
1472 return -ENODEV;
1473
Chris Wilson76c1dec2010-09-25 11:22:51 +01001474 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001475 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001476 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001477
Dave Airlieff72145b2011-02-07 12:16:14 +10001478 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001479 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001480 ret = -ENOENT;
1481 goto unlock;
1482 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001483
Chris Wilson05394f32010-11-08 19:18:58 +00001484 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001485 ret = -E2BIG;
1486 goto unlock;
1487 }
1488
Chris Wilson05394f32010-11-08 19:18:58 +00001489 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001490 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001491 ret = -EINVAL;
1492 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001493 }
1494
Chris Wilson05394f32010-11-08 19:18:58 +00001495 if (!obj->base.map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001496 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001497 if (ret)
1498 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001499 }
1500
Dave Airlieff72145b2011-02-07 12:16:14 +10001501 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001502
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001503out:
Chris Wilson05394f32010-11-08 19:18:58 +00001504 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001505unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001506 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001507 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001508}
1509
Dave Airlieff72145b2011-02-07 12:16:14 +10001510/**
1511 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1512 * @dev: DRM device
1513 * @data: GTT mapping ioctl data
1514 * @file: GEM object info
1515 *
1516 * Simply returns the fake offset to userspace so it can mmap it.
1517 * The mmap call will end up in drm_gem_mmap(), which will set things
1518 * up so we can get faults in the handler above.
1519 *
1520 * The fault handler will take care of binding the object into the GTT
1521 * (since it may have been evicted to make room for something), allocating
1522 * a fence register, and mapping the appropriate aperture address into
1523 * userspace.
1524 */
1525int
1526i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1527 struct drm_file *file)
1528{
1529 struct drm_i915_gem_mmap_gtt *args = data;
1530
1531 if (!(dev->driver->driver_features & DRIVER_GEM))
1532 return -ENODEV;
1533
1534 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1535}
1536
1537
Chris Wilsone5281cc2010-10-28 13:45:36 +01001538static int
Chris Wilson05394f32010-11-08 19:18:58 +00001539i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001540 gfp_t gfpmask)
1541{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001542 int page_count, i;
1543 struct address_space *mapping;
1544 struct inode *inode;
1545 struct page *page;
1546
1547 /* Get the list of pages out of our struct file. They'll be pinned
1548 * at this point until we release them.
1549 */
Chris Wilson05394f32010-11-08 19:18:58 +00001550 page_count = obj->base.size / PAGE_SIZE;
1551 BUG_ON(obj->pages != NULL);
1552 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1553 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001554 return -ENOMEM;
1555
Chris Wilson05394f32010-11-08 19:18:58 +00001556 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001557 mapping = inode->i_mapping;
Hugh Dickins5949eac2011-06-27 16:18:18 -07001558 gfpmask |= mapping_gfp_mask(mapping);
1559
Chris Wilsone5281cc2010-10-28 13:45:36 +01001560 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07001561 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001562 if (IS_ERR(page))
1563 goto err_pages;
1564
Chris Wilson05394f32010-11-08 19:18:58 +00001565 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001566 }
1567
Chris Wilson05394f32010-11-08 19:18:58 +00001568 if (obj->tiling_mode != I915_TILING_NONE)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001569 i915_gem_object_do_bit_17_swizzle(obj);
1570
1571 return 0;
1572
1573err_pages:
1574 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001575 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001576
Chris Wilson05394f32010-11-08 19:18:58 +00001577 drm_free_large(obj->pages);
1578 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001579 return PTR_ERR(page);
1580}
1581
Chris Wilson5cdf5882010-09-27 15:51:07 +01001582static void
Chris Wilson05394f32010-11-08 19:18:58 +00001583i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001584{
Chris Wilson05394f32010-11-08 19:18:58 +00001585 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001586 int i;
1587
Chris Wilson05394f32010-11-08 19:18:58 +00001588 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001589
Chris Wilson05394f32010-11-08 19:18:58 +00001590 if (obj->tiling_mode != I915_TILING_NONE)
Eric Anholt280b7132009-03-12 16:56:27 -07001591 i915_gem_object_save_bit_17_swizzle(obj);
1592
Chris Wilson05394f32010-11-08 19:18:58 +00001593 if (obj->madv == I915_MADV_DONTNEED)
1594 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001595
1596 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001597 if (obj->dirty)
1598 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001599
Chris Wilson05394f32010-11-08 19:18:58 +00001600 if (obj->madv == I915_MADV_WILLNEED)
1601 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001602
Chris Wilson05394f32010-11-08 19:18:58 +00001603 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001604 }
Chris Wilson05394f32010-11-08 19:18:58 +00001605 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001606
Chris Wilson05394f32010-11-08 19:18:58 +00001607 drm_free_large(obj->pages);
1608 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001609}
1610
Chris Wilson54cf91d2010-11-25 18:00:26 +00001611void
Chris Wilson05394f32010-11-08 19:18:58 +00001612i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001613 struct intel_ring_buffer *ring,
1614 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001615{
Chris Wilson05394f32010-11-08 19:18:58 +00001616 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001617 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001618
Zou Nan hai852835f2010-05-21 09:08:56 +08001619 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001620 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001621
1622 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001623 if (!obj->active) {
1624 drm_gem_object_reference(&obj->base);
1625 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001626 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001627
Eric Anholt673a3942008-07-30 12:06:12 -07001628 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001629 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1630 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001631
Chris Wilson05394f32010-11-08 19:18:58 +00001632 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001633 if (obj->fenced_gpu_access) {
1634 struct drm_i915_fence_reg *reg;
1635
1636 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1637
1638 obj->last_fenced_seqno = seqno;
1639 obj->last_fenced_ring = ring;
1640
1641 reg = &dev_priv->fence_regs[obj->fence_reg];
1642 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1643 }
1644}
1645
1646static void
1647i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1648{
1649 list_del_init(&obj->ring_list);
1650 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001651}
1652
Eric Anholtce44b0e2008-11-06 16:00:31 -08001653static void
Chris Wilson05394f32010-11-08 19:18:58 +00001654i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001655{
Chris Wilson05394f32010-11-08 19:18:58 +00001656 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001657 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001658
Chris Wilson05394f32010-11-08 19:18:58 +00001659 BUG_ON(!obj->active);
1660 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001661
1662 i915_gem_object_move_off_active(obj);
1663}
1664
1665static void
1666i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1667{
1668 struct drm_device *dev = obj->base.dev;
1669 struct drm_i915_private *dev_priv = dev->dev_private;
1670
1671 if (obj->pin_count != 0)
1672 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1673 else
1674 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1675
1676 BUG_ON(!list_empty(&obj->gpu_write_list));
1677 BUG_ON(!obj->active);
1678 obj->ring = NULL;
1679
1680 i915_gem_object_move_off_active(obj);
1681 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001682
1683 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001684 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001685 drm_gem_object_unreference(&obj->base);
1686
1687 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001688}
Eric Anholt673a3942008-07-30 12:06:12 -07001689
Chris Wilson963b4832009-09-20 23:03:54 +01001690/* Immediately discard the backing storage */
1691static void
Chris Wilson05394f32010-11-08 19:18:58 +00001692i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001693{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001694 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001695
Chris Wilsonae9fed62010-08-07 11:01:30 +01001696 /* Our goal here is to return as much of the memory as
1697 * is possible back to the system as we are called from OOM.
1698 * To do this we must instruct the shmfs to drop all of its
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001699 * backing pages, *now*.
Chris Wilsonae9fed62010-08-07 11:01:30 +01001700 */
Chris Wilson05394f32010-11-08 19:18:58 +00001701 inode = obj->base.filp->f_path.dentry->d_inode;
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001702 shmem_truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001703
Chris Wilson05394f32010-11-08 19:18:58 +00001704 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001705}
1706
1707static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001708i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001709{
Chris Wilson05394f32010-11-08 19:18:58 +00001710 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001711}
1712
Eric Anholt673a3942008-07-30 12:06:12 -07001713static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001714i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1715 uint32_t flush_domains)
Daniel Vetter63560392010-02-19 11:51:59 +01001716{
Chris Wilson05394f32010-11-08 19:18:58 +00001717 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001718
Chris Wilson05394f32010-11-08 19:18:58 +00001719 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001720 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001721 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001722 if (obj->base.write_domain & flush_domains) {
1723 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001724
Chris Wilson05394f32010-11-08 19:18:58 +00001725 obj->base.write_domain = 0;
1726 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001727 i915_gem_object_move_to_active(obj, ring,
Chris Wilsondb53a302011-02-03 11:57:46 +00001728 i915_gem_next_request_seqno(ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001729
Daniel Vetter63560392010-02-19 11:51:59 +01001730 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001731 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001732 old_write_domain);
1733 }
1734 }
1735}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001736
Chris Wilson3cce4692010-10-27 16:11:02 +01001737int
Chris Wilsondb53a302011-02-03 11:57:46 +00001738i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001739 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001740 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001741{
Chris Wilsondb53a302011-02-03 11:57:46 +00001742 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001743 uint32_t seqno;
1744 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001745 int ret;
1746
1747 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001748
Chris Wilson3cce4692010-10-27 16:11:02 +01001749 ret = ring->add_request(ring, &seqno);
1750 if (ret)
1751 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001752
Chris Wilsondb53a302011-02-03 11:57:46 +00001753 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001754
1755 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001756 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001757 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001758 was_empty = list_empty(&ring->request_list);
1759 list_add_tail(&request->list, &ring->request_list);
1760
Chris Wilsondb53a302011-02-03 11:57:46 +00001761 if (file) {
1762 struct drm_i915_file_private *file_priv = file->driver_priv;
1763
Chris Wilson1c255952010-09-26 11:03:27 +01001764 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001765 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001766 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001767 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001768 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001769 }
Eric Anholt673a3942008-07-30 12:06:12 -07001770
Chris Wilsondb53a302011-02-03 11:57:46 +00001771 ring->outstanding_lazy_request = false;
1772
Ben Gamarif65d9422009-09-14 17:48:44 -04001773 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001774 if (i915_enable_hangcheck) {
1775 mod_timer(&dev_priv->hangcheck_timer,
1776 jiffies +
1777 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1778 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001779 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001780 queue_delayed_work(dev_priv->wq,
1781 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001782 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001783 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001784}
1785
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001786static inline void
1787i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001788{
Chris Wilson1c255952010-09-26 11:03:27 +01001789 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001790
Chris Wilson1c255952010-09-26 11:03:27 +01001791 if (!file_priv)
1792 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001793
Chris Wilson1c255952010-09-26 11:03:27 +01001794 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001795 if (request->file_priv) {
1796 list_del(&request->client_list);
1797 request->file_priv = NULL;
1798 }
Chris Wilson1c255952010-09-26 11:03:27 +01001799 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001800}
1801
Chris Wilsondfaae392010-09-22 10:31:52 +01001802static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1803 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001804{
Chris Wilsondfaae392010-09-22 10:31:52 +01001805 while (!list_empty(&ring->request_list)) {
1806 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001807
Chris Wilsondfaae392010-09-22 10:31:52 +01001808 request = list_first_entry(&ring->request_list,
1809 struct drm_i915_gem_request,
1810 list);
1811
1812 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001813 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001814 kfree(request);
1815 }
1816
1817 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001818 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001819
Chris Wilson05394f32010-11-08 19:18:58 +00001820 obj = list_first_entry(&ring->active_list,
1821 struct drm_i915_gem_object,
1822 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001823
Chris Wilson05394f32010-11-08 19:18:58 +00001824 obj->base.write_domain = 0;
1825 list_del_init(&obj->gpu_write_list);
1826 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001827 }
Eric Anholt673a3942008-07-30 12:06:12 -07001828}
1829
Chris Wilson312817a2010-11-22 11:50:11 +00001830static void i915_gem_reset_fences(struct drm_device *dev)
1831{
1832 struct drm_i915_private *dev_priv = dev->dev_private;
1833 int i;
1834
1835 for (i = 0; i < 16; i++) {
1836 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001837 struct drm_i915_gem_object *obj = reg->obj;
1838
1839 if (!obj)
1840 continue;
1841
1842 if (obj->tiling_mode)
1843 i915_gem_release_mmap(obj);
1844
Chris Wilsond9e86c02010-11-10 16:40:20 +00001845 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1846 reg->obj->fenced_gpu_access = false;
1847 reg->obj->last_fenced_seqno = 0;
1848 reg->obj->last_fenced_ring = NULL;
1849 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001850 }
1851}
1852
Chris Wilson069efc12010-09-30 16:53:18 +01001853void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001854{
Chris Wilsondfaae392010-09-22 10:31:52 +01001855 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001856 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001857 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001858
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001859 for (i = 0; i < I915_NUM_RINGS; i++)
1860 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001861
1862 /* Remove anything from the flushing lists. The GPU cache is likely
1863 * to be lost on reset along with the data, so simply move the
1864 * lost bo to the inactive list.
1865 */
1866 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001867 obj= list_first_entry(&dev_priv->mm.flushing_list,
1868 struct drm_i915_gem_object,
1869 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001870
Chris Wilson05394f32010-11-08 19:18:58 +00001871 obj->base.write_domain = 0;
1872 list_del_init(&obj->gpu_write_list);
1873 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001874 }
Chris Wilson9375e442010-09-19 12:21:28 +01001875
Chris Wilsondfaae392010-09-22 10:31:52 +01001876 /* Move everything out of the GPU domains to ensure we do any
1877 * necessary invalidation upon reuse.
1878 */
Chris Wilson05394f32010-11-08 19:18:58 +00001879 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001880 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001881 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001882 {
Chris Wilson05394f32010-11-08 19:18:58 +00001883 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001884 }
Chris Wilson069efc12010-09-30 16:53:18 +01001885
1886 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001887 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001888}
1889
1890/**
1891 * This function clears the request list as sequence numbers are passed.
1892 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001893static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001894i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001895{
Eric Anholt673a3942008-07-30 12:06:12 -07001896 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001897 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001898
Chris Wilsondb53a302011-02-03 11:57:46 +00001899 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001900 return;
1901
Chris Wilsondb53a302011-02-03 11:57:46 +00001902 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001903
Chris Wilson78501ea2010-10-27 12:18:21 +01001904 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001905
Chris Wilson076e2c02011-01-21 10:07:18 +00001906 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001907 if (seqno >= ring->sync_seqno[i])
1908 ring->sync_seqno[i] = 0;
1909
Zou Nan hai852835f2010-05-21 09:08:56 +08001910 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001911 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001912
Zou Nan hai852835f2010-05-21 09:08:56 +08001913 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001914 struct drm_i915_gem_request,
1915 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001916
Chris Wilsondfaae392010-09-22 10:31:52 +01001917 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001918 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001919
Chris Wilsondb53a302011-02-03 11:57:46 +00001920 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001921
1922 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001923 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001924 kfree(request);
1925 }
1926
1927 /* Move any buffers on the active list that are no longer referenced
1928 * by the ringbuffer to the flushing/inactive lists as appropriate.
1929 */
1930 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001931 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001932
Chris Wilson05394f32010-11-08 19:18:58 +00001933 obj= list_first_entry(&ring->active_list,
1934 struct drm_i915_gem_object,
1935 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001936
Chris Wilson05394f32010-11-08 19:18:58 +00001937 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001938 break;
1939
Chris Wilson05394f32010-11-08 19:18:58 +00001940 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001941 i915_gem_object_move_to_flushing(obj);
1942 else
1943 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001944 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001945
Chris Wilsondb53a302011-02-03 11:57:46 +00001946 if (unlikely(ring->trace_irq_seqno &&
1947 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001948 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001949 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001950 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001951
Chris Wilsondb53a302011-02-03 11:57:46 +00001952 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001953}
1954
1955void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001956i915_gem_retire_requests(struct drm_device *dev)
1957{
1958 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001959 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001960
Chris Wilsonbe726152010-07-23 23:18:50 +01001961 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001962 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001963
1964 /* We must be careful that during unbind() we do not
1965 * accidentally infinitely recurse into retire requests.
1966 * Currently:
1967 * retire -> free -> unbind -> wait -> retire_ring
1968 */
Chris Wilson05394f32010-11-08 19:18:58 +00001969 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001970 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001971 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001972 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001973 }
1974
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001975 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilsondb53a302011-02-03 11:57:46 +00001976 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001977}
1978
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001979static void
Eric Anholt673a3942008-07-30 12:06:12 -07001980i915_gem_retire_work_handler(struct work_struct *work)
1981{
1982 drm_i915_private_t *dev_priv;
1983 struct drm_device *dev;
Chris Wilson0a587052011-01-09 21:05:44 +00001984 bool idle;
1985 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001986
1987 dev_priv = container_of(work, drm_i915_private_t,
1988 mm.retire_work.work);
1989 dev = dev_priv->dev;
1990
Chris Wilson891b48c2010-09-29 12:26:37 +01001991 /* Come back later if the device is busy... */
1992 if (!mutex_trylock(&dev->struct_mutex)) {
1993 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1994 return;
1995 }
1996
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001997 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001998
Chris Wilson0a587052011-01-09 21:05:44 +00001999 /* Send a periodic flush down the ring so we don't hold onto GEM
2000 * objects indefinitely.
2001 */
2002 idle = true;
2003 for (i = 0; i < I915_NUM_RINGS; i++) {
2004 struct intel_ring_buffer *ring = &dev_priv->ring[i];
2005
2006 if (!list_empty(&ring->gpu_write_list)) {
2007 struct drm_i915_gem_request *request;
2008 int ret;
2009
Chris Wilsondb53a302011-02-03 11:57:46 +00002010 ret = i915_gem_flush_ring(ring,
2011 0, I915_GEM_GPU_DOMAINS);
Chris Wilson0a587052011-01-09 21:05:44 +00002012 request = kzalloc(sizeof(*request), GFP_KERNEL);
2013 if (ret || request == NULL ||
Chris Wilsondb53a302011-02-03 11:57:46 +00002014 i915_add_request(ring, NULL, request))
Chris Wilson0a587052011-01-09 21:05:44 +00002015 kfree(request);
2016 }
2017
2018 idle &= list_empty(&ring->request_list);
2019 }
2020
2021 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002022 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilson0a587052011-01-09 21:05:44 +00002023
Eric Anholt673a3942008-07-30 12:06:12 -07002024 mutex_unlock(&dev->struct_mutex);
2025}
2026
Chris Wilsondb53a302011-02-03 11:57:46 +00002027/**
2028 * Waits for a sequence number to be signaled, and cleans up the
2029 * request and object lists appropriately for that event.
2030 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002031int
Chris Wilsondb53a302011-02-03 11:57:46 +00002032i915_wait_request(struct intel_ring_buffer *ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002033 uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07002034{
Chris Wilsondb53a302011-02-03 11:57:46 +00002035 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002036 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002037 int ret = 0;
2038
2039 BUG_ON(seqno == 0);
2040
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002041 if (atomic_read(&dev_priv->mm.wedged)) {
2042 struct completion *x = &dev_priv->error_completion;
2043 bool recovery_complete;
2044 unsigned long flags;
2045
2046 /* Give the error handler a chance to run. */
2047 spin_lock_irqsave(&x->wait.lock, flags);
2048 recovery_complete = x->done > 0;
2049 spin_unlock_irqrestore(&x->wait.lock, flags);
2050
2051 return recovery_complete ? -EIO : -EAGAIN;
2052 }
Ben Gamariffed1d02009-09-14 17:48:41 -04002053
Chris Wilson5d97eb62010-11-10 20:40:02 +00002054 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002055 struct drm_i915_gem_request *request;
2056
2057 request = kzalloc(sizeof(*request), GFP_KERNEL);
2058 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002059 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002060
Chris Wilsondb53a302011-02-03 11:57:46 +00002061 ret = i915_add_request(ring, NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +01002062 if (ret) {
2063 kfree(request);
2064 return ret;
2065 }
2066
2067 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002068 }
2069
Chris Wilson78501ea2010-10-27 12:18:21 +01002070 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002071 if (HAS_PCH_SPLIT(ring->dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002072 ier = I915_READ(DEIER) | I915_READ(GTIER);
2073 else
2074 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002075 if (!ier) {
2076 DRM_ERROR("something (likely vbetool) disabled "
2077 "interrupts, re-enabling\n");
Chris Wilsonf01c22f2011-06-28 11:48:51 +01002078 ring->dev->driver->irq_preinstall(ring->dev);
2079 ring->dev->driver->irq_postinstall(ring->dev);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002080 }
2081
Chris Wilsondb53a302011-02-03 11:57:46 +00002082 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002083
Chris Wilsonb2223492010-10-27 15:27:33 +01002084 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002085 if (ring->irq_get(ring)) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002086 if (dev_priv->mm.interruptible)
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002087 ret = wait_event_interruptible(ring->irq_queue,
2088 i915_seqno_passed(ring->get_seqno(ring), seqno)
2089 || atomic_read(&dev_priv->mm.wedged));
2090 else
2091 wait_event(ring->irq_queue,
2092 i915_seqno_passed(ring->get_seqno(ring), seqno)
2093 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002094
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002095 ring->irq_put(ring);
Chris Wilsonb5ba1772010-12-14 12:17:15 +00002096 } else if (wait_for(i915_seqno_passed(ring->get_seqno(ring),
2097 seqno) ||
2098 atomic_read(&dev_priv->mm.wedged), 3000))
2099 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01002100 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002101
Chris Wilsondb53a302011-02-03 11:57:46 +00002102 trace_i915_gem_request_wait_end(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002103 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002104 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002105 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002106
2107 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002108 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002109 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002110 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002111
2112 /* Directly dispatch request retiring. While we have the work queue
2113 * to handle this, the waiter on a request often wants an associated
2114 * buffer to have made it to the inactive list, and we would need
2115 * a separate wait queue to handle that.
2116 */
2117 if (ret == 0)
Chris Wilsondb53a302011-02-03 11:57:46 +00002118 i915_gem_retire_requests_ring(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002119
2120 return ret;
2121}
2122
Daniel Vetter48764bf2009-09-15 22:57:32 +02002123/**
Eric Anholt673a3942008-07-30 12:06:12 -07002124 * Ensures that all rendering to the object has completed and the object is
2125 * safe to unbind from the GTT or access from the CPU.
2126 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00002127int
Chris Wilsonce453d82011-02-21 14:43:56 +00002128i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002129{
Eric Anholt673a3942008-07-30 12:06:12 -07002130 int ret;
2131
Eric Anholte47c68e2008-11-14 13:35:19 -08002132 /* This function only exists to support waiting for existing rendering,
2133 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002134 */
Chris Wilson05394f32010-11-08 19:18:58 +00002135 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002136
2137 /* If there is rendering queued on the buffer being evicted, wait for
2138 * it.
2139 */
Chris Wilson05394f32010-11-08 19:18:58 +00002140 if (obj->active) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002141 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002142 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002143 return ret;
2144 }
2145
2146 return 0;
2147}
2148
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002149static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2150{
2151 u32 old_write_domain, old_read_domains;
2152
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002153 /* Act a barrier for all accesses through the GTT */
2154 mb();
2155
2156 /* Force a pagefault for domain tracking on next user access */
2157 i915_gem_release_mmap(obj);
2158
Keith Packardb97c3d92011-06-24 21:02:59 -07002159 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2160 return;
2161
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002162 old_read_domains = obj->base.read_domains;
2163 old_write_domain = obj->base.write_domain;
2164
2165 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2166 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2167
2168 trace_i915_gem_object_change_domain(obj,
2169 old_read_domains,
2170 old_write_domain);
2171}
2172
Eric Anholt673a3942008-07-30 12:06:12 -07002173/**
2174 * Unbinds an object from the GTT aperture.
2175 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002176int
Chris Wilson05394f32010-11-08 19:18:58 +00002177i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002178{
Eric Anholt673a3942008-07-30 12:06:12 -07002179 int ret = 0;
2180
Chris Wilson05394f32010-11-08 19:18:58 +00002181 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002182 return 0;
2183
Chris Wilson05394f32010-11-08 19:18:58 +00002184 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002185 DRM_ERROR("Attempting to unbind pinned buffer\n");
2186 return -EINVAL;
2187 }
2188
Chris Wilsona8198ee2011-04-13 22:04:09 +01002189 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson8dc17752010-07-23 23:18:51 +01002190 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002191 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002192 /* Continue on if we fail due to EIO, the GPU is hung so we
2193 * should be safe and we need to cleanup or else we might
2194 * cause memory corruption through use-after-free.
2195 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002196
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002197 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002198
2199 /* Move the object to the CPU domain to ensure that
2200 * any possible CPU writes while it's not in the GTT
2201 * are flushed when we go to remap it.
2202 */
2203 if (ret == 0)
2204 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2205 if (ret == -ERESTARTSYS)
2206 return ret;
Chris Wilson812ed4922010-09-30 15:08:57 +01002207 if (ret) {
Chris Wilsona8198ee2011-04-13 22:04:09 +01002208 /* In the event of a disaster, abandon all caches and
2209 * hope for the best.
2210 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002211 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002212 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002213 }
Eric Anholt673a3942008-07-30 12:06:12 -07002214
Daniel Vetter96b47b62009-12-15 17:50:00 +01002215 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002216 ret = i915_gem_object_put_fence(obj);
2217 if (ret == -ERESTARTSYS)
2218 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002219
Chris Wilsondb53a302011-02-03 11:57:46 +00002220 trace_i915_gem_object_unbind(obj);
2221
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002222 i915_gem_gtt_unbind_object(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002223 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002224
Chris Wilson6299f992010-11-24 12:23:44 +00002225 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002226 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002227 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002228 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002229
Chris Wilson05394f32010-11-08 19:18:58 +00002230 drm_mm_put_block(obj->gtt_space);
2231 obj->gtt_space = NULL;
2232 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002233
Chris Wilson05394f32010-11-08 19:18:58 +00002234 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002235 i915_gem_object_truncate(obj);
2236
Chris Wilson8dc17752010-07-23 23:18:51 +01002237 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002238}
2239
Chris Wilson88241782011-01-07 17:09:48 +00002240int
Chris Wilsondb53a302011-02-03 11:57:46 +00002241i915_gem_flush_ring(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002242 uint32_t invalidate_domains,
2243 uint32_t flush_domains)
2244{
Chris Wilson88241782011-01-07 17:09:48 +00002245 int ret;
2246
Chris Wilson36d527d2011-03-19 22:26:49 +00002247 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2248 return 0;
2249
Chris Wilsondb53a302011-02-03 11:57:46 +00002250 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2251
Chris Wilson88241782011-01-07 17:09:48 +00002252 ret = ring->flush(ring, invalidate_domains, flush_domains);
2253 if (ret)
2254 return ret;
2255
Chris Wilson36d527d2011-03-19 22:26:49 +00002256 if (flush_domains & I915_GEM_GPU_DOMAINS)
2257 i915_gem_process_flushing_list(ring, flush_domains);
2258
Chris Wilson88241782011-01-07 17:09:48 +00002259 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002260}
2261
Chris Wilsondb53a302011-02-03 11:57:46 +00002262static int i915_ring_idle(struct intel_ring_buffer *ring)
Chris Wilsona56ba562010-09-28 10:07:56 +01002263{
Chris Wilson88241782011-01-07 17:09:48 +00002264 int ret;
2265
Chris Wilson395b70b2010-10-28 21:28:46 +01002266 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002267 return 0;
2268
Chris Wilson88241782011-01-07 17:09:48 +00002269 if (!list_empty(&ring->gpu_write_list)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002270 ret = i915_gem_flush_ring(ring,
Chris Wilson0ac74c62010-12-06 14:36:02 +00002271 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilson88241782011-01-07 17:09:48 +00002272 if (ret)
2273 return ret;
2274 }
2275
Chris Wilsonce453d82011-02-21 14:43:56 +00002276 return i915_wait_request(ring, i915_gem_next_request_seqno(ring));
Chris Wilsona56ba562010-09-28 10:07:56 +01002277}
2278
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002279int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002280i915_gpu_idle(struct drm_device *dev)
2281{
2282 drm_i915_private_t *dev_priv = dev->dev_private;
2283 bool lists_empty;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002284 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002285
Zou Nan haid1b851f2010-05-21 09:08:57 +08002286 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002287 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002288 if (lists_empty)
2289 return 0;
2290
2291 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002292 for (i = 0; i < I915_NUM_RINGS; i++) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002293 ret = i915_ring_idle(&dev_priv->ring[i]);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002294 if (ret)
2295 return ret;
2296 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002297
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002298 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002299}
2300
Daniel Vetterc6642782010-11-12 13:46:18 +00002301static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2302 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002303{
Chris Wilson05394f32010-11-08 19:18:58 +00002304 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002305 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002306 u32 size = obj->gtt_space->size;
2307 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002308 uint64_t val;
2309
Chris Wilson05394f32010-11-08 19:18:58 +00002310 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002311 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002312 val |= obj->gtt_offset & 0xfffff000;
2313 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002314 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2315
Chris Wilson05394f32010-11-08 19:18:58 +00002316 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002317 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2318 val |= I965_FENCE_REG_VALID;
2319
Daniel Vetterc6642782010-11-12 13:46:18 +00002320 if (pipelined) {
2321 int ret = intel_ring_begin(pipelined, 6);
2322 if (ret)
2323 return ret;
2324
2325 intel_ring_emit(pipelined, MI_NOOP);
2326 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2327 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2328 intel_ring_emit(pipelined, (u32)val);
2329 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2330 intel_ring_emit(pipelined, (u32)(val >> 32));
2331 intel_ring_advance(pipelined);
2332 } else
2333 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2334
2335 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002336}
2337
Daniel Vetterc6642782010-11-12 13:46:18 +00002338static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2339 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002340{
Chris Wilson05394f32010-11-08 19:18:58 +00002341 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002342 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002343 u32 size = obj->gtt_space->size;
2344 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002345 uint64_t val;
2346
Chris Wilson05394f32010-11-08 19:18:58 +00002347 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002348 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002349 val |= obj->gtt_offset & 0xfffff000;
2350 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2351 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2353 val |= I965_FENCE_REG_VALID;
2354
Daniel Vetterc6642782010-11-12 13:46:18 +00002355 if (pipelined) {
2356 int ret = intel_ring_begin(pipelined, 6);
2357 if (ret)
2358 return ret;
2359
2360 intel_ring_emit(pipelined, MI_NOOP);
2361 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2362 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2363 intel_ring_emit(pipelined, (u32)val);
2364 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2365 intel_ring_emit(pipelined, (u32)(val >> 32));
2366 intel_ring_advance(pipelined);
2367 } else
2368 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2369
2370 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002371}
2372
Daniel Vetterc6642782010-11-12 13:46:18 +00002373static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2374 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002375{
Chris Wilson05394f32010-11-08 19:18:58 +00002376 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002377 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002378 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002379 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002380 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002381
Daniel Vetterc6642782010-11-12 13:46:18 +00002382 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2383 (size & -size) != size ||
2384 (obj->gtt_offset & (size - 1)),
2385 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2386 obj->gtt_offset, obj->map_and_fenceable, size))
2387 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002388
Daniel Vetterc6642782010-11-12 13:46:18 +00002389 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002390 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002391 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002392 tile_width = 512;
2393
2394 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002395 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002396 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002397
Chris Wilson05394f32010-11-08 19:18:58 +00002398 val = obj->gtt_offset;
2399 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002400 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002401 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2403 val |= I830_FENCE_REG_VALID;
2404
Chris Wilson05394f32010-11-08 19:18:58 +00002405 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002406 if (fence_reg < 8)
2407 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002408 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002409 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002410
2411 if (pipelined) {
2412 int ret = intel_ring_begin(pipelined, 4);
2413 if (ret)
2414 return ret;
2415
2416 intel_ring_emit(pipelined, MI_NOOP);
2417 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2418 intel_ring_emit(pipelined, fence_reg);
2419 intel_ring_emit(pipelined, val);
2420 intel_ring_advance(pipelined);
2421 } else
2422 I915_WRITE(fence_reg, val);
2423
2424 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002425}
2426
Daniel Vetterc6642782010-11-12 13:46:18 +00002427static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2428 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002429{
Chris Wilson05394f32010-11-08 19:18:58 +00002430 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002431 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002432 u32 size = obj->gtt_space->size;
2433 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002434 uint32_t val;
2435 uint32_t pitch_val;
2436
Daniel Vetterc6642782010-11-12 13:46:18 +00002437 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2438 (size & -size) != size ||
2439 (obj->gtt_offset & (size - 1)),
2440 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2441 obj->gtt_offset, size))
2442 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002443
Chris Wilson05394f32010-11-08 19:18:58 +00002444 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002445 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002446
Chris Wilson05394f32010-11-08 19:18:58 +00002447 val = obj->gtt_offset;
2448 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002449 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002450 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002451 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2452 val |= I830_FENCE_REG_VALID;
2453
Daniel Vetterc6642782010-11-12 13:46:18 +00002454 if (pipelined) {
2455 int ret = intel_ring_begin(pipelined, 4);
2456 if (ret)
2457 return ret;
2458
2459 intel_ring_emit(pipelined, MI_NOOP);
2460 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2461 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2462 intel_ring_emit(pipelined, val);
2463 intel_ring_advance(pipelined);
2464 } else
2465 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2466
2467 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002468}
2469
Chris Wilsond9e86c02010-11-10 16:40:20 +00002470static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2471{
2472 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2473}
2474
2475static int
2476i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002477 struct intel_ring_buffer *pipelined)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002478{
2479 int ret;
2480
2481 if (obj->fenced_gpu_access) {
Chris Wilson88241782011-01-07 17:09:48 +00002482 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002483 ret = i915_gem_flush_ring(obj->last_fenced_ring,
Chris Wilson88241782011-01-07 17:09:48 +00002484 0, obj->base.write_domain);
2485 if (ret)
2486 return ret;
2487 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002488
2489 obj->fenced_gpu_access = false;
2490 }
2491
2492 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2493 if (!ring_passed_seqno(obj->last_fenced_ring,
2494 obj->last_fenced_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002495 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002496 obj->last_fenced_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002497 if (ret)
2498 return ret;
2499 }
2500
2501 obj->last_fenced_seqno = 0;
2502 obj->last_fenced_ring = NULL;
2503 }
2504
Chris Wilson63256ec2011-01-04 18:42:07 +00002505 /* Ensure that all CPU reads are completed before installing a fence
2506 * and all writes before removing the fence.
2507 */
2508 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2509 mb();
2510
Chris Wilsond9e86c02010-11-10 16:40:20 +00002511 return 0;
2512}
2513
2514int
2515i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2516{
2517 int ret;
2518
2519 if (obj->tiling_mode)
2520 i915_gem_release_mmap(obj);
2521
Chris Wilsonce453d82011-02-21 14:43:56 +00002522 ret = i915_gem_object_flush_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002523 if (ret)
2524 return ret;
2525
2526 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2527 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2528 i915_gem_clear_fence_reg(obj->base.dev,
2529 &dev_priv->fence_regs[obj->fence_reg]);
2530
2531 obj->fence_reg = I915_FENCE_REG_NONE;
2532 }
2533
2534 return 0;
2535}
2536
2537static struct drm_i915_fence_reg *
2538i915_find_fence_reg(struct drm_device *dev,
2539 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002540{
Daniel Vetterae3db242010-02-19 11:51:58 +01002541 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002542 struct drm_i915_fence_reg *reg, *first, *avail;
2543 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002544
2545 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002546 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002547 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2548 reg = &dev_priv->fence_regs[i];
2549 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002550 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002551
Chris Wilson05394f32010-11-08 19:18:58 +00002552 if (!reg->obj->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002553 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002554 }
2555
Chris Wilsond9e86c02010-11-10 16:40:20 +00002556 if (avail == NULL)
2557 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002558
2559 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002560 avail = first = NULL;
2561 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2562 if (reg->obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002563 continue;
2564
Chris Wilsond9e86c02010-11-10 16:40:20 +00002565 if (first == NULL)
2566 first = reg;
2567
2568 if (!pipelined ||
2569 !reg->obj->last_fenced_ring ||
2570 reg->obj->last_fenced_ring == pipelined) {
2571 avail = reg;
2572 break;
2573 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002574 }
2575
Chris Wilsond9e86c02010-11-10 16:40:20 +00002576 if (avail == NULL)
2577 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002578
Chris Wilsona00b10c2010-09-24 21:15:47 +01002579 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002580}
2581
Jesse Barnesde151cf2008-11-12 10:03:55 -08002582/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002583 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002584 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002585 * @pipelined: ring on which to queue the change, or NULL for CPU access
2586 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002587 *
2588 * When mapping objects through the GTT, userspace wants to be able to write
2589 * to them without having to worry about swizzling if the object is tiled.
2590 *
2591 * This function walks the fence regs looking for a free one for @obj,
2592 * stealing one if it can't find any.
2593 *
2594 * It then sets up the reg based on the object's properties: address, pitch
2595 * and tiling format.
2596 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002597int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002598i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002599 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002600{
Chris Wilson05394f32010-11-08 19:18:58 +00002601 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002602 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002603 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002604 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002605
Chris Wilson6bda10d2010-12-05 21:04:18 +00002606 /* XXX disable pipelining. There are bugs. Shocking. */
2607 pipelined = NULL;
2608
Chris Wilsond9e86c02010-11-10 16:40:20 +00002609 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002610 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2611 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002612 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002613
Chris Wilson29c5a582011-03-17 15:23:22 +00002614 if (obj->tiling_changed) {
2615 ret = i915_gem_object_flush_fence(obj, pipelined);
2616 if (ret)
2617 return ret;
2618
2619 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2620 pipelined = NULL;
2621
2622 if (pipelined) {
2623 reg->setup_seqno =
2624 i915_gem_next_request_seqno(pipelined);
2625 obj->last_fenced_seqno = reg->setup_seqno;
2626 obj->last_fenced_ring = pipelined;
2627 }
2628
2629 goto update;
2630 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002631
2632 if (!pipelined) {
2633 if (reg->setup_seqno) {
2634 if (!ring_passed_seqno(obj->last_fenced_ring,
2635 reg->setup_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002636 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002637 reg->setup_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002638 if (ret)
2639 return ret;
2640 }
2641
2642 reg->setup_seqno = 0;
2643 }
2644 } else if (obj->last_fenced_ring &&
2645 obj->last_fenced_ring != pipelined) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002646 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002647 if (ret)
2648 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002649 }
2650
Eric Anholta09ba7f2009-08-29 12:49:51 -07002651 return 0;
2652 }
2653
Chris Wilsond9e86c02010-11-10 16:40:20 +00002654 reg = i915_find_fence_reg(dev, pipelined);
2655 if (reg == NULL)
2656 return -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002657
Chris Wilsonce453d82011-02-21 14:43:56 +00002658 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002659 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002660 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002661
Chris Wilsond9e86c02010-11-10 16:40:20 +00002662 if (reg->obj) {
2663 struct drm_i915_gem_object *old = reg->obj;
2664
2665 drm_gem_object_reference(&old->base);
2666
2667 if (old->tiling_mode)
2668 i915_gem_release_mmap(old);
2669
Chris Wilsonce453d82011-02-21 14:43:56 +00002670 ret = i915_gem_object_flush_fence(old, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002671 if (ret) {
2672 drm_gem_object_unreference(&old->base);
2673 return ret;
2674 }
2675
2676 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2677 pipelined = NULL;
2678
2679 old->fence_reg = I915_FENCE_REG_NONE;
2680 old->last_fenced_ring = pipelined;
2681 old->last_fenced_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002682 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002683
2684 drm_gem_object_unreference(&old->base);
2685 } else if (obj->last_fenced_seqno == 0)
2686 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002687
Jesse Barnesde151cf2008-11-12 10:03:55 -08002688 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002689 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2690 obj->fence_reg = reg - dev_priv->fence_regs;
2691 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002692
Chris Wilsond9e86c02010-11-10 16:40:20 +00002693 reg->setup_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002694 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002695 obj->last_fenced_seqno = reg->setup_seqno;
2696
2697update:
2698 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002699 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002700 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002701 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002702 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002703 break;
2704 case 5:
2705 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002706 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002707 break;
2708 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002709 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002710 break;
2711 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002712 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002713 break;
2714 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002715
Daniel Vetterc6642782010-11-12 13:46:18 +00002716 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002717}
2718
2719/**
2720 * i915_gem_clear_fence_reg - clear out fence register info
2721 * @obj: object to clear
2722 *
2723 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002724 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002725 */
2726static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002727i915_gem_clear_fence_reg(struct drm_device *dev,
2728 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002729{
Jesse Barnes79e53942008-11-07 14:24:08 -08002730 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002731 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002732
Chris Wilsone259bef2010-09-17 00:32:02 +01002733 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002734 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002735 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002736 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002737 break;
2738 case 5:
2739 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002740 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002741 break;
2742 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002743 if (fence_reg >= 8)
2744 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002745 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002746 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002747 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002748
2749 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002750 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002751 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002752
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002753 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002754 reg->obj = NULL;
2755 reg->setup_seqno = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002756}
2757
2758/**
Eric Anholt673a3942008-07-30 12:06:12 -07002759 * Finds free space in the GTT aperture and binds the object there.
2760 */
2761static int
Chris Wilson05394f32010-11-08 19:18:58 +00002762i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002763 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002764 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002765{
Chris Wilson05394f32010-11-08 19:18:58 +00002766 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002767 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002768 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002769 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002770 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002771 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002772 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002773
Chris Wilson05394f32010-11-08 19:18:58 +00002774 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002775 DRM_ERROR("Attempting to bind a purgeable object\n");
2776 return -EINVAL;
2777 }
2778
Chris Wilson05394f32010-11-08 19:18:58 +00002779 fence_size = i915_gem_get_gtt_size(obj);
2780 fence_alignment = i915_gem_get_gtt_alignment(obj);
2781 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002782
Eric Anholt673a3942008-07-30 12:06:12 -07002783 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002784 alignment = map_and_fenceable ? fence_alignment :
2785 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002786 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002787 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2788 return -EINVAL;
2789 }
2790
Chris Wilson05394f32010-11-08 19:18:58 +00002791 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002792
Chris Wilson654fc602010-05-27 13:18:21 +01002793 /* If the object is bigger than the entire aperture, reject it early
2794 * before evicting everything in a vain attempt to find space.
2795 */
Chris Wilson05394f32010-11-08 19:18:58 +00002796 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002797 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002798 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2799 return -E2BIG;
2800 }
2801
Eric Anholt673a3942008-07-30 12:06:12 -07002802 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002803 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002804 free_space =
2805 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002806 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002807 dev_priv->mm.gtt_mappable_end,
2808 0);
2809 else
2810 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002811 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002812
2813 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002814 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002815 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002816 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002817 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002818 dev_priv->mm.gtt_mappable_end,
2819 0);
2820 else
Chris Wilson05394f32010-11-08 19:18:58 +00002821 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002822 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002823 }
Chris Wilson05394f32010-11-08 19:18:58 +00002824 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002825 /* If the gtt is empty and we're still having trouble
2826 * fitting our object in, we're out of memory.
2827 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002828 ret = i915_gem_evict_something(dev, size, alignment,
2829 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002830 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002831 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002832
Eric Anholt673a3942008-07-30 12:06:12 -07002833 goto search_free;
2834 }
2835
Chris Wilsone5281cc2010-10-28 13:45:36 +01002836 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002837 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002838 drm_mm_put_block(obj->gtt_space);
2839 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002840
2841 if (ret == -ENOMEM) {
Chris Wilson809b6332011-01-10 17:33:15 +00002842 /* first try to reclaim some memory by clearing the GTT */
2843 ret = i915_gem_evict_everything(dev, false);
Chris Wilson07f73f62009-09-14 16:50:30 +01002844 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002845 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002846 if (gfpmask) {
2847 gfpmask = 0;
2848 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002849 }
2850
Chris Wilson809b6332011-01-10 17:33:15 +00002851 return -ENOMEM;
Chris Wilson07f73f62009-09-14 16:50:30 +01002852 }
2853
2854 goto search_free;
2855 }
2856
Eric Anholt673a3942008-07-30 12:06:12 -07002857 return ret;
2858 }
2859
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002860 ret = i915_gem_gtt_bind_object(obj);
2861 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002862 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002863 drm_mm_put_block(obj->gtt_space);
2864 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002865
Chris Wilson809b6332011-01-10 17:33:15 +00002866 if (i915_gem_evict_everything(dev, false))
Chris Wilson07f73f62009-09-14 16:50:30 +01002867 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002868
2869 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002870 }
Eric Anholt673a3942008-07-30 12:06:12 -07002871
Chris Wilson6299f992010-11-24 12:23:44 +00002872 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002873 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002874
Eric Anholt673a3942008-07-30 12:06:12 -07002875 /* Assert that the object is not currently in any GPU domain. As it
2876 * wasn't in the GTT, there shouldn't be any way it could have been in
2877 * a GPU cache
2878 */
Chris Wilson05394f32010-11-08 19:18:58 +00002879 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2880 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002881
Chris Wilson6299f992010-11-24 12:23:44 +00002882 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002883
Daniel Vetter75e9e912010-11-04 17:11:09 +01002884 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002885 obj->gtt_space->size == fence_size &&
2886 (obj->gtt_space->start & (fence_alignment -1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002887
Daniel Vetter75e9e912010-11-04 17:11:09 +01002888 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002889 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002890
Chris Wilson05394f32010-11-08 19:18:58 +00002891 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002892
Chris Wilsondb53a302011-02-03 11:57:46 +00002893 trace_i915_gem_object_bind(obj, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002894 return 0;
2895}
2896
2897void
Chris Wilson05394f32010-11-08 19:18:58 +00002898i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002899{
Eric Anholt673a3942008-07-30 12:06:12 -07002900 /* If we don't have a page list set up, then we're not pinned
2901 * to GPU, and we can ignore the cache flush because it'll happen
2902 * again at bind time.
2903 */
Chris Wilson05394f32010-11-08 19:18:58 +00002904 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002905 return;
2906
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002907 /* If the GPU is snooping the contents of the CPU cache,
2908 * we do not need to manually clear the CPU cache lines. However,
2909 * the caches are only snooped when the render cache is
2910 * flushed/invalidated. As we always have to emit invalidations
2911 * and flushes when moving into and out of the RENDER domain, correct
2912 * snooping behaviour occurs naturally as the result of our domain
2913 * tracking.
2914 */
2915 if (obj->cache_level != I915_CACHE_NONE)
2916 return;
2917
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002918 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002919
Chris Wilson05394f32010-11-08 19:18:58 +00002920 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002921}
2922
Eric Anholte47c68e2008-11-14 13:35:19 -08002923/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson88241782011-01-07 17:09:48 +00002924static int
Chris Wilson3619df02010-11-28 15:37:17 +00002925i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002926{
Chris Wilson05394f32010-11-08 19:18:58 +00002927 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson88241782011-01-07 17:09:48 +00002928 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002929
2930 /* Queue the GPU write cache flushing we need. */
Chris Wilsondb53a302011-02-03 11:57:46 +00002931 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002932}
2933
2934/** Flushes the GTT write domain for the object if it's dirty. */
2935static void
Chris Wilson05394f32010-11-08 19:18:58 +00002936i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002937{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002938 uint32_t old_write_domain;
2939
Chris Wilson05394f32010-11-08 19:18:58 +00002940 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002941 return;
2942
Chris Wilson63256ec2011-01-04 18:42:07 +00002943 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002944 * to it immediately go to main memory as far as we know, so there's
2945 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002946 *
2947 * However, we do have to enforce the order so that all writes through
2948 * the GTT land before any writes to the device, such as updates to
2949 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002950 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002951 wmb();
2952
Chris Wilson05394f32010-11-08 19:18:58 +00002953 old_write_domain = obj->base.write_domain;
2954 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002955
2956 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002957 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002958 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002959}
2960
2961/** Flushes the CPU write domain for the object if it's dirty. */
2962static void
Chris Wilson05394f32010-11-08 19:18:58 +00002963i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002964{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002965 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002966
Chris Wilson05394f32010-11-08 19:18:58 +00002967 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002968 return;
2969
2970 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002971 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002972 old_write_domain = obj->base.write_domain;
2973 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002974
2975 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002976 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002977 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002978}
2979
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002980/**
2981 * Moves a single object to the GTT read, and possibly write domain.
2982 *
2983 * This function returns when the move is complete, including waiting on
2984 * flushes to occur.
2985 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002986int
Chris Wilson20217462010-11-23 15:26:33 +00002987i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002988{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002989 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002990 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002991
Eric Anholt02354392008-11-26 13:58:13 -08002992 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002993 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002994 return -EINVAL;
2995
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002996 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2997 return 0;
2998
Chris Wilson88241782011-01-07 17:09:48 +00002999 ret = i915_gem_object_flush_gpu_write_domain(obj);
3000 if (ret)
3001 return ret;
3002
Chris Wilson87ca9c82010-12-02 09:42:56 +00003003 if (obj->pending_gpu_write || write) {
Chris Wilsonce453d82011-02-21 14:43:56 +00003004 ret = i915_gem_object_wait_rendering(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00003005 if (ret)
3006 return ret;
3007 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003008
Chris Wilson72133422010-09-13 23:56:38 +01003009 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003010
Chris Wilson05394f32010-11-08 19:18:58 +00003011 old_write_domain = obj->base.write_domain;
3012 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003013
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003014 /* It should now be out of any other write domains, and we can update
3015 * the domain values for our changes.
3016 */
Chris Wilson05394f32010-11-08 19:18:58 +00003017 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3018 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003019 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003020 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3021 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3022 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003023 }
3024
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003025 trace_i915_gem_object_change_domain(obj,
3026 old_read_domains,
3027 old_write_domain);
3028
Eric Anholte47c68e2008-11-14 13:35:19 -08003029 return 0;
3030}
3031
Chris Wilsone4ffd172011-04-04 09:44:39 +01003032int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3033 enum i915_cache_level cache_level)
3034{
3035 int ret;
3036
3037 if (obj->cache_level == cache_level)
3038 return 0;
3039
3040 if (obj->pin_count) {
3041 DRM_DEBUG("can not change the cache level of pinned objects\n");
3042 return -EBUSY;
3043 }
3044
3045 if (obj->gtt_space) {
3046 ret = i915_gem_object_finish_gpu(obj);
3047 if (ret)
3048 return ret;
3049
3050 i915_gem_object_finish_gtt(obj);
3051
3052 /* Before SandyBridge, you could not use tiling or fence
3053 * registers with snooped memory, so relinquish any fences
3054 * currently pointing to our region in the aperture.
3055 */
3056 if (INTEL_INFO(obj->base.dev)->gen < 6) {
3057 ret = i915_gem_object_put_fence(obj);
3058 if (ret)
3059 return ret;
3060 }
3061
3062 i915_gem_gtt_rebind_object(obj, cache_level);
3063 }
3064
3065 if (cache_level == I915_CACHE_NONE) {
3066 u32 old_read_domains, old_write_domain;
3067
3068 /* If we're coming from LLC cached, then we haven't
3069 * actually been tracking whether the data is in the
3070 * CPU cache or not, since we only allow one bit set
3071 * in obj->write_domain and have been skipping the clflushes.
3072 * Just set it to the CPU cache for now.
3073 */
3074 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3075 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3076
3077 old_read_domains = obj->base.read_domains;
3078 old_write_domain = obj->base.write_domain;
3079
3080 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3081 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3082
3083 trace_i915_gem_object_change_domain(obj,
3084 old_read_domains,
3085 old_write_domain);
3086 }
3087
3088 obj->cache_level = cache_level;
3089 return 0;
3090}
3091
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003092/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003093 * Prepare buffer for display plane (scanout, cursors, etc).
3094 * Can be called from an uninterruptible phase (modesetting) and allows
3095 * any flushes to be pipelined (for pageflips).
3096 *
3097 * For the display plane, we want to be in the GTT but out of any write
3098 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
3099 * ability to pipeline the waits, pinning and any additional subtleties
3100 * that may differentiate the display plane from ordinary buffers.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003101 */
3102int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003103i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3104 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003105 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003106{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003107 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003108 int ret;
3109
Chris Wilson88241782011-01-07 17:09:48 +00003110 ret = i915_gem_object_flush_gpu_write_domain(obj);
3111 if (ret)
3112 return ret;
3113
Chris Wilson0be73282010-12-06 14:36:27 +00003114 if (pipelined != obj->ring) {
Chris Wilsonce453d82011-02-21 14:43:56 +00003115 ret = i915_gem_object_wait_rendering(obj);
Keith Packardf0b69ef2011-07-19 16:21:40 -07003116 if (ret == -ERESTARTSYS)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003117 return ret;
3118 }
3119
Eric Anholta7ef0642011-03-29 16:59:54 -07003120 /* The display engine is not coherent with the LLC cache on gen6. As
3121 * a result, we make sure that the pinning that is about to occur is
3122 * done with uncached PTEs. This is lowest common denominator for all
3123 * chipsets.
3124 *
3125 * However for gen6+, we could do better by using the GFDT bit instead
3126 * of uncaching, which would allow us to flush all the LLC-cached data
3127 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3128 */
3129 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3130 if (ret)
3131 return ret;
3132
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003133 /* As the user may map the buffer once pinned in the display plane
3134 * (e.g. libkms for the bootup splash), we have to ensure that we
3135 * always use map_and_fenceable for all scanout buffers.
3136 */
3137 ret = i915_gem_object_pin(obj, alignment, true);
3138 if (ret)
3139 return ret;
3140
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003141 i915_gem_object_flush_cpu_write_domain(obj);
3142
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003143 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003144 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003145
3146 /* It should now be out of any other write domains, and we can update
3147 * the domain values for our changes.
3148 */
3149 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003150 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003151
3152 trace_i915_gem_object_change_domain(obj,
3153 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003154 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003155
3156 return 0;
3157}
3158
Chris Wilson85345512010-11-13 09:49:11 +00003159int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003160i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003161{
Chris Wilson88241782011-01-07 17:09:48 +00003162 int ret;
3163
Chris Wilsona8198ee2011-04-13 22:04:09 +01003164 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003165 return 0;
3166
Chris Wilson88241782011-01-07 17:09:48 +00003167 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003168 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Chris Wilson88241782011-01-07 17:09:48 +00003169 if (ret)
3170 return ret;
3171 }
Chris Wilson85345512010-11-13 09:49:11 +00003172
Chris Wilsona8198ee2011-04-13 22:04:09 +01003173 /* Ensure that we invalidate the GPU's caches and TLBs. */
3174 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3175
Chris Wilsonce453d82011-02-21 14:43:56 +00003176 return i915_gem_object_wait_rendering(obj);
Chris Wilson85345512010-11-13 09:49:11 +00003177}
3178
Eric Anholte47c68e2008-11-14 13:35:19 -08003179/**
3180 * Moves a single object to the CPU read, and possibly write domain.
3181 *
3182 * This function returns when the move is complete, including waiting on
3183 * flushes to occur.
3184 */
3185static int
Chris Wilson919926a2010-11-12 13:42:53 +00003186i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003187{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003188 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003189 int ret;
3190
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003191 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3192 return 0;
3193
Chris Wilson88241782011-01-07 17:09:48 +00003194 ret = i915_gem_object_flush_gpu_write_domain(obj);
3195 if (ret)
3196 return ret;
3197
Chris Wilsonce453d82011-02-21 14:43:56 +00003198 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003199 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003200 return ret;
3201
3202 i915_gem_object_flush_gtt_write_domain(obj);
3203
3204 /* If we have a partially-valid cache of the object in the CPU,
3205 * finish invalidating it and free the per-page flags.
3206 */
3207 i915_gem_object_set_to_full_cpu_read_domain(obj);
3208
Chris Wilson05394f32010-11-08 19:18:58 +00003209 old_write_domain = obj->base.write_domain;
3210 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003211
Eric Anholte47c68e2008-11-14 13:35:19 -08003212 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003213 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003214 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003215
Chris Wilson05394f32010-11-08 19:18:58 +00003216 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003217 }
3218
3219 /* It should now be out of any other write domains, and we can update
3220 * the domain values for our changes.
3221 */
Chris Wilson05394f32010-11-08 19:18:58 +00003222 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003223
3224 /* If we're writing through the CPU, then the GPU read domains will
3225 * need to be invalidated at next use.
3226 */
3227 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003228 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3229 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003230 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003231
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003232 trace_i915_gem_object_change_domain(obj,
3233 old_read_domains,
3234 old_write_domain);
3235
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003236 return 0;
3237}
3238
Eric Anholt673a3942008-07-30 12:06:12 -07003239/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003240 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003241 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003242 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3243 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3244 */
3245static void
Chris Wilson05394f32010-11-08 19:18:58 +00003246i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003247{
Chris Wilson05394f32010-11-08 19:18:58 +00003248 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003249 return;
3250
3251 /* If we're partially in the CPU read domain, finish moving it in.
3252 */
Chris Wilson05394f32010-11-08 19:18:58 +00003253 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003254 int i;
3255
Chris Wilson05394f32010-11-08 19:18:58 +00003256 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3257 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003258 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003259 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003260 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003261 }
3262
3263 /* Free the page_cpu_valid mappings which are now stale, whether
3264 * or not we've got I915_GEM_DOMAIN_CPU.
3265 */
Chris Wilson05394f32010-11-08 19:18:58 +00003266 kfree(obj->page_cpu_valid);
3267 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003268}
3269
3270/**
3271 * Set the CPU read domain on a range of the object.
3272 *
3273 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3274 * not entirely valid. The page_cpu_valid member of the object flags which
3275 * pages have been flushed, and will be respected by
3276 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3277 * of the whole object.
3278 *
3279 * This function returns when the move is complete, including waiting on
3280 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003281 */
3282static int
Chris Wilson05394f32010-11-08 19:18:58 +00003283i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003284 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003285{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003286 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003287 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003288
Chris Wilson05394f32010-11-08 19:18:58 +00003289 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003290 return i915_gem_object_set_to_cpu_domain(obj, 0);
3291
Chris Wilson88241782011-01-07 17:09:48 +00003292 ret = i915_gem_object_flush_gpu_write_domain(obj);
3293 if (ret)
3294 return ret;
3295
Chris Wilsonce453d82011-02-21 14:43:56 +00003296 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003297 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003298 return ret;
Daniel Vetterde18a292010-11-27 22:30:41 +01003299
Eric Anholte47c68e2008-11-14 13:35:19 -08003300 i915_gem_object_flush_gtt_write_domain(obj);
3301
3302 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003303 if (obj->page_cpu_valid == NULL &&
3304 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003305 return 0;
3306
Eric Anholte47c68e2008-11-14 13:35:19 -08003307 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3308 * newly adding I915_GEM_DOMAIN_CPU
3309 */
Chris Wilson05394f32010-11-08 19:18:58 +00003310 if (obj->page_cpu_valid == NULL) {
3311 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3312 GFP_KERNEL);
3313 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003314 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003315 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3316 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003317
3318 /* Flush the cache on any pages that are still invalid from the CPU's
3319 * perspective.
3320 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003321 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3322 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003323 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003324 continue;
3325
Chris Wilson05394f32010-11-08 19:18:58 +00003326 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003327
Chris Wilson05394f32010-11-08 19:18:58 +00003328 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003329 }
3330
Eric Anholte47c68e2008-11-14 13:35:19 -08003331 /* It should now be out of any other write domains, and we can update
3332 * the domain values for our changes.
3333 */
Chris Wilson05394f32010-11-08 19:18:58 +00003334 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003335
Chris Wilson05394f32010-11-08 19:18:58 +00003336 old_read_domains = obj->base.read_domains;
3337 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003338
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003339 trace_i915_gem_object_change_domain(obj,
3340 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003341 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003342
Eric Anholt673a3942008-07-30 12:06:12 -07003343 return 0;
3344}
3345
Eric Anholt673a3942008-07-30 12:06:12 -07003346/* Throttle our rendering by waiting until the ring has completed our requests
3347 * emitted over 20 msec ago.
3348 *
Eric Anholtb9624422009-06-03 07:27:35 +00003349 * Note that if we were to use the current jiffies each time around the loop,
3350 * we wouldn't escape the function with any frames outstanding if the time to
3351 * render a frame was over 20ms.
3352 *
Eric Anholt673a3942008-07-30 12:06:12 -07003353 * This should get us reasonable parallelism between CPU and GPU but also
3354 * relatively low latency when blocking on a particular request to finish.
3355 */
3356static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003357i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003358{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003359 struct drm_i915_private *dev_priv = dev->dev_private;
3360 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003361 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003362 struct drm_i915_gem_request *request;
3363 struct intel_ring_buffer *ring = NULL;
3364 u32 seqno = 0;
3365 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003366
Chris Wilsone110e8d2011-01-26 15:39:14 +00003367 if (atomic_read(&dev_priv->mm.wedged))
3368 return -EIO;
3369
Chris Wilson1c255952010-09-26 11:03:27 +01003370 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003371 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003372 if (time_after_eq(request->emitted_jiffies, recent_enough))
3373 break;
3374
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003375 ring = request->ring;
3376 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003377 }
Chris Wilson1c255952010-09-26 11:03:27 +01003378 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003379
3380 if (seqno == 0)
3381 return 0;
3382
3383 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003384 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003385 /* And wait for the seqno passing without holding any locks and
3386 * causing extra latency for others. This is safe as the irq
3387 * generation is designed to be run atomically and so is
3388 * lockless.
3389 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003390 if (ring->irq_get(ring)) {
3391 ret = wait_event_interruptible(ring->irq_queue,
3392 i915_seqno_passed(ring->get_seqno(ring), seqno)
3393 || atomic_read(&dev_priv->mm.wedged));
3394 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003395
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003396 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3397 ret = -EIO;
3398 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003399 }
3400
3401 if (ret == 0)
3402 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003403
Eric Anholt673a3942008-07-30 12:06:12 -07003404 return ret;
3405}
3406
Eric Anholt673a3942008-07-30 12:06:12 -07003407int
Chris Wilson05394f32010-11-08 19:18:58 +00003408i915_gem_object_pin(struct drm_i915_gem_object *obj,
3409 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003410 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003411{
Chris Wilson05394f32010-11-08 19:18:58 +00003412 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003413 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003414 int ret;
3415
Chris Wilson05394f32010-11-08 19:18:58 +00003416 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003417 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003418
Chris Wilson05394f32010-11-08 19:18:58 +00003419 if (obj->gtt_space != NULL) {
3420 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3421 (map_and_fenceable && !obj->map_and_fenceable)) {
3422 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003423 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003424 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3425 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003426 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003427 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003428 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003429 ret = i915_gem_object_unbind(obj);
3430 if (ret)
3431 return ret;
3432 }
3433 }
3434
Chris Wilson05394f32010-11-08 19:18:58 +00003435 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003436 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003437 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003438 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003439 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003440 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003441
Chris Wilson05394f32010-11-08 19:18:58 +00003442 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003443 if (!obj->active)
3444 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003445 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003446 }
Chris Wilson6299f992010-11-24 12:23:44 +00003447 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003448
Chris Wilson23bc5982010-09-29 16:10:57 +01003449 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003450 return 0;
3451}
3452
3453void
Chris Wilson05394f32010-11-08 19:18:58 +00003454i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003455{
Chris Wilson05394f32010-11-08 19:18:58 +00003456 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003457 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003458
Chris Wilson23bc5982010-09-29 16:10:57 +01003459 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003460 BUG_ON(obj->pin_count == 0);
3461 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003462
Chris Wilson05394f32010-11-08 19:18:58 +00003463 if (--obj->pin_count == 0) {
3464 if (!obj->active)
3465 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003466 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003467 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003468 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003469 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003470}
3471
3472int
3473i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003474 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003475{
3476 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003477 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003478 int ret;
3479
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003480 ret = i915_mutex_lock_interruptible(dev);
3481 if (ret)
3482 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003483
Chris Wilson05394f32010-11-08 19:18:58 +00003484 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003485 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003486 ret = -ENOENT;
3487 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003488 }
Eric Anholt673a3942008-07-30 12:06:12 -07003489
Chris Wilson05394f32010-11-08 19:18:58 +00003490 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003491 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003492 ret = -EINVAL;
3493 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003494 }
3495
Chris Wilson05394f32010-11-08 19:18:58 +00003496 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003497 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3498 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003499 ret = -EINVAL;
3500 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003501 }
3502
Chris Wilson05394f32010-11-08 19:18:58 +00003503 obj->user_pin_count++;
3504 obj->pin_filp = file;
3505 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003506 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003507 if (ret)
3508 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003509 }
3510
3511 /* XXX - flush the CPU caches for pinned objects
3512 * as the X server doesn't manage domains yet
3513 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003514 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003515 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003516out:
Chris Wilson05394f32010-11-08 19:18:58 +00003517 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003518unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003519 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003520 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003521}
3522
3523int
3524i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003525 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003526{
3527 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003528 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003529 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003530
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003531 ret = i915_mutex_lock_interruptible(dev);
3532 if (ret)
3533 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003534
Chris Wilson05394f32010-11-08 19:18:58 +00003535 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003536 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003537 ret = -ENOENT;
3538 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003539 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003540
Chris Wilson05394f32010-11-08 19:18:58 +00003541 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003542 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3543 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003544 ret = -EINVAL;
3545 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003546 }
Chris Wilson05394f32010-11-08 19:18:58 +00003547 obj->user_pin_count--;
3548 if (obj->user_pin_count == 0) {
3549 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003550 i915_gem_object_unpin(obj);
3551 }
Eric Anholt673a3942008-07-30 12:06:12 -07003552
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003553out:
Chris Wilson05394f32010-11-08 19:18:58 +00003554 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003555unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003556 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003557 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003558}
3559
3560int
3561i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003562 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003563{
3564 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003565 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003566 int ret;
3567
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003568 ret = i915_mutex_lock_interruptible(dev);
3569 if (ret)
3570 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003571
Chris Wilson05394f32010-11-08 19:18:58 +00003572 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003573 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003574 ret = -ENOENT;
3575 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003576 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003577
Chris Wilson0be555b2010-08-04 15:36:30 +01003578 /* Count all active objects as busy, even if they are currently not used
3579 * by the gpu. Users of this interface expect objects to eventually
3580 * become non-busy without any further actions, therefore emit any
3581 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003582 */
Chris Wilson05394f32010-11-08 19:18:58 +00003583 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003584 if (args->busy) {
3585 /* Unconditionally flush objects, even when the gpu still uses this
3586 * object. Userspace calling this function indicates that it wants to
3587 * use this buffer rather sooner than later, so issuing the required
3588 * flush earlier is beneficial.
3589 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003590 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003591 ret = i915_gem_flush_ring(obj->ring,
Chris Wilson88241782011-01-07 17:09:48 +00003592 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003593 } else if (obj->ring->outstanding_lazy_request ==
3594 obj->last_rendering_seqno) {
3595 struct drm_i915_gem_request *request;
3596
Chris Wilson7a194872010-12-07 10:38:40 +00003597 /* This ring is not being cleared by active usage,
3598 * so emit a request to do so.
3599 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003600 request = kzalloc(sizeof(*request), GFP_KERNEL);
3601 if (request)
Chris Wilsondb53a302011-02-03 11:57:46 +00003602 ret = i915_add_request(obj->ring, NULL,request);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003603 else
Chris Wilson7a194872010-12-07 10:38:40 +00003604 ret = -ENOMEM;
3605 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003606
3607 /* Update the active list for the hardware's current position.
3608 * Otherwise this only updates on a delayed timer or when irqs
3609 * are actually unmasked, and our working set ends up being
3610 * larger than required.
3611 */
Chris Wilsondb53a302011-02-03 11:57:46 +00003612 i915_gem_retire_requests_ring(obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003613
Chris Wilson05394f32010-11-08 19:18:58 +00003614 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003615 }
Eric Anholt673a3942008-07-30 12:06:12 -07003616
Chris Wilson05394f32010-11-08 19:18:58 +00003617 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003618unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003619 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003620 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003621}
3622
3623int
3624i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3625 struct drm_file *file_priv)
3626{
3627 return i915_gem_ring_throttle(dev, file_priv);
3628}
3629
Chris Wilson3ef94da2009-09-14 16:50:29 +01003630int
3631i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3632 struct drm_file *file_priv)
3633{
3634 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003635 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003636 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003637
3638 switch (args->madv) {
3639 case I915_MADV_DONTNEED:
3640 case I915_MADV_WILLNEED:
3641 break;
3642 default:
3643 return -EINVAL;
3644 }
3645
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003646 ret = i915_mutex_lock_interruptible(dev);
3647 if (ret)
3648 return ret;
3649
Chris Wilson05394f32010-11-08 19:18:58 +00003650 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003651 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003652 ret = -ENOENT;
3653 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003654 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003655
Chris Wilson05394f32010-11-08 19:18:58 +00003656 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003657 ret = -EINVAL;
3658 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003659 }
3660
Chris Wilson05394f32010-11-08 19:18:58 +00003661 if (obj->madv != __I915_MADV_PURGED)
3662 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003663
Chris Wilson2d7ef392009-09-20 23:13:10 +01003664 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003665 if (i915_gem_object_is_purgeable(obj) &&
3666 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003667 i915_gem_object_truncate(obj);
3668
Chris Wilson05394f32010-11-08 19:18:58 +00003669 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003670
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003671out:
Chris Wilson05394f32010-11-08 19:18:58 +00003672 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003673unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003674 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003675 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003676}
3677
Chris Wilson05394f32010-11-08 19:18:58 +00003678struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3679 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003680{
Chris Wilson73aa8082010-09-30 11:46:12 +01003681 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003682 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003683 struct address_space *mapping;
Daniel Vetterc397b902010-04-09 19:05:07 +00003684
3685 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3686 if (obj == NULL)
3687 return NULL;
3688
3689 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3690 kfree(obj);
3691 return NULL;
3692 }
3693
Hugh Dickins5949eac2011-06-27 16:18:18 -07003694 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3695 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3696
Chris Wilson73aa8082010-09-30 11:46:12 +01003697 i915_gem_info_add_obj(dev_priv, size);
3698
Daniel Vetterc397b902010-04-09 19:05:07 +00003699 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3700 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3701
Eric Anholta1871112011-03-29 16:59:55 -07003702 if (IS_GEN6(dev)) {
3703 /* On Gen6, we can have the GPU use the LLC (the CPU
3704 * cache) for about a 10% performance improvement
3705 * compared to uncached. Graphics requests other than
3706 * display scanout are coherent with the CPU in
3707 * accessing this cache. This means in this mode we
3708 * don't need to clflush on the CPU side, and on the
3709 * GPU side we only need to flush internal caches to
3710 * get data visible to the CPU.
3711 *
3712 * However, we maintain the display planes as UC, and so
3713 * need to rebind when first used as such.
3714 */
3715 obj->cache_level = I915_CACHE_LLC;
3716 } else
3717 obj->cache_level = I915_CACHE_NONE;
3718
Daniel Vetter62b8b212010-04-09 19:05:08 +00003719 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003720 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003721 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003722 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003723 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003724 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003725 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003726 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003727 /* Avoid an unnecessary call to unbind on the first bind. */
3728 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003729
Chris Wilson05394f32010-11-08 19:18:58 +00003730 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003731}
3732
Eric Anholt673a3942008-07-30 12:06:12 -07003733int i915_gem_init_object(struct drm_gem_object *obj)
3734{
Daniel Vetterc397b902010-04-09 19:05:07 +00003735 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003736
Eric Anholt673a3942008-07-30 12:06:12 -07003737 return 0;
3738}
3739
Chris Wilson05394f32010-11-08 19:18:58 +00003740static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003741{
Chris Wilson05394f32010-11-08 19:18:58 +00003742 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003743 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003744 int ret;
3745
3746 ret = i915_gem_object_unbind(obj);
3747 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003748 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003749 &dev_priv->mm.deferred_free_list);
3750 return;
3751 }
3752
Chris Wilson26e12f82011-03-20 11:20:19 +00003753 trace_i915_gem_object_destroy(obj);
3754
Chris Wilson05394f32010-11-08 19:18:58 +00003755 if (obj->base.map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01003756 i915_gem_free_mmap_offset(obj);
3757
Chris Wilson05394f32010-11-08 19:18:58 +00003758 drm_gem_object_release(&obj->base);
3759 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003760
Chris Wilson05394f32010-11-08 19:18:58 +00003761 kfree(obj->page_cpu_valid);
3762 kfree(obj->bit_17);
3763 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003764}
3765
Chris Wilson05394f32010-11-08 19:18:58 +00003766void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003767{
Chris Wilson05394f32010-11-08 19:18:58 +00003768 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3769 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003770
Chris Wilson05394f32010-11-08 19:18:58 +00003771 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003772 i915_gem_object_unpin(obj);
3773
Chris Wilson05394f32010-11-08 19:18:58 +00003774 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003775 i915_gem_detach_phys_object(dev, obj);
3776
Chris Wilsonbe726152010-07-23 23:18:50 +01003777 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003778}
3779
Jesse Barnes5669fca2009-02-17 15:13:31 -08003780int
Eric Anholt673a3942008-07-30 12:06:12 -07003781i915_gem_idle(struct drm_device *dev)
3782{
3783 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003784 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003785
Keith Packard6dbe2772008-10-14 21:41:13 -07003786 mutex_lock(&dev->struct_mutex);
3787
Chris Wilson87acb0a2010-10-19 10:13:00 +01003788 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003789 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003790 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003791 }
Eric Anholt673a3942008-07-30 12:06:12 -07003792
Chris Wilson29105cc2010-01-07 10:39:13 +00003793 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003794 if (ret) {
3795 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003796 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003797 }
Eric Anholt673a3942008-07-30 12:06:12 -07003798
Chris Wilson29105cc2010-01-07 10:39:13 +00003799 /* Under UMS, be paranoid and evict. */
3800 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003801 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003802 if (ret) {
3803 mutex_unlock(&dev->struct_mutex);
3804 return ret;
3805 }
3806 }
3807
Chris Wilson312817a2010-11-22 11:50:11 +00003808 i915_gem_reset_fences(dev);
3809
Chris Wilson29105cc2010-01-07 10:39:13 +00003810 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3811 * We need to replace this with a semaphore, or something.
3812 * And not confound mm.suspended!
3813 */
3814 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003815 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003816
3817 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003818 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003819
Keith Packard6dbe2772008-10-14 21:41:13 -07003820 mutex_unlock(&dev->struct_mutex);
3821
Chris Wilson29105cc2010-01-07 10:39:13 +00003822 /* Cancel the retire work handler, which should be idle now. */
3823 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3824
Eric Anholt673a3942008-07-30 12:06:12 -07003825 return 0;
3826}
3827
Eric Anholt673a3942008-07-30 12:06:12 -07003828int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003829i915_gem_init_ringbuffer(struct drm_device *dev)
3830{
3831 drm_i915_private_t *dev_priv = dev->dev_private;
3832 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003833
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003834 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003835 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003836 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003837
3838 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003839 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003840 if (ret)
3841 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003842 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003843
Chris Wilson549f7362010-10-19 11:19:32 +01003844 if (HAS_BLT(dev)) {
3845 ret = intel_init_blt_ring_buffer(dev);
3846 if (ret)
3847 goto cleanup_bsd_ring;
3848 }
3849
Chris Wilson6f392d52010-08-07 11:01:22 +01003850 dev_priv->next_seqno = 1;
3851
Chris Wilson68f95ba2010-05-27 13:18:22 +01003852 return 0;
3853
Chris Wilson549f7362010-10-19 11:19:32 +01003854cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003855 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003856cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003857 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003858 return ret;
3859}
3860
3861void
3862i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3863{
3864 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003865 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003866
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003867 for (i = 0; i < I915_NUM_RINGS; i++)
3868 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003869}
3870
3871int
Eric Anholt673a3942008-07-30 12:06:12 -07003872i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3873 struct drm_file *file_priv)
3874{
3875 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003876 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003877
Jesse Barnes79e53942008-11-07 14:24:08 -08003878 if (drm_core_check_feature(dev, DRIVER_MODESET))
3879 return 0;
3880
Ben Gamariba1234d2009-09-14 17:48:47 -04003881 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003882 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003883 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003884 }
3885
Eric Anholt673a3942008-07-30 12:06:12 -07003886 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003887 dev_priv->mm.suspended = 0;
3888
3889 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003890 if (ret != 0) {
3891 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003892 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003893 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003894
Chris Wilson69dc4982010-10-19 10:36:51 +01003895 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003896 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3897 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003898 for (i = 0; i < I915_NUM_RINGS; i++) {
3899 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3900 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3901 }
Eric Anholt673a3942008-07-30 12:06:12 -07003902 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003903
Chris Wilson5f353082010-06-07 14:03:03 +01003904 ret = drm_irq_install(dev);
3905 if (ret)
3906 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003907
Eric Anholt673a3942008-07-30 12:06:12 -07003908 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003909
3910cleanup_ringbuffer:
3911 mutex_lock(&dev->struct_mutex);
3912 i915_gem_cleanup_ringbuffer(dev);
3913 dev_priv->mm.suspended = 1;
3914 mutex_unlock(&dev->struct_mutex);
3915
3916 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003917}
3918
3919int
3920i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3921 struct drm_file *file_priv)
3922{
Jesse Barnes79e53942008-11-07 14:24:08 -08003923 if (drm_core_check_feature(dev, DRIVER_MODESET))
3924 return 0;
3925
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003926 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003927 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003928}
3929
3930void
3931i915_gem_lastclose(struct drm_device *dev)
3932{
3933 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003934
Eric Anholte806b492009-01-22 09:56:58 -08003935 if (drm_core_check_feature(dev, DRIVER_MODESET))
3936 return;
3937
Keith Packard6dbe2772008-10-14 21:41:13 -07003938 ret = i915_gem_idle(dev);
3939 if (ret)
3940 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003941}
3942
Chris Wilson64193402010-10-24 12:38:05 +01003943static void
3944init_ring_lists(struct intel_ring_buffer *ring)
3945{
3946 INIT_LIST_HEAD(&ring->active_list);
3947 INIT_LIST_HEAD(&ring->request_list);
3948 INIT_LIST_HEAD(&ring->gpu_write_list);
3949}
3950
Eric Anholt673a3942008-07-30 12:06:12 -07003951void
3952i915_gem_load(struct drm_device *dev)
3953{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003954 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003955 drm_i915_private_t *dev_priv = dev->dev_private;
3956
Chris Wilson69dc4982010-10-19 10:36:51 +01003957 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003958 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3959 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003960 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003961 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003962 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003963 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003964 for (i = 0; i < I915_NUM_RINGS; i++)
3965 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003966 for (i = 0; i < 16; i++)
3967 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003968 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3969 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003970 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003971
Dave Airlie94400122010-07-20 13:15:31 +10003972 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3973 if (IS_GEN3(dev)) {
3974 u32 tmp = I915_READ(MI_ARB_STATE);
3975 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3976 /* arb state is a masked write, so set bit + bit in mask */
3977 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3978 I915_WRITE(MI_ARB_STATE, tmp);
3979 }
3980 }
3981
Chris Wilson72bfa192010-12-19 11:42:05 +00003982 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3983
Jesse Barnesde151cf2008-11-12 10:03:55 -08003984 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003985 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3986 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003987
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003988 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003989 dev_priv->num_fence_regs = 16;
3990 else
3991 dev_priv->num_fence_regs = 8;
3992
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003993 /* Initialize fence registers to zero */
Eric Anholt10ed13e2011-05-06 13:53:49 -07003994 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3995 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003996 }
Eric Anholt10ed13e2011-05-06 13:53:49 -07003997
Eric Anholt673a3942008-07-30 12:06:12 -07003998 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003999 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004000
Chris Wilsonce453d82011-02-21 14:43:56 +00004001 dev_priv->mm.interruptible = true;
4002
Chris Wilson17250b72010-10-28 12:51:39 +01004003 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4004 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4005 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004006}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004007
4008/*
4009 * Create a physically contiguous memory object for this object
4010 * e.g. for cursor + overlay regs
4011 */
Chris Wilson995b6762010-08-20 13:23:26 +01004012static int i915_gem_init_phys_object(struct drm_device *dev,
4013 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004014{
4015 drm_i915_private_t *dev_priv = dev->dev_private;
4016 struct drm_i915_gem_phys_object *phys_obj;
4017 int ret;
4018
4019 if (dev_priv->mm.phys_objs[id - 1] || !size)
4020 return 0;
4021
Eric Anholt9a298b22009-03-24 12:23:04 -07004022 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004023 if (!phys_obj)
4024 return -ENOMEM;
4025
4026 phys_obj->id = id;
4027
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004028 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004029 if (!phys_obj->handle) {
4030 ret = -ENOMEM;
4031 goto kfree_obj;
4032 }
4033#ifdef CONFIG_X86
4034 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4035#endif
4036
4037 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4038
4039 return 0;
4040kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004041 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004042 return ret;
4043}
4044
Chris Wilson995b6762010-08-20 13:23:26 +01004045static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004046{
4047 drm_i915_private_t *dev_priv = dev->dev_private;
4048 struct drm_i915_gem_phys_object *phys_obj;
4049
4050 if (!dev_priv->mm.phys_objs[id - 1])
4051 return;
4052
4053 phys_obj = dev_priv->mm.phys_objs[id - 1];
4054 if (phys_obj->cur_obj) {
4055 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4056 }
4057
4058#ifdef CONFIG_X86
4059 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4060#endif
4061 drm_pci_free(dev, phys_obj->handle);
4062 kfree(phys_obj);
4063 dev_priv->mm.phys_objs[id - 1] = NULL;
4064}
4065
4066void i915_gem_free_all_phys_object(struct drm_device *dev)
4067{
4068 int i;
4069
Dave Airlie260883c2009-01-22 17:58:49 +10004070 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004071 i915_gem_free_phys_object(dev, i);
4072}
4073
4074void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004075 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004076{
Chris Wilson05394f32010-11-08 19:18:58 +00004077 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004078 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004079 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004080 int page_count;
4081
Chris Wilson05394f32010-11-08 19:18:58 +00004082 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004083 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004084 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004085
Chris Wilson05394f32010-11-08 19:18:58 +00004086 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004087 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07004088 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004089 if (!IS_ERR(page)) {
4090 char *dst = kmap_atomic(page);
4091 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4092 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004093
Chris Wilsone5281cc2010-10-28 13:45:36 +01004094 drm_clflush_pages(&page, 1);
4095
4096 set_page_dirty(page);
4097 mark_page_accessed(page);
4098 page_cache_release(page);
4099 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004100 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004101 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004102
Chris Wilson05394f32010-11-08 19:18:58 +00004103 obj->phys_obj->cur_obj = NULL;
4104 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004105}
4106
4107int
4108i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004109 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004110 int id,
4111 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004112{
Chris Wilson05394f32010-11-08 19:18:58 +00004113 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004114 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004115 int ret = 0;
4116 int page_count;
4117 int i;
4118
4119 if (id > I915_MAX_PHYS_OBJECT)
4120 return -EINVAL;
4121
Chris Wilson05394f32010-11-08 19:18:58 +00004122 if (obj->phys_obj) {
4123 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004124 return 0;
4125 i915_gem_detach_phys_object(dev, obj);
4126 }
4127
Dave Airlie71acb5e2008-12-30 20:31:46 +10004128 /* create a new object */
4129 if (!dev_priv->mm.phys_objs[id - 1]) {
4130 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004131 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004132 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004133 DRM_ERROR("failed to init phys object %d size: %zu\n",
4134 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004135 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004136 }
4137 }
4138
4139 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004140 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4141 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004142
Chris Wilson05394f32010-11-08 19:18:58 +00004143 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004144
4145 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004146 struct page *page;
4147 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004148
Hugh Dickins5949eac2011-06-27 16:18:18 -07004149 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004150 if (IS_ERR(page))
4151 return PTR_ERR(page);
4152
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004153 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004154 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004155 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004156 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004157
4158 mark_page_accessed(page);
4159 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004160 }
4161
4162 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004163}
4164
4165static int
Chris Wilson05394f32010-11-08 19:18:58 +00004166i915_gem_phys_pwrite(struct drm_device *dev,
4167 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004168 struct drm_i915_gem_pwrite *args,
4169 struct drm_file *file_priv)
4170{
Chris Wilson05394f32010-11-08 19:18:58 +00004171 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004172 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004173
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004174 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4175 unsigned long unwritten;
4176
4177 /* The physical object once assigned is fixed for the lifetime
4178 * of the obj, so we can safely drop the lock and continue
4179 * to access vaddr.
4180 */
4181 mutex_unlock(&dev->struct_mutex);
4182 unwritten = copy_from_user(vaddr, user_data, args->size);
4183 mutex_lock(&dev->struct_mutex);
4184 if (unwritten)
4185 return -EFAULT;
4186 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004187
Daniel Vetter40ce6572010-11-05 18:12:18 +01004188 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004189 return 0;
4190}
Eric Anholtb9624422009-06-03 07:27:35 +00004191
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004192void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004193{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004194 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004195
4196 /* Clean up our request list when the client is going away, so that
4197 * later retire_requests won't dereference our soon-to-be-gone
4198 * file_priv.
4199 */
Chris Wilson1c255952010-09-26 11:03:27 +01004200 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004201 while (!list_empty(&file_priv->mm.request_list)) {
4202 struct drm_i915_gem_request *request;
4203
4204 request = list_first_entry(&file_priv->mm.request_list,
4205 struct drm_i915_gem_request,
4206 client_list);
4207 list_del(&request->client_list);
4208 request->file_priv = NULL;
4209 }
Chris Wilson1c255952010-09-26 11:03:27 +01004210 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004211}
Chris Wilson31169712009-09-14 16:50:28 +01004212
Chris Wilson31169712009-09-14 16:50:28 +01004213static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004214i915_gpu_is_active(struct drm_device *dev)
4215{
4216 drm_i915_private_t *dev_priv = dev->dev_private;
4217 int lists_empty;
4218
Chris Wilson1637ef42010-04-20 17:10:35 +01004219 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004220 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004221
4222 return !lists_empty;
4223}
4224
4225static int
Ying Han1495f232011-05-24 17:12:27 -07004226i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004227{
Chris Wilson17250b72010-10-28 12:51:39 +01004228 struct drm_i915_private *dev_priv =
4229 container_of(shrinker,
4230 struct drm_i915_private,
4231 mm.inactive_shrinker);
4232 struct drm_device *dev = dev_priv->dev;
4233 struct drm_i915_gem_object *obj, *next;
Ying Han1495f232011-05-24 17:12:27 -07004234 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01004235 int cnt;
4236
4237 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004238 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004239
4240 /* "fast-path" to count number of available objects */
4241 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01004242 cnt = 0;
4243 list_for_each_entry(obj,
4244 &dev_priv->mm.inactive_list,
4245 mm_list)
4246 cnt++;
4247 mutex_unlock(&dev->struct_mutex);
4248 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004249 }
4250
Chris Wilson1637ef42010-04-20 17:10:35 +01004251rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004252 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01004253 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01004254
Chris Wilson17250b72010-10-28 12:51:39 +01004255 list_for_each_entry_safe(obj, next,
4256 &dev_priv->mm.inactive_list,
4257 mm_list) {
4258 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00004259 if (i915_gem_object_unbind(obj) == 0 &&
4260 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004261 break;
Chris Wilson31169712009-09-14 16:50:28 +01004262 }
Chris Wilson31169712009-09-14 16:50:28 +01004263 }
4264
4265 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01004266 cnt = 0;
4267 list_for_each_entry_safe(obj, next,
4268 &dev_priv->mm.inactive_list,
4269 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00004270 if (nr_to_scan &&
4271 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004272 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00004273 else
Chris Wilson17250b72010-10-28 12:51:39 +01004274 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01004275 }
4276
Chris Wilson17250b72010-10-28 12:51:39 +01004277 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01004278 /*
4279 * We are desperate for pages, so as a last resort, wait
4280 * for the GPU to finish and discard whatever we can.
4281 * This has a dramatic impact to reduce the number of
4282 * OOM-killer events whilst running the GPU aggressively.
4283 */
Chris Wilson17250b72010-10-28 12:51:39 +01004284 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01004285 goto rescan;
4286 }
Chris Wilson17250b72010-10-28 12:51:39 +01004287 mutex_unlock(&dev->struct_mutex);
4288 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004289}