blob: 8eb8453208b5ce491e77c1c06c77c1c0a2da41bc [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 Wilson4f27b752010-10-14 15:26:45 +0100550 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100551 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100552 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700553
554 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100555 if (obj == NULL) {
556 ret = -ENOENT;
557 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100558 }
Daniel Vetter23010e42010-03-08 13:35:02 +0100559 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700560
Chris Wilson7dcd2492010-09-26 20:21:44 +0100561 /* Bounds check source. */
562 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100563 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100564 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100565 }
566
Chris Wilson35b62a82010-09-26 20:23:38 +0100567 if (args->size == 0)
568 goto out;
569
Chris Wilsonce9d4192010-09-26 20:50:05 +0100570 if (!access_ok(VERIFY_WRITE,
571 (char __user *)(uintptr_t)args->data_ptr,
572 args->size)) {
573 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +0100574 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -0700575 }
576
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100577 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
578 args->size);
579 if (ret) {
580 ret = -EFAULT;
581 goto out;
582 }
583
Chris Wilson4f27b752010-10-14 15:26:45 +0100584 ret = i915_gem_object_get_pages_or_evict(obj);
585 if (ret)
586 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -0700587
Chris Wilson4f27b752010-10-14 15:26:45 +0100588 ret = i915_gem_object_set_cpu_read_domain_range(obj,
589 args->offset,
590 args->size);
591 if (ret)
592 goto out_put;
593
594 ret = -EFAULT;
595 if (!i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt673a3942008-07-30 12:06:12 -0700596 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
Chris Wilson4f27b752010-10-14 15:26:45 +0100597 if (ret == -EFAULT)
598 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700599
Chris Wilson4f27b752010-10-14 15:26:45 +0100600out_put:
601 i915_gem_object_put_pages(obj);
Chris Wilson35b62a82010-09-26 20:23:38 +0100602out:
Chris Wilson4f27b752010-10-14 15:26:45 +0100603 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100604unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100605 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700606 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700607}
608
Keith Packard0839ccb2008-10-30 19:38:48 -0700609/* This is the fast write path which cannot handle
610 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700611 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700612
Keith Packard0839ccb2008-10-30 19:38:48 -0700613static inline int
614fast_user_write(struct io_mapping *mapping,
615 loff_t page_base, int page_offset,
616 char __user *user_data,
617 int length)
618{
619 char *vaddr_atomic;
620 unsigned long unwritten;
621
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700622 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700623 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
624 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700625 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100626 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700627}
628
629/* Here's the write path which can sleep for
630 * page faults
631 */
632
Chris Wilsonab34c222010-05-27 14:15:35 +0100633static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700634slow_kernel_write(struct io_mapping *mapping,
635 loff_t gtt_base, int gtt_offset,
636 struct page *user_page, int user_offset,
637 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700638{
Chris Wilsonab34c222010-05-27 14:15:35 +0100639 char __iomem *dst_vaddr;
640 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700641
Chris Wilsonab34c222010-05-27 14:15:35 +0100642 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
643 src_vaddr = kmap(user_page);
644
645 memcpy_toio(dst_vaddr + gtt_offset,
646 src_vaddr + user_offset,
647 length);
648
649 kunmap(user_page);
650 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700651}
652
Eric Anholt40123c12009-03-09 13:42:30 -0700653static inline int
654fast_shmem_write(struct page **pages,
655 loff_t page_base, int page_offset,
656 char __user *data,
657 int length)
658{
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100659 char *vaddr;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100660 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700661
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700662 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT]);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100663 ret = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700664 kunmap_atomic(vaddr);
Eric Anholt40123c12009-03-09 13:42:30 -0700665
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100666 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700667}
668
Eric Anholt3de09aa2009-03-09 09:42:23 -0700669/**
670 * This is the fast pwrite path, where we copy the data directly from the
671 * user into the GTT, uncached.
672 */
Eric Anholt673a3942008-07-30 12:06:12 -0700673static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700674i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
675 struct drm_i915_gem_pwrite *args,
676 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700677{
Daniel Vetter23010e42010-03-08 13:35:02 +0100678 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700679 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700680 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700681 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700682 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700683 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700684
685 user_data = (char __user *) (uintptr_t) args->data_ptr;
686 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700687
Daniel Vetter23010e42010-03-08 13:35:02 +0100688 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700689 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700690
691 while (remain > 0) {
692 /* Operation in this page
693 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700694 * page_base = page offset within aperture
695 * page_offset = offset within page
696 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700697 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700698 page_base = (offset & ~(PAGE_SIZE-1));
699 page_offset = offset & (PAGE_SIZE-1);
700 page_length = remain;
701 if ((page_offset + remain) > PAGE_SIZE)
702 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700703
Keith Packard0839ccb2008-10-30 19:38:48 -0700704 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700705 * source page isn't available. Return the error and we'll
706 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700707 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100708 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
709 page_offset, user_data, page_length))
710
711 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700712
Keith Packard0839ccb2008-10-30 19:38:48 -0700713 remain -= page_length;
714 user_data += page_length;
715 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700716 }
Eric Anholt673a3942008-07-30 12:06:12 -0700717
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100718 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700719}
720
Eric Anholt3de09aa2009-03-09 09:42:23 -0700721/**
722 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
723 * the memory and maps it using kmap_atomic for copying.
724 *
725 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
726 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
727 */
Eric Anholt3043c602008-10-02 12:24:47 -0700728static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700729i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
730 struct drm_i915_gem_pwrite *args,
731 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700732{
Daniel Vetter23010e42010-03-08 13:35:02 +0100733 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700734 drm_i915_private_t *dev_priv = dev->dev_private;
735 ssize_t remain;
736 loff_t gtt_page_base, offset;
737 loff_t first_data_page, last_data_page, num_pages;
738 loff_t pinned_pages, i;
739 struct page **user_pages;
740 struct mm_struct *mm = current->mm;
741 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700742 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700743 uint64_t data_ptr = args->data_ptr;
744
745 remain = args->size;
746
747 /* Pin the user pages containing the data. We can't fault while
748 * holding the struct mutex, and all of the pwrite implementations
749 * want to hold it while dereferencing the user data.
750 */
751 first_data_page = data_ptr / PAGE_SIZE;
752 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
753 num_pages = last_data_page - first_data_page + 1;
754
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100755 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700756 if (user_pages == NULL)
757 return -ENOMEM;
758
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100759 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700760 down_read(&mm->mmap_sem);
761 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
762 num_pages, 0, 0, user_pages, NULL);
763 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100764 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700765 if (pinned_pages < num_pages) {
766 ret = -EFAULT;
767 goto out_unpin_pages;
768 }
769
Eric Anholt3de09aa2009-03-09 09:42:23 -0700770 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
771 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100772 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700773
Daniel Vetter23010e42010-03-08 13:35:02 +0100774 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700775 offset = obj_priv->gtt_offset + args->offset;
776
777 while (remain > 0) {
778 /* Operation in this page
779 *
780 * gtt_page_base = page offset within aperture
781 * gtt_page_offset = offset within page in aperture
782 * data_page_index = page number in get_user_pages return
783 * data_page_offset = offset with data_page_index page.
784 * page_length = bytes to copy for this page
785 */
786 gtt_page_base = offset & PAGE_MASK;
787 gtt_page_offset = offset & ~PAGE_MASK;
788 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
789 data_page_offset = data_ptr & ~PAGE_MASK;
790
791 page_length = remain;
792 if ((gtt_page_offset + page_length) > PAGE_SIZE)
793 page_length = PAGE_SIZE - gtt_page_offset;
794 if ((data_page_offset + page_length) > PAGE_SIZE)
795 page_length = PAGE_SIZE - data_page_offset;
796
Chris Wilsonab34c222010-05-27 14:15:35 +0100797 slow_kernel_write(dev_priv->mm.gtt_mapping,
798 gtt_page_base, gtt_page_offset,
799 user_pages[data_page_index],
800 data_page_offset,
801 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700802
803 remain -= page_length;
804 offset += page_length;
805 data_ptr += page_length;
806 }
807
Eric Anholt3de09aa2009-03-09 09:42:23 -0700808out_unpin_pages:
809 for (i = 0; i < pinned_pages; i++)
810 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700811 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700812
813 return ret;
814}
815
Eric Anholt40123c12009-03-09 13:42:30 -0700816/**
817 * This is the fast shmem pwrite path, which attempts to directly
818 * copy_from_user into the kmapped pages backing the object.
819 */
Eric Anholt673a3942008-07-30 12:06:12 -0700820static int
Eric Anholt40123c12009-03-09 13:42:30 -0700821i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
822 struct drm_i915_gem_pwrite *args,
823 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700824{
Daniel Vetter23010e42010-03-08 13:35:02 +0100825 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700826 ssize_t remain;
827 loff_t offset, page_base;
828 char __user *user_data;
829 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700830
831 user_data = (char __user *) (uintptr_t) args->data_ptr;
832 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700833
Daniel Vetter23010e42010-03-08 13:35:02 +0100834 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700835 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700836 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700837
Eric Anholt40123c12009-03-09 13:42:30 -0700838 while (remain > 0) {
839 /* Operation in this page
840 *
841 * page_base = page offset within aperture
842 * page_offset = offset within page
843 * page_length = bytes to copy for this page
844 */
845 page_base = (offset & ~(PAGE_SIZE-1));
846 page_offset = offset & (PAGE_SIZE-1);
847 page_length = remain;
848 if ((page_offset + remain) > PAGE_SIZE)
849 page_length = PAGE_SIZE - page_offset;
850
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100851 if (fast_shmem_write(obj_priv->pages,
Eric Anholt40123c12009-03-09 13:42:30 -0700852 page_base, page_offset,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100853 user_data, page_length))
854 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700855
856 remain -= page_length;
857 user_data += page_length;
858 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700859 }
860
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100861 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700862}
863
864/**
865 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
866 * the memory and maps it using kmap_atomic for copying.
867 *
868 * This avoids taking mmap_sem for faulting on the user's address while the
869 * struct_mutex is held.
870 */
871static int
872i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
873 struct drm_i915_gem_pwrite *args,
874 struct drm_file *file_priv)
875{
Daniel Vetter23010e42010-03-08 13:35:02 +0100876 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700877 struct mm_struct *mm = current->mm;
878 struct page **user_pages;
879 ssize_t remain;
880 loff_t offset, pinned_pages, i;
881 loff_t first_data_page, last_data_page, num_pages;
882 int shmem_page_index, shmem_page_offset;
883 int data_page_index, data_page_offset;
884 int page_length;
885 int ret;
886 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700887 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700888
889 remain = args->size;
890
891 /* Pin the user pages containing the data. We can't fault while
892 * holding the struct mutex, and all of the pwrite implementations
893 * want to hold it while dereferencing the user data.
894 */
895 first_data_page = data_ptr / PAGE_SIZE;
896 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
897 num_pages = last_data_page - first_data_page + 1;
898
Chris Wilson4f27b752010-10-14 15:26:45 +0100899 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700900 if (user_pages == NULL)
901 return -ENOMEM;
902
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100903 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700904 down_read(&mm->mmap_sem);
905 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
906 num_pages, 0, 0, user_pages, NULL);
907 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100908 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700909 if (pinned_pages < num_pages) {
910 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100911 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700912 }
913
Eric Anholt40123c12009-03-09 13:42:30 -0700914 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100915 if (ret)
916 goto out;
917
918 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700919
Daniel Vetter23010e42010-03-08 13:35:02 +0100920 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700921 offset = args->offset;
922 obj_priv->dirty = 1;
923
924 while (remain > 0) {
925 /* Operation in this page
926 *
927 * shmem_page_index = page number within shmem file
928 * shmem_page_offset = offset within page in shmem file
929 * data_page_index = page number in get_user_pages return
930 * data_page_offset = offset with data_page_index page.
931 * page_length = bytes to copy for this page
932 */
933 shmem_page_index = offset / PAGE_SIZE;
934 shmem_page_offset = offset & ~PAGE_MASK;
935 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
936 data_page_offset = data_ptr & ~PAGE_MASK;
937
938 page_length = remain;
939 if ((shmem_page_offset + page_length) > PAGE_SIZE)
940 page_length = PAGE_SIZE - shmem_page_offset;
941 if ((data_page_offset + page_length) > PAGE_SIZE)
942 page_length = PAGE_SIZE - data_page_offset;
943
Eric Anholt280b7132009-03-12 16:56:27 -0700944 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100945 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700946 shmem_page_offset,
947 user_pages[data_page_index],
948 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100949 page_length,
950 0);
951 } else {
952 slow_shmem_copy(obj_priv->pages[shmem_page_index],
953 shmem_page_offset,
954 user_pages[data_page_index],
955 data_page_offset,
956 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700957 }
Eric Anholt40123c12009-03-09 13:42:30 -0700958
959 remain -= page_length;
960 data_ptr += page_length;
961 offset += page_length;
962 }
963
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100964out:
Eric Anholt40123c12009-03-09 13:42:30 -0700965 for (i = 0; i < pinned_pages; i++)
966 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700967 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700968
969 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700970}
971
972/**
973 * Writes data to the object referenced by handle.
974 *
975 * On error, the contents of the buffer that were to be modified are undefined.
976 */
977int
978i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100979 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700980{
981 struct drm_i915_gem_pwrite *args = data;
982 struct drm_gem_object *obj;
983 struct drm_i915_gem_object *obj_priv;
984 int ret = 0;
985
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100986 ret = i915_mutex_lock_interruptible(dev);
987 if (ret)
988 return ret;
989
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100990 obj = drm_gem_object_lookup(dev, file, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100991 if (obj == NULL) {
992 ret = -ENOENT;
993 goto unlock;
994 }
Daniel Vetter23010e42010-03-08 13:35:02 +0100995 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700996
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100997
Chris Wilson7dcd2492010-09-26 20:21:44 +0100998 /* Bounds check destination. */
999 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001000 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001001 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001002 }
1003
Chris Wilson35b62a82010-09-26 20:23:38 +01001004 if (args->size == 0)
1005 goto out;
1006
Chris Wilsonce9d4192010-09-26 20:50:05 +01001007 if (!access_ok(VERIFY_READ,
1008 (char __user *)(uintptr_t)args->data_ptr,
1009 args->size)) {
1010 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +01001011 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001012 }
1013
Chris Wilsonb5e4feb2010-10-14 13:47:43 +01001014 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1015 args->size);
1016 if (ret) {
1017 ret = -EFAULT;
1018 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001019 }
1020
1021 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1022 * it would end up going through the fenced access, and we'll get
1023 * different detiling behavior between reading and writing.
1024 * pread/pwrite currently are reading and writing from the CPU
1025 * perspective, requiring manual detiling by the client.
1026 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001027 if (obj_priv->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001028 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Dave Airlie71acb5e2008-12-30 20:31:46 +10001029 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001030 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001031 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001032 ret = i915_gem_object_pin(obj, 0);
1033 if (ret)
1034 goto out;
1035
1036 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1037 if (ret)
1038 goto out_unpin;
1039
1040 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1041 if (ret == -EFAULT)
1042 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1043
1044out_unpin:
1045 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001046 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001047 ret = i915_gem_object_get_pages_or_evict(obj);
1048 if (ret)
1049 goto out;
1050
1051 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1052 if (ret)
1053 goto out_put;
1054
1055 ret = -EFAULT;
1056 if (!i915_gem_object_needs_bit17_swizzle(obj))
1057 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1058 if (ret == -EFAULT)
1059 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
1060
1061out_put:
1062 i915_gem_object_put_pages(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001063 }
Eric Anholt673a3942008-07-30 12:06:12 -07001064
Chris Wilson35b62a82010-09-26 20:23:38 +01001065out:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001066 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001067unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001068 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001069 return ret;
1070}
1071
1072/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001073 * Called when user space prepares to use an object with the CPU, either
1074 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001075 */
1076int
1077i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1078 struct drm_file *file_priv)
1079{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001080 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001081 struct drm_i915_gem_set_domain *args = data;
1082 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001083 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001084 uint32_t read_domains = args->read_domains;
1085 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001086 int ret;
1087
1088 if (!(dev->driver->driver_features & DRIVER_GEM))
1089 return -ENODEV;
1090
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001091 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001092 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001093 return -EINVAL;
1094
Chris Wilson21d509e2009-06-06 09:46:02 +01001095 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001096 return -EINVAL;
1097
1098 /* Having something in the write domain implies it's in the read
1099 * domain, and only that read domain. Enforce that in the request.
1100 */
1101 if (write_domain != 0 && read_domains != write_domain)
1102 return -EINVAL;
1103
Chris Wilson76c1dec2010-09-25 11:22:51 +01001104 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001105 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001106 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001107
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001108 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1109 if (obj == NULL) {
1110 ret = -ENOENT;
1111 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001112 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001113 obj_priv = to_intel_bo(obj);
Jesse Barnes652c3932009-08-17 13:31:43 -07001114
1115 intel_mark_busy(dev, obj);
1116
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001117 if (read_domains & I915_GEM_DOMAIN_GTT) {
1118 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001119
Eric Anholta09ba7f2009-08-29 12:49:51 -07001120 /* Update the LRU on the fence for the CPU access that's
1121 * about to occur.
1122 */
1123 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001124 struct drm_i915_fence_reg *reg =
1125 &dev_priv->fence_regs[obj_priv->fence_reg];
1126 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001127 &dev_priv->mm.fence_list);
1128 }
1129
Eric Anholt02354392008-11-26 13:58:13 -08001130 /* Silently promote "you're not bound, there was nothing to do"
1131 * to success, since the client was just asking us to
1132 * make sure everything was done.
1133 */
1134 if (ret == -EINVAL)
1135 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001136 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001137 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001138 }
1139
Chris Wilson7d1c4802010-08-07 21:45:03 +01001140 /* Maintain LRU order of "inactive" objects */
1141 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001142 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001143
Eric Anholt673a3942008-07-30 12:06:12 -07001144 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001145unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001146 mutex_unlock(&dev->struct_mutex);
1147 return ret;
1148}
1149
1150/**
1151 * Called when user space has done writes to this buffer
1152 */
1153int
1154i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1155 struct drm_file *file_priv)
1156{
1157 struct drm_i915_gem_sw_finish *args = data;
1158 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001159 int ret = 0;
1160
1161 if (!(dev->driver->driver_features & DRIVER_GEM))
1162 return -ENODEV;
1163
Chris Wilson76c1dec2010-09-25 11:22:51 +01001164 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001165 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001166 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001167
Eric Anholt673a3942008-07-30 12:06:12 -07001168 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1169 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001170 ret = -ENOENT;
1171 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001172 }
1173
Eric Anholt673a3942008-07-30 12:06:12 -07001174 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001175 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001176 i915_gem_object_flush_cpu_write_domain(obj);
1177
Eric Anholt673a3942008-07-30 12:06:12 -07001178 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001179unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001180 mutex_unlock(&dev->struct_mutex);
1181 return ret;
1182}
1183
1184/**
1185 * Maps the contents of an object, returning the address it is mapped
1186 * into.
1187 *
1188 * While the mapping holds a reference on the contents of the object, it doesn't
1189 * imply a ref on the object itself.
1190 */
1191int
1192i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1193 struct drm_file *file_priv)
1194{
1195 struct drm_i915_gem_mmap *args = data;
1196 struct drm_gem_object *obj;
1197 loff_t offset;
1198 unsigned long addr;
1199
1200 if (!(dev->driver->driver_features & DRIVER_GEM))
1201 return -ENODEV;
1202
1203 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1204 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001205 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001206
1207 offset = args->offset;
1208
1209 down_write(&current->mm->mmap_sem);
1210 addr = do_mmap(obj->filp, 0, args->size,
1211 PROT_READ | PROT_WRITE, MAP_SHARED,
1212 args->offset);
1213 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001214 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001215 if (IS_ERR((void *)addr))
1216 return addr;
1217
1218 args->addr_ptr = (uint64_t) addr;
1219
1220 return 0;
1221}
1222
Jesse Barnesde151cf2008-11-12 10:03:55 -08001223/**
1224 * i915_gem_fault - fault a page into the GTT
1225 * vma: VMA in question
1226 * vmf: fault info
1227 *
1228 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1229 * from userspace. The fault handler takes care of binding the object to
1230 * the GTT (if needed), allocating and programming a fence register (again,
1231 * only if needed based on whether the old reg is still valid or the object
1232 * is tiled) and inserting a new PTE into the faulting process.
1233 *
1234 * Note that the faulting process may involve evicting existing objects
1235 * from the GTT and/or fence registers to make room. So performance may
1236 * suffer if the GTT working set is large or there are few fence registers
1237 * left.
1238 */
1239int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1240{
1241 struct drm_gem_object *obj = vma->vm_private_data;
1242 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001243 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001244 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001245 pgoff_t page_offset;
1246 unsigned long pfn;
1247 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001248 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001249
1250 /* We don't use vmf->pgoff since that has the fake offset */
1251 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1252 PAGE_SHIFT;
1253
1254 /* Now bind it into the GTT if needed */
1255 mutex_lock(&dev->struct_mutex);
1256 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001257 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001258 if (ret)
1259 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001260
Jesse Barnesde151cf2008-11-12 10:03:55 -08001261 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001262 if (ret)
1263 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001264 }
1265
1266 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001267 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001268 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001269 if (ret)
1270 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001271 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001272
Chris Wilson7d1c4802010-08-07 21:45:03 +01001273 if (i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001274 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001275
Jesse Barnesde151cf2008-11-12 10:03:55 -08001276 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1277 page_offset;
1278
1279 /* Finally, remap it using the new GTT offset */
1280 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001281unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001282 mutex_unlock(&dev->struct_mutex);
1283
1284 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001285 case 0:
1286 case -ERESTARTSYS:
1287 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001288 case -ENOMEM:
1289 case -EAGAIN:
1290 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001291 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001292 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001293 }
1294}
1295
1296/**
1297 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1298 * @obj: obj in question
1299 *
1300 * GEM memory mapping works by handing back to userspace a fake mmap offset
1301 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1302 * up the object based on the offset and sets up the various memory mapping
1303 * structures.
1304 *
1305 * This routine allocates and attaches a fake offset for @obj.
1306 */
1307static int
1308i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1309{
1310 struct drm_device *dev = obj->dev;
1311 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001312 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001313 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001314 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001315 int ret = 0;
1316
1317 /* Set the object up for mmap'ing */
1318 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001319 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001320 if (!list->map)
1321 return -ENOMEM;
1322
1323 map = list->map;
1324 map->type = _DRM_GEM;
1325 map->size = obj->size;
1326 map->handle = obj;
1327
1328 /* Get a DRM GEM mmap offset allocated... */
1329 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1330 obj->size / PAGE_SIZE, 0, 0);
1331 if (!list->file_offset_node) {
1332 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001333 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001334 goto out_free_list;
1335 }
1336
1337 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1338 obj->size / PAGE_SIZE, 0);
1339 if (!list->file_offset_node) {
1340 ret = -ENOMEM;
1341 goto out_free_list;
1342 }
1343
1344 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001345 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1346 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001347 DRM_ERROR("failed to add to map hash\n");
1348 goto out_free_mm;
1349 }
1350
1351 /* By now we should be all set, any drm_mmap request on the offset
1352 * below will get to our mmap & fault handler */
1353 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1354
1355 return 0;
1356
1357out_free_mm:
1358 drm_mm_put_block(list->file_offset_node);
1359out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001360 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001361
1362 return ret;
1363}
1364
Chris Wilson901782b2009-07-10 08:18:50 +01001365/**
1366 * i915_gem_release_mmap - remove physical page mappings
1367 * @obj: obj in question
1368 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001369 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001370 * relinquish ownership of the pages back to the system.
1371 *
1372 * It is vital that we remove the page mapping if we have mapped a tiled
1373 * object through the GTT and then lose the fence register due to
1374 * resource pressure. Similarly if the object has been moved out of the
1375 * aperture, than pages mapped into userspace must be revoked. Removing the
1376 * mapping will then trigger a page fault on the next user access, allowing
1377 * fixup by i915_gem_fault().
1378 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001379void
Chris Wilson901782b2009-07-10 08:18:50 +01001380i915_gem_release_mmap(struct drm_gem_object *obj)
1381{
1382 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001383 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001384
1385 if (dev->dev_mapping)
1386 unmap_mapping_range(dev->dev_mapping,
1387 obj_priv->mmap_offset, obj->size, 1);
1388}
1389
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001390static void
1391i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1392{
1393 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001394 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001395 struct drm_gem_mm *mm = dev->mm_private;
1396 struct drm_map_list *list;
1397
1398 list = &obj->map_list;
1399 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1400
1401 if (list->file_offset_node) {
1402 drm_mm_put_block(list->file_offset_node);
1403 list->file_offset_node = NULL;
1404 }
1405
1406 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001407 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001408 list->map = NULL;
1409 }
1410
1411 obj_priv->mmap_offset = 0;
1412}
1413
Jesse Barnesde151cf2008-11-12 10:03:55 -08001414/**
1415 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1416 * @obj: object to check
1417 *
1418 * Return the required GTT alignment for an object, taking into account
1419 * potential fence register mapping if needed.
1420 */
1421static uint32_t
1422i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1423{
1424 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001425 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001426 int start, i;
1427
1428 /*
1429 * Minimum alignment is 4k (GTT page size), but might be greater
1430 * if a fence register is needed for the object.
1431 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001432 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001433 return 4096;
1434
1435 /*
1436 * Previous chips need to be aligned to the size of the smallest
1437 * fence register that can contain the object.
1438 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001439 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001440 start = 1024*1024;
1441 else
1442 start = 512*1024;
1443
1444 for (i = start; i < obj->size; i <<= 1)
1445 ;
1446
1447 return i;
1448}
1449
1450/**
1451 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1452 * @dev: DRM device
1453 * @data: GTT mapping ioctl data
1454 * @file_priv: GEM object info
1455 *
1456 * Simply returns the fake offset to userspace so it can mmap it.
1457 * The mmap call will end up in drm_gem_mmap(), which will set things
1458 * up so we can get faults in the handler above.
1459 *
1460 * The fault handler will take care of binding the object into the GTT
1461 * (since it may have been evicted to make room for something), allocating
1462 * a fence register, and mapping the appropriate aperture address into
1463 * userspace.
1464 */
1465int
1466i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1467 struct drm_file *file_priv)
1468{
1469 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001470 struct drm_gem_object *obj;
1471 struct drm_i915_gem_object *obj_priv;
1472 int ret;
1473
1474 if (!(dev->driver->driver_features & DRIVER_GEM))
1475 return -ENODEV;
1476
Chris Wilson76c1dec2010-09-25 11:22:51 +01001477 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001478 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001479 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001480
Jesse Barnesde151cf2008-11-12 10:03:55 -08001481 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001482 if (obj == NULL) {
1483 ret = -ENOENT;
1484 goto unlock;
1485 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001486 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001487
Chris Wilsonab182822009-09-22 18:46:17 +01001488 if (obj_priv->madv != I915_MADV_WILLNEED) {
1489 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001490 ret = -EINVAL;
1491 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001492 }
1493
Jesse Barnesde151cf2008-11-12 10:03:55 -08001494 if (!obj_priv->mmap_offset) {
1495 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001496 if (ret)
1497 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001498 }
1499
1500 args->offset = obj_priv->mmap_offset;
1501
Jesse Barnesde151cf2008-11-12 10:03:55 -08001502 /*
1503 * Pull it into the GTT so that we have a page list (makes the
1504 * initial fault faster and any subsequent flushing possible).
1505 */
1506 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001507 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001508 if (ret)
1509 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001510 }
1511
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001512out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001513 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001514unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001515 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001516 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001517}
1518
Chris Wilson5cdf5882010-09-27 15:51:07 +01001519static void
Eric Anholt856fa192009-03-19 14:10:50 -07001520i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001521{
Daniel Vetter23010e42010-03-08 13:35:02 +01001522 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001523 int page_count = obj->size / PAGE_SIZE;
1524 int i;
1525
Eric Anholt856fa192009-03-19 14:10:50 -07001526 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001527 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001528
1529 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001530 return;
1531
Eric Anholt280b7132009-03-12 16:56:27 -07001532 if (obj_priv->tiling_mode != I915_TILING_NONE)
1533 i915_gem_object_save_bit_17_swizzle(obj);
1534
Chris Wilson3ef94da2009-09-14 16:50:29 +01001535 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001536 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001537
1538 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001539 if (obj_priv->dirty)
1540 set_page_dirty(obj_priv->pages[i]);
1541
1542 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001543 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001544
1545 page_cache_release(obj_priv->pages[i]);
1546 }
Eric Anholt673a3942008-07-30 12:06:12 -07001547 obj_priv->dirty = 0;
1548
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001549 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001550 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001551}
1552
Chris Wilsona56ba562010-09-28 10:07:56 +01001553static uint32_t
1554i915_gem_next_request_seqno(struct drm_device *dev,
1555 struct intel_ring_buffer *ring)
1556{
1557 drm_i915_private_t *dev_priv = dev->dev_private;
1558
1559 ring->outstanding_lazy_request = true;
1560 return dev_priv->next_seqno;
1561}
1562
Eric Anholt673a3942008-07-30 12:06:12 -07001563static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001564i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001565 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001566{
1567 struct drm_device *dev = obj->dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001568 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001569 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001570 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001571
Zou Nan hai852835f2010-05-21 09:08:56 +08001572 BUG_ON(ring == NULL);
1573 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001574
1575 /* Add a reference if we're newly entering the active list. */
1576 if (!obj_priv->active) {
1577 drm_gem_object_reference(obj);
1578 obj_priv->active = 1;
1579 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001580
Eric Anholt673a3942008-07-30 12:06:12 -07001581 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson69dc4982010-10-19 10:36:51 +01001582 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
1583 list_move_tail(&obj_priv->ring_list, &ring->active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001584 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001585}
1586
Eric Anholtce44b0e2008-11-06 16:00:31 -08001587static void
1588i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1589{
1590 struct drm_device *dev = obj->dev;
1591 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001592 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001593
1594 BUG_ON(!obj_priv->active);
Chris Wilson69dc4982010-10-19 10:36:51 +01001595 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
1596 list_del_init(&obj_priv->ring_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001597 obj_priv->last_rendering_seqno = 0;
1598}
Eric Anholt673a3942008-07-30 12:06:12 -07001599
Chris Wilson963b4832009-09-20 23:03:54 +01001600/* Immediately discard the backing storage */
1601static void
1602i915_gem_object_truncate(struct drm_gem_object *obj)
1603{
Daniel Vetter23010e42010-03-08 13:35:02 +01001604 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001605 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001606
Chris Wilsonae9fed62010-08-07 11:01:30 +01001607 /* Our goal here is to return as much of the memory as
1608 * is possible back to the system as we are called from OOM.
1609 * To do this we must instruct the shmfs to drop all of its
1610 * backing pages, *now*. Here we mirror the actions taken
1611 * when by shmem_delete_inode() to release the backing store.
1612 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001613 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001614 truncate_inode_pages(inode->i_mapping, 0);
1615 if (inode->i_op->truncate_range)
1616 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001617
1618 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001619}
1620
1621static inline int
1622i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1623{
1624 return obj_priv->madv == I915_MADV_DONTNEED;
1625}
1626
Eric Anholt673a3942008-07-30 12:06:12 -07001627static void
1628i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1629{
1630 struct drm_device *dev = obj->dev;
1631 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001632 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001633
Eric Anholt673a3942008-07-30 12:06:12 -07001634 if (obj_priv->pin_count != 0)
Chris Wilson69dc4982010-10-19 10:36:51 +01001635 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001636 else
Chris Wilson69dc4982010-10-19 10:36:51 +01001637 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1638 list_del_init(&obj_priv->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001639
Daniel Vetter99fcb762010-02-07 16:20:18 +01001640 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1641
Eric Anholtce44b0e2008-11-06 16:00:31 -08001642 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001643 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001644 if (obj_priv->active) {
1645 obj_priv->active = 0;
1646 drm_gem_object_unreference(obj);
1647 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001648 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001649}
1650
Daniel Vetter63560392010-02-19 11:51:59 +01001651static void
1652i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001653 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001654 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001655{
1656 drm_i915_private_t *dev_priv = dev->dev_private;
1657 struct drm_i915_gem_object *obj_priv, *next;
1658
1659 list_for_each_entry_safe(obj_priv, next,
Chris Wilson64193402010-10-24 12:38:05 +01001660 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001661 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001662 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001663
Chris Wilson64193402010-10-24 12:38:05 +01001664 if (obj->write_domain & flush_domains) {
Daniel Vetter63560392010-02-19 11:51:59 +01001665 uint32_t old_write_domain = obj->write_domain;
1666
1667 obj->write_domain = 0;
1668 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001669 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001670
1671 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001672 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1673 struct drm_i915_fence_reg *reg =
1674 &dev_priv->fence_regs[obj_priv->fence_reg];
1675 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001676 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001677 }
Daniel Vetter63560392010-02-19 11:51:59 +01001678
1679 trace_i915_gem_object_change_domain(obj,
1680 obj->read_domains,
1681 old_write_domain);
1682 }
1683 }
1684}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001685
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001686uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001687i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001688 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001689 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001690 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001691{
1692 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001693 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001694 uint32_t seqno;
1695 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001696
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001697 if (file != NULL)
1698 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001699
Chris Wilson8dc5d142010-08-12 12:36:12 +01001700 if (request == NULL) {
1701 request = kzalloc(sizeof(*request), GFP_KERNEL);
1702 if (request == NULL)
1703 return 0;
1704 }
Eric Anholt673a3942008-07-30 12:06:12 -07001705
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001706 seqno = ring->add_request(dev, ring, 0);
Chris Wilsona56ba562010-09-28 10:07:56 +01001707 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001708
1709 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001710 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001711 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001712 was_empty = list_empty(&ring->request_list);
1713 list_add_tail(&request->list, &ring->request_list);
1714
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001715 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001716 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001717 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001718 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001719 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001720 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001721 }
Eric Anholt673a3942008-07-30 12:06:12 -07001722
Ben Gamarif65d9422009-09-14 17:48:44 -04001723 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001724 mod_timer(&dev_priv->hangcheck_timer,
1725 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001726 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001727 queue_delayed_work(dev_priv->wq,
1728 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001729 }
Eric Anholt673a3942008-07-30 12:06:12 -07001730 return seqno;
1731}
1732
1733/**
1734 * Command execution barrier
1735 *
1736 * Ensures that all commands in the ring are finished
1737 * before signalling the CPU
1738 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001739static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001740i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001741{
Eric Anholt673a3942008-07-30 12:06:12 -07001742 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001743
1744 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001745 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001746 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001747
1748 ring->flush(dev, ring,
1749 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001750}
1751
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001752static inline void
1753i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001754{
Chris Wilson1c255952010-09-26 11:03:27 +01001755 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001756
Chris Wilson1c255952010-09-26 11:03:27 +01001757 if (!file_priv)
1758 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001759
Chris Wilson1c255952010-09-26 11:03:27 +01001760 spin_lock(&file_priv->mm.lock);
1761 list_del(&request->client_list);
1762 request->file_priv = NULL;
1763 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001764}
1765
Chris Wilsondfaae392010-09-22 10:31:52 +01001766static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1767 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001768{
Chris Wilsondfaae392010-09-22 10:31:52 +01001769 while (!list_empty(&ring->request_list)) {
1770 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001771
Chris Wilsondfaae392010-09-22 10:31:52 +01001772 request = list_first_entry(&ring->request_list,
1773 struct drm_i915_gem_request,
1774 list);
1775
1776 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001777 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001778 kfree(request);
1779 }
1780
1781 while (!list_empty(&ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001782 struct drm_i915_gem_object *obj_priv;
1783
Chris Wilsondfaae392010-09-22 10:31:52 +01001784 obj_priv = list_first_entry(&ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001785 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001786 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001787
Chris Wilsondfaae392010-09-22 10:31:52 +01001788 obj_priv->base.write_domain = 0;
1789 list_del_init(&obj_priv->gpu_write_list);
1790 i915_gem_object_move_to_inactive(&obj_priv->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001791 }
Eric Anholt673a3942008-07-30 12:06:12 -07001792}
1793
Chris Wilson069efc12010-09-30 16:53:18 +01001794void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001795{
Chris Wilsondfaae392010-09-22 10:31:52 +01001796 struct drm_i915_private *dev_priv = dev->dev_private;
1797 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001798 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001799
Chris Wilsondfaae392010-09-22 10:31:52 +01001800 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001801 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001802 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001803
1804 /* Remove anything from the flushing lists. The GPU cache is likely
1805 * to be lost on reset along with the data, so simply move the
1806 * lost bo to the inactive list.
1807 */
1808 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001809 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1810 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001811 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001812
1813 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001814 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001815 i915_gem_object_move_to_inactive(&obj_priv->base);
1816 }
Chris Wilson9375e442010-09-19 12:21:28 +01001817
Chris Wilsondfaae392010-09-22 10:31:52 +01001818 /* Move everything out of the GPU domains to ensure we do any
1819 * necessary invalidation upon reuse.
1820 */
Chris Wilson77f01232010-09-19 12:31:36 +01001821 list_for_each_entry(obj_priv,
1822 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001823 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001824 {
1825 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1826 }
Chris Wilson069efc12010-09-30 16:53:18 +01001827
1828 /* The fence registers are invalidated so clear them out */
1829 for (i = 0; i < 16; i++) {
1830 struct drm_i915_fence_reg *reg;
1831
1832 reg = &dev_priv->fence_regs[i];
1833 if (!reg->obj)
1834 continue;
1835
1836 i915_gem_clear_fence_reg(reg->obj);
1837 }
Eric Anholt673a3942008-07-30 12:06:12 -07001838}
1839
1840/**
1841 * This function clears the request list as sequence numbers are passed.
1842 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001843static void
1844i915_gem_retire_requests_ring(struct drm_device *dev,
1845 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001846{
1847 drm_i915_private_t *dev_priv = dev->dev_private;
1848 uint32_t seqno;
1849
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001850 if (!ring->status_page.page_addr ||
1851 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001852 return;
1853
Chris Wilson23bc5982010-09-29 16:10:57 +01001854 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001855
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001856 seqno = ring->get_seqno(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001857 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001858 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001859
Zou Nan hai852835f2010-05-21 09:08:56 +08001860 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001861 struct drm_i915_gem_request,
1862 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001863
Chris Wilsondfaae392010-09-22 10:31:52 +01001864 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001865 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001866
1867 trace_i915_gem_request_retire(dev, request->seqno);
1868
1869 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001870 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001871 kfree(request);
1872 }
1873
1874 /* Move any buffers on the active list that are no longer referenced
1875 * by the ringbuffer to the flushing/inactive lists as appropriate.
1876 */
1877 while (!list_empty(&ring->active_list)) {
1878 struct drm_gem_object *obj;
1879 struct drm_i915_gem_object *obj_priv;
1880
1881 obj_priv = list_first_entry(&ring->active_list,
1882 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001883 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001884
Chris Wilsondfaae392010-09-22 10:31:52 +01001885 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001886 break;
1887
1888 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001889 if (obj->write_domain != 0)
1890 i915_gem_object_move_to_flushing(obj);
1891 else
1892 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001893 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001894
1895 if (unlikely (dev_priv->trace_irq_seqno &&
1896 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001897 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001898 dev_priv->trace_irq_seqno = 0;
1899 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001900
1901 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001902}
1903
1904void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001905i915_gem_retire_requests(struct drm_device *dev)
1906{
1907 drm_i915_private_t *dev_priv = dev->dev_private;
1908
Chris Wilsonbe726152010-07-23 23:18:50 +01001909 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1910 struct drm_i915_gem_object *obj_priv, *tmp;
1911
1912 /* We must be careful that during unbind() we do not
1913 * accidentally infinitely recurse into retire requests.
1914 * Currently:
1915 * retire -> free -> unbind -> wait -> retire_ring
1916 */
1917 list_for_each_entry_safe(obj_priv, tmp,
1918 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001919 mm_list)
Chris Wilsonbe726152010-07-23 23:18:50 +01001920 i915_gem_free_object_tail(&obj_priv->base);
1921 }
1922
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001923 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001924 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001925 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001926}
1927
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001928static void
Eric Anholt673a3942008-07-30 12:06:12 -07001929i915_gem_retire_work_handler(struct work_struct *work)
1930{
1931 drm_i915_private_t *dev_priv;
1932 struct drm_device *dev;
1933
1934 dev_priv = container_of(work, drm_i915_private_t,
1935 mm.retire_work.work);
1936 dev = dev_priv->dev;
1937
Chris Wilson891b48c2010-09-29 12:26:37 +01001938 /* Come back later if the device is busy... */
1939 if (!mutex_trylock(&dev->struct_mutex)) {
1940 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1941 return;
1942 }
1943
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001944 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001945
Keith Packard6dbe2772008-10-14 21:41:13 -07001946 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001947 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01001948 !list_empty(&dev_priv->bsd_ring.request_list) ||
1949 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001950 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001951 mutex_unlock(&dev->struct_mutex);
1952}
1953
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001954int
Zou Nan hai852835f2010-05-21 09:08:56 +08001955i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001956 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001957{
1958 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001959 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001960 int ret = 0;
1961
1962 BUG_ON(seqno == 0);
1963
Ben Gamariba1234d2009-09-14 17:48:47 -04001964 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001965 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04001966
Chris Wilsona56ba562010-09-28 10:07:56 +01001967 if (ring->outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01001968 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001969 if (seqno == 0)
1970 return -ENOMEM;
1971 }
Chris Wilsona56ba562010-09-28 10:07:56 +01001972 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001973
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001974 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001975 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001976 ier = I915_READ(DEIER) | I915_READ(GTIER);
1977 else
1978 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001979 if (!ier) {
1980 DRM_ERROR("something (likely vbetool) disabled "
1981 "interrupts, re-enabling\n");
1982 i915_driver_irq_preinstall(dev);
1983 i915_driver_irq_postinstall(dev);
1984 }
1985
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001986 trace_i915_gem_request_wait_begin(dev, seqno);
1987
Zou Nan hai852835f2010-05-21 09:08:56 +08001988 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001989 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001990 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001991 ret = wait_event_interruptible(ring->irq_queue,
1992 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001993 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001994 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001995 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001996 wait_event(ring->irq_queue,
1997 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001998 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001999 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002000
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002001 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08002002 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002003
2004 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002005 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002006 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002007 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002008
2009 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002010 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002011 __func__, ret, seqno, ring->get_seqno(dev, ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002012 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002013
2014 /* Directly dispatch request retiring. While we have the work queue
2015 * to handle this, the waiter on a request often wants an associated
2016 * buffer to have made it to the inactive list, and we would need
2017 * a separate wait queue to handle that.
2018 */
2019 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002020 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002021
2022 return ret;
2023}
2024
Daniel Vetter48764bf2009-09-15 22:57:32 +02002025/**
2026 * Waits for a sequence number to be signaled, and cleans up the
2027 * request and object lists appropriately for that event.
2028 */
2029static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002030i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002031 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002032{
Zou Nan hai852835f2010-05-21 09:08:56 +08002033 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002034}
2035
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002036static void
Chris Wilson92204342010-09-18 11:02:01 +01002037i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002038 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002039 struct intel_ring_buffer *ring,
2040 uint32_t invalidate_domains,
2041 uint32_t flush_domains)
2042{
2043 ring->flush(dev, ring, invalidate_domains, flush_domains);
2044 i915_gem_process_flushing_list(dev, flush_domains, ring);
2045}
2046
2047static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002048i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002049 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002050 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002051 uint32_t flush_domains,
2052 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002053{
2054 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002055
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002056 if (flush_domains & I915_GEM_DOMAIN_CPU)
2057 drm_agp_chipset_flush(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002058
Chris Wilson92204342010-09-18 11:02:01 +01002059 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2060 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002061 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002062 &dev_priv->render_ring,
2063 invalidate_domains, flush_domains);
2064 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002065 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002066 &dev_priv->bsd_ring,
2067 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002068 if (flush_rings & RING_BLT)
2069 i915_gem_flush_ring(dev, file_priv,
2070 &dev_priv->blt_ring,
2071 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002072 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002073}
2074
Eric Anholt673a3942008-07-30 12:06:12 -07002075/**
2076 * Ensures that all rendering to the object has completed and the object is
2077 * safe to unbind from the GTT or access from the CPU.
2078 */
2079static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002080i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2081 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002082{
2083 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002084 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002085 int ret;
2086
Eric Anholte47c68e2008-11-14 13:35:19 -08002087 /* This function only exists to support waiting for existing rendering,
2088 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002089 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002090 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002091
2092 /* If there is rendering queued on the buffer being evicted, wait for
2093 * it.
2094 */
2095 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002096 ret = i915_do_wait_request(dev,
2097 obj_priv->last_rendering_seqno,
2098 interruptible,
2099 obj_priv->ring);
2100 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002101 return ret;
2102 }
2103
2104 return 0;
2105}
2106
2107/**
2108 * Unbinds an object from the GTT aperture.
2109 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002110int
Eric Anholt673a3942008-07-30 12:06:12 -07002111i915_gem_object_unbind(struct drm_gem_object *obj)
2112{
2113 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002114 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002115 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002116 int ret = 0;
2117
Eric Anholt673a3942008-07-30 12:06:12 -07002118 if (obj_priv->gtt_space == NULL)
2119 return 0;
2120
2121 if (obj_priv->pin_count != 0) {
2122 DRM_ERROR("Attempting to unbind pinned buffer\n");
2123 return -EINVAL;
2124 }
2125
Eric Anholt5323fd02009-09-09 11:50:45 -07002126 /* blow away mappings if mapped through GTT */
2127 i915_gem_release_mmap(obj);
2128
Eric Anholt673a3942008-07-30 12:06:12 -07002129 /* Move the object to the CPU domain to ensure that
2130 * any possible CPU writes while it's not in the GTT
2131 * are flushed when we go to remap it. This will
2132 * also ensure that all pending GPU writes are finished
2133 * before we unbind.
2134 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002135 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002136 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002137 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002138 /* Continue on if we fail due to EIO, the GPU is hung so we
2139 * should be safe and we need to cleanup or else we might
2140 * cause memory corruption through use-after-free.
2141 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002142 if (ret) {
2143 i915_gem_clflush_object(obj);
2144 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2145 }
Eric Anholt673a3942008-07-30 12:06:12 -07002146
Daniel Vetter96b47b62009-12-15 17:50:00 +01002147 /* release the fence reg _after_ flushing */
2148 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2149 i915_gem_clear_fence_reg(obj);
2150
Chris Wilson73aa8082010-09-30 11:46:12 +01002151 drm_unbind_agp(obj_priv->agp_mem);
2152 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002153
Eric Anholt856fa192009-03-19 14:10:50 -07002154 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002155 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002156
Chris Wilson73aa8082010-09-30 11:46:12 +01002157 i915_gem_info_remove_gtt(dev_priv, obj->size);
Chris Wilson69dc4982010-10-19 10:36:51 +01002158 list_del_init(&obj_priv->mm_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002159
Chris Wilson73aa8082010-09-30 11:46:12 +01002160 drm_mm_put_block(obj_priv->gtt_space);
2161 obj_priv->gtt_space = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01002162 obj_priv->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002163
Chris Wilson963b4832009-09-20 23:03:54 +01002164 if (i915_gem_object_is_purgeable(obj_priv))
2165 i915_gem_object_truncate(obj);
2166
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002167 trace_i915_gem_object_unbind(obj);
2168
Chris Wilson8dc17752010-07-23 23:18:51 +01002169 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002170}
2171
Chris Wilsona56ba562010-09-28 10:07:56 +01002172static int i915_ring_idle(struct drm_device *dev,
2173 struct intel_ring_buffer *ring)
2174{
Chris Wilson64193402010-10-24 12:38:05 +01002175 if (list_empty(&ring->gpu_write_list))
2176 return 0;
2177
Chris Wilsona56ba562010-09-28 10:07:56 +01002178 i915_gem_flush_ring(dev, NULL, ring,
2179 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2180 return i915_wait_request(dev,
2181 i915_gem_next_request_seqno(dev, ring),
2182 ring);
2183}
2184
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002185int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002186i915_gpu_idle(struct drm_device *dev)
2187{
2188 drm_i915_private_t *dev_priv = dev->dev_private;
2189 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002190 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002191
Zou Nan haid1b851f2010-05-21 09:08:57 +08002192 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2193 list_empty(&dev_priv->render_ring.active_list) &&
Chris Wilson549f7362010-10-19 11:19:32 +01002194 list_empty(&dev_priv->bsd_ring.active_list) &&
2195 list_empty(&dev_priv->blt_ring.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002196 if (lists_empty)
2197 return 0;
2198
2199 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002200 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002201 if (ret)
2202 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002203
Chris Wilson87acb0a2010-10-19 10:13:00 +01002204 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2205 if (ret)
2206 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002207
Chris Wilson549f7362010-10-19 11:19:32 +01002208 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2209 if (ret)
2210 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002211
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002212 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002213}
2214
Chris Wilson5cdf5882010-09-27 15:51:07 +01002215static int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002216i915_gem_object_get_pages(struct drm_gem_object *obj,
2217 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002218{
Daniel Vetter23010e42010-03-08 13:35:02 +01002219 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002220 int page_count, i;
2221 struct address_space *mapping;
2222 struct inode *inode;
2223 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002224
Daniel Vetter778c3542010-05-13 11:49:44 +02002225 BUG_ON(obj_priv->pages_refcount
2226 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2227
Eric Anholt856fa192009-03-19 14:10:50 -07002228 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002229 return 0;
2230
2231 /* Get the list of pages out of our struct file. They'll be pinned
2232 * at this point until we release them.
2233 */
2234 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002235 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002236 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002237 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002238 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002239 return -ENOMEM;
2240 }
2241
2242 inode = obj->filp->f_path.dentry->d_inode;
2243 mapping = inode->i_mapping;
2244 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002245 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002246 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002247 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002248 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002249 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002250 if (IS_ERR(page))
2251 goto err_pages;
2252
Eric Anholt856fa192009-03-19 14:10:50 -07002253 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002254 }
Eric Anholt280b7132009-03-12 16:56:27 -07002255
2256 if (obj_priv->tiling_mode != I915_TILING_NONE)
2257 i915_gem_object_do_bit_17_swizzle(obj);
2258
Eric Anholt673a3942008-07-30 12:06:12 -07002259 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002260
2261err_pages:
2262 while (i--)
2263 page_cache_release(obj_priv->pages[i]);
2264
2265 drm_free_large(obj_priv->pages);
2266 obj_priv->pages = NULL;
2267 obj_priv->pages_refcount--;
2268 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002269}
2270
Eric Anholt4e901fd2009-10-26 16:44:17 -07002271static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2272{
2273 struct drm_gem_object *obj = reg->obj;
2274 struct drm_device *dev = obj->dev;
2275 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002276 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002277 int regnum = obj_priv->fence_reg;
2278 uint64_t val;
2279
2280 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2281 0xfffff000) << 32;
2282 val |= obj_priv->gtt_offset & 0xfffff000;
2283 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2284 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2285
2286 if (obj_priv->tiling_mode == I915_TILING_Y)
2287 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2288 val |= I965_FENCE_REG_VALID;
2289
2290 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2291}
2292
Jesse Barnesde151cf2008-11-12 10:03:55 -08002293static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2294{
2295 struct drm_gem_object *obj = reg->obj;
2296 struct drm_device *dev = obj->dev;
2297 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002298 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002299 int regnum = obj_priv->fence_reg;
2300 uint64_t val;
2301
2302 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2303 0xfffff000) << 32;
2304 val |= obj_priv->gtt_offset & 0xfffff000;
2305 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2306 if (obj_priv->tiling_mode == I915_TILING_Y)
2307 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2308 val |= I965_FENCE_REG_VALID;
2309
2310 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2311}
2312
2313static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2314{
2315 struct drm_gem_object *obj = reg->obj;
2316 struct drm_device *dev = obj->dev;
2317 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002318 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002319 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002320 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002321 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002322 uint32_t pitch_val;
2323
2324 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2325 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002326 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002327 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002328 return;
2329 }
2330
Jesse Barnes0f973f22009-01-26 17:10:45 -08002331 if (obj_priv->tiling_mode == I915_TILING_Y &&
2332 HAS_128_BYTE_Y_TILING(dev))
2333 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002334 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002335 tile_width = 512;
2336
2337 /* Note: pitch better be a power of two tile widths */
2338 pitch_val = obj_priv->stride / tile_width;
2339 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002340
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002341 if (obj_priv->tiling_mode == I915_TILING_Y &&
2342 HAS_128_BYTE_Y_TILING(dev))
2343 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2344 else
2345 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2346
Jesse Barnesde151cf2008-11-12 10:03:55 -08002347 val = obj_priv->gtt_offset;
2348 if (obj_priv->tiling_mode == I915_TILING_Y)
2349 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2350 val |= I915_FENCE_SIZE_BITS(obj->size);
2351 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2352 val |= I830_FENCE_REG_VALID;
2353
Eric Anholtdc529a42009-03-10 22:34:49 -07002354 if (regnum < 8)
2355 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2356 else
2357 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2358 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002359}
2360
2361static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2362{
2363 struct drm_gem_object *obj = reg->obj;
2364 struct drm_device *dev = obj->dev;
2365 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002366 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002367 int regnum = obj_priv->fence_reg;
2368 uint32_t val;
2369 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002370 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002371
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002372 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002373 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002374 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002375 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002376 return;
2377 }
2378
Eric Anholte76a16d2009-05-26 17:44:56 -07002379 pitch_val = obj_priv->stride / 128;
2380 pitch_val = ffs(pitch_val) - 1;
2381 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2382
Jesse Barnesde151cf2008-11-12 10:03:55 -08002383 val = obj_priv->gtt_offset;
2384 if (obj_priv->tiling_mode == I915_TILING_Y)
2385 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002386 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2387 WARN_ON(fence_size_bits & ~0x00000f00);
2388 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002389 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2390 val |= I830_FENCE_REG_VALID;
2391
2392 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002393}
2394
Chris Wilson2cf34d72010-09-14 13:03:28 +01002395static int i915_find_fence_reg(struct drm_device *dev,
2396 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002397{
2398 struct drm_i915_fence_reg *reg = NULL;
2399 struct drm_i915_gem_object *obj_priv = NULL;
2400 struct drm_i915_private *dev_priv = dev->dev_private;
2401 struct drm_gem_object *obj = NULL;
2402 int i, avail, ret;
2403
2404 /* First try to find a free reg */
2405 avail = 0;
2406 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2407 reg = &dev_priv->fence_regs[i];
2408 if (!reg->obj)
2409 return i;
2410
Daniel Vetter23010e42010-03-08 13:35:02 +01002411 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002412 if (!obj_priv->pin_count)
2413 avail++;
2414 }
2415
2416 if (avail == 0)
2417 return -ENOSPC;
2418
2419 /* None available, try to steal one or wait for a user to finish */
2420 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002421 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2422 lru_list) {
2423 obj = reg->obj;
2424 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002425
2426 if (obj_priv->pin_count)
2427 continue;
2428
2429 /* found one! */
2430 i = obj_priv->fence_reg;
2431 break;
2432 }
2433
2434 BUG_ON(i == I915_FENCE_REG_NONE);
2435
2436 /* We only have a reference on obj from the active list. put_fence_reg
2437 * might drop that one, causing a use-after-free in it. So hold a
2438 * private reference to obj like the other callers of put_fence_reg
2439 * (set_tiling ioctl) do. */
2440 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002441 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002442 drm_gem_object_unreference(obj);
2443 if (ret != 0)
2444 return ret;
2445
2446 return i;
2447}
2448
Jesse Barnesde151cf2008-11-12 10:03:55 -08002449/**
2450 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2451 * @obj: object to map through a fence reg
2452 *
2453 * When mapping objects through the GTT, userspace wants to be able to write
2454 * to them without having to worry about swizzling if the object is tiled.
2455 *
2456 * This function walks the fence regs looking for a free one for @obj,
2457 * stealing one if it can't find any.
2458 *
2459 * It then sets up the reg based on the object's properties: address, pitch
2460 * and tiling format.
2461 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002462int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002463i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2464 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002465{
2466 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002467 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002468 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002469 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002470 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002471
Eric Anholta09ba7f2009-08-29 12:49:51 -07002472 /* Just update our place in the LRU if our fence is getting used. */
2473 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002474 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2475 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002476 return 0;
2477 }
2478
Jesse Barnesde151cf2008-11-12 10:03:55 -08002479 switch (obj_priv->tiling_mode) {
2480 case I915_TILING_NONE:
2481 WARN(1, "allocating a fence for non-tiled object?\n");
2482 break;
2483 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002484 if (!obj_priv->stride)
2485 return -EINVAL;
2486 WARN((obj_priv->stride & (512 - 1)),
2487 "object 0x%08x is X tiled but has non-512B pitch\n",
2488 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002489 break;
2490 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002491 if (!obj_priv->stride)
2492 return -EINVAL;
2493 WARN((obj_priv->stride & (128 - 1)),
2494 "object 0x%08x is Y tiled but has non-128B pitch\n",
2495 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002496 break;
2497 }
2498
Chris Wilson2cf34d72010-09-14 13:03:28 +01002499 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002500 if (ret < 0)
2501 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002502
Daniel Vetterae3db242010-02-19 11:51:58 +01002503 obj_priv->fence_reg = ret;
2504 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002505 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002506
Jesse Barnesde151cf2008-11-12 10:03:55 -08002507 reg->obj = obj;
2508
Chris Wilsone259bef2010-09-17 00:32:02 +01002509 switch (INTEL_INFO(dev)->gen) {
2510 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002511 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002512 break;
2513 case 5:
2514 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002515 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002516 break;
2517 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002518 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002519 break;
2520 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002521 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002522 break;
2523 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002524
Daniel Vetterae3db242010-02-19 11:51:58 +01002525 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2526 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002527
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002528 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002529}
2530
2531/**
2532 * i915_gem_clear_fence_reg - clear out fence register info
2533 * @obj: object to clear
2534 *
2535 * Zeroes out the fence register itself and clears out the associated
2536 * data structures in dev_priv and obj_priv.
2537 */
2538static void
2539i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2540{
2541 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002542 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002543 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002544 struct drm_i915_fence_reg *reg =
2545 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002546 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002547
Chris Wilsone259bef2010-09-17 00:32:02 +01002548 switch (INTEL_INFO(dev)->gen) {
2549 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002550 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2551 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002552 break;
2553 case 5:
2554 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002555 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002556 break;
2557 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002558 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002559 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002560 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002561 case 2:
2562 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002563
2564 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002565 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002566 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002567
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002568 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002569 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002570 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002571}
2572
Eric Anholt673a3942008-07-30 12:06:12 -07002573/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002574 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2575 * to the buffer to finish, and then resets the fence register.
2576 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002577 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002578 *
2579 * Zeroes out the fence register itself and clears out the associated
2580 * data structures in dev_priv and obj_priv.
2581 */
2582int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002583i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2584 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002585{
2586 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002587 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002588 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002589 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002590
2591 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2592 return 0;
2593
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002594 /* If we've changed tiling, GTT-mappings of the object
2595 * need to re-fault to ensure that the correct fence register
2596 * setup is in place.
2597 */
2598 i915_gem_release_mmap(obj);
2599
Chris Wilson52dc7d32009-06-06 09:46:01 +01002600 /* On the i915, GPU access to tiled buffers is via a fence,
2601 * therefore we must wait for any outstanding access to complete
2602 * before clearing the fence.
2603 */
Chris Wilson53640e12010-09-20 11:40:50 +01002604 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2605 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002606 int ret;
2607
Chris Wilson2cf34d72010-09-14 13:03:28 +01002608 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002609 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002610 return ret;
2611
Chris Wilson2cf34d72010-09-14 13:03:28 +01002612 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002613 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002614 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002615
2616 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002617 }
2618
Daniel Vetter4a726612010-02-01 13:59:16 +01002619 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002620 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002621
2622 return 0;
2623}
2624
2625/**
Eric Anholt673a3942008-07-30 12:06:12 -07002626 * Finds free space in the GTT aperture and binds the object there.
2627 */
2628static int
2629i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2630{
2631 struct drm_device *dev = obj->dev;
2632 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002633 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002634 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002635 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002636 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002637
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002638 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002639 DRM_ERROR("Attempting to bind a purgeable object\n");
2640 return -EINVAL;
2641 }
2642
Eric Anholt673a3942008-07-30 12:06:12 -07002643 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002644 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002645 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002646 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2647 return -EINVAL;
2648 }
2649
Chris Wilson654fc602010-05-27 13:18:21 +01002650 /* If the object is bigger than the entire aperture, reject it early
2651 * before evicting everything in a vain attempt to find space.
2652 */
Chris Wilson73aa8082010-09-30 11:46:12 +01002653 if (obj->size > dev_priv->mm.gtt_total) {
Chris Wilson654fc602010-05-27 13:18:21 +01002654 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2655 return -E2BIG;
2656 }
2657
Eric Anholt673a3942008-07-30 12:06:12 -07002658 search_free:
2659 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2660 obj->size, alignment, 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01002661 if (free_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002662 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2663 alignment);
Eric Anholt673a3942008-07-30 12:06:12 -07002664 if (obj_priv->gtt_space == NULL) {
2665 /* If the gtt is empty and we're still having trouble
2666 * fitting our object in, we're out of memory.
2667 */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002668 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002669 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002670 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002671
Eric Anholt673a3942008-07-30 12:06:12 -07002672 goto search_free;
2673 }
2674
Chris Wilson4bdadb92010-01-27 13:36:32 +00002675 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002676 if (ret) {
2677 drm_mm_put_block(obj_priv->gtt_space);
2678 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002679
2680 if (ret == -ENOMEM) {
2681 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002682 ret = i915_gem_evict_something(dev, obj->size,
2683 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002684 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002685 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002686 if (gfpmask) {
2687 gfpmask = 0;
2688 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002689 }
2690
2691 return ret;
2692 }
2693
2694 goto search_free;
2695 }
2696
Eric Anholt673a3942008-07-30 12:06:12 -07002697 return ret;
2698 }
2699
Eric Anholt673a3942008-07-30 12:06:12 -07002700 /* Create an AGP memory structure pointing at our pages, and bind it
2701 * into the GTT.
2702 */
2703 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002704 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002705 obj->size >> PAGE_SHIFT,
Chris Wilson9af90d12010-10-17 10:01:56 +01002706 obj_priv->gtt_space->start,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002707 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002708 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002709 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002710 drm_mm_put_block(obj_priv->gtt_space);
2711 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002712
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002713 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002714 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002715 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002716
2717 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002718 }
Eric Anholt673a3942008-07-30 12:06:12 -07002719
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002720 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson69dc4982010-10-19 10:36:51 +01002721 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01002722 i915_gem_info_add_gtt(dev_priv, obj->size);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002723
Eric Anholt673a3942008-07-30 12:06:12 -07002724 /* Assert that the object is not currently in any GPU domain. As it
2725 * wasn't in the GTT, there shouldn't be any way it could have been in
2726 * a GPU cache
2727 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002728 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2729 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002730
Chris Wilson9af90d12010-10-17 10:01:56 +01002731 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002732 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2733
Eric Anholt673a3942008-07-30 12:06:12 -07002734 return 0;
2735}
2736
2737void
2738i915_gem_clflush_object(struct drm_gem_object *obj)
2739{
Daniel Vetter23010e42010-03-08 13:35:02 +01002740 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002741
2742 /* If we don't have a page list set up, then we're not pinned
2743 * to GPU, and we can ignore the cache flush because it'll happen
2744 * again at bind time.
2745 */
Eric Anholt856fa192009-03-19 14:10:50 -07002746 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002747 return;
2748
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002749 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002750
Eric Anholt856fa192009-03-19 14:10:50 -07002751 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002752}
2753
Eric Anholte47c68e2008-11-14 13:35:19 -08002754/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002755static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002756i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2757 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002758{
2759 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002760 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002761
2762 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002763 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002764
2765 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002766 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002767 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002768 to_intel_bo(obj)->ring,
2769 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002770 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002771
2772 trace_i915_gem_object_change_domain(obj,
2773 obj->read_domains,
2774 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002775
2776 if (pipelined)
2777 return 0;
2778
Chris Wilson2cf34d72010-09-14 13:03:28 +01002779 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002780}
2781
2782/** Flushes the GTT write domain for the object if it's dirty. */
2783static void
2784i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2785{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002786 uint32_t old_write_domain;
2787
Eric Anholte47c68e2008-11-14 13:35:19 -08002788 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2789 return;
2790
2791 /* No actual flushing is required for the GTT write domain. Writes
2792 * to it immediately go to main memory as far as we know, so there's
2793 * no chipset flush. It also doesn't land in render cache.
2794 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002795 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002796 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002797
2798 trace_i915_gem_object_change_domain(obj,
2799 obj->read_domains,
2800 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002801}
2802
2803/** Flushes the CPU write domain for the object if it's dirty. */
2804static void
2805i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2806{
2807 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002808 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002809
2810 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2811 return;
2812
2813 i915_gem_clflush_object(obj);
2814 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002815 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002816 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002817
2818 trace_i915_gem_object_change_domain(obj,
2819 obj->read_domains,
2820 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002821}
2822
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002823/**
2824 * Moves a single object to the GTT read, and possibly write domain.
2825 *
2826 * This function returns when the move is complete, including waiting on
2827 * flushes to occur.
2828 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002829int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002830i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2831{
Daniel Vetter23010e42010-03-08 13:35:02 +01002832 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002833 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002834 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002835
Eric Anholt02354392008-11-26 13:58:13 -08002836 /* Not valid to be called on unbound objects. */
2837 if (obj_priv->gtt_space == NULL)
2838 return -EINVAL;
2839
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002840 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002841 if (ret != 0)
2842 return ret;
2843
Chris Wilson72133422010-09-13 23:56:38 +01002844 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002845
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002846 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002847 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002848 if (ret)
2849 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002850 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002851
2852 old_write_domain = obj->write_domain;
2853 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002854
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002855 /* It should now be out of any other write domains, and we can update
2856 * the domain values for our changes.
2857 */
2858 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2859 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002860 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002861 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002862 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002863 obj_priv->dirty = 1;
2864 }
2865
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002866 trace_i915_gem_object_change_domain(obj,
2867 old_read_domains,
2868 old_write_domain);
2869
Eric Anholte47c68e2008-11-14 13:35:19 -08002870 return 0;
2871}
2872
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002873/*
2874 * Prepare buffer for display plane. Use uninterruptible for possible flush
2875 * wait, as in modesetting process we're not supposed to be interrupted.
2876 */
2877int
Chris Wilson48b956c2010-09-14 12:50:34 +01002878i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2879 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002880{
Daniel Vetter23010e42010-03-08 13:35:02 +01002881 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002882 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002883 int ret;
2884
2885 /* Not valid to be called on unbound objects. */
2886 if (obj_priv->gtt_space == NULL)
2887 return -EINVAL;
2888
Chris Wilsonced270f2010-09-26 22:47:46 +01002889 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002890 if (ret)
2891 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002892
Chris Wilsonced270f2010-09-26 22:47:46 +01002893 /* Currently, we are always called from an non-interruptible context. */
2894 if (!pipelined) {
2895 ret = i915_gem_object_wait_rendering(obj, false);
2896 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002897 return ret;
2898 }
2899
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002900 i915_gem_object_flush_cpu_write_domain(obj);
2901
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002902 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002903 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002904
2905 trace_i915_gem_object_change_domain(obj,
2906 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002907 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002908
2909 return 0;
2910}
2911
Eric Anholte47c68e2008-11-14 13:35:19 -08002912/**
2913 * Moves a single object to the CPU read, and possibly write domain.
2914 *
2915 * This function returns when the move is complete, including waiting on
2916 * flushes to occur.
2917 */
2918static int
2919i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2920{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002921 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002922 int ret;
2923
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002924 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002925 if (ret != 0)
2926 return ret;
2927
2928 i915_gem_object_flush_gtt_write_domain(obj);
2929
2930 /* If we have a partially-valid cache of the object in the CPU,
2931 * finish invalidating it and free the per-page flags.
2932 */
2933 i915_gem_object_set_to_full_cpu_read_domain(obj);
2934
Chris Wilson72133422010-09-13 23:56:38 +01002935 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002936 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002937 if (ret)
2938 return ret;
2939 }
2940
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002941 old_write_domain = obj->write_domain;
2942 old_read_domains = obj->read_domains;
2943
Eric Anholte47c68e2008-11-14 13:35:19 -08002944 /* Flush the CPU cache if it's still invalid. */
2945 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2946 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002947
2948 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2949 }
2950
2951 /* It should now be out of any other write domains, and we can update
2952 * the domain values for our changes.
2953 */
2954 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2955
2956 /* If we're writing through the CPU, then the GPU read domains will
2957 * need to be invalidated at next use.
2958 */
2959 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002960 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002961 obj->write_domain = I915_GEM_DOMAIN_CPU;
2962 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002963
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002964 trace_i915_gem_object_change_domain(obj,
2965 old_read_domains,
2966 old_write_domain);
2967
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002968 return 0;
2969}
2970
Eric Anholt673a3942008-07-30 12:06:12 -07002971/*
2972 * Set the next domain for the specified object. This
2973 * may not actually perform the necessary flushing/invaliding though,
2974 * as that may want to be batched with other set_domain operations
2975 *
2976 * This is (we hope) the only really tricky part of gem. The goal
2977 * is fairly simple -- track which caches hold bits of the object
2978 * and make sure they remain coherent. A few concrete examples may
2979 * help to explain how it works. For shorthand, we use the notation
2980 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2981 * a pair of read and write domain masks.
2982 *
2983 * Case 1: the batch buffer
2984 *
2985 * 1. Allocated
2986 * 2. Written by CPU
2987 * 3. Mapped to GTT
2988 * 4. Read by GPU
2989 * 5. Unmapped from GTT
2990 * 6. Freed
2991 *
2992 * Let's take these a step at a time
2993 *
2994 * 1. Allocated
2995 * Pages allocated from the kernel may still have
2996 * cache contents, so we set them to (CPU, CPU) always.
2997 * 2. Written by CPU (using pwrite)
2998 * The pwrite function calls set_domain (CPU, CPU) and
2999 * this function does nothing (as nothing changes)
3000 * 3. Mapped by GTT
3001 * This function asserts that the object is not
3002 * currently in any GPU-based read or write domains
3003 * 4. Read by GPU
3004 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3005 * As write_domain is zero, this function adds in the
3006 * current read domains (CPU+COMMAND, 0).
3007 * flush_domains is set to CPU.
3008 * invalidate_domains is set to COMMAND
3009 * clflush is run to get data out of the CPU caches
3010 * then i915_dev_set_domain calls i915_gem_flush to
3011 * emit an MI_FLUSH and drm_agp_chipset_flush
3012 * 5. Unmapped from GTT
3013 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3014 * flush_domains and invalidate_domains end up both zero
3015 * so no flushing/invalidating happens
3016 * 6. Freed
3017 * yay, done
3018 *
3019 * Case 2: The shared render buffer
3020 *
3021 * 1. Allocated
3022 * 2. Mapped to GTT
3023 * 3. Read/written by GPU
3024 * 4. set_domain to (CPU,CPU)
3025 * 5. Read/written by CPU
3026 * 6. Read/written by GPU
3027 *
3028 * 1. Allocated
3029 * Same as last example, (CPU, CPU)
3030 * 2. Mapped to GTT
3031 * Nothing changes (assertions find that it is not in the GPU)
3032 * 3. Read/written by GPU
3033 * execbuffer calls set_domain (RENDER, RENDER)
3034 * flush_domains gets CPU
3035 * invalidate_domains gets GPU
3036 * clflush (obj)
3037 * MI_FLUSH and drm_agp_chipset_flush
3038 * 4. set_domain (CPU, CPU)
3039 * flush_domains gets GPU
3040 * invalidate_domains gets CPU
3041 * wait_rendering (obj) to make sure all drawing is complete.
3042 * This will include an MI_FLUSH to get the data from GPU
3043 * to memory
3044 * clflush (obj) to invalidate the CPU cache
3045 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3046 * 5. Read/written by CPU
3047 * cache lines are loaded and dirtied
3048 * 6. Read written by GPU
3049 * Same as last GPU access
3050 *
3051 * Case 3: The constant buffer
3052 *
3053 * 1. Allocated
3054 * 2. Written by CPU
3055 * 3. Read by GPU
3056 * 4. Updated (written) by CPU again
3057 * 5. Read by GPU
3058 *
3059 * 1. Allocated
3060 * (CPU, CPU)
3061 * 2. Written by CPU
3062 * (CPU, CPU)
3063 * 3. Read by GPU
3064 * (CPU+RENDER, 0)
3065 * flush_domains = CPU
3066 * invalidate_domains = RENDER
3067 * clflush (obj)
3068 * MI_FLUSH
3069 * drm_agp_chipset_flush
3070 * 4. Updated (written) by CPU again
3071 * (CPU, CPU)
3072 * flush_domains = 0 (no previous write domain)
3073 * invalidate_domains = 0 (no new read domains)
3074 * 5. Read by GPU
3075 * (CPU+RENDER, 0)
3076 * flush_domains = CPU
3077 * invalidate_domains = RENDER
3078 * clflush (obj)
3079 * MI_FLUSH
3080 * drm_agp_chipset_flush
3081 */
Keith Packardc0d90822008-11-20 23:11:08 -08003082static void
Chris Wilsonb6651452010-10-23 10:15:06 +01003083i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
3084 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07003085{
3086 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003087 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003088 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003089 uint32_t invalidate_domains = 0;
3090 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003091 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003092
Jesse Barnes652c3932009-08-17 13:31:43 -07003093 intel_mark_busy(dev, obj);
3094
Eric Anholt673a3942008-07-30 12:06:12 -07003095 /*
3096 * If the object isn't moving to a new write domain,
3097 * let the object stay in multiple read domains
3098 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003099 if (obj->pending_write_domain == 0)
3100 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003101 else
3102 obj_priv->dirty = 1;
3103
3104 /*
3105 * Flush the current write domain if
3106 * the new read domains don't match. Invalidate
3107 * any read domains which differ from the old
3108 * write domain
3109 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003110 if (obj->write_domain &&
3111 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003112 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003113 invalidate_domains |=
3114 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003115 }
3116 /*
3117 * Invalidate any read caches which may have
3118 * stale data. That is, any new read domains.
3119 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003120 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003121 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003122 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003123
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003124 old_read_domains = obj->read_domains;
3125
Eric Anholtefbeed92009-02-19 14:54:51 -08003126 /* The actual obj->write_domain will be updated with
3127 * pending_write_domain after we emit the accumulated flush for all
3128 * of our domain changes in execbuffers (which clears objects'
3129 * write_domains). So if we have a current write domain that we
3130 * aren't changing, set pending_write_domain to that.
3131 */
3132 if (flush_domains == 0 && obj->pending_write_domain == 0)
3133 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003134 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003135
3136 dev->invalidate_domains |= invalidate_domains;
3137 dev->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003138 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson92204342010-09-18 11:02:01 +01003139 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003140 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
3141 dev_priv->mm.flush_rings |= ring->id;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003142
3143 trace_i915_gem_object_change_domain(obj,
3144 old_read_domains,
3145 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003146}
3147
3148/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003149 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003150 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003151 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3152 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3153 */
3154static void
3155i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3156{
Daniel Vetter23010e42010-03-08 13:35:02 +01003157 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003158
3159 if (!obj_priv->page_cpu_valid)
3160 return;
3161
3162 /* If we're partially in the CPU read domain, finish moving it in.
3163 */
3164 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3165 int i;
3166
3167 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3168 if (obj_priv->page_cpu_valid[i])
3169 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003170 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003171 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003172 }
3173
3174 /* Free the page_cpu_valid mappings which are now stale, whether
3175 * or not we've got I915_GEM_DOMAIN_CPU.
3176 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003177 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003178 obj_priv->page_cpu_valid = NULL;
3179}
3180
3181/**
3182 * Set the CPU read domain on a range of the object.
3183 *
3184 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3185 * not entirely valid. The page_cpu_valid member of the object flags which
3186 * pages have been flushed, and will be respected by
3187 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3188 * of the whole object.
3189 *
3190 * This function returns when the move is complete, including waiting on
3191 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003192 */
3193static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003194i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3195 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003196{
Daniel Vetter23010e42010-03-08 13:35:02 +01003197 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003198 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003199 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003200
Eric Anholte47c68e2008-11-14 13:35:19 -08003201 if (offset == 0 && size == obj->size)
3202 return i915_gem_object_set_to_cpu_domain(obj, 0);
3203
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003204 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003205 if (ret != 0)
3206 return ret;
3207 i915_gem_object_flush_gtt_write_domain(obj);
3208
3209 /* If we're already fully in the CPU read domain, we're done. */
3210 if (obj_priv->page_cpu_valid == NULL &&
3211 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003212 return 0;
3213
Eric Anholte47c68e2008-11-14 13:35:19 -08003214 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3215 * newly adding I915_GEM_DOMAIN_CPU
3216 */
Eric Anholt673a3942008-07-30 12:06:12 -07003217 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003218 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3219 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003220 if (obj_priv->page_cpu_valid == NULL)
3221 return -ENOMEM;
3222 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3223 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003224
3225 /* Flush the cache on any pages that are still invalid from the CPU's
3226 * perspective.
3227 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003228 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3229 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003230 if (obj_priv->page_cpu_valid[i])
3231 continue;
3232
Eric Anholt856fa192009-03-19 14:10:50 -07003233 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003234
3235 obj_priv->page_cpu_valid[i] = 1;
3236 }
3237
Eric Anholte47c68e2008-11-14 13:35:19 -08003238 /* It should now be out of any other write domains, and we can update
3239 * the domain values for our changes.
3240 */
3241 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3242
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003243 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003244 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3245
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003246 trace_i915_gem_object_change_domain(obj,
3247 old_read_domains,
3248 obj->write_domain);
3249
Eric Anholt673a3942008-07-30 12:06:12 -07003250 return 0;
3251}
3252
3253/**
Eric Anholt673a3942008-07-30 12:06:12 -07003254 * Pin an object to the GTT and evaluate the relocations landing in it.
3255 */
3256static int
Chris Wilson9af90d12010-10-17 10:01:56 +01003257i915_gem_execbuffer_relocate(struct drm_i915_gem_object *obj,
3258 struct drm_file *file_priv,
3259 struct drm_i915_gem_exec_object2 *entry)
Eric Anholt673a3942008-07-30 12:06:12 -07003260{
Chris Wilson9af90d12010-10-17 10:01:56 +01003261 struct drm_device *dev = obj->base.dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003262 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson2549d6c2010-10-14 12:10:41 +01003263 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson9af90d12010-10-17 10:01:56 +01003264 struct drm_gem_object *target_obj = NULL;
3265 uint32_t target_handle = 0;
3266 int i, ret = 0;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003267
Chris Wilson2549d6c2010-10-14 12:10:41 +01003268 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Eric Anholt673a3942008-07-30 12:06:12 -07003269 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2549d6c2010-10-14 12:10:41 +01003270 struct drm_i915_gem_relocation_entry reloc;
Chris Wilson9af90d12010-10-17 10:01:56 +01003271 uint32_t target_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003272
Chris Wilson9af90d12010-10-17 10:01:56 +01003273 if (__copy_from_user_inatomic(&reloc,
3274 user_relocs+i,
3275 sizeof(reloc))) {
3276 ret = -EFAULT;
3277 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003278 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003279
Chris Wilson9af90d12010-10-17 10:01:56 +01003280 if (reloc.target_handle != target_handle) {
3281 drm_gem_object_unreference(target_obj);
3282
3283 target_obj = drm_gem_object_lookup(dev, file_priv,
3284 reloc.target_handle);
3285 if (target_obj == NULL) {
3286 ret = -ENOENT;
3287 break;
3288 }
3289
3290 target_handle = reloc.target_handle;
Eric Anholt673a3942008-07-30 12:06:12 -07003291 }
Chris Wilson9af90d12010-10-17 10:01:56 +01003292 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003293
Chris Wilson8542a0b2009-09-09 21:15:15 +01003294#if WATCH_RELOC
3295 DRM_INFO("%s: obj %p offset %08x target %d "
3296 "read %08x write %08x gtt %08x "
3297 "presumed %08x delta %08x\n",
3298 __func__,
3299 obj,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003300 (int) reloc.offset,
3301 (int) reloc.target_handle,
3302 (int) reloc.read_domains,
3303 (int) reloc.write_domain,
Chris Wilson9af90d12010-10-17 10:01:56 +01003304 (int) target_offset,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003305 (int) reloc.presumed_offset,
3306 reloc.delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003307#endif
3308
Eric Anholt673a3942008-07-30 12:06:12 -07003309 /* The target buffer should have appeared before us in the
3310 * exec_object list, so it should have a GTT space bound by now.
3311 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003312 if (target_offset == 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07003313 DRM_ERROR("No GTT space found for object %d\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003314 reloc.target_handle);
Chris Wilson9af90d12010-10-17 10:01:56 +01003315 ret = -EINVAL;
3316 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003317 }
3318
Chris Wilson8542a0b2009-09-09 21:15:15 +01003319 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003320 if (reloc.write_domain & (reloc.write_domain - 1)) {
Daniel Vetter16edd552010-02-19 11:52:02 +01003321 DRM_ERROR("reloc with multiple write domains: "
3322 "obj %p target %d offset %d "
3323 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003324 obj, reloc.target_handle,
3325 (int) reloc.offset,
3326 reloc.read_domains,
3327 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003328 ret = -EINVAL;
3329 break;
Daniel Vetter16edd552010-02-19 11:52:02 +01003330 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003331 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
3332 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003333 DRM_ERROR("reloc with read/write CPU domains: "
3334 "obj %p target %d offset %d "
3335 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003336 obj, reloc.target_handle,
3337 (int) reloc.offset,
3338 reloc.read_domains,
3339 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003340 ret = -EINVAL;
3341 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003342 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003343 if (reloc.write_domain && target_obj->pending_write_domain &&
3344 reloc.write_domain != target_obj->pending_write_domain) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003345 DRM_ERROR("Write domain conflict: "
3346 "obj %p target %d offset %d "
3347 "new %08x old %08x\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003348 obj, reloc.target_handle,
3349 (int) reloc.offset,
3350 reloc.write_domain,
Chris Wilson8542a0b2009-09-09 21:15:15 +01003351 target_obj->pending_write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003352 ret = -EINVAL;
3353 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003354 }
3355
Chris Wilson2549d6c2010-10-14 12:10:41 +01003356 target_obj->pending_read_domains |= reloc.read_domains;
Chris Wilson878a3c32010-10-22 10:48:12 +01003357 target_obj->pending_write_domain |= reloc.write_domain;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003358
3359 /* If the relocation already has the right value in it, no
3360 * more work needs to be done.
3361 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003362 if (target_offset == reloc.presumed_offset)
Chris Wilson8542a0b2009-09-09 21:15:15 +01003363 continue;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003364
3365 /* Check that the relocation address is valid... */
Chris Wilson9af90d12010-10-17 10:01:56 +01003366 if (reloc.offset > obj->base.size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003367 DRM_ERROR("Relocation beyond object bounds: "
3368 "obj %p target %d offset %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003369 obj, reloc.target_handle,
Chris Wilson9af90d12010-10-17 10:01:56 +01003370 (int) reloc.offset, (int) obj->base.size);
3371 ret = -EINVAL;
3372 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003373 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003374 if (reloc.offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003375 DRM_ERROR("Relocation not 4-byte aligned: "
3376 "obj %p target %d offset %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003377 obj, reloc.target_handle,
3378 (int) reloc.offset);
Chris Wilson9af90d12010-10-17 10:01:56 +01003379 ret = -EINVAL;
3380 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003381 }
3382
Chris Wilson8542a0b2009-09-09 21:15:15 +01003383 /* and points to somewhere within the target object. */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003384 if (reloc.delta >= target_obj->size) {
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003385 DRM_ERROR("Relocation beyond target object bounds: "
3386 "obj %p target %d delta %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003387 obj, reloc.target_handle,
3388 (int) reloc.delta, (int) target_obj->size);
Chris Wilson9af90d12010-10-17 10:01:56 +01003389 ret = -EINVAL;
3390 break;
Eric Anholte47c68e2008-11-14 13:35:19 -08003391 }
3392
Chris Wilson9af90d12010-10-17 10:01:56 +01003393 reloc.delta += target_offset;
3394 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003395 uint32_t page_offset = reloc.offset & ~PAGE_MASK;
3396 char *vaddr;
3397
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003398 vaddr = kmap_atomic(obj->pages[reloc.offset >> PAGE_SHIFT]);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003399 *(uint32_t *)(vaddr + page_offset) = reloc.delta;
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003400 kunmap_atomic(vaddr);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003401 } else {
3402 uint32_t __iomem *reloc_entry;
3403 void __iomem *reloc_page;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003404
Chris Wilson9af90d12010-10-17 10:01:56 +01003405 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3406 if (ret)
3407 break;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003408
3409 /* Map the page containing the relocation we're going to perform. */
Chris Wilson9af90d12010-10-17 10:01:56 +01003410 reloc.offset += obj->gtt_offset;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003411 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003412 reloc.offset & PAGE_MASK);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003413 reloc_entry = (uint32_t __iomem *)
3414 (reloc_page + (reloc.offset & ~PAGE_MASK));
3415 iowrite32(reloc.delta, reloc_entry);
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003416 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003417 }
3418
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003419 /* and update the user's relocation entry */
3420 reloc.presumed_offset = target_offset;
3421 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3422 &reloc.presumed_offset,
3423 sizeof(reloc.presumed_offset))) {
3424 ret = -EFAULT;
3425 break;
3426 }
Eric Anholt673a3942008-07-30 12:06:12 -07003427 }
3428
Chris Wilson9af90d12010-10-17 10:01:56 +01003429 drm_gem_object_unreference(target_obj);
3430 return ret;
3431}
3432
3433static int
3434i915_gem_execbuffer_pin(struct drm_device *dev,
3435 struct drm_file *file,
3436 struct drm_gem_object **object_list,
3437 struct drm_i915_gem_exec_object2 *exec_list,
3438 int count)
3439{
3440 struct drm_i915_private *dev_priv = dev->dev_private;
3441 int ret, i, retry;
3442
3443 /* attempt to pin all of the buffers into the GTT */
3444 for (retry = 0; retry < 2; retry++) {
3445 ret = 0;
3446 for (i = 0; i < count; i++) {
3447 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
3448 struct drm_i915_gem_object *obj= to_intel_bo(object_list[i]);
3449 bool need_fence =
3450 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3451 obj->tiling_mode != I915_TILING_NONE;
3452
3453 /* Check fence reg constraints and rebind if necessary */
3454 if (need_fence &&
3455 !i915_gem_object_fence_offset_ok(&obj->base,
3456 obj->tiling_mode)) {
3457 ret = i915_gem_object_unbind(&obj->base);
3458 if (ret)
3459 break;
3460 }
3461
3462 ret = i915_gem_object_pin(&obj->base, entry->alignment);
3463 if (ret)
3464 break;
3465
3466 /*
3467 * Pre-965 chips need a fence register set up in order
3468 * to properly handle blits to/from tiled surfaces.
3469 */
3470 if (need_fence) {
3471 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3472 if (ret) {
3473 i915_gem_object_unpin(&obj->base);
3474 break;
3475 }
3476
3477 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3478 }
3479
3480 entry->offset = obj->gtt_offset;
3481 }
3482
3483 while (i--)
3484 i915_gem_object_unpin(object_list[i]);
3485
3486 if (ret == 0)
3487 break;
3488
3489 if (ret != -ENOSPC || retry)
3490 return ret;
3491
3492 ret = i915_gem_evict_everything(dev);
3493 if (ret)
3494 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003495 }
3496
Eric Anholt673a3942008-07-30 12:06:12 -07003497 return 0;
3498}
3499
Eric Anholt673a3942008-07-30 12:06:12 -07003500/* Throttle our rendering by waiting until the ring has completed our requests
3501 * emitted over 20 msec ago.
3502 *
Eric Anholtb9624422009-06-03 07:27:35 +00003503 * Note that if we were to use the current jiffies each time around the loop,
3504 * we wouldn't escape the function with any frames outstanding if the time to
3505 * render a frame was over 20ms.
3506 *
Eric Anholt673a3942008-07-30 12:06:12 -07003507 * This should get us reasonable parallelism between CPU and GPU but also
3508 * relatively low latency when blocking on a particular request to finish.
3509 */
3510static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003511i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003512{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003513 struct drm_i915_private *dev_priv = dev->dev_private;
3514 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003515 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003516 struct drm_i915_gem_request *request;
3517 struct intel_ring_buffer *ring = NULL;
3518 u32 seqno = 0;
3519 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003520
Chris Wilson1c255952010-09-26 11:03:27 +01003521 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003522 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003523 if (time_after_eq(request->emitted_jiffies, recent_enough))
3524 break;
3525
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003526 ring = request->ring;
3527 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003528 }
Chris Wilson1c255952010-09-26 11:03:27 +01003529 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003530
3531 if (seqno == 0)
3532 return 0;
3533
3534 ret = 0;
3535 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3536 /* And wait for the seqno passing without holding any locks and
3537 * causing extra latency for others. This is safe as the irq
3538 * generation is designed to be run atomically and so is
3539 * lockless.
3540 */
3541 ring->user_irq_get(dev, ring);
3542 ret = wait_event_interruptible(ring->irq_queue,
3543 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3544 || atomic_read(&dev_priv->mm.wedged));
3545 ring->user_irq_put(dev, ring);
3546
3547 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3548 ret = -EIO;
3549 }
3550
3551 if (ret == 0)
3552 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003553
Eric Anholt673a3942008-07-30 12:06:12 -07003554 return ret;
3555}
3556
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003557static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003558i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3559 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003560{
3561 uint32_t exec_start, exec_len;
3562
3563 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3564 exec_len = (uint32_t) exec->batch_len;
3565
3566 if ((exec_start | exec_len) & 0x7)
3567 return -EINVAL;
3568
3569 if (!exec_start)
3570 return -EINVAL;
3571
3572 return 0;
3573}
3574
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003575static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003576validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3577 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003578{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003579 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003580
Chris Wilson2549d6c2010-10-14 12:10:41 +01003581 for (i = 0; i < count; i++) {
3582 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3583 size_t length = exec[i].relocation_count * sizeof(struct drm_i915_gem_relocation_entry);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003584
Chris Wilson2549d6c2010-10-14 12:10:41 +01003585 if (!access_ok(VERIFY_READ, ptr, length))
3586 return -EFAULT;
3587
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003588 /* we may also need to update the presumed offsets */
3589 if (!access_ok(VERIFY_WRITE, ptr, length))
3590 return -EFAULT;
3591
Chris Wilson2549d6c2010-10-14 12:10:41 +01003592 if (fault_in_pages_readable(ptr, length))
3593 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003594 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003595
Chris Wilson2549d6c2010-10-14 12:10:41 +01003596 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003597}
3598
Chris Wilson2549d6c2010-10-14 12:10:41 +01003599static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003600i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003601 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003602 struct drm_i915_gem_execbuffer2 *args,
3603 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003604{
3605 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003606 struct drm_gem_object **object_list = NULL;
3607 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003608 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003609 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003610 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003611 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003612 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003613
Zou Nan hai852835f2010-05-21 09:08:56 +08003614 struct intel_ring_buffer *ring = NULL;
3615
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003616 ret = i915_gem_check_is_wedged(dev);
3617 if (ret)
3618 return ret;
3619
Chris Wilson2549d6c2010-10-14 12:10:41 +01003620 ret = validate_exec_list(exec_list, args->buffer_count);
3621 if (ret)
3622 return ret;
3623
Eric Anholt673a3942008-07-30 12:06:12 -07003624#if WATCH_EXEC
3625 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3626 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3627#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003628 switch (args->flags & I915_EXEC_RING_MASK) {
3629 case I915_EXEC_DEFAULT:
3630 case I915_EXEC_RENDER:
3631 ring = &dev_priv->render_ring;
3632 break;
3633 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003634 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003635 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003636 return -EINVAL;
3637 }
3638 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003639 break;
3640 case I915_EXEC_BLT:
3641 if (!HAS_BLT(dev)) {
3642 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3643 return -EINVAL;
3644 }
3645 ring = &dev_priv->blt_ring;
3646 break;
3647 default:
3648 DRM_ERROR("execbuf with unknown ring: %d\n",
3649 (int)(args->flags & I915_EXEC_RING_MASK));
3650 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003651 }
3652
Eric Anholt4f481ed2008-09-10 14:22:49 -07003653 if (args->buffer_count < 1) {
3654 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3655 return -EINVAL;
3656 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003657 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003658 if (object_list == NULL) {
3659 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003660 args->buffer_count);
3661 ret = -ENOMEM;
3662 goto pre_mutex_err;
3663 }
Eric Anholt673a3942008-07-30 12:06:12 -07003664
Eric Anholt201361a2009-03-11 12:30:04 -07003665 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003666 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3667 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003668 if (cliprects == NULL) {
3669 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003670 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003671 }
Eric Anholt201361a2009-03-11 12:30:04 -07003672
3673 ret = copy_from_user(cliprects,
3674 (struct drm_clip_rect __user *)
3675 (uintptr_t) args->cliprects_ptr,
3676 sizeof(*cliprects) * args->num_cliprects);
3677 if (ret != 0) {
3678 DRM_ERROR("copy %d cliprects failed: %d\n",
3679 args->num_cliprects, ret);
Dan Carpenterc877cdce2010-06-23 19:03:01 +02003680 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003681 goto pre_mutex_err;
3682 }
3683 }
3684
Chris Wilson8dc5d142010-08-12 12:36:12 +01003685 request = kzalloc(sizeof(*request), GFP_KERNEL);
3686 if (request == NULL) {
3687 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003688 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003689 }
3690
Chris Wilson76c1dec2010-09-25 11:22:51 +01003691 ret = i915_mutex_lock_interruptible(dev);
3692 if (ret)
3693 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003694
Eric Anholt673a3942008-07-30 12:06:12 -07003695 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003696 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003697 ret = -EBUSY;
3698 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003699 }
3700
Keith Packardac94a962008-11-20 23:30:27 -08003701 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003702 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson9af90d12010-10-17 10:01:56 +01003703 object_list[i] = drm_gem_object_lookup(dev, file,
Eric Anholt673a3942008-07-30 12:06:12 -07003704 exec_list[i].handle);
3705 if (object_list[i] == NULL) {
3706 DRM_ERROR("Invalid object handle %d at index %d\n",
3707 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003708 /* prevent error path from reading uninitialized data */
3709 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003710 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003711 goto err;
3712 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003713
Daniel Vetter23010e42010-03-08 13:35:02 +01003714 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003715 if (obj_priv->in_execbuffer) {
3716 DRM_ERROR("Object %p appears more than once in object list\n",
3717 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003718 /* prevent error path from reading uninitialized data */
3719 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003720 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003721 goto err;
3722 }
3723 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003724 }
3725
Chris Wilson9af90d12010-10-17 10:01:56 +01003726 /* Move the objects en-masse into the GTT, evicting if necessary. */
3727 ret = i915_gem_execbuffer_pin(dev, file,
3728 object_list, exec_list,
3729 args->buffer_count);
3730 if (ret)
3731 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003732
Chris Wilson9af90d12010-10-17 10:01:56 +01003733 /* The objects are in their final locations, apply the relocations. */
3734 for (i = 0; i < args->buffer_count; i++) {
3735 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3736 obj->base.pending_read_domains = 0;
3737 obj->base.pending_write_domain = 0;
3738 ret = i915_gem_execbuffer_relocate(obj, file, &exec_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003739 if (ret)
3740 goto err;
3741 }
3742
Eric Anholt673a3942008-07-30 12:06:12 -07003743 /* Set the pending read domains for the batch buffer to COMMAND */
3744 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003745 if (batch_obj->pending_write_domain) {
3746 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3747 ret = -EINVAL;
3748 goto err;
3749 }
3750 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003751
Chris Wilson9af90d12010-10-17 10:01:56 +01003752 /* Sanity check the batch buffer */
3753 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
3754 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01003755 if (ret != 0) {
3756 DRM_ERROR("execbuf with invalid offset/length\n");
3757 goto err;
3758 }
3759
Keith Packard646f0f62008-11-20 23:23:03 -08003760 /* Zero the global flush/invalidate flags. These
3761 * will be modified as new domains are computed
3762 * for each object
3763 */
3764 dev->invalidate_domains = 0;
3765 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003766 dev_priv->mm.flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003767
Eric Anholt673a3942008-07-30 12:06:12 -07003768 for (i = 0; i < args->buffer_count; i++) {
3769 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003770
Keith Packard646f0f62008-11-20 23:23:03 -08003771 /* Compute new gpu domains and update invalidate/flush */
Chris Wilsonb6651452010-10-23 10:15:06 +01003772 i915_gem_object_set_to_gpu_domain(obj, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003773 }
3774
Keith Packard646f0f62008-11-20 23:23:03 -08003775 if (dev->invalidate_domains | dev->flush_domains) {
3776#if WATCH_EXEC
3777 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3778 __func__,
3779 dev->invalidate_domains,
3780 dev->flush_domains);
3781#endif
Chris Wilson9af90d12010-10-17 10:01:56 +01003782 i915_gem_flush(dev, file,
Keith Packard646f0f62008-11-20 23:23:03 -08003783 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003784 dev->flush_domains,
3785 dev_priv->mm.flush_rings);
Keith Packard646f0f62008-11-20 23:23:03 -08003786 }
Eric Anholt673a3942008-07-30 12:06:12 -07003787
Eric Anholtefbeed92009-02-19 14:54:51 -08003788 for (i = 0; i < args->buffer_count; i++) {
3789 struct drm_gem_object *obj = object_list[i];
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003790 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003791 obj->write_domain = obj->pending_write_domain;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003792 trace_i915_gem_object_change_domain(obj,
3793 obj->read_domains,
3794 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003795 }
3796
Eric Anholt673a3942008-07-30 12:06:12 -07003797#if WATCH_COHERENCY
3798 for (i = 0; i < args->buffer_count; i++) {
3799 i915_gem_object_check_coherency(object_list[i],
3800 exec_list[i].handle);
3801 }
3802#endif
3803
Eric Anholt673a3942008-07-30 12:06:12 -07003804#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003805 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003806 args->batch_len,
3807 __func__,
3808 ~0);
3809#endif
3810
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003811 /* Check for any pending flips. As we only maintain a flip queue depth
3812 * of 1, we can simply insert a WAIT for the next display flip prior
3813 * to executing the batch and avoid stalling the CPU.
3814 */
3815 flips = 0;
3816 for (i = 0; i < args->buffer_count; i++) {
3817 if (object_list[i]->write_domain)
3818 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
3819 }
3820 if (flips) {
3821 int plane, flip_mask;
3822
3823 for (plane = 0; flips >> plane; plane++) {
3824 if (((flips >> plane) & 1) == 0)
3825 continue;
3826
3827 if (plane)
3828 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
3829 else
3830 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
3831
3832 intel_ring_begin(dev, ring, 2);
3833 intel_ring_emit(dev, ring,
3834 MI_WAIT_FOR_EVENT | flip_mask);
3835 intel_ring_emit(dev, ring, MI_NOOP);
3836 intel_ring_advance(dev, ring);
3837 }
3838 }
3839
Eric Anholt673a3942008-07-30 12:06:12 -07003840 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003841 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003842 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003843 if (ret) {
3844 DRM_ERROR("dispatch failed %d\n", ret);
3845 goto err;
3846 }
3847
3848 /*
3849 * Ensure that the commands in the batch buffer are
3850 * finished before the interrupt fires
3851 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003852 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003853
Eric Anholt673a3942008-07-30 12:06:12 -07003854 for (i = 0; i < args->buffer_count; i++) {
3855 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003856
Daniel Vetter617dbe22010-02-11 22:16:02 +01003857 i915_gem_object_move_to_active(obj, ring);
Chris Wilson64193402010-10-24 12:38:05 +01003858 if (obj->write_domain)
3859 list_move_tail(&to_intel_bo(obj)->gpu_write_list,
3860 &ring->gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003861 }
Eric Anholt673a3942008-07-30 12:06:12 -07003862
Chris Wilson9af90d12010-10-17 10:01:56 +01003863 i915_add_request(dev, file, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003864 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003865
Eric Anholt673a3942008-07-30 12:06:12 -07003866err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003867 for (i = 0; i < args->buffer_count; i++) {
3868 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003869 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003870 obj_priv->in_execbuffer = false;
3871 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003872 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003873 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003874
Eric Anholt673a3942008-07-30 12:06:12 -07003875 mutex_unlock(&dev->struct_mutex);
3876
Chris Wilson93533c22010-01-31 10:40:48 +00003877pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003878 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003879 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003880 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003881
3882 return ret;
3883}
3884
Jesse Barnes76446ca2009-12-17 22:05:42 -05003885/*
3886 * Legacy execbuffer just creates an exec2 list from the original exec object
3887 * list array and passes it to the real function.
3888 */
3889int
3890i915_gem_execbuffer(struct drm_device *dev, void *data,
3891 struct drm_file *file_priv)
3892{
3893 struct drm_i915_gem_execbuffer *args = data;
3894 struct drm_i915_gem_execbuffer2 exec2;
3895 struct drm_i915_gem_exec_object *exec_list = NULL;
3896 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3897 int ret, i;
3898
3899#if WATCH_EXEC
3900 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3901 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3902#endif
3903
3904 if (args->buffer_count < 1) {
3905 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3906 return -EINVAL;
3907 }
3908
3909 /* Copy in the exec list from userland */
3910 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3911 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3912 if (exec_list == NULL || exec2_list == NULL) {
3913 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3914 args->buffer_count);
3915 drm_free_large(exec_list);
3916 drm_free_large(exec2_list);
3917 return -ENOMEM;
3918 }
3919 ret = copy_from_user(exec_list,
3920 (struct drm_i915_relocation_entry __user *)
3921 (uintptr_t) args->buffers_ptr,
3922 sizeof(*exec_list) * args->buffer_count);
3923 if (ret != 0) {
3924 DRM_ERROR("copy %d exec entries failed %d\n",
3925 args->buffer_count, ret);
3926 drm_free_large(exec_list);
3927 drm_free_large(exec2_list);
3928 return -EFAULT;
3929 }
3930
3931 for (i = 0; i < args->buffer_count; i++) {
3932 exec2_list[i].handle = exec_list[i].handle;
3933 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3934 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3935 exec2_list[i].alignment = exec_list[i].alignment;
3936 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003937 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05003938 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3939 else
3940 exec2_list[i].flags = 0;
3941 }
3942
3943 exec2.buffers_ptr = args->buffers_ptr;
3944 exec2.buffer_count = args->buffer_count;
3945 exec2.batch_start_offset = args->batch_start_offset;
3946 exec2.batch_len = args->batch_len;
3947 exec2.DR1 = args->DR1;
3948 exec2.DR4 = args->DR4;
3949 exec2.num_cliprects = args->num_cliprects;
3950 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003951 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003952
3953 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3954 if (!ret) {
3955 /* Copy the new buffer offsets back to the user's exec list. */
3956 for (i = 0; i < args->buffer_count; i++)
3957 exec_list[i].offset = exec2_list[i].offset;
3958 /* ... and back out to userspace */
3959 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3960 (uintptr_t) args->buffers_ptr,
3961 exec_list,
3962 sizeof(*exec_list) * args->buffer_count);
3963 if (ret) {
3964 ret = -EFAULT;
3965 DRM_ERROR("failed to copy %d exec entries "
3966 "back to user (%d)\n",
3967 args->buffer_count, ret);
3968 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003969 }
3970
3971 drm_free_large(exec_list);
3972 drm_free_large(exec2_list);
3973 return ret;
3974}
3975
3976int
3977i915_gem_execbuffer2(struct drm_device *dev, void *data,
3978 struct drm_file *file_priv)
3979{
3980 struct drm_i915_gem_execbuffer2 *args = data;
3981 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3982 int ret;
3983
3984#if WATCH_EXEC
3985 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3986 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3987#endif
3988
3989 if (args->buffer_count < 1) {
3990 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
3991 return -EINVAL;
3992 }
3993
3994 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3995 if (exec2_list == NULL) {
3996 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3997 args->buffer_count);
3998 return -ENOMEM;
3999 }
4000 ret = copy_from_user(exec2_list,
4001 (struct drm_i915_relocation_entry __user *)
4002 (uintptr_t) args->buffers_ptr,
4003 sizeof(*exec2_list) * args->buffer_count);
4004 if (ret != 0) {
4005 DRM_ERROR("copy %d exec entries failed %d\n",
4006 args->buffer_count, ret);
4007 drm_free_large(exec2_list);
4008 return -EFAULT;
4009 }
4010
4011 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4012 if (!ret) {
4013 /* Copy the new buffer offsets back to the user's exec list. */
4014 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4015 (uintptr_t) args->buffers_ptr,
4016 exec2_list,
4017 sizeof(*exec2_list) * args->buffer_count);
4018 if (ret) {
4019 ret = -EFAULT;
4020 DRM_ERROR("failed to copy %d exec entries "
4021 "back to user (%d)\n",
4022 args->buffer_count, ret);
4023 }
4024 }
4025
4026 drm_free_large(exec2_list);
4027 return ret;
4028}
4029
Eric Anholt673a3942008-07-30 12:06:12 -07004030int
4031i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4032{
4033 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004034 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004035 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004036 int ret;
4037
Daniel Vetter778c3542010-05-13 11:49:44 +02004038 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004039 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004040
4041 if (obj_priv->gtt_space != NULL) {
4042 if (alignment == 0)
4043 alignment = i915_gem_get_gtt_alignment(obj);
4044 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004045 WARN(obj_priv->pin_count,
4046 "bo is already pinned with incorrect alignment:"
4047 " offset=%x, req.alignment=%x\n",
4048 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004049 ret = i915_gem_object_unbind(obj);
4050 if (ret)
4051 return ret;
4052 }
4053 }
4054
Eric Anholt673a3942008-07-30 12:06:12 -07004055 if (obj_priv->gtt_space == NULL) {
4056 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004057 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004058 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004059 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004060
Eric Anholt673a3942008-07-30 12:06:12 -07004061 obj_priv->pin_count++;
4062
4063 /* If the object is not active and not pending a flush,
4064 * remove it from the inactive list
4065 */
4066 if (obj_priv->pin_count == 1) {
Chris Wilson73aa8082010-09-30 11:46:12 +01004067 i915_gem_info_add_pin(dev_priv, obj->size);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004068 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004069 list_move_tail(&obj_priv->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004070 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004071 }
Eric Anholt673a3942008-07-30 12:06:12 -07004072
Chris Wilson23bc5982010-09-29 16:10:57 +01004073 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004074 return 0;
4075}
4076
4077void
4078i915_gem_object_unpin(struct drm_gem_object *obj)
4079{
4080 struct drm_device *dev = obj->dev;
4081 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004082 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004083
Chris Wilson23bc5982010-09-29 16:10:57 +01004084 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004085 obj_priv->pin_count--;
4086 BUG_ON(obj_priv->pin_count < 0);
4087 BUG_ON(obj_priv->gtt_space == NULL);
4088
4089 /* If the object is no longer pinned, and is
4090 * neither active nor being flushed, then stick it on
4091 * the inactive list
4092 */
4093 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004094 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004095 list_move_tail(&obj_priv->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004096 &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01004097 i915_gem_info_remove_pin(dev_priv, obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07004098 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004099 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004100}
4101
4102int
4103i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4104 struct drm_file *file_priv)
4105{
4106 struct drm_i915_gem_pin *args = data;
4107 struct drm_gem_object *obj;
4108 struct drm_i915_gem_object *obj_priv;
4109 int ret;
4110
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004111 ret = i915_mutex_lock_interruptible(dev);
4112 if (ret)
4113 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004114
4115 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4116 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004117 ret = -ENOENT;
4118 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004119 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004120 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004121
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004122 if (obj_priv->madv != I915_MADV_WILLNEED) {
4123 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004124 ret = -EINVAL;
4125 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004126 }
4127
Jesse Barnes79e53942008-11-07 14:24:08 -08004128 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4129 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4130 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004131 ret = -EINVAL;
4132 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004133 }
4134
4135 obj_priv->user_pin_count++;
4136 obj_priv->pin_filp = file_priv;
4137 if (obj_priv->user_pin_count == 1) {
4138 ret = i915_gem_object_pin(obj, args->alignment);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004139 if (ret)
4140 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004141 }
4142
4143 /* XXX - flush the CPU caches for pinned objects
4144 * as the X server doesn't manage domains yet
4145 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004146 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004147 args->offset = obj_priv->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004148out:
Eric Anholt673a3942008-07-30 12:06:12 -07004149 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004150unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004151 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004152 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004153}
4154
4155int
4156i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4157 struct drm_file *file_priv)
4158{
4159 struct drm_i915_gem_pin *args = data;
4160 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004161 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004162 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004163
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004164 ret = i915_mutex_lock_interruptible(dev);
4165 if (ret)
4166 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004167
4168 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4169 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004170 ret = -ENOENT;
4171 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004172 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004173 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004174
Jesse Barnes79e53942008-11-07 14:24:08 -08004175 if (obj_priv->pin_filp != file_priv) {
4176 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4177 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004178 ret = -EINVAL;
4179 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004180 }
4181 obj_priv->user_pin_count--;
4182 if (obj_priv->user_pin_count == 0) {
4183 obj_priv->pin_filp = NULL;
4184 i915_gem_object_unpin(obj);
4185 }
Eric Anholt673a3942008-07-30 12:06:12 -07004186
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004187out:
Eric Anholt673a3942008-07-30 12:06:12 -07004188 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004189unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004190 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004191 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004192}
4193
4194int
4195i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4196 struct drm_file *file_priv)
4197{
4198 struct drm_i915_gem_busy *args = data;
4199 struct drm_gem_object *obj;
4200 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004201 int ret;
4202
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004203 ret = i915_mutex_lock_interruptible(dev);
4204 if (ret)
4205 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004206
Eric Anholt673a3942008-07-30 12:06:12 -07004207 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4208 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004209 ret = -ENOENT;
4210 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004211 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004212 obj_priv = to_intel_bo(obj);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004213
Chris Wilson0be555b2010-08-04 15:36:30 +01004214 /* Count all active objects as busy, even if they are currently not used
4215 * by the gpu. Users of this interface expect objects to eventually
4216 * become non-busy without any further actions, therefore emit any
4217 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004218 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004219 args->busy = obj_priv->active;
4220 if (args->busy) {
4221 /* Unconditionally flush objects, even when the gpu still uses this
4222 * object. Userspace calling this function indicates that it wants to
4223 * use this buffer rather sooner than later, so issuing the required
4224 * flush earlier is beneficial.
4225 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004226 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4227 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004228 obj_priv->ring,
4229 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004230
4231 /* Update the active list for the hardware's current position.
4232 * Otherwise this only updates on a delayed timer or when irqs
4233 * are actually unmasked, and our working set ends up being
4234 * larger than required.
4235 */
4236 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4237
4238 args->busy = obj_priv->active;
4239 }
Eric Anholt673a3942008-07-30 12:06:12 -07004240
4241 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004242unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004243 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004244 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004245}
4246
4247int
4248i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4249 struct drm_file *file_priv)
4250{
4251 return i915_gem_ring_throttle(dev, file_priv);
4252}
4253
Chris Wilson3ef94da2009-09-14 16:50:29 +01004254int
4255i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4256 struct drm_file *file_priv)
4257{
4258 struct drm_i915_gem_madvise *args = data;
4259 struct drm_gem_object *obj;
4260 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004261 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004262
4263 switch (args->madv) {
4264 case I915_MADV_DONTNEED:
4265 case I915_MADV_WILLNEED:
4266 break;
4267 default:
4268 return -EINVAL;
4269 }
4270
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004271 ret = i915_mutex_lock_interruptible(dev);
4272 if (ret)
4273 return ret;
4274
Chris Wilson3ef94da2009-09-14 16:50:29 +01004275 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4276 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004277 ret = -ENOENT;
4278 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004279 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004280 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004281
4282 if (obj_priv->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004283 ret = -EINVAL;
4284 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004285 }
4286
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004287 if (obj_priv->madv != __I915_MADV_PURGED)
4288 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004289
Chris Wilson2d7ef392009-09-20 23:13:10 +01004290 /* if the object is no longer bound, discard its backing storage */
4291 if (i915_gem_object_is_purgeable(obj_priv) &&
4292 obj_priv->gtt_space == NULL)
4293 i915_gem_object_truncate(obj);
4294
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004295 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4296
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004297out:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004298 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004299unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004300 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004301 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004302}
4303
Daniel Vetterac52bc52010-04-09 19:05:06 +00004304struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4305 size_t size)
4306{
Chris Wilson73aa8082010-09-30 11:46:12 +01004307 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004308 struct drm_i915_gem_object *obj;
4309
4310 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4311 if (obj == NULL)
4312 return NULL;
4313
4314 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4315 kfree(obj);
4316 return NULL;
4317 }
4318
Chris Wilson73aa8082010-09-30 11:46:12 +01004319 i915_gem_info_add_obj(dev_priv, size);
4320
Daniel Vetterc397b902010-04-09 19:05:07 +00004321 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4322 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4323
4324 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004325 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004326 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004327 INIT_LIST_HEAD(&obj->mm_list);
4328 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004329 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004330 obj->madv = I915_MADV_WILLNEED;
4331
Daniel Vetterc397b902010-04-09 19:05:07 +00004332 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004333}
4334
Eric Anholt673a3942008-07-30 12:06:12 -07004335int i915_gem_init_object(struct drm_gem_object *obj)
4336{
Daniel Vetterc397b902010-04-09 19:05:07 +00004337 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004338
Eric Anholt673a3942008-07-30 12:06:12 -07004339 return 0;
4340}
4341
Chris Wilsonbe726152010-07-23 23:18:50 +01004342static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4343{
4344 struct drm_device *dev = obj->dev;
4345 drm_i915_private_t *dev_priv = dev->dev_private;
4346 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4347 int ret;
4348
4349 ret = i915_gem_object_unbind(obj);
4350 if (ret == -ERESTARTSYS) {
Chris Wilson69dc4982010-10-19 10:36:51 +01004351 list_move(&obj_priv->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004352 &dev_priv->mm.deferred_free_list);
4353 return;
4354 }
4355
4356 if (obj_priv->mmap_offset)
4357 i915_gem_free_mmap_offset(obj);
4358
4359 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004360 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004361
4362 kfree(obj_priv->page_cpu_valid);
4363 kfree(obj_priv->bit_17);
4364 kfree(obj_priv);
4365}
4366
Eric Anholt673a3942008-07-30 12:06:12 -07004367void i915_gem_free_object(struct drm_gem_object *obj)
4368{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004369 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004370 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004371
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004372 trace_i915_gem_object_destroy(obj);
4373
Eric Anholt673a3942008-07-30 12:06:12 -07004374 while (obj_priv->pin_count > 0)
4375 i915_gem_object_unpin(obj);
4376
Dave Airlie71acb5e2008-12-30 20:31:46 +10004377 if (obj_priv->phys_obj)
4378 i915_gem_detach_phys_object(dev, obj);
4379
Chris Wilsonbe726152010-07-23 23:18:50 +01004380 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004381}
4382
Jesse Barnes5669fca2009-02-17 15:13:31 -08004383int
Eric Anholt673a3942008-07-30 12:06:12 -07004384i915_gem_idle(struct drm_device *dev)
4385{
4386 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004387 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004388
Keith Packard6dbe2772008-10-14 21:41:13 -07004389 mutex_lock(&dev->struct_mutex);
4390
Chris Wilson87acb0a2010-10-19 10:13:00 +01004391 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004392 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004393 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004394 }
Eric Anholt673a3942008-07-30 12:06:12 -07004395
Chris Wilson29105cc2010-01-07 10:39:13 +00004396 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004397 if (ret) {
4398 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004399 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004400 }
Eric Anholt673a3942008-07-30 12:06:12 -07004401
Chris Wilson29105cc2010-01-07 10:39:13 +00004402 /* Under UMS, be paranoid and evict. */
4403 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004404 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004405 if (ret) {
4406 mutex_unlock(&dev->struct_mutex);
4407 return ret;
4408 }
4409 }
4410
4411 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4412 * We need to replace this with a semaphore, or something.
4413 * And not confound mm.suspended!
4414 */
4415 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004416 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004417
4418 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004419 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004420
Keith Packard6dbe2772008-10-14 21:41:13 -07004421 mutex_unlock(&dev->struct_mutex);
4422
Chris Wilson29105cc2010-01-07 10:39:13 +00004423 /* Cancel the retire work handler, which should be idle now. */
4424 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4425
Eric Anholt673a3942008-07-30 12:06:12 -07004426 return 0;
4427}
4428
Jesse Barnese552eb72010-04-21 11:39:23 -07004429/*
4430 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4431 * over cache flushing.
4432 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004433static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004434i915_gem_init_pipe_control(struct drm_device *dev)
4435{
4436 drm_i915_private_t *dev_priv = dev->dev_private;
4437 struct drm_gem_object *obj;
4438 struct drm_i915_gem_object *obj_priv;
4439 int ret;
4440
Eric Anholt34dc4d42010-05-07 14:30:03 -07004441 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004442 if (obj == NULL) {
4443 DRM_ERROR("Failed to allocate seqno page\n");
4444 ret = -ENOMEM;
4445 goto err;
4446 }
4447 obj_priv = to_intel_bo(obj);
4448 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4449
4450 ret = i915_gem_object_pin(obj, 4096);
4451 if (ret)
4452 goto err_unref;
4453
4454 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4455 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4456 if (dev_priv->seqno_page == NULL)
4457 goto err_unpin;
4458
4459 dev_priv->seqno_obj = obj;
4460 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4461
4462 return 0;
4463
4464err_unpin:
4465 i915_gem_object_unpin(obj);
4466err_unref:
4467 drm_gem_object_unreference(obj);
4468err:
4469 return ret;
4470}
4471
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004472
4473static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004474i915_gem_cleanup_pipe_control(struct drm_device *dev)
4475{
4476 drm_i915_private_t *dev_priv = dev->dev_private;
4477 struct drm_gem_object *obj;
4478 struct drm_i915_gem_object *obj_priv;
4479
4480 obj = dev_priv->seqno_obj;
4481 obj_priv = to_intel_bo(obj);
4482 kunmap(obj_priv->pages[0]);
4483 i915_gem_object_unpin(obj);
4484 drm_gem_object_unreference(obj);
4485 dev_priv->seqno_obj = NULL;
4486
4487 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004488}
4489
Eric Anholt673a3942008-07-30 12:06:12 -07004490int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004491i915_gem_init_ringbuffer(struct drm_device *dev)
4492{
4493 drm_i915_private_t *dev_priv = dev->dev_private;
4494 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004495
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004496 if (HAS_PIPE_CONTROL(dev)) {
4497 ret = i915_gem_init_pipe_control(dev);
4498 if (ret)
4499 return ret;
4500 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004501
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004502 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004503 if (ret)
4504 goto cleanup_pipe_control;
4505
4506 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004507 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004508 if (ret)
4509 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004510 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004511
Chris Wilson549f7362010-10-19 11:19:32 +01004512 if (HAS_BLT(dev)) {
4513 ret = intel_init_blt_ring_buffer(dev);
4514 if (ret)
4515 goto cleanup_bsd_ring;
4516 }
4517
Chris Wilson6f392d52010-08-07 11:01:22 +01004518 dev_priv->next_seqno = 1;
4519
Chris Wilson68f95ba2010-05-27 13:18:22 +01004520 return 0;
4521
Chris Wilson549f7362010-10-19 11:19:32 +01004522cleanup_bsd_ring:
4523 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004524cleanup_render_ring:
4525 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4526cleanup_pipe_control:
4527 if (HAS_PIPE_CONTROL(dev))
4528 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004529 return ret;
4530}
4531
4532void
4533i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4534{
4535 drm_i915_private_t *dev_priv = dev->dev_private;
4536
4537 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01004538 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01004539 intel_cleanup_ring_buffer(dev, &dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004540 if (HAS_PIPE_CONTROL(dev))
4541 i915_gem_cleanup_pipe_control(dev);
4542}
4543
4544int
Eric Anholt673a3942008-07-30 12:06:12 -07004545i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4546 struct drm_file *file_priv)
4547{
4548 drm_i915_private_t *dev_priv = dev->dev_private;
4549 int ret;
4550
Jesse Barnes79e53942008-11-07 14:24:08 -08004551 if (drm_core_check_feature(dev, DRIVER_MODESET))
4552 return 0;
4553
Ben Gamariba1234d2009-09-14 17:48:47 -04004554 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004555 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004556 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004557 }
4558
Eric Anholt673a3942008-07-30 12:06:12 -07004559 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004560 dev_priv->mm.suspended = 0;
4561
4562 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004563 if (ret != 0) {
4564 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004565 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004566 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004567
Chris Wilson69dc4982010-10-19 10:36:51 +01004568 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004569 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004570 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004571 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004572 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4573 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004574 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004575 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004576 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004577 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004578
Chris Wilson5f353082010-06-07 14:03:03 +01004579 ret = drm_irq_install(dev);
4580 if (ret)
4581 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004582
Eric Anholt673a3942008-07-30 12:06:12 -07004583 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004584
4585cleanup_ringbuffer:
4586 mutex_lock(&dev->struct_mutex);
4587 i915_gem_cleanup_ringbuffer(dev);
4588 dev_priv->mm.suspended = 1;
4589 mutex_unlock(&dev->struct_mutex);
4590
4591 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004592}
4593
4594int
4595i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4596 struct drm_file *file_priv)
4597{
Jesse Barnes79e53942008-11-07 14:24:08 -08004598 if (drm_core_check_feature(dev, DRIVER_MODESET))
4599 return 0;
4600
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004601 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004602 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004603}
4604
4605void
4606i915_gem_lastclose(struct drm_device *dev)
4607{
4608 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004609
Eric Anholte806b492009-01-22 09:56:58 -08004610 if (drm_core_check_feature(dev, DRIVER_MODESET))
4611 return;
4612
Keith Packard6dbe2772008-10-14 21:41:13 -07004613 ret = i915_gem_idle(dev);
4614 if (ret)
4615 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004616}
4617
Chris Wilson64193402010-10-24 12:38:05 +01004618static void
4619init_ring_lists(struct intel_ring_buffer *ring)
4620{
4621 INIT_LIST_HEAD(&ring->active_list);
4622 INIT_LIST_HEAD(&ring->request_list);
4623 INIT_LIST_HEAD(&ring->gpu_write_list);
4624}
4625
Eric Anholt673a3942008-07-30 12:06:12 -07004626void
4627i915_gem_load(struct drm_device *dev)
4628{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004629 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004630 drm_i915_private_t *dev_priv = dev->dev_private;
4631
Chris Wilson69dc4982010-10-19 10:36:51 +01004632 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004633 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4634 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004635 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004636 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004637 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Chris Wilson64193402010-10-24 12:38:05 +01004638 init_ring_lists(&dev_priv->render_ring);
4639 init_ring_lists(&dev_priv->bsd_ring);
4640 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004641 for (i = 0; i < 16; i++)
4642 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004643 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4644 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004645 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004646 spin_lock(&shrink_list_lock);
4647 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4648 spin_unlock(&shrink_list_lock);
4649
Dave Airlie94400122010-07-20 13:15:31 +10004650 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4651 if (IS_GEN3(dev)) {
4652 u32 tmp = I915_READ(MI_ARB_STATE);
4653 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4654 /* arb state is a masked write, so set bit + bit in mask */
4655 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4656 I915_WRITE(MI_ARB_STATE, tmp);
4657 }
4658 }
4659
Jesse Barnesde151cf2008-11-12 10:03:55 -08004660 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004661 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4662 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004663
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004664 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004665 dev_priv->num_fence_regs = 16;
4666 else
4667 dev_priv->num_fence_regs = 8;
4668
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004669 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004670 switch (INTEL_INFO(dev)->gen) {
4671 case 6:
4672 for (i = 0; i < 16; i++)
4673 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4674 break;
4675 case 5:
4676 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004677 for (i = 0; i < 16; i++)
4678 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004679 break;
4680 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004681 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4682 for (i = 0; i < 8; i++)
4683 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004684 case 2:
4685 for (i = 0; i < 8; i++)
4686 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4687 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004688 }
Eric Anholt673a3942008-07-30 12:06:12 -07004689 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004690 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004691}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004692
4693/*
4694 * Create a physically contiguous memory object for this object
4695 * e.g. for cursor + overlay regs
4696 */
Chris Wilson995b6762010-08-20 13:23:26 +01004697static int i915_gem_init_phys_object(struct drm_device *dev,
4698 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004699{
4700 drm_i915_private_t *dev_priv = dev->dev_private;
4701 struct drm_i915_gem_phys_object *phys_obj;
4702 int ret;
4703
4704 if (dev_priv->mm.phys_objs[id - 1] || !size)
4705 return 0;
4706
Eric Anholt9a298b22009-03-24 12:23:04 -07004707 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004708 if (!phys_obj)
4709 return -ENOMEM;
4710
4711 phys_obj->id = id;
4712
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004713 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004714 if (!phys_obj->handle) {
4715 ret = -ENOMEM;
4716 goto kfree_obj;
4717 }
4718#ifdef CONFIG_X86
4719 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4720#endif
4721
4722 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4723
4724 return 0;
4725kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004726 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004727 return ret;
4728}
4729
Chris Wilson995b6762010-08-20 13:23:26 +01004730static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004731{
4732 drm_i915_private_t *dev_priv = dev->dev_private;
4733 struct drm_i915_gem_phys_object *phys_obj;
4734
4735 if (!dev_priv->mm.phys_objs[id - 1])
4736 return;
4737
4738 phys_obj = dev_priv->mm.phys_objs[id - 1];
4739 if (phys_obj->cur_obj) {
4740 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4741 }
4742
4743#ifdef CONFIG_X86
4744 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4745#endif
4746 drm_pci_free(dev, phys_obj->handle);
4747 kfree(phys_obj);
4748 dev_priv->mm.phys_objs[id - 1] = NULL;
4749}
4750
4751void i915_gem_free_all_phys_object(struct drm_device *dev)
4752{
4753 int i;
4754
Dave Airlie260883c2009-01-22 17:58:49 +10004755 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004756 i915_gem_free_phys_object(dev, i);
4757}
4758
4759void i915_gem_detach_phys_object(struct drm_device *dev,
4760 struct drm_gem_object *obj)
4761{
4762 struct drm_i915_gem_object *obj_priv;
4763 int i;
4764 int ret;
4765 int page_count;
4766
Daniel Vetter23010e42010-03-08 13:35:02 +01004767 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004768 if (!obj_priv->phys_obj)
4769 return;
4770
Chris Wilson4bdadb92010-01-27 13:36:32 +00004771 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004772 if (ret)
4773 goto out;
4774
4775 page_count = obj->size / PAGE_SIZE;
4776
4777 for (i = 0; i < page_count; i++) {
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004778 char *dst = kmap_atomic(obj_priv->pages[i]);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004779 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4780
4781 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004782 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004783 }
Eric Anholt856fa192009-03-19 14:10:50 -07004784 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004785 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004786
4787 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004788out:
4789 obj_priv->phys_obj->cur_obj = NULL;
4790 obj_priv->phys_obj = NULL;
4791}
4792
4793int
4794i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004795 struct drm_gem_object *obj,
4796 int id,
4797 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004798{
4799 drm_i915_private_t *dev_priv = dev->dev_private;
4800 struct drm_i915_gem_object *obj_priv;
4801 int ret = 0;
4802 int page_count;
4803 int i;
4804
4805 if (id > I915_MAX_PHYS_OBJECT)
4806 return -EINVAL;
4807
Daniel Vetter23010e42010-03-08 13:35:02 +01004808 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004809
4810 if (obj_priv->phys_obj) {
4811 if (obj_priv->phys_obj->id == id)
4812 return 0;
4813 i915_gem_detach_phys_object(dev, obj);
4814 }
4815
Dave Airlie71acb5e2008-12-30 20:31:46 +10004816 /* create a new object */
4817 if (!dev_priv->mm.phys_objs[id - 1]) {
4818 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004819 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004820 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004821 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004822 goto out;
4823 }
4824 }
4825
4826 /* bind to the object */
4827 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4828 obj_priv->phys_obj->cur_obj = obj;
4829
Chris Wilson4bdadb92010-01-27 13:36:32 +00004830 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004831 if (ret) {
4832 DRM_ERROR("failed to get page list\n");
4833 goto out;
4834 }
4835
4836 page_count = obj->size / PAGE_SIZE;
4837
4838 for (i = 0; i < page_count; i++) {
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004839 char *src = kmap_atomic(obj_priv->pages[i]);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004840 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4841
4842 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004843 kunmap_atomic(src);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004844 }
4845
Chris Wilsond78b47b2009-06-17 21:52:49 +01004846 i915_gem_object_put_pages(obj);
4847
Dave Airlie71acb5e2008-12-30 20:31:46 +10004848 return 0;
4849out:
4850 return ret;
4851}
4852
4853static int
4854i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4855 struct drm_i915_gem_pwrite *args,
4856 struct drm_file *file_priv)
4857{
Daniel Vetter23010e42010-03-08 13:35:02 +01004858 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004859 void *obj_addr;
4860 int ret;
4861 char __user *user_data;
4862
4863 user_data = (char __user *) (uintptr_t) args->data_ptr;
4864 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4865
Zhao Yakui44d98a62009-10-09 11:39:40 +08004866 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004867 ret = copy_from_user(obj_addr, user_data, args->size);
4868 if (ret)
4869 return -EFAULT;
4870
4871 drm_agp_chipset_flush(dev);
4872 return 0;
4873}
Eric Anholtb9624422009-06-03 07:27:35 +00004874
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004875void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004876{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004877 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004878
4879 /* Clean up our request list when the client is going away, so that
4880 * later retire_requests won't dereference our soon-to-be-gone
4881 * file_priv.
4882 */
Chris Wilson1c255952010-09-26 11:03:27 +01004883 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004884 while (!list_empty(&file_priv->mm.request_list)) {
4885 struct drm_i915_gem_request *request;
4886
4887 request = list_first_entry(&file_priv->mm.request_list,
4888 struct drm_i915_gem_request,
4889 client_list);
4890 list_del(&request->client_list);
4891 request->file_priv = NULL;
4892 }
Chris Wilson1c255952010-09-26 11:03:27 +01004893 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004894}
Chris Wilson31169712009-09-14 16:50:28 +01004895
Chris Wilson31169712009-09-14 16:50:28 +01004896static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004897i915_gpu_is_active(struct drm_device *dev)
4898{
4899 drm_i915_private_t *dev_priv = dev->dev_private;
4900 int lists_empty;
4901
Chris Wilson1637ef42010-04-20 17:10:35 +01004902 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson87acb0a2010-10-19 10:13:00 +01004903 list_empty(&dev_priv->render_ring.active_list) &&
Chris Wilson549f7362010-10-19 11:19:32 +01004904 list_empty(&dev_priv->bsd_ring.active_list) &&
4905 list_empty(&dev_priv->blt_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004906
4907 return !lists_empty;
4908}
4909
4910static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004911i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004912{
4913 drm_i915_private_t *dev_priv, *next_dev;
4914 struct drm_i915_gem_object *obj_priv, *next_obj;
4915 int cnt = 0;
4916 int would_deadlock = 1;
4917
4918 /* "fast-path" to count number of available objects */
4919 if (nr_to_scan == 0) {
4920 spin_lock(&shrink_list_lock);
4921 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4922 struct drm_device *dev = dev_priv->dev;
4923
4924 if (mutex_trylock(&dev->struct_mutex)) {
4925 list_for_each_entry(obj_priv,
4926 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01004927 mm_list)
Chris Wilson31169712009-09-14 16:50:28 +01004928 cnt++;
4929 mutex_unlock(&dev->struct_mutex);
4930 }
4931 }
4932 spin_unlock(&shrink_list_lock);
4933
4934 return (cnt / 100) * sysctl_vfs_cache_pressure;
4935 }
4936
4937 spin_lock(&shrink_list_lock);
4938
Chris Wilson1637ef42010-04-20 17:10:35 +01004939rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004940 /* first scan for clean buffers */
4941 list_for_each_entry_safe(dev_priv, next_dev,
4942 &shrink_list, mm.shrink_list) {
4943 struct drm_device *dev = dev_priv->dev;
4944
4945 if (! mutex_trylock(&dev->struct_mutex))
4946 continue;
4947
4948 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004949 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004950
Chris Wilson31169712009-09-14 16:50:28 +01004951 list_for_each_entry_safe(obj_priv, next_obj,
4952 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01004953 mm_list) {
Chris Wilson31169712009-09-14 16:50:28 +01004954 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004955 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004956 if (--nr_to_scan <= 0)
4957 break;
4958 }
4959 }
4960
4961 spin_lock(&shrink_list_lock);
4962 mutex_unlock(&dev->struct_mutex);
4963
Chris Wilson963b4832009-09-20 23:03:54 +01004964 would_deadlock = 0;
4965
Chris Wilson31169712009-09-14 16:50:28 +01004966 if (nr_to_scan <= 0)
4967 break;
4968 }
4969
4970 /* second pass, evict/count anything still on the inactive list */
4971 list_for_each_entry_safe(dev_priv, next_dev,
4972 &shrink_list, mm.shrink_list) {
4973 struct drm_device *dev = dev_priv->dev;
4974
4975 if (! mutex_trylock(&dev->struct_mutex))
4976 continue;
4977
4978 spin_unlock(&shrink_list_lock);
4979
4980 list_for_each_entry_safe(obj_priv, next_obj,
4981 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01004982 mm_list) {
Chris Wilson31169712009-09-14 16:50:28 +01004983 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004984 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004985 nr_to_scan--;
4986 } else
4987 cnt++;
4988 }
4989
4990 spin_lock(&shrink_list_lock);
4991 mutex_unlock(&dev->struct_mutex);
4992
4993 would_deadlock = 0;
4994 }
4995
Chris Wilson1637ef42010-04-20 17:10:35 +01004996 if (nr_to_scan) {
4997 int active = 0;
4998
4999 /*
5000 * We are desperate for pages, so as a last resort, wait
5001 * for the GPU to finish and discard whatever we can.
5002 * This has a dramatic impact to reduce the number of
5003 * OOM-killer events whilst running the GPU aggressively.
5004 */
5005 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5006 struct drm_device *dev = dev_priv->dev;
5007
5008 if (!mutex_trylock(&dev->struct_mutex))
5009 continue;
5010
5011 spin_unlock(&shrink_list_lock);
5012
5013 if (i915_gpu_is_active(dev)) {
5014 i915_gpu_idle(dev);
5015 active++;
5016 }
5017
5018 spin_lock(&shrink_list_lock);
5019 mutex_unlock(&dev->struct_mutex);
5020 }
5021
5022 if (active)
5023 goto rescan;
5024 }
5025
Chris Wilson31169712009-09-14 16:50:28 +01005026 spin_unlock(&shrink_list_lock);
5027
5028 if (would_deadlock)
5029 return -1;
5030 else if (cnt > 0)
5031 return (cnt / 100) * sysctl_vfs_cache_pressure;
5032 else
5033 return 0;
5034}
5035
5036static struct shrinker shrinker = {
5037 .shrink = i915_gem_shrink,
5038 .seeks = DEFAULT_SEEKS,
5039};
5040
5041__init void
5042i915_gem_shrinker_init(void)
5043{
5044 register_shrinker(&shrinker);
5045}
5046
5047__exit void
5048i915_gem_shrinker_exit(void)
5049{
5050 unregister_shrinker(&shrinker);
5051}