blob: 92b097dbe4ffd49bba069d79f58d62c3c99dbb81 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080037#include <linux/intel-gtt.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Daniel Vetter0108a3e2010-08-07 11:01:21 +010039static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010040
41static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
42 bool pipelined);
Eric Anholte47c68e2008-11-14 13:35:19 -080043static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
44static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080045static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
46 int write);
47static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
48 uint64_t offset,
49 uint64_t size);
50static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +010051static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
52 bool interruptible);
Jesse Barnesde151cf2008-11-12 10:03:55 -080053static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
54 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080055static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100056static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
57 struct drm_i915_gem_pwrite *args,
58 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010059static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070060
Chris Wilson5cdf5882010-09-27 15:51:07 +010061static int
62i915_gem_object_get_pages(struct drm_gem_object *obj,
63 gfp_t gfpmask);
64
65static void
66i915_gem_object_put_pages(struct drm_gem_object *obj);
67
Chris Wilson31169712009-09-14 16:50:28 +010068static LIST_HEAD(shrink_list);
69static DEFINE_SPINLOCK(shrink_list_lock);
70
Chris Wilson73aa8082010-09-30 11:46:12 +010071/* some bookkeeping */
72static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
73 size_t size)
74{
75 dev_priv->mm.object_count++;
76 dev_priv->mm.object_memory += size;
77}
78
79static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
80 size_t size)
81{
82 dev_priv->mm.object_count--;
83 dev_priv->mm.object_memory -= size;
84}
85
86static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
87 size_t size)
88{
89 dev_priv->mm.gtt_count++;
90 dev_priv->mm.gtt_memory += size;
91}
92
93static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
94 size_t size)
95{
96 dev_priv->mm.gtt_count--;
97 dev_priv->mm.gtt_memory -= size;
98}
99
100static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
101 size_t size)
102{
103 dev_priv->mm.pin_count++;
104 dev_priv->mm.pin_memory += size;
105}
106
107static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
108 size_t size)
109{
110 dev_priv->mm.pin_count--;
111 dev_priv->mm.pin_memory -= size;
112}
113
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100114int
115i915_gem_check_is_wedged(struct drm_device *dev)
116{
117 struct drm_i915_private *dev_priv = dev->dev_private;
118 struct completion *x = &dev_priv->error_completion;
119 unsigned long flags;
120 int ret;
121
122 if (!atomic_read(&dev_priv->mm.wedged))
123 return 0;
124
125 ret = wait_for_completion_interruptible(x);
126 if (ret)
127 return ret;
128
129 /* Success, we reset the GPU! */
130 if (!atomic_read(&dev_priv->mm.wedged))
131 return 0;
132
133 /* GPU is hung, bump the completion count to account for
134 * the token we just consumed so that we never hit zero and
135 * end up waiting upon a subsequent completion event that
136 * will never happen.
137 */
138 spin_lock_irqsave(&x->wait.lock, flags);
139 x->done++;
140 spin_unlock_irqrestore(&x->wait.lock, flags);
141 return -EIO;
142}
143
Chris Wilson76c1dec2010-09-25 11:22:51 +0100144static int i915_mutex_lock_interruptible(struct drm_device *dev)
145{
146 struct drm_i915_private *dev_priv = dev->dev_private;
147 int ret;
148
149 ret = i915_gem_check_is_wedged(dev);
150 if (ret)
151 return ret;
152
153 ret = mutex_lock_interruptible(&dev->struct_mutex);
154 if (ret)
155 return ret;
156
157 if (atomic_read(&dev_priv->mm.wedged)) {
158 mutex_unlock(&dev->struct_mutex);
159 return -EAGAIN;
160 }
161
Chris Wilson23bc5982010-09-29 16:10:57 +0100162 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100163 return 0;
164}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100165
Chris Wilson7d1c4802010-08-07 21:45:03 +0100166static inline bool
167i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
168{
169 return obj_priv->gtt_space &&
170 !obj_priv->active &&
171 obj_priv->pin_count == 0;
172}
173
Chris Wilson73aa8082010-09-30 11:46:12 +0100174int i915_gem_do_init(struct drm_device *dev,
175 unsigned long start,
Jesse Barnes79e53942008-11-07 14:24:08 -0800176 unsigned long end)
177{
178 drm_i915_private_t *dev_priv = dev->dev_private;
179
180 if (start >= end ||
181 (start & (PAGE_SIZE - 1)) != 0 ||
182 (end & (PAGE_SIZE - 1)) != 0) {
183 return -EINVAL;
184 }
185
186 drm_mm_init(&dev_priv->mm.gtt_space, start,
187 end - start);
188
Chris Wilson73aa8082010-09-30 11:46:12 +0100189 dev_priv->mm.gtt_total = end - start;
Jesse Barnes79e53942008-11-07 14:24:08 -0800190
191 return 0;
192}
Keith Packard6dbe2772008-10-14 21:41:13 -0700193
Eric Anholt673a3942008-07-30 12:06:12 -0700194int
195i915_gem_init_ioctl(struct drm_device *dev, void *data,
196 struct drm_file *file_priv)
197{
Eric Anholt673a3942008-07-30 12:06:12 -0700198 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800199 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700200
201 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -0800202 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700203 mutex_unlock(&dev->struct_mutex);
204
Jesse Barnes79e53942008-11-07 14:24:08 -0800205 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700206}
207
Eric Anholt5a125c32008-10-22 21:40:13 -0700208int
209i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
210 struct drm_file *file_priv)
211{
Chris Wilson73aa8082010-09-30 11:46:12 +0100212 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700213 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700214
215 if (!(dev->driver->driver_features & DRIVER_GEM))
216 return -ENODEV;
217
Chris Wilson73aa8082010-09-30 11:46:12 +0100218 mutex_lock(&dev->struct_mutex);
219 args->aper_size = dev_priv->mm.gtt_total;
220 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
221 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700222
223 return 0;
224}
225
Eric Anholt673a3942008-07-30 12:06:12 -0700226
227/**
228 * Creates a new mm object and returns a handle to it.
229 */
230int
231i915_gem_create_ioctl(struct drm_device *dev, void *data,
232 struct drm_file *file_priv)
233{
234 struct drm_i915_gem_create *args = data;
235 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300236 int ret;
237 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700238
239 args->size = roundup(args->size, PAGE_SIZE);
240
241 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000242 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700243 if (obj == NULL)
244 return -ENOMEM;
245
246 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100247 if (ret) {
Chris Wilson202f2fe2010-10-14 13:20:40 +0100248 drm_gem_object_release(obj);
249 i915_gem_info_remove_obj(dev->dev_private, obj->size);
250 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700251 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100252 }
253
Chris Wilson202f2fe2010-10-14 13:20:40 +0100254 /* drop reference from allocate - handle holds it now */
255 drm_gem_object_unreference(obj);
256 trace_i915_gem_object_create(obj);
257
Eric Anholt673a3942008-07-30 12:06:12 -0700258 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700259 return 0;
260}
261
Eric Anholt40123c12009-03-09 13:42:30 -0700262static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700263fast_shmem_read(struct page **pages,
264 loff_t page_base, int page_offset,
265 char __user *data,
266 int length)
267{
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100268 char *vaddr;
Chris Wilson4f27b752010-10-14 15:26:45 +0100269 int ret;
Eric Anholteb014592009-03-10 11:44:52 -0700270
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700271 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT]);
Chris Wilson4f27b752010-10-14 15:26:45 +0100272 ret = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700273 kunmap_atomic(vaddr);
Eric Anholteb014592009-03-10 11:44:52 -0700274
Chris Wilson4f27b752010-10-14 15:26:45 +0100275 return ret;
Eric Anholteb014592009-03-10 11:44:52 -0700276}
277
Eric Anholt280b7132009-03-12 16:56:27 -0700278static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
279{
280 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100281 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700282
283 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
284 obj_priv->tiling_mode != I915_TILING_NONE;
285}
286
Chris Wilson99a03df2010-05-27 14:15:34 +0100287static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700288slow_shmem_copy(struct page *dst_page,
289 int dst_offset,
290 struct page *src_page,
291 int src_offset,
292 int length)
293{
294 char *dst_vaddr, *src_vaddr;
295
Chris Wilson99a03df2010-05-27 14:15:34 +0100296 dst_vaddr = kmap(dst_page);
297 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700298
299 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
300
Chris Wilson99a03df2010-05-27 14:15:34 +0100301 kunmap(src_page);
302 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700303}
304
Chris Wilson99a03df2010-05-27 14:15:34 +0100305static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700306slow_shmem_bit17_copy(struct page *gpu_page,
307 int gpu_offset,
308 struct page *cpu_page,
309 int cpu_offset,
310 int length,
311 int is_read)
312{
313 char *gpu_vaddr, *cpu_vaddr;
314
315 /* Use the unswizzled path if this page isn't affected. */
316 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
317 if (is_read)
318 return slow_shmem_copy(cpu_page, cpu_offset,
319 gpu_page, gpu_offset, length);
320 else
321 return slow_shmem_copy(gpu_page, gpu_offset,
322 cpu_page, cpu_offset, length);
323 }
324
Chris Wilson99a03df2010-05-27 14:15:34 +0100325 gpu_vaddr = kmap(gpu_page);
326 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700327
328 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
329 * XORing with the other bits (A9 for Y, A9 and A10 for X)
330 */
331 while (length > 0) {
332 int cacheline_end = ALIGN(gpu_offset + 1, 64);
333 int this_length = min(cacheline_end - gpu_offset, length);
334 int swizzled_gpu_offset = gpu_offset ^ 64;
335
336 if (is_read) {
337 memcpy(cpu_vaddr + cpu_offset,
338 gpu_vaddr + swizzled_gpu_offset,
339 this_length);
340 } else {
341 memcpy(gpu_vaddr + swizzled_gpu_offset,
342 cpu_vaddr + cpu_offset,
343 this_length);
344 }
345 cpu_offset += this_length;
346 gpu_offset += this_length;
347 length -= this_length;
348 }
349
Chris Wilson99a03df2010-05-27 14:15:34 +0100350 kunmap(cpu_page);
351 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700352}
353
Eric Anholt673a3942008-07-30 12:06:12 -0700354/**
Eric Anholteb014592009-03-10 11:44:52 -0700355 * This is the fast shmem pread path, which attempts to copy_from_user directly
356 * from the backing pages of the object to the user's address space. On a
357 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
358 */
359static int
360i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
361 struct drm_i915_gem_pread *args,
362 struct drm_file *file_priv)
363{
Daniel Vetter23010e42010-03-08 13:35:02 +0100364 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700365 ssize_t remain;
366 loff_t offset, page_base;
367 char __user *user_data;
368 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700369
370 user_data = (char __user *) (uintptr_t) args->data_ptr;
371 remain = args->size;
372
Daniel Vetter23010e42010-03-08 13:35:02 +0100373 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700374 offset = args->offset;
375
376 while (remain > 0) {
377 /* Operation in this page
378 *
379 * page_base = page offset within aperture
380 * page_offset = offset within page
381 * page_length = bytes to copy for this page
382 */
383 page_base = (offset & ~(PAGE_SIZE-1));
384 page_offset = offset & (PAGE_SIZE-1);
385 page_length = remain;
386 if ((page_offset + remain) > PAGE_SIZE)
387 page_length = PAGE_SIZE - page_offset;
388
Chris Wilson4f27b752010-10-14 15:26:45 +0100389 if (fast_shmem_read(obj_priv->pages,
390 page_base, page_offset,
391 user_data, page_length))
392 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700393
394 remain -= page_length;
395 user_data += page_length;
396 offset += page_length;
397 }
398
Chris Wilson4f27b752010-10-14 15:26:45 +0100399 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700400}
401
Chris Wilson07f73f62009-09-14 16:50:30 +0100402static int
403i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
404{
405 int ret;
406
Chris Wilson4bdadb92010-01-27 13:36:32 +0000407 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100408
409 /* If we've insufficient memory to map in the pages, attempt
410 * to make some space by throwing out some old buffers.
411 */
412 if (ret == -ENOMEM) {
413 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100414
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100415 ret = i915_gem_evict_something(dev, obj->size,
416 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100417 if (ret)
418 return ret;
419
Chris Wilson4bdadb92010-01-27 13:36:32 +0000420 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100421 }
422
423 return ret;
424}
425
Eric Anholteb014592009-03-10 11:44:52 -0700426/**
427 * This is the fallback shmem pread path, which allocates temporary storage
428 * in kernel space to copy_to_user into outside of the struct_mutex, so we
429 * can copy out of the object's backing pages while holding the struct mutex
430 * and not take page faults.
431 */
432static int
433i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
434 struct drm_i915_gem_pread *args,
435 struct drm_file *file_priv)
436{
Daniel Vetter23010e42010-03-08 13:35:02 +0100437 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700438 struct mm_struct *mm = current->mm;
439 struct page **user_pages;
440 ssize_t remain;
441 loff_t offset, pinned_pages, i;
442 loff_t first_data_page, last_data_page, num_pages;
443 int shmem_page_index, shmem_page_offset;
444 int data_page_index, data_page_offset;
445 int page_length;
446 int ret;
447 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700448 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700449
450 remain = args->size;
451
452 /* Pin the user pages containing the data. We can't fault while
453 * holding the struct mutex, yet we want to hold it while
454 * dereferencing the user data.
455 */
456 first_data_page = data_ptr / PAGE_SIZE;
457 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
458 num_pages = last_data_page - first_data_page + 1;
459
Chris Wilson4f27b752010-10-14 15:26:45 +0100460 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700461 if (user_pages == NULL)
462 return -ENOMEM;
463
Chris Wilson4f27b752010-10-14 15:26:45 +0100464 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700465 down_read(&mm->mmap_sem);
466 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700467 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700468 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100469 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700470 if (pinned_pages < num_pages) {
471 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100472 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700473 }
474
Chris Wilson4f27b752010-10-14 15:26:45 +0100475 ret = i915_gem_object_set_cpu_read_domain_range(obj,
476 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700477 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100478 if (ret)
479 goto out;
480
481 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700482
Daniel Vetter23010e42010-03-08 13:35:02 +0100483 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700484 offset = args->offset;
485
486 while (remain > 0) {
487 /* Operation in this page
488 *
489 * shmem_page_index = page number within shmem file
490 * shmem_page_offset = offset within page in shmem file
491 * data_page_index = page number in get_user_pages return
492 * data_page_offset = offset with data_page_index page.
493 * page_length = bytes to copy for this page
494 */
495 shmem_page_index = offset / PAGE_SIZE;
496 shmem_page_offset = offset & ~PAGE_MASK;
497 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
498 data_page_offset = data_ptr & ~PAGE_MASK;
499
500 page_length = remain;
501 if ((shmem_page_offset + page_length) > PAGE_SIZE)
502 page_length = PAGE_SIZE - shmem_page_offset;
503 if ((data_page_offset + page_length) > PAGE_SIZE)
504 page_length = PAGE_SIZE - data_page_offset;
505
Eric Anholt280b7132009-03-12 16:56:27 -0700506 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100507 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700508 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100509 user_pages[data_page_index],
510 data_page_offset,
511 page_length,
512 1);
513 } else {
514 slow_shmem_copy(user_pages[data_page_index],
515 data_page_offset,
516 obj_priv->pages[shmem_page_index],
517 shmem_page_offset,
518 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700519 }
Eric Anholteb014592009-03-10 11:44:52 -0700520
521 remain -= page_length;
522 data_ptr += page_length;
523 offset += page_length;
524 }
525
Chris Wilson4f27b752010-10-14 15:26:45 +0100526out:
Eric Anholteb014592009-03-10 11:44:52 -0700527 for (i = 0; i < pinned_pages; i++) {
528 SetPageDirty(user_pages[i]);
529 page_cache_release(user_pages[i]);
530 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700531 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700532
533 return ret;
534}
535
Eric Anholt673a3942008-07-30 12:06:12 -0700536/**
537 * Reads data from the object referenced by handle.
538 *
539 * On error, the contents of *data are undefined.
540 */
541int
542i915_gem_pread_ioctl(struct drm_device *dev, void *data,
543 struct drm_file *file_priv)
544{
545 struct drm_i915_gem_pread *args = data;
546 struct drm_gem_object *obj;
547 struct drm_i915_gem_object *obj_priv;
Chris Wilson35b62a82010-09-26 20:23:38 +0100548 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700549
Chris Wilson51311d02010-11-17 09:10:42 +0000550 if (args->size == 0)
551 return 0;
552
553 if (!access_ok(VERIFY_WRITE,
554 (char __user *)(uintptr_t)args->data_ptr,
555 args->size))
556 return -EFAULT;
557
558 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
559 args->size);
560 if (ret)
561 return -EFAULT;
562
Chris Wilson4f27b752010-10-14 15:26:45 +0100563 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100564 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100565 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700566
567 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100568 if (obj == NULL) {
569 ret = -ENOENT;
570 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100571 }
Daniel Vetter23010e42010-03-08 13:35:02 +0100572 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700573
Chris Wilson7dcd2492010-09-26 20:21:44 +0100574 /* Bounds check source. */
575 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100576 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100577 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100578 }
579
Chris Wilson4f27b752010-10-14 15:26:45 +0100580 ret = i915_gem_object_get_pages_or_evict(obj);
581 if (ret)
582 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -0700583
Chris Wilson4f27b752010-10-14 15:26:45 +0100584 ret = i915_gem_object_set_cpu_read_domain_range(obj,
585 args->offset,
586 args->size);
587 if (ret)
588 goto out_put;
589
590 ret = -EFAULT;
591 if (!i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt673a3942008-07-30 12:06:12 -0700592 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
Chris Wilson4f27b752010-10-14 15:26:45 +0100593 if (ret == -EFAULT)
594 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700595
Chris Wilson4f27b752010-10-14 15:26:45 +0100596out_put:
597 i915_gem_object_put_pages(obj);
Chris Wilson35b62a82010-09-26 20:23:38 +0100598out:
Chris Wilson4f27b752010-10-14 15:26:45 +0100599 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100600unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100601 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700602 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700603}
604
Keith Packard0839ccb2008-10-30 19:38:48 -0700605/* This is the fast write path which cannot handle
606 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700607 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700608
Keith Packard0839ccb2008-10-30 19:38:48 -0700609static inline int
610fast_user_write(struct io_mapping *mapping,
611 loff_t page_base, int page_offset,
612 char __user *user_data,
613 int length)
614{
615 char *vaddr_atomic;
616 unsigned long unwritten;
617
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700618 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700619 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
620 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700621 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100622 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700623}
624
625/* Here's the write path which can sleep for
626 * page faults
627 */
628
Chris Wilsonab34c222010-05-27 14:15:35 +0100629static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700630slow_kernel_write(struct io_mapping *mapping,
631 loff_t gtt_base, int gtt_offset,
632 struct page *user_page, int user_offset,
633 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700634{
Chris Wilsonab34c222010-05-27 14:15:35 +0100635 char __iomem *dst_vaddr;
636 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700637
Chris Wilsonab34c222010-05-27 14:15:35 +0100638 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
639 src_vaddr = kmap(user_page);
640
641 memcpy_toio(dst_vaddr + gtt_offset,
642 src_vaddr + user_offset,
643 length);
644
645 kunmap(user_page);
646 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700647}
648
Eric Anholt40123c12009-03-09 13:42:30 -0700649static inline int
650fast_shmem_write(struct page **pages,
651 loff_t page_base, int page_offset,
652 char __user *data,
653 int length)
654{
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100655 char *vaddr;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100656 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700657
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700658 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT]);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100659 ret = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700660 kunmap_atomic(vaddr);
Eric Anholt40123c12009-03-09 13:42:30 -0700661
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100662 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700663}
664
Eric Anholt3de09aa2009-03-09 09:42:23 -0700665/**
666 * This is the fast pwrite path, where we copy the data directly from the
667 * user into the GTT, uncached.
668 */
Eric Anholt673a3942008-07-30 12:06:12 -0700669static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700670i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
671 struct drm_i915_gem_pwrite *args,
672 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700673{
Daniel Vetter23010e42010-03-08 13:35:02 +0100674 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700675 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700676 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700677 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700678 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700679 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700680
681 user_data = (char __user *) (uintptr_t) args->data_ptr;
682 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700683
Daniel Vetter23010e42010-03-08 13:35:02 +0100684 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700685 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700686
687 while (remain > 0) {
688 /* Operation in this page
689 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700690 * page_base = page offset within aperture
691 * page_offset = offset within page
692 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700693 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700694 page_base = (offset & ~(PAGE_SIZE-1));
695 page_offset = offset & (PAGE_SIZE-1);
696 page_length = remain;
697 if ((page_offset + remain) > PAGE_SIZE)
698 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700699
Keith Packard0839ccb2008-10-30 19:38:48 -0700700 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700701 * source page isn't available. Return the error and we'll
702 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700703 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100704 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
705 page_offset, user_data, page_length))
706
707 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700708
Keith Packard0839ccb2008-10-30 19:38:48 -0700709 remain -= page_length;
710 user_data += page_length;
711 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700712 }
Eric Anholt673a3942008-07-30 12:06:12 -0700713
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100714 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700715}
716
Eric Anholt3de09aa2009-03-09 09:42:23 -0700717/**
718 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
719 * the memory and maps it using kmap_atomic for copying.
720 *
721 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
722 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
723 */
Eric Anholt3043c602008-10-02 12:24:47 -0700724static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700725i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
726 struct drm_i915_gem_pwrite *args,
727 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700728{
Daniel Vetter23010e42010-03-08 13:35:02 +0100729 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700730 drm_i915_private_t *dev_priv = dev->dev_private;
731 ssize_t remain;
732 loff_t gtt_page_base, offset;
733 loff_t first_data_page, last_data_page, num_pages;
734 loff_t pinned_pages, i;
735 struct page **user_pages;
736 struct mm_struct *mm = current->mm;
737 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700738 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700739 uint64_t data_ptr = args->data_ptr;
740
741 remain = args->size;
742
743 /* Pin the user pages containing the data. We can't fault while
744 * holding the struct mutex, and all of the pwrite implementations
745 * want to hold it while dereferencing the user data.
746 */
747 first_data_page = data_ptr / PAGE_SIZE;
748 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
749 num_pages = last_data_page - first_data_page + 1;
750
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100751 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700752 if (user_pages == NULL)
753 return -ENOMEM;
754
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100755 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700756 down_read(&mm->mmap_sem);
757 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
758 num_pages, 0, 0, user_pages, NULL);
759 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100760 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700761 if (pinned_pages < num_pages) {
762 ret = -EFAULT;
763 goto out_unpin_pages;
764 }
765
Eric Anholt3de09aa2009-03-09 09:42:23 -0700766 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
767 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100768 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700769
Daniel Vetter23010e42010-03-08 13:35:02 +0100770 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700771 offset = obj_priv->gtt_offset + args->offset;
772
773 while (remain > 0) {
774 /* Operation in this page
775 *
776 * gtt_page_base = page offset within aperture
777 * gtt_page_offset = offset within page in aperture
778 * data_page_index = page number in get_user_pages return
779 * data_page_offset = offset with data_page_index page.
780 * page_length = bytes to copy for this page
781 */
782 gtt_page_base = offset & PAGE_MASK;
783 gtt_page_offset = offset & ~PAGE_MASK;
784 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
785 data_page_offset = data_ptr & ~PAGE_MASK;
786
787 page_length = remain;
788 if ((gtt_page_offset + page_length) > PAGE_SIZE)
789 page_length = PAGE_SIZE - gtt_page_offset;
790 if ((data_page_offset + page_length) > PAGE_SIZE)
791 page_length = PAGE_SIZE - data_page_offset;
792
Chris Wilsonab34c222010-05-27 14:15:35 +0100793 slow_kernel_write(dev_priv->mm.gtt_mapping,
794 gtt_page_base, gtt_page_offset,
795 user_pages[data_page_index],
796 data_page_offset,
797 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700798
799 remain -= page_length;
800 offset += page_length;
801 data_ptr += page_length;
802 }
803
Eric Anholt3de09aa2009-03-09 09:42:23 -0700804out_unpin_pages:
805 for (i = 0; i < pinned_pages; i++)
806 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700807 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700808
809 return ret;
810}
811
Eric Anholt40123c12009-03-09 13:42:30 -0700812/**
813 * This is the fast shmem pwrite path, which attempts to directly
814 * copy_from_user into the kmapped pages backing the object.
815 */
Eric Anholt673a3942008-07-30 12:06:12 -0700816static int
Eric Anholt40123c12009-03-09 13:42:30 -0700817i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
818 struct drm_i915_gem_pwrite *args,
819 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700820{
Daniel Vetter23010e42010-03-08 13:35:02 +0100821 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700822 ssize_t remain;
823 loff_t offset, page_base;
824 char __user *user_data;
825 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700826
827 user_data = (char __user *) (uintptr_t) args->data_ptr;
828 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700829
Daniel Vetter23010e42010-03-08 13:35:02 +0100830 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700831 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700832 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700833
Eric Anholt40123c12009-03-09 13:42:30 -0700834 while (remain > 0) {
835 /* Operation in this page
836 *
837 * page_base = page offset within aperture
838 * page_offset = offset within page
839 * page_length = bytes to copy for this page
840 */
841 page_base = (offset & ~(PAGE_SIZE-1));
842 page_offset = offset & (PAGE_SIZE-1);
843 page_length = remain;
844 if ((page_offset + remain) > PAGE_SIZE)
845 page_length = PAGE_SIZE - page_offset;
846
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100847 if (fast_shmem_write(obj_priv->pages,
Eric Anholt40123c12009-03-09 13:42:30 -0700848 page_base, page_offset,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100849 user_data, page_length))
850 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700851
852 remain -= page_length;
853 user_data += page_length;
854 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700855 }
856
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100857 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700858}
859
860/**
861 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
862 * the memory and maps it using kmap_atomic for copying.
863 *
864 * This avoids taking mmap_sem for faulting on the user's address while the
865 * struct_mutex is held.
866 */
867static int
868i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
869 struct drm_i915_gem_pwrite *args,
870 struct drm_file *file_priv)
871{
Daniel Vetter23010e42010-03-08 13:35:02 +0100872 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700873 struct mm_struct *mm = current->mm;
874 struct page **user_pages;
875 ssize_t remain;
876 loff_t offset, pinned_pages, i;
877 loff_t first_data_page, last_data_page, num_pages;
878 int shmem_page_index, shmem_page_offset;
879 int data_page_index, data_page_offset;
880 int page_length;
881 int ret;
882 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700883 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700884
885 remain = args->size;
886
887 /* Pin the user pages containing the data. We can't fault while
888 * holding the struct mutex, and all of the pwrite implementations
889 * want to hold it while dereferencing the user data.
890 */
891 first_data_page = data_ptr / PAGE_SIZE;
892 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
893 num_pages = last_data_page - first_data_page + 1;
894
Chris Wilson4f27b752010-10-14 15:26:45 +0100895 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700896 if (user_pages == NULL)
897 return -ENOMEM;
898
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100899 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700900 down_read(&mm->mmap_sem);
901 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
902 num_pages, 0, 0, user_pages, NULL);
903 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100904 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700905 if (pinned_pages < num_pages) {
906 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100907 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700908 }
909
Eric Anholt40123c12009-03-09 13:42:30 -0700910 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100911 if (ret)
912 goto out;
913
914 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700915
Daniel Vetter23010e42010-03-08 13:35:02 +0100916 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700917 offset = args->offset;
918 obj_priv->dirty = 1;
919
920 while (remain > 0) {
921 /* Operation in this page
922 *
923 * shmem_page_index = page number within shmem file
924 * shmem_page_offset = offset within page in shmem file
925 * data_page_index = page number in get_user_pages return
926 * data_page_offset = offset with data_page_index page.
927 * page_length = bytes to copy for this page
928 */
929 shmem_page_index = offset / PAGE_SIZE;
930 shmem_page_offset = offset & ~PAGE_MASK;
931 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
932 data_page_offset = data_ptr & ~PAGE_MASK;
933
934 page_length = remain;
935 if ((shmem_page_offset + page_length) > PAGE_SIZE)
936 page_length = PAGE_SIZE - shmem_page_offset;
937 if ((data_page_offset + page_length) > PAGE_SIZE)
938 page_length = PAGE_SIZE - data_page_offset;
939
Eric Anholt280b7132009-03-12 16:56:27 -0700940 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100941 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700942 shmem_page_offset,
943 user_pages[data_page_index],
944 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100945 page_length,
946 0);
947 } else {
948 slow_shmem_copy(obj_priv->pages[shmem_page_index],
949 shmem_page_offset,
950 user_pages[data_page_index],
951 data_page_offset,
952 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700953 }
Eric Anholt40123c12009-03-09 13:42:30 -0700954
955 remain -= page_length;
956 data_ptr += page_length;
957 offset += page_length;
958 }
959
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100960out:
Eric Anholt40123c12009-03-09 13:42:30 -0700961 for (i = 0; i < pinned_pages; i++)
962 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700963 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700964
965 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700966}
967
968/**
969 * Writes data to the object referenced by handle.
970 *
971 * On error, the contents of the buffer that were to be modified are undefined.
972 */
973int
974i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100975 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700976{
977 struct drm_i915_gem_pwrite *args = data;
978 struct drm_gem_object *obj;
979 struct drm_i915_gem_object *obj_priv;
Chris Wilson51311d02010-11-17 09:10:42 +0000980 int ret;
981
982 if (args->size == 0)
983 return 0;
984
985 if (!access_ok(VERIFY_READ,
986 (char __user *)(uintptr_t)args->data_ptr,
987 args->size))
988 return -EFAULT;
989
990 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
991 args->size);
992 if (ret)
993 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700994
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100995 ret = i915_mutex_lock_interruptible(dev);
996 if (ret)
997 return ret;
998
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100999 obj = drm_gem_object_lookup(dev, file, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001000 if (obj == NULL) {
1001 ret = -ENOENT;
1002 goto unlock;
1003 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001004 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001005
Chris Wilson7dcd2492010-09-26 20:21:44 +01001006 /* Bounds check destination. */
1007 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001008 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001009 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001010 }
1011
Eric Anholt673a3942008-07-30 12:06:12 -07001012 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1013 * it would end up going through the fenced access, and we'll get
1014 * different detiling behavior between reading and writing.
1015 * pread/pwrite currently are reading and writing from the CPU
1016 * perspective, requiring manual detiling by the client.
1017 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001018 if (obj_priv->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001019 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Dave Airlie71acb5e2008-12-30 20:31:46 +10001020 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001021 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001022 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001023 ret = i915_gem_object_pin(obj, 0);
1024 if (ret)
1025 goto out;
1026
1027 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1028 if (ret)
1029 goto out_unpin;
1030
1031 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1032 if (ret == -EFAULT)
1033 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1034
1035out_unpin:
1036 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001037 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001038 ret = i915_gem_object_get_pages_or_evict(obj);
1039 if (ret)
1040 goto out;
1041
1042 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1043 if (ret)
1044 goto out_put;
1045
1046 ret = -EFAULT;
1047 if (!i915_gem_object_needs_bit17_swizzle(obj))
1048 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1049 if (ret == -EFAULT)
1050 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
1051
1052out_put:
1053 i915_gem_object_put_pages(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001054 }
Eric Anholt673a3942008-07-30 12:06:12 -07001055
Chris Wilson35b62a82010-09-26 20:23:38 +01001056out:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001057 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001058unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001059 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001060 return ret;
1061}
1062
1063/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001064 * Called when user space prepares to use an object with the CPU, either
1065 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001066 */
1067int
1068i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1069 struct drm_file *file_priv)
1070{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001071 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001072 struct drm_i915_gem_set_domain *args = data;
1073 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001074 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001075 uint32_t read_domains = args->read_domains;
1076 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001077 int ret;
1078
1079 if (!(dev->driver->driver_features & DRIVER_GEM))
1080 return -ENODEV;
1081
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001082 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001083 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001084 return -EINVAL;
1085
Chris Wilson21d509e2009-06-06 09:46:02 +01001086 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001087 return -EINVAL;
1088
1089 /* Having something in the write domain implies it's in the read
1090 * domain, and only that read domain. Enforce that in the request.
1091 */
1092 if (write_domain != 0 && read_domains != write_domain)
1093 return -EINVAL;
1094
Chris Wilson76c1dec2010-09-25 11:22:51 +01001095 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001096 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001097 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001098
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001099 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1100 if (obj == NULL) {
1101 ret = -ENOENT;
1102 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001103 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001104 obj_priv = to_intel_bo(obj);
Jesse Barnes652c3932009-08-17 13:31:43 -07001105
1106 intel_mark_busy(dev, obj);
1107
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001108 if (read_domains & I915_GEM_DOMAIN_GTT) {
1109 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001110
Eric Anholta09ba7f2009-08-29 12:49:51 -07001111 /* Update the LRU on the fence for the CPU access that's
1112 * about to occur.
1113 */
1114 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001115 struct drm_i915_fence_reg *reg =
1116 &dev_priv->fence_regs[obj_priv->fence_reg];
1117 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001118 &dev_priv->mm.fence_list);
1119 }
1120
Eric Anholt02354392008-11-26 13:58:13 -08001121 /* Silently promote "you're not bound, there was nothing to do"
1122 * to success, since the client was just asking us to
1123 * make sure everything was done.
1124 */
1125 if (ret == -EINVAL)
1126 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001127 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001128 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001129 }
1130
Chris Wilson7d1c4802010-08-07 21:45:03 +01001131 /* Maintain LRU order of "inactive" objects */
1132 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001133 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001134
Eric Anholt673a3942008-07-30 12:06:12 -07001135 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001136unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001137 mutex_unlock(&dev->struct_mutex);
1138 return ret;
1139}
1140
1141/**
1142 * Called when user space has done writes to this buffer
1143 */
1144int
1145i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1146 struct drm_file *file_priv)
1147{
1148 struct drm_i915_gem_sw_finish *args = data;
1149 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001150 int ret = 0;
1151
1152 if (!(dev->driver->driver_features & DRIVER_GEM))
1153 return -ENODEV;
1154
Chris Wilson76c1dec2010-09-25 11:22:51 +01001155 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001156 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001157 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001158
Eric Anholt673a3942008-07-30 12:06:12 -07001159 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1160 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001161 ret = -ENOENT;
1162 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001163 }
1164
Eric Anholt673a3942008-07-30 12:06:12 -07001165 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001166 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001167 i915_gem_object_flush_cpu_write_domain(obj);
1168
Eric Anholt673a3942008-07-30 12:06:12 -07001169 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001170unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001171 mutex_unlock(&dev->struct_mutex);
1172 return ret;
1173}
1174
1175/**
1176 * Maps the contents of an object, returning the address it is mapped
1177 * into.
1178 *
1179 * While the mapping holds a reference on the contents of the object, it doesn't
1180 * imply a ref on the object itself.
1181 */
1182int
1183i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1184 struct drm_file *file_priv)
1185{
1186 struct drm_i915_gem_mmap *args = data;
1187 struct drm_gem_object *obj;
1188 loff_t offset;
1189 unsigned long addr;
1190
1191 if (!(dev->driver->driver_features & DRIVER_GEM))
1192 return -ENODEV;
1193
1194 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1195 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001196 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001197
1198 offset = args->offset;
1199
1200 down_write(&current->mm->mmap_sem);
1201 addr = do_mmap(obj->filp, 0, args->size,
1202 PROT_READ | PROT_WRITE, MAP_SHARED,
1203 args->offset);
1204 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001205 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001206 if (IS_ERR((void *)addr))
1207 return addr;
1208
1209 args->addr_ptr = (uint64_t) addr;
1210
1211 return 0;
1212}
1213
Jesse Barnesde151cf2008-11-12 10:03:55 -08001214/**
1215 * i915_gem_fault - fault a page into the GTT
1216 * vma: VMA in question
1217 * vmf: fault info
1218 *
1219 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1220 * from userspace. The fault handler takes care of binding the object to
1221 * the GTT (if needed), allocating and programming a fence register (again,
1222 * only if needed based on whether the old reg is still valid or the object
1223 * is tiled) and inserting a new PTE into the faulting process.
1224 *
1225 * Note that the faulting process may involve evicting existing objects
1226 * from the GTT and/or fence registers to make room. So performance may
1227 * suffer if the GTT working set is large or there are few fence registers
1228 * left.
1229 */
1230int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1231{
1232 struct drm_gem_object *obj = vma->vm_private_data;
1233 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001234 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001235 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001236 pgoff_t page_offset;
1237 unsigned long pfn;
1238 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001239 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001240
1241 /* We don't use vmf->pgoff since that has the fake offset */
1242 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1243 PAGE_SHIFT;
1244
1245 /* Now bind it into the GTT if needed */
1246 mutex_lock(&dev->struct_mutex);
1247 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001248 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001249 if (ret)
1250 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001251
Jesse Barnesde151cf2008-11-12 10:03:55 -08001252 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001253 if (ret)
1254 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001255 }
1256
1257 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001258 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001259 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001260 if (ret)
1261 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001262 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263
Chris Wilson7d1c4802010-08-07 21:45:03 +01001264 if (i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001265 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001266
Jesse Barnesde151cf2008-11-12 10:03:55 -08001267 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1268 page_offset;
1269
1270 /* Finally, remap it using the new GTT offset */
1271 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001272unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001273 mutex_unlock(&dev->struct_mutex);
1274
1275 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001276 case 0:
1277 case -ERESTARTSYS:
1278 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001279 case -ENOMEM:
1280 case -EAGAIN:
1281 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001282 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001283 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001284 }
1285}
1286
1287/**
1288 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1289 * @obj: obj in question
1290 *
1291 * GEM memory mapping works by handing back to userspace a fake mmap offset
1292 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1293 * up the object based on the offset and sets up the various memory mapping
1294 * structures.
1295 *
1296 * This routine allocates and attaches a fake offset for @obj.
1297 */
1298static int
1299i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1300{
1301 struct drm_device *dev = obj->dev;
1302 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001303 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001304 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001305 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001306 int ret = 0;
1307
1308 /* Set the object up for mmap'ing */
1309 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001310 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001311 if (!list->map)
1312 return -ENOMEM;
1313
1314 map = list->map;
1315 map->type = _DRM_GEM;
1316 map->size = obj->size;
1317 map->handle = obj;
1318
1319 /* Get a DRM GEM mmap offset allocated... */
1320 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1321 obj->size / PAGE_SIZE, 0, 0);
1322 if (!list->file_offset_node) {
1323 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001324 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001325 goto out_free_list;
1326 }
1327
1328 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1329 obj->size / PAGE_SIZE, 0);
1330 if (!list->file_offset_node) {
1331 ret = -ENOMEM;
1332 goto out_free_list;
1333 }
1334
1335 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001336 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1337 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001338 DRM_ERROR("failed to add to map hash\n");
1339 goto out_free_mm;
1340 }
1341
1342 /* By now we should be all set, any drm_mmap request on the offset
1343 * below will get to our mmap & fault handler */
1344 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1345
1346 return 0;
1347
1348out_free_mm:
1349 drm_mm_put_block(list->file_offset_node);
1350out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001351 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001352
1353 return ret;
1354}
1355
Chris Wilson901782b2009-07-10 08:18:50 +01001356/**
1357 * i915_gem_release_mmap - remove physical page mappings
1358 * @obj: obj in question
1359 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001360 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001361 * relinquish ownership of the pages back to the system.
1362 *
1363 * It is vital that we remove the page mapping if we have mapped a tiled
1364 * object through the GTT and then lose the fence register due to
1365 * resource pressure. Similarly if the object has been moved out of the
1366 * aperture, than pages mapped into userspace must be revoked. Removing the
1367 * mapping will then trigger a page fault on the next user access, allowing
1368 * fixup by i915_gem_fault().
1369 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001370void
Chris Wilson901782b2009-07-10 08:18:50 +01001371i915_gem_release_mmap(struct drm_gem_object *obj)
1372{
1373 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001374 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001375
1376 if (dev->dev_mapping)
1377 unmap_mapping_range(dev->dev_mapping,
1378 obj_priv->mmap_offset, obj->size, 1);
1379}
1380
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001381static void
1382i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1383{
1384 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001385 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001386 struct drm_gem_mm *mm = dev->mm_private;
1387 struct drm_map_list *list;
1388
1389 list = &obj->map_list;
1390 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1391
1392 if (list->file_offset_node) {
1393 drm_mm_put_block(list->file_offset_node);
1394 list->file_offset_node = NULL;
1395 }
1396
1397 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001398 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001399 list->map = NULL;
1400 }
1401
1402 obj_priv->mmap_offset = 0;
1403}
1404
Jesse Barnesde151cf2008-11-12 10:03:55 -08001405/**
1406 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1407 * @obj: object to check
1408 *
1409 * Return the required GTT alignment for an object, taking into account
1410 * potential fence register mapping if needed.
1411 */
1412static uint32_t
1413i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1414{
1415 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001416 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001417 int start, i;
1418
1419 /*
1420 * Minimum alignment is 4k (GTT page size), but might be greater
1421 * if a fence register is needed for the object.
1422 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001423 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001424 return 4096;
1425
1426 /*
1427 * Previous chips need to be aligned to the size of the smallest
1428 * fence register that can contain the object.
1429 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001430 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001431 start = 1024*1024;
1432 else
1433 start = 512*1024;
1434
1435 for (i = start; i < obj->size; i <<= 1)
1436 ;
1437
1438 return i;
1439}
1440
1441/**
1442 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1443 * @dev: DRM device
1444 * @data: GTT mapping ioctl data
1445 * @file_priv: GEM object info
1446 *
1447 * Simply returns the fake offset to userspace so it can mmap it.
1448 * The mmap call will end up in drm_gem_mmap(), which will set things
1449 * up so we can get faults in the handler above.
1450 *
1451 * The fault handler will take care of binding the object into the GTT
1452 * (since it may have been evicted to make room for something), allocating
1453 * a fence register, and mapping the appropriate aperture address into
1454 * userspace.
1455 */
1456int
1457i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1458 struct drm_file *file_priv)
1459{
1460 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001461 struct drm_gem_object *obj;
1462 struct drm_i915_gem_object *obj_priv;
1463 int ret;
1464
1465 if (!(dev->driver->driver_features & DRIVER_GEM))
1466 return -ENODEV;
1467
Chris Wilson76c1dec2010-09-25 11:22:51 +01001468 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001469 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001470 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001471
Jesse Barnesde151cf2008-11-12 10:03:55 -08001472 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001473 if (obj == NULL) {
1474 ret = -ENOENT;
1475 goto unlock;
1476 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001477 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001478
Chris Wilsonab182822009-09-22 18:46:17 +01001479 if (obj_priv->madv != I915_MADV_WILLNEED) {
1480 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001481 ret = -EINVAL;
1482 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001483 }
1484
Jesse Barnesde151cf2008-11-12 10:03:55 -08001485 if (!obj_priv->mmap_offset) {
1486 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001487 if (ret)
1488 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001489 }
1490
1491 args->offset = obj_priv->mmap_offset;
1492
Jesse Barnesde151cf2008-11-12 10:03:55 -08001493 /*
1494 * Pull it into the GTT so that we have a page list (makes the
1495 * initial fault faster and any subsequent flushing possible).
1496 */
1497 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001498 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001499 if (ret)
1500 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001501 }
1502
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001503out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001504 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001505unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001506 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001507 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001508}
1509
Chris Wilson5cdf5882010-09-27 15:51:07 +01001510static void
Eric Anholt856fa192009-03-19 14:10:50 -07001511i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001512{
Daniel Vetter23010e42010-03-08 13:35:02 +01001513 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001514 int page_count = obj->size / PAGE_SIZE;
1515 int i;
1516
Eric Anholt856fa192009-03-19 14:10:50 -07001517 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001518 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001519
1520 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001521 return;
1522
Eric Anholt280b7132009-03-12 16:56:27 -07001523 if (obj_priv->tiling_mode != I915_TILING_NONE)
1524 i915_gem_object_save_bit_17_swizzle(obj);
1525
Chris Wilson3ef94da2009-09-14 16:50:29 +01001526 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001527 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001528
1529 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001530 if (obj_priv->dirty)
1531 set_page_dirty(obj_priv->pages[i]);
1532
1533 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001534 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001535
1536 page_cache_release(obj_priv->pages[i]);
1537 }
Eric Anholt673a3942008-07-30 12:06:12 -07001538 obj_priv->dirty = 0;
1539
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001540 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001541 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001542}
1543
Chris Wilsona56ba562010-09-28 10:07:56 +01001544static uint32_t
1545i915_gem_next_request_seqno(struct drm_device *dev,
1546 struct intel_ring_buffer *ring)
1547{
1548 drm_i915_private_t *dev_priv = dev->dev_private;
1549
1550 ring->outstanding_lazy_request = true;
1551 return dev_priv->next_seqno;
1552}
1553
Eric Anholt673a3942008-07-30 12:06:12 -07001554static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001555i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001556 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001557{
1558 struct drm_device *dev = obj->dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001559 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001560 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001561 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001562
Zou Nan hai852835f2010-05-21 09:08:56 +08001563 BUG_ON(ring == NULL);
1564 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001565
1566 /* Add a reference if we're newly entering the active list. */
1567 if (!obj_priv->active) {
1568 drm_gem_object_reference(obj);
1569 obj_priv->active = 1;
1570 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001571
Eric Anholt673a3942008-07-30 12:06:12 -07001572 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson69dc4982010-10-19 10:36:51 +01001573 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
1574 list_move_tail(&obj_priv->ring_list, &ring->active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001575 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001576}
1577
Eric Anholtce44b0e2008-11-06 16:00:31 -08001578static void
1579i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1580{
1581 struct drm_device *dev = obj->dev;
1582 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001583 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001584
1585 BUG_ON(!obj_priv->active);
Chris Wilson69dc4982010-10-19 10:36:51 +01001586 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
1587 list_del_init(&obj_priv->ring_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001588 obj_priv->last_rendering_seqno = 0;
1589}
Eric Anholt673a3942008-07-30 12:06:12 -07001590
Chris Wilson963b4832009-09-20 23:03:54 +01001591/* Immediately discard the backing storage */
1592static void
1593i915_gem_object_truncate(struct drm_gem_object *obj)
1594{
Daniel Vetter23010e42010-03-08 13:35:02 +01001595 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001596 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001597
Chris Wilsonae9fed62010-08-07 11:01:30 +01001598 /* Our goal here is to return as much of the memory as
1599 * is possible back to the system as we are called from OOM.
1600 * To do this we must instruct the shmfs to drop all of its
1601 * backing pages, *now*. Here we mirror the actions taken
1602 * when by shmem_delete_inode() to release the backing store.
1603 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001604 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001605 truncate_inode_pages(inode->i_mapping, 0);
1606 if (inode->i_op->truncate_range)
1607 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001608
1609 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001610}
1611
1612static inline int
1613i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1614{
1615 return obj_priv->madv == I915_MADV_DONTNEED;
1616}
1617
Eric Anholt673a3942008-07-30 12:06:12 -07001618static void
1619i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1620{
1621 struct drm_device *dev = obj->dev;
1622 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001623 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001624
Eric Anholt673a3942008-07-30 12:06:12 -07001625 if (obj_priv->pin_count != 0)
Chris Wilson69dc4982010-10-19 10:36:51 +01001626 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001627 else
Chris Wilson69dc4982010-10-19 10:36:51 +01001628 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1629 list_del_init(&obj_priv->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001630
Daniel Vetter99fcb762010-02-07 16:20:18 +01001631 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1632
Eric Anholtce44b0e2008-11-06 16:00:31 -08001633 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001634 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001635 if (obj_priv->active) {
1636 obj_priv->active = 0;
1637 drm_gem_object_unreference(obj);
1638 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001639 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001640}
1641
Daniel Vetter63560392010-02-19 11:51:59 +01001642static void
1643i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001644 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001645 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001646{
1647 drm_i915_private_t *dev_priv = dev->dev_private;
1648 struct drm_i915_gem_object *obj_priv, *next;
1649
1650 list_for_each_entry_safe(obj_priv, next,
Chris Wilson64193402010-10-24 12:38:05 +01001651 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001652 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001653 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001654
Chris Wilson64193402010-10-24 12:38:05 +01001655 if (obj->write_domain & flush_domains) {
Daniel Vetter63560392010-02-19 11:51:59 +01001656 uint32_t old_write_domain = obj->write_domain;
1657
1658 obj->write_domain = 0;
1659 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001660 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001661
1662 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001663 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1664 struct drm_i915_fence_reg *reg =
1665 &dev_priv->fence_regs[obj_priv->fence_reg];
1666 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001667 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001668 }
Daniel Vetter63560392010-02-19 11:51:59 +01001669
1670 trace_i915_gem_object_change_domain(obj,
1671 obj->read_domains,
1672 old_write_domain);
1673 }
1674 }
1675}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001676
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001677uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001678i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001679 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001680 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001681 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001682{
1683 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001684 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001685 uint32_t seqno;
1686 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001687
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001688 if (file != NULL)
1689 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001690
Chris Wilson8dc5d142010-08-12 12:36:12 +01001691 if (request == NULL) {
1692 request = kzalloc(sizeof(*request), GFP_KERNEL);
1693 if (request == NULL)
1694 return 0;
1695 }
Eric Anholt673a3942008-07-30 12:06:12 -07001696
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001697 seqno = ring->add_request(dev, ring, 0);
Chris Wilsona56ba562010-09-28 10:07:56 +01001698 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001699
1700 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001701 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001702 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001703 was_empty = list_empty(&ring->request_list);
1704 list_add_tail(&request->list, &ring->request_list);
1705
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001706 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001707 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001708 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001709 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001710 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001711 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001712 }
Eric Anholt673a3942008-07-30 12:06:12 -07001713
Ben Gamarif65d9422009-09-14 17:48:44 -04001714 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001715 mod_timer(&dev_priv->hangcheck_timer,
1716 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001717 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001718 queue_delayed_work(dev_priv->wq,
1719 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001720 }
Eric Anholt673a3942008-07-30 12:06:12 -07001721 return seqno;
1722}
1723
1724/**
1725 * Command execution barrier
1726 *
1727 * Ensures that all commands in the ring are finished
1728 * before signalling the CPU
1729 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001730static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001731i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001732{
Eric Anholt673a3942008-07-30 12:06:12 -07001733 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001734
1735 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001736 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001737 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001738
1739 ring->flush(dev, ring,
1740 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001741}
1742
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001743static inline void
1744i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001745{
Chris Wilson1c255952010-09-26 11:03:27 +01001746 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001747
Chris Wilson1c255952010-09-26 11:03:27 +01001748 if (!file_priv)
1749 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001750
Chris Wilson1c255952010-09-26 11:03:27 +01001751 spin_lock(&file_priv->mm.lock);
1752 list_del(&request->client_list);
1753 request->file_priv = NULL;
1754 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001755}
1756
Chris Wilsondfaae392010-09-22 10:31:52 +01001757static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1758 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001759{
Chris Wilsondfaae392010-09-22 10:31:52 +01001760 while (!list_empty(&ring->request_list)) {
1761 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001762
Chris Wilsondfaae392010-09-22 10:31:52 +01001763 request = list_first_entry(&ring->request_list,
1764 struct drm_i915_gem_request,
1765 list);
1766
1767 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001768 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001769 kfree(request);
1770 }
1771
1772 while (!list_empty(&ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001773 struct drm_i915_gem_object *obj_priv;
1774
Chris Wilsondfaae392010-09-22 10:31:52 +01001775 obj_priv = list_first_entry(&ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001776 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001777 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001778
Chris Wilsondfaae392010-09-22 10:31:52 +01001779 obj_priv->base.write_domain = 0;
1780 list_del_init(&obj_priv->gpu_write_list);
1781 i915_gem_object_move_to_inactive(&obj_priv->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001782 }
Eric Anholt673a3942008-07-30 12:06:12 -07001783}
1784
Chris Wilson069efc12010-09-30 16:53:18 +01001785void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001786{
Chris Wilsondfaae392010-09-22 10:31:52 +01001787 struct drm_i915_private *dev_priv = dev->dev_private;
1788 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001789 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001790
Chris Wilsondfaae392010-09-22 10:31:52 +01001791 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001792 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001793 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001794
1795 /* Remove anything from the flushing lists. The GPU cache is likely
1796 * to be lost on reset along with the data, so simply move the
1797 * lost bo to the inactive list.
1798 */
1799 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001800 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1801 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001802 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001803
1804 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001805 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001806 i915_gem_object_move_to_inactive(&obj_priv->base);
1807 }
Chris Wilson9375e442010-09-19 12:21:28 +01001808
Chris Wilsondfaae392010-09-22 10:31:52 +01001809 /* Move everything out of the GPU domains to ensure we do any
1810 * necessary invalidation upon reuse.
1811 */
Chris Wilson77f01232010-09-19 12:31:36 +01001812 list_for_each_entry(obj_priv,
1813 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001814 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001815 {
1816 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1817 }
Chris Wilson069efc12010-09-30 16:53:18 +01001818
1819 /* The fence registers are invalidated so clear them out */
1820 for (i = 0; i < 16; i++) {
1821 struct drm_i915_fence_reg *reg;
1822
1823 reg = &dev_priv->fence_regs[i];
1824 if (!reg->obj)
1825 continue;
1826
1827 i915_gem_clear_fence_reg(reg->obj);
1828 }
Eric Anholt673a3942008-07-30 12:06:12 -07001829}
1830
1831/**
1832 * This function clears the request list as sequence numbers are passed.
1833 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001834static void
1835i915_gem_retire_requests_ring(struct drm_device *dev,
1836 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001837{
1838 drm_i915_private_t *dev_priv = dev->dev_private;
1839 uint32_t seqno;
1840
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001841 if (!ring->status_page.page_addr ||
1842 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001843 return;
1844
Chris Wilson23bc5982010-09-29 16:10:57 +01001845 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001846
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001847 seqno = ring->get_seqno(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001848 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001849 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001850
Zou Nan hai852835f2010-05-21 09:08:56 +08001851 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001852 struct drm_i915_gem_request,
1853 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001854
Chris Wilsondfaae392010-09-22 10:31:52 +01001855 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001856 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001857
1858 trace_i915_gem_request_retire(dev, request->seqno);
1859
1860 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001861 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001862 kfree(request);
1863 }
1864
1865 /* Move any buffers on the active list that are no longer referenced
1866 * by the ringbuffer to the flushing/inactive lists as appropriate.
1867 */
1868 while (!list_empty(&ring->active_list)) {
1869 struct drm_gem_object *obj;
1870 struct drm_i915_gem_object *obj_priv;
1871
1872 obj_priv = list_first_entry(&ring->active_list,
1873 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001874 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001875
Chris Wilsondfaae392010-09-22 10:31:52 +01001876 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001877 break;
1878
1879 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001880 if (obj->write_domain != 0)
1881 i915_gem_object_move_to_flushing(obj);
1882 else
1883 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001884 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001885
1886 if (unlikely (dev_priv->trace_irq_seqno &&
1887 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001888 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001889 dev_priv->trace_irq_seqno = 0;
1890 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001891
1892 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001893}
1894
1895void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001896i915_gem_retire_requests(struct drm_device *dev)
1897{
1898 drm_i915_private_t *dev_priv = dev->dev_private;
1899
Chris Wilsonbe726152010-07-23 23:18:50 +01001900 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1901 struct drm_i915_gem_object *obj_priv, *tmp;
1902
1903 /* We must be careful that during unbind() we do not
1904 * accidentally infinitely recurse into retire requests.
1905 * Currently:
1906 * retire -> free -> unbind -> wait -> retire_ring
1907 */
1908 list_for_each_entry_safe(obj_priv, tmp,
1909 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001910 mm_list)
Chris Wilsonbe726152010-07-23 23:18:50 +01001911 i915_gem_free_object_tail(&obj_priv->base);
1912 }
1913
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001914 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001915 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001916 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001917}
1918
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001919static void
Eric Anholt673a3942008-07-30 12:06:12 -07001920i915_gem_retire_work_handler(struct work_struct *work)
1921{
1922 drm_i915_private_t *dev_priv;
1923 struct drm_device *dev;
1924
1925 dev_priv = container_of(work, drm_i915_private_t,
1926 mm.retire_work.work);
1927 dev = dev_priv->dev;
1928
Chris Wilson891b48c2010-09-29 12:26:37 +01001929 /* Come back later if the device is busy... */
1930 if (!mutex_trylock(&dev->struct_mutex)) {
1931 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1932 return;
1933 }
1934
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001935 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001936
Keith Packard6dbe2772008-10-14 21:41:13 -07001937 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001938 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01001939 !list_empty(&dev_priv->bsd_ring.request_list) ||
1940 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001941 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001942 mutex_unlock(&dev->struct_mutex);
1943}
1944
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001945int
Zou Nan hai852835f2010-05-21 09:08:56 +08001946i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001947 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001948{
1949 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001950 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001951 int ret = 0;
1952
1953 BUG_ON(seqno == 0);
1954
Ben Gamariba1234d2009-09-14 17:48:47 -04001955 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001956 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04001957
Chris Wilsona56ba562010-09-28 10:07:56 +01001958 if (ring->outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01001959 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001960 if (seqno == 0)
1961 return -ENOMEM;
1962 }
Chris Wilsona56ba562010-09-28 10:07:56 +01001963 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001964
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001965 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001966 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001967 ier = I915_READ(DEIER) | I915_READ(GTIER);
1968 else
1969 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001970 if (!ier) {
1971 DRM_ERROR("something (likely vbetool) disabled "
1972 "interrupts, re-enabling\n");
1973 i915_driver_irq_preinstall(dev);
1974 i915_driver_irq_postinstall(dev);
1975 }
1976
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001977 trace_i915_gem_request_wait_begin(dev, seqno);
1978
Zou Nan hai852835f2010-05-21 09:08:56 +08001979 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001980 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001981 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001982 ret = wait_event_interruptible(ring->irq_queue,
1983 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001984 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001985 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001986 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001987 wait_event(ring->irq_queue,
1988 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001989 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001990 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001991
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001992 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001993 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001994
1995 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001996 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001997 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001998 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07001999
2000 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002001 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002002 __func__, ret, seqno, ring->get_seqno(dev, ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002003 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002004
2005 /* Directly dispatch request retiring. While we have the work queue
2006 * to handle this, the waiter on a request often wants an associated
2007 * buffer to have made it to the inactive list, and we would need
2008 * a separate wait queue to handle that.
2009 */
2010 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002011 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002012
2013 return ret;
2014}
2015
Daniel Vetter48764bf2009-09-15 22:57:32 +02002016/**
2017 * Waits for a sequence number to be signaled, and cleans up the
2018 * request and object lists appropriately for that event.
2019 */
2020static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002021i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002022 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002023{
Zou Nan hai852835f2010-05-21 09:08:56 +08002024 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002025}
2026
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002027static void
Chris Wilson92204342010-09-18 11:02:01 +01002028i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002029 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002030 struct intel_ring_buffer *ring,
2031 uint32_t invalidate_domains,
2032 uint32_t flush_domains)
2033{
2034 ring->flush(dev, ring, invalidate_domains, flush_domains);
2035 i915_gem_process_flushing_list(dev, flush_domains, ring);
2036}
2037
2038static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002039i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002040 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002041 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002042 uint32_t flush_domains,
2043 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002044{
2045 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002046
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002047 if (flush_domains & I915_GEM_DOMAIN_CPU)
2048 drm_agp_chipset_flush(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002049
Chris Wilson92204342010-09-18 11:02:01 +01002050 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2051 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002052 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002053 &dev_priv->render_ring,
2054 invalidate_domains, flush_domains);
2055 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002056 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002057 &dev_priv->bsd_ring,
2058 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002059 if (flush_rings & RING_BLT)
2060 i915_gem_flush_ring(dev, file_priv,
2061 &dev_priv->blt_ring,
2062 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002063 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002064}
2065
Eric Anholt673a3942008-07-30 12:06:12 -07002066/**
2067 * Ensures that all rendering to the object has completed and the object is
2068 * safe to unbind from the GTT or access from the CPU.
2069 */
2070static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002071i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2072 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002073{
2074 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002075 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002076 int ret;
2077
Eric Anholte47c68e2008-11-14 13:35:19 -08002078 /* This function only exists to support waiting for existing rendering,
2079 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002080 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002081 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002082
2083 /* If there is rendering queued on the buffer being evicted, wait for
2084 * it.
2085 */
2086 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002087 ret = i915_do_wait_request(dev,
2088 obj_priv->last_rendering_seqno,
2089 interruptible,
2090 obj_priv->ring);
2091 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002092 return ret;
2093 }
2094
2095 return 0;
2096}
2097
2098/**
2099 * Unbinds an object from the GTT aperture.
2100 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002101int
Eric Anholt673a3942008-07-30 12:06:12 -07002102i915_gem_object_unbind(struct drm_gem_object *obj)
2103{
2104 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002105 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002106 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002107 int ret = 0;
2108
Eric Anholt673a3942008-07-30 12:06:12 -07002109 if (obj_priv->gtt_space == NULL)
2110 return 0;
2111
2112 if (obj_priv->pin_count != 0) {
2113 DRM_ERROR("Attempting to unbind pinned buffer\n");
2114 return -EINVAL;
2115 }
2116
Eric Anholt5323fd02009-09-09 11:50:45 -07002117 /* blow away mappings if mapped through GTT */
2118 i915_gem_release_mmap(obj);
2119
Eric Anholt673a3942008-07-30 12:06:12 -07002120 /* Move the object to the CPU domain to ensure that
2121 * any possible CPU writes while it's not in the GTT
2122 * are flushed when we go to remap it. This will
2123 * also ensure that all pending GPU writes are finished
2124 * before we unbind.
2125 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002126 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002127 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002128 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002129 /* Continue on if we fail due to EIO, the GPU is hung so we
2130 * should be safe and we need to cleanup or else we might
2131 * cause memory corruption through use-after-free.
2132 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002133 if (ret) {
2134 i915_gem_clflush_object(obj);
2135 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2136 }
Eric Anholt673a3942008-07-30 12:06:12 -07002137
Daniel Vetter96b47b62009-12-15 17:50:00 +01002138 /* release the fence reg _after_ flushing */
2139 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2140 i915_gem_clear_fence_reg(obj);
2141
Chris Wilson73aa8082010-09-30 11:46:12 +01002142 drm_unbind_agp(obj_priv->agp_mem);
2143 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002144
Eric Anholt856fa192009-03-19 14:10:50 -07002145 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002146 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002147
Chris Wilson73aa8082010-09-30 11:46:12 +01002148 i915_gem_info_remove_gtt(dev_priv, obj->size);
Chris Wilson69dc4982010-10-19 10:36:51 +01002149 list_del_init(&obj_priv->mm_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002150
Chris Wilson73aa8082010-09-30 11:46:12 +01002151 drm_mm_put_block(obj_priv->gtt_space);
2152 obj_priv->gtt_space = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01002153 obj_priv->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002154
Chris Wilson963b4832009-09-20 23:03:54 +01002155 if (i915_gem_object_is_purgeable(obj_priv))
2156 i915_gem_object_truncate(obj);
2157
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002158 trace_i915_gem_object_unbind(obj);
2159
Chris Wilson8dc17752010-07-23 23:18:51 +01002160 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002161}
2162
Chris Wilsona56ba562010-09-28 10:07:56 +01002163static int i915_ring_idle(struct drm_device *dev,
2164 struct intel_ring_buffer *ring)
2165{
Chris Wilson395b70b2010-10-28 21:28:46 +01002166 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002167 return 0;
2168
Chris Wilsona56ba562010-09-28 10:07:56 +01002169 i915_gem_flush_ring(dev, NULL, ring,
2170 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2171 return i915_wait_request(dev,
2172 i915_gem_next_request_seqno(dev, ring),
2173 ring);
2174}
2175
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002176int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002177i915_gpu_idle(struct drm_device *dev)
2178{
2179 drm_i915_private_t *dev_priv = dev->dev_private;
2180 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002181 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002182
Zou Nan haid1b851f2010-05-21 09:08:57 +08002183 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002184 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002185 if (lists_empty)
2186 return 0;
2187
2188 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002189 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002190 if (ret)
2191 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002192
Chris Wilson87acb0a2010-10-19 10:13:00 +01002193 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2194 if (ret)
2195 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002196
Chris Wilson549f7362010-10-19 11:19:32 +01002197 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2198 if (ret)
2199 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002200
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002201 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002202}
2203
Chris Wilson5cdf5882010-09-27 15:51:07 +01002204static int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002205i915_gem_object_get_pages(struct drm_gem_object *obj,
2206 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002207{
Daniel Vetter23010e42010-03-08 13:35:02 +01002208 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002209 int page_count, i;
2210 struct address_space *mapping;
2211 struct inode *inode;
2212 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002213
Daniel Vetter778c3542010-05-13 11:49:44 +02002214 BUG_ON(obj_priv->pages_refcount
2215 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2216
Eric Anholt856fa192009-03-19 14:10:50 -07002217 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002218 return 0;
2219
2220 /* Get the list of pages out of our struct file. They'll be pinned
2221 * at this point until we release them.
2222 */
2223 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002224 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002225 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002226 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002227 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002228 return -ENOMEM;
2229 }
2230
2231 inode = obj->filp->f_path.dentry->d_inode;
2232 mapping = inode->i_mapping;
2233 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002234 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002235 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002236 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002237 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002238 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002239 if (IS_ERR(page))
2240 goto err_pages;
2241
Eric Anholt856fa192009-03-19 14:10:50 -07002242 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002243 }
Eric Anholt280b7132009-03-12 16:56:27 -07002244
2245 if (obj_priv->tiling_mode != I915_TILING_NONE)
2246 i915_gem_object_do_bit_17_swizzle(obj);
2247
Eric Anholt673a3942008-07-30 12:06:12 -07002248 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002249
2250err_pages:
2251 while (i--)
2252 page_cache_release(obj_priv->pages[i]);
2253
2254 drm_free_large(obj_priv->pages);
2255 obj_priv->pages = NULL;
2256 obj_priv->pages_refcount--;
2257 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002258}
2259
Eric Anholt4e901fd2009-10-26 16:44:17 -07002260static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2261{
2262 struct drm_gem_object *obj = reg->obj;
2263 struct drm_device *dev = obj->dev;
2264 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002265 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002266 int regnum = obj_priv->fence_reg;
2267 uint64_t val;
2268
2269 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2270 0xfffff000) << 32;
2271 val |= obj_priv->gtt_offset & 0xfffff000;
2272 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2273 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2274
2275 if (obj_priv->tiling_mode == I915_TILING_Y)
2276 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2277 val |= I965_FENCE_REG_VALID;
2278
2279 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2280}
2281
Jesse Barnesde151cf2008-11-12 10:03:55 -08002282static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2283{
2284 struct drm_gem_object *obj = reg->obj;
2285 struct drm_device *dev = obj->dev;
2286 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002287 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002288 int regnum = obj_priv->fence_reg;
2289 uint64_t val;
2290
2291 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2292 0xfffff000) << 32;
2293 val |= obj_priv->gtt_offset & 0xfffff000;
2294 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2295 if (obj_priv->tiling_mode == I915_TILING_Y)
2296 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2297 val |= I965_FENCE_REG_VALID;
2298
2299 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2300}
2301
2302static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2303{
2304 struct drm_gem_object *obj = reg->obj;
2305 struct drm_device *dev = obj->dev;
2306 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002307 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002309 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002310 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002311 uint32_t pitch_val;
2312
2313 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2314 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002315 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002316 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317 return;
2318 }
2319
Jesse Barnes0f973f22009-01-26 17:10:45 -08002320 if (obj_priv->tiling_mode == I915_TILING_Y &&
2321 HAS_128_BYTE_Y_TILING(dev))
2322 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002323 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002324 tile_width = 512;
2325
2326 /* Note: pitch better be a power of two tile widths */
2327 pitch_val = obj_priv->stride / tile_width;
2328 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002329
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002330 if (obj_priv->tiling_mode == I915_TILING_Y &&
2331 HAS_128_BYTE_Y_TILING(dev))
2332 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2333 else
2334 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2335
Jesse Barnesde151cf2008-11-12 10:03:55 -08002336 val = obj_priv->gtt_offset;
2337 if (obj_priv->tiling_mode == I915_TILING_Y)
2338 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2339 val |= I915_FENCE_SIZE_BITS(obj->size);
2340 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2341 val |= I830_FENCE_REG_VALID;
2342
Eric Anholtdc529a42009-03-10 22:34:49 -07002343 if (regnum < 8)
2344 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2345 else
2346 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2347 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002348}
2349
2350static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2351{
2352 struct drm_gem_object *obj = reg->obj;
2353 struct drm_device *dev = obj->dev;
2354 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002355 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356 int regnum = obj_priv->fence_reg;
2357 uint32_t val;
2358 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002359 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002360
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002361 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002363 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002364 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002365 return;
2366 }
2367
Eric Anholte76a16d2009-05-26 17:44:56 -07002368 pitch_val = obj_priv->stride / 128;
2369 pitch_val = ffs(pitch_val) - 1;
2370 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2371
Jesse Barnesde151cf2008-11-12 10:03:55 -08002372 val = obj_priv->gtt_offset;
2373 if (obj_priv->tiling_mode == I915_TILING_Y)
2374 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002375 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2376 WARN_ON(fence_size_bits & ~0x00000f00);
2377 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002378 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2379 val |= I830_FENCE_REG_VALID;
2380
2381 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002382}
2383
Chris Wilson2cf34d72010-09-14 13:03:28 +01002384static int i915_find_fence_reg(struct drm_device *dev,
2385 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002386{
2387 struct drm_i915_fence_reg *reg = NULL;
2388 struct drm_i915_gem_object *obj_priv = NULL;
2389 struct drm_i915_private *dev_priv = dev->dev_private;
2390 struct drm_gem_object *obj = NULL;
2391 int i, avail, ret;
2392
2393 /* First try to find a free reg */
2394 avail = 0;
2395 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2396 reg = &dev_priv->fence_regs[i];
2397 if (!reg->obj)
2398 return i;
2399
Daniel Vetter23010e42010-03-08 13:35:02 +01002400 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002401 if (!obj_priv->pin_count)
2402 avail++;
2403 }
2404
2405 if (avail == 0)
2406 return -ENOSPC;
2407
2408 /* None available, try to steal one or wait for a user to finish */
2409 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002410 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2411 lru_list) {
2412 obj = reg->obj;
2413 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002414
2415 if (obj_priv->pin_count)
2416 continue;
2417
2418 /* found one! */
2419 i = obj_priv->fence_reg;
2420 break;
2421 }
2422
2423 BUG_ON(i == I915_FENCE_REG_NONE);
2424
2425 /* We only have a reference on obj from the active list. put_fence_reg
2426 * might drop that one, causing a use-after-free in it. So hold a
2427 * private reference to obj like the other callers of put_fence_reg
2428 * (set_tiling ioctl) do. */
2429 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002430 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002431 drm_gem_object_unreference(obj);
2432 if (ret != 0)
2433 return ret;
2434
2435 return i;
2436}
2437
Jesse Barnesde151cf2008-11-12 10:03:55 -08002438/**
2439 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2440 * @obj: object to map through a fence reg
2441 *
2442 * When mapping objects through the GTT, userspace wants to be able to write
2443 * to them without having to worry about swizzling if the object is tiled.
2444 *
2445 * This function walks the fence regs looking for a free one for @obj,
2446 * stealing one if it can't find any.
2447 *
2448 * It then sets up the reg based on the object's properties: address, pitch
2449 * and tiling format.
2450 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002451int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002452i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2453 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002454{
2455 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002456 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002457 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002458 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002459 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002460
Eric Anholta09ba7f2009-08-29 12:49:51 -07002461 /* Just update our place in the LRU if our fence is getting used. */
2462 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002463 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2464 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002465 return 0;
2466 }
2467
Jesse Barnesde151cf2008-11-12 10:03:55 -08002468 switch (obj_priv->tiling_mode) {
2469 case I915_TILING_NONE:
2470 WARN(1, "allocating a fence for non-tiled object?\n");
2471 break;
2472 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002473 if (!obj_priv->stride)
2474 return -EINVAL;
2475 WARN((obj_priv->stride & (512 - 1)),
2476 "object 0x%08x is X tiled but has non-512B pitch\n",
2477 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002478 break;
2479 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002480 if (!obj_priv->stride)
2481 return -EINVAL;
2482 WARN((obj_priv->stride & (128 - 1)),
2483 "object 0x%08x is Y tiled but has non-128B pitch\n",
2484 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002485 break;
2486 }
2487
Chris Wilson2cf34d72010-09-14 13:03:28 +01002488 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002489 if (ret < 0)
2490 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002491
Daniel Vetterae3db242010-02-19 11:51:58 +01002492 obj_priv->fence_reg = ret;
2493 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002494 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002495
Jesse Barnesde151cf2008-11-12 10:03:55 -08002496 reg->obj = obj;
2497
Chris Wilsone259bef2010-09-17 00:32:02 +01002498 switch (INTEL_INFO(dev)->gen) {
2499 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002500 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002501 break;
2502 case 5:
2503 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002504 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002505 break;
2506 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002507 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002508 break;
2509 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002510 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002511 break;
2512 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002513
Daniel Vetterae3db242010-02-19 11:51:58 +01002514 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2515 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002516
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002517 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002518}
2519
2520/**
2521 * i915_gem_clear_fence_reg - clear out fence register info
2522 * @obj: object to clear
2523 *
2524 * Zeroes out the fence register itself and clears out the associated
2525 * data structures in dev_priv and obj_priv.
2526 */
2527static void
2528i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2529{
2530 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002531 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002532 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002533 struct drm_i915_fence_reg *reg =
2534 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002535 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002536
Chris Wilsone259bef2010-09-17 00:32:02 +01002537 switch (INTEL_INFO(dev)->gen) {
2538 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002539 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2540 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002541 break;
2542 case 5:
2543 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002544 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002545 break;
2546 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002547 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002548 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002549 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002550 case 2:
2551 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002552
2553 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002554 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002555 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002556
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002557 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002558 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002559 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002560}
2561
Eric Anholt673a3942008-07-30 12:06:12 -07002562/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002563 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2564 * to the buffer to finish, and then resets the fence register.
2565 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002566 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002567 *
2568 * Zeroes out the fence register itself and clears out the associated
2569 * data structures in dev_priv and obj_priv.
2570 */
2571int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002572i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2573 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002574{
2575 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002576 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002577 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002578 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002579
2580 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2581 return 0;
2582
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002583 /* If we've changed tiling, GTT-mappings of the object
2584 * need to re-fault to ensure that the correct fence register
2585 * setup is in place.
2586 */
2587 i915_gem_release_mmap(obj);
2588
Chris Wilson52dc7d32009-06-06 09:46:01 +01002589 /* On the i915, GPU access to tiled buffers is via a fence,
2590 * therefore we must wait for any outstanding access to complete
2591 * before clearing the fence.
2592 */
Chris Wilson53640e12010-09-20 11:40:50 +01002593 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2594 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002595 int ret;
2596
Chris Wilson2cf34d72010-09-14 13:03:28 +01002597 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002598 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002599 return ret;
2600
Chris Wilson2cf34d72010-09-14 13:03:28 +01002601 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002602 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002603 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002604
2605 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002606 }
2607
Daniel Vetter4a726612010-02-01 13:59:16 +01002608 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002609 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002610
2611 return 0;
2612}
2613
2614/**
Eric Anholt673a3942008-07-30 12:06:12 -07002615 * Finds free space in the GTT aperture and binds the object there.
2616 */
2617static int
2618i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2619{
2620 struct drm_device *dev = obj->dev;
2621 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002622 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002623 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002624 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002625 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002626
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002627 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002628 DRM_ERROR("Attempting to bind a purgeable object\n");
2629 return -EINVAL;
2630 }
2631
Eric Anholt673a3942008-07-30 12:06:12 -07002632 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002633 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002634 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002635 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2636 return -EINVAL;
2637 }
2638
Chris Wilson654fc602010-05-27 13:18:21 +01002639 /* If the object is bigger than the entire aperture, reject it early
2640 * before evicting everything in a vain attempt to find space.
2641 */
Chris Wilson73aa8082010-09-30 11:46:12 +01002642 if (obj->size > dev_priv->mm.gtt_total) {
Chris Wilson654fc602010-05-27 13:18:21 +01002643 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2644 return -E2BIG;
2645 }
2646
Eric Anholt673a3942008-07-30 12:06:12 -07002647 search_free:
2648 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2649 obj->size, alignment, 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01002650 if (free_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002651 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2652 alignment);
Eric Anholt673a3942008-07-30 12:06:12 -07002653 if (obj_priv->gtt_space == NULL) {
2654 /* If the gtt is empty and we're still having trouble
2655 * fitting our object in, we're out of memory.
2656 */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002657 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002658 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002659 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002660
Eric Anholt673a3942008-07-30 12:06:12 -07002661 goto search_free;
2662 }
2663
Chris Wilson4bdadb92010-01-27 13:36:32 +00002664 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002665 if (ret) {
2666 drm_mm_put_block(obj_priv->gtt_space);
2667 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002668
2669 if (ret == -ENOMEM) {
2670 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002671 ret = i915_gem_evict_something(dev, obj->size,
2672 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002673 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002674 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002675 if (gfpmask) {
2676 gfpmask = 0;
2677 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002678 }
2679
2680 return ret;
2681 }
2682
2683 goto search_free;
2684 }
2685
Eric Anholt673a3942008-07-30 12:06:12 -07002686 return ret;
2687 }
2688
Eric Anholt673a3942008-07-30 12:06:12 -07002689 /* Create an AGP memory structure pointing at our pages, and bind it
2690 * into the GTT.
2691 */
2692 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002693 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002694 obj->size >> PAGE_SHIFT,
Chris Wilson9af90d12010-10-17 10:01:56 +01002695 obj_priv->gtt_space->start,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002696 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002697 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002698 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002699 drm_mm_put_block(obj_priv->gtt_space);
2700 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002701
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002702 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002703 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002704 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002705
2706 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002707 }
Eric Anholt673a3942008-07-30 12:06:12 -07002708
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002709 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson69dc4982010-10-19 10:36:51 +01002710 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01002711 i915_gem_info_add_gtt(dev_priv, obj->size);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002712
Eric Anholt673a3942008-07-30 12:06:12 -07002713 /* Assert that the object is not currently in any GPU domain. As it
2714 * wasn't in the GTT, there shouldn't be any way it could have been in
2715 * a GPU cache
2716 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002717 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2718 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002719
Chris Wilson9af90d12010-10-17 10:01:56 +01002720 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002721 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2722
Eric Anholt673a3942008-07-30 12:06:12 -07002723 return 0;
2724}
2725
2726void
2727i915_gem_clflush_object(struct drm_gem_object *obj)
2728{
Daniel Vetter23010e42010-03-08 13:35:02 +01002729 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002730
2731 /* If we don't have a page list set up, then we're not pinned
2732 * to GPU, and we can ignore the cache flush because it'll happen
2733 * again at bind time.
2734 */
Eric Anholt856fa192009-03-19 14:10:50 -07002735 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002736 return;
2737
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002738 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002739
Eric Anholt856fa192009-03-19 14:10:50 -07002740 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002741}
2742
Eric Anholte47c68e2008-11-14 13:35:19 -08002743/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002744static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002745i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2746 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002747{
2748 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002749 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002750
2751 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002752 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002753
2754 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002755 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002756 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002757 to_intel_bo(obj)->ring,
2758 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002759 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002760
2761 trace_i915_gem_object_change_domain(obj,
2762 obj->read_domains,
2763 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002764
2765 if (pipelined)
2766 return 0;
2767
Chris Wilson2cf34d72010-09-14 13:03:28 +01002768 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002769}
2770
2771/** Flushes the GTT write domain for the object if it's dirty. */
2772static void
2773i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2774{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002775 uint32_t old_write_domain;
2776
Eric Anholte47c68e2008-11-14 13:35:19 -08002777 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2778 return;
2779
2780 /* No actual flushing is required for the GTT write domain. Writes
2781 * to it immediately go to main memory as far as we know, so there's
2782 * no chipset flush. It also doesn't land in render cache.
2783 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002784 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002785 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002786
2787 trace_i915_gem_object_change_domain(obj,
2788 obj->read_domains,
2789 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002790}
2791
2792/** Flushes the CPU write domain for the object if it's dirty. */
2793static void
2794i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2795{
2796 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002797 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002798
2799 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2800 return;
2801
2802 i915_gem_clflush_object(obj);
2803 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002804 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002805 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002806
2807 trace_i915_gem_object_change_domain(obj,
2808 obj->read_domains,
2809 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002810}
2811
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002812/**
2813 * Moves a single object to the GTT read, and possibly write domain.
2814 *
2815 * This function returns when the move is complete, including waiting on
2816 * flushes to occur.
2817 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002818int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002819i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2820{
Daniel Vetter23010e42010-03-08 13:35:02 +01002821 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002822 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002823 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002824
Eric Anholt02354392008-11-26 13:58:13 -08002825 /* Not valid to be called on unbound objects. */
2826 if (obj_priv->gtt_space == NULL)
2827 return -EINVAL;
2828
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002829 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002830 if (ret != 0)
2831 return ret;
2832
Chris Wilson72133422010-09-13 23:56:38 +01002833 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002834
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002835 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002836 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002837 if (ret)
2838 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002839 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002840
2841 old_write_domain = obj->write_domain;
2842 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002843
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002844 /* It should now be out of any other write domains, and we can update
2845 * the domain values for our changes.
2846 */
2847 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2848 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002849 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002850 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002851 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002852 obj_priv->dirty = 1;
2853 }
2854
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002855 trace_i915_gem_object_change_domain(obj,
2856 old_read_domains,
2857 old_write_domain);
2858
Eric Anholte47c68e2008-11-14 13:35:19 -08002859 return 0;
2860}
2861
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002862/*
2863 * Prepare buffer for display plane. Use uninterruptible for possible flush
2864 * wait, as in modesetting process we're not supposed to be interrupted.
2865 */
2866int
Chris Wilson48b956c2010-09-14 12:50:34 +01002867i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2868 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002869{
Daniel Vetter23010e42010-03-08 13:35:02 +01002870 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002871 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002872 int ret;
2873
2874 /* Not valid to be called on unbound objects. */
2875 if (obj_priv->gtt_space == NULL)
2876 return -EINVAL;
2877
Chris Wilsonced270f2010-09-26 22:47:46 +01002878 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002879 if (ret)
2880 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002881
Chris Wilsonced270f2010-09-26 22:47:46 +01002882 /* Currently, we are always called from an non-interruptible context. */
2883 if (!pipelined) {
2884 ret = i915_gem_object_wait_rendering(obj, false);
2885 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002886 return ret;
2887 }
2888
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002889 i915_gem_object_flush_cpu_write_domain(obj);
2890
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002891 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002892 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002893
2894 trace_i915_gem_object_change_domain(obj,
2895 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002896 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002897
2898 return 0;
2899}
2900
Chris Wilson85345512010-11-13 09:49:11 +00002901int
2902i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
2903 bool interruptible)
2904{
2905 if (!obj->active)
2906 return 0;
2907
2908 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
2909 i915_gem_flush_ring(obj->base.dev, NULL, obj->ring,
2910 0, obj->base.write_domain);
2911
2912 return i915_gem_object_wait_rendering(&obj->base, interruptible);
2913}
2914
Eric Anholte47c68e2008-11-14 13:35:19 -08002915/**
2916 * Moves a single object to the CPU read, and possibly write domain.
2917 *
2918 * This function returns when the move is complete, including waiting on
2919 * flushes to occur.
2920 */
2921static int
2922i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2923{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002924 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002925 int ret;
2926
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002927 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002928 if (ret != 0)
2929 return ret;
2930
2931 i915_gem_object_flush_gtt_write_domain(obj);
2932
2933 /* If we have a partially-valid cache of the object in the CPU,
2934 * finish invalidating it and free the per-page flags.
2935 */
2936 i915_gem_object_set_to_full_cpu_read_domain(obj);
2937
Chris Wilson72133422010-09-13 23:56:38 +01002938 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002939 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002940 if (ret)
2941 return ret;
2942 }
2943
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002944 old_write_domain = obj->write_domain;
2945 old_read_domains = obj->read_domains;
2946
Eric Anholte47c68e2008-11-14 13:35:19 -08002947 /* Flush the CPU cache if it's still invalid. */
2948 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2949 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002950
2951 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2952 }
2953
2954 /* It should now be out of any other write domains, and we can update
2955 * the domain values for our changes.
2956 */
2957 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2958
2959 /* If we're writing through the CPU, then the GPU read domains will
2960 * need to be invalidated at next use.
2961 */
2962 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002963 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002964 obj->write_domain = I915_GEM_DOMAIN_CPU;
2965 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002966
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002967 trace_i915_gem_object_change_domain(obj,
2968 old_read_domains,
2969 old_write_domain);
2970
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002971 return 0;
2972}
2973
Eric Anholt673a3942008-07-30 12:06:12 -07002974/*
2975 * Set the next domain for the specified object. This
2976 * may not actually perform the necessary flushing/invaliding though,
2977 * as that may want to be batched with other set_domain operations
2978 *
2979 * This is (we hope) the only really tricky part of gem. The goal
2980 * is fairly simple -- track which caches hold bits of the object
2981 * and make sure they remain coherent. A few concrete examples may
2982 * help to explain how it works. For shorthand, we use the notation
2983 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2984 * a pair of read and write domain masks.
2985 *
2986 * Case 1: the batch buffer
2987 *
2988 * 1. Allocated
2989 * 2. Written by CPU
2990 * 3. Mapped to GTT
2991 * 4. Read by GPU
2992 * 5. Unmapped from GTT
2993 * 6. Freed
2994 *
2995 * Let's take these a step at a time
2996 *
2997 * 1. Allocated
2998 * Pages allocated from the kernel may still have
2999 * cache contents, so we set them to (CPU, CPU) always.
3000 * 2. Written by CPU (using pwrite)
3001 * The pwrite function calls set_domain (CPU, CPU) and
3002 * this function does nothing (as nothing changes)
3003 * 3. Mapped by GTT
3004 * This function asserts that the object is not
3005 * currently in any GPU-based read or write domains
3006 * 4. Read by GPU
3007 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3008 * As write_domain is zero, this function adds in the
3009 * current read domains (CPU+COMMAND, 0).
3010 * flush_domains is set to CPU.
3011 * invalidate_domains is set to COMMAND
3012 * clflush is run to get data out of the CPU caches
3013 * then i915_dev_set_domain calls i915_gem_flush to
3014 * emit an MI_FLUSH and drm_agp_chipset_flush
3015 * 5. Unmapped from GTT
3016 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3017 * flush_domains and invalidate_domains end up both zero
3018 * so no flushing/invalidating happens
3019 * 6. Freed
3020 * yay, done
3021 *
3022 * Case 2: The shared render buffer
3023 *
3024 * 1. Allocated
3025 * 2. Mapped to GTT
3026 * 3. Read/written by GPU
3027 * 4. set_domain to (CPU,CPU)
3028 * 5. Read/written by CPU
3029 * 6. Read/written by GPU
3030 *
3031 * 1. Allocated
3032 * Same as last example, (CPU, CPU)
3033 * 2. Mapped to GTT
3034 * Nothing changes (assertions find that it is not in the GPU)
3035 * 3. Read/written by GPU
3036 * execbuffer calls set_domain (RENDER, RENDER)
3037 * flush_domains gets CPU
3038 * invalidate_domains gets GPU
3039 * clflush (obj)
3040 * MI_FLUSH and drm_agp_chipset_flush
3041 * 4. set_domain (CPU, CPU)
3042 * flush_domains gets GPU
3043 * invalidate_domains gets CPU
3044 * wait_rendering (obj) to make sure all drawing is complete.
3045 * This will include an MI_FLUSH to get the data from GPU
3046 * to memory
3047 * clflush (obj) to invalidate the CPU cache
3048 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3049 * 5. Read/written by CPU
3050 * cache lines are loaded and dirtied
3051 * 6. Read written by GPU
3052 * Same as last GPU access
3053 *
3054 * Case 3: The constant buffer
3055 *
3056 * 1. Allocated
3057 * 2. Written by CPU
3058 * 3. Read by GPU
3059 * 4. Updated (written) by CPU again
3060 * 5. Read by GPU
3061 *
3062 * 1. Allocated
3063 * (CPU, CPU)
3064 * 2. Written by CPU
3065 * (CPU, CPU)
3066 * 3. Read by GPU
3067 * (CPU+RENDER, 0)
3068 * flush_domains = CPU
3069 * invalidate_domains = RENDER
3070 * clflush (obj)
3071 * MI_FLUSH
3072 * drm_agp_chipset_flush
3073 * 4. Updated (written) by CPU again
3074 * (CPU, CPU)
3075 * flush_domains = 0 (no previous write domain)
3076 * invalidate_domains = 0 (no new read domains)
3077 * 5. Read by GPU
3078 * (CPU+RENDER, 0)
3079 * flush_domains = CPU
3080 * invalidate_domains = RENDER
3081 * clflush (obj)
3082 * MI_FLUSH
3083 * drm_agp_chipset_flush
3084 */
Keith Packardc0d90822008-11-20 23:11:08 -08003085static void
Chris Wilsonb6651452010-10-23 10:15:06 +01003086i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
3087 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07003088{
3089 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003090 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003091 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003092 uint32_t invalidate_domains = 0;
3093 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003094 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003095
Jesse Barnes652c3932009-08-17 13:31:43 -07003096 intel_mark_busy(dev, obj);
3097
Eric Anholt673a3942008-07-30 12:06:12 -07003098 /*
3099 * If the object isn't moving to a new write domain,
3100 * let the object stay in multiple read domains
3101 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003102 if (obj->pending_write_domain == 0)
3103 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003104 else
3105 obj_priv->dirty = 1;
3106
3107 /*
3108 * Flush the current write domain if
3109 * the new read domains don't match. Invalidate
3110 * any read domains which differ from the old
3111 * write domain
3112 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003113 if (obj->write_domain &&
Chris Wilsonc6afd652010-11-01 13:39:24 +00003114 (obj->write_domain != obj->pending_read_domains ||
3115 obj_priv->ring != ring)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003116 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003117 invalidate_domains |=
3118 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003119 }
3120 /*
3121 * Invalidate any read caches which may have
3122 * stale data. That is, any new read domains.
3123 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003124 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003125 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003126 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003127
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003128 old_read_domains = obj->read_domains;
3129
Eric Anholtefbeed92009-02-19 14:54:51 -08003130 /* The actual obj->write_domain will be updated with
3131 * pending_write_domain after we emit the accumulated flush for all
3132 * of our domain changes in execbuffers (which clears objects'
3133 * write_domains). So if we have a current write domain that we
3134 * aren't changing, set pending_write_domain to that.
3135 */
3136 if (flush_domains == 0 && obj->pending_write_domain == 0)
3137 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003138 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003139
3140 dev->invalidate_domains |= invalidate_domains;
3141 dev->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003142 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson92204342010-09-18 11:02:01 +01003143 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003144 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
3145 dev_priv->mm.flush_rings |= ring->id;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003146
3147 trace_i915_gem_object_change_domain(obj,
3148 old_read_domains,
3149 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003150}
3151
3152/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003153 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003154 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003155 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3156 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3157 */
3158static void
3159i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3160{
Daniel Vetter23010e42010-03-08 13:35:02 +01003161 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003162
3163 if (!obj_priv->page_cpu_valid)
3164 return;
3165
3166 /* If we're partially in the CPU read domain, finish moving it in.
3167 */
3168 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3169 int i;
3170
3171 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3172 if (obj_priv->page_cpu_valid[i])
3173 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003174 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003175 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003176 }
3177
3178 /* Free the page_cpu_valid mappings which are now stale, whether
3179 * or not we've got I915_GEM_DOMAIN_CPU.
3180 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003181 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003182 obj_priv->page_cpu_valid = NULL;
3183}
3184
3185/**
3186 * Set the CPU read domain on a range of the object.
3187 *
3188 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3189 * not entirely valid. The page_cpu_valid member of the object flags which
3190 * pages have been flushed, and will be respected by
3191 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3192 * of the whole object.
3193 *
3194 * This function returns when the move is complete, including waiting on
3195 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003196 */
3197static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003198i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3199 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003200{
Daniel Vetter23010e42010-03-08 13:35:02 +01003201 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003202 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003203 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003204
Eric Anholte47c68e2008-11-14 13:35:19 -08003205 if (offset == 0 && size == obj->size)
3206 return i915_gem_object_set_to_cpu_domain(obj, 0);
3207
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003208 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003209 if (ret != 0)
3210 return ret;
3211 i915_gem_object_flush_gtt_write_domain(obj);
3212
3213 /* If we're already fully in the CPU read domain, we're done. */
3214 if (obj_priv->page_cpu_valid == NULL &&
3215 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003216 return 0;
3217
Eric Anholte47c68e2008-11-14 13:35:19 -08003218 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3219 * newly adding I915_GEM_DOMAIN_CPU
3220 */
Eric Anholt673a3942008-07-30 12:06:12 -07003221 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003222 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3223 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003224 if (obj_priv->page_cpu_valid == NULL)
3225 return -ENOMEM;
3226 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3227 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003228
3229 /* Flush the cache on any pages that are still invalid from the CPU's
3230 * perspective.
3231 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003232 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3233 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003234 if (obj_priv->page_cpu_valid[i])
3235 continue;
3236
Eric Anholt856fa192009-03-19 14:10:50 -07003237 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003238
3239 obj_priv->page_cpu_valid[i] = 1;
3240 }
3241
Eric Anholte47c68e2008-11-14 13:35:19 -08003242 /* It should now be out of any other write domains, and we can update
3243 * the domain values for our changes.
3244 */
3245 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3246
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003247 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003248 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3249
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003250 trace_i915_gem_object_change_domain(obj,
3251 old_read_domains,
3252 obj->write_domain);
3253
Eric Anholt673a3942008-07-30 12:06:12 -07003254 return 0;
3255}
3256
Eric Anholt673a3942008-07-30 12:06:12 -07003257static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003258i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
3259 struct drm_file *file_priv,
3260 struct drm_i915_gem_exec_object2 *entry,
3261 struct drm_i915_gem_relocation_entry *reloc)
Eric Anholt673a3942008-07-30 12:06:12 -07003262{
Chris Wilson9af90d12010-10-17 10:01:56 +01003263 struct drm_device *dev = obj->base.dev;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003264 struct drm_gem_object *target_obj;
3265 uint32_t target_offset;
3266 int ret = -EINVAL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003267
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003268 target_obj = drm_gem_object_lookup(dev, file_priv,
3269 reloc->target_handle);
3270 if (target_obj == NULL)
3271 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003272
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003273 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003274
Chris Wilson8542a0b2009-09-09 21:15:15 +01003275#if WATCH_RELOC
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003276 DRM_INFO("%s: obj %p offset %08x target %d "
3277 "read %08x write %08x gtt %08x "
3278 "presumed %08x delta %08x\n",
3279 __func__,
3280 obj,
3281 (int) reloc->offset,
3282 (int) reloc->target_handle,
3283 (int) reloc->read_domains,
3284 (int) reloc->write_domain,
3285 (int) target_offset,
3286 (int) reloc->presumed_offset,
3287 reloc->delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003288#endif
3289
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003290 /* The target buffer should have appeared before us in the
3291 * exec_object list, so it should have a GTT space bound by now.
3292 */
3293 if (target_offset == 0) {
3294 DRM_ERROR("No GTT space found for object %d\n",
3295 reloc->target_handle);
3296 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003297 }
3298
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003299 /* Validate that the target is in a valid r/w GPU domain */
3300 if (reloc->write_domain & (reloc->write_domain - 1)) {
3301 DRM_ERROR("reloc with multiple write domains: "
3302 "obj %p target %d offset %d "
3303 "read %08x write %08x",
3304 obj, reloc->target_handle,
3305 (int) reloc->offset,
3306 reloc->read_domains,
3307 reloc->write_domain);
3308 goto err;
3309 }
3310 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3311 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3312 DRM_ERROR("reloc with read/write CPU domains: "
3313 "obj %p target %d offset %d "
3314 "read %08x write %08x",
3315 obj, reloc->target_handle,
3316 (int) reloc->offset,
3317 reloc->read_domains,
3318 reloc->write_domain);
3319 goto err;
3320 }
3321 if (reloc->write_domain && target_obj->pending_write_domain &&
3322 reloc->write_domain != target_obj->pending_write_domain) {
3323 DRM_ERROR("Write domain conflict: "
3324 "obj %p target %d offset %d "
3325 "new %08x old %08x\n",
3326 obj, reloc->target_handle,
3327 (int) reloc->offset,
3328 reloc->write_domain,
3329 target_obj->pending_write_domain);
3330 goto err;
3331 }
3332
3333 target_obj->pending_read_domains |= reloc->read_domains;
3334 target_obj->pending_write_domain |= reloc->write_domain;
3335
3336 /* If the relocation already has the right value in it, no
3337 * more work needs to be done.
3338 */
3339 if (target_offset == reloc->presumed_offset)
3340 goto out;
3341
3342 /* Check that the relocation address is valid... */
3343 if (reloc->offset > obj->base.size - 4) {
3344 DRM_ERROR("Relocation beyond object bounds: "
3345 "obj %p target %d offset %d size %d.\n",
3346 obj, reloc->target_handle,
3347 (int) reloc->offset,
3348 (int) obj->base.size);
3349 goto err;
3350 }
3351 if (reloc->offset & 3) {
3352 DRM_ERROR("Relocation not 4-byte aligned: "
3353 "obj %p target %d offset %d.\n",
3354 obj, reloc->target_handle,
3355 (int) reloc->offset);
3356 goto err;
3357 }
3358
3359 /* and points to somewhere within the target object. */
3360 if (reloc->delta >= target_obj->size) {
3361 DRM_ERROR("Relocation beyond target object bounds: "
3362 "obj %p target %d delta %d size %d.\n",
3363 obj, reloc->target_handle,
3364 (int) reloc->delta,
3365 (int) target_obj->size);
3366 goto err;
3367 }
3368
3369 reloc->delta += target_offset;
3370 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3371 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
3372 char *vaddr;
3373
3374 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
3375 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
3376 kunmap_atomic(vaddr);
3377 } else {
3378 struct drm_i915_private *dev_priv = dev->dev_private;
3379 uint32_t __iomem *reloc_entry;
3380 void __iomem *reloc_page;
3381
3382 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3383 if (ret)
3384 goto err;
3385
3386 /* Map the page containing the relocation we're going to perform. */
3387 reloc->offset += obj->gtt_offset;
3388 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3389 reloc->offset & PAGE_MASK);
3390 reloc_entry = (uint32_t __iomem *)
3391 (reloc_page + (reloc->offset & ~PAGE_MASK));
3392 iowrite32(reloc->delta, reloc_entry);
3393 io_mapping_unmap_atomic(reloc_page);
3394 }
3395
3396 /* and update the user's relocation entry */
3397 reloc->presumed_offset = target_offset;
3398
3399out:
3400 ret = 0;
3401err:
Chris Wilson9af90d12010-10-17 10:01:56 +01003402 drm_gem_object_unreference(target_obj);
3403 return ret;
3404}
3405
3406static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003407i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
3408 struct drm_file *file_priv,
3409 struct drm_i915_gem_exec_object2 *entry)
3410{
3411 struct drm_i915_gem_relocation_entry __user *user_relocs;
3412 int i, ret;
3413
3414 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3415 for (i = 0; i < entry->relocation_count; i++) {
3416 struct drm_i915_gem_relocation_entry reloc;
3417
3418 if (__copy_from_user_inatomic(&reloc,
3419 user_relocs+i,
3420 sizeof(reloc)))
3421 return -EFAULT;
3422
3423 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
3424 if (ret)
3425 return ret;
3426
3427 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3428 &reloc.presumed_offset,
3429 sizeof(reloc.presumed_offset)))
3430 return -EFAULT;
3431 }
3432
3433 return 0;
3434}
3435
3436static int
3437i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
3438 struct drm_file *file_priv,
3439 struct drm_i915_gem_exec_object2 *entry,
3440 struct drm_i915_gem_relocation_entry *relocs)
3441{
3442 int i, ret;
3443
3444 for (i = 0; i < entry->relocation_count; i++) {
3445 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
3446 if (ret)
3447 return ret;
3448 }
3449
3450 return 0;
3451}
3452
3453static int
3454i915_gem_execbuffer_relocate(struct drm_device *dev,
3455 struct drm_file *file,
3456 struct drm_gem_object **object_list,
3457 struct drm_i915_gem_exec_object2 *exec_list,
3458 int count)
3459{
3460 int i, ret;
3461
3462 for (i = 0; i < count; i++) {
3463 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3464 obj->base.pending_read_domains = 0;
3465 obj->base.pending_write_domain = 0;
3466 ret = i915_gem_execbuffer_relocate_object(obj, file,
3467 &exec_list[i]);
3468 if (ret)
3469 return ret;
3470 }
3471
3472 return 0;
3473}
3474
3475static int
3476i915_gem_execbuffer_reserve(struct drm_device *dev,
3477 struct drm_file *file,
3478 struct drm_gem_object **object_list,
3479 struct drm_i915_gem_exec_object2 *exec_list,
3480 int count)
Chris Wilson9af90d12010-10-17 10:01:56 +01003481{
3482 struct drm_i915_private *dev_priv = dev->dev_private;
3483 int ret, i, retry;
3484
3485 /* attempt to pin all of the buffers into the GTT */
3486 for (retry = 0; retry < 2; retry++) {
3487 ret = 0;
3488 for (i = 0; i < count; i++) {
3489 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
3490 struct drm_i915_gem_object *obj= to_intel_bo(object_list[i]);
3491 bool need_fence =
3492 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3493 obj->tiling_mode != I915_TILING_NONE;
3494
3495 /* Check fence reg constraints and rebind if necessary */
3496 if (need_fence &&
3497 !i915_gem_object_fence_offset_ok(&obj->base,
3498 obj->tiling_mode)) {
3499 ret = i915_gem_object_unbind(&obj->base);
3500 if (ret)
3501 break;
3502 }
3503
3504 ret = i915_gem_object_pin(&obj->base, entry->alignment);
3505 if (ret)
3506 break;
3507
3508 /*
3509 * Pre-965 chips need a fence register set up in order
3510 * to properly handle blits to/from tiled surfaces.
3511 */
3512 if (need_fence) {
3513 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3514 if (ret) {
3515 i915_gem_object_unpin(&obj->base);
3516 break;
3517 }
3518
3519 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3520 }
3521
3522 entry->offset = obj->gtt_offset;
3523 }
3524
3525 while (i--)
3526 i915_gem_object_unpin(object_list[i]);
3527
3528 if (ret == 0)
3529 break;
3530
3531 if (ret != -ENOSPC || retry)
3532 return ret;
3533
3534 ret = i915_gem_evict_everything(dev);
3535 if (ret)
3536 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003537 }
3538
Eric Anholt673a3942008-07-30 12:06:12 -07003539 return 0;
3540}
3541
Chris Wilsonc6afd652010-11-01 13:39:24 +00003542static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003543i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
3544 struct drm_file *file,
3545 struct drm_gem_object **object_list,
3546 struct drm_i915_gem_exec_object2 *exec_list,
3547 int count)
3548{
3549 struct drm_i915_gem_relocation_entry *reloc;
3550 int i, total, ret;
3551
3552 for (i = 0; i < count; i++) {
3553 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3554 obj->in_execbuffer = false;
3555 }
3556
3557 mutex_unlock(&dev->struct_mutex);
3558
3559 total = 0;
3560 for (i = 0; i < count; i++)
3561 total += exec_list[i].relocation_count;
3562
3563 reloc = drm_malloc_ab(total, sizeof(*reloc));
3564 if (reloc == NULL) {
3565 mutex_lock(&dev->struct_mutex);
3566 return -ENOMEM;
3567 }
3568
3569 total = 0;
3570 for (i = 0; i < count; i++) {
3571 struct drm_i915_gem_relocation_entry __user *user_relocs;
3572
3573 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3574
3575 if (copy_from_user(reloc+total, user_relocs,
3576 exec_list[i].relocation_count *
3577 sizeof(*reloc))) {
3578 ret = -EFAULT;
3579 mutex_lock(&dev->struct_mutex);
3580 goto err;
3581 }
3582
3583 total += exec_list[i].relocation_count;
3584 }
3585
3586 ret = i915_mutex_lock_interruptible(dev);
3587 if (ret) {
3588 mutex_lock(&dev->struct_mutex);
3589 goto err;
3590 }
3591
3592 ret = i915_gem_execbuffer_reserve(dev, file,
3593 object_list, exec_list,
3594 count);
3595 if (ret)
3596 goto err;
3597
3598 total = 0;
3599 for (i = 0; i < count; i++) {
3600 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3601 obj->base.pending_read_domains = 0;
3602 obj->base.pending_write_domain = 0;
3603 ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
3604 &exec_list[i],
3605 reloc + total);
3606 if (ret)
3607 goto err;
3608
3609 total += exec_list[i].relocation_count;
3610 }
3611
3612 /* Leave the user relocations as are, this is the painfully slow path,
3613 * and we want to avoid the complication of dropping the lock whilst
3614 * having buffers reserved in the aperture and so causing spurious
3615 * ENOSPC for random operations.
3616 */
3617
3618err:
3619 drm_free_large(reloc);
3620 return ret;
3621}
3622
3623static int
Chris Wilsonc6afd652010-11-01 13:39:24 +00003624i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3625 struct drm_file *file,
3626 struct intel_ring_buffer *ring,
3627 struct drm_gem_object **objects,
3628 int count)
3629{
3630 struct drm_i915_private *dev_priv = dev->dev_private;
3631 int ret, i;
3632
3633 /* Zero the global flush/invalidate flags. These
3634 * will be modified as new domains are computed
3635 * for each object
3636 */
3637 dev->invalidate_domains = 0;
3638 dev->flush_domains = 0;
3639 dev_priv->mm.flush_rings = 0;
3640 for (i = 0; i < count; i++)
3641 i915_gem_object_set_to_gpu_domain(objects[i], ring);
3642
3643 if (dev->invalidate_domains | dev->flush_domains) {
3644#if WATCH_EXEC
3645 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3646 __func__,
3647 dev->invalidate_domains,
3648 dev->flush_domains);
3649#endif
3650 i915_gem_flush(dev, file,
3651 dev->invalidate_domains,
3652 dev->flush_domains,
3653 dev_priv->mm.flush_rings);
3654 }
3655
3656 for (i = 0; i < count; i++) {
3657 struct drm_i915_gem_object *obj = to_intel_bo(objects[i]);
3658 /* XXX replace with semaphores */
3659 if (obj->ring && ring != obj->ring) {
3660 ret = i915_gem_object_wait_rendering(&obj->base, true);
3661 if (ret)
3662 return ret;
3663 }
3664 }
3665
3666 return 0;
3667}
3668
Eric Anholt673a3942008-07-30 12:06:12 -07003669/* Throttle our rendering by waiting until the ring has completed our requests
3670 * emitted over 20 msec ago.
3671 *
Eric Anholtb9624422009-06-03 07:27:35 +00003672 * Note that if we were to use the current jiffies each time around the loop,
3673 * we wouldn't escape the function with any frames outstanding if the time to
3674 * render a frame was over 20ms.
3675 *
Eric Anholt673a3942008-07-30 12:06:12 -07003676 * This should get us reasonable parallelism between CPU and GPU but also
3677 * relatively low latency when blocking on a particular request to finish.
3678 */
3679static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003680i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003681{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003682 struct drm_i915_private *dev_priv = dev->dev_private;
3683 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003684 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003685 struct drm_i915_gem_request *request;
3686 struct intel_ring_buffer *ring = NULL;
3687 u32 seqno = 0;
3688 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003689
Chris Wilson1c255952010-09-26 11:03:27 +01003690 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003691 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003692 if (time_after_eq(request->emitted_jiffies, recent_enough))
3693 break;
3694
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003695 ring = request->ring;
3696 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003697 }
Chris Wilson1c255952010-09-26 11:03:27 +01003698 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003699
3700 if (seqno == 0)
3701 return 0;
3702
3703 ret = 0;
3704 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3705 /* And wait for the seqno passing without holding any locks and
3706 * causing extra latency for others. This is safe as the irq
3707 * generation is designed to be run atomically and so is
3708 * lockless.
3709 */
3710 ring->user_irq_get(dev, ring);
3711 ret = wait_event_interruptible(ring->irq_queue,
3712 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3713 || atomic_read(&dev_priv->mm.wedged));
3714 ring->user_irq_put(dev, ring);
3715
3716 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3717 ret = -EIO;
3718 }
3719
3720 if (ret == 0)
3721 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003722
Eric Anholt673a3942008-07-30 12:06:12 -07003723 return ret;
3724}
3725
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003726static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003727i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3728 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003729{
3730 uint32_t exec_start, exec_len;
3731
3732 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3733 exec_len = (uint32_t) exec->batch_len;
3734
3735 if ((exec_start | exec_len) & 0x7)
3736 return -EINVAL;
3737
3738 if (!exec_start)
3739 return -EINVAL;
3740
3741 return 0;
3742}
3743
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003744static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003745validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3746 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003747{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003748 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003749
Chris Wilson2549d6c2010-10-14 12:10:41 +01003750 for (i = 0; i < count; i++) {
3751 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilsond1d78832010-11-21 09:23:48 +00003752 int length; /* limited by fault_in_pages_readable() */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003753
Chris Wilsond1d78832010-11-21 09:23:48 +00003754 /* First check for malicious input causing overflow */
3755 if (exec[i].relocation_count >
3756 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
3757 return -EINVAL;
3758
3759 length = exec[i].relocation_count *
3760 sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson2549d6c2010-10-14 12:10:41 +01003761 if (!access_ok(VERIFY_READ, ptr, length))
3762 return -EFAULT;
3763
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003764 /* we may also need to update the presumed offsets */
3765 if (!access_ok(VERIFY_WRITE, ptr, length))
3766 return -EFAULT;
3767
Chris Wilson2549d6c2010-10-14 12:10:41 +01003768 if (fault_in_pages_readable(ptr, length))
3769 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003770 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003771
Chris Wilson2549d6c2010-10-14 12:10:41 +01003772 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003773}
3774
Chris Wilson2549d6c2010-10-14 12:10:41 +01003775static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003776i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003777 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003778 struct drm_i915_gem_execbuffer2 *args,
3779 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003780{
3781 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003782 struct drm_gem_object **object_list = NULL;
3783 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003784 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003785 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003786 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003787 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003788 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003789
Zou Nan hai852835f2010-05-21 09:08:56 +08003790 struct intel_ring_buffer *ring = NULL;
3791
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003792 ret = i915_gem_check_is_wedged(dev);
3793 if (ret)
3794 return ret;
3795
Chris Wilson2549d6c2010-10-14 12:10:41 +01003796 ret = validate_exec_list(exec_list, args->buffer_count);
3797 if (ret)
3798 return ret;
3799
Eric Anholt673a3942008-07-30 12:06:12 -07003800#if WATCH_EXEC
3801 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3802 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3803#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003804 switch (args->flags & I915_EXEC_RING_MASK) {
3805 case I915_EXEC_DEFAULT:
3806 case I915_EXEC_RENDER:
3807 ring = &dev_priv->render_ring;
3808 break;
3809 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003810 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003811 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003812 return -EINVAL;
3813 }
3814 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003815 break;
3816 case I915_EXEC_BLT:
3817 if (!HAS_BLT(dev)) {
3818 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3819 return -EINVAL;
3820 }
3821 ring = &dev_priv->blt_ring;
3822 break;
3823 default:
3824 DRM_ERROR("execbuf with unknown ring: %d\n",
3825 (int)(args->flags & I915_EXEC_RING_MASK));
3826 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003827 }
3828
Eric Anholt4f481ed2008-09-10 14:22:49 -07003829 if (args->buffer_count < 1) {
3830 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3831 return -EINVAL;
3832 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003833 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003834 if (object_list == NULL) {
3835 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003836 args->buffer_count);
3837 ret = -ENOMEM;
3838 goto pre_mutex_err;
3839 }
Eric Anholt673a3942008-07-30 12:06:12 -07003840
Eric Anholt201361a2009-03-11 12:30:04 -07003841 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003842 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3843 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003844 if (cliprects == NULL) {
3845 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003846 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003847 }
Eric Anholt201361a2009-03-11 12:30:04 -07003848
3849 ret = copy_from_user(cliprects,
3850 (struct drm_clip_rect __user *)
3851 (uintptr_t) args->cliprects_ptr,
3852 sizeof(*cliprects) * args->num_cliprects);
3853 if (ret != 0) {
3854 DRM_ERROR("copy %d cliprects failed: %d\n",
3855 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003856 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003857 goto pre_mutex_err;
3858 }
3859 }
3860
Chris Wilson8dc5d142010-08-12 12:36:12 +01003861 request = kzalloc(sizeof(*request), GFP_KERNEL);
3862 if (request == NULL) {
3863 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003864 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003865 }
3866
Chris Wilson76c1dec2010-09-25 11:22:51 +01003867 ret = i915_mutex_lock_interruptible(dev);
3868 if (ret)
3869 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003870
Eric Anholt673a3942008-07-30 12:06:12 -07003871 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003872 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003873 ret = -EBUSY;
3874 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003875 }
3876
Keith Packardac94a962008-11-20 23:30:27 -08003877 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003878 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson9af90d12010-10-17 10:01:56 +01003879 object_list[i] = drm_gem_object_lookup(dev, file,
Eric Anholt673a3942008-07-30 12:06:12 -07003880 exec_list[i].handle);
3881 if (object_list[i] == NULL) {
3882 DRM_ERROR("Invalid object handle %d at index %d\n",
3883 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003884 /* prevent error path from reading uninitialized data */
3885 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003886 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003887 goto err;
3888 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003889
Daniel Vetter23010e42010-03-08 13:35:02 +01003890 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003891 if (obj_priv->in_execbuffer) {
3892 DRM_ERROR("Object %p appears more than once in object list\n",
3893 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003894 /* prevent error path from reading uninitialized data */
3895 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003896 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003897 goto err;
3898 }
3899 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003900 }
3901
Chris Wilson9af90d12010-10-17 10:01:56 +01003902 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003903 ret = i915_gem_execbuffer_reserve(dev, file,
3904 object_list, exec_list,
3905 args->buffer_count);
Chris Wilson9af90d12010-10-17 10:01:56 +01003906 if (ret)
3907 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003908
Chris Wilson9af90d12010-10-17 10:01:56 +01003909 /* The objects are in their final locations, apply the relocations. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003910 ret = i915_gem_execbuffer_relocate(dev, file,
3911 object_list, exec_list,
3912 args->buffer_count);
3913 if (ret) {
3914 if (ret == -EFAULT) {
3915 ret = i915_gem_execbuffer_relocate_slow(dev, file,
3916 object_list,
3917 exec_list,
3918 args->buffer_count);
3919 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
3920 }
Eric Anholt673a3942008-07-30 12:06:12 -07003921 if (ret)
3922 goto err;
3923 }
3924
Eric Anholt673a3942008-07-30 12:06:12 -07003925 /* Set the pending read domains for the batch buffer to COMMAND */
3926 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003927 if (batch_obj->pending_write_domain) {
3928 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3929 ret = -EINVAL;
3930 goto err;
3931 }
3932 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003933
Chris Wilson9af90d12010-10-17 10:01:56 +01003934 /* Sanity check the batch buffer */
3935 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
3936 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01003937 if (ret != 0) {
3938 DRM_ERROR("execbuf with invalid offset/length\n");
3939 goto err;
3940 }
3941
Chris Wilsonc6afd652010-11-01 13:39:24 +00003942 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
3943 object_list, args->buffer_count);
3944 if (ret)
3945 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003946
Eric Anholtefbeed92009-02-19 14:54:51 -08003947 for (i = 0; i < args->buffer_count; i++) {
3948 struct drm_gem_object *obj = object_list[i];
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003949 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003950 obj->write_domain = obj->pending_write_domain;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003951 trace_i915_gem_object_change_domain(obj,
3952 obj->read_domains,
3953 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003954 }
3955
Eric Anholt673a3942008-07-30 12:06:12 -07003956#if WATCH_COHERENCY
3957 for (i = 0; i < args->buffer_count; i++) {
3958 i915_gem_object_check_coherency(object_list[i],
3959 exec_list[i].handle);
3960 }
3961#endif
3962
Eric Anholt673a3942008-07-30 12:06:12 -07003963#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003964 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003965 args->batch_len,
3966 __func__,
3967 ~0);
3968#endif
3969
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003970 /* Check for any pending flips. As we only maintain a flip queue depth
3971 * of 1, we can simply insert a WAIT for the next display flip prior
3972 * to executing the batch and avoid stalling the CPU.
3973 */
3974 flips = 0;
3975 for (i = 0; i < args->buffer_count; i++) {
3976 if (object_list[i]->write_domain)
3977 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
3978 }
3979 if (flips) {
3980 int plane, flip_mask;
3981
3982 for (plane = 0; flips >> plane; plane++) {
3983 if (((flips >> plane) & 1) == 0)
3984 continue;
3985
3986 if (plane)
3987 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
3988 else
3989 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
3990
3991 intel_ring_begin(dev, ring, 2);
3992 intel_ring_emit(dev, ring,
3993 MI_WAIT_FOR_EVENT | flip_mask);
3994 intel_ring_emit(dev, ring, MI_NOOP);
3995 intel_ring_advance(dev, ring);
3996 }
3997 }
3998
Eric Anholt673a3942008-07-30 12:06:12 -07003999 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08004000 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004001 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07004002 if (ret) {
4003 DRM_ERROR("dispatch failed %d\n", ret);
4004 goto err;
4005 }
4006
4007 /*
4008 * Ensure that the commands in the batch buffer are
4009 * finished before the interrupt fires
4010 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01004011 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07004012
Eric Anholt673a3942008-07-30 12:06:12 -07004013 for (i = 0; i < args->buffer_count; i++) {
4014 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07004015
Daniel Vetter617dbe22010-02-11 22:16:02 +01004016 i915_gem_object_move_to_active(obj, ring);
Chris Wilson64193402010-10-24 12:38:05 +01004017 if (obj->write_domain)
4018 list_move_tail(&to_intel_bo(obj)->gpu_write_list,
4019 &ring->gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004020 }
Eric Anholt673a3942008-07-30 12:06:12 -07004021
Chris Wilson9af90d12010-10-17 10:01:56 +01004022 i915_add_request(dev, file, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004023 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004024
Eric Anholt673a3942008-07-30 12:06:12 -07004025err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004026 for (i = 0; i < args->buffer_count; i++) {
4027 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01004028 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004029 obj_priv->in_execbuffer = false;
4030 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004031 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004032 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004033
Eric Anholt673a3942008-07-30 12:06:12 -07004034 mutex_unlock(&dev->struct_mutex);
4035
Chris Wilson93533c22010-01-31 10:40:48 +00004036pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004037 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004038 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004039 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07004040
4041 return ret;
4042}
4043
Jesse Barnes76446ca2009-12-17 22:05:42 -05004044/*
4045 * Legacy execbuffer just creates an exec2 list from the original exec object
4046 * list array and passes it to the real function.
4047 */
4048int
4049i915_gem_execbuffer(struct drm_device *dev, void *data,
4050 struct drm_file *file_priv)
4051{
4052 struct drm_i915_gem_execbuffer *args = data;
4053 struct drm_i915_gem_execbuffer2 exec2;
4054 struct drm_i915_gem_exec_object *exec_list = NULL;
4055 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4056 int ret, i;
4057
4058#if WATCH_EXEC
4059 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4060 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4061#endif
4062
4063 if (args->buffer_count < 1) {
4064 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4065 return -EINVAL;
4066 }
4067
4068 /* Copy in the exec list from userland */
4069 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4070 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4071 if (exec_list == NULL || exec2_list == NULL) {
4072 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4073 args->buffer_count);
4074 drm_free_large(exec_list);
4075 drm_free_large(exec2_list);
4076 return -ENOMEM;
4077 }
4078 ret = copy_from_user(exec_list,
4079 (struct drm_i915_relocation_entry __user *)
4080 (uintptr_t) args->buffers_ptr,
4081 sizeof(*exec_list) * args->buffer_count);
4082 if (ret != 0) {
4083 DRM_ERROR("copy %d exec entries failed %d\n",
4084 args->buffer_count, ret);
4085 drm_free_large(exec_list);
4086 drm_free_large(exec2_list);
4087 return -EFAULT;
4088 }
4089
4090 for (i = 0; i < args->buffer_count; i++) {
4091 exec2_list[i].handle = exec_list[i].handle;
4092 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4093 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4094 exec2_list[i].alignment = exec_list[i].alignment;
4095 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004096 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004097 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4098 else
4099 exec2_list[i].flags = 0;
4100 }
4101
4102 exec2.buffers_ptr = args->buffers_ptr;
4103 exec2.buffer_count = args->buffer_count;
4104 exec2.batch_start_offset = args->batch_start_offset;
4105 exec2.batch_len = args->batch_len;
4106 exec2.DR1 = args->DR1;
4107 exec2.DR4 = args->DR4;
4108 exec2.num_cliprects = args->num_cliprects;
4109 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004110 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004111
4112 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4113 if (!ret) {
4114 /* Copy the new buffer offsets back to the user's exec list. */
4115 for (i = 0; i < args->buffer_count; i++)
4116 exec_list[i].offset = exec2_list[i].offset;
4117 /* ... and back out to userspace */
4118 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4119 (uintptr_t) args->buffers_ptr,
4120 exec_list,
4121 sizeof(*exec_list) * args->buffer_count);
4122 if (ret) {
4123 ret = -EFAULT;
4124 DRM_ERROR("failed to copy %d exec entries "
4125 "back to user (%d)\n",
4126 args->buffer_count, ret);
4127 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004128 }
4129
4130 drm_free_large(exec_list);
4131 drm_free_large(exec2_list);
4132 return ret;
4133}
4134
4135int
4136i915_gem_execbuffer2(struct drm_device *dev, void *data,
4137 struct drm_file *file_priv)
4138{
4139 struct drm_i915_gem_execbuffer2 *args = data;
4140 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4141 int ret;
4142
4143#if WATCH_EXEC
4144 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4145 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4146#endif
4147
4148 if (args->buffer_count < 1) {
4149 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4150 return -EINVAL;
4151 }
4152
4153 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4154 if (exec2_list == NULL) {
4155 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4156 args->buffer_count);
4157 return -ENOMEM;
4158 }
4159 ret = copy_from_user(exec2_list,
4160 (struct drm_i915_relocation_entry __user *)
4161 (uintptr_t) args->buffers_ptr,
4162 sizeof(*exec2_list) * args->buffer_count);
4163 if (ret != 0) {
4164 DRM_ERROR("copy %d exec entries failed %d\n",
4165 args->buffer_count, ret);
4166 drm_free_large(exec2_list);
4167 return -EFAULT;
4168 }
4169
4170 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4171 if (!ret) {
4172 /* Copy the new buffer offsets back to the user's exec list. */
4173 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4174 (uintptr_t) args->buffers_ptr,
4175 exec2_list,
4176 sizeof(*exec2_list) * args->buffer_count);
4177 if (ret) {
4178 ret = -EFAULT;
4179 DRM_ERROR("failed to copy %d exec entries "
4180 "back to user (%d)\n",
4181 args->buffer_count, ret);
4182 }
4183 }
4184
4185 drm_free_large(exec2_list);
4186 return ret;
4187}
4188
Eric Anholt673a3942008-07-30 12:06:12 -07004189int
4190i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4191{
4192 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004193 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004194 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004195 int ret;
4196
Daniel Vetter778c3542010-05-13 11:49:44 +02004197 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004198 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004199
4200 if (obj_priv->gtt_space != NULL) {
4201 if (alignment == 0)
4202 alignment = i915_gem_get_gtt_alignment(obj);
4203 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004204 WARN(obj_priv->pin_count,
Joe Perchesfce7d612010-10-30 21:08:30 +00004205 "bo is already pinned with incorrect alignment: offset=%x, req.alignment=%x\n",
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004206 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004207 ret = i915_gem_object_unbind(obj);
4208 if (ret)
4209 return ret;
4210 }
4211 }
4212
Eric Anholt673a3942008-07-30 12:06:12 -07004213 if (obj_priv->gtt_space == NULL) {
4214 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004215 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004216 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004217 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004218
Eric Anholt673a3942008-07-30 12:06:12 -07004219 obj_priv->pin_count++;
4220
4221 /* If the object is not active and not pending a flush,
4222 * remove it from the inactive list
4223 */
4224 if (obj_priv->pin_count == 1) {
Chris Wilson73aa8082010-09-30 11:46:12 +01004225 i915_gem_info_add_pin(dev_priv, obj->size);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004226 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004227 list_move_tail(&obj_priv->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004228 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004229 }
Eric Anholt673a3942008-07-30 12:06:12 -07004230
Chris Wilson23bc5982010-09-29 16:10:57 +01004231 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004232 return 0;
4233}
4234
4235void
4236i915_gem_object_unpin(struct drm_gem_object *obj)
4237{
4238 struct drm_device *dev = obj->dev;
4239 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004240 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004241
Chris Wilson23bc5982010-09-29 16:10:57 +01004242 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004243 obj_priv->pin_count--;
4244 BUG_ON(obj_priv->pin_count < 0);
4245 BUG_ON(obj_priv->gtt_space == NULL);
4246
4247 /* If the object is no longer pinned, and is
4248 * neither active nor being flushed, then stick it on
4249 * the inactive list
4250 */
4251 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004252 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004253 list_move_tail(&obj_priv->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004254 &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01004255 i915_gem_info_remove_pin(dev_priv, obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07004256 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004257 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004258}
4259
4260int
4261i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4262 struct drm_file *file_priv)
4263{
4264 struct drm_i915_gem_pin *args = data;
4265 struct drm_gem_object *obj;
4266 struct drm_i915_gem_object *obj_priv;
4267 int ret;
4268
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004269 ret = i915_mutex_lock_interruptible(dev);
4270 if (ret)
4271 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004272
4273 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4274 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004275 ret = -ENOENT;
4276 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004277 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004278 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004279
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004280 if (obj_priv->madv != I915_MADV_WILLNEED) {
4281 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004282 ret = -EINVAL;
4283 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004284 }
4285
Jesse Barnes79e53942008-11-07 14:24:08 -08004286 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4287 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4288 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004289 ret = -EINVAL;
4290 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004291 }
4292
4293 obj_priv->user_pin_count++;
4294 obj_priv->pin_filp = file_priv;
4295 if (obj_priv->user_pin_count == 1) {
4296 ret = i915_gem_object_pin(obj, args->alignment);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004297 if (ret)
4298 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004299 }
4300
4301 /* XXX - flush the CPU caches for pinned objects
4302 * as the X server doesn't manage domains yet
4303 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004304 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004305 args->offset = obj_priv->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004306out:
Eric Anholt673a3942008-07-30 12:06:12 -07004307 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004308unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004309 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004310 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004311}
4312
4313int
4314i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4315 struct drm_file *file_priv)
4316{
4317 struct drm_i915_gem_pin *args = data;
4318 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004319 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004320 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004321
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004322 ret = i915_mutex_lock_interruptible(dev);
4323 if (ret)
4324 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004325
4326 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4327 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004328 ret = -ENOENT;
4329 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004330 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004331 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004332
Jesse Barnes79e53942008-11-07 14:24:08 -08004333 if (obj_priv->pin_filp != file_priv) {
4334 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4335 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004336 ret = -EINVAL;
4337 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004338 }
4339 obj_priv->user_pin_count--;
4340 if (obj_priv->user_pin_count == 0) {
4341 obj_priv->pin_filp = NULL;
4342 i915_gem_object_unpin(obj);
4343 }
Eric Anholt673a3942008-07-30 12:06:12 -07004344
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004345out:
Eric Anholt673a3942008-07-30 12:06:12 -07004346 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004347unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004348 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004349 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004350}
4351
4352int
4353i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4354 struct drm_file *file_priv)
4355{
4356 struct drm_i915_gem_busy *args = data;
4357 struct drm_gem_object *obj;
4358 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004359 int ret;
4360
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004361 ret = i915_mutex_lock_interruptible(dev);
4362 if (ret)
4363 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004364
Eric Anholt673a3942008-07-30 12:06:12 -07004365 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4366 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004367 ret = -ENOENT;
4368 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004369 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004370 obj_priv = to_intel_bo(obj);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004371
Chris Wilson0be555b2010-08-04 15:36:30 +01004372 /* Count all active objects as busy, even if they are currently not used
4373 * by the gpu. Users of this interface expect objects to eventually
4374 * become non-busy without any further actions, therefore emit any
4375 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004376 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004377 args->busy = obj_priv->active;
4378 if (args->busy) {
4379 /* Unconditionally flush objects, even when the gpu still uses this
4380 * object. Userspace calling this function indicates that it wants to
4381 * use this buffer rather sooner than later, so issuing the required
4382 * flush earlier is beneficial.
4383 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004384 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4385 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004386 obj_priv->ring,
4387 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004388
4389 /* Update the active list for the hardware's current position.
4390 * Otherwise this only updates on a delayed timer or when irqs
4391 * are actually unmasked, and our working set ends up being
4392 * larger than required.
4393 */
4394 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4395
4396 args->busy = obj_priv->active;
4397 }
Eric Anholt673a3942008-07-30 12:06:12 -07004398
4399 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004400unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004401 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004402 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004403}
4404
4405int
4406i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4407 struct drm_file *file_priv)
4408{
4409 return i915_gem_ring_throttle(dev, file_priv);
4410}
4411
Chris Wilson3ef94da2009-09-14 16:50:29 +01004412int
4413i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4414 struct drm_file *file_priv)
4415{
4416 struct drm_i915_gem_madvise *args = data;
4417 struct drm_gem_object *obj;
4418 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004419 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004420
4421 switch (args->madv) {
4422 case I915_MADV_DONTNEED:
4423 case I915_MADV_WILLNEED:
4424 break;
4425 default:
4426 return -EINVAL;
4427 }
4428
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004429 ret = i915_mutex_lock_interruptible(dev);
4430 if (ret)
4431 return ret;
4432
Chris Wilson3ef94da2009-09-14 16:50:29 +01004433 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4434 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004435 ret = -ENOENT;
4436 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004437 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004438 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004439
4440 if (obj_priv->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004441 ret = -EINVAL;
4442 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004443 }
4444
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004445 if (obj_priv->madv != __I915_MADV_PURGED)
4446 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004447
Chris Wilson2d7ef392009-09-20 23:13:10 +01004448 /* if the object is no longer bound, discard its backing storage */
4449 if (i915_gem_object_is_purgeable(obj_priv) &&
4450 obj_priv->gtt_space == NULL)
4451 i915_gem_object_truncate(obj);
4452
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004453 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4454
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004455out:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004456 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004457unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004458 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004459 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004460}
4461
Daniel Vetterac52bc52010-04-09 19:05:06 +00004462struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4463 size_t size)
4464{
Chris Wilson73aa8082010-09-30 11:46:12 +01004465 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004466 struct drm_i915_gem_object *obj;
4467
4468 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4469 if (obj == NULL)
4470 return NULL;
4471
4472 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4473 kfree(obj);
4474 return NULL;
4475 }
4476
Chris Wilson73aa8082010-09-30 11:46:12 +01004477 i915_gem_info_add_obj(dev_priv, size);
4478
Daniel Vetterc397b902010-04-09 19:05:07 +00004479 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4480 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4481
4482 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004483 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004484 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004485 INIT_LIST_HEAD(&obj->mm_list);
4486 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004487 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004488 obj->madv = I915_MADV_WILLNEED;
4489
Daniel Vetterc397b902010-04-09 19:05:07 +00004490 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004491}
4492
Eric Anholt673a3942008-07-30 12:06:12 -07004493int i915_gem_init_object(struct drm_gem_object *obj)
4494{
Daniel Vetterc397b902010-04-09 19:05:07 +00004495 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004496
Eric Anholt673a3942008-07-30 12:06:12 -07004497 return 0;
4498}
4499
Chris Wilsonbe726152010-07-23 23:18:50 +01004500static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4501{
4502 struct drm_device *dev = obj->dev;
4503 drm_i915_private_t *dev_priv = dev->dev_private;
4504 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4505 int ret;
4506
4507 ret = i915_gem_object_unbind(obj);
4508 if (ret == -ERESTARTSYS) {
Chris Wilson69dc4982010-10-19 10:36:51 +01004509 list_move(&obj_priv->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004510 &dev_priv->mm.deferred_free_list);
4511 return;
4512 }
4513
4514 if (obj_priv->mmap_offset)
4515 i915_gem_free_mmap_offset(obj);
4516
4517 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004518 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004519
4520 kfree(obj_priv->page_cpu_valid);
4521 kfree(obj_priv->bit_17);
4522 kfree(obj_priv);
4523}
4524
Eric Anholt673a3942008-07-30 12:06:12 -07004525void i915_gem_free_object(struct drm_gem_object *obj)
4526{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004527 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004528 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004529
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004530 trace_i915_gem_object_destroy(obj);
4531
Eric Anholt673a3942008-07-30 12:06:12 -07004532 while (obj_priv->pin_count > 0)
4533 i915_gem_object_unpin(obj);
4534
Dave Airlie71acb5e2008-12-30 20:31:46 +10004535 if (obj_priv->phys_obj)
4536 i915_gem_detach_phys_object(dev, obj);
4537
Chris Wilsonbe726152010-07-23 23:18:50 +01004538 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004539}
4540
Jesse Barnes5669fca2009-02-17 15:13:31 -08004541int
Eric Anholt673a3942008-07-30 12:06:12 -07004542i915_gem_idle(struct drm_device *dev)
4543{
4544 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004545 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004546
Keith Packard6dbe2772008-10-14 21:41:13 -07004547 mutex_lock(&dev->struct_mutex);
4548
Chris Wilson87acb0a2010-10-19 10:13:00 +01004549 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004550 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004551 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004552 }
Eric Anholt673a3942008-07-30 12:06:12 -07004553
Chris Wilson29105cc2010-01-07 10:39:13 +00004554 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004555 if (ret) {
4556 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004557 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004558 }
Eric Anholt673a3942008-07-30 12:06:12 -07004559
Chris Wilson29105cc2010-01-07 10:39:13 +00004560 /* Under UMS, be paranoid and evict. */
4561 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004562 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004563 if (ret) {
4564 mutex_unlock(&dev->struct_mutex);
4565 return ret;
4566 }
4567 }
4568
4569 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4570 * We need to replace this with a semaphore, or something.
4571 * And not confound mm.suspended!
4572 */
4573 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004574 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004575
4576 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004577 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004578
Keith Packard6dbe2772008-10-14 21:41:13 -07004579 mutex_unlock(&dev->struct_mutex);
4580
Chris Wilson29105cc2010-01-07 10:39:13 +00004581 /* Cancel the retire work handler, which should be idle now. */
4582 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4583
Eric Anholt673a3942008-07-30 12:06:12 -07004584 return 0;
4585}
4586
Jesse Barnese552eb72010-04-21 11:39:23 -07004587/*
4588 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4589 * over cache flushing.
4590 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004591static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004592i915_gem_init_pipe_control(struct drm_device *dev)
4593{
4594 drm_i915_private_t *dev_priv = dev->dev_private;
4595 struct drm_gem_object *obj;
4596 struct drm_i915_gem_object *obj_priv;
4597 int ret;
4598
Eric Anholt34dc4d42010-05-07 14:30:03 -07004599 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004600 if (obj == NULL) {
4601 DRM_ERROR("Failed to allocate seqno page\n");
4602 ret = -ENOMEM;
4603 goto err;
4604 }
4605 obj_priv = to_intel_bo(obj);
4606 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4607
4608 ret = i915_gem_object_pin(obj, 4096);
4609 if (ret)
4610 goto err_unref;
4611
4612 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4613 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4614 if (dev_priv->seqno_page == NULL)
4615 goto err_unpin;
4616
4617 dev_priv->seqno_obj = obj;
4618 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4619
4620 return 0;
4621
4622err_unpin:
4623 i915_gem_object_unpin(obj);
4624err_unref:
4625 drm_gem_object_unreference(obj);
4626err:
4627 return ret;
4628}
4629
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004630
4631static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004632i915_gem_cleanup_pipe_control(struct drm_device *dev)
4633{
4634 drm_i915_private_t *dev_priv = dev->dev_private;
4635 struct drm_gem_object *obj;
4636 struct drm_i915_gem_object *obj_priv;
4637
4638 obj = dev_priv->seqno_obj;
4639 obj_priv = to_intel_bo(obj);
4640 kunmap(obj_priv->pages[0]);
4641 i915_gem_object_unpin(obj);
4642 drm_gem_object_unreference(obj);
4643 dev_priv->seqno_obj = NULL;
4644
4645 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004646}
4647
Eric Anholt673a3942008-07-30 12:06:12 -07004648int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004649i915_gem_init_ringbuffer(struct drm_device *dev)
4650{
4651 drm_i915_private_t *dev_priv = dev->dev_private;
4652 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004653
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004654 if (HAS_PIPE_CONTROL(dev)) {
4655 ret = i915_gem_init_pipe_control(dev);
4656 if (ret)
4657 return ret;
4658 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004659
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004660 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004661 if (ret)
4662 goto cleanup_pipe_control;
4663
4664 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004665 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004666 if (ret)
4667 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004668 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004669
Chris Wilson549f7362010-10-19 11:19:32 +01004670 if (HAS_BLT(dev)) {
4671 ret = intel_init_blt_ring_buffer(dev);
4672 if (ret)
4673 goto cleanup_bsd_ring;
4674 }
4675
Chris Wilson6f392d52010-08-07 11:01:22 +01004676 dev_priv->next_seqno = 1;
4677
Chris Wilson68f95ba2010-05-27 13:18:22 +01004678 return 0;
4679
Chris Wilson549f7362010-10-19 11:19:32 +01004680cleanup_bsd_ring:
4681 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004682cleanup_render_ring:
4683 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4684cleanup_pipe_control:
4685 if (HAS_PIPE_CONTROL(dev))
4686 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004687 return ret;
4688}
4689
4690void
4691i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4692{
4693 drm_i915_private_t *dev_priv = dev->dev_private;
4694
4695 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01004696 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01004697 intel_cleanup_ring_buffer(dev, &dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004698 if (HAS_PIPE_CONTROL(dev))
4699 i915_gem_cleanup_pipe_control(dev);
4700}
4701
4702int
Eric Anholt673a3942008-07-30 12:06:12 -07004703i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4704 struct drm_file *file_priv)
4705{
4706 drm_i915_private_t *dev_priv = dev->dev_private;
4707 int ret;
4708
Jesse Barnes79e53942008-11-07 14:24:08 -08004709 if (drm_core_check_feature(dev, DRIVER_MODESET))
4710 return 0;
4711
Ben Gamariba1234d2009-09-14 17:48:47 -04004712 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004713 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004714 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004715 }
4716
Eric Anholt673a3942008-07-30 12:06:12 -07004717 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004718 dev_priv->mm.suspended = 0;
4719
4720 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004721 if (ret != 0) {
4722 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004723 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004724 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004725
Chris Wilson69dc4982010-10-19 10:36:51 +01004726 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004727 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004728 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004729 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004730 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4731 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004732 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004733 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004734 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004735 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004736
Chris Wilson5f353082010-06-07 14:03:03 +01004737 ret = drm_irq_install(dev);
4738 if (ret)
4739 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004740
Eric Anholt673a3942008-07-30 12:06:12 -07004741 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004742
4743cleanup_ringbuffer:
4744 mutex_lock(&dev->struct_mutex);
4745 i915_gem_cleanup_ringbuffer(dev);
4746 dev_priv->mm.suspended = 1;
4747 mutex_unlock(&dev->struct_mutex);
4748
4749 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004750}
4751
4752int
4753i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4754 struct drm_file *file_priv)
4755{
Jesse Barnes79e53942008-11-07 14:24:08 -08004756 if (drm_core_check_feature(dev, DRIVER_MODESET))
4757 return 0;
4758
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004759 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004760 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004761}
4762
4763void
4764i915_gem_lastclose(struct drm_device *dev)
4765{
4766 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004767
Eric Anholte806b492009-01-22 09:56:58 -08004768 if (drm_core_check_feature(dev, DRIVER_MODESET))
4769 return;
4770
Keith Packard6dbe2772008-10-14 21:41:13 -07004771 ret = i915_gem_idle(dev);
4772 if (ret)
4773 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004774}
4775
Chris Wilson64193402010-10-24 12:38:05 +01004776static void
4777init_ring_lists(struct intel_ring_buffer *ring)
4778{
4779 INIT_LIST_HEAD(&ring->active_list);
4780 INIT_LIST_HEAD(&ring->request_list);
4781 INIT_LIST_HEAD(&ring->gpu_write_list);
4782}
4783
Eric Anholt673a3942008-07-30 12:06:12 -07004784void
4785i915_gem_load(struct drm_device *dev)
4786{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004787 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004788 drm_i915_private_t *dev_priv = dev->dev_private;
4789
Chris Wilson69dc4982010-10-19 10:36:51 +01004790 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004791 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4792 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004793 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004794 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004795 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Chris Wilson64193402010-10-24 12:38:05 +01004796 init_ring_lists(&dev_priv->render_ring);
4797 init_ring_lists(&dev_priv->bsd_ring);
4798 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004799 for (i = 0; i < 16; i++)
4800 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004801 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4802 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004803 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004804 spin_lock(&shrink_list_lock);
4805 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4806 spin_unlock(&shrink_list_lock);
4807
Dave Airlie94400122010-07-20 13:15:31 +10004808 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4809 if (IS_GEN3(dev)) {
4810 u32 tmp = I915_READ(MI_ARB_STATE);
4811 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4812 /* arb state is a masked write, so set bit + bit in mask */
4813 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4814 I915_WRITE(MI_ARB_STATE, tmp);
4815 }
4816 }
4817
Jesse Barnesde151cf2008-11-12 10:03:55 -08004818 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004819 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4820 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004821
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004822 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004823 dev_priv->num_fence_regs = 16;
4824 else
4825 dev_priv->num_fence_regs = 8;
4826
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004827 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004828 switch (INTEL_INFO(dev)->gen) {
4829 case 6:
4830 for (i = 0; i < 16; i++)
4831 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4832 break;
4833 case 5:
4834 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004835 for (i = 0; i < 16; i++)
4836 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004837 break;
4838 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004839 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4840 for (i = 0; i < 8; i++)
4841 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004842 case 2:
4843 for (i = 0; i < 8; i++)
4844 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4845 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004846 }
Eric Anholt673a3942008-07-30 12:06:12 -07004847 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004848 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004849}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004850
4851/*
4852 * Create a physically contiguous memory object for this object
4853 * e.g. for cursor + overlay regs
4854 */
Chris Wilson995b6762010-08-20 13:23:26 +01004855static int i915_gem_init_phys_object(struct drm_device *dev,
4856 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004857{
4858 drm_i915_private_t *dev_priv = dev->dev_private;
4859 struct drm_i915_gem_phys_object *phys_obj;
4860 int ret;
4861
4862 if (dev_priv->mm.phys_objs[id - 1] || !size)
4863 return 0;
4864
Eric Anholt9a298b22009-03-24 12:23:04 -07004865 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004866 if (!phys_obj)
4867 return -ENOMEM;
4868
4869 phys_obj->id = id;
4870
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004871 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004872 if (!phys_obj->handle) {
4873 ret = -ENOMEM;
4874 goto kfree_obj;
4875 }
4876#ifdef CONFIG_X86
4877 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4878#endif
4879
4880 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4881
4882 return 0;
4883kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004884 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004885 return ret;
4886}
4887
Chris Wilson995b6762010-08-20 13:23:26 +01004888static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004889{
4890 drm_i915_private_t *dev_priv = dev->dev_private;
4891 struct drm_i915_gem_phys_object *phys_obj;
4892
4893 if (!dev_priv->mm.phys_objs[id - 1])
4894 return;
4895
4896 phys_obj = dev_priv->mm.phys_objs[id - 1];
4897 if (phys_obj->cur_obj) {
4898 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4899 }
4900
4901#ifdef CONFIG_X86
4902 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4903#endif
4904 drm_pci_free(dev, phys_obj->handle);
4905 kfree(phys_obj);
4906 dev_priv->mm.phys_objs[id - 1] = NULL;
4907}
4908
4909void i915_gem_free_all_phys_object(struct drm_device *dev)
4910{
4911 int i;
4912
Dave Airlie260883c2009-01-22 17:58:49 +10004913 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004914 i915_gem_free_phys_object(dev, i);
4915}
4916
4917void i915_gem_detach_phys_object(struct drm_device *dev,
4918 struct drm_gem_object *obj)
4919{
4920 struct drm_i915_gem_object *obj_priv;
4921 int i;
4922 int ret;
4923 int page_count;
4924
Daniel Vetter23010e42010-03-08 13:35:02 +01004925 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004926 if (!obj_priv->phys_obj)
4927 return;
4928
Chris Wilson4bdadb92010-01-27 13:36:32 +00004929 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004930 if (ret)
4931 goto out;
4932
4933 page_count = obj->size / PAGE_SIZE;
4934
4935 for (i = 0; i < page_count; i++) {
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004936 char *dst = kmap_atomic(obj_priv->pages[i]);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004937 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4938
4939 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004940 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004941 }
Eric Anholt856fa192009-03-19 14:10:50 -07004942 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004943 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004944
4945 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004946out:
4947 obj_priv->phys_obj->cur_obj = NULL;
4948 obj_priv->phys_obj = NULL;
4949}
4950
4951int
4952i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004953 struct drm_gem_object *obj,
4954 int id,
4955 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004956{
4957 drm_i915_private_t *dev_priv = dev->dev_private;
4958 struct drm_i915_gem_object *obj_priv;
4959 int ret = 0;
4960 int page_count;
4961 int i;
4962
4963 if (id > I915_MAX_PHYS_OBJECT)
4964 return -EINVAL;
4965
Daniel Vetter23010e42010-03-08 13:35:02 +01004966 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004967
4968 if (obj_priv->phys_obj) {
4969 if (obj_priv->phys_obj->id == id)
4970 return 0;
4971 i915_gem_detach_phys_object(dev, obj);
4972 }
4973
Dave Airlie71acb5e2008-12-30 20:31:46 +10004974 /* create a new object */
4975 if (!dev_priv->mm.phys_objs[id - 1]) {
4976 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004977 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004978 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004979 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004980 goto out;
4981 }
4982 }
4983
4984 /* bind to the object */
4985 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4986 obj_priv->phys_obj->cur_obj = obj;
4987
Chris Wilson4bdadb92010-01-27 13:36:32 +00004988 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004989 if (ret) {
4990 DRM_ERROR("failed to get page list\n");
4991 goto out;
4992 }
4993
4994 page_count = obj->size / PAGE_SIZE;
4995
4996 for (i = 0; i < page_count; i++) {
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004997 char *src = kmap_atomic(obj_priv->pages[i]);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004998 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4999
5000 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07005001 kunmap_atomic(src);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005002 }
5003
Chris Wilsond78b47b2009-06-17 21:52:49 +01005004 i915_gem_object_put_pages(obj);
5005
Dave Airlie71acb5e2008-12-30 20:31:46 +10005006 return 0;
5007out:
5008 return ret;
5009}
5010
5011static int
5012i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
5013 struct drm_i915_gem_pwrite *args,
5014 struct drm_file *file_priv)
5015{
Daniel Vetter23010e42010-03-08 13:35:02 +01005016 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005017 void *vaddr = obj_priv->phys_obj->handle->vaddr + args->offset;
5018 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005019
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005020 DRM_DEBUG_DRIVER("vaddr %p, %lld\n", vaddr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005021
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005022 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
5023 unsigned long unwritten;
5024
5025 /* The physical object once assigned is fixed for the lifetime
5026 * of the obj, so we can safely drop the lock and continue
5027 * to access vaddr.
5028 */
5029 mutex_unlock(&dev->struct_mutex);
5030 unwritten = copy_from_user(vaddr, user_data, args->size);
5031 mutex_lock(&dev->struct_mutex);
5032 if (unwritten)
5033 return -EFAULT;
5034 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005035
5036 drm_agp_chipset_flush(dev);
5037 return 0;
5038}
Eric Anholtb9624422009-06-03 07:27:35 +00005039
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005040void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005041{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005042 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005043
5044 /* Clean up our request list when the client is going away, so that
5045 * later retire_requests won't dereference our soon-to-be-gone
5046 * file_priv.
5047 */
Chris Wilson1c255952010-09-26 11:03:27 +01005048 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005049 while (!list_empty(&file_priv->mm.request_list)) {
5050 struct drm_i915_gem_request *request;
5051
5052 request = list_first_entry(&file_priv->mm.request_list,
5053 struct drm_i915_gem_request,
5054 client_list);
5055 list_del(&request->client_list);
5056 request->file_priv = NULL;
5057 }
Chris Wilson1c255952010-09-26 11:03:27 +01005058 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00005059}
Chris Wilson31169712009-09-14 16:50:28 +01005060
Chris Wilson31169712009-09-14 16:50:28 +01005061static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005062i915_gpu_is_active(struct drm_device *dev)
5063{
5064 drm_i915_private_t *dev_priv = dev->dev_private;
5065 int lists_empty;
5066
Chris Wilson1637ef42010-04-20 17:10:35 +01005067 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01005068 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005069
5070 return !lists_empty;
5071}
5072
5073static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10005074i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005075{
5076 drm_i915_private_t *dev_priv, *next_dev;
5077 struct drm_i915_gem_object *obj_priv, *next_obj;
5078 int cnt = 0;
5079 int would_deadlock = 1;
5080
5081 /* "fast-path" to count number of available objects */
5082 if (nr_to_scan == 0) {
5083 spin_lock(&shrink_list_lock);
5084 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5085 struct drm_device *dev = dev_priv->dev;
5086
5087 if (mutex_trylock(&dev->struct_mutex)) {
5088 list_for_each_entry(obj_priv,
5089 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01005090 mm_list)
Chris Wilson31169712009-09-14 16:50:28 +01005091 cnt++;
5092 mutex_unlock(&dev->struct_mutex);
5093 }
5094 }
5095 spin_unlock(&shrink_list_lock);
5096
5097 return (cnt / 100) * sysctl_vfs_cache_pressure;
5098 }
5099
5100 spin_lock(&shrink_list_lock);
5101
Chris Wilson1637ef42010-04-20 17:10:35 +01005102rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005103 /* first scan for clean buffers */
5104 list_for_each_entry_safe(dev_priv, next_dev,
5105 &shrink_list, mm.shrink_list) {
5106 struct drm_device *dev = dev_priv->dev;
5107
5108 if (! mutex_trylock(&dev->struct_mutex))
5109 continue;
5110
5111 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005112 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005113
Chris Wilson31169712009-09-14 16:50:28 +01005114 list_for_each_entry_safe(obj_priv, next_obj,
5115 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01005116 mm_list) {
Chris Wilson31169712009-09-14 16:50:28 +01005117 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005118 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005119 if (--nr_to_scan <= 0)
5120 break;
5121 }
5122 }
5123
5124 spin_lock(&shrink_list_lock);
5125 mutex_unlock(&dev->struct_mutex);
5126
Chris Wilson963b4832009-09-20 23:03:54 +01005127 would_deadlock = 0;
5128
Chris Wilson31169712009-09-14 16:50:28 +01005129 if (nr_to_scan <= 0)
5130 break;
5131 }
5132
5133 /* second pass, evict/count anything still on the inactive list */
5134 list_for_each_entry_safe(dev_priv, next_dev,
5135 &shrink_list, mm.shrink_list) {
5136 struct drm_device *dev = dev_priv->dev;
5137
5138 if (! mutex_trylock(&dev->struct_mutex))
5139 continue;
5140
5141 spin_unlock(&shrink_list_lock);
5142
5143 list_for_each_entry_safe(obj_priv, next_obj,
5144 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01005145 mm_list) {
Chris Wilson31169712009-09-14 16:50:28 +01005146 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005147 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005148 nr_to_scan--;
5149 } else
5150 cnt++;
5151 }
5152
5153 spin_lock(&shrink_list_lock);
5154 mutex_unlock(&dev->struct_mutex);
5155
5156 would_deadlock = 0;
5157 }
5158
Chris Wilson1637ef42010-04-20 17:10:35 +01005159 if (nr_to_scan) {
5160 int active = 0;
5161
5162 /*
5163 * We are desperate for pages, so as a last resort, wait
5164 * for the GPU to finish and discard whatever we can.
5165 * This has a dramatic impact to reduce the number of
5166 * OOM-killer events whilst running the GPU aggressively.
5167 */
5168 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5169 struct drm_device *dev = dev_priv->dev;
5170
5171 if (!mutex_trylock(&dev->struct_mutex))
5172 continue;
5173
5174 spin_unlock(&shrink_list_lock);
5175
5176 if (i915_gpu_is_active(dev)) {
5177 i915_gpu_idle(dev);
5178 active++;
5179 }
5180
5181 spin_lock(&shrink_list_lock);
5182 mutex_unlock(&dev->struct_mutex);
5183 }
5184
5185 if (active)
5186 goto rescan;
5187 }
5188
Chris Wilson31169712009-09-14 16:50:28 +01005189 spin_unlock(&shrink_list_lock);
5190
5191 if (would_deadlock)
5192 return -1;
5193 else if (cnt > 0)
5194 return (cnt / 100) * sysctl_vfs_cache_pressure;
5195 else
5196 return 0;
5197}
5198
5199static struct shrinker shrinker = {
5200 .shrink = i915_gem_shrink,
5201 .seeks = DEFAULT_SEEKS,
5202};
5203
5204__init void
5205i915_gem_shrinker_init(void)
5206{
5207 register_shrinker(&shrinker);
5208}
5209
5210__exit void
5211i915_gem_shrinker_exit(void)
5212{
5213 unregister_shrinker(&shrinker);
5214}