blob: 7bb32ecc13c5fa60b0f8d3213ee945a6b302badf [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Hugh Dickins5949eac2011-06-27 16:18:18 -070034#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070036#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Chris Wilson88241782011-01-07 17:09:48 +000039static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson05394f32010-11-08 19:18:58 +000040static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
41static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000042static __must_check int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
43 bool write);
44static __must_check int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
45 uint64_t offset,
46 uint64_t size);
Chris Wilson05394f32010-11-08 19:18:58 +000047static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000048static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
49 unsigned alignment,
50 bool map_and_fenceable);
Chris Wilsond9e86c02010-11-10 16:40:20 +000051static void i915_gem_clear_fence_reg(struct drm_device *dev,
52 struct drm_i915_fence_reg *reg);
Chris Wilson05394f32010-11-08 19:18:58 +000053static int i915_gem_phys_pwrite(struct drm_device *dev,
54 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100055 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000056 struct drm_file *file);
57static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070058
Chris Wilson17250b72010-10-28 12:51:39 +010059static int i915_gem_inactive_shrink(struct shrinker *shrinker,
Ying Han1495f232011-05-24 17:12:27 -070060 struct shrink_control *sc);
Chris Wilson31169712009-09-14 16:50:28 +010061
Chris Wilson73aa8082010-09-30 11:46:12 +010062/* some bookkeeping */
63static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
64 size_t size)
65{
66 dev_priv->mm.object_count++;
67 dev_priv->mm.object_memory += size;
68}
69
70static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
71 size_t size)
72{
73 dev_priv->mm.object_count--;
74 dev_priv->mm.object_memory -= size;
75}
76
Chris Wilson21dd3732011-01-26 15:55:56 +000077static int
78i915_gem_wait_for_error(struct drm_device *dev)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010079{
80 struct drm_i915_private *dev_priv = dev->dev_private;
81 struct completion *x = &dev_priv->error_completion;
82 unsigned long flags;
83 int ret;
84
85 if (!atomic_read(&dev_priv->mm.wedged))
86 return 0;
87
88 ret = wait_for_completion_interruptible(x);
89 if (ret)
90 return ret;
91
Chris Wilson21dd3732011-01-26 15:55:56 +000092 if (atomic_read(&dev_priv->mm.wedged)) {
93 /* GPU is hung, bump the completion count to account for
94 * the token we just consumed so that we never hit zero and
95 * end up waiting upon a subsequent completion event that
96 * will never happen.
97 */
98 spin_lock_irqsave(&x->wait.lock, flags);
99 x->done++;
100 spin_unlock_irqrestore(&x->wait.lock, flags);
101 }
102 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100103}
104
Chris Wilson54cf91d2010-11-25 18:00:26 +0000105int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100106{
Chris Wilson76c1dec2010-09-25 11:22:51 +0100107 int ret;
108
Chris Wilson21dd3732011-01-26 15:55:56 +0000109 ret = i915_gem_wait_for_error(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100110 if (ret)
111 return ret;
112
113 ret = mutex_lock_interruptible(&dev->struct_mutex);
114 if (ret)
115 return ret;
116
Chris Wilson23bc5982010-09-29 16:10:57 +0100117 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100118 return 0;
119}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100120
Chris Wilson7d1c4802010-08-07 21:45:03 +0100121static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000122i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100123{
Chris Wilson05394f32010-11-08 19:18:58 +0000124 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100125}
126
Chris Wilson20217462010-11-23 15:26:33 +0000127void i915_gem_do_init(struct drm_device *dev,
128 unsigned long start,
129 unsigned long mappable_end,
130 unsigned long end)
Jesse Barnes79e53942008-11-07 14:24:08 -0800131{
132 drm_i915_private_t *dev_priv = dev->dev_private;
133
Chris Wilsonbee4a182011-01-21 10:54:32 +0000134 drm_mm_init(&dev_priv->mm.gtt_space, start, end - start);
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Chris Wilsonbee4a182011-01-21 10:54:32 +0000136 dev_priv->mm.gtt_start = start;
137 dev_priv->mm.gtt_mappable_end = mappable_end;
138 dev_priv->mm.gtt_end = end;
Chris Wilson73aa8082010-09-30 11:46:12 +0100139 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200140 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Chris Wilsonbee4a182011-01-21 10:54:32 +0000141
142 /* Take over this portion of the GTT */
143 intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
Jesse Barnes79e53942008-11-07 14:24:08 -0800144}
Keith Packard6dbe2772008-10-14 21:41:13 -0700145
Eric Anholt673a3942008-07-30 12:06:12 -0700146int
147i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000148 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700149{
Eric Anholt673a3942008-07-30 12:06:12 -0700150 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000151
152 if (args->gtt_start >= args->gtt_end ||
153 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
154 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700155
156 mutex_lock(&dev->struct_mutex);
Chris Wilson20217462010-11-23 15:26:33 +0000157 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700158 mutex_unlock(&dev->struct_mutex);
159
Chris Wilson20217462010-11-23 15:26:33 +0000160 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700161}
162
Eric Anholt5a125c32008-10-22 21:40:13 -0700163int
164i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000165 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700166{
Chris Wilson73aa8082010-09-30 11:46:12 +0100167 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700168 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000169 struct drm_i915_gem_object *obj;
170 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700171
172 if (!(dev->driver->driver_features & DRIVER_GEM))
173 return -ENODEV;
174
Chris Wilson6299f992010-11-24 12:23:44 +0000175 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100176 mutex_lock(&dev->struct_mutex);
Chris Wilson6299f992010-11-24 12:23:44 +0000177 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
178 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100179 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700180
Chris Wilson6299f992010-11-24 12:23:44 +0000181 args->aper_size = dev_priv->mm.gtt_total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400182 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000183
Eric Anholt5a125c32008-10-22 21:40:13 -0700184 return 0;
185}
186
Dave Airlieff72145b2011-02-07 12:16:14 +1000187static int
188i915_gem_create(struct drm_file *file,
189 struct drm_device *dev,
190 uint64_t size,
191 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700192{
Chris Wilson05394f32010-11-08 19:18:58 +0000193 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300194 int ret;
195 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700196
Dave Airlieff72145b2011-02-07 12:16:14 +1000197 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200198 if (size == 0)
199 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700200
201 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000202 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700203 if (obj == NULL)
204 return -ENOMEM;
205
Chris Wilson05394f32010-11-08 19:18:58 +0000206 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100207 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000208 drm_gem_object_release(&obj->base);
209 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100210 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700211 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100212 }
213
Chris Wilson202f2fe2010-10-14 13:20:40 +0100214 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000215 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100216 trace_i915_gem_object_create(obj);
217
Dave Airlieff72145b2011-02-07 12:16:14 +1000218 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700219 return 0;
220}
221
Dave Airlieff72145b2011-02-07 12:16:14 +1000222int
223i915_gem_dumb_create(struct drm_file *file,
224 struct drm_device *dev,
225 struct drm_mode_create_dumb *args)
226{
227 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000228 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000229 args->size = args->pitch * args->height;
230 return i915_gem_create(file, dev,
231 args->size, &args->handle);
232}
233
234int i915_gem_dumb_destroy(struct drm_file *file,
235 struct drm_device *dev,
236 uint32_t handle)
237{
238 return drm_gem_handle_delete(file, handle);
239}
240
241/**
242 * Creates a new mm object and returns a handle to it.
243 */
244int
245i915_gem_create_ioctl(struct drm_device *dev, void *data,
246 struct drm_file *file)
247{
248 struct drm_i915_gem_create *args = data;
249 return i915_gem_create(file, dev,
250 args->size, &args->handle);
251}
252
Chris Wilson05394f32010-11-08 19:18:58 +0000253static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700254{
Chris Wilson05394f32010-11-08 19:18:58 +0000255 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700256
257 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000258 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700259}
260
Chris Wilson99a03df2010-05-27 14:15:34 +0100261static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700262slow_shmem_copy(struct page *dst_page,
263 int dst_offset,
264 struct page *src_page,
265 int src_offset,
266 int length)
267{
268 char *dst_vaddr, *src_vaddr;
269
Chris Wilson99a03df2010-05-27 14:15:34 +0100270 dst_vaddr = kmap(dst_page);
271 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700272
273 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
274
Chris Wilson99a03df2010-05-27 14:15:34 +0100275 kunmap(src_page);
276 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700277}
278
Chris Wilson99a03df2010-05-27 14:15:34 +0100279static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700280slow_shmem_bit17_copy(struct page *gpu_page,
281 int gpu_offset,
282 struct page *cpu_page,
283 int cpu_offset,
284 int length,
285 int is_read)
286{
287 char *gpu_vaddr, *cpu_vaddr;
288
289 /* Use the unswizzled path if this page isn't affected. */
290 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
291 if (is_read)
292 return slow_shmem_copy(cpu_page, cpu_offset,
293 gpu_page, gpu_offset, length);
294 else
295 return slow_shmem_copy(gpu_page, gpu_offset,
296 cpu_page, cpu_offset, length);
297 }
298
Chris Wilson99a03df2010-05-27 14:15:34 +0100299 gpu_vaddr = kmap(gpu_page);
300 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700301
302 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
303 * XORing with the other bits (A9 for Y, A9 and A10 for X)
304 */
305 while (length > 0) {
306 int cacheline_end = ALIGN(gpu_offset + 1, 64);
307 int this_length = min(cacheline_end - gpu_offset, length);
308 int swizzled_gpu_offset = gpu_offset ^ 64;
309
310 if (is_read) {
311 memcpy(cpu_vaddr + cpu_offset,
312 gpu_vaddr + swizzled_gpu_offset,
313 this_length);
314 } else {
315 memcpy(gpu_vaddr + swizzled_gpu_offset,
316 cpu_vaddr + cpu_offset,
317 this_length);
318 }
319 cpu_offset += this_length;
320 gpu_offset += this_length;
321 length -= this_length;
322 }
323
Chris Wilson99a03df2010-05-27 14:15:34 +0100324 kunmap(cpu_page);
325 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700326}
327
Eric Anholt673a3942008-07-30 12:06:12 -0700328/**
Eric Anholteb014592009-03-10 11:44:52 -0700329 * This is the fast shmem pread path, which attempts to copy_from_user directly
330 * from the backing pages of the object to the user's address space. On a
331 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
332 */
333static int
Chris Wilson05394f32010-11-08 19:18:58 +0000334i915_gem_shmem_pread_fast(struct drm_device *dev,
335 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700336 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000337 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700338{
Chris Wilson05394f32010-11-08 19:18:58 +0000339 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700340 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100341 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700342 char __user *user_data;
343 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700344
345 user_data = (char __user *) (uintptr_t) args->data_ptr;
346 remain = args->size;
347
Eric Anholteb014592009-03-10 11:44:52 -0700348 offset = args->offset;
349
350 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100351 struct page *page;
352 char *vaddr;
353 int ret;
354
Eric Anholteb014592009-03-10 11:44:52 -0700355 /* Operation in this page
356 *
Eric Anholteb014592009-03-10 11:44:52 -0700357 * page_offset = offset within page
358 * page_length = bytes to copy for this page
359 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100360 page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700361 page_length = remain;
362 if ((page_offset + remain) > PAGE_SIZE)
363 page_length = PAGE_SIZE - page_offset;
364
Hugh Dickins5949eac2011-06-27 16:18:18 -0700365 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100366 if (IS_ERR(page))
367 return PTR_ERR(page);
368
369 vaddr = kmap_atomic(page);
370 ret = __copy_to_user_inatomic(user_data,
371 vaddr + page_offset,
372 page_length);
373 kunmap_atomic(vaddr);
374
375 mark_page_accessed(page);
376 page_cache_release(page);
377 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100378 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700379
380 remain -= page_length;
381 user_data += page_length;
382 offset += page_length;
383 }
384
Chris Wilson4f27b752010-10-14 15:26:45 +0100385 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700386}
387
388/**
389 * This is the fallback shmem pread path, which allocates temporary storage
390 * in kernel space to copy_to_user into outside of the struct_mutex, so we
391 * can copy out of the object's backing pages while holding the struct mutex
392 * and not take page faults.
393 */
394static int
Chris Wilson05394f32010-11-08 19:18:58 +0000395i915_gem_shmem_pread_slow(struct drm_device *dev,
396 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700397 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000398 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700399{
Chris Wilson05394f32010-11-08 19:18:58 +0000400 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700401 struct mm_struct *mm = current->mm;
402 struct page **user_pages;
403 ssize_t remain;
404 loff_t offset, pinned_pages, i;
405 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100406 int shmem_page_offset;
407 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700408 int page_length;
409 int ret;
410 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700411 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700412
413 remain = args->size;
414
415 /* Pin the user pages containing the data. We can't fault while
416 * holding the struct mutex, yet we want to hold it while
417 * dereferencing the user data.
418 */
419 first_data_page = data_ptr / PAGE_SIZE;
420 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
421 num_pages = last_data_page - first_data_page + 1;
422
Chris Wilson4f27b752010-10-14 15:26:45 +0100423 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700424 if (user_pages == NULL)
425 return -ENOMEM;
426
Chris Wilson4f27b752010-10-14 15:26:45 +0100427 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700428 down_read(&mm->mmap_sem);
429 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700430 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700431 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100432 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700433 if (pinned_pages < num_pages) {
434 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100435 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700436 }
437
Chris Wilson4f27b752010-10-14 15:26:45 +0100438 ret = i915_gem_object_set_cpu_read_domain_range(obj,
439 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700440 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100441 if (ret)
442 goto out;
443
444 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700445
Eric Anholteb014592009-03-10 11:44:52 -0700446 offset = args->offset;
447
448 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100449 struct page *page;
450
Eric Anholteb014592009-03-10 11:44:52 -0700451 /* Operation in this page
452 *
Eric Anholteb014592009-03-10 11:44:52 -0700453 * shmem_page_offset = offset within page in shmem file
454 * data_page_index = page number in get_user_pages return
455 * data_page_offset = offset with data_page_index page.
456 * page_length = bytes to copy for this page
457 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100458 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700459 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100460 data_page_offset = offset_in_page(data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700461
462 page_length = remain;
463 if ((shmem_page_offset + page_length) > PAGE_SIZE)
464 page_length = PAGE_SIZE - shmem_page_offset;
465 if ((data_page_offset + page_length) > PAGE_SIZE)
466 page_length = PAGE_SIZE - data_page_offset;
467
Hugh Dickins5949eac2011-06-27 16:18:18 -0700468 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Jesper Juhlb65552f2011-06-12 20:53:44 +0000469 if (IS_ERR(page)) {
470 ret = PTR_ERR(page);
471 goto out;
472 }
Chris Wilsone5281cc2010-10-28 13:45:36 +0100473
Eric Anholt280b7132009-03-12 16:56:27 -0700474 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100475 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700476 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100477 user_pages[data_page_index],
478 data_page_offset,
479 page_length,
480 1);
481 } else {
482 slow_shmem_copy(user_pages[data_page_index],
483 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100484 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100485 shmem_page_offset,
486 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700487 }
Eric Anholteb014592009-03-10 11:44:52 -0700488
Chris Wilsone5281cc2010-10-28 13:45:36 +0100489 mark_page_accessed(page);
490 page_cache_release(page);
491
Eric Anholteb014592009-03-10 11:44:52 -0700492 remain -= page_length;
493 data_ptr += page_length;
494 offset += page_length;
495 }
496
Chris Wilson4f27b752010-10-14 15:26:45 +0100497out:
Eric Anholteb014592009-03-10 11:44:52 -0700498 for (i = 0; i < pinned_pages; i++) {
499 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100500 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700501 page_cache_release(user_pages[i]);
502 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700503 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700504
505 return ret;
506}
507
Eric Anholt673a3942008-07-30 12:06:12 -0700508/**
509 * Reads data from the object referenced by handle.
510 *
511 * On error, the contents of *data are undefined.
512 */
513int
514i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000515 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700516{
517 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000518 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100519 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700520
Chris Wilson51311d02010-11-17 09:10:42 +0000521 if (args->size == 0)
522 return 0;
523
524 if (!access_ok(VERIFY_WRITE,
525 (char __user *)(uintptr_t)args->data_ptr,
526 args->size))
527 return -EFAULT;
528
529 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
530 args->size);
531 if (ret)
532 return -EFAULT;
533
Chris Wilson4f27b752010-10-14 15:26:45 +0100534 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100535 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100536 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700537
Chris Wilson05394f32010-11-08 19:18:58 +0000538 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000539 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100540 ret = -ENOENT;
541 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100542 }
Eric Anholt673a3942008-07-30 12:06:12 -0700543
Chris Wilson7dcd2492010-09-26 20:21:44 +0100544 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000545 if (args->offset > obj->base.size ||
546 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100547 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100548 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100549 }
550
Chris Wilsondb53a302011-02-03 11:57:46 +0000551 trace_i915_gem_object_pread(obj, args->offset, args->size);
552
Chris Wilson4f27b752010-10-14 15:26:45 +0100553 ret = i915_gem_object_set_cpu_read_domain_range(obj,
554 args->offset,
555 args->size);
556 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100557 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100558
559 ret = -EFAULT;
560 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000561 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100562 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000563 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700564
Chris Wilson35b62a82010-09-26 20:23:38 +0100565out:
Chris Wilson05394f32010-11-08 19:18:58 +0000566 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100567unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100568 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700569 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700570}
571
Keith Packard0839ccb2008-10-30 19:38:48 -0700572/* This is the fast write path which cannot handle
573 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700574 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700575
Keith Packard0839ccb2008-10-30 19:38:48 -0700576static inline int
577fast_user_write(struct io_mapping *mapping,
578 loff_t page_base, int page_offset,
579 char __user *user_data,
580 int length)
581{
582 char *vaddr_atomic;
583 unsigned long unwritten;
584
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700585 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700586 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
587 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700588 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100589 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700590}
591
592/* Here's the write path which can sleep for
593 * page faults
594 */
595
Chris Wilsonab34c222010-05-27 14:15:35 +0100596static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700597slow_kernel_write(struct io_mapping *mapping,
598 loff_t gtt_base, int gtt_offset,
599 struct page *user_page, int user_offset,
600 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700601{
Chris Wilsonab34c222010-05-27 14:15:35 +0100602 char __iomem *dst_vaddr;
603 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700604
Chris Wilsonab34c222010-05-27 14:15:35 +0100605 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
606 src_vaddr = kmap(user_page);
607
608 memcpy_toio(dst_vaddr + gtt_offset,
609 src_vaddr + user_offset,
610 length);
611
612 kunmap(user_page);
613 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700614}
615
Eric Anholt3de09aa2009-03-09 09:42:23 -0700616/**
617 * This is the fast pwrite path, where we copy the data directly from the
618 * user into the GTT, uncached.
619 */
Eric Anholt673a3942008-07-30 12:06:12 -0700620static int
Chris Wilson05394f32010-11-08 19:18:58 +0000621i915_gem_gtt_pwrite_fast(struct drm_device *dev,
622 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700623 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000624 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700625{
Keith Packard0839ccb2008-10-30 19:38:48 -0700626 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700627 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700628 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700629 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700630 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700631
632 user_data = (char __user *) (uintptr_t) args->data_ptr;
633 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700634
Chris Wilson05394f32010-11-08 19:18:58 +0000635 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700636
637 while (remain > 0) {
638 /* Operation in this page
639 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700640 * page_base = page offset within aperture
641 * page_offset = offset within page
642 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700643 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100644 page_base = offset & PAGE_MASK;
645 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700646 page_length = remain;
647 if ((page_offset + remain) > PAGE_SIZE)
648 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700649
Keith Packard0839ccb2008-10-30 19:38:48 -0700650 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700651 * source page isn't available. Return the error and we'll
652 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700653 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100654 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
655 page_offset, user_data, page_length))
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100656 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700657
Keith Packard0839ccb2008-10-30 19:38:48 -0700658 remain -= page_length;
659 user_data += page_length;
660 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700661 }
Eric Anholt673a3942008-07-30 12:06:12 -0700662
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100663 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700664}
665
Eric Anholt3de09aa2009-03-09 09:42:23 -0700666/**
667 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
668 * the memory and maps it using kmap_atomic for copying.
669 *
670 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
671 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
672 */
Eric Anholt3043c602008-10-02 12:24:47 -0700673static int
Chris Wilson05394f32010-11-08 19:18:58 +0000674i915_gem_gtt_pwrite_slow(struct drm_device *dev,
675 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700676 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000677 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700678{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700679 drm_i915_private_t *dev_priv = dev->dev_private;
680 ssize_t remain;
681 loff_t gtt_page_base, offset;
682 loff_t first_data_page, last_data_page, num_pages;
683 loff_t pinned_pages, i;
684 struct page **user_pages;
685 struct mm_struct *mm = current->mm;
686 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700687 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700688 uint64_t data_ptr = args->data_ptr;
689
690 remain = args->size;
691
692 /* Pin the user pages containing the data. We can't fault while
693 * holding the struct mutex, and all of the pwrite implementations
694 * want to hold it while dereferencing the user data.
695 */
696 first_data_page = data_ptr / PAGE_SIZE;
697 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
698 num_pages = last_data_page - first_data_page + 1;
699
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100700 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700701 if (user_pages == NULL)
702 return -ENOMEM;
703
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100704 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700705 down_read(&mm->mmap_sem);
706 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
707 num_pages, 0, 0, user_pages, NULL);
708 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100709 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700710 if (pinned_pages < num_pages) {
711 ret = -EFAULT;
712 goto out_unpin_pages;
713 }
714
Chris Wilsond9e86c02010-11-10 16:40:20 +0000715 ret = i915_gem_object_set_to_gtt_domain(obj, true);
716 if (ret)
717 goto out_unpin_pages;
718
719 ret = i915_gem_object_put_fence(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700720 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100721 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700722
Chris Wilson05394f32010-11-08 19:18:58 +0000723 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700724
725 while (remain > 0) {
726 /* Operation in this page
727 *
728 * gtt_page_base = page offset within aperture
729 * gtt_page_offset = offset within page in aperture
730 * data_page_index = page number in get_user_pages return
731 * data_page_offset = offset with data_page_index page.
732 * page_length = bytes to copy for this page
733 */
734 gtt_page_base = offset & PAGE_MASK;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100735 gtt_page_offset = offset_in_page(offset);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700736 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100737 data_page_offset = offset_in_page(data_ptr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700738
739 page_length = remain;
740 if ((gtt_page_offset + page_length) > PAGE_SIZE)
741 page_length = PAGE_SIZE - gtt_page_offset;
742 if ((data_page_offset + page_length) > PAGE_SIZE)
743 page_length = PAGE_SIZE - data_page_offset;
744
Chris Wilsonab34c222010-05-27 14:15:35 +0100745 slow_kernel_write(dev_priv->mm.gtt_mapping,
746 gtt_page_base, gtt_page_offset,
747 user_pages[data_page_index],
748 data_page_offset,
749 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700750
751 remain -= page_length;
752 offset += page_length;
753 data_ptr += page_length;
754 }
755
Eric Anholt3de09aa2009-03-09 09:42:23 -0700756out_unpin_pages:
757 for (i = 0; i < pinned_pages; i++)
758 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700759 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700760
761 return ret;
762}
763
Eric Anholt40123c12009-03-09 13:42:30 -0700764/**
765 * This is the fast shmem pwrite path, which attempts to directly
766 * copy_from_user into the kmapped pages backing the object.
767 */
Eric Anholt673a3942008-07-30 12:06:12 -0700768static int
Chris Wilson05394f32010-11-08 19:18:58 +0000769i915_gem_shmem_pwrite_fast(struct drm_device *dev,
770 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700771 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000772 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700773{
Chris Wilson05394f32010-11-08 19:18:58 +0000774 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700775 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100776 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700777 char __user *user_data;
778 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700779
780 user_data = (char __user *) (uintptr_t) args->data_ptr;
781 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700782
Eric Anholt673a3942008-07-30 12:06:12 -0700783 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000784 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700785
Eric Anholt40123c12009-03-09 13:42:30 -0700786 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100787 struct page *page;
788 char *vaddr;
789 int ret;
790
Eric Anholt40123c12009-03-09 13:42:30 -0700791 /* Operation in this page
792 *
Eric Anholt40123c12009-03-09 13:42:30 -0700793 * page_offset = offset within page
794 * page_length = bytes to copy for this page
795 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100796 page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700797 page_length = remain;
798 if ((page_offset + remain) > PAGE_SIZE)
799 page_length = PAGE_SIZE - page_offset;
800
Hugh Dickins5949eac2011-06-27 16:18:18 -0700801 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100802 if (IS_ERR(page))
803 return PTR_ERR(page);
804
Daniel Vetter130c2562011-09-17 20:55:46 +0200805 vaddr = kmap_atomic(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100806 ret = __copy_from_user_inatomic(vaddr + page_offset,
807 user_data,
808 page_length);
Daniel Vetter130c2562011-09-17 20:55:46 +0200809 kunmap_atomic(vaddr);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100810
811 set_page_dirty(page);
812 mark_page_accessed(page);
813 page_cache_release(page);
814
815 /* If we get a fault while copying data, then (presumably) our
816 * source page isn't available. Return the error and we'll
817 * retry in the slow path.
818 */
819 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100820 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700821
822 remain -= page_length;
823 user_data += page_length;
824 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700825 }
826
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100827 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700828}
829
830/**
831 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
832 * the memory and maps it using kmap_atomic for copying.
833 *
834 * This avoids taking mmap_sem for faulting on the user's address while the
835 * struct_mutex is held.
836 */
837static int
Chris Wilson05394f32010-11-08 19:18:58 +0000838i915_gem_shmem_pwrite_slow(struct drm_device *dev,
839 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700840 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000841 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700842{
Chris Wilson05394f32010-11-08 19:18:58 +0000843 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700844 struct mm_struct *mm = current->mm;
845 struct page **user_pages;
846 ssize_t remain;
847 loff_t offset, pinned_pages, i;
848 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100849 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700850 int data_page_index, data_page_offset;
851 int page_length;
852 int ret;
853 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700854 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700855
856 remain = args->size;
857
858 /* Pin the user pages containing the data. We can't fault while
859 * holding the struct mutex, and all of the pwrite implementations
860 * want to hold it while dereferencing the user data.
861 */
862 first_data_page = data_ptr / PAGE_SIZE;
863 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
864 num_pages = last_data_page - first_data_page + 1;
865
Chris Wilson4f27b752010-10-14 15:26:45 +0100866 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700867 if (user_pages == NULL)
868 return -ENOMEM;
869
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100870 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700871 down_read(&mm->mmap_sem);
872 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
873 num_pages, 0, 0, user_pages, NULL);
874 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100875 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700876 if (pinned_pages < num_pages) {
877 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100878 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700879 }
880
Eric Anholt40123c12009-03-09 13:42:30 -0700881 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100882 if (ret)
883 goto out;
884
885 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700886
Eric Anholt40123c12009-03-09 13:42:30 -0700887 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000888 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700889
890 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100891 struct page *page;
892
Eric Anholt40123c12009-03-09 13:42:30 -0700893 /* Operation in this page
894 *
Eric Anholt40123c12009-03-09 13:42:30 -0700895 * shmem_page_offset = offset within page in shmem file
896 * data_page_index = page number in get_user_pages return
897 * data_page_offset = offset with data_page_index page.
898 * page_length = bytes to copy for this page
899 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100900 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700901 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100902 data_page_offset = offset_in_page(data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700903
904 page_length = remain;
905 if ((shmem_page_offset + page_length) > PAGE_SIZE)
906 page_length = PAGE_SIZE - shmem_page_offset;
907 if ((data_page_offset + page_length) > PAGE_SIZE)
908 page_length = PAGE_SIZE - data_page_offset;
909
Hugh Dickins5949eac2011-06-27 16:18:18 -0700910 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100911 if (IS_ERR(page)) {
912 ret = PTR_ERR(page);
913 goto out;
914 }
915
Eric Anholt280b7132009-03-12 16:56:27 -0700916 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100917 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700918 shmem_page_offset,
919 user_pages[data_page_index],
920 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100921 page_length,
922 0);
923 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100924 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100925 shmem_page_offset,
926 user_pages[data_page_index],
927 data_page_offset,
928 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700929 }
Eric Anholt40123c12009-03-09 13:42:30 -0700930
Chris Wilsone5281cc2010-10-28 13:45:36 +0100931 set_page_dirty(page);
932 mark_page_accessed(page);
933 page_cache_release(page);
934
Eric Anholt40123c12009-03-09 13:42:30 -0700935 remain -= page_length;
936 data_ptr += page_length;
937 offset += page_length;
938 }
939
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100940out:
Eric Anholt40123c12009-03-09 13:42:30 -0700941 for (i = 0; i < pinned_pages; i++)
942 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700943 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700944
945 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700946}
947
948/**
949 * Writes data to the object referenced by handle.
950 *
951 * On error, the contents of the buffer that were to be modified are undefined.
952 */
953int
954i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100955 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700956{
957 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000958 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000959 int ret;
960
961 if (args->size == 0)
962 return 0;
963
964 if (!access_ok(VERIFY_READ,
965 (char __user *)(uintptr_t)args->data_ptr,
966 args->size))
967 return -EFAULT;
968
969 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
970 args->size);
971 if (ret)
972 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700973
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100974 ret = i915_mutex_lock_interruptible(dev);
975 if (ret)
976 return ret;
977
Chris Wilson05394f32010-11-08 19:18:58 +0000978 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000979 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100980 ret = -ENOENT;
981 goto unlock;
982 }
Eric Anholt673a3942008-07-30 12:06:12 -0700983
Chris Wilson7dcd2492010-09-26 20:21:44 +0100984 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000985 if (args->offset > obj->base.size ||
986 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100987 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100988 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100989 }
990
Chris Wilsondb53a302011-02-03 11:57:46 +0000991 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
992
Eric Anholt673a3942008-07-30 12:06:12 -0700993 /* We can only do the GTT pwrite on untiled buffers, as otherwise
994 * it would end up going through the fenced access, and we'll get
995 * different detiling behavior between reading and writing.
996 * pread/pwrite currently are reading and writing from the CPU
997 * perspective, requiring manual detiling by the client.
998 */
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100999 if (obj->phys_obj) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001000 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001001 goto out;
1002 }
1003
1004 if (obj->gtt_space &&
1005 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001006 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001007 if (ret)
1008 goto out;
1009
Chris Wilsond9e86c02010-11-10 16:40:20 +00001010 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1011 if (ret)
1012 goto out_unpin;
1013
1014 ret = i915_gem_object_put_fence(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001015 if (ret)
1016 goto out_unpin;
1017
1018 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1019 if (ret == -EFAULT)
1020 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1021
1022out_unpin:
1023 i915_gem_object_unpin(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001024
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001025 if (ret != -EFAULT)
1026 goto out;
1027 /* Fall through to the shmfs paths because the gtt paths might
1028 * fail with non-page-backed user pointers (e.g. gtt mappings
1029 * when moving data between textures). */
Eric Anholt40123c12009-03-09 13:42:30 -07001030 }
Eric Anholt673a3942008-07-30 12:06:12 -07001031
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001032 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1033 if (ret)
1034 goto out;
1035
1036 ret = -EFAULT;
1037 if (!i915_gem_object_needs_bit17_swizzle(obj))
1038 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1039 if (ret == -EFAULT)
1040 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
1041
Chris Wilson35b62a82010-09-26 20:23:38 +01001042out:
Chris Wilson05394f32010-11-08 19:18:58 +00001043 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001044unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001045 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001046 return ret;
1047}
1048
1049/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001050 * Called when user space prepares to use an object with the CPU, either
1051 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001052 */
1053int
1054i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001055 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001056{
1057 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001058 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001059 uint32_t read_domains = args->read_domains;
1060 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001061 int ret;
1062
1063 if (!(dev->driver->driver_features & DRIVER_GEM))
1064 return -ENODEV;
1065
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001066 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001067 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001068 return -EINVAL;
1069
Chris Wilson21d509e2009-06-06 09:46:02 +01001070 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001071 return -EINVAL;
1072
1073 /* Having something in the write domain implies it's in the read
1074 * domain, and only that read domain. Enforce that in the request.
1075 */
1076 if (write_domain != 0 && read_domains != write_domain)
1077 return -EINVAL;
1078
Chris Wilson76c1dec2010-09-25 11:22:51 +01001079 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001080 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001081 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001082
Chris Wilson05394f32010-11-08 19:18:58 +00001083 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001084 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001085 ret = -ENOENT;
1086 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001087 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001088
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001089 if (read_domains & I915_GEM_DOMAIN_GTT) {
1090 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001091
1092 /* Silently promote "you're not bound, there was nothing to do"
1093 * to success, since the client was just asking us to
1094 * make sure everything was done.
1095 */
1096 if (ret == -EINVAL)
1097 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001098 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001099 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001100 }
1101
Chris Wilson05394f32010-11-08 19:18:58 +00001102 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001103unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001104 mutex_unlock(&dev->struct_mutex);
1105 return ret;
1106}
1107
1108/**
1109 * Called when user space has done writes to this buffer
1110 */
1111int
1112i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001113 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001114{
1115 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001116 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001117 int ret = 0;
1118
1119 if (!(dev->driver->driver_features & DRIVER_GEM))
1120 return -ENODEV;
1121
Chris Wilson76c1dec2010-09-25 11:22:51 +01001122 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001123 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001124 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001125
Chris Wilson05394f32010-11-08 19:18:58 +00001126 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001127 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001128 ret = -ENOENT;
1129 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001130 }
1131
Eric Anholt673a3942008-07-30 12:06:12 -07001132 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001133 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001134 i915_gem_object_flush_cpu_write_domain(obj);
1135
Chris Wilson05394f32010-11-08 19:18:58 +00001136 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001137unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001138 mutex_unlock(&dev->struct_mutex);
1139 return ret;
1140}
1141
1142/**
1143 * Maps the contents of an object, returning the address it is mapped
1144 * into.
1145 *
1146 * While the mapping holds a reference on the contents of the object, it doesn't
1147 * imply a ref on the object itself.
1148 */
1149int
1150i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001151 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001152{
1153 struct drm_i915_gem_mmap *args = data;
1154 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001155 unsigned long addr;
1156
1157 if (!(dev->driver->driver_features & DRIVER_GEM))
1158 return -ENODEV;
1159
Chris Wilson05394f32010-11-08 19:18:58 +00001160 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001161 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001162 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001163
Eric Anholt673a3942008-07-30 12:06:12 -07001164 down_write(&current->mm->mmap_sem);
1165 addr = do_mmap(obj->filp, 0, args->size,
1166 PROT_READ | PROT_WRITE, MAP_SHARED,
1167 args->offset);
1168 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001169 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001170 if (IS_ERR((void *)addr))
1171 return addr;
1172
1173 args->addr_ptr = (uint64_t) addr;
1174
1175 return 0;
1176}
1177
Jesse Barnesde151cf2008-11-12 10:03:55 -08001178/**
1179 * i915_gem_fault - fault a page into the GTT
1180 * vma: VMA in question
1181 * vmf: fault info
1182 *
1183 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1184 * from userspace. The fault handler takes care of binding the object to
1185 * the GTT (if needed), allocating and programming a fence register (again,
1186 * only if needed based on whether the old reg is still valid or the object
1187 * is tiled) and inserting a new PTE into the faulting process.
1188 *
1189 * Note that the faulting process may involve evicting existing objects
1190 * from the GTT and/or fence registers to make room. So performance may
1191 * suffer if the GTT working set is large or there are few fence registers
1192 * left.
1193 */
1194int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1195{
Chris Wilson05394f32010-11-08 19:18:58 +00001196 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1197 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001198 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001199 pgoff_t page_offset;
1200 unsigned long pfn;
1201 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001202 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001203
1204 /* We don't use vmf->pgoff since that has the fake offset */
1205 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1206 PAGE_SHIFT;
1207
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001208 ret = i915_mutex_lock_interruptible(dev);
1209 if (ret)
1210 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001211
Chris Wilsondb53a302011-02-03 11:57:46 +00001212 trace_i915_gem_object_fault(obj, page_offset, true, write);
1213
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001214 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001215 if (!obj->map_and_fenceable) {
1216 ret = i915_gem_object_unbind(obj);
1217 if (ret)
1218 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001219 }
Chris Wilson05394f32010-11-08 19:18:58 +00001220 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001221 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001222 if (ret)
1223 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001224
Eric Anholte92d03b2011-06-14 16:43:09 -07001225 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1226 if (ret)
1227 goto unlock;
1228 }
Chris Wilson4a684a42010-10-28 14:44:08 +01001229
Chris Wilsond9e86c02010-11-10 16:40:20 +00001230 if (obj->tiling_mode == I915_TILING_NONE)
1231 ret = i915_gem_object_put_fence(obj);
1232 else
Chris Wilsonce453d82011-02-21 14:43:56 +00001233 ret = i915_gem_object_get_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001234 if (ret)
1235 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001236
Chris Wilson05394f32010-11-08 19:18:58 +00001237 if (i915_gem_object_is_inactive(obj))
1238 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001239
Chris Wilson6299f992010-11-24 12:23:44 +00001240 obj->fault_mappable = true;
1241
Chris Wilson05394f32010-11-08 19:18:58 +00001242 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001243 page_offset;
1244
1245 /* Finally, remap it using the new GTT offset */
1246 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001247unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001248 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001249out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001250 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001251 case -EIO:
Chris Wilson045e7692010-11-07 09:18:22 +00001252 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001253 /* Give the error handler a chance to run and move the
1254 * objects off the GPU active list. Next time we service the
1255 * fault, we should be able to transition the page into the
1256 * GTT without touching the GPU (and so avoid further
1257 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1258 * with coherency, just lost writes.
1259 */
Chris Wilson045e7692010-11-07 09:18:22 +00001260 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001261 case 0:
1262 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001263 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001264 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001265 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001266 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001267 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001268 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001269 }
1270}
1271
1272/**
Chris Wilson901782b2009-07-10 08:18:50 +01001273 * i915_gem_release_mmap - remove physical page mappings
1274 * @obj: obj in question
1275 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001276 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001277 * relinquish ownership of the pages back to the system.
1278 *
1279 * It is vital that we remove the page mapping if we have mapped a tiled
1280 * object through the GTT and then lose the fence register due to
1281 * resource pressure. Similarly if the object has been moved out of the
1282 * aperture, than pages mapped into userspace must be revoked. Removing the
1283 * mapping will then trigger a page fault on the next user access, allowing
1284 * fixup by i915_gem_fault().
1285 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001286void
Chris Wilson05394f32010-11-08 19:18:58 +00001287i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001288{
Chris Wilson6299f992010-11-24 12:23:44 +00001289 if (!obj->fault_mappable)
1290 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001291
Chris Wilsonf6e47882011-03-20 21:09:12 +00001292 if (obj->base.dev->dev_mapping)
1293 unmap_mapping_range(obj->base.dev->dev_mapping,
1294 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1295 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001296
Chris Wilson6299f992010-11-24 12:23:44 +00001297 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001298}
1299
Chris Wilson92b88ae2010-11-09 11:47:32 +00001300static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001301i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001302{
Chris Wilsone28f8712011-07-18 13:11:49 -07001303 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001304
1305 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001306 tiling_mode == I915_TILING_NONE)
1307 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001308
1309 /* Previous chips need a power-of-two fence region when tiling */
1310 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001311 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001312 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001313 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001314
Chris Wilsone28f8712011-07-18 13:11:49 -07001315 while (gtt_size < size)
1316 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001317
Chris Wilsone28f8712011-07-18 13:11:49 -07001318 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001319}
1320
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321/**
1322 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1323 * @obj: object to check
1324 *
1325 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001326 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001327 */
1328static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001329i915_gem_get_gtt_alignment(struct drm_device *dev,
1330 uint32_t size,
1331 int tiling_mode)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001332{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001333 /*
1334 * Minimum alignment is 4k (GTT page size), but might be greater
1335 * if a fence register is needed for the object.
1336 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001337 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001338 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001339 return 4096;
1340
1341 /*
1342 * Previous chips need to be aligned to the size of the smallest
1343 * fence register that can contain the object.
1344 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001345 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001346}
1347
Daniel Vetter5e783302010-11-14 22:32:36 +01001348/**
1349 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1350 * unfenced object
Chris Wilsone28f8712011-07-18 13:11:49 -07001351 * @dev: the device
1352 * @size: size of the object
1353 * @tiling_mode: tiling mode of the object
Daniel Vetter5e783302010-11-14 22:32:36 +01001354 *
1355 * Return the required GTT alignment for an object, only taking into account
1356 * unfenced tiled surface requirements.
1357 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001358uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001359i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1360 uint32_t size,
1361 int tiling_mode)
Daniel Vetter5e783302010-11-14 22:32:36 +01001362{
Daniel Vetter5e783302010-11-14 22:32:36 +01001363 /*
1364 * Minimum alignment is 4k (GTT page size) for sane hw.
1365 */
1366 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001367 tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001368 return 4096;
1369
Chris Wilsone28f8712011-07-18 13:11:49 -07001370 /* Previous hardware however needs to be aligned to a power-of-two
1371 * tile height. The simplest method for determining this is to reuse
1372 * the power-of-tile object size.
Daniel Vetter5e783302010-11-14 22:32:36 +01001373 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001374 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Daniel Vetter5e783302010-11-14 22:32:36 +01001375}
1376
Jesse Barnesde151cf2008-11-12 10:03:55 -08001377int
Dave Airlieff72145b2011-02-07 12:16:14 +10001378i915_gem_mmap_gtt(struct drm_file *file,
1379 struct drm_device *dev,
1380 uint32_t handle,
1381 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001382{
Chris Wilsonda761a62010-10-27 17:37:08 +01001383 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001384 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001385 int ret;
1386
1387 if (!(dev->driver->driver_features & DRIVER_GEM))
1388 return -ENODEV;
1389
Chris Wilson76c1dec2010-09-25 11:22:51 +01001390 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001391 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001392 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001393
Dave Airlieff72145b2011-02-07 12:16:14 +10001394 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001395 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001396 ret = -ENOENT;
1397 goto unlock;
1398 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001399
Chris Wilson05394f32010-11-08 19:18:58 +00001400 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001401 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001402 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001403 }
1404
Chris Wilson05394f32010-11-08 19:18:58 +00001405 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001406 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001407 ret = -EINVAL;
1408 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001409 }
1410
Chris Wilson05394f32010-11-08 19:18:58 +00001411 if (!obj->base.map_list.map) {
Rob Clarkb464e9a2011-08-10 08:09:08 -05001412 ret = drm_gem_create_mmap_offset(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001413 if (ret)
1414 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001415 }
1416
Dave Airlieff72145b2011-02-07 12:16:14 +10001417 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001418
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001419out:
Chris Wilson05394f32010-11-08 19:18:58 +00001420 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001421unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001422 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001423 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001424}
1425
Dave Airlieff72145b2011-02-07 12:16:14 +10001426/**
1427 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1428 * @dev: DRM device
1429 * @data: GTT mapping ioctl data
1430 * @file: GEM object info
1431 *
1432 * Simply returns the fake offset to userspace so it can mmap it.
1433 * The mmap call will end up in drm_gem_mmap(), which will set things
1434 * up so we can get faults in the handler above.
1435 *
1436 * The fault handler will take care of binding the object into the GTT
1437 * (since it may have been evicted to make room for something), allocating
1438 * a fence register, and mapping the appropriate aperture address into
1439 * userspace.
1440 */
1441int
1442i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1443 struct drm_file *file)
1444{
1445 struct drm_i915_gem_mmap_gtt *args = data;
1446
1447 if (!(dev->driver->driver_features & DRIVER_GEM))
1448 return -ENODEV;
1449
1450 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1451}
1452
1453
Chris Wilsone5281cc2010-10-28 13:45:36 +01001454static int
Chris Wilson05394f32010-11-08 19:18:58 +00001455i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001456 gfp_t gfpmask)
1457{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001458 int page_count, i;
1459 struct address_space *mapping;
1460 struct inode *inode;
1461 struct page *page;
1462
1463 /* Get the list of pages out of our struct file. They'll be pinned
1464 * at this point until we release them.
1465 */
Chris Wilson05394f32010-11-08 19:18:58 +00001466 page_count = obj->base.size / PAGE_SIZE;
1467 BUG_ON(obj->pages != NULL);
1468 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1469 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001470 return -ENOMEM;
1471
Chris Wilson05394f32010-11-08 19:18:58 +00001472 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001473 mapping = inode->i_mapping;
Hugh Dickins5949eac2011-06-27 16:18:18 -07001474 gfpmask |= mapping_gfp_mask(mapping);
1475
Chris Wilsone5281cc2010-10-28 13:45:36 +01001476 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07001477 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001478 if (IS_ERR(page))
1479 goto err_pages;
1480
Chris Wilson05394f32010-11-08 19:18:58 +00001481 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001482 }
1483
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001484 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilsone5281cc2010-10-28 13:45:36 +01001485 i915_gem_object_do_bit_17_swizzle(obj);
1486
1487 return 0;
1488
1489err_pages:
1490 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001491 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001492
Chris Wilson05394f32010-11-08 19:18:58 +00001493 drm_free_large(obj->pages);
1494 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001495 return PTR_ERR(page);
1496}
1497
Chris Wilson5cdf5882010-09-27 15:51:07 +01001498static void
Chris Wilson05394f32010-11-08 19:18:58 +00001499i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001500{
Chris Wilson05394f32010-11-08 19:18:58 +00001501 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001502 int i;
1503
Chris Wilson05394f32010-11-08 19:18:58 +00001504 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001505
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001506 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07001507 i915_gem_object_save_bit_17_swizzle(obj);
1508
Chris Wilson05394f32010-11-08 19:18:58 +00001509 if (obj->madv == I915_MADV_DONTNEED)
1510 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001511
1512 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001513 if (obj->dirty)
1514 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001515
Chris Wilson05394f32010-11-08 19:18:58 +00001516 if (obj->madv == I915_MADV_WILLNEED)
1517 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001518
Chris Wilson05394f32010-11-08 19:18:58 +00001519 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001520 }
Chris Wilson05394f32010-11-08 19:18:58 +00001521 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001522
Chris Wilson05394f32010-11-08 19:18:58 +00001523 drm_free_large(obj->pages);
1524 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001525}
1526
Chris Wilson54cf91d2010-11-25 18:00:26 +00001527void
Chris Wilson05394f32010-11-08 19:18:58 +00001528i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001529 struct intel_ring_buffer *ring,
1530 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001531{
Chris Wilson05394f32010-11-08 19:18:58 +00001532 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001533 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001534
Zou Nan hai852835f2010-05-21 09:08:56 +08001535 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001536 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001537
1538 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001539 if (!obj->active) {
1540 drm_gem_object_reference(&obj->base);
1541 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001542 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001543
Eric Anholt673a3942008-07-30 12:06:12 -07001544 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001545 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1546 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001547
Chris Wilson05394f32010-11-08 19:18:58 +00001548 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001549 if (obj->fenced_gpu_access) {
1550 struct drm_i915_fence_reg *reg;
1551
1552 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1553
1554 obj->last_fenced_seqno = seqno;
1555 obj->last_fenced_ring = ring;
1556
1557 reg = &dev_priv->fence_regs[obj->fence_reg];
1558 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1559 }
1560}
1561
1562static void
1563i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1564{
1565 list_del_init(&obj->ring_list);
1566 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001567}
1568
Eric Anholtce44b0e2008-11-06 16:00:31 -08001569static void
Chris Wilson05394f32010-11-08 19:18:58 +00001570i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001571{
Chris Wilson05394f32010-11-08 19:18:58 +00001572 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001573 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001574
Chris Wilson05394f32010-11-08 19:18:58 +00001575 BUG_ON(!obj->active);
1576 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001577
1578 i915_gem_object_move_off_active(obj);
1579}
1580
1581static void
1582i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1583{
1584 struct drm_device *dev = obj->base.dev;
1585 struct drm_i915_private *dev_priv = dev->dev_private;
1586
1587 if (obj->pin_count != 0)
1588 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1589 else
1590 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1591
1592 BUG_ON(!list_empty(&obj->gpu_write_list));
1593 BUG_ON(!obj->active);
1594 obj->ring = NULL;
1595
1596 i915_gem_object_move_off_active(obj);
1597 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001598
1599 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001600 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001601 drm_gem_object_unreference(&obj->base);
1602
1603 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001604}
Eric Anholt673a3942008-07-30 12:06:12 -07001605
Chris Wilson963b4832009-09-20 23:03:54 +01001606/* Immediately discard the backing storage */
1607static void
Chris Wilson05394f32010-11-08 19:18:58 +00001608i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001609{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001610 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001611
Chris Wilsonae9fed62010-08-07 11:01:30 +01001612 /* Our goal here is to return as much of the memory as
1613 * is possible back to the system as we are called from OOM.
1614 * To do this we must instruct the shmfs to drop all of its
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001615 * backing pages, *now*.
Chris Wilsonae9fed62010-08-07 11:01:30 +01001616 */
Chris Wilson05394f32010-11-08 19:18:58 +00001617 inode = obj->base.filp->f_path.dentry->d_inode;
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001618 shmem_truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001619
Chris Wilson05394f32010-11-08 19:18:58 +00001620 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001621}
1622
1623static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001624i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001625{
Chris Wilson05394f32010-11-08 19:18:58 +00001626 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001627}
1628
Eric Anholt673a3942008-07-30 12:06:12 -07001629static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001630i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1631 uint32_t flush_domains)
Daniel Vetter63560392010-02-19 11:51:59 +01001632{
Chris Wilson05394f32010-11-08 19:18:58 +00001633 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001634
Chris Wilson05394f32010-11-08 19:18:58 +00001635 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001636 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001637 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001638 if (obj->base.write_domain & flush_domains) {
1639 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001640
Chris Wilson05394f32010-11-08 19:18:58 +00001641 obj->base.write_domain = 0;
1642 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001643 i915_gem_object_move_to_active(obj, ring,
Chris Wilsondb53a302011-02-03 11:57:46 +00001644 i915_gem_next_request_seqno(ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001645
Daniel Vetter63560392010-02-19 11:51:59 +01001646 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001647 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001648 old_write_domain);
1649 }
1650 }
1651}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001652
Chris Wilson3cce4692010-10-27 16:11:02 +01001653int
Chris Wilsondb53a302011-02-03 11:57:46 +00001654i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001655 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001656 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001657{
Chris Wilsondb53a302011-02-03 11:57:46 +00001658 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001659 uint32_t seqno;
1660 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001661 int ret;
1662
1663 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001664
Chris Wilson3cce4692010-10-27 16:11:02 +01001665 ret = ring->add_request(ring, &seqno);
1666 if (ret)
1667 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001668
Chris Wilsondb53a302011-02-03 11:57:46 +00001669 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001670
1671 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001672 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001673 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001674 was_empty = list_empty(&ring->request_list);
1675 list_add_tail(&request->list, &ring->request_list);
1676
Chris Wilsondb53a302011-02-03 11:57:46 +00001677 if (file) {
1678 struct drm_i915_file_private *file_priv = file->driver_priv;
1679
Chris Wilson1c255952010-09-26 11:03:27 +01001680 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001681 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001682 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001683 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001684 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001685 }
Eric Anholt673a3942008-07-30 12:06:12 -07001686
Chris Wilsondb53a302011-02-03 11:57:46 +00001687 ring->outstanding_lazy_request = false;
1688
Ben Gamarif65d9422009-09-14 17:48:44 -04001689 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001690 if (i915_enable_hangcheck) {
1691 mod_timer(&dev_priv->hangcheck_timer,
1692 jiffies +
1693 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1694 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001695 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001696 queue_delayed_work(dev_priv->wq,
1697 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001698 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001699 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001700}
1701
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001702static inline void
1703i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001704{
Chris Wilson1c255952010-09-26 11:03:27 +01001705 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001706
Chris Wilson1c255952010-09-26 11:03:27 +01001707 if (!file_priv)
1708 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001709
Chris Wilson1c255952010-09-26 11:03:27 +01001710 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001711 if (request->file_priv) {
1712 list_del(&request->client_list);
1713 request->file_priv = NULL;
1714 }
Chris Wilson1c255952010-09-26 11:03:27 +01001715 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001716}
1717
Chris Wilsondfaae392010-09-22 10:31:52 +01001718static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1719 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001720{
Chris Wilsondfaae392010-09-22 10:31:52 +01001721 while (!list_empty(&ring->request_list)) {
1722 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001723
Chris Wilsondfaae392010-09-22 10:31:52 +01001724 request = list_first_entry(&ring->request_list,
1725 struct drm_i915_gem_request,
1726 list);
1727
1728 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001729 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001730 kfree(request);
1731 }
1732
1733 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001734 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001735
Chris Wilson05394f32010-11-08 19:18:58 +00001736 obj = list_first_entry(&ring->active_list,
1737 struct drm_i915_gem_object,
1738 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001739
Chris Wilson05394f32010-11-08 19:18:58 +00001740 obj->base.write_domain = 0;
1741 list_del_init(&obj->gpu_write_list);
1742 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001743 }
Eric Anholt673a3942008-07-30 12:06:12 -07001744}
1745
Chris Wilson312817a2010-11-22 11:50:11 +00001746static void i915_gem_reset_fences(struct drm_device *dev)
1747{
1748 struct drm_i915_private *dev_priv = dev->dev_private;
1749 int i;
1750
Daniel Vetter4b9de732011-10-09 21:52:02 +02001751 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00001752 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001753 struct drm_i915_gem_object *obj = reg->obj;
1754
1755 if (!obj)
1756 continue;
1757
1758 if (obj->tiling_mode)
1759 i915_gem_release_mmap(obj);
1760
Chris Wilsond9e86c02010-11-10 16:40:20 +00001761 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1762 reg->obj->fenced_gpu_access = false;
1763 reg->obj->last_fenced_seqno = 0;
1764 reg->obj->last_fenced_ring = NULL;
1765 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001766 }
1767}
1768
Chris Wilson069efc12010-09-30 16:53:18 +01001769void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001770{
Chris Wilsondfaae392010-09-22 10:31:52 +01001771 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001772 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001773 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001774
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001775 for (i = 0; i < I915_NUM_RINGS; i++)
1776 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001777
1778 /* Remove anything from the flushing lists. The GPU cache is likely
1779 * to be lost on reset along with the data, so simply move the
1780 * lost bo to the inactive list.
1781 */
1782 while (!list_empty(&dev_priv->mm.flushing_list)) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001783 obj = list_first_entry(&dev_priv->mm.flushing_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001784 struct drm_i915_gem_object,
1785 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001786
Chris Wilson05394f32010-11-08 19:18:58 +00001787 obj->base.write_domain = 0;
1788 list_del_init(&obj->gpu_write_list);
1789 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001790 }
Chris Wilson9375e442010-09-19 12:21:28 +01001791
Chris Wilsondfaae392010-09-22 10:31:52 +01001792 /* Move everything out of the GPU domains to ensure we do any
1793 * necessary invalidation upon reuse.
1794 */
Chris Wilson05394f32010-11-08 19:18:58 +00001795 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001796 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001797 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001798 {
Chris Wilson05394f32010-11-08 19:18:58 +00001799 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001800 }
Chris Wilson069efc12010-09-30 16:53:18 +01001801
1802 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001803 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001804}
1805
1806/**
1807 * This function clears the request list as sequence numbers are passed.
1808 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001809static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001810i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001811{
Eric Anholt673a3942008-07-30 12:06:12 -07001812 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001813 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001814
Chris Wilsondb53a302011-02-03 11:57:46 +00001815 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001816 return;
1817
Chris Wilsondb53a302011-02-03 11:57:46 +00001818 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001819
Chris Wilson78501ea2010-10-27 12:18:21 +01001820 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001821
Chris Wilson076e2c02011-01-21 10:07:18 +00001822 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001823 if (seqno >= ring->sync_seqno[i])
1824 ring->sync_seqno[i] = 0;
1825
Zou Nan hai852835f2010-05-21 09:08:56 +08001826 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001827 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001828
Zou Nan hai852835f2010-05-21 09:08:56 +08001829 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001830 struct drm_i915_gem_request,
1831 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001832
Chris Wilsondfaae392010-09-22 10:31:52 +01001833 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001834 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001835
Chris Wilsondb53a302011-02-03 11:57:46 +00001836 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001837
1838 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001839 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001840 kfree(request);
1841 }
1842
1843 /* Move any buffers on the active list that are no longer referenced
1844 * by the ringbuffer to the flushing/inactive lists as appropriate.
1845 */
1846 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001847 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001848
Akshay Joshi0206e352011-08-16 15:34:10 -04001849 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001850 struct drm_i915_gem_object,
1851 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001852
Chris Wilson05394f32010-11-08 19:18:58 +00001853 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001854 break;
1855
Chris Wilson05394f32010-11-08 19:18:58 +00001856 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001857 i915_gem_object_move_to_flushing(obj);
1858 else
1859 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001860 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001861
Chris Wilsondb53a302011-02-03 11:57:46 +00001862 if (unlikely(ring->trace_irq_seqno &&
1863 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001864 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001865 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001866 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001867
Chris Wilsondb53a302011-02-03 11:57:46 +00001868 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001869}
1870
1871void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001872i915_gem_retire_requests(struct drm_device *dev)
1873{
1874 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001875 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001876
Chris Wilsonbe726152010-07-23 23:18:50 +01001877 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001878 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001879
1880 /* We must be careful that during unbind() we do not
1881 * accidentally infinitely recurse into retire requests.
1882 * Currently:
1883 * retire -> free -> unbind -> wait -> retire_ring
1884 */
Chris Wilson05394f32010-11-08 19:18:58 +00001885 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001886 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001887 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001888 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001889 }
1890
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001891 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilsondb53a302011-02-03 11:57:46 +00001892 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001893}
1894
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001895static void
Eric Anholt673a3942008-07-30 12:06:12 -07001896i915_gem_retire_work_handler(struct work_struct *work)
1897{
1898 drm_i915_private_t *dev_priv;
1899 struct drm_device *dev;
Chris Wilson0a587052011-01-09 21:05:44 +00001900 bool idle;
1901 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001902
1903 dev_priv = container_of(work, drm_i915_private_t,
1904 mm.retire_work.work);
1905 dev = dev_priv->dev;
1906
Chris Wilson891b48c2010-09-29 12:26:37 +01001907 /* Come back later if the device is busy... */
1908 if (!mutex_trylock(&dev->struct_mutex)) {
1909 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1910 return;
1911 }
1912
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001913 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001914
Chris Wilson0a587052011-01-09 21:05:44 +00001915 /* Send a periodic flush down the ring so we don't hold onto GEM
1916 * objects indefinitely.
1917 */
1918 idle = true;
1919 for (i = 0; i < I915_NUM_RINGS; i++) {
1920 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1921
1922 if (!list_empty(&ring->gpu_write_list)) {
1923 struct drm_i915_gem_request *request;
1924 int ret;
1925
Chris Wilsondb53a302011-02-03 11:57:46 +00001926 ret = i915_gem_flush_ring(ring,
1927 0, I915_GEM_GPU_DOMAINS);
Chris Wilson0a587052011-01-09 21:05:44 +00001928 request = kzalloc(sizeof(*request), GFP_KERNEL);
1929 if (ret || request == NULL ||
Chris Wilsondb53a302011-02-03 11:57:46 +00001930 i915_add_request(ring, NULL, request))
Chris Wilson0a587052011-01-09 21:05:44 +00001931 kfree(request);
1932 }
1933
1934 idle &= list_empty(&ring->request_list);
1935 }
1936
1937 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001938 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilson0a587052011-01-09 21:05:44 +00001939
Eric Anholt673a3942008-07-30 12:06:12 -07001940 mutex_unlock(&dev->struct_mutex);
1941}
1942
Chris Wilsondb53a302011-02-03 11:57:46 +00001943/**
1944 * Waits for a sequence number to be signaled, and cleans up the
1945 * request and object lists appropriately for that event.
1946 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001947int
Chris Wilsondb53a302011-02-03 11:57:46 +00001948i915_wait_request(struct intel_ring_buffer *ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001949 uint32_t seqno,
1950 bool do_retire)
Eric Anholt673a3942008-07-30 12:06:12 -07001951{
Chris Wilsondb53a302011-02-03 11:57:46 +00001952 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001953 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001954 int ret = 0;
1955
1956 BUG_ON(seqno == 0);
1957
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001958 if (atomic_read(&dev_priv->mm.wedged)) {
1959 struct completion *x = &dev_priv->error_completion;
1960 bool recovery_complete;
1961 unsigned long flags;
1962
1963 /* Give the error handler a chance to run. */
1964 spin_lock_irqsave(&x->wait.lock, flags);
1965 recovery_complete = x->done > 0;
1966 spin_unlock_irqrestore(&x->wait.lock, flags);
1967
1968 return recovery_complete ? -EIO : -EAGAIN;
1969 }
Ben Gamariffed1d02009-09-14 17:48:41 -04001970
Chris Wilson5d97eb62010-11-10 20:40:02 +00001971 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01001972 struct drm_i915_gem_request *request;
1973
1974 request = kzalloc(sizeof(*request), GFP_KERNEL);
1975 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001976 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01001977
Chris Wilsondb53a302011-02-03 11:57:46 +00001978 ret = i915_add_request(ring, NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +01001979 if (ret) {
1980 kfree(request);
1981 return ret;
1982 }
1983
1984 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01001985 }
1986
Chris Wilson78501ea2010-10-27 12:18:21 +01001987 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001988 if (HAS_PCH_SPLIT(ring->dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001989 ier = I915_READ(DEIER) | I915_READ(GTIER);
1990 else
1991 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001992 if (!ier) {
1993 DRM_ERROR("something (likely vbetool) disabled "
1994 "interrupts, re-enabling\n");
Chris Wilsonf01c22f2011-06-28 11:48:51 +01001995 ring->dev->driver->irq_preinstall(ring->dev);
1996 ring->dev->driver->irq_postinstall(ring->dev);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001997 }
1998
Chris Wilsondb53a302011-02-03 11:57:46 +00001999 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002000
Chris Wilsonb2223492010-10-27 15:27:33 +01002001 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002002 if (ring->irq_get(ring)) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002003 if (dev_priv->mm.interruptible)
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002004 ret = wait_event_interruptible(ring->irq_queue,
2005 i915_seqno_passed(ring->get_seqno(ring), seqno)
2006 || atomic_read(&dev_priv->mm.wedged));
2007 else
2008 wait_event(ring->irq_queue,
2009 i915_seqno_passed(ring->get_seqno(ring), seqno)
2010 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002011
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002012 ring->irq_put(ring);
Eric Anholte959b5d2011-12-22 14:55:01 -08002013 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
2014 seqno) ||
2015 atomic_read(&dev_priv->mm.wedged), 3000))
Chris Wilsonb5ba1772010-12-14 12:17:15 +00002016 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01002017 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002018
Chris Wilsondb53a302011-02-03 11:57:46 +00002019 trace_i915_gem_request_wait_end(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002020 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002021 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002022 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002023
2024 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002025 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002026 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002027 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002028
2029 /* Directly dispatch request retiring. While we have the work queue
2030 * to handle this, the waiter on a request often wants an associated
2031 * buffer to have made it to the inactive list, and we would need
2032 * a separate wait queue to handle that.
2033 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002034 if (ret == 0 && do_retire)
Chris Wilsondb53a302011-02-03 11:57:46 +00002035 i915_gem_retire_requests_ring(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002036
2037 return ret;
2038}
2039
Daniel Vetter48764bf2009-09-15 22:57:32 +02002040/**
Eric Anholt673a3942008-07-30 12:06:12 -07002041 * Ensures that all rendering to the object has completed and the object is
2042 * safe to unbind from the GTT or access from the CPU.
2043 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00002044int
Chris Wilsonce453d82011-02-21 14:43:56 +00002045i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002046{
Eric Anholt673a3942008-07-30 12:06:12 -07002047 int ret;
2048
Eric Anholte47c68e2008-11-14 13:35:19 -08002049 /* This function only exists to support waiting for existing rendering,
2050 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002051 */
Chris Wilson05394f32010-11-08 19:18:58 +00002052 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002053
2054 /* If there is rendering queued on the buffer being evicted, wait for
2055 * it.
2056 */
Chris Wilson05394f32010-11-08 19:18:58 +00002057 if (obj->active) {
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002058 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno,
2059 true);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002060 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002061 return ret;
2062 }
2063
2064 return 0;
2065}
2066
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002067static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2068{
2069 u32 old_write_domain, old_read_domains;
2070
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002071 /* Act a barrier for all accesses through the GTT */
2072 mb();
2073
2074 /* Force a pagefault for domain tracking on next user access */
2075 i915_gem_release_mmap(obj);
2076
Keith Packardb97c3d92011-06-24 21:02:59 -07002077 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2078 return;
2079
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002080 old_read_domains = obj->base.read_domains;
2081 old_write_domain = obj->base.write_domain;
2082
2083 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2084 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2085
2086 trace_i915_gem_object_change_domain(obj,
2087 old_read_domains,
2088 old_write_domain);
2089}
2090
Eric Anholt673a3942008-07-30 12:06:12 -07002091/**
2092 * Unbinds an object from the GTT aperture.
2093 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002094int
Chris Wilson05394f32010-11-08 19:18:58 +00002095i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002096{
Eric Anholt673a3942008-07-30 12:06:12 -07002097 int ret = 0;
2098
Chris Wilson05394f32010-11-08 19:18:58 +00002099 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002100 return 0;
2101
Chris Wilson05394f32010-11-08 19:18:58 +00002102 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002103 DRM_ERROR("Attempting to unbind pinned buffer\n");
2104 return -EINVAL;
2105 }
2106
Chris Wilsona8198ee2011-04-13 22:04:09 +01002107 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson8dc17752010-07-23 23:18:51 +01002108 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002109 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002110 /* Continue on if we fail due to EIO, the GPU is hung so we
2111 * should be safe and we need to cleanup or else we might
2112 * cause memory corruption through use-after-free.
2113 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002114
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002115 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002116
2117 /* Move the object to the CPU domain to ensure that
2118 * any possible CPU writes while it's not in the GTT
2119 * are flushed when we go to remap it.
2120 */
2121 if (ret == 0)
2122 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2123 if (ret == -ERESTARTSYS)
2124 return ret;
Chris Wilson812ed4922010-09-30 15:08:57 +01002125 if (ret) {
Chris Wilsona8198ee2011-04-13 22:04:09 +01002126 /* In the event of a disaster, abandon all caches and
2127 * hope for the best.
2128 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002129 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002130 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002131 }
Eric Anholt673a3942008-07-30 12:06:12 -07002132
Daniel Vetter96b47b62009-12-15 17:50:00 +01002133 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002134 ret = i915_gem_object_put_fence(obj);
2135 if (ret == -ERESTARTSYS)
2136 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002137
Chris Wilsondb53a302011-02-03 11:57:46 +00002138 trace_i915_gem_object_unbind(obj);
2139
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002140 i915_gem_gtt_unbind_object(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002141 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002142
Chris Wilson6299f992010-11-24 12:23:44 +00002143 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002144 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002145 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002146 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002147
Chris Wilson05394f32010-11-08 19:18:58 +00002148 drm_mm_put_block(obj->gtt_space);
2149 obj->gtt_space = NULL;
2150 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002151
Chris Wilson05394f32010-11-08 19:18:58 +00002152 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002153 i915_gem_object_truncate(obj);
2154
Chris Wilson8dc17752010-07-23 23:18:51 +01002155 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002156}
2157
Chris Wilson88241782011-01-07 17:09:48 +00002158int
Chris Wilsondb53a302011-02-03 11:57:46 +00002159i915_gem_flush_ring(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002160 uint32_t invalidate_domains,
2161 uint32_t flush_domains)
2162{
Chris Wilson88241782011-01-07 17:09:48 +00002163 int ret;
2164
Chris Wilson36d527d2011-03-19 22:26:49 +00002165 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2166 return 0;
2167
Chris Wilsondb53a302011-02-03 11:57:46 +00002168 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2169
Chris Wilson88241782011-01-07 17:09:48 +00002170 ret = ring->flush(ring, invalidate_domains, flush_domains);
2171 if (ret)
2172 return ret;
2173
Chris Wilson36d527d2011-03-19 22:26:49 +00002174 if (flush_domains & I915_GEM_GPU_DOMAINS)
2175 i915_gem_process_flushing_list(ring, flush_domains);
2176
Chris Wilson88241782011-01-07 17:09:48 +00002177 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002178}
2179
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002180static int i915_ring_idle(struct intel_ring_buffer *ring, bool do_retire)
Chris Wilsona56ba562010-09-28 10:07:56 +01002181{
Chris Wilson88241782011-01-07 17:09:48 +00002182 int ret;
2183
Chris Wilson395b70b2010-10-28 21:28:46 +01002184 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002185 return 0;
2186
Chris Wilson88241782011-01-07 17:09:48 +00002187 if (!list_empty(&ring->gpu_write_list)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002188 ret = i915_gem_flush_ring(ring,
Chris Wilson0ac74c62010-12-06 14:36:02 +00002189 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilson88241782011-01-07 17:09:48 +00002190 if (ret)
2191 return ret;
2192 }
2193
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002194 return i915_wait_request(ring, i915_gem_next_request_seqno(ring),
2195 do_retire);
Chris Wilsona56ba562010-09-28 10:07:56 +01002196}
2197
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002198int i915_gpu_idle(struct drm_device *dev, bool do_retire)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002199{
2200 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002201 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002202
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002203 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002204 for (i = 0; i < I915_NUM_RINGS; i++) {
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002205 ret = i915_ring_idle(&dev_priv->ring[i], do_retire);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002206 if (ret)
2207 return ret;
2208 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002209
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002210 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002211}
2212
Daniel Vetterc6642782010-11-12 13:46:18 +00002213static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2214 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002215{
Chris Wilson05394f32010-11-08 19:18:58 +00002216 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002217 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002218 u32 size = obj->gtt_space->size;
2219 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002220 uint64_t val;
2221
Chris Wilson05394f32010-11-08 19:18:58 +00002222 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002223 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002224 val |= obj->gtt_offset & 0xfffff000;
2225 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002226 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2227
Chris Wilson05394f32010-11-08 19:18:58 +00002228 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002229 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2230 val |= I965_FENCE_REG_VALID;
2231
Daniel Vetterc6642782010-11-12 13:46:18 +00002232 if (pipelined) {
2233 int ret = intel_ring_begin(pipelined, 6);
2234 if (ret)
2235 return ret;
2236
2237 intel_ring_emit(pipelined, MI_NOOP);
2238 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2239 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2240 intel_ring_emit(pipelined, (u32)val);
2241 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2242 intel_ring_emit(pipelined, (u32)(val >> 32));
2243 intel_ring_advance(pipelined);
2244 } else
2245 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2246
2247 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002248}
2249
Daniel Vetterc6642782010-11-12 13:46:18 +00002250static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2251 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002252{
Chris Wilson05394f32010-11-08 19:18:58 +00002253 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002254 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002255 u32 size = obj->gtt_space->size;
2256 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002257 uint64_t val;
2258
Chris Wilson05394f32010-11-08 19:18:58 +00002259 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002260 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002261 val |= obj->gtt_offset & 0xfffff000;
2262 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2263 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002264 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2265 val |= I965_FENCE_REG_VALID;
2266
Daniel Vetterc6642782010-11-12 13:46:18 +00002267 if (pipelined) {
2268 int ret = intel_ring_begin(pipelined, 6);
2269 if (ret)
2270 return ret;
2271
2272 intel_ring_emit(pipelined, MI_NOOP);
2273 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2274 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2275 intel_ring_emit(pipelined, (u32)val);
2276 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2277 intel_ring_emit(pipelined, (u32)(val >> 32));
2278 intel_ring_advance(pipelined);
2279 } else
2280 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2281
2282 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002283}
2284
Daniel Vetterc6642782010-11-12 13:46:18 +00002285static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2286 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002287{
Chris Wilson05394f32010-11-08 19:18:58 +00002288 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002289 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002290 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002291 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002292 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002293
Daniel Vetterc6642782010-11-12 13:46:18 +00002294 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2295 (size & -size) != size ||
2296 (obj->gtt_offset & (size - 1)),
2297 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2298 obj->gtt_offset, obj->map_and_fenceable, size))
2299 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002300
Daniel Vetterc6642782010-11-12 13:46:18 +00002301 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002302 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002303 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002304 tile_width = 512;
2305
2306 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002307 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002308 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002309
Chris Wilson05394f32010-11-08 19:18:58 +00002310 val = obj->gtt_offset;
2311 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002312 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002313 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002314 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2315 val |= I830_FENCE_REG_VALID;
2316
Chris Wilson05394f32010-11-08 19:18:58 +00002317 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002318 if (fence_reg < 8)
2319 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002320 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002321 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002322
2323 if (pipelined) {
2324 int ret = intel_ring_begin(pipelined, 4);
2325 if (ret)
2326 return ret;
2327
2328 intel_ring_emit(pipelined, MI_NOOP);
2329 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2330 intel_ring_emit(pipelined, fence_reg);
2331 intel_ring_emit(pipelined, val);
2332 intel_ring_advance(pipelined);
2333 } else
2334 I915_WRITE(fence_reg, val);
2335
2336 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002337}
2338
Daniel Vetterc6642782010-11-12 13:46:18 +00002339static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2340 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002341{
Chris Wilson05394f32010-11-08 19:18:58 +00002342 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002344 u32 size = obj->gtt_space->size;
2345 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002346 uint32_t val;
2347 uint32_t pitch_val;
2348
Daniel Vetterc6642782010-11-12 13:46:18 +00002349 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2350 (size & -size) != size ||
2351 (obj->gtt_offset & (size - 1)),
2352 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2353 obj->gtt_offset, size))
2354 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002355
Chris Wilson05394f32010-11-08 19:18:58 +00002356 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002357 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002358
Chris Wilson05394f32010-11-08 19:18:58 +00002359 val = obj->gtt_offset;
2360 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002361 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002362 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002363 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2364 val |= I830_FENCE_REG_VALID;
2365
Daniel Vetterc6642782010-11-12 13:46:18 +00002366 if (pipelined) {
2367 int ret = intel_ring_begin(pipelined, 4);
2368 if (ret)
2369 return ret;
2370
2371 intel_ring_emit(pipelined, MI_NOOP);
2372 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2373 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2374 intel_ring_emit(pipelined, val);
2375 intel_ring_advance(pipelined);
2376 } else
2377 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2378
2379 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002380}
2381
Chris Wilsond9e86c02010-11-10 16:40:20 +00002382static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2383{
2384 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2385}
2386
2387static int
2388i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002389 struct intel_ring_buffer *pipelined)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002390{
2391 int ret;
2392
2393 if (obj->fenced_gpu_access) {
Chris Wilson88241782011-01-07 17:09:48 +00002394 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002395 ret = i915_gem_flush_ring(obj->last_fenced_ring,
Chris Wilson88241782011-01-07 17:09:48 +00002396 0, obj->base.write_domain);
2397 if (ret)
2398 return ret;
2399 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002400
2401 obj->fenced_gpu_access = false;
2402 }
2403
2404 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2405 if (!ring_passed_seqno(obj->last_fenced_ring,
2406 obj->last_fenced_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002407 ret = i915_wait_request(obj->last_fenced_ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002408 obj->last_fenced_seqno,
2409 true);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002410 if (ret)
2411 return ret;
2412 }
2413
2414 obj->last_fenced_seqno = 0;
2415 obj->last_fenced_ring = NULL;
2416 }
2417
Chris Wilson63256ec2011-01-04 18:42:07 +00002418 /* Ensure that all CPU reads are completed before installing a fence
2419 * and all writes before removing the fence.
2420 */
2421 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2422 mb();
2423
Chris Wilsond9e86c02010-11-10 16:40:20 +00002424 return 0;
2425}
2426
2427int
2428i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2429{
2430 int ret;
2431
2432 if (obj->tiling_mode)
2433 i915_gem_release_mmap(obj);
2434
Chris Wilsonce453d82011-02-21 14:43:56 +00002435 ret = i915_gem_object_flush_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002436 if (ret)
2437 return ret;
2438
2439 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2440 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002441
2442 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002443 i915_gem_clear_fence_reg(obj->base.dev,
2444 &dev_priv->fence_regs[obj->fence_reg]);
2445
2446 obj->fence_reg = I915_FENCE_REG_NONE;
2447 }
2448
2449 return 0;
2450}
2451
2452static struct drm_i915_fence_reg *
2453i915_find_fence_reg(struct drm_device *dev,
2454 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002455{
Daniel Vetterae3db242010-02-19 11:51:58 +01002456 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002457 struct drm_i915_fence_reg *reg, *first, *avail;
2458 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002459
2460 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002461 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002462 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2463 reg = &dev_priv->fence_regs[i];
2464 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002465 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002466
Chris Wilson1690e1e2011-12-14 13:57:08 +01002467 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002468 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002469 }
2470
Chris Wilsond9e86c02010-11-10 16:40:20 +00002471 if (avail == NULL)
2472 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002473
2474 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002475 avail = first = NULL;
2476 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002477 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002478 continue;
2479
Chris Wilsond9e86c02010-11-10 16:40:20 +00002480 if (first == NULL)
2481 first = reg;
2482
2483 if (!pipelined ||
2484 !reg->obj->last_fenced_ring ||
2485 reg->obj->last_fenced_ring == pipelined) {
2486 avail = reg;
2487 break;
2488 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002489 }
2490
Chris Wilsond9e86c02010-11-10 16:40:20 +00002491 if (avail == NULL)
2492 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002493
Chris Wilsona00b10c2010-09-24 21:15:47 +01002494 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002495}
2496
Jesse Barnesde151cf2008-11-12 10:03:55 -08002497/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002498 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002499 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002500 * @pipelined: ring on which to queue the change, or NULL for CPU access
2501 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002502 *
2503 * When mapping objects through the GTT, userspace wants to be able to write
2504 * to them without having to worry about swizzling if the object is tiled.
2505 *
2506 * This function walks the fence regs looking for a free one for @obj,
2507 * stealing one if it can't find any.
2508 *
2509 * It then sets up the reg based on the object's properties: address, pitch
2510 * and tiling format.
2511 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002512int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002513i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002514 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002515{
Chris Wilson05394f32010-11-08 19:18:58 +00002516 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002517 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002518 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002519 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002520
Chris Wilson6bda10d2010-12-05 21:04:18 +00002521 /* XXX disable pipelining. There are bugs. Shocking. */
2522 pipelined = NULL;
2523
Chris Wilsond9e86c02010-11-10 16:40:20 +00002524 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002525 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2526 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002527 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002528
Chris Wilson29c5a582011-03-17 15:23:22 +00002529 if (obj->tiling_changed) {
2530 ret = i915_gem_object_flush_fence(obj, pipelined);
2531 if (ret)
2532 return ret;
2533
2534 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2535 pipelined = NULL;
2536
2537 if (pipelined) {
2538 reg->setup_seqno =
2539 i915_gem_next_request_seqno(pipelined);
2540 obj->last_fenced_seqno = reg->setup_seqno;
2541 obj->last_fenced_ring = pipelined;
2542 }
2543
2544 goto update;
2545 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002546
2547 if (!pipelined) {
2548 if (reg->setup_seqno) {
2549 if (!ring_passed_seqno(obj->last_fenced_ring,
2550 reg->setup_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002551 ret = i915_wait_request(obj->last_fenced_ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002552 reg->setup_seqno,
2553 true);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002554 if (ret)
2555 return ret;
2556 }
2557
2558 reg->setup_seqno = 0;
2559 }
2560 } else if (obj->last_fenced_ring &&
2561 obj->last_fenced_ring != pipelined) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002562 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002563 if (ret)
2564 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002565 }
2566
Eric Anholta09ba7f2009-08-29 12:49:51 -07002567 return 0;
2568 }
2569
Chris Wilsond9e86c02010-11-10 16:40:20 +00002570 reg = i915_find_fence_reg(dev, pipelined);
2571 if (reg == NULL)
Daniel Vetter39965b32011-12-14 13:57:09 +01002572 return -EDEADLK;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002573
Chris Wilsonce453d82011-02-21 14:43:56 +00002574 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002575 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002576 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002577
Chris Wilsond9e86c02010-11-10 16:40:20 +00002578 if (reg->obj) {
2579 struct drm_i915_gem_object *old = reg->obj;
2580
2581 drm_gem_object_reference(&old->base);
2582
2583 if (old->tiling_mode)
2584 i915_gem_release_mmap(old);
2585
Chris Wilsonce453d82011-02-21 14:43:56 +00002586 ret = i915_gem_object_flush_fence(old, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002587 if (ret) {
2588 drm_gem_object_unreference(&old->base);
2589 return ret;
2590 }
2591
2592 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2593 pipelined = NULL;
2594
2595 old->fence_reg = I915_FENCE_REG_NONE;
2596 old->last_fenced_ring = pipelined;
2597 old->last_fenced_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002598 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002599
2600 drm_gem_object_unreference(&old->base);
2601 } else if (obj->last_fenced_seqno == 0)
2602 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002603
Jesse Barnesde151cf2008-11-12 10:03:55 -08002604 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002605 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2606 obj->fence_reg = reg - dev_priv->fence_regs;
2607 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002608
Chris Wilsond9e86c02010-11-10 16:40:20 +00002609 reg->setup_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002610 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002611 obj->last_fenced_seqno = reg->setup_seqno;
2612
2613update:
2614 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002615 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002616 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002617 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002618 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002619 break;
2620 case 5:
2621 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002622 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002623 break;
2624 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002625 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002626 break;
2627 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002628 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002629 break;
2630 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002631
Daniel Vetterc6642782010-11-12 13:46:18 +00002632 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002633}
2634
2635/**
2636 * i915_gem_clear_fence_reg - clear out fence register info
2637 * @obj: object to clear
2638 *
2639 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002640 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002641 */
2642static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002643i915_gem_clear_fence_reg(struct drm_device *dev,
2644 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002645{
Jesse Barnes79e53942008-11-07 14:24:08 -08002646 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002647 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002648
Chris Wilsone259bef2010-09-17 00:32:02 +01002649 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002650 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002651 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002652 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002653 break;
2654 case 5:
2655 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002656 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002657 break;
2658 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002659 if (fence_reg >= 8)
2660 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002661 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002662 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002663 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002664
2665 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002666 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002667 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002668
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002669 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002670 reg->obj = NULL;
2671 reg->setup_seqno = 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002672 reg->pin_count = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002673}
2674
2675/**
Eric Anholt673a3942008-07-30 12:06:12 -07002676 * Finds free space in the GTT aperture and binds the object there.
2677 */
2678static int
Chris Wilson05394f32010-11-08 19:18:58 +00002679i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002680 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002681 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002682{
Chris Wilson05394f32010-11-08 19:18:58 +00002683 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002684 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002685 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002686 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002687 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002688 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002689 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002690
Chris Wilson05394f32010-11-08 19:18:58 +00002691 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002692 DRM_ERROR("Attempting to bind a purgeable object\n");
2693 return -EINVAL;
2694 }
2695
Chris Wilsone28f8712011-07-18 13:11:49 -07002696 fence_size = i915_gem_get_gtt_size(dev,
2697 obj->base.size,
2698 obj->tiling_mode);
2699 fence_alignment = i915_gem_get_gtt_alignment(dev,
2700 obj->base.size,
2701 obj->tiling_mode);
2702 unfenced_alignment =
2703 i915_gem_get_unfenced_gtt_alignment(dev,
2704 obj->base.size,
2705 obj->tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002706
Eric Anholt673a3942008-07-30 12:06:12 -07002707 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002708 alignment = map_and_fenceable ? fence_alignment :
2709 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002710 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002711 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2712 return -EINVAL;
2713 }
2714
Chris Wilson05394f32010-11-08 19:18:58 +00002715 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002716
Chris Wilson654fc602010-05-27 13:18:21 +01002717 /* If the object is bigger than the entire aperture, reject it early
2718 * before evicting everything in a vain attempt to find space.
2719 */
Chris Wilson05394f32010-11-08 19:18:58 +00002720 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002721 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002722 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2723 return -E2BIG;
2724 }
2725
Eric Anholt673a3942008-07-30 12:06:12 -07002726 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002727 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002728 free_space =
2729 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002730 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002731 dev_priv->mm.gtt_mappable_end,
2732 0);
2733 else
2734 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002735 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002736
2737 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002738 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002739 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002740 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002741 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002742 dev_priv->mm.gtt_mappable_end,
2743 0);
2744 else
Chris Wilson05394f32010-11-08 19:18:58 +00002745 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002746 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002747 }
Chris Wilson05394f32010-11-08 19:18:58 +00002748 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002749 /* If the gtt is empty and we're still having trouble
2750 * fitting our object in, we're out of memory.
2751 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002752 ret = i915_gem_evict_something(dev, size, alignment,
2753 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002754 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002755 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002756
Eric Anholt673a3942008-07-30 12:06:12 -07002757 goto search_free;
2758 }
2759
Chris Wilsone5281cc2010-10-28 13:45:36 +01002760 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002761 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002762 drm_mm_put_block(obj->gtt_space);
2763 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002764
2765 if (ret == -ENOMEM) {
Chris Wilson809b6332011-01-10 17:33:15 +00002766 /* first try to reclaim some memory by clearing the GTT */
2767 ret = i915_gem_evict_everything(dev, false);
Chris Wilson07f73f62009-09-14 16:50:30 +01002768 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002769 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002770 if (gfpmask) {
2771 gfpmask = 0;
2772 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002773 }
2774
Chris Wilson809b6332011-01-10 17:33:15 +00002775 return -ENOMEM;
Chris Wilson07f73f62009-09-14 16:50:30 +01002776 }
2777
2778 goto search_free;
2779 }
2780
Eric Anholt673a3942008-07-30 12:06:12 -07002781 return ret;
2782 }
2783
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002784 ret = i915_gem_gtt_bind_object(obj);
2785 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002786 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002787 drm_mm_put_block(obj->gtt_space);
2788 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002789
Chris Wilson809b6332011-01-10 17:33:15 +00002790 if (i915_gem_evict_everything(dev, false))
Chris Wilson07f73f62009-09-14 16:50:30 +01002791 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002792
2793 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002794 }
Eric Anholt673a3942008-07-30 12:06:12 -07002795
Chris Wilson6299f992010-11-24 12:23:44 +00002796 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002797 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002798
Eric Anholt673a3942008-07-30 12:06:12 -07002799 /* Assert that the object is not currently in any GPU domain. As it
2800 * wasn't in the GTT, there shouldn't be any way it could have been in
2801 * a GPU cache
2802 */
Chris Wilson05394f32010-11-08 19:18:58 +00002803 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2804 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002805
Chris Wilson6299f992010-11-24 12:23:44 +00002806 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002807
Daniel Vetter75e9e912010-11-04 17:11:09 +01002808 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002809 obj->gtt_space->size == fence_size &&
Akshay Joshi0206e352011-08-16 15:34:10 -04002810 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002811
Daniel Vetter75e9e912010-11-04 17:11:09 +01002812 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002813 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002814
Chris Wilson05394f32010-11-08 19:18:58 +00002815 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002816
Chris Wilsondb53a302011-02-03 11:57:46 +00002817 trace_i915_gem_object_bind(obj, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002818 return 0;
2819}
2820
2821void
Chris Wilson05394f32010-11-08 19:18:58 +00002822i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002823{
Eric Anholt673a3942008-07-30 12:06:12 -07002824 /* If we don't have a page list set up, then we're not pinned
2825 * to GPU, and we can ignore the cache flush because it'll happen
2826 * again at bind time.
2827 */
Chris Wilson05394f32010-11-08 19:18:58 +00002828 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002829 return;
2830
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002831 /* If the GPU is snooping the contents of the CPU cache,
2832 * we do not need to manually clear the CPU cache lines. However,
2833 * the caches are only snooped when the render cache is
2834 * flushed/invalidated. As we always have to emit invalidations
2835 * and flushes when moving into and out of the RENDER domain, correct
2836 * snooping behaviour occurs naturally as the result of our domain
2837 * tracking.
2838 */
2839 if (obj->cache_level != I915_CACHE_NONE)
2840 return;
2841
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002842 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002843
Chris Wilson05394f32010-11-08 19:18:58 +00002844 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002845}
2846
Eric Anholte47c68e2008-11-14 13:35:19 -08002847/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson88241782011-01-07 17:09:48 +00002848static int
Chris Wilson3619df02010-11-28 15:37:17 +00002849i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002850{
Chris Wilson05394f32010-11-08 19:18:58 +00002851 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson88241782011-01-07 17:09:48 +00002852 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002853
2854 /* Queue the GPU write cache flushing we need. */
Chris Wilsondb53a302011-02-03 11:57:46 +00002855 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002856}
2857
2858/** Flushes the GTT write domain for the object if it's dirty. */
2859static void
Chris Wilson05394f32010-11-08 19:18:58 +00002860i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002861{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002862 uint32_t old_write_domain;
2863
Chris Wilson05394f32010-11-08 19:18:58 +00002864 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002865 return;
2866
Chris Wilson63256ec2011-01-04 18:42:07 +00002867 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002868 * to it immediately go to main memory as far as we know, so there's
2869 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002870 *
2871 * However, we do have to enforce the order so that all writes through
2872 * the GTT land before any writes to the device, such as updates to
2873 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002874 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002875 wmb();
2876
Chris Wilson05394f32010-11-08 19:18:58 +00002877 old_write_domain = obj->base.write_domain;
2878 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002879
2880 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002881 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002882 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002883}
2884
2885/** Flushes the CPU write domain for the object if it's dirty. */
2886static void
Chris Wilson05394f32010-11-08 19:18:58 +00002887i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002888{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002889 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002890
Chris Wilson05394f32010-11-08 19:18:58 +00002891 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002892 return;
2893
2894 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002895 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002896 old_write_domain = obj->base.write_domain;
2897 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002898
2899 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002900 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002901 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002902}
2903
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002904/**
2905 * Moves a single object to the GTT read, and possibly write domain.
2906 *
2907 * This function returns when the move is complete, including waiting on
2908 * flushes to occur.
2909 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002910int
Chris Wilson20217462010-11-23 15:26:33 +00002911i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002912{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002913 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002914 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002915
Eric Anholt02354392008-11-26 13:58:13 -08002916 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002917 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002918 return -EINVAL;
2919
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002920 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2921 return 0;
2922
Chris Wilson88241782011-01-07 17:09:48 +00002923 ret = i915_gem_object_flush_gpu_write_domain(obj);
2924 if (ret)
2925 return ret;
2926
Chris Wilson87ca9c82010-12-02 09:42:56 +00002927 if (obj->pending_gpu_write || write) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002928 ret = i915_gem_object_wait_rendering(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00002929 if (ret)
2930 return ret;
2931 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002932
Chris Wilson72133422010-09-13 23:56:38 +01002933 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002934
Chris Wilson05394f32010-11-08 19:18:58 +00002935 old_write_domain = obj->base.write_domain;
2936 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002937
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002938 /* It should now be out of any other write domains, and we can update
2939 * the domain values for our changes.
2940 */
Chris Wilson05394f32010-11-08 19:18:58 +00002941 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2942 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002943 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002944 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2945 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2946 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002947 }
2948
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002949 trace_i915_gem_object_change_domain(obj,
2950 old_read_domains,
2951 old_write_domain);
2952
Eric Anholte47c68e2008-11-14 13:35:19 -08002953 return 0;
2954}
2955
Chris Wilsone4ffd172011-04-04 09:44:39 +01002956int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2957 enum i915_cache_level cache_level)
2958{
2959 int ret;
2960
2961 if (obj->cache_level == cache_level)
2962 return 0;
2963
2964 if (obj->pin_count) {
2965 DRM_DEBUG("can not change the cache level of pinned objects\n");
2966 return -EBUSY;
2967 }
2968
2969 if (obj->gtt_space) {
2970 ret = i915_gem_object_finish_gpu(obj);
2971 if (ret)
2972 return ret;
2973
2974 i915_gem_object_finish_gtt(obj);
2975
2976 /* Before SandyBridge, you could not use tiling or fence
2977 * registers with snooped memory, so relinquish any fences
2978 * currently pointing to our region in the aperture.
2979 */
2980 if (INTEL_INFO(obj->base.dev)->gen < 6) {
2981 ret = i915_gem_object_put_fence(obj);
2982 if (ret)
2983 return ret;
2984 }
2985
2986 i915_gem_gtt_rebind_object(obj, cache_level);
2987 }
2988
2989 if (cache_level == I915_CACHE_NONE) {
2990 u32 old_read_domains, old_write_domain;
2991
2992 /* If we're coming from LLC cached, then we haven't
2993 * actually been tracking whether the data is in the
2994 * CPU cache or not, since we only allow one bit set
2995 * in obj->write_domain and have been skipping the clflushes.
2996 * Just set it to the CPU cache for now.
2997 */
2998 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
2999 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3000
3001 old_read_domains = obj->base.read_domains;
3002 old_write_domain = obj->base.write_domain;
3003
3004 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3005 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3006
3007 trace_i915_gem_object_change_domain(obj,
3008 old_read_domains,
3009 old_write_domain);
3010 }
3011
3012 obj->cache_level = cache_level;
3013 return 0;
3014}
3015
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003016/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003017 * Prepare buffer for display plane (scanout, cursors, etc).
3018 * Can be called from an uninterruptible phase (modesetting) and allows
3019 * any flushes to be pipelined (for pageflips).
3020 *
3021 * For the display plane, we want to be in the GTT but out of any write
3022 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
3023 * ability to pipeline the waits, pinning and any additional subtleties
3024 * that may differentiate the display plane from ordinary buffers.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003025 */
3026int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003027i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3028 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003029 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003030{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003031 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003032 int ret;
3033
Chris Wilson88241782011-01-07 17:09:48 +00003034 ret = i915_gem_object_flush_gpu_write_domain(obj);
3035 if (ret)
3036 return ret;
3037
Chris Wilson0be73282010-12-06 14:36:27 +00003038 if (pipelined != obj->ring) {
Chris Wilsonce453d82011-02-21 14:43:56 +00003039 ret = i915_gem_object_wait_rendering(obj);
Keith Packardf0b69ef2011-07-19 16:21:40 -07003040 if (ret == -ERESTARTSYS)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003041 return ret;
3042 }
3043
Eric Anholta7ef0642011-03-29 16:59:54 -07003044 /* The display engine is not coherent with the LLC cache on gen6. As
3045 * a result, we make sure that the pinning that is about to occur is
3046 * done with uncached PTEs. This is lowest common denominator for all
3047 * chipsets.
3048 *
3049 * However for gen6+, we could do better by using the GFDT bit instead
3050 * of uncaching, which would allow us to flush all the LLC-cached data
3051 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3052 */
3053 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3054 if (ret)
3055 return ret;
3056
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003057 /* As the user may map the buffer once pinned in the display plane
3058 * (e.g. libkms for the bootup splash), we have to ensure that we
3059 * always use map_and_fenceable for all scanout buffers.
3060 */
3061 ret = i915_gem_object_pin(obj, alignment, true);
3062 if (ret)
3063 return ret;
3064
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003065 i915_gem_object_flush_cpu_write_domain(obj);
3066
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003067 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003068 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003069
3070 /* It should now be out of any other write domains, and we can update
3071 * the domain values for our changes.
3072 */
3073 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003074 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003075
3076 trace_i915_gem_object_change_domain(obj,
3077 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003078 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003079
3080 return 0;
3081}
3082
Chris Wilson85345512010-11-13 09:49:11 +00003083int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003084i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003085{
Chris Wilson88241782011-01-07 17:09:48 +00003086 int ret;
3087
Chris Wilsona8198ee2011-04-13 22:04:09 +01003088 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003089 return 0;
3090
Chris Wilson88241782011-01-07 17:09:48 +00003091 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003092 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Chris Wilson88241782011-01-07 17:09:48 +00003093 if (ret)
3094 return ret;
3095 }
Chris Wilson85345512010-11-13 09:49:11 +00003096
Chris Wilsona8198ee2011-04-13 22:04:09 +01003097 /* Ensure that we invalidate the GPU's caches and TLBs. */
3098 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3099
Chris Wilsonce453d82011-02-21 14:43:56 +00003100 return i915_gem_object_wait_rendering(obj);
Chris Wilson85345512010-11-13 09:49:11 +00003101}
3102
Eric Anholte47c68e2008-11-14 13:35:19 -08003103/**
3104 * Moves a single object to the CPU read, and possibly write domain.
3105 *
3106 * This function returns when the move is complete, including waiting on
3107 * flushes to occur.
3108 */
3109static int
Chris Wilson919926a2010-11-12 13:42:53 +00003110i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003111{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003112 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003113 int ret;
3114
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003115 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3116 return 0;
3117
Chris Wilson88241782011-01-07 17:09:48 +00003118 ret = i915_gem_object_flush_gpu_write_domain(obj);
3119 if (ret)
3120 return ret;
3121
Chris Wilsonce453d82011-02-21 14:43:56 +00003122 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003123 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003124 return ret;
3125
3126 i915_gem_object_flush_gtt_write_domain(obj);
3127
3128 /* If we have a partially-valid cache of the object in the CPU,
3129 * finish invalidating it and free the per-page flags.
3130 */
3131 i915_gem_object_set_to_full_cpu_read_domain(obj);
3132
Chris Wilson05394f32010-11-08 19:18:58 +00003133 old_write_domain = obj->base.write_domain;
3134 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003135
Eric Anholte47c68e2008-11-14 13:35:19 -08003136 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003137 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003138 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003139
Chris Wilson05394f32010-11-08 19:18:58 +00003140 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003141 }
3142
3143 /* It should now be out of any other write domains, and we can update
3144 * the domain values for our changes.
3145 */
Chris Wilson05394f32010-11-08 19:18:58 +00003146 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003147
3148 /* If we're writing through the CPU, then the GPU read domains will
3149 * need to be invalidated at next use.
3150 */
3151 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003152 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3153 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003154 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003155
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003156 trace_i915_gem_object_change_domain(obj,
3157 old_read_domains,
3158 old_write_domain);
3159
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003160 return 0;
3161}
3162
Eric Anholt673a3942008-07-30 12:06:12 -07003163/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003164 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003165 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003166 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3167 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3168 */
3169static void
Chris Wilson05394f32010-11-08 19:18:58 +00003170i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003171{
Chris Wilson05394f32010-11-08 19:18:58 +00003172 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003173 return;
3174
3175 /* If we're partially in the CPU read domain, finish moving it in.
3176 */
Chris Wilson05394f32010-11-08 19:18:58 +00003177 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003178 int i;
3179
Chris Wilson05394f32010-11-08 19:18:58 +00003180 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3181 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003182 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003183 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003184 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003185 }
3186
3187 /* Free the page_cpu_valid mappings which are now stale, whether
3188 * or not we've got I915_GEM_DOMAIN_CPU.
3189 */
Chris Wilson05394f32010-11-08 19:18:58 +00003190 kfree(obj->page_cpu_valid);
3191 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003192}
3193
3194/**
3195 * Set the CPU read domain on a range of the object.
3196 *
3197 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3198 * not entirely valid. The page_cpu_valid member of the object flags which
3199 * pages have been flushed, and will be respected by
3200 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3201 * of the whole object.
3202 *
3203 * This function returns when the move is complete, including waiting on
3204 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003205 */
3206static int
Chris Wilson05394f32010-11-08 19:18:58 +00003207i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003208 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003209{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003210 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003211 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003212
Chris Wilson05394f32010-11-08 19:18:58 +00003213 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003214 return i915_gem_object_set_to_cpu_domain(obj, 0);
3215
Chris Wilson88241782011-01-07 17:09:48 +00003216 ret = i915_gem_object_flush_gpu_write_domain(obj);
3217 if (ret)
3218 return ret;
3219
Chris Wilsonce453d82011-02-21 14:43:56 +00003220 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003221 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003222 return ret;
Daniel Vetterde18a292010-11-27 22:30:41 +01003223
Eric Anholte47c68e2008-11-14 13:35:19 -08003224 i915_gem_object_flush_gtt_write_domain(obj);
3225
3226 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003227 if (obj->page_cpu_valid == NULL &&
3228 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003229 return 0;
3230
Eric Anholte47c68e2008-11-14 13:35:19 -08003231 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3232 * newly adding I915_GEM_DOMAIN_CPU
3233 */
Chris Wilson05394f32010-11-08 19:18:58 +00003234 if (obj->page_cpu_valid == NULL) {
3235 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3236 GFP_KERNEL);
3237 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003238 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003239 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3240 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003241
3242 /* Flush the cache on any pages that are still invalid from the CPU's
3243 * perspective.
3244 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003245 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3246 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003247 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003248 continue;
3249
Chris Wilson05394f32010-11-08 19:18:58 +00003250 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003251
Chris Wilson05394f32010-11-08 19:18:58 +00003252 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003253 }
3254
Eric Anholte47c68e2008-11-14 13:35:19 -08003255 /* It should now be out of any other write domains, and we can update
3256 * the domain values for our changes.
3257 */
Chris Wilson05394f32010-11-08 19:18:58 +00003258 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003259
Chris Wilson05394f32010-11-08 19:18:58 +00003260 old_read_domains = obj->base.read_domains;
3261 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003262
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003263 trace_i915_gem_object_change_domain(obj,
3264 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003265 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003266
Eric Anholt673a3942008-07-30 12:06:12 -07003267 return 0;
3268}
3269
Eric Anholt673a3942008-07-30 12:06:12 -07003270/* Throttle our rendering by waiting until the ring has completed our requests
3271 * emitted over 20 msec ago.
3272 *
Eric Anholtb9624422009-06-03 07:27:35 +00003273 * Note that if we were to use the current jiffies each time around the loop,
3274 * we wouldn't escape the function with any frames outstanding if the time to
3275 * render a frame was over 20ms.
3276 *
Eric Anholt673a3942008-07-30 12:06:12 -07003277 * This should get us reasonable parallelism between CPU and GPU but also
3278 * relatively low latency when blocking on a particular request to finish.
3279 */
3280static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003281i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003282{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003283 struct drm_i915_private *dev_priv = dev->dev_private;
3284 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003285 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003286 struct drm_i915_gem_request *request;
3287 struct intel_ring_buffer *ring = NULL;
3288 u32 seqno = 0;
3289 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003290
Chris Wilsone110e8d2011-01-26 15:39:14 +00003291 if (atomic_read(&dev_priv->mm.wedged))
3292 return -EIO;
3293
Chris Wilson1c255952010-09-26 11:03:27 +01003294 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003295 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003296 if (time_after_eq(request->emitted_jiffies, recent_enough))
3297 break;
3298
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003299 ring = request->ring;
3300 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003301 }
Chris Wilson1c255952010-09-26 11:03:27 +01003302 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003303
3304 if (seqno == 0)
3305 return 0;
3306
3307 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003308 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003309 /* And wait for the seqno passing without holding any locks and
3310 * causing extra latency for others. This is safe as the irq
3311 * generation is designed to be run atomically and so is
3312 * lockless.
3313 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003314 if (ring->irq_get(ring)) {
3315 ret = wait_event_interruptible(ring->irq_queue,
3316 i915_seqno_passed(ring->get_seqno(ring), seqno)
3317 || atomic_read(&dev_priv->mm.wedged));
3318 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003319
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003320 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3321 ret = -EIO;
Eric Anholte959b5d2011-12-22 14:55:01 -08003322 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
3323 seqno) ||
Eric Anholt7ea29b12011-12-22 14:54:59 -08003324 atomic_read(&dev_priv->mm.wedged), 3000)) {
3325 ret = -EBUSY;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003326 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003327 }
3328
3329 if (ret == 0)
3330 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003331
Eric Anholt673a3942008-07-30 12:06:12 -07003332 return ret;
3333}
3334
Eric Anholt673a3942008-07-30 12:06:12 -07003335int
Chris Wilson05394f32010-11-08 19:18:58 +00003336i915_gem_object_pin(struct drm_i915_gem_object *obj,
3337 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003338 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003339{
Chris Wilson05394f32010-11-08 19:18:58 +00003340 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003341 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003342 int ret;
3343
Chris Wilson05394f32010-11-08 19:18:58 +00003344 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003345 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003346
Chris Wilson05394f32010-11-08 19:18:58 +00003347 if (obj->gtt_space != NULL) {
3348 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3349 (map_and_fenceable && !obj->map_and_fenceable)) {
3350 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003351 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003352 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3353 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003354 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003355 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003356 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003357 ret = i915_gem_object_unbind(obj);
3358 if (ret)
3359 return ret;
3360 }
3361 }
3362
Chris Wilson05394f32010-11-08 19:18:58 +00003363 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003364 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003365 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003366 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003367 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003368 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003369
Chris Wilson05394f32010-11-08 19:18:58 +00003370 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003371 if (!obj->active)
3372 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003373 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003374 }
Chris Wilson6299f992010-11-24 12:23:44 +00003375 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003376
Chris Wilson23bc5982010-09-29 16:10:57 +01003377 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003378 return 0;
3379}
3380
3381void
Chris Wilson05394f32010-11-08 19:18:58 +00003382i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003383{
Chris Wilson05394f32010-11-08 19:18:58 +00003384 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003385 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003386
Chris Wilson23bc5982010-09-29 16:10:57 +01003387 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003388 BUG_ON(obj->pin_count == 0);
3389 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003390
Chris Wilson05394f32010-11-08 19:18:58 +00003391 if (--obj->pin_count == 0) {
3392 if (!obj->active)
3393 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003394 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003395 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003396 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003397 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003398}
3399
3400int
3401i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003402 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003403{
3404 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003405 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003406 int ret;
3407
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003408 ret = i915_mutex_lock_interruptible(dev);
3409 if (ret)
3410 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003411
Chris Wilson05394f32010-11-08 19:18:58 +00003412 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003413 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003414 ret = -ENOENT;
3415 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003416 }
Eric Anholt673a3942008-07-30 12:06:12 -07003417
Chris Wilson05394f32010-11-08 19:18:58 +00003418 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003419 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003420 ret = -EINVAL;
3421 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003422 }
3423
Chris Wilson05394f32010-11-08 19:18:58 +00003424 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003425 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3426 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003427 ret = -EINVAL;
3428 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003429 }
3430
Chris Wilson05394f32010-11-08 19:18:58 +00003431 obj->user_pin_count++;
3432 obj->pin_filp = file;
3433 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003434 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003435 if (ret)
3436 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003437 }
3438
3439 /* XXX - flush the CPU caches for pinned objects
3440 * as the X server doesn't manage domains yet
3441 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003442 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003443 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003444out:
Chris Wilson05394f32010-11-08 19:18:58 +00003445 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003446unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003447 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003448 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003449}
3450
3451int
3452i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003453 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003454{
3455 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003456 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003457 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003458
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003459 ret = i915_mutex_lock_interruptible(dev);
3460 if (ret)
3461 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003462
Chris Wilson05394f32010-11-08 19:18:58 +00003463 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003464 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003465 ret = -ENOENT;
3466 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003467 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003468
Chris Wilson05394f32010-11-08 19:18:58 +00003469 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003470 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3471 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003472 ret = -EINVAL;
3473 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003474 }
Chris Wilson05394f32010-11-08 19:18:58 +00003475 obj->user_pin_count--;
3476 if (obj->user_pin_count == 0) {
3477 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003478 i915_gem_object_unpin(obj);
3479 }
Eric Anholt673a3942008-07-30 12:06:12 -07003480
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003481out:
Chris Wilson05394f32010-11-08 19:18:58 +00003482 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003483unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003484 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003485 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003486}
3487
3488int
3489i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003490 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003491{
3492 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003493 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003494 int ret;
3495
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003496 ret = i915_mutex_lock_interruptible(dev);
3497 if (ret)
3498 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003499
Chris Wilson05394f32010-11-08 19:18:58 +00003500 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003501 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003502 ret = -ENOENT;
3503 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003504 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003505
Chris Wilson0be555b2010-08-04 15:36:30 +01003506 /* Count all active objects as busy, even if they are currently not used
3507 * by the gpu. Users of this interface expect objects to eventually
3508 * become non-busy without any further actions, therefore emit any
3509 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003510 */
Chris Wilson05394f32010-11-08 19:18:58 +00003511 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003512 if (args->busy) {
3513 /* Unconditionally flush objects, even when the gpu still uses this
3514 * object. Userspace calling this function indicates that it wants to
3515 * use this buffer rather sooner than later, so issuing the required
3516 * flush earlier is beneficial.
3517 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003518 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003519 ret = i915_gem_flush_ring(obj->ring,
Chris Wilson88241782011-01-07 17:09:48 +00003520 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003521 } else if (obj->ring->outstanding_lazy_request ==
3522 obj->last_rendering_seqno) {
3523 struct drm_i915_gem_request *request;
3524
Chris Wilson7a194872010-12-07 10:38:40 +00003525 /* This ring is not being cleared by active usage,
3526 * so emit a request to do so.
3527 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003528 request = kzalloc(sizeof(*request), GFP_KERNEL);
Rakib Mullick457eafc2011-11-16 00:49:28 +06003529 if (request) {
Akshay Joshi0206e352011-08-16 15:34:10 -04003530 ret = i915_add_request(obj->ring, NULL, request);
Rakib Mullick457eafc2011-11-16 00:49:28 +06003531 if (ret)
3532 kfree(request);
3533 } else
Chris Wilson7a194872010-12-07 10:38:40 +00003534 ret = -ENOMEM;
3535 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003536
3537 /* Update the active list for the hardware's current position.
3538 * Otherwise this only updates on a delayed timer or when irqs
3539 * are actually unmasked, and our working set ends up being
3540 * larger than required.
3541 */
Chris Wilsondb53a302011-02-03 11:57:46 +00003542 i915_gem_retire_requests_ring(obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003543
Chris Wilson05394f32010-11-08 19:18:58 +00003544 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003545 }
Eric Anholt673a3942008-07-30 12:06:12 -07003546
Chris Wilson05394f32010-11-08 19:18:58 +00003547 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003548unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003549 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003550 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003551}
3552
3553int
3554i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3555 struct drm_file *file_priv)
3556{
Akshay Joshi0206e352011-08-16 15:34:10 -04003557 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003558}
3559
Chris Wilson3ef94da2009-09-14 16:50:29 +01003560int
3561i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3562 struct drm_file *file_priv)
3563{
3564 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003565 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003566 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003567
3568 switch (args->madv) {
3569 case I915_MADV_DONTNEED:
3570 case I915_MADV_WILLNEED:
3571 break;
3572 default:
3573 return -EINVAL;
3574 }
3575
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003576 ret = i915_mutex_lock_interruptible(dev);
3577 if (ret)
3578 return ret;
3579
Chris Wilson05394f32010-11-08 19:18:58 +00003580 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003581 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003582 ret = -ENOENT;
3583 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003584 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003585
Chris Wilson05394f32010-11-08 19:18:58 +00003586 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003587 ret = -EINVAL;
3588 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003589 }
3590
Chris Wilson05394f32010-11-08 19:18:58 +00003591 if (obj->madv != __I915_MADV_PURGED)
3592 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003593
Chris Wilson2d7ef392009-09-20 23:13:10 +01003594 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003595 if (i915_gem_object_is_purgeable(obj) &&
3596 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003597 i915_gem_object_truncate(obj);
3598
Chris Wilson05394f32010-11-08 19:18:58 +00003599 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003600
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003601out:
Chris Wilson05394f32010-11-08 19:18:58 +00003602 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003603unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003604 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003605 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003606}
3607
Chris Wilson05394f32010-11-08 19:18:58 +00003608struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3609 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003610{
Chris Wilson73aa8082010-09-30 11:46:12 +01003611 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003612 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003613 struct address_space *mapping;
Daniel Vetterc397b902010-04-09 19:05:07 +00003614
3615 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3616 if (obj == NULL)
3617 return NULL;
3618
3619 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3620 kfree(obj);
3621 return NULL;
3622 }
3623
Hugh Dickins5949eac2011-06-27 16:18:18 -07003624 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3625 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3626
Chris Wilson73aa8082010-09-30 11:46:12 +01003627 i915_gem_info_add_obj(dev_priv, size);
3628
Daniel Vetterc397b902010-04-09 19:05:07 +00003629 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3630 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3631
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02003632 if (HAS_LLC(dev)) {
3633 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07003634 * cache) for about a 10% performance improvement
3635 * compared to uncached. Graphics requests other than
3636 * display scanout are coherent with the CPU in
3637 * accessing this cache. This means in this mode we
3638 * don't need to clflush on the CPU side, and on the
3639 * GPU side we only need to flush internal caches to
3640 * get data visible to the CPU.
3641 *
3642 * However, we maintain the display planes as UC, and so
3643 * need to rebind when first used as such.
3644 */
3645 obj->cache_level = I915_CACHE_LLC;
3646 } else
3647 obj->cache_level = I915_CACHE_NONE;
3648
Daniel Vetter62b8b212010-04-09 19:05:08 +00003649 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003650 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003651 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003652 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003653 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003654 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003655 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003656 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003657 /* Avoid an unnecessary call to unbind on the first bind. */
3658 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003659
Chris Wilson05394f32010-11-08 19:18:58 +00003660 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003661}
3662
Eric Anholt673a3942008-07-30 12:06:12 -07003663int i915_gem_init_object(struct drm_gem_object *obj)
3664{
Daniel Vetterc397b902010-04-09 19:05:07 +00003665 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003666
Eric Anholt673a3942008-07-30 12:06:12 -07003667 return 0;
3668}
3669
Chris Wilson05394f32010-11-08 19:18:58 +00003670static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003671{
Chris Wilson05394f32010-11-08 19:18:58 +00003672 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003673 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003674 int ret;
3675
3676 ret = i915_gem_object_unbind(obj);
3677 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003678 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003679 &dev_priv->mm.deferred_free_list);
3680 return;
3681 }
3682
Chris Wilson26e12f892011-03-20 11:20:19 +00003683 trace_i915_gem_object_destroy(obj);
3684
Chris Wilson05394f32010-11-08 19:18:58 +00003685 if (obj->base.map_list.map)
Rob Clarkb464e9a2011-08-10 08:09:08 -05003686 drm_gem_free_mmap_offset(&obj->base);
Chris Wilsonbe726152010-07-23 23:18:50 +01003687
Chris Wilson05394f32010-11-08 19:18:58 +00003688 drm_gem_object_release(&obj->base);
3689 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003690
Chris Wilson05394f32010-11-08 19:18:58 +00003691 kfree(obj->page_cpu_valid);
3692 kfree(obj->bit_17);
3693 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003694}
3695
Chris Wilson05394f32010-11-08 19:18:58 +00003696void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003697{
Chris Wilson05394f32010-11-08 19:18:58 +00003698 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3699 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003700
Chris Wilson05394f32010-11-08 19:18:58 +00003701 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003702 i915_gem_object_unpin(obj);
3703
Chris Wilson05394f32010-11-08 19:18:58 +00003704 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003705 i915_gem_detach_phys_object(dev, obj);
3706
Chris Wilsonbe726152010-07-23 23:18:50 +01003707 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003708}
3709
Jesse Barnes5669fca2009-02-17 15:13:31 -08003710int
Eric Anholt673a3942008-07-30 12:06:12 -07003711i915_gem_idle(struct drm_device *dev)
3712{
3713 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003714 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003715
Keith Packard6dbe2772008-10-14 21:41:13 -07003716 mutex_lock(&dev->struct_mutex);
3717
Chris Wilson87acb0a2010-10-19 10:13:00 +01003718 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003719 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003720 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003721 }
Eric Anholt673a3942008-07-30 12:06:12 -07003722
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08003723 ret = i915_gpu_idle(dev, true);
Keith Packard6dbe2772008-10-14 21:41:13 -07003724 if (ret) {
3725 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003726 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003727 }
Eric Anholt673a3942008-07-30 12:06:12 -07003728
Chris Wilson29105cc2010-01-07 10:39:13 +00003729 /* Under UMS, be paranoid and evict. */
3730 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003731 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003732 if (ret) {
3733 mutex_unlock(&dev->struct_mutex);
3734 return ret;
3735 }
3736 }
3737
Chris Wilson312817a2010-11-22 11:50:11 +00003738 i915_gem_reset_fences(dev);
3739
Chris Wilson29105cc2010-01-07 10:39:13 +00003740 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3741 * We need to replace this with a semaphore, or something.
3742 * And not confound mm.suspended!
3743 */
3744 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003745 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003746
3747 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003748 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003749
Keith Packard6dbe2772008-10-14 21:41:13 -07003750 mutex_unlock(&dev->struct_mutex);
3751
Chris Wilson29105cc2010-01-07 10:39:13 +00003752 /* Cancel the retire work handler, which should be idle now. */
3753 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3754
Eric Anholt673a3942008-07-30 12:06:12 -07003755 return 0;
3756}
3757
Eric Anholt673a3942008-07-30 12:06:12 -07003758int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003759i915_gem_init_ringbuffer(struct drm_device *dev)
3760{
3761 drm_i915_private_t *dev_priv = dev->dev_private;
3762 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003763
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003764 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003765 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003766 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003767
3768 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003769 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003770 if (ret)
3771 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003772 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003773
Chris Wilson549f7362010-10-19 11:19:32 +01003774 if (HAS_BLT(dev)) {
3775 ret = intel_init_blt_ring_buffer(dev);
3776 if (ret)
3777 goto cleanup_bsd_ring;
3778 }
3779
Chris Wilson6f392d5482010-08-07 11:01:22 +01003780 dev_priv->next_seqno = 1;
3781
Chris Wilson68f95ba2010-05-27 13:18:22 +01003782 return 0;
3783
Chris Wilson549f7362010-10-19 11:19:32 +01003784cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003785 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003786cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003787 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003788 return ret;
3789}
3790
3791void
3792i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3793{
3794 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003795 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003796
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003797 for (i = 0; i < I915_NUM_RINGS; i++)
3798 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003799}
3800
3801int
Eric Anholt673a3942008-07-30 12:06:12 -07003802i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3803 struct drm_file *file_priv)
3804{
3805 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003806 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003807
Jesse Barnes79e53942008-11-07 14:24:08 -08003808 if (drm_core_check_feature(dev, DRIVER_MODESET))
3809 return 0;
3810
Ben Gamariba1234d2009-09-14 17:48:47 -04003811 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003812 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003813 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003814 }
3815
Eric Anholt673a3942008-07-30 12:06:12 -07003816 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003817 dev_priv->mm.suspended = 0;
3818
3819 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003820 if (ret != 0) {
3821 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003822 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003823 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003824
Chris Wilson69dc4982010-10-19 10:36:51 +01003825 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003826 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3827 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003828 for (i = 0; i < I915_NUM_RINGS; i++) {
3829 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3830 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3831 }
Eric Anholt673a3942008-07-30 12:06:12 -07003832 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003833
Chris Wilson5f353082010-06-07 14:03:03 +01003834 ret = drm_irq_install(dev);
3835 if (ret)
3836 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003837
Eric Anholt673a3942008-07-30 12:06:12 -07003838 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003839
3840cleanup_ringbuffer:
3841 mutex_lock(&dev->struct_mutex);
3842 i915_gem_cleanup_ringbuffer(dev);
3843 dev_priv->mm.suspended = 1;
3844 mutex_unlock(&dev->struct_mutex);
3845
3846 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003847}
3848
3849int
3850i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3851 struct drm_file *file_priv)
3852{
Jesse Barnes79e53942008-11-07 14:24:08 -08003853 if (drm_core_check_feature(dev, DRIVER_MODESET))
3854 return 0;
3855
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003856 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003857 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003858}
3859
3860void
3861i915_gem_lastclose(struct drm_device *dev)
3862{
3863 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003864
Eric Anholte806b492009-01-22 09:56:58 -08003865 if (drm_core_check_feature(dev, DRIVER_MODESET))
3866 return;
3867
Keith Packard6dbe2772008-10-14 21:41:13 -07003868 ret = i915_gem_idle(dev);
3869 if (ret)
3870 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003871}
3872
Chris Wilson64193402010-10-24 12:38:05 +01003873static void
3874init_ring_lists(struct intel_ring_buffer *ring)
3875{
3876 INIT_LIST_HEAD(&ring->active_list);
3877 INIT_LIST_HEAD(&ring->request_list);
3878 INIT_LIST_HEAD(&ring->gpu_write_list);
3879}
3880
Eric Anholt673a3942008-07-30 12:06:12 -07003881void
3882i915_gem_load(struct drm_device *dev)
3883{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003884 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003885 drm_i915_private_t *dev_priv = dev->dev_private;
3886
Chris Wilson69dc4982010-10-19 10:36:51 +01003887 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003888 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3889 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003890 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003891 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003892 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003893 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003894 for (i = 0; i < I915_NUM_RINGS; i++)
3895 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02003896 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003897 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003898 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3899 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003900 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003901
Dave Airlie94400122010-07-20 13:15:31 +10003902 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3903 if (IS_GEN3(dev)) {
3904 u32 tmp = I915_READ(MI_ARB_STATE);
3905 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3906 /* arb state is a masked write, so set bit + bit in mask */
3907 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3908 I915_WRITE(MI_ARB_STATE, tmp);
3909 }
3910 }
3911
Chris Wilson72bfa192010-12-19 11:42:05 +00003912 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3913
Jesse Barnesde151cf2008-11-12 10:03:55 -08003914 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003915 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3916 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003917
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003918 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003919 dev_priv->num_fence_regs = 16;
3920 else
3921 dev_priv->num_fence_regs = 8;
3922
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003923 /* Initialize fence registers to zero */
Eric Anholt10ed13e2011-05-06 13:53:49 -07003924 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3925 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003926 }
Eric Anholt10ed13e2011-05-06 13:53:49 -07003927
Eric Anholt673a3942008-07-30 12:06:12 -07003928 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003929 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01003930
Chris Wilsonce453d82011-02-21 14:43:56 +00003931 dev_priv->mm.interruptible = true;
3932
Chris Wilson17250b72010-10-28 12:51:39 +01003933 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3934 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3935 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07003936}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003937
3938/*
3939 * Create a physically contiguous memory object for this object
3940 * e.g. for cursor + overlay regs
3941 */
Chris Wilson995b6762010-08-20 13:23:26 +01003942static int i915_gem_init_phys_object(struct drm_device *dev,
3943 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003944{
3945 drm_i915_private_t *dev_priv = dev->dev_private;
3946 struct drm_i915_gem_phys_object *phys_obj;
3947 int ret;
3948
3949 if (dev_priv->mm.phys_objs[id - 1] || !size)
3950 return 0;
3951
Eric Anholt9a298b22009-03-24 12:23:04 -07003952 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003953 if (!phys_obj)
3954 return -ENOMEM;
3955
3956 phys_obj->id = id;
3957
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003958 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003959 if (!phys_obj->handle) {
3960 ret = -ENOMEM;
3961 goto kfree_obj;
3962 }
3963#ifdef CONFIG_X86
3964 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3965#endif
3966
3967 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3968
3969 return 0;
3970kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07003971 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003972 return ret;
3973}
3974
Chris Wilson995b6762010-08-20 13:23:26 +01003975static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003976{
3977 drm_i915_private_t *dev_priv = dev->dev_private;
3978 struct drm_i915_gem_phys_object *phys_obj;
3979
3980 if (!dev_priv->mm.phys_objs[id - 1])
3981 return;
3982
3983 phys_obj = dev_priv->mm.phys_objs[id - 1];
3984 if (phys_obj->cur_obj) {
3985 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3986 }
3987
3988#ifdef CONFIG_X86
3989 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3990#endif
3991 drm_pci_free(dev, phys_obj->handle);
3992 kfree(phys_obj);
3993 dev_priv->mm.phys_objs[id - 1] = NULL;
3994}
3995
3996void i915_gem_free_all_phys_object(struct drm_device *dev)
3997{
3998 int i;
3999
Dave Airlie260883c2009-01-22 17:58:49 +10004000 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004001 i915_gem_free_phys_object(dev, i);
4002}
4003
4004void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004005 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004006{
Chris Wilson05394f32010-11-08 19:18:58 +00004007 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004008 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004009 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004010 int page_count;
4011
Chris Wilson05394f32010-11-08 19:18:58 +00004012 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004013 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004014 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004015
Chris Wilson05394f32010-11-08 19:18:58 +00004016 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004017 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07004018 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004019 if (!IS_ERR(page)) {
4020 char *dst = kmap_atomic(page);
4021 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4022 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004023
Chris Wilsone5281cc2010-10-28 13:45:36 +01004024 drm_clflush_pages(&page, 1);
4025
4026 set_page_dirty(page);
4027 mark_page_accessed(page);
4028 page_cache_release(page);
4029 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004030 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004031 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004032
Chris Wilson05394f32010-11-08 19:18:58 +00004033 obj->phys_obj->cur_obj = NULL;
4034 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004035}
4036
4037int
4038i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004039 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004040 int id,
4041 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004042{
Chris Wilson05394f32010-11-08 19:18:58 +00004043 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004044 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004045 int ret = 0;
4046 int page_count;
4047 int i;
4048
4049 if (id > I915_MAX_PHYS_OBJECT)
4050 return -EINVAL;
4051
Chris Wilson05394f32010-11-08 19:18:58 +00004052 if (obj->phys_obj) {
4053 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004054 return 0;
4055 i915_gem_detach_phys_object(dev, obj);
4056 }
4057
Dave Airlie71acb5e2008-12-30 20:31:46 +10004058 /* create a new object */
4059 if (!dev_priv->mm.phys_objs[id - 1]) {
4060 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004061 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004062 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004063 DRM_ERROR("failed to init phys object %d size: %zu\n",
4064 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004065 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004066 }
4067 }
4068
4069 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004070 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4071 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004072
Chris Wilson05394f32010-11-08 19:18:58 +00004073 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004074
4075 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004076 struct page *page;
4077 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004078
Hugh Dickins5949eac2011-06-27 16:18:18 -07004079 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004080 if (IS_ERR(page))
4081 return PTR_ERR(page);
4082
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004083 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004084 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004085 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004086 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004087
4088 mark_page_accessed(page);
4089 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004090 }
4091
4092 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004093}
4094
4095static int
Chris Wilson05394f32010-11-08 19:18:58 +00004096i915_gem_phys_pwrite(struct drm_device *dev,
4097 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004098 struct drm_i915_gem_pwrite *args,
4099 struct drm_file *file_priv)
4100{
Chris Wilson05394f32010-11-08 19:18:58 +00004101 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004102 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004103
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004104 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4105 unsigned long unwritten;
4106
4107 /* The physical object once assigned is fixed for the lifetime
4108 * of the obj, so we can safely drop the lock and continue
4109 * to access vaddr.
4110 */
4111 mutex_unlock(&dev->struct_mutex);
4112 unwritten = copy_from_user(vaddr, user_data, args->size);
4113 mutex_lock(&dev->struct_mutex);
4114 if (unwritten)
4115 return -EFAULT;
4116 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004117
Daniel Vetter40ce6572010-11-05 18:12:18 +01004118 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004119 return 0;
4120}
Eric Anholtb9624422009-06-03 07:27:35 +00004121
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004122void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004123{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004124 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004125
4126 /* Clean up our request list when the client is going away, so that
4127 * later retire_requests won't dereference our soon-to-be-gone
4128 * file_priv.
4129 */
Chris Wilson1c255952010-09-26 11:03:27 +01004130 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004131 while (!list_empty(&file_priv->mm.request_list)) {
4132 struct drm_i915_gem_request *request;
4133
4134 request = list_first_entry(&file_priv->mm.request_list,
4135 struct drm_i915_gem_request,
4136 client_list);
4137 list_del(&request->client_list);
4138 request->file_priv = NULL;
4139 }
Chris Wilson1c255952010-09-26 11:03:27 +01004140 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004141}
Chris Wilson31169712009-09-14 16:50:28 +01004142
Chris Wilson31169712009-09-14 16:50:28 +01004143static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004144i915_gpu_is_active(struct drm_device *dev)
4145{
4146 drm_i915_private_t *dev_priv = dev->dev_private;
4147 int lists_empty;
4148
Chris Wilson1637ef42010-04-20 17:10:35 +01004149 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004150 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004151
4152 return !lists_empty;
4153}
4154
4155static int
Ying Han1495f232011-05-24 17:12:27 -07004156i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004157{
Chris Wilson17250b72010-10-28 12:51:39 +01004158 struct drm_i915_private *dev_priv =
4159 container_of(shrinker,
4160 struct drm_i915_private,
4161 mm.inactive_shrinker);
4162 struct drm_device *dev = dev_priv->dev;
4163 struct drm_i915_gem_object *obj, *next;
Ying Han1495f232011-05-24 17:12:27 -07004164 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01004165 int cnt;
4166
4167 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004168 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004169
4170 /* "fast-path" to count number of available objects */
4171 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01004172 cnt = 0;
4173 list_for_each_entry(obj,
4174 &dev_priv->mm.inactive_list,
4175 mm_list)
4176 cnt++;
4177 mutex_unlock(&dev->struct_mutex);
4178 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004179 }
4180
Chris Wilson1637ef42010-04-20 17:10:35 +01004181rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004182 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01004183 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01004184
Chris Wilson17250b72010-10-28 12:51:39 +01004185 list_for_each_entry_safe(obj, next,
4186 &dev_priv->mm.inactive_list,
4187 mm_list) {
4188 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00004189 if (i915_gem_object_unbind(obj) == 0 &&
4190 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004191 break;
Chris Wilson31169712009-09-14 16:50:28 +01004192 }
Chris Wilson31169712009-09-14 16:50:28 +01004193 }
4194
4195 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01004196 cnt = 0;
4197 list_for_each_entry_safe(obj, next,
4198 &dev_priv->mm.inactive_list,
4199 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00004200 if (nr_to_scan &&
4201 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004202 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00004203 else
Chris Wilson17250b72010-10-28 12:51:39 +01004204 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01004205 }
4206
Chris Wilson17250b72010-10-28 12:51:39 +01004207 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01004208 /*
4209 * We are desperate for pages, so as a last resort, wait
4210 * for the GPU to finish and discard whatever we can.
4211 * This has a dramatic impact to reduce the number of
4212 * OOM-killer events whilst running the GPU aggressively.
4213 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08004214 if (i915_gpu_idle(dev, true) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01004215 goto rescan;
4216 }
Chris Wilson17250b72010-10-28 12:51:39 +01004217 mutex_unlock(&dev->struct_mutex);
4218 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004219}