blob: 17b1cba3b5f11c281eedc13c5adaa5a98ac4eef8 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080037#include <linux/intel-gtt.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Daniel Vetter0108a3e2010-08-07 11:01:21 +010039static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010040
41static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
42 bool pipelined);
Eric Anholte47c68e2008-11-14 13:35:19 -080043static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
44static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080045static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
46 int write);
47static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
48 uint64_t offset,
49 uint64_t size);
50static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +010051static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
52 bool interruptible);
Jesse Barnesde151cf2008-11-12 10:03:55 -080053static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
54 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080055static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100056static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
57 struct drm_i915_gem_pwrite *args,
58 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010059static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070060
Chris Wilson5cdf5882010-09-27 15:51:07 +010061static int
62i915_gem_object_get_pages(struct drm_gem_object *obj,
63 gfp_t gfpmask);
64
65static void
66i915_gem_object_put_pages(struct drm_gem_object *obj);
67
Chris Wilson31169712009-09-14 16:50:28 +010068static LIST_HEAD(shrink_list);
69static DEFINE_SPINLOCK(shrink_list_lock);
70
Chris Wilson73aa8082010-09-30 11:46:12 +010071/* some bookkeeping */
72static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
73 size_t size)
74{
75 dev_priv->mm.object_count++;
76 dev_priv->mm.object_memory += size;
77}
78
79static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
80 size_t size)
81{
82 dev_priv->mm.object_count--;
83 dev_priv->mm.object_memory -= size;
84}
85
86static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
87 size_t size)
88{
89 dev_priv->mm.gtt_count++;
90 dev_priv->mm.gtt_memory += size;
91}
92
93static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
94 size_t size)
95{
96 dev_priv->mm.gtt_count--;
97 dev_priv->mm.gtt_memory -= size;
98}
99
100static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
101 size_t size)
102{
103 dev_priv->mm.pin_count++;
104 dev_priv->mm.pin_memory += size;
105}
106
107static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
108 size_t size)
109{
110 dev_priv->mm.pin_count--;
111 dev_priv->mm.pin_memory -= size;
112}
113
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100114int
115i915_gem_check_is_wedged(struct drm_device *dev)
116{
117 struct drm_i915_private *dev_priv = dev->dev_private;
118 struct completion *x = &dev_priv->error_completion;
119 unsigned long flags;
120 int ret;
121
122 if (!atomic_read(&dev_priv->mm.wedged))
123 return 0;
124
125 ret = wait_for_completion_interruptible(x);
126 if (ret)
127 return ret;
128
129 /* Success, we reset the GPU! */
130 if (!atomic_read(&dev_priv->mm.wedged))
131 return 0;
132
133 /* GPU is hung, bump the completion count to account for
134 * the token we just consumed so that we never hit zero and
135 * end up waiting upon a subsequent completion event that
136 * will never happen.
137 */
138 spin_lock_irqsave(&x->wait.lock, flags);
139 x->done++;
140 spin_unlock_irqrestore(&x->wait.lock, flags);
141 return -EIO;
142}
143
Chris Wilson76c1dec2010-09-25 11:22:51 +0100144static int i915_mutex_lock_interruptible(struct drm_device *dev)
145{
146 struct drm_i915_private *dev_priv = dev->dev_private;
147 int ret;
148
149 ret = i915_gem_check_is_wedged(dev);
150 if (ret)
151 return ret;
152
153 ret = mutex_lock_interruptible(&dev->struct_mutex);
154 if (ret)
155 return ret;
156
157 if (atomic_read(&dev_priv->mm.wedged)) {
158 mutex_unlock(&dev->struct_mutex);
159 return -EAGAIN;
160 }
161
Chris Wilson23bc5982010-09-29 16:10:57 +0100162 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100163 return 0;
164}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100165
Chris Wilson7d1c4802010-08-07 21:45:03 +0100166static inline bool
167i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
168{
169 return obj_priv->gtt_space &&
170 !obj_priv->active &&
171 obj_priv->pin_count == 0;
172}
173
Chris Wilson73aa8082010-09-30 11:46:12 +0100174int i915_gem_do_init(struct drm_device *dev,
175 unsigned long start,
Jesse Barnes79e53942008-11-07 14:24:08 -0800176 unsigned long end)
177{
178 drm_i915_private_t *dev_priv = dev->dev_private;
179
180 if (start >= end ||
181 (start & (PAGE_SIZE - 1)) != 0 ||
182 (end & (PAGE_SIZE - 1)) != 0) {
183 return -EINVAL;
184 }
185
186 drm_mm_init(&dev_priv->mm.gtt_space, start,
187 end - start);
188
Chris Wilson73aa8082010-09-30 11:46:12 +0100189 dev_priv->mm.gtt_total = end - start;
Jesse Barnes79e53942008-11-07 14:24:08 -0800190
191 return 0;
192}
Keith Packard6dbe2772008-10-14 21:41:13 -0700193
Eric Anholt673a3942008-07-30 12:06:12 -0700194int
195i915_gem_init_ioctl(struct drm_device *dev, void *data,
196 struct drm_file *file_priv)
197{
Eric Anholt673a3942008-07-30 12:06:12 -0700198 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800199 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700200
201 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -0800202 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700203 mutex_unlock(&dev->struct_mutex);
204
Jesse Barnes79e53942008-11-07 14:24:08 -0800205 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700206}
207
Eric Anholt5a125c32008-10-22 21:40:13 -0700208int
209i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
210 struct drm_file *file_priv)
211{
Chris Wilson73aa8082010-09-30 11:46:12 +0100212 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700213 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700214
215 if (!(dev->driver->driver_features & DRIVER_GEM))
216 return -ENODEV;
217
Chris Wilson73aa8082010-09-30 11:46:12 +0100218 mutex_lock(&dev->struct_mutex);
219 args->aper_size = dev_priv->mm.gtt_total;
220 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
221 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700222
223 return 0;
224}
225
Eric Anholt673a3942008-07-30 12:06:12 -0700226
227/**
228 * Creates a new mm object and returns a handle to it.
229 */
230int
231i915_gem_create_ioctl(struct drm_device *dev, void *data,
232 struct drm_file *file_priv)
233{
234 struct drm_i915_gem_create *args = data;
235 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300236 int ret;
237 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700238
239 args->size = roundup(args->size, PAGE_SIZE);
240
241 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000242 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700243 if (obj == NULL)
244 return -ENOMEM;
245
246 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100247 if (ret) {
Chris Wilson202f2fe2010-10-14 13:20:40 +0100248 drm_gem_object_release(obj);
249 i915_gem_info_remove_obj(dev->dev_private, obj->size);
250 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700251 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100252 }
253
Chris Wilson202f2fe2010-10-14 13:20:40 +0100254 /* drop reference from allocate - handle holds it now */
255 drm_gem_object_unreference(obj);
256 trace_i915_gem_object_create(obj);
257
Eric Anholt673a3942008-07-30 12:06:12 -0700258 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700259 return 0;
260}
261
Eric Anholt40123c12009-03-09 13:42:30 -0700262static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700263fast_shmem_read(struct page **pages,
264 loff_t page_base, int page_offset,
265 char __user *data,
266 int length)
267{
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100268 char *vaddr;
Chris Wilson4f27b752010-10-14 15:26:45 +0100269 int ret;
Eric Anholteb014592009-03-10 11:44:52 -0700270
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700271 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT]);
Chris Wilson4f27b752010-10-14 15:26:45 +0100272 ret = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700273 kunmap_atomic(vaddr);
Eric Anholteb014592009-03-10 11:44:52 -0700274
Chris Wilson4f27b752010-10-14 15:26:45 +0100275 return ret;
Eric Anholteb014592009-03-10 11:44:52 -0700276}
277
Eric Anholt280b7132009-03-12 16:56:27 -0700278static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
279{
280 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100281 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700282
283 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
284 obj_priv->tiling_mode != I915_TILING_NONE;
285}
286
Chris Wilson99a03df2010-05-27 14:15:34 +0100287static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700288slow_shmem_copy(struct page *dst_page,
289 int dst_offset,
290 struct page *src_page,
291 int src_offset,
292 int length)
293{
294 char *dst_vaddr, *src_vaddr;
295
Chris Wilson99a03df2010-05-27 14:15:34 +0100296 dst_vaddr = kmap(dst_page);
297 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700298
299 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
300
Chris Wilson99a03df2010-05-27 14:15:34 +0100301 kunmap(src_page);
302 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700303}
304
Chris Wilson99a03df2010-05-27 14:15:34 +0100305static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700306slow_shmem_bit17_copy(struct page *gpu_page,
307 int gpu_offset,
308 struct page *cpu_page,
309 int cpu_offset,
310 int length,
311 int is_read)
312{
313 char *gpu_vaddr, *cpu_vaddr;
314
315 /* Use the unswizzled path if this page isn't affected. */
316 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
317 if (is_read)
318 return slow_shmem_copy(cpu_page, cpu_offset,
319 gpu_page, gpu_offset, length);
320 else
321 return slow_shmem_copy(gpu_page, gpu_offset,
322 cpu_page, cpu_offset, length);
323 }
324
Chris Wilson99a03df2010-05-27 14:15:34 +0100325 gpu_vaddr = kmap(gpu_page);
326 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700327
328 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
329 * XORing with the other bits (A9 for Y, A9 and A10 for X)
330 */
331 while (length > 0) {
332 int cacheline_end = ALIGN(gpu_offset + 1, 64);
333 int this_length = min(cacheline_end - gpu_offset, length);
334 int swizzled_gpu_offset = gpu_offset ^ 64;
335
336 if (is_read) {
337 memcpy(cpu_vaddr + cpu_offset,
338 gpu_vaddr + swizzled_gpu_offset,
339 this_length);
340 } else {
341 memcpy(gpu_vaddr + swizzled_gpu_offset,
342 cpu_vaddr + cpu_offset,
343 this_length);
344 }
345 cpu_offset += this_length;
346 gpu_offset += this_length;
347 length -= this_length;
348 }
349
Chris Wilson99a03df2010-05-27 14:15:34 +0100350 kunmap(cpu_page);
351 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700352}
353
Eric Anholt673a3942008-07-30 12:06:12 -0700354/**
Eric Anholteb014592009-03-10 11:44:52 -0700355 * This is the fast shmem pread path, which attempts to copy_from_user directly
356 * from the backing pages of the object to the user's address space. On a
357 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
358 */
359static int
360i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
361 struct drm_i915_gem_pread *args,
362 struct drm_file *file_priv)
363{
Daniel Vetter23010e42010-03-08 13:35:02 +0100364 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700365 ssize_t remain;
366 loff_t offset, page_base;
367 char __user *user_data;
368 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700369
370 user_data = (char __user *) (uintptr_t) args->data_ptr;
371 remain = args->size;
372
Daniel Vetter23010e42010-03-08 13:35:02 +0100373 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700374 offset = args->offset;
375
376 while (remain > 0) {
377 /* Operation in this page
378 *
379 * page_base = page offset within aperture
380 * page_offset = offset within page
381 * page_length = bytes to copy for this page
382 */
383 page_base = (offset & ~(PAGE_SIZE-1));
384 page_offset = offset & (PAGE_SIZE-1);
385 page_length = remain;
386 if ((page_offset + remain) > PAGE_SIZE)
387 page_length = PAGE_SIZE - page_offset;
388
Chris Wilson4f27b752010-10-14 15:26:45 +0100389 if (fast_shmem_read(obj_priv->pages,
390 page_base, page_offset,
391 user_data, page_length))
392 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700393
394 remain -= page_length;
395 user_data += page_length;
396 offset += page_length;
397 }
398
Chris Wilson4f27b752010-10-14 15:26:45 +0100399 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700400}
401
Chris Wilson07f73f62009-09-14 16:50:30 +0100402static int
403i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
404{
405 int ret;
406
Chris Wilson4bdadb92010-01-27 13:36:32 +0000407 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100408
409 /* If we've insufficient memory to map in the pages, attempt
410 * to make some space by throwing out some old buffers.
411 */
412 if (ret == -ENOMEM) {
413 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100414
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100415 ret = i915_gem_evict_something(dev, obj->size,
416 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100417 if (ret)
418 return ret;
419
Chris Wilson4bdadb92010-01-27 13:36:32 +0000420 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100421 }
422
423 return ret;
424}
425
Eric Anholteb014592009-03-10 11:44:52 -0700426/**
427 * This is the fallback shmem pread path, which allocates temporary storage
428 * in kernel space to copy_to_user into outside of the struct_mutex, so we
429 * can copy out of the object's backing pages while holding the struct mutex
430 * and not take page faults.
431 */
432static int
433i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
434 struct drm_i915_gem_pread *args,
435 struct drm_file *file_priv)
436{
Daniel Vetter23010e42010-03-08 13:35:02 +0100437 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700438 struct mm_struct *mm = current->mm;
439 struct page **user_pages;
440 ssize_t remain;
441 loff_t offset, pinned_pages, i;
442 loff_t first_data_page, last_data_page, num_pages;
443 int shmem_page_index, shmem_page_offset;
444 int data_page_index, data_page_offset;
445 int page_length;
446 int ret;
447 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700448 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700449
450 remain = args->size;
451
452 /* Pin the user pages containing the data. We can't fault while
453 * holding the struct mutex, yet we want to hold it while
454 * dereferencing the user data.
455 */
456 first_data_page = data_ptr / PAGE_SIZE;
457 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
458 num_pages = last_data_page - first_data_page + 1;
459
Chris Wilson4f27b752010-10-14 15:26:45 +0100460 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700461 if (user_pages == NULL)
462 return -ENOMEM;
463
Chris Wilson4f27b752010-10-14 15:26:45 +0100464 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700465 down_read(&mm->mmap_sem);
466 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700467 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700468 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100469 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700470 if (pinned_pages < num_pages) {
471 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100472 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700473 }
474
Chris Wilson4f27b752010-10-14 15:26:45 +0100475 ret = i915_gem_object_set_cpu_read_domain_range(obj,
476 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700477 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100478 if (ret)
479 goto out;
480
481 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700482
Daniel Vetter23010e42010-03-08 13:35:02 +0100483 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700484 offset = args->offset;
485
486 while (remain > 0) {
487 /* Operation in this page
488 *
489 * shmem_page_index = page number within shmem file
490 * shmem_page_offset = offset within page in shmem file
491 * data_page_index = page number in get_user_pages return
492 * data_page_offset = offset with data_page_index page.
493 * page_length = bytes to copy for this page
494 */
495 shmem_page_index = offset / PAGE_SIZE;
496 shmem_page_offset = offset & ~PAGE_MASK;
497 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
498 data_page_offset = data_ptr & ~PAGE_MASK;
499
500 page_length = remain;
501 if ((shmem_page_offset + page_length) > PAGE_SIZE)
502 page_length = PAGE_SIZE - shmem_page_offset;
503 if ((data_page_offset + page_length) > PAGE_SIZE)
504 page_length = PAGE_SIZE - data_page_offset;
505
Eric Anholt280b7132009-03-12 16:56:27 -0700506 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100507 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700508 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100509 user_pages[data_page_index],
510 data_page_offset,
511 page_length,
512 1);
513 } else {
514 slow_shmem_copy(user_pages[data_page_index],
515 data_page_offset,
516 obj_priv->pages[shmem_page_index],
517 shmem_page_offset,
518 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700519 }
Eric Anholteb014592009-03-10 11:44:52 -0700520
521 remain -= page_length;
522 data_ptr += page_length;
523 offset += page_length;
524 }
525
Chris Wilson4f27b752010-10-14 15:26:45 +0100526out:
Eric Anholteb014592009-03-10 11:44:52 -0700527 for (i = 0; i < pinned_pages; i++) {
528 SetPageDirty(user_pages[i]);
529 page_cache_release(user_pages[i]);
530 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700531 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700532
533 return ret;
534}
535
Eric Anholt673a3942008-07-30 12:06:12 -0700536/**
537 * Reads data from the object referenced by handle.
538 *
539 * On error, the contents of *data are undefined.
540 */
541int
542i915_gem_pread_ioctl(struct drm_device *dev, void *data,
543 struct drm_file *file_priv)
544{
545 struct drm_i915_gem_pread *args = data;
546 struct drm_gem_object *obj;
547 struct drm_i915_gem_object *obj_priv;
Chris Wilson35b62a82010-09-26 20:23:38 +0100548 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700549
Chris Wilson51311d02010-11-17 09:10:42 +0000550 if (args->size == 0)
551 return 0;
552
553 if (!access_ok(VERIFY_WRITE,
554 (char __user *)(uintptr_t)args->data_ptr,
555 args->size))
556 return -EFAULT;
557
558 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
559 args->size);
560 if (ret)
561 return -EFAULT;
562
Chris Wilson4f27b752010-10-14 15:26:45 +0100563 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100564 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100565 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700566
567 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100568 if (obj == NULL) {
569 ret = -ENOENT;
570 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100571 }
Daniel Vetter23010e42010-03-08 13:35:02 +0100572 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700573
Chris Wilson7dcd2492010-09-26 20:21:44 +0100574 /* Bounds check source. */
575 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100576 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100577 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100578 }
579
Chris Wilson4f27b752010-10-14 15:26:45 +0100580 ret = i915_gem_object_get_pages_or_evict(obj);
581 if (ret)
582 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -0700583
Chris Wilson4f27b752010-10-14 15:26:45 +0100584 ret = i915_gem_object_set_cpu_read_domain_range(obj,
585 args->offset,
586 args->size);
587 if (ret)
588 goto out_put;
589
590 ret = -EFAULT;
591 if (!i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt673a3942008-07-30 12:06:12 -0700592 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
Chris Wilson4f27b752010-10-14 15:26:45 +0100593 if (ret == -EFAULT)
594 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700595
Chris Wilson4f27b752010-10-14 15:26:45 +0100596out_put:
597 i915_gem_object_put_pages(obj);
Chris Wilson35b62a82010-09-26 20:23:38 +0100598out:
Chris Wilson4f27b752010-10-14 15:26:45 +0100599 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100600unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100601 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700602 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700603}
604
Keith Packard0839ccb2008-10-30 19:38:48 -0700605/* This is the fast write path which cannot handle
606 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700607 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700608
Keith Packard0839ccb2008-10-30 19:38:48 -0700609static inline int
610fast_user_write(struct io_mapping *mapping,
611 loff_t page_base, int page_offset,
612 char __user *user_data,
613 int length)
614{
615 char *vaddr_atomic;
616 unsigned long unwritten;
617
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700618 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700619 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
620 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700621 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100622 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700623}
624
625/* Here's the write path which can sleep for
626 * page faults
627 */
628
Chris Wilsonab34c222010-05-27 14:15:35 +0100629static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700630slow_kernel_write(struct io_mapping *mapping,
631 loff_t gtt_base, int gtt_offset,
632 struct page *user_page, int user_offset,
633 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700634{
Chris Wilsonab34c222010-05-27 14:15:35 +0100635 char __iomem *dst_vaddr;
636 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700637
Chris Wilsonab34c222010-05-27 14:15:35 +0100638 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
639 src_vaddr = kmap(user_page);
640
641 memcpy_toio(dst_vaddr + gtt_offset,
642 src_vaddr + user_offset,
643 length);
644
645 kunmap(user_page);
646 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700647}
648
Eric Anholt40123c12009-03-09 13:42:30 -0700649static inline int
650fast_shmem_write(struct page **pages,
651 loff_t page_base, int page_offset,
652 char __user *data,
653 int length)
654{
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100655 char *vaddr;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100656 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700657
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700658 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT]);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100659 ret = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700660 kunmap_atomic(vaddr);
Eric Anholt40123c12009-03-09 13:42:30 -0700661
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100662 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700663}
664
Eric Anholt3de09aa2009-03-09 09:42:23 -0700665/**
666 * This is the fast pwrite path, where we copy the data directly from the
667 * user into the GTT, uncached.
668 */
Eric Anholt673a3942008-07-30 12:06:12 -0700669static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700670i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
671 struct drm_i915_gem_pwrite *args,
672 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700673{
Daniel Vetter23010e42010-03-08 13:35:02 +0100674 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700675 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700676 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700677 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700678 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700679 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700680
681 user_data = (char __user *) (uintptr_t) args->data_ptr;
682 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700683
Daniel Vetter23010e42010-03-08 13:35:02 +0100684 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700685 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700686
687 while (remain > 0) {
688 /* Operation in this page
689 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700690 * page_base = page offset within aperture
691 * page_offset = offset within page
692 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700693 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700694 page_base = (offset & ~(PAGE_SIZE-1));
695 page_offset = offset & (PAGE_SIZE-1);
696 page_length = remain;
697 if ((page_offset + remain) > PAGE_SIZE)
698 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700699
Keith Packard0839ccb2008-10-30 19:38:48 -0700700 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700701 * source page isn't available. Return the error and we'll
702 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700703 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100704 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
705 page_offset, user_data, page_length))
706
707 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700708
Keith Packard0839ccb2008-10-30 19:38:48 -0700709 remain -= page_length;
710 user_data += page_length;
711 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700712 }
Eric Anholt673a3942008-07-30 12:06:12 -0700713
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100714 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700715}
716
Eric Anholt3de09aa2009-03-09 09:42:23 -0700717/**
718 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
719 * the memory and maps it using kmap_atomic for copying.
720 *
721 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
722 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
723 */
Eric Anholt3043c602008-10-02 12:24:47 -0700724static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700725i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
726 struct drm_i915_gem_pwrite *args,
727 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700728{
Daniel Vetter23010e42010-03-08 13:35:02 +0100729 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700730 drm_i915_private_t *dev_priv = dev->dev_private;
731 ssize_t remain;
732 loff_t gtt_page_base, offset;
733 loff_t first_data_page, last_data_page, num_pages;
734 loff_t pinned_pages, i;
735 struct page **user_pages;
736 struct mm_struct *mm = current->mm;
737 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700738 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700739 uint64_t data_ptr = args->data_ptr;
740
741 remain = args->size;
742
743 /* Pin the user pages containing the data. We can't fault while
744 * holding the struct mutex, and all of the pwrite implementations
745 * want to hold it while dereferencing the user data.
746 */
747 first_data_page = data_ptr / PAGE_SIZE;
748 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
749 num_pages = last_data_page - first_data_page + 1;
750
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100751 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700752 if (user_pages == NULL)
753 return -ENOMEM;
754
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100755 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700756 down_read(&mm->mmap_sem);
757 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
758 num_pages, 0, 0, user_pages, NULL);
759 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100760 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700761 if (pinned_pages < num_pages) {
762 ret = -EFAULT;
763 goto out_unpin_pages;
764 }
765
Eric Anholt3de09aa2009-03-09 09:42:23 -0700766 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
767 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100768 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700769
Daniel Vetter23010e42010-03-08 13:35:02 +0100770 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700771 offset = obj_priv->gtt_offset + args->offset;
772
773 while (remain > 0) {
774 /* Operation in this page
775 *
776 * gtt_page_base = page offset within aperture
777 * gtt_page_offset = offset within page in aperture
778 * data_page_index = page number in get_user_pages return
779 * data_page_offset = offset with data_page_index page.
780 * page_length = bytes to copy for this page
781 */
782 gtt_page_base = offset & PAGE_MASK;
783 gtt_page_offset = offset & ~PAGE_MASK;
784 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
785 data_page_offset = data_ptr & ~PAGE_MASK;
786
787 page_length = remain;
788 if ((gtt_page_offset + page_length) > PAGE_SIZE)
789 page_length = PAGE_SIZE - gtt_page_offset;
790 if ((data_page_offset + page_length) > PAGE_SIZE)
791 page_length = PAGE_SIZE - data_page_offset;
792
Chris Wilsonab34c222010-05-27 14:15:35 +0100793 slow_kernel_write(dev_priv->mm.gtt_mapping,
794 gtt_page_base, gtt_page_offset,
795 user_pages[data_page_index],
796 data_page_offset,
797 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700798
799 remain -= page_length;
800 offset += page_length;
801 data_ptr += page_length;
802 }
803
Eric Anholt3de09aa2009-03-09 09:42:23 -0700804out_unpin_pages:
805 for (i = 0; i < pinned_pages; i++)
806 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700807 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700808
809 return ret;
810}
811
Eric Anholt40123c12009-03-09 13:42:30 -0700812/**
813 * This is the fast shmem pwrite path, which attempts to directly
814 * copy_from_user into the kmapped pages backing the object.
815 */
Eric Anholt673a3942008-07-30 12:06:12 -0700816static int
Eric Anholt40123c12009-03-09 13:42:30 -0700817i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
818 struct drm_i915_gem_pwrite *args,
819 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700820{
Daniel Vetter23010e42010-03-08 13:35:02 +0100821 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700822 ssize_t remain;
823 loff_t offset, page_base;
824 char __user *user_data;
825 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700826
827 user_data = (char __user *) (uintptr_t) args->data_ptr;
828 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700829
Daniel Vetter23010e42010-03-08 13:35:02 +0100830 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700831 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700832 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700833
Eric Anholt40123c12009-03-09 13:42:30 -0700834 while (remain > 0) {
835 /* Operation in this page
836 *
837 * page_base = page offset within aperture
838 * page_offset = offset within page
839 * page_length = bytes to copy for this page
840 */
841 page_base = (offset & ~(PAGE_SIZE-1));
842 page_offset = offset & (PAGE_SIZE-1);
843 page_length = remain;
844 if ((page_offset + remain) > PAGE_SIZE)
845 page_length = PAGE_SIZE - page_offset;
846
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100847 if (fast_shmem_write(obj_priv->pages,
Eric Anholt40123c12009-03-09 13:42:30 -0700848 page_base, page_offset,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100849 user_data, page_length))
850 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700851
852 remain -= page_length;
853 user_data += page_length;
854 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700855 }
856
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100857 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700858}
859
860/**
861 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
862 * the memory and maps it using kmap_atomic for copying.
863 *
864 * This avoids taking mmap_sem for faulting on the user's address while the
865 * struct_mutex is held.
866 */
867static int
868i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
869 struct drm_i915_gem_pwrite *args,
870 struct drm_file *file_priv)
871{
Daniel Vetter23010e42010-03-08 13:35:02 +0100872 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700873 struct mm_struct *mm = current->mm;
874 struct page **user_pages;
875 ssize_t remain;
876 loff_t offset, pinned_pages, i;
877 loff_t first_data_page, last_data_page, num_pages;
878 int shmem_page_index, shmem_page_offset;
879 int data_page_index, data_page_offset;
880 int page_length;
881 int ret;
882 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700883 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700884
885 remain = args->size;
886
887 /* Pin the user pages containing the data. We can't fault while
888 * holding the struct mutex, and all of the pwrite implementations
889 * want to hold it while dereferencing the user data.
890 */
891 first_data_page = data_ptr / PAGE_SIZE;
892 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
893 num_pages = last_data_page - first_data_page + 1;
894
Chris Wilson4f27b752010-10-14 15:26:45 +0100895 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700896 if (user_pages == NULL)
897 return -ENOMEM;
898
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100899 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700900 down_read(&mm->mmap_sem);
901 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
902 num_pages, 0, 0, user_pages, NULL);
903 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100904 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700905 if (pinned_pages < num_pages) {
906 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100907 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700908 }
909
Eric Anholt40123c12009-03-09 13:42:30 -0700910 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100911 if (ret)
912 goto out;
913
914 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700915
Daniel Vetter23010e42010-03-08 13:35:02 +0100916 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700917 offset = args->offset;
918 obj_priv->dirty = 1;
919
920 while (remain > 0) {
921 /* Operation in this page
922 *
923 * shmem_page_index = page number within shmem file
924 * shmem_page_offset = offset within page in shmem file
925 * data_page_index = page number in get_user_pages return
926 * data_page_offset = offset with data_page_index page.
927 * page_length = bytes to copy for this page
928 */
929 shmem_page_index = offset / PAGE_SIZE;
930 shmem_page_offset = offset & ~PAGE_MASK;
931 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
932 data_page_offset = data_ptr & ~PAGE_MASK;
933
934 page_length = remain;
935 if ((shmem_page_offset + page_length) > PAGE_SIZE)
936 page_length = PAGE_SIZE - shmem_page_offset;
937 if ((data_page_offset + page_length) > PAGE_SIZE)
938 page_length = PAGE_SIZE - data_page_offset;
939
Eric Anholt280b7132009-03-12 16:56:27 -0700940 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100941 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700942 shmem_page_offset,
943 user_pages[data_page_index],
944 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100945 page_length,
946 0);
947 } else {
948 slow_shmem_copy(obj_priv->pages[shmem_page_index],
949 shmem_page_offset,
950 user_pages[data_page_index],
951 data_page_offset,
952 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700953 }
Eric Anholt40123c12009-03-09 13:42:30 -0700954
955 remain -= page_length;
956 data_ptr += page_length;
957 offset += page_length;
958 }
959
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100960out:
Eric Anholt40123c12009-03-09 13:42:30 -0700961 for (i = 0; i < pinned_pages; i++)
962 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700963 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700964
965 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700966}
967
968/**
969 * Writes data to the object referenced by handle.
970 *
971 * On error, the contents of the buffer that were to be modified are undefined.
972 */
973int
974i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100975 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700976{
977 struct drm_i915_gem_pwrite *args = data;
978 struct drm_gem_object *obj;
979 struct drm_i915_gem_object *obj_priv;
Chris Wilson51311d02010-11-17 09:10:42 +0000980 int ret;
981
982 if (args->size == 0)
983 return 0;
984
985 if (!access_ok(VERIFY_READ,
986 (char __user *)(uintptr_t)args->data_ptr,
987 args->size))
988 return -EFAULT;
989
990 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
991 args->size);
992 if (ret)
993 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700994
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100995 ret = i915_mutex_lock_interruptible(dev);
996 if (ret)
997 return ret;
998
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100999 obj = drm_gem_object_lookup(dev, file, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001000 if (obj == NULL) {
1001 ret = -ENOENT;
1002 goto unlock;
1003 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001004 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001005
Chris Wilson7dcd2492010-09-26 20:21:44 +01001006 /* Bounds check destination. */
1007 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001008 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001009 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001010 }
1011
Eric Anholt673a3942008-07-30 12:06:12 -07001012 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1013 * it would end up going through the fenced access, and we'll get
1014 * different detiling behavior between reading and writing.
1015 * pread/pwrite currently are reading and writing from the CPU
1016 * perspective, requiring manual detiling by the client.
1017 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001018 if (obj_priv->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001019 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Dave Airlie71acb5e2008-12-30 20:31:46 +10001020 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001021 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001022 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001023 ret = i915_gem_object_pin(obj, 0);
1024 if (ret)
1025 goto out;
1026
1027 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1028 if (ret)
1029 goto out_unpin;
1030
1031 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1032 if (ret == -EFAULT)
1033 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1034
1035out_unpin:
1036 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001037 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001038 ret = i915_gem_object_get_pages_or_evict(obj);
1039 if (ret)
1040 goto out;
1041
1042 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1043 if (ret)
1044 goto out_put;
1045
1046 ret = -EFAULT;
1047 if (!i915_gem_object_needs_bit17_swizzle(obj))
1048 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1049 if (ret == -EFAULT)
1050 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
1051
1052out_put:
1053 i915_gem_object_put_pages(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001054 }
Eric Anholt673a3942008-07-30 12:06:12 -07001055
Chris Wilson35b62a82010-09-26 20:23:38 +01001056out:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001057 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001058unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001059 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001060 return ret;
1061}
1062
1063/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001064 * Called when user space prepares to use an object with the CPU, either
1065 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001066 */
1067int
1068i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1069 struct drm_file *file_priv)
1070{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001071 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001072 struct drm_i915_gem_set_domain *args = data;
1073 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001074 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001075 uint32_t read_domains = args->read_domains;
1076 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001077 int ret;
1078
1079 if (!(dev->driver->driver_features & DRIVER_GEM))
1080 return -ENODEV;
1081
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001082 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001083 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001084 return -EINVAL;
1085
Chris Wilson21d509e2009-06-06 09:46:02 +01001086 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001087 return -EINVAL;
1088
1089 /* Having something in the write domain implies it's in the read
1090 * domain, and only that read domain. Enforce that in the request.
1091 */
1092 if (write_domain != 0 && read_domains != write_domain)
1093 return -EINVAL;
1094
Chris Wilson76c1dec2010-09-25 11:22:51 +01001095 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001096 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001097 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001098
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001099 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1100 if (obj == NULL) {
1101 ret = -ENOENT;
1102 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001103 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001104 obj_priv = to_intel_bo(obj);
Jesse Barnes652c3932009-08-17 13:31:43 -07001105
1106 intel_mark_busy(dev, obj);
1107
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001108 if (read_domains & I915_GEM_DOMAIN_GTT) {
1109 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001110
Eric Anholta09ba7f2009-08-29 12:49:51 -07001111 /* Update the LRU on the fence for the CPU access that's
1112 * about to occur.
1113 */
1114 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001115 struct drm_i915_fence_reg *reg =
1116 &dev_priv->fence_regs[obj_priv->fence_reg];
1117 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001118 &dev_priv->mm.fence_list);
1119 }
1120
Eric Anholt02354392008-11-26 13:58:13 -08001121 /* Silently promote "you're not bound, there was nothing to do"
1122 * to success, since the client was just asking us to
1123 * make sure everything was done.
1124 */
1125 if (ret == -EINVAL)
1126 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001127 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001128 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001129 }
1130
Chris Wilson7d1c4802010-08-07 21:45:03 +01001131 /* Maintain LRU order of "inactive" objects */
1132 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001133 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001134
Eric Anholt673a3942008-07-30 12:06:12 -07001135 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001136unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001137 mutex_unlock(&dev->struct_mutex);
1138 return ret;
1139}
1140
1141/**
1142 * Called when user space has done writes to this buffer
1143 */
1144int
1145i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1146 struct drm_file *file_priv)
1147{
1148 struct drm_i915_gem_sw_finish *args = data;
1149 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001150 int ret = 0;
1151
1152 if (!(dev->driver->driver_features & DRIVER_GEM))
1153 return -ENODEV;
1154
Chris Wilson76c1dec2010-09-25 11:22:51 +01001155 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001156 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001157 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001158
Eric Anholt673a3942008-07-30 12:06:12 -07001159 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1160 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001161 ret = -ENOENT;
1162 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001163 }
1164
Eric Anholt673a3942008-07-30 12:06:12 -07001165 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001166 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001167 i915_gem_object_flush_cpu_write_domain(obj);
1168
Eric Anholt673a3942008-07-30 12:06:12 -07001169 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001170unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001171 mutex_unlock(&dev->struct_mutex);
1172 return ret;
1173}
1174
1175/**
1176 * Maps the contents of an object, returning the address it is mapped
1177 * into.
1178 *
1179 * While the mapping holds a reference on the contents of the object, it doesn't
1180 * imply a ref on the object itself.
1181 */
1182int
1183i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1184 struct drm_file *file_priv)
1185{
1186 struct drm_i915_gem_mmap *args = data;
1187 struct drm_gem_object *obj;
1188 loff_t offset;
1189 unsigned long addr;
1190
1191 if (!(dev->driver->driver_features & DRIVER_GEM))
1192 return -ENODEV;
1193
1194 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1195 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001196 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001197
1198 offset = args->offset;
1199
1200 down_write(&current->mm->mmap_sem);
1201 addr = do_mmap(obj->filp, 0, args->size,
1202 PROT_READ | PROT_WRITE, MAP_SHARED,
1203 args->offset);
1204 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001205 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001206 if (IS_ERR((void *)addr))
1207 return addr;
1208
1209 args->addr_ptr = (uint64_t) addr;
1210
1211 return 0;
1212}
1213
Jesse Barnesde151cf2008-11-12 10:03:55 -08001214/**
1215 * i915_gem_fault - fault a page into the GTT
1216 * vma: VMA in question
1217 * vmf: fault info
1218 *
1219 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1220 * from userspace. The fault handler takes care of binding the object to
1221 * the GTT (if needed), allocating and programming a fence register (again,
1222 * only if needed based on whether the old reg is still valid or the object
1223 * is tiled) and inserting a new PTE into the faulting process.
1224 *
1225 * Note that the faulting process may involve evicting existing objects
1226 * from the GTT and/or fence registers to make room. So performance may
1227 * suffer if the GTT working set is large or there are few fence registers
1228 * left.
1229 */
1230int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1231{
1232 struct drm_gem_object *obj = vma->vm_private_data;
1233 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001234 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001235 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001236 pgoff_t page_offset;
1237 unsigned long pfn;
1238 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001239 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001240
1241 /* We don't use vmf->pgoff since that has the fake offset */
1242 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1243 PAGE_SHIFT;
1244
1245 /* Now bind it into the GTT if needed */
1246 mutex_lock(&dev->struct_mutex);
1247 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001248 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001249 if (ret)
1250 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001251
Jesse Barnesde151cf2008-11-12 10:03:55 -08001252 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001253 if (ret)
1254 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001255 }
1256
1257 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001258 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001259 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001260 if (ret)
1261 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001262 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263
Chris Wilson7d1c4802010-08-07 21:45:03 +01001264 if (i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001265 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001266
Jesse Barnesde151cf2008-11-12 10:03:55 -08001267 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1268 page_offset;
1269
1270 /* Finally, remap it using the new GTT offset */
1271 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001272unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001273 mutex_unlock(&dev->struct_mutex);
1274
1275 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001276 case 0:
1277 case -ERESTARTSYS:
1278 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001279 case -ENOMEM:
1280 case -EAGAIN:
1281 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001282 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001283 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001284 }
1285}
1286
1287/**
1288 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1289 * @obj: obj in question
1290 *
1291 * GEM memory mapping works by handing back to userspace a fake mmap offset
1292 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1293 * up the object based on the offset and sets up the various memory mapping
1294 * structures.
1295 *
1296 * This routine allocates and attaches a fake offset for @obj.
1297 */
1298static int
1299i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1300{
1301 struct drm_device *dev = obj->dev;
1302 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001303 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001304 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001305 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001306 int ret = 0;
1307
1308 /* Set the object up for mmap'ing */
1309 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001310 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001311 if (!list->map)
1312 return -ENOMEM;
1313
1314 map = list->map;
1315 map->type = _DRM_GEM;
1316 map->size = obj->size;
1317 map->handle = obj;
1318
1319 /* Get a DRM GEM mmap offset allocated... */
1320 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1321 obj->size / PAGE_SIZE, 0, 0);
1322 if (!list->file_offset_node) {
1323 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001324 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001325 goto out_free_list;
1326 }
1327
1328 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1329 obj->size / PAGE_SIZE, 0);
1330 if (!list->file_offset_node) {
1331 ret = -ENOMEM;
1332 goto out_free_list;
1333 }
1334
1335 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001336 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1337 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001338 DRM_ERROR("failed to add to map hash\n");
1339 goto out_free_mm;
1340 }
1341
1342 /* By now we should be all set, any drm_mmap request on the offset
1343 * below will get to our mmap & fault handler */
1344 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1345
1346 return 0;
1347
1348out_free_mm:
1349 drm_mm_put_block(list->file_offset_node);
1350out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001351 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001352
1353 return ret;
1354}
1355
Chris Wilson901782b2009-07-10 08:18:50 +01001356/**
1357 * i915_gem_release_mmap - remove physical page mappings
1358 * @obj: obj in question
1359 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001360 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001361 * relinquish ownership of the pages back to the system.
1362 *
1363 * It is vital that we remove the page mapping if we have mapped a tiled
1364 * object through the GTT and then lose the fence register due to
1365 * resource pressure. Similarly if the object has been moved out of the
1366 * aperture, than pages mapped into userspace must be revoked. Removing the
1367 * mapping will then trigger a page fault on the next user access, allowing
1368 * fixup by i915_gem_fault().
1369 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001370void
Chris Wilson901782b2009-07-10 08:18:50 +01001371i915_gem_release_mmap(struct drm_gem_object *obj)
1372{
1373 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001374 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001375
1376 if (dev->dev_mapping)
1377 unmap_mapping_range(dev->dev_mapping,
1378 obj_priv->mmap_offset, obj->size, 1);
1379}
1380
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001381static void
1382i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1383{
1384 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001385 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001386 struct drm_gem_mm *mm = dev->mm_private;
1387 struct drm_map_list *list;
1388
1389 list = &obj->map_list;
1390 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1391
1392 if (list->file_offset_node) {
1393 drm_mm_put_block(list->file_offset_node);
1394 list->file_offset_node = NULL;
1395 }
1396
1397 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001398 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001399 list->map = NULL;
1400 }
1401
1402 obj_priv->mmap_offset = 0;
1403}
1404
Jesse Barnesde151cf2008-11-12 10:03:55 -08001405/**
1406 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1407 * @obj: object to check
1408 *
1409 * Return the required GTT alignment for an object, taking into account
1410 * potential fence register mapping if needed.
1411 */
1412static uint32_t
1413i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1414{
1415 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001416 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001417 int start, i;
1418
1419 /*
1420 * Minimum alignment is 4k (GTT page size), but might be greater
1421 * if a fence register is needed for the object.
1422 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001423 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001424 return 4096;
1425
1426 /*
1427 * Previous chips need to be aligned to the size of the smallest
1428 * fence register that can contain the object.
1429 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001430 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001431 start = 1024*1024;
1432 else
1433 start = 512*1024;
1434
1435 for (i = start; i < obj->size; i <<= 1)
1436 ;
1437
1438 return i;
1439}
1440
1441/**
1442 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1443 * @dev: DRM device
1444 * @data: GTT mapping ioctl data
1445 * @file_priv: GEM object info
1446 *
1447 * Simply returns the fake offset to userspace so it can mmap it.
1448 * The mmap call will end up in drm_gem_mmap(), which will set things
1449 * up so we can get faults in the handler above.
1450 *
1451 * The fault handler will take care of binding the object into the GTT
1452 * (since it may have been evicted to make room for something), allocating
1453 * a fence register, and mapping the appropriate aperture address into
1454 * userspace.
1455 */
1456int
1457i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1458 struct drm_file *file_priv)
1459{
1460 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001461 struct drm_gem_object *obj;
1462 struct drm_i915_gem_object *obj_priv;
1463 int ret;
1464
1465 if (!(dev->driver->driver_features & DRIVER_GEM))
1466 return -ENODEV;
1467
Chris Wilson76c1dec2010-09-25 11:22:51 +01001468 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001469 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001470 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001471
Jesse Barnesde151cf2008-11-12 10:03:55 -08001472 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001473 if (obj == NULL) {
1474 ret = -ENOENT;
1475 goto unlock;
1476 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001477 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001478
Chris Wilsonab182822009-09-22 18:46:17 +01001479 if (obj_priv->madv != I915_MADV_WILLNEED) {
1480 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001481 ret = -EINVAL;
1482 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001483 }
1484
Jesse Barnesde151cf2008-11-12 10:03:55 -08001485 if (!obj_priv->mmap_offset) {
1486 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001487 if (ret)
1488 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001489 }
1490
1491 args->offset = obj_priv->mmap_offset;
1492
Jesse Barnesde151cf2008-11-12 10:03:55 -08001493 /*
1494 * Pull it into the GTT so that we have a page list (makes the
1495 * initial fault faster and any subsequent flushing possible).
1496 */
1497 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001498 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001499 if (ret)
1500 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001501 }
1502
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001503out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001504 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001505unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001506 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001507 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001508}
1509
Chris Wilson5cdf5882010-09-27 15:51:07 +01001510static void
Eric Anholt856fa192009-03-19 14:10:50 -07001511i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001512{
Daniel Vetter23010e42010-03-08 13:35:02 +01001513 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001514 int page_count = obj->size / PAGE_SIZE;
1515 int i;
1516
Eric Anholt856fa192009-03-19 14:10:50 -07001517 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001518 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001519
1520 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001521 return;
1522
Eric Anholt280b7132009-03-12 16:56:27 -07001523 if (obj_priv->tiling_mode != I915_TILING_NONE)
1524 i915_gem_object_save_bit_17_swizzle(obj);
1525
Chris Wilson3ef94da2009-09-14 16:50:29 +01001526 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001527 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001528
1529 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001530 if (obj_priv->dirty)
1531 set_page_dirty(obj_priv->pages[i]);
1532
1533 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001534 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001535
1536 page_cache_release(obj_priv->pages[i]);
1537 }
Eric Anholt673a3942008-07-30 12:06:12 -07001538 obj_priv->dirty = 0;
1539
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001540 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001541 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001542}
1543
Chris Wilsona56ba562010-09-28 10:07:56 +01001544static uint32_t
1545i915_gem_next_request_seqno(struct drm_device *dev,
1546 struct intel_ring_buffer *ring)
1547{
1548 drm_i915_private_t *dev_priv = dev->dev_private;
1549
1550 ring->outstanding_lazy_request = true;
1551 return dev_priv->next_seqno;
1552}
1553
Eric Anholt673a3942008-07-30 12:06:12 -07001554static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001555i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001556 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001557{
1558 struct drm_device *dev = obj->dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001559 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001560 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001561 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001562
Zou Nan hai852835f2010-05-21 09:08:56 +08001563 BUG_ON(ring == NULL);
1564 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001565
1566 /* Add a reference if we're newly entering the active list. */
1567 if (!obj_priv->active) {
1568 drm_gem_object_reference(obj);
1569 obj_priv->active = 1;
1570 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001571
Eric Anholt673a3942008-07-30 12:06:12 -07001572 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson69dc4982010-10-19 10:36:51 +01001573 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
1574 list_move_tail(&obj_priv->ring_list, &ring->active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001575 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001576}
1577
Eric Anholtce44b0e2008-11-06 16:00:31 -08001578static void
1579i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1580{
1581 struct drm_device *dev = obj->dev;
1582 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001583 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001584
1585 BUG_ON(!obj_priv->active);
Chris Wilson69dc4982010-10-19 10:36:51 +01001586 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
1587 list_del_init(&obj_priv->ring_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001588 obj_priv->last_rendering_seqno = 0;
1589}
Eric Anholt673a3942008-07-30 12:06:12 -07001590
Chris Wilson963b4832009-09-20 23:03:54 +01001591/* Immediately discard the backing storage */
1592static void
1593i915_gem_object_truncate(struct drm_gem_object *obj)
1594{
Daniel Vetter23010e42010-03-08 13:35:02 +01001595 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001596 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001597
Chris Wilsonae9fed62010-08-07 11:01:30 +01001598 /* Our goal here is to return as much of the memory as
1599 * is possible back to the system as we are called from OOM.
1600 * To do this we must instruct the shmfs to drop all of its
1601 * backing pages, *now*. Here we mirror the actions taken
1602 * when by shmem_delete_inode() to release the backing store.
1603 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001604 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001605 truncate_inode_pages(inode->i_mapping, 0);
1606 if (inode->i_op->truncate_range)
1607 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001608
1609 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001610}
1611
1612static inline int
1613i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1614{
1615 return obj_priv->madv == I915_MADV_DONTNEED;
1616}
1617
Eric Anholt673a3942008-07-30 12:06:12 -07001618static void
1619i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1620{
1621 struct drm_device *dev = obj->dev;
1622 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001623 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001624
Eric Anholt673a3942008-07-30 12:06:12 -07001625 if (obj_priv->pin_count != 0)
Chris Wilson69dc4982010-10-19 10:36:51 +01001626 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001627 else
Chris Wilson69dc4982010-10-19 10:36:51 +01001628 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1629 list_del_init(&obj_priv->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001630
Daniel Vetter99fcb762010-02-07 16:20:18 +01001631 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1632
Eric Anholtce44b0e2008-11-06 16:00:31 -08001633 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001634 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001635 if (obj_priv->active) {
1636 obj_priv->active = 0;
1637 drm_gem_object_unreference(obj);
1638 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001639 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001640}
1641
Daniel Vetter63560392010-02-19 11:51:59 +01001642static void
1643i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001644 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001645 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001646{
1647 drm_i915_private_t *dev_priv = dev->dev_private;
1648 struct drm_i915_gem_object *obj_priv, *next;
1649
1650 list_for_each_entry_safe(obj_priv, next,
Chris Wilson64193402010-10-24 12:38:05 +01001651 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001652 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001653 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001654
Chris Wilson64193402010-10-24 12:38:05 +01001655 if (obj->write_domain & flush_domains) {
Daniel Vetter63560392010-02-19 11:51:59 +01001656 uint32_t old_write_domain = obj->write_domain;
1657
1658 obj->write_domain = 0;
1659 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001660 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001661
1662 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001663 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1664 struct drm_i915_fence_reg *reg =
1665 &dev_priv->fence_regs[obj_priv->fence_reg];
1666 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001667 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001668 }
Daniel Vetter63560392010-02-19 11:51:59 +01001669
1670 trace_i915_gem_object_change_domain(obj,
1671 obj->read_domains,
1672 old_write_domain);
1673 }
1674 }
1675}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001676
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001677uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001678i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001679 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001680 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001681 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001682{
1683 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001684 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001685 uint32_t seqno;
1686 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001687
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001688 if (file != NULL)
1689 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001690
Chris Wilson8dc5d142010-08-12 12:36:12 +01001691 if (request == NULL) {
1692 request = kzalloc(sizeof(*request), GFP_KERNEL);
1693 if (request == NULL)
1694 return 0;
1695 }
Eric Anholt673a3942008-07-30 12:06:12 -07001696
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001697 seqno = ring->add_request(dev, ring, 0);
Chris Wilsona56ba562010-09-28 10:07:56 +01001698 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001699
1700 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001701 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001702 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001703 was_empty = list_empty(&ring->request_list);
1704 list_add_tail(&request->list, &ring->request_list);
1705
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001706 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001707 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001708 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001709 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001710 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001711 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001712 }
Eric Anholt673a3942008-07-30 12:06:12 -07001713
Ben Gamarif65d9422009-09-14 17:48:44 -04001714 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001715 mod_timer(&dev_priv->hangcheck_timer,
1716 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001717 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001718 queue_delayed_work(dev_priv->wq,
1719 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001720 }
Eric Anholt673a3942008-07-30 12:06:12 -07001721 return seqno;
1722}
1723
1724/**
1725 * Command execution barrier
1726 *
1727 * Ensures that all commands in the ring are finished
1728 * before signalling the CPU
1729 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001730static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001731i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001732{
Eric Anholt673a3942008-07-30 12:06:12 -07001733 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001734
1735 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001736 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001737 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001738
1739 ring->flush(dev, ring,
1740 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001741}
1742
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001743static inline void
1744i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001745{
Chris Wilson1c255952010-09-26 11:03:27 +01001746 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001747
Chris Wilson1c255952010-09-26 11:03:27 +01001748 if (!file_priv)
1749 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001750
Chris Wilson1c255952010-09-26 11:03:27 +01001751 spin_lock(&file_priv->mm.lock);
1752 list_del(&request->client_list);
1753 request->file_priv = NULL;
1754 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001755}
1756
Chris Wilsondfaae392010-09-22 10:31:52 +01001757static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1758 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001759{
Chris Wilsondfaae392010-09-22 10:31:52 +01001760 while (!list_empty(&ring->request_list)) {
1761 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001762
Chris Wilsondfaae392010-09-22 10:31:52 +01001763 request = list_first_entry(&ring->request_list,
1764 struct drm_i915_gem_request,
1765 list);
1766
1767 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001768 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001769 kfree(request);
1770 }
1771
1772 while (!list_empty(&ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001773 struct drm_i915_gem_object *obj_priv;
1774
Chris Wilsondfaae392010-09-22 10:31:52 +01001775 obj_priv = list_first_entry(&ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001776 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001777 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001778
Chris Wilsondfaae392010-09-22 10:31:52 +01001779 obj_priv->base.write_domain = 0;
1780 list_del_init(&obj_priv->gpu_write_list);
1781 i915_gem_object_move_to_inactive(&obj_priv->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001782 }
Eric Anholt673a3942008-07-30 12:06:12 -07001783}
1784
Chris Wilson069efc12010-09-30 16:53:18 +01001785void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001786{
Chris Wilsondfaae392010-09-22 10:31:52 +01001787 struct drm_i915_private *dev_priv = dev->dev_private;
1788 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001789 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001790
Chris Wilsondfaae392010-09-22 10:31:52 +01001791 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001792 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001793 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001794
1795 /* Remove anything from the flushing lists. The GPU cache is likely
1796 * to be lost on reset along with the data, so simply move the
1797 * lost bo to the inactive list.
1798 */
1799 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001800 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1801 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001802 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001803
1804 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001805 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001806 i915_gem_object_move_to_inactive(&obj_priv->base);
1807 }
Chris Wilson9375e442010-09-19 12:21:28 +01001808
Chris Wilsondfaae392010-09-22 10:31:52 +01001809 /* Move everything out of the GPU domains to ensure we do any
1810 * necessary invalidation upon reuse.
1811 */
Chris Wilson77f01232010-09-19 12:31:36 +01001812 list_for_each_entry(obj_priv,
1813 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001814 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001815 {
1816 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1817 }
Chris Wilson069efc12010-09-30 16:53:18 +01001818
1819 /* The fence registers are invalidated so clear them out */
1820 for (i = 0; i < 16; i++) {
1821 struct drm_i915_fence_reg *reg;
1822
1823 reg = &dev_priv->fence_regs[i];
1824 if (!reg->obj)
1825 continue;
1826
1827 i915_gem_clear_fence_reg(reg->obj);
1828 }
Eric Anholt673a3942008-07-30 12:06:12 -07001829}
1830
1831/**
1832 * This function clears the request list as sequence numbers are passed.
1833 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001834static void
1835i915_gem_retire_requests_ring(struct drm_device *dev,
1836 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001837{
1838 drm_i915_private_t *dev_priv = dev->dev_private;
1839 uint32_t seqno;
1840
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001841 if (!ring->status_page.page_addr ||
1842 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001843 return;
1844
Chris Wilson23bc5982010-09-29 16:10:57 +01001845 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001846
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001847 seqno = ring->get_seqno(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001848 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001849 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001850
Zou Nan hai852835f2010-05-21 09:08:56 +08001851 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001852 struct drm_i915_gem_request,
1853 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001854
Chris Wilsondfaae392010-09-22 10:31:52 +01001855 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001856 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001857
1858 trace_i915_gem_request_retire(dev, request->seqno);
1859
1860 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001861 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001862 kfree(request);
1863 }
1864
1865 /* Move any buffers on the active list that are no longer referenced
1866 * by the ringbuffer to the flushing/inactive lists as appropriate.
1867 */
1868 while (!list_empty(&ring->active_list)) {
1869 struct drm_gem_object *obj;
1870 struct drm_i915_gem_object *obj_priv;
1871
1872 obj_priv = list_first_entry(&ring->active_list,
1873 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001874 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001875
Chris Wilsondfaae392010-09-22 10:31:52 +01001876 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001877 break;
1878
1879 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001880 if (obj->write_domain != 0)
1881 i915_gem_object_move_to_flushing(obj);
1882 else
1883 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001884 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001885
1886 if (unlikely (dev_priv->trace_irq_seqno &&
1887 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001888 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001889 dev_priv->trace_irq_seqno = 0;
1890 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001891
1892 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001893}
1894
1895void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001896i915_gem_retire_requests(struct drm_device *dev)
1897{
1898 drm_i915_private_t *dev_priv = dev->dev_private;
1899
Chris Wilsonbe726152010-07-23 23:18:50 +01001900 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1901 struct drm_i915_gem_object *obj_priv, *tmp;
1902
1903 /* We must be careful that during unbind() we do not
1904 * accidentally infinitely recurse into retire requests.
1905 * Currently:
1906 * retire -> free -> unbind -> wait -> retire_ring
1907 */
1908 list_for_each_entry_safe(obj_priv, tmp,
1909 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001910 mm_list)
Chris Wilsonbe726152010-07-23 23:18:50 +01001911 i915_gem_free_object_tail(&obj_priv->base);
1912 }
1913
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001914 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001915 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001916 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001917}
1918
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001919static void
Eric Anholt673a3942008-07-30 12:06:12 -07001920i915_gem_retire_work_handler(struct work_struct *work)
1921{
1922 drm_i915_private_t *dev_priv;
1923 struct drm_device *dev;
1924
1925 dev_priv = container_of(work, drm_i915_private_t,
1926 mm.retire_work.work);
1927 dev = dev_priv->dev;
1928
Chris Wilson891b48c2010-09-29 12:26:37 +01001929 /* Come back later if the device is busy... */
1930 if (!mutex_trylock(&dev->struct_mutex)) {
1931 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1932 return;
1933 }
1934
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001935 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001936
Keith Packard6dbe2772008-10-14 21:41:13 -07001937 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001938 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01001939 !list_empty(&dev_priv->bsd_ring.request_list) ||
1940 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001941 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001942 mutex_unlock(&dev->struct_mutex);
1943}
1944
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001945int
Zou Nan hai852835f2010-05-21 09:08:56 +08001946i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001947 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001948{
1949 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001950 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001951 int ret = 0;
1952
1953 BUG_ON(seqno == 0);
1954
Ben Gamariba1234d2009-09-14 17:48:47 -04001955 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001956 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04001957
Chris Wilsona56ba562010-09-28 10:07:56 +01001958 if (ring->outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01001959 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001960 if (seqno == 0)
1961 return -ENOMEM;
1962 }
Chris Wilsona56ba562010-09-28 10:07:56 +01001963 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001964
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001965 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001966 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001967 ier = I915_READ(DEIER) | I915_READ(GTIER);
1968 else
1969 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001970 if (!ier) {
1971 DRM_ERROR("something (likely vbetool) disabled "
1972 "interrupts, re-enabling\n");
1973 i915_driver_irq_preinstall(dev);
1974 i915_driver_irq_postinstall(dev);
1975 }
1976
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001977 trace_i915_gem_request_wait_begin(dev, seqno);
1978
Zou Nan hai852835f2010-05-21 09:08:56 +08001979 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001980 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001981 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001982 ret = wait_event_interruptible(ring->irq_queue,
1983 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001984 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001985 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001986 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001987 wait_event(ring->irq_queue,
1988 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001989 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001990 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001991
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001992 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001993 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001994
1995 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001996 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001997 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001998 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07001999
2000 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002001 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002002 __func__, ret, seqno, ring->get_seqno(dev, ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002003 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002004
2005 /* Directly dispatch request retiring. While we have the work queue
2006 * to handle this, the waiter on a request often wants an associated
2007 * buffer to have made it to the inactive list, and we would need
2008 * a separate wait queue to handle that.
2009 */
2010 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002011 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002012
2013 return ret;
2014}
2015
Daniel Vetter48764bf2009-09-15 22:57:32 +02002016/**
2017 * Waits for a sequence number to be signaled, and cleans up the
2018 * request and object lists appropriately for that event.
2019 */
2020static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002021i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002022 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002023{
Zou Nan hai852835f2010-05-21 09:08:56 +08002024 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002025}
2026
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002027static void
Chris Wilson92204342010-09-18 11:02:01 +01002028i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002029 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002030 struct intel_ring_buffer *ring,
2031 uint32_t invalidate_domains,
2032 uint32_t flush_domains)
2033{
2034 ring->flush(dev, ring, invalidate_domains, flush_domains);
2035 i915_gem_process_flushing_list(dev, flush_domains, ring);
2036}
2037
2038static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002039i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002040 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002041 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002042 uint32_t flush_domains,
2043 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002044{
2045 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002046
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002047 if (flush_domains & I915_GEM_DOMAIN_CPU)
2048 drm_agp_chipset_flush(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002049
Chris Wilson92204342010-09-18 11:02:01 +01002050 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2051 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002052 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002053 &dev_priv->render_ring,
2054 invalidate_domains, flush_domains);
2055 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002056 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002057 &dev_priv->bsd_ring,
2058 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002059 if (flush_rings & RING_BLT)
2060 i915_gem_flush_ring(dev, file_priv,
2061 &dev_priv->blt_ring,
2062 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002063 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002064}
2065
Eric Anholt673a3942008-07-30 12:06:12 -07002066/**
2067 * Ensures that all rendering to the object has completed and the object is
2068 * safe to unbind from the GTT or access from the CPU.
2069 */
2070static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002071i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2072 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002073{
2074 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002075 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002076 int ret;
2077
Eric Anholte47c68e2008-11-14 13:35:19 -08002078 /* This function only exists to support waiting for existing rendering,
2079 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002080 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002081 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002082
2083 /* If there is rendering queued on the buffer being evicted, wait for
2084 * it.
2085 */
2086 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002087 ret = i915_do_wait_request(dev,
2088 obj_priv->last_rendering_seqno,
2089 interruptible,
2090 obj_priv->ring);
2091 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002092 return ret;
2093 }
2094
2095 return 0;
2096}
2097
2098/**
2099 * Unbinds an object from the GTT aperture.
2100 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002101int
Eric Anholt673a3942008-07-30 12:06:12 -07002102i915_gem_object_unbind(struct drm_gem_object *obj)
2103{
2104 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002105 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002106 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002107 int ret = 0;
2108
Eric Anholt673a3942008-07-30 12:06:12 -07002109 if (obj_priv->gtt_space == NULL)
2110 return 0;
2111
2112 if (obj_priv->pin_count != 0) {
2113 DRM_ERROR("Attempting to unbind pinned buffer\n");
2114 return -EINVAL;
2115 }
2116
Eric Anholt5323fd02009-09-09 11:50:45 -07002117 /* blow away mappings if mapped through GTT */
2118 i915_gem_release_mmap(obj);
2119
Eric Anholt673a3942008-07-30 12:06:12 -07002120 /* Move the object to the CPU domain to ensure that
2121 * any possible CPU writes while it's not in the GTT
2122 * are flushed when we go to remap it. This will
2123 * also ensure that all pending GPU writes are finished
2124 * before we unbind.
2125 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002126 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002127 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002128 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002129 /* Continue on if we fail due to EIO, the GPU is hung so we
2130 * should be safe and we need to cleanup or else we might
2131 * cause memory corruption through use-after-free.
2132 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002133 if (ret) {
2134 i915_gem_clflush_object(obj);
2135 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2136 }
Eric Anholt673a3942008-07-30 12:06:12 -07002137
Daniel Vetter96b47b62009-12-15 17:50:00 +01002138 /* release the fence reg _after_ flushing */
2139 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2140 i915_gem_clear_fence_reg(obj);
2141
Chris Wilson73aa8082010-09-30 11:46:12 +01002142 drm_unbind_agp(obj_priv->agp_mem);
2143 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002144
Eric Anholt856fa192009-03-19 14:10:50 -07002145 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002146 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002147
Chris Wilson73aa8082010-09-30 11:46:12 +01002148 i915_gem_info_remove_gtt(dev_priv, obj->size);
Chris Wilson69dc4982010-10-19 10:36:51 +01002149 list_del_init(&obj_priv->mm_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002150
Chris Wilson73aa8082010-09-30 11:46:12 +01002151 drm_mm_put_block(obj_priv->gtt_space);
2152 obj_priv->gtt_space = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01002153 obj_priv->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002154
Chris Wilson963b4832009-09-20 23:03:54 +01002155 if (i915_gem_object_is_purgeable(obj_priv))
2156 i915_gem_object_truncate(obj);
2157
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002158 trace_i915_gem_object_unbind(obj);
2159
Chris Wilson8dc17752010-07-23 23:18:51 +01002160 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002161}
2162
Chris Wilsona56ba562010-09-28 10:07:56 +01002163static int i915_ring_idle(struct drm_device *dev,
2164 struct intel_ring_buffer *ring)
2165{
Chris Wilson395b70b2010-10-28 21:28:46 +01002166 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002167 return 0;
2168
Chris Wilsona56ba562010-09-28 10:07:56 +01002169 i915_gem_flush_ring(dev, NULL, ring,
2170 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2171 return i915_wait_request(dev,
2172 i915_gem_next_request_seqno(dev, ring),
2173 ring);
2174}
2175
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002176int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002177i915_gpu_idle(struct drm_device *dev)
2178{
2179 drm_i915_private_t *dev_priv = dev->dev_private;
2180 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002181 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002182
Zou Nan haid1b851f2010-05-21 09:08:57 +08002183 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002184 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002185 if (lists_empty)
2186 return 0;
2187
2188 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002189 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002190 if (ret)
2191 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002192
Chris Wilson87acb0a2010-10-19 10:13:00 +01002193 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2194 if (ret)
2195 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002196
Chris Wilson549f7362010-10-19 11:19:32 +01002197 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2198 if (ret)
2199 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002200
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002201 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002202}
2203
Chris Wilson5cdf5882010-09-27 15:51:07 +01002204static int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002205i915_gem_object_get_pages(struct drm_gem_object *obj,
2206 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002207{
Daniel Vetter23010e42010-03-08 13:35:02 +01002208 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002209 int page_count, i;
2210 struct address_space *mapping;
2211 struct inode *inode;
2212 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002213
Daniel Vetter778c3542010-05-13 11:49:44 +02002214 BUG_ON(obj_priv->pages_refcount
2215 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2216
Eric Anholt856fa192009-03-19 14:10:50 -07002217 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002218 return 0;
2219
2220 /* Get the list of pages out of our struct file. They'll be pinned
2221 * at this point until we release them.
2222 */
2223 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002224 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002225 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002226 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002227 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002228 return -ENOMEM;
2229 }
2230
2231 inode = obj->filp->f_path.dentry->d_inode;
2232 mapping = inode->i_mapping;
2233 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002234 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002235 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002236 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002237 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002238 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002239 if (IS_ERR(page))
2240 goto err_pages;
2241
Eric Anholt856fa192009-03-19 14:10:50 -07002242 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002243 }
Eric Anholt280b7132009-03-12 16:56:27 -07002244
2245 if (obj_priv->tiling_mode != I915_TILING_NONE)
2246 i915_gem_object_do_bit_17_swizzle(obj);
2247
Eric Anholt673a3942008-07-30 12:06:12 -07002248 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002249
2250err_pages:
2251 while (i--)
2252 page_cache_release(obj_priv->pages[i]);
2253
2254 drm_free_large(obj_priv->pages);
2255 obj_priv->pages = NULL;
2256 obj_priv->pages_refcount--;
2257 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002258}
2259
Eric Anholt4e901fd2009-10-26 16:44:17 -07002260static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2261{
2262 struct drm_gem_object *obj = reg->obj;
2263 struct drm_device *dev = obj->dev;
2264 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002265 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002266 int regnum = obj_priv->fence_reg;
2267 uint64_t val;
2268
2269 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2270 0xfffff000) << 32;
2271 val |= obj_priv->gtt_offset & 0xfffff000;
2272 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2273 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2274
2275 if (obj_priv->tiling_mode == I915_TILING_Y)
2276 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2277 val |= I965_FENCE_REG_VALID;
2278
2279 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2280}
2281
Jesse Barnesde151cf2008-11-12 10:03:55 -08002282static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2283{
2284 struct drm_gem_object *obj = reg->obj;
2285 struct drm_device *dev = obj->dev;
2286 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002287 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002288 int regnum = obj_priv->fence_reg;
2289 uint64_t val;
2290
2291 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2292 0xfffff000) << 32;
2293 val |= obj_priv->gtt_offset & 0xfffff000;
2294 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2295 if (obj_priv->tiling_mode == I915_TILING_Y)
2296 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2297 val |= I965_FENCE_REG_VALID;
2298
2299 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2300}
2301
2302static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2303{
2304 struct drm_gem_object *obj = reg->obj;
2305 struct drm_device *dev = obj->dev;
2306 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002307 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002309 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002310 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002311 uint32_t pitch_val;
2312
2313 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2314 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002315 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002316 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317 return;
2318 }
2319
Jesse Barnes0f973f22009-01-26 17:10:45 -08002320 if (obj_priv->tiling_mode == I915_TILING_Y &&
2321 HAS_128_BYTE_Y_TILING(dev))
2322 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002323 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002324 tile_width = 512;
2325
2326 /* Note: pitch better be a power of two tile widths */
2327 pitch_val = obj_priv->stride / tile_width;
2328 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002329
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002330 if (obj_priv->tiling_mode == I915_TILING_Y &&
2331 HAS_128_BYTE_Y_TILING(dev))
2332 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2333 else
2334 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2335
Jesse Barnesde151cf2008-11-12 10:03:55 -08002336 val = obj_priv->gtt_offset;
2337 if (obj_priv->tiling_mode == I915_TILING_Y)
2338 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2339 val |= I915_FENCE_SIZE_BITS(obj->size);
2340 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2341 val |= I830_FENCE_REG_VALID;
2342
Eric Anholtdc529a42009-03-10 22:34:49 -07002343 if (regnum < 8)
2344 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2345 else
2346 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2347 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002348}
2349
2350static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2351{
2352 struct drm_gem_object *obj = reg->obj;
2353 struct drm_device *dev = obj->dev;
2354 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002355 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356 int regnum = obj_priv->fence_reg;
2357 uint32_t val;
2358 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002359 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002360
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002361 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002363 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002364 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002365 return;
2366 }
2367
Eric Anholte76a16d2009-05-26 17:44:56 -07002368 pitch_val = obj_priv->stride / 128;
2369 pitch_val = ffs(pitch_val) - 1;
2370 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2371
Jesse Barnesde151cf2008-11-12 10:03:55 -08002372 val = obj_priv->gtt_offset;
2373 if (obj_priv->tiling_mode == I915_TILING_Y)
2374 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002375 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2376 WARN_ON(fence_size_bits & ~0x00000f00);
2377 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002378 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2379 val |= I830_FENCE_REG_VALID;
2380
2381 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002382}
2383
Chris Wilson2cf34d72010-09-14 13:03:28 +01002384static int i915_find_fence_reg(struct drm_device *dev,
2385 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002386{
2387 struct drm_i915_fence_reg *reg = NULL;
2388 struct drm_i915_gem_object *obj_priv = NULL;
2389 struct drm_i915_private *dev_priv = dev->dev_private;
2390 struct drm_gem_object *obj = NULL;
2391 int i, avail, ret;
2392
2393 /* First try to find a free reg */
2394 avail = 0;
2395 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2396 reg = &dev_priv->fence_regs[i];
2397 if (!reg->obj)
2398 return i;
2399
Daniel Vetter23010e42010-03-08 13:35:02 +01002400 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002401 if (!obj_priv->pin_count)
2402 avail++;
2403 }
2404
2405 if (avail == 0)
2406 return -ENOSPC;
2407
2408 /* None available, try to steal one or wait for a user to finish */
2409 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002410 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2411 lru_list) {
2412 obj = reg->obj;
2413 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002414
2415 if (obj_priv->pin_count)
2416 continue;
2417
2418 /* found one! */
2419 i = obj_priv->fence_reg;
2420 break;
2421 }
2422
2423 BUG_ON(i == I915_FENCE_REG_NONE);
2424
2425 /* We only have a reference on obj from the active list. put_fence_reg
2426 * might drop that one, causing a use-after-free in it. So hold a
2427 * private reference to obj like the other callers of put_fence_reg
2428 * (set_tiling ioctl) do. */
2429 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002430 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002431 drm_gem_object_unreference(obj);
2432 if (ret != 0)
2433 return ret;
2434
2435 return i;
2436}
2437
Jesse Barnesde151cf2008-11-12 10:03:55 -08002438/**
2439 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2440 * @obj: object to map through a fence reg
2441 *
2442 * When mapping objects through the GTT, userspace wants to be able to write
2443 * to them without having to worry about swizzling if the object is tiled.
2444 *
2445 * This function walks the fence regs looking for a free one for @obj,
2446 * stealing one if it can't find any.
2447 *
2448 * It then sets up the reg based on the object's properties: address, pitch
2449 * and tiling format.
2450 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002451int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002452i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2453 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002454{
2455 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002456 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002457 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002458 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002459 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002460
Eric Anholta09ba7f2009-08-29 12:49:51 -07002461 /* Just update our place in the LRU if our fence is getting used. */
2462 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002463 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2464 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002465 return 0;
2466 }
2467
Jesse Barnesde151cf2008-11-12 10:03:55 -08002468 switch (obj_priv->tiling_mode) {
2469 case I915_TILING_NONE:
2470 WARN(1, "allocating a fence for non-tiled object?\n");
2471 break;
2472 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002473 if (!obj_priv->stride)
2474 return -EINVAL;
2475 WARN((obj_priv->stride & (512 - 1)),
2476 "object 0x%08x is X tiled but has non-512B pitch\n",
2477 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002478 break;
2479 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002480 if (!obj_priv->stride)
2481 return -EINVAL;
2482 WARN((obj_priv->stride & (128 - 1)),
2483 "object 0x%08x is Y tiled but has non-128B pitch\n",
2484 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002485 break;
2486 }
2487
Chris Wilson2cf34d72010-09-14 13:03:28 +01002488 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002489 if (ret < 0)
2490 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002491
Daniel Vetterae3db242010-02-19 11:51:58 +01002492 obj_priv->fence_reg = ret;
2493 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002494 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002495
Jesse Barnesde151cf2008-11-12 10:03:55 -08002496 reg->obj = obj;
2497
Chris Wilsone259bef2010-09-17 00:32:02 +01002498 switch (INTEL_INFO(dev)->gen) {
2499 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002500 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002501 break;
2502 case 5:
2503 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002504 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002505 break;
2506 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002507 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002508 break;
2509 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002510 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002511 break;
2512 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002513
Daniel Vetterae3db242010-02-19 11:51:58 +01002514 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2515 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002516
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002517 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002518}
2519
2520/**
2521 * i915_gem_clear_fence_reg - clear out fence register info
2522 * @obj: object to clear
2523 *
2524 * Zeroes out the fence register itself and clears out the associated
2525 * data structures in dev_priv and obj_priv.
2526 */
2527static void
2528i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2529{
2530 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002531 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002532 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002533 struct drm_i915_fence_reg *reg =
2534 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002535 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002536
Chris Wilsone259bef2010-09-17 00:32:02 +01002537 switch (INTEL_INFO(dev)->gen) {
2538 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002539 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2540 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002541 break;
2542 case 5:
2543 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002544 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002545 break;
2546 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002547 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002548 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002549 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002550 case 2:
2551 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002552
2553 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002554 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002555 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002556
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002557 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002558 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002559 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002560}
2561
Eric Anholt673a3942008-07-30 12:06:12 -07002562/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002563 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2564 * to the buffer to finish, and then resets the fence register.
2565 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002566 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002567 *
2568 * Zeroes out the fence register itself and clears out the associated
2569 * data structures in dev_priv and obj_priv.
2570 */
2571int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002572i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2573 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002574{
2575 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002576 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002577 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002578 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002579
2580 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2581 return 0;
2582
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002583 /* If we've changed tiling, GTT-mappings of the object
2584 * need to re-fault to ensure that the correct fence register
2585 * setup is in place.
2586 */
2587 i915_gem_release_mmap(obj);
2588
Chris Wilson52dc7d32009-06-06 09:46:01 +01002589 /* On the i915, GPU access to tiled buffers is via a fence,
2590 * therefore we must wait for any outstanding access to complete
2591 * before clearing the fence.
2592 */
Chris Wilson53640e12010-09-20 11:40:50 +01002593 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2594 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002595 int ret;
2596
Chris Wilson2cf34d72010-09-14 13:03:28 +01002597 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002598 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002599 return ret;
2600
Chris Wilson2cf34d72010-09-14 13:03:28 +01002601 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002602 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002603 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002604
2605 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002606 }
2607
Daniel Vetter4a726612010-02-01 13:59:16 +01002608 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002609 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002610
2611 return 0;
2612}
2613
2614/**
Eric Anholt673a3942008-07-30 12:06:12 -07002615 * Finds free space in the GTT aperture and binds the object there.
2616 */
2617static int
2618i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2619{
2620 struct drm_device *dev = obj->dev;
2621 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002622 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002623 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002624 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002625 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002626
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002627 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002628 DRM_ERROR("Attempting to bind a purgeable object\n");
2629 return -EINVAL;
2630 }
2631
Eric Anholt673a3942008-07-30 12:06:12 -07002632 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002633 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002634 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002635 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2636 return -EINVAL;
2637 }
2638
Chris Wilson654fc602010-05-27 13:18:21 +01002639 /* If the object is bigger than the entire aperture, reject it early
2640 * before evicting everything in a vain attempt to find space.
2641 */
Chris Wilson73aa8082010-09-30 11:46:12 +01002642 if (obj->size > dev_priv->mm.gtt_total) {
Chris Wilson654fc602010-05-27 13:18:21 +01002643 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2644 return -E2BIG;
2645 }
2646
Eric Anholt673a3942008-07-30 12:06:12 -07002647 search_free:
2648 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2649 obj->size, alignment, 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01002650 if (free_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002651 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2652 alignment);
Eric Anholt673a3942008-07-30 12:06:12 -07002653 if (obj_priv->gtt_space == NULL) {
2654 /* If the gtt is empty and we're still having trouble
2655 * fitting our object in, we're out of memory.
2656 */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002657 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002658 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002659 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002660
Eric Anholt673a3942008-07-30 12:06:12 -07002661 goto search_free;
2662 }
2663
Chris Wilson4bdadb92010-01-27 13:36:32 +00002664 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002665 if (ret) {
2666 drm_mm_put_block(obj_priv->gtt_space);
2667 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002668
2669 if (ret == -ENOMEM) {
2670 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002671 ret = i915_gem_evict_something(dev, obj->size,
2672 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002673 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002674 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002675 if (gfpmask) {
2676 gfpmask = 0;
2677 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002678 }
2679
2680 return ret;
2681 }
2682
2683 goto search_free;
2684 }
2685
Eric Anholt673a3942008-07-30 12:06:12 -07002686 return ret;
2687 }
2688
Eric Anholt673a3942008-07-30 12:06:12 -07002689 /* Create an AGP memory structure pointing at our pages, and bind it
2690 * into the GTT.
2691 */
2692 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002693 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002694 obj->size >> PAGE_SHIFT,
Chris Wilson9af90d12010-10-17 10:01:56 +01002695 obj_priv->gtt_space->start,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002696 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002697 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002698 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002699 drm_mm_put_block(obj_priv->gtt_space);
2700 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002701
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002702 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002703 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002704 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002705
2706 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002707 }
Eric Anholt673a3942008-07-30 12:06:12 -07002708
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002709 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson69dc4982010-10-19 10:36:51 +01002710 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01002711 i915_gem_info_add_gtt(dev_priv, obj->size);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002712
Eric Anholt673a3942008-07-30 12:06:12 -07002713 /* Assert that the object is not currently in any GPU domain. As it
2714 * wasn't in the GTT, there shouldn't be any way it could have been in
2715 * a GPU cache
2716 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002717 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2718 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002719
Chris Wilson9af90d12010-10-17 10:01:56 +01002720 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002721 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2722
Eric Anholt673a3942008-07-30 12:06:12 -07002723 return 0;
2724}
2725
2726void
2727i915_gem_clflush_object(struct drm_gem_object *obj)
2728{
Daniel Vetter23010e42010-03-08 13:35:02 +01002729 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002730
2731 /* If we don't have a page list set up, then we're not pinned
2732 * to GPU, and we can ignore the cache flush because it'll happen
2733 * again at bind time.
2734 */
Eric Anholt856fa192009-03-19 14:10:50 -07002735 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002736 return;
2737
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002738 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002739
Eric Anholt856fa192009-03-19 14:10:50 -07002740 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002741}
2742
Eric Anholte47c68e2008-11-14 13:35:19 -08002743/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002744static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002745i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2746 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002747{
2748 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002749 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002750
2751 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002752 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002753
2754 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002755 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002756 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002757 to_intel_bo(obj)->ring,
2758 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002759 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002760
2761 trace_i915_gem_object_change_domain(obj,
2762 obj->read_domains,
2763 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002764
2765 if (pipelined)
2766 return 0;
2767
Chris Wilson2cf34d72010-09-14 13:03:28 +01002768 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002769}
2770
2771/** Flushes the GTT write domain for the object if it's dirty. */
2772static void
2773i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2774{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002775 uint32_t old_write_domain;
2776
Eric Anholte47c68e2008-11-14 13:35:19 -08002777 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2778 return;
2779
2780 /* No actual flushing is required for the GTT write domain. Writes
2781 * to it immediately go to main memory as far as we know, so there's
2782 * no chipset flush. It also doesn't land in render cache.
2783 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002784 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002785 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002786
2787 trace_i915_gem_object_change_domain(obj,
2788 obj->read_domains,
2789 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002790}
2791
2792/** Flushes the CPU write domain for the object if it's dirty. */
2793static void
2794i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2795{
2796 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002797 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002798
2799 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2800 return;
2801
2802 i915_gem_clflush_object(obj);
2803 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002804 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002805 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002806
2807 trace_i915_gem_object_change_domain(obj,
2808 obj->read_domains,
2809 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002810}
2811
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002812/**
2813 * Moves a single object to the GTT read, and possibly write domain.
2814 *
2815 * This function returns when the move is complete, including waiting on
2816 * flushes to occur.
2817 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002818int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002819i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2820{
Daniel Vetter23010e42010-03-08 13:35:02 +01002821 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002822 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002823 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002824
Eric Anholt02354392008-11-26 13:58:13 -08002825 /* Not valid to be called on unbound objects. */
2826 if (obj_priv->gtt_space == NULL)
2827 return -EINVAL;
2828
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002829 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002830 if (ret != 0)
2831 return ret;
2832
Chris Wilson72133422010-09-13 23:56:38 +01002833 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002834
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002835 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002836 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002837 if (ret)
2838 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002839 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002840
2841 old_write_domain = obj->write_domain;
2842 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002843
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002844 /* It should now be out of any other write domains, and we can update
2845 * the domain values for our changes.
2846 */
2847 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2848 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002849 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002850 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002851 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002852 obj_priv->dirty = 1;
2853 }
2854
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002855 trace_i915_gem_object_change_domain(obj,
2856 old_read_domains,
2857 old_write_domain);
2858
Eric Anholte47c68e2008-11-14 13:35:19 -08002859 return 0;
2860}
2861
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002862/*
2863 * Prepare buffer for display plane. Use uninterruptible for possible flush
2864 * wait, as in modesetting process we're not supposed to be interrupted.
2865 */
2866int
Chris Wilson48b956c2010-09-14 12:50:34 +01002867i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2868 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002869{
Daniel Vetter23010e42010-03-08 13:35:02 +01002870 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002871 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002872 int ret;
2873
2874 /* Not valid to be called on unbound objects. */
2875 if (obj_priv->gtt_space == NULL)
2876 return -EINVAL;
2877
Chris Wilsonced270f2010-09-26 22:47:46 +01002878 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002879 if (ret)
2880 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002881
Chris Wilsonced270f2010-09-26 22:47:46 +01002882 /* Currently, we are always called from an non-interruptible context. */
2883 if (!pipelined) {
2884 ret = i915_gem_object_wait_rendering(obj, false);
2885 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002886 return ret;
2887 }
2888
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002889 i915_gem_object_flush_cpu_write_domain(obj);
2890
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002891 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002892 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002893
2894 trace_i915_gem_object_change_domain(obj,
2895 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002896 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002897
2898 return 0;
2899}
2900
Chris Wilson85345512010-11-13 09:49:11 +00002901int
2902i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
2903 bool interruptible)
2904{
2905 if (!obj->active)
2906 return 0;
2907
2908 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
2909 i915_gem_flush_ring(obj->base.dev, NULL, obj->ring,
2910 0, obj->base.write_domain);
2911
2912 return i915_gem_object_wait_rendering(&obj->base, interruptible);
2913}
2914
Eric Anholte47c68e2008-11-14 13:35:19 -08002915/**
2916 * Moves a single object to the CPU read, and possibly write domain.
2917 *
2918 * This function returns when the move is complete, including waiting on
2919 * flushes to occur.
2920 */
2921static int
2922i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2923{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002924 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002925 int ret;
2926
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002927 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002928 if (ret != 0)
2929 return ret;
2930
2931 i915_gem_object_flush_gtt_write_domain(obj);
2932
2933 /* If we have a partially-valid cache of the object in the CPU,
2934 * finish invalidating it and free the per-page flags.
2935 */
2936 i915_gem_object_set_to_full_cpu_read_domain(obj);
2937
Chris Wilson72133422010-09-13 23:56:38 +01002938 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002939 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002940 if (ret)
2941 return ret;
2942 }
2943
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002944 old_write_domain = obj->write_domain;
2945 old_read_domains = obj->read_domains;
2946
Eric Anholte47c68e2008-11-14 13:35:19 -08002947 /* Flush the CPU cache if it's still invalid. */
2948 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2949 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002950
2951 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2952 }
2953
2954 /* It should now be out of any other write domains, and we can update
2955 * the domain values for our changes.
2956 */
2957 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2958
2959 /* If we're writing through the CPU, then the GPU read domains will
2960 * need to be invalidated at next use.
2961 */
2962 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002963 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002964 obj->write_domain = I915_GEM_DOMAIN_CPU;
2965 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002966
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002967 trace_i915_gem_object_change_domain(obj,
2968 old_read_domains,
2969 old_write_domain);
2970
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002971 return 0;
2972}
2973
Eric Anholt673a3942008-07-30 12:06:12 -07002974/*
2975 * Set the next domain for the specified object. This
2976 * may not actually perform the necessary flushing/invaliding though,
2977 * as that may want to be batched with other set_domain operations
2978 *
2979 * This is (we hope) the only really tricky part of gem. The goal
2980 * is fairly simple -- track which caches hold bits of the object
2981 * and make sure they remain coherent. A few concrete examples may
2982 * help to explain how it works. For shorthand, we use the notation
2983 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2984 * a pair of read and write domain masks.
2985 *
2986 * Case 1: the batch buffer
2987 *
2988 * 1. Allocated
2989 * 2. Written by CPU
2990 * 3. Mapped to GTT
2991 * 4. Read by GPU
2992 * 5. Unmapped from GTT
2993 * 6. Freed
2994 *
2995 * Let's take these a step at a time
2996 *
2997 * 1. Allocated
2998 * Pages allocated from the kernel may still have
2999 * cache contents, so we set them to (CPU, CPU) always.
3000 * 2. Written by CPU (using pwrite)
3001 * The pwrite function calls set_domain (CPU, CPU) and
3002 * this function does nothing (as nothing changes)
3003 * 3. Mapped by GTT
3004 * This function asserts that the object is not
3005 * currently in any GPU-based read or write domains
3006 * 4. Read by GPU
3007 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3008 * As write_domain is zero, this function adds in the
3009 * current read domains (CPU+COMMAND, 0).
3010 * flush_domains is set to CPU.
3011 * invalidate_domains is set to COMMAND
3012 * clflush is run to get data out of the CPU caches
3013 * then i915_dev_set_domain calls i915_gem_flush to
3014 * emit an MI_FLUSH and drm_agp_chipset_flush
3015 * 5. Unmapped from GTT
3016 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3017 * flush_domains and invalidate_domains end up both zero
3018 * so no flushing/invalidating happens
3019 * 6. Freed
3020 * yay, done
3021 *
3022 * Case 2: The shared render buffer
3023 *
3024 * 1. Allocated
3025 * 2. Mapped to GTT
3026 * 3. Read/written by GPU
3027 * 4. set_domain to (CPU,CPU)
3028 * 5. Read/written by CPU
3029 * 6. Read/written by GPU
3030 *
3031 * 1. Allocated
3032 * Same as last example, (CPU, CPU)
3033 * 2. Mapped to GTT
3034 * Nothing changes (assertions find that it is not in the GPU)
3035 * 3. Read/written by GPU
3036 * execbuffer calls set_domain (RENDER, RENDER)
3037 * flush_domains gets CPU
3038 * invalidate_domains gets GPU
3039 * clflush (obj)
3040 * MI_FLUSH and drm_agp_chipset_flush
3041 * 4. set_domain (CPU, CPU)
3042 * flush_domains gets GPU
3043 * invalidate_domains gets CPU
3044 * wait_rendering (obj) to make sure all drawing is complete.
3045 * This will include an MI_FLUSH to get the data from GPU
3046 * to memory
3047 * clflush (obj) to invalidate the CPU cache
3048 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3049 * 5. Read/written by CPU
3050 * cache lines are loaded and dirtied
3051 * 6. Read written by GPU
3052 * Same as last GPU access
3053 *
3054 * Case 3: The constant buffer
3055 *
3056 * 1. Allocated
3057 * 2. Written by CPU
3058 * 3. Read by GPU
3059 * 4. Updated (written) by CPU again
3060 * 5. Read by GPU
3061 *
3062 * 1. Allocated
3063 * (CPU, CPU)
3064 * 2. Written by CPU
3065 * (CPU, CPU)
3066 * 3. Read by GPU
3067 * (CPU+RENDER, 0)
3068 * flush_domains = CPU
3069 * invalidate_domains = RENDER
3070 * clflush (obj)
3071 * MI_FLUSH
3072 * drm_agp_chipset_flush
3073 * 4. Updated (written) by CPU again
3074 * (CPU, CPU)
3075 * flush_domains = 0 (no previous write domain)
3076 * invalidate_domains = 0 (no new read domains)
3077 * 5. Read by GPU
3078 * (CPU+RENDER, 0)
3079 * flush_domains = CPU
3080 * invalidate_domains = RENDER
3081 * clflush (obj)
3082 * MI_FLUSH
3083 * drm_agp_chipset_flush
3084 */
Keith Packardc0d90822008-11-20 23:11:08 -08003085static void
Chris Wilsonb6651452010-10-23 10:15:06 +01003086i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
3087 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07003088{
3089 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003090 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003091 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003092 uint32_t invalidate_domains = 0;
3093 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003094 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003095
Jesse Barnes652c3932009-08-17 13:31:43 -07003096 intel_mark_busy(dev, obj);
3097
Eric Anholt673a3942008-07-30 12:06:12 -07003098 /*
3099 * If the object isn't moving to a new write domain,
3100 * let the object stay in multiple read domains
3101 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003102 if (obj->pending_write_domain == 0)
3103 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003104 else
3105 obj_priv->dirty = 1;
3106
3107 /*
3108 * Flush the current write domain if
3109 * the new read domains don't match. Invalidate
3110 * any read domains which differ from the old
3111 * write domain
3112 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003113 if (obj->write_domain &&
Chris Wilsonc6afd652010-11-01 13:39:24 +00003114 (obj->write_domain != obj->pending_read_domains ||
3115 obj_priv->ring != ring)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003116 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003117 invalidate_domains |=
3118 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003119 }
3120 /*
3121 * Invalidate any read caches which may have
3122 * stale data. That is, any new read domains.
3123 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003124 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003125 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003126 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003127
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003128 old_read_domains = obj->read_domains;
3129
Eric Anholtefbeed92009-02-19 14:54:51 -08003130 /* The actual obj->write_domain will be updated with
3131 * pending_write_domain after we emit the accumulated flush for all
3132 * of our domain changes in execbuffers (which clears objects'
3133 * write_domains). So if we have a current write domain that we
3134 * aren't changing, set pending_write_domain to that.
3135 */
3136 if (flush_domains == 0 && obj->pending_write_domain == 0)
3137 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003138 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003139
3140 dev->invalidate_domains |= invalidate_domains;
3141 dev->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003142 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson92204342010-09-18 11:02:01 +01003143 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003144 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
3145 dev_priv->mm.flush_rings |= ring->id;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003146
3147 trace_i915_gem_object_change_domain(obj,
3148 old_read_domains,
3149 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003150}
3151
3152/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003153 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003154 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003155 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3156 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3157 */
3158static void
3159i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3160{
Daniel Vetter23010e42010-03-08 13:35:02 +01003161 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003162
3163 if (!obj_priv->page_cpu_valid)
3164 return;
3165
3166 /* If we're partially in the CPU read domain, finish moving it in.
3167 */
3168 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3169 int i;
3170
3171 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3172 if (obj_priv->page_cpu_valid[i])
3173 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003174 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003175 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003176 }
3177
3178 /* Free the page_cpu_valid mappings which are now stale, whether
3179 * or not we've got I915_GEM_DOMAIN_CPU.
3180 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003181 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003182 obj_priv->page_cpu_valid = NULL;
3183}
3184
3185/**
3186 * Set the CPU read domain on a range of the object.
3187 *
3188 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3189 * not entirely valid. The page_cpu_valid member of the object flags which
3190 * pages have been flushed, and will be respected by
3191 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3192 * of the whole object.
3193 *
3194 * This function returns when the move is complete, including waiting on
3195 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003196 */
3197static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003198i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3199 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003200{
Daniel Vetter23010e42010-03-08 13:35:02 +01003201 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003202 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003203 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003204
Eric Anholte47c68e2008-11-14 13:35:19 -08003205 if (offset == 0 && size == obj->size)
3206 return i915_gem_object_set_to_cpu_domain(obj, 0);
3207
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003208 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003209 if (ret != 0)
3210 return ret;
3211 i915_gem_object_flush_gtt_write_domain(obj);
3212
3213 /* If we're already fully in the CPU read domain, we're done. */
3214 if (obj_priv->page_cpu_valid == NULL &&
3215 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003216 return 0;
3217
Eric Anholte47c68e2008-11-14 13:35:19 -08003218 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3219 * newly adding I915_GEM_DOMAIN_CPU
3220 */
Eric Anholt673a3942008-07-30 12:06:12 -07003221 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003222 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3223 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003224 if (obj_priv->page_cpu_valid == NULL)
3225 return -ENOMEM;
3226 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3227 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003228
3229 /* Flush the cache on any pages that are still invalid from the CPU's
3230 * perspective.
3231 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003232 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3233 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003234 if (obj_priv->page_cpu_valid[i])
3235 continue;
3236
Eric Anholt856fa192009-03-19 14:10:50 -07003237 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003238
3239 obj_priv->page_cpu_valid[i] = 1;
3240 }
3241
Eric Anholte47c68e2008-11-14 13:35:19 -08003242 /* It should now be out of any other write domains, and we can update
3243 * the domain values for our changes.
3244 */
3245 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3246
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003247 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003248 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3249
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003250 trace_i915_gem_object_change_domain(obj,
3251 old_read_domains,
3252 obj->write_domain);
3253
Eric Anholt673a3942008-07-30 12:06:12 -07003254 return 0;
3255}
3256
3257/**
Eric Anholt673a3942008-07-30 12:06:12 -07003258 * Pin an object to the GTT and evaluate the relocations landing in it.
3259 */
3260static int
Chris Wilson9af90d12010-10-17 10:01:56 +01003261i915_gem_execbuffer_relocate(struct drm_i915_gem_object *obj,
3262 struct drm_file *file_priv,
3263 struct drm_i915_gem_exec_object2 *entry)
Eric Anholt673a3942008-07-30 12:06:12 -07003264{
Chris Wilson9af90d12010-10-17 10:01:56 +01003265 struct drm_device *dev = obj->base.dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003266 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson2549d6c2010-10-14 12:10:41 +01003267 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson9af90d12010-10-17 10:01:56 +01003268 struct drm_gem_object *target_obj = NULL;
3269 uint32_t target_handle = 0;
3270 int i, ret = 0;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003271
Chris Wilson2549d6c2010-10-14 12:10:41 +01003272 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Eric Anholt673a3942008-07-30 12:06:12 -07003273 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2549d6c2010-10-14 12:10:41 +01003274 struct drm_i915_gem_relocation_entry reloc;
Chris Wilson9af90d12010-10-17 10:01:56 +01003275 uint32_t target_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003276
Chris Wilson9af90d12010-10-17 10:01:56 +01003277 if (__copy_from_user_inatomic(&reloc,
3278 user_relocs+i,
3279 sizeof(reloc))) {
3280 ret = -EFAULT;
3281 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003282 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003283
Chris Wilson9af90d12010-10-17 10:01:56 +01003284 if (reloc.target_handle != target_handle) {
3285 drm_gem_object_unreference(target_obj);
3286
3287 target_obj = drm_gem_object_lookup(dev, file_priv,
3288 reloc.target_handle);
3289 if (target_obj == NULL) {
3290 ret = -ENOENT;
3291 break;
3292 }
3293
3294 target_handle = reloc.target_handle;
Eric Anholt673a3942008-07-30 12:06:12 -07003295 }
Chris Wilson9af90d12010-10-17 10:01:56 +01003296 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003297
Chris Wilson8542a0b2009-09-09 21:15:15 +01003298#if WATCH_RELOC
3299 DRM_INFO("%s: obj %p offset %08x target %d "
3300 "read %08x write %08x gtt %08x "
3301 "presumed %08x delta %08x\n",
3302 __func__,
3303 obj,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003304 (int) reloc.offset,
3305 (int) reloc.target_handle,
3306 (int) reloc.read_domains,
3307 (int) reloc.write_domain,
Chris Wilson9af90d12010-10-17 10:01:56 +01003308 (int) target_offset,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003309 (int) reloc.presumed_offset,
3310 reloc.delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003311#endif
3312
Eric Anholt673a3942008-07-30 12:06:12 -07003313 /* The target buffer should have appeared before us in the
3314 * exec_object list, so it should have a GTT space bound by now.
3315 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003316 if (target_offset == 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07003317 DRM_ERROR("No GTT space found for object %d\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003318 reloc.target_handle);
Chris Wilson9af90d12010-10-17 10:01:56 +01003319 ret = -EINVAL;
3320 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003321 }
3322
Chris Wilson8542a0b2009-09-09 21:15:15 +01003323 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003324 if (reloc.write_domain & (reloc.write_domain - 1)) {
Daniel Vetter16edd552010-02-19 11:52:02 +01003325 DRM_ERROR("reloc with multiple write domains: "
3326 "obj %p target %d offset %d "
3327 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003328 obj, reloc.target_handle,
3329 (int) reloc.offset,
3330 reloc.read_domains,
3331 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003332 ret = -EINVAL;
3333 break;
Daniel Vetter16edd552010-02-19 11:52:02 +01003334 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003335 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
3336 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003337 DRM_ERROR("reloc with read/write CPU domains: "
3338 "obj %p target %d offset %d "
3339 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003340 obj, reloc.target_handle,
3341 (int) reloc.offset,
3342 reloc.read_domains,
3343 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003344 ret = -EINVAL;
3345 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003346 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003347 if (reloc.write_domain && target_obj->pending_write_domain &&
3348 reloc.write_domain != target_obj->pending_write_domain) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003349 DRM_ERROR("Write domain conflict: "
3350 "obj %p target %d offset %d "
3351 "new %08x old %08x\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003352 obj, reloc.target_handle,
3353 (int) reloc.offset,
3354 reloc.write_domain,
Chris Wilson8542a0b2009-09-09 21:15:15 +01003355 target_obj->pending_write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003356 ret = -EINVAL;
3357 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003358 }
3359
Chris Wilson2549d6c2010-10-14 12:10:41 +01003360 target_obj->pending_read_domains |= reloc.read_domains;
Chris Wilson878a3c32010-10-22 10:48:12 +01003361 target_obj->pending_write_domain |= reloc.write_domain;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003362
3363 /* If the relocation already has the right value in it, no
3364 * more work needs to be done.
3365 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003366 if (target_offset == reloc.presumed_offset)
Chris Wilson8542a0b2009-09-09 21:15:15 +01003367 continue;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003368
3369 /* Check that the relocation address is valid... */
Chris Wilson9af90d12010-10-17 10:01:56 +01003370 if (reloc.offset > obj->base.size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003371 DRM_ERROR("Relocation beyond object bounds: "
3372 "obj %p target %d offset %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003373 obj, reloc.target_handle,
Chris Wilson9af90d12010-10-17 10:01:56 +01003374 (int) reloc.offset, (int) obj->base.size);
3375 ret = -EINVAL;
3376 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003377 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003378 if (reloc.offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003379 DRM_ERROR("Relocation not 4-byte aligned: "
3380 "obj %p target %d offset %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003381 obj, reloc.target_handle,
3382 (int) reloc.offset);
Chris Wilson9af90d12010-10-17 10:01:56 +01003383 ret = -EINVAL;
3384 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003385 }
3386
Chris Wilson8542a0b2009-09-09 21:15:15 +01003387 /* and points to somewhere within the target object. */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003388 if (reloc.delta >= target_obj->size) {
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003389 DRM_ERROR("Relocation beyond target object bounds: "
3390 "obj %p target %d delta %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003391 obj, reloc.target_handle,
3392 (int) reloc.delta, (int) target_obj->size);
Chris Wilson9af90d12010-10-17 10:01:56 +01003393 ret = -EINVAL;
3394 break;
Eric Anholte47c68e2008-11-14 13:35:19 -08003395 }
3396
Chris Wilson9af90d12010-10-17 10:01:56 +01003397 reloc.delta += target_offset;
3398 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003399 uint32_t page_offset = reloc.offset & ~PAGE_MASK;
3400 char *vaddr;
3401
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003402 vaddr = kmap_atomic(obj->pages[reloc.offset >> PAGE_SHIFT]);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003403 *(uint32_t *)(vaddr + page_offset) = reloc.delta;
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003404 kunmap_atomic(vaddr);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003405 } else {
3406 uint32_t __iomem *reloc_entry;
3407 void __iomem *reloc_page;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003408
Chris Wilson9af90d12010-10-17 10:01:56 +01003409 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3410 if (ret)
3411 break;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003412
3413 /* Map the page containing the relocation we're going to perform. */
Chris Wilson9af90d12010-10-17 10:01:56 +01003414 reloc.offset += obj->gtt_offset;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003415 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003416 reloc.offset & PAGE_MASK);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003417 reloc_entry = (uint32_t __iomem *)
3418 (reloc_page + (reloc.offset & ~PAGE_MASK));
3419 iowrite32(reloc.delta, reloc_entry);
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003420 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003421 }
3422
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003423 /* and update the user's relocation entry */
3424 reloc.presumed_offset = target_offset;
3425 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3426 &reloc.presumed_offset,
3427 sizeof(reloc.presumed_offset))) {
3428 ret = -EFAULT;
3429 break;
3430 }
Eric Anholt673a3942008-07-30 12:06:12 -07003431 }
3432
Chris Wilson9af90d12010-10-17 10:01:56 +01003433 drm_gem_object_unreference(target_obj);
3434 return ret;
3435}
3436
3437static int
3438i915_gem_execbuffer_pin(struct drm_device *dev,
3439 struct drm_file *file,
3440 struct drm_gem_object **object_list,
3441 struct drm_i915_gem_exec_object2 *exec_list,
3442 int count)
3443{
3444 struct drm_i915_private *dev_priv = dev->dev_private;
3445 int ret, i, retry;
3446
3447 /* attempt to pin all of the buffers into the GTT */
3448 for (retry = 0; retry < 2; retry++) {
3449 ret = 0;
3450 for (i = 0; i < count; i++) {
3451 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
3452 struct drm_i915_gem_object *obj= to_intel_bo(object_list[i]);
3453 bool need_fence =
3454 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3455 obj->tiling_mode != I915_TILING_NONE;
3456
3457 /* Check fence reg constraints and rebind if necessary */
3458 if (need_fence &&
3459 !i915_gem_object_fence_offset_ok(&obj->base,
3460 obj->tiling_mode)) {
3461 ret = i915_gem_object_unbind(&obj->base);
3462 if (ret)
3463 break;
3464 }
3465
3466 ret = i915_gem_object_pin(&obj->base, entry->alignment);
3467 if (ret)
3468 break;
3469
3470 /*
3471 * Pre-965 chips need a fence register set up in order
3472 * to properly handle blits to/from tiled surfaces.
3473 */
3474 if (need_fence) {
3475 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3476 if (ret) {
3477 i915_gem_object_unpin(&obj->base);
3478 break;
3479 }
3480
3481 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3482 }
3483
3484 entry->offset = obj->gtt_offset;
3485 }
3486
3487 while (i--)
3488 i915_gem_object_unpin(object_list[i]);
3489
3490 if (ret == 0)
3491 break;
3492
3493 if (ret != -ENOSPC || retry)
3494 return ret;
3495
3496 ret = i915_gem_evict_everything(dev);
3497 if (ret)
3498 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003499 }
3500
Eric Anholt673a3942008-07-30 12:06:12 -07003501 return 0;
3502}
3503
Chris Wilsonc6afd652010-11-01 13:39:24 +00003504static int
3505i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3506 struct drm_file *file,
3507 struct intel_ring_buffer *ring,
3508 struct drm_gem_object **objects,
3509 int count)
3510{
3511 struct drm_i915_private *dev_priv = dev->dev_private;
3512 int ret, i;
3513
3514 /* Zero the global flush/invalidate flags. These
3515 * will be modified as new domains are computed
3516 * for each object
3517 */
3518 dev->invalidate_domains = 0;
3519 dev->flush_domains = 0;
3520 dev_priv->mm.flush_rings = 0;
3521 for (i = 0; i < count; i++)
3522 i915_gem_object_set_to_gpu_domain(objects[i], ring);
3523
3524 if (dev->invalidate_domains | dev->flush_domains) {
3525#if WATCH_EXEC
3526 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3527 __func__,
3528 dev->invalidate_domains,
3529 dev->flush_domains);
3530#endif
3531 i915_gem_flush(dev, file,
3532 dev->invalidate_domains,
3533 dev->flush_domains,
3534 dev_priv->mm.flush_rings);
3535 }
3536
3537 for (i = 0; i < count; i++) {
3538 struct drm_i915_gem_object *obj = to_intel_bo(objects[i]);
3539 /* XXX replace with semaphores */
3540 if (obj->ring && ring != obj->ring) {
3541 ret = i915_gem_object_wait_rendering(&obj->base, true);
3542 if (ret)
3543 return ret;
3544 }
3545 }
3546
3547 return 0;
3548}
3549
Eric Anholt673a3942008-07-30 12:06:12 -07003550/* Throttle our rendering by waiting until the ring has completed our requests
3551 * emitted over 20 msec ago.
3552 *
Eric Anholtb9624422009-06-03 07:27:35 +00003553 * Note that if we were to use the current jiffies each time around the loop,
3554 * we wouldn't escape the function with any frames outstanding if the time to
3555 * render a frame was over 20ms.
3556 *
Eric Anholt673a3942008-07-30 12:06:12 -07003557 * This should get us reasonable parallelism between CPU and GPU but also
3558 * relatively low latency when blocking on a particular request to finish.
3559 */
3560static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003561i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003562{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003563 struct drm_i915_private *dev_priv = dev->dev_private;
3564 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003565 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003566 struct drm_i915_gem_request *request;
3567 struct intel_ring_buffer *ring = NULL;
3568 u32 seqno = 0;
3569 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003570
Chris Wilson1c255952010-09-26 11:03:27 +01003571 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003572 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003573 if (time_after_eq(request->emitted_jiffies, recent_enough))
3574 break;
3575
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003576 ring = request->ring;
3577 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003578 }
Chris Wilson1c255952010-09-26 11:03:27 +01003579 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003580
3581 if (seqno == 0)
3582 return 0;
3583
3584 ret = 0;
3585 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3586 /* And wait for the seqno passing without holding any locks and
3587 * causing extra latency for others. This is safe as the irq
3588 * generation is designed to be run atomically and so is
3589 * lockless.
3590 */
3591 ring->user_irq_get(dev, ring);
3592 ret = wait_event_interruptible(ring->irq_queue,
3593 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3594 || atomic_read(&dev_priv->mm.wedged));
3595 ring->user_irq_put(dev, ring);
3596
3597 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3598 ret = -EIO;
3599 }
3600
3601 if (ret == 0)
3602 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003603
Eric Anholt673a3942008-07-30 12:06:12 -07003604 return ret;
3605}
3606
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003607static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003608i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3609 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003610{
3611 uint32_t exec_start, exec_len;
3612
3613 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3614 exec_len = (uint32_t) exec->batch_len;
3615
3616 if ((exec_start | exec_len) & 0x7)
3617 return -EINVAL;
3618
3619 if (!exec_start)
3620 return -EINVAL;
3621
3622 return 0;
3623}
3624
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003625static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003626validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3627 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003628{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003629 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003630
Chris Wilson2549d6c2010-10-14 12:10:41 +01003631 for (i = 0; i < count; i++) {
3632 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3633 size_t length = exec[i].relocation_count * sizeof(struct drm_i915_gem_relocation_entry);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003634
Chris Wilson2549d6c2010-10-14 12:10:41 +01003635 if (!access_ok(VERIFY_READ, ptr, length))
3636 return -EFAULT;
3637
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003638 /* we may also need to update the presumed offsets */
3639 if (!access_ok(VERIFY_WRITE, ptr, length))
3640 return -EFAULT;
3641
Chris Wilson2549d6c2010-10-14 12:10:41 +01003642 if (fault_in_pages_readable(ptr, length))
3643 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003644 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003645
Chris Wilson2549d6c2010-10-14 12:10:41 +01003646 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003647}
3648
Chris Wilson2549d6c2010-10-14 12:10:41 +01003649static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003650i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003651 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003652 struct drm_i915_gem_execbuffer2 *args,
3653 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003654{
3655 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003656 struct drm_gem_object **object_list = NULL;
3657 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003658 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003659 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003660 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003661 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003662 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003663
Zou Nan hai852835f2010-05-21 09:08:56 +08003664 struct intel_ring_buffer *ring = NULL;
3665
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003666 ret = i915_gem_check_is_wedged(dev);
3667 if (ret)
3668 return ret;
3669
Chris Wilson2549d6c2010-10-14 12:10:41 +01003670 ret = validate_exec_list(exec_list, args->buffer_count);
3671 if (ret)
3672 return ret;
3673
Eric Anholt673a3942008-07-30 12:06:12 -07003674#if WATCH_EXEC
3675 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3676 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3677#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003678 switch (args->flags & I915_EXEC_RING_MASK) {
3679 case I915_EXEC_DEFAULT:
3680 case I915_EXEC_RENDER:
3681 ring = &dev_priv->render_ring;
3682 break;
3683 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003684 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003685 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003686 return -EINVAL;
3687 }
3688 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003689 break;
3690 case I915_EXEC_BLT:
3691 if (!HAS_BLT(dev)) {
3692 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3693 return -EINVAL;
3694 }
3695 ring = &dev_priv->blt_ring;
3696 break;
3697 default:
3698 DRM_ERROR("execbuf with unknown ring: %d\n",
3699 (int)(args->flags & I915_EXEC_RING_MASK));
3700 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003701 }
3702
Eric Anholt4f481ed2008-09-10 14:22:49 -07003703 if (args->buffer_count < 1) {
3704 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3705 return -EINVAL;
3706 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003707 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003708 if (object_list == NULL) {
3709 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003710 args->buffer_count);
3711 ret = -ENOMEM;
3712 goto pre_mutex_err;
3713 }
Eric Anholt673a3942008-07-30 12:06:12 -07003714
Eric Anholt201361a2009-03-11 12:30:04 -07003715 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003716 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3717 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003718 if (cliprects == NULL) {
3719 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003720 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003721 }
Eric Anholt201361a2009-03-11 12:30:04 -07003722
3723 ret = copy_from_user(cliprects,
3724 (struct drm_clip_rect __user *)
3725 (uintptr_t) args->cliprects_ptr,
3726 sizeof(*cliprects) * args->num_cliprects);
3727 if (ret != 0) {
3728 DRM_ERROR("copy %d cliprects failed: %d\n",
3729 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003730 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003731 goto pre_mutex_err;
3732 }
3733 }
3734
Chris Wilson8dc5d142010-08-12 12:36:12 +01003735 request = kzalloc(sizeof(*request), GFP_KERNEL);
3736 if (request == NULL) {
3737 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003738 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003739 }
3740
Chris Wilson76c1dec2010-09-25 11:22:51 +01003741 ret = i915_mutex_lock_interruptible(dev);
3742 if (ret)
3743 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003744
Eric Anholt673a3942008-07-30 12:06:12 -07003745 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003746 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003747 ret = -EBUSY;
3748 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003749 }
3750
Keith Packardac94a962008-11-20 23:30:27 -08003751 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003752 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson9af90d12010-10-17 10:01:56 +01003753 object_list[i] = drm_gem_object_lookup(dev, file,
Eric Anholt673a3942008-07-30 12:06:12 -07003754 exec_list[i].handle);
3755 if (object_list[i] == NULL) {
3756 DRM_ERROR("Invalid object handle %d at index %d\n",
3757 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003758 /* prevent error path from reading uninitialized data */
3759 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003760 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003761 goto err;
3762 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003763
Daniel Vetter23010e42010-03-08 13:35:02 +01003764 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003765 if (obj_priv->in_execbuffer) {
3766 DRM_ERROR("Object %p appears more than once in object list\n",
3767 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003768 /* prevent error path from reading uninitialized data */
3769 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003770 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003771 goto err;
3772 }
3773 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003774 }
3775
Chris Wilson9af90d12010-10-17 10:01:56 +01003776 /* Move the objects en-masse into the GTT, evicting if necessary. */
3777 ret = i915_gem_execbuffer_pin(dev, file,
3778 object_list, exec_list,
3779 args->buffer_count);
3780 if (ret)
3781 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003782
Chris Wilson9af90d12010-10-17 10:01:56 +01003783 /* The objects are in their final locations, apply the relocations. */
3784 for (i = 0; i < args->buffer_count; i++) {
3785 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3786 obj->base.pending_read_domains = 0;
3787 obj->base.pending_write_domain = 0;
3788 ret = i915_gem_execbuffer_relocate(obj, file, &exec_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003789 if (ret)
3790 goto err;
3791 }
3792
Eric Anholt673a3942008-07-30 12:06:12 -07003793 /* Set the pending read domains for the batch buffer to COMMAND */
3794 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003795 if (batch_obj->pending_write_domain) {
3796 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3797 ret = -EINVAL;
3798 goto err;
3799 }
3800 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003801
Chris Wilson9af90d12010-10-17 10:01:56 +01003802 /* Sanity check the batch buffer */
3803 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
3804 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01003805 if (ret != 0) {
3806 DRM_ERROR("execbuf with invalid offset/length\n");
3807 goto err;
3808 }
3809
Chris Wilsonc6afd652010-11-01 13:39:24 +00003810 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
3811 object_list, args->buffer_count);
3812 if (ret)
3813 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003814
Eric Anholtefbeed92009-02-19 14:54:51 -08003815 for (i = 0; i < args->buffer_count; i++) {
3816 struct drm_gem_object *obj = object_list[i];
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003817 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003818 obj->write_domain = obj->pending_write_domain;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003819 trace_i915_gem_object_change_domain(obj,
3820 obj->read_domains,
3821 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003822 }
3823
Eric Anholt673a3942008-07-30 12:06:12 -07003824#if WATCH_COHERENCY
3825 for (i = 0; i < args->buffer_count; i++) {
3826 i915_gem_object_check_coherency(object_list[i],
3827 exec_list[i].handle);
3828 }
3829#endif
3830
Eric Anholt673a3942008-07-30 12:06:12 -07003831#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003832 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003833 args->batch_len,
3834 __func__,
3835 ~0);
3836#endif
3837
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003838 /* Check for any pending flips. As we only maintain a flip queue depth
3839 * of 1, we can simply insert a WAIT for the next display flip prior
3840 * to executing the batch and avoid stalling the CPU.
3841 */
3842 flips = 0;
3843 for (i = 0; i < args->buffer_count; i++) {
3844 if (object_list[i]->write_domain)
3845 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
3846 }
3847 if (flips) {
3848 int plane, flip_mask;
3849
3850 for (plane = 0; flips >> plane; plane++) {
3851 if (((flips >> plane) & 1) == 0)
3852 continue;
3853
3854 if (plane)
3855 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
3856 else
3857 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
3858
3859 intel_ring_begin(dev, ring, 2);
3860 intel_ring_emit(dev, ring,
3861 MI_WAIT_FOR_EVENT | flip_mask);
3862 intel_ring_emit(dev, ring, MI_NOOP);
3863 intel_ring_advance(dev, ring);
3864 }
3865 }
3866
Eric Anholt673a3942008-07-30 12:06:12 -07003867 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003868 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003869 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003870 if (ret) {
3871 DRM_ERROR("dispatch failed %d\n", ret);
3872 goto err;
3873 }
3874
3875 /*
3876 * Ensure that the commands in the batch buffer are
3877 * finished before the interrupt fires
3878 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003879 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003880
Eric Anholt673a3942008-07-30 12:06:12 -07003881 for (i = 0; i < args->buffer_count; i++) {
3882 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003883
Daniel Vetter617dbe22010-02-11 22:16:02 +01003884 i915_gem_object_move_to_active(obj, ring);
Chris Wilson64193402010-10-24 12:38:05 +01003885 if (obj->write_domain)
3886 list_move_tail(&to_intel_bo(obj)->gpu_write_list,
3887 &ring->gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003888 }
Eric Anholt673a3942008-07-30 12:06:12 -07003889
Chris Wilson9af90d12010-10-17 10:01:56 +01003890 i915_add_request(dev, file, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003891 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003892
Eric Anholt673a3942008-07-30 12:06:12 -07003893err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003894 for (i = 0; i < args->buffer_count; i++) {
3895 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003896 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003897 obj_priv->in_execbuffer = false;
3898 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003899 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003900 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003901
Eric Anholt673a3942008-07-30 12:06:12 -07003902 mutex_unlock(&dev->struct_mutex);
3903
Chris Wilson93533c22010-01-31 10:40:48 +00003904pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003905 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003906 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003907 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003908
3909 return ret;
3910}
3911
Jesse Barnes76446ca2009-12-17 22:05:42 -05003912/*
3913 * Legacy execbuffer just creates an exec2 list from the original exec object
3914 * list array and passes it to the real function.
3915 */
3916int
3917i915_gem_execbuffer(struct drm_device *dev, void *data,
3918 struct drm_file *file_priv)
3919{
3920 struct drm_i915_gem_execbuffer *args = data;
3921 struct drm_i915_gem_execbuffer2 exec2;
3922 struct drm_i915_gem_exec_object *exec_list = NULL;
3923 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3924 int ret, i;
3925
3926#if WATCH_EXEC
3927 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3928 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3929#endif
3930
3931 if (args->buffer_count < 1) {
3932 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3933 return -EINVAL;
3934 }
3935
3936 /* Copy in the exec list from userland */
3937 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3938 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3939 if (exec_list == NULL || exec2_list == NULL) {
3940 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3941 args->buffer_count);
3942 drm_free_large(exec_list);
3943 drm_free_large(exec2_list);
3944 return -ENOMEM;
3945 }
3946 ret = copy_from_user(exec_list,
3947 (struct drm_i915_relocation_entry __user *)
3948 (uintptr_t) args->buffers_ptr,
3949 sizeof(*exec_list) * args->buffer_count);
3950 if (ret != 0) {
3951 DRM_ERROR("copy %d exec entries failed %d\n",
3952 args->buffer_count, ret);
3953 drm_free_large(exec_list);
3954 drm_free_large(exec2_list);
3955 return -EFAULT;
3956 }
3957
3958 for (i = 0; i < args->buffer_count; i++) {
3959 exec2_list[i].handle = exec_list[i].handle;
3960 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3961 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3962 exec2_list[i].alignment = exec_list[i].alignment;
3963 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003964 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05003965 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3966 else
3967 exec2_list[i].flags = 0;
3968 }
3969
3970 exec2.buffers_ptr = args->buffers_ptr;
3971 exec2.buffer_count = args->buffer_count;
3972 exec2.batch_start_offset = args->batch_start_offset;
3973 exec2.batch_len = args->batch_len;
3974 exec2.DR1 = args->DR1;
3975 exec2.DR4 = args->DR4;
3976 exec2.num_cliprects = args->num_cliprects;
3977 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003978 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003979
3980 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3981 if (!ret) {
3982 /* Copy the new buffer offsets back to the user's exec list. */
3983 for (i = 0; i < args->buffer_count; i++)
3984 exec_list[i].offset = exec2_list[i].offset;
3985 /* ... and back out to userspace */
3986 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3987 (uintptr_t) args->buffers_ptr,
3988 exec_list,
3989 sizeof(*exec_list) * args->buffer_count);
3990 if (ret) {
3991 ret = -EFAULT;
3992 DRM_ERROR("failed to copy %d exec entries "
3993 "back to user (%d)\n",
3994 args->buffer_count, ret);
3995 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003996 }
3997
3998 drm_free_large(exec_list);
3999 drm_free_large(exec2_list);
4000 return ret;
4001}
4002
4003int
4004i915_gem_execbuffer2(struct drm_device *dev, void *data,
4005 struct drm_file *file_priv)
4006{
4007 struct drm_i915_gem_execbuffer2 *args = data;
4008 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4009 int ret;
4010
4011#if WATCH_EXEC
4012 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4013 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4014#endif
4015
4016 if (args->buffer_count < 1) {
4017 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4018 return -EINVAL;
4019 }
4020
4021 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4022 if (exec2_list == NULL) {
4023 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4024 args->buffer_count);
4025 return -ENOMEM;
4026 }
4027 ret = copy_from_user(exec2_list,
4028 (struct drm_i915_relocation_entry __user *)
4029 (uintptr_t) args->buffers_ptr,
4030 sizeof(*exec2_list) * args->buffer_count);
4031 if (ret != 0) {
4032 DRM_ERROR("copy %d exec entries failed %d\n",
4033 args->buffer_count, ret);
4034 drm_free_large(exec2_list);
4035 return -EFAULT;
4036 }
4037
4038 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4039 if (!ret) {
4040 /* Copy the new buffer offsets back to the user's exec list. */
4041 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4042 (uintptr_t) args->buffers_ptr,
4043 exec2_list,
4044 sizeof(*exec2_list) * args->buffer_count);
4045 if (ret) {
4046 ret = -EFAULT;
4047 DRM_ERROR("failed to copy %d exec entries "
4048 "back to user (%d)\n",
4049 args->buffer_count, ret);
4050 }
4051 }
4052
4053 drm_free_large(exec2_list);
4054 return ret;
4055}
4056
Eric Anholt673a3942008-07-30 12:06:12 -07004057int
4058i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4059{
4060 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004061 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004062 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004063 int ret;
4064
Daniel Vetter778c3542010-05-13 11:49:44 +02004065 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004066 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004067
4068 if (obj_priv->gtt_space != NULL) {
4069 if (alignment == 0)
4070 alignment = i915_gem_get_gtt_alignment(obj);
4071 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004072 WARN(obj_priv->pin_count,
Joe Perchesfce7d612010-10-30 21:08:30 +00004073 "bo is already pinned with incorrect alignment: offset=%x, req.alignment=%x\n",
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004074 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004075 ret = i915_gem_object_unbind(obj);
4076 if (ret)
4077 return ret;
4078 }
4079 }
4080
Eric Anholt673a3942008-07-30 12:06:12 -07004081 if (obj_priv->gtt_space == NULL) {
4082 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004083 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004084 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004085 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004086
Eric Anholt673a3942008-07-30 12:06:12 -07004087 obj_priv->pin_count++;
4088
4089 /* If the object is not active and not pending a flush,
4090 * remove it from the inactive list
4091 */
4092 if (obj_priv->pin_count == 1) {
Chris Wilson73aa8082010-09-30 11:46:12 +01004093 i915_gem_info_add_pin(dev_priv, obj->size);
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,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004096 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004097 }
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 return 0;
4101}
4102
4103void
4104i915_gem_object_unpin(struct drm_gem_object *obj)
4105{
4106 struct drm_device *dev = obj->dev;
4107 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004108 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004109
Chris Wilson23bc5982010-09-29 16:10:57 +01004110 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004111 obj_priv->pin_count--;
4112 BUG_ON(obj_priv->pin_count < 0);
4113 BUG_ON(obj_priv->gtt_space == NULL);
4114
4115 /* If the object is no longer pinned, and is
4116 * neither active nor being flushed, then stick it on
4117 * the inactive list
4118 */
4119 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004120 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004121 list_move_tail(&obj_priv->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004122 &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01004123 i915_gem_info_remove_pin(dev_priv, obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07004124 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004125 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004126}
4127
4128int
4129i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4130 struct drm_file *file_priv)
4131{
4132 struct drm_i915_gem_pin *args = data;
4133 struct drm_gem_object *obj;
4134 struct drm_i915_gem_object *obj_priv;
4135 int ret;
4136
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004137 ret = i915_mutex_lock_interruptible(dev);
4138 if (ret)
4139 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004140
4141 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4142 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004143 ret = -ENOENT;
4144 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004145 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004146 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004147
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004148 if (obj_priv->madv != I915_MADV_WILLNEED) {
4149 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004150 ret = -EINVAL;
4151 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004152 }
4153
Jesse Barnes79e53942008-11-07 14:24:08 -08004154 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4155 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4156 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004157 ret = -EINVAL;
4158 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004159 }
4160
4161 obj_priv->user_pin_count++;
4162 obj_priv->pin_filp = file_priv;
4163 if (obj_priv->user_pin_count == 1) {
4164 ret = i915_gem_object_pin(obj, args->alignment);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004165 if (ret)
4166 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004167 }
4168
4169 /* XXX - flush the CPU caches for pinned objects
4170 * as the X server doesn't manage domains yet
4171 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004172 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004173 args->offset = obj_priv->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004174out:
Eric Anholt673a3942008-07-30 12:06:12 -07004175 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004176unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004177 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004178 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004179}
4180
4181int
4182i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4183 struct drm_file *file_priv)
4184{
4185 struct drm_i915_gem_pin *args = data;
4186 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004187 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004188 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004189
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004190 ret = i915_mutex_lock_interruptible(dev);
4191 if (ret)
4192 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004193
4194 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4195 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004196 ret = -ENOENT;
4197 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004198 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004199 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004200
Jesse Barnes79e53942008-11-07 14:24:08 -08004201 if (obj_priv->pin_filp != file_priv) {
4202 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4203 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004204 ret = -EINVAL;
4205 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004206 }
4207 obj_priv->user_pin_count--;
4208 if (obj_priv->user_pin_count == 0) {
4209 obj_priv->pin_filp = NULL;
4210 i915_gem_object_unpin(obj);
4211 }
Eric Anholt673a3942008-07-30 12:06:12 -07004212
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004213out:
Eric Anholt673a3942008-07-30 12:06:12 -07004214 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004215unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004216 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004217 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004218}
4219
4220int
4221i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4222 struct drm_file *file_priv)
4223{
4224 struct drm_i915_gem_busy *args = data;
4225 struct drm_gem_object *obj;
4226 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004227 int ret;
4228
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004229 ret = i915_mutex_lock_interruptible(dev);
4230 if (ret)
4231 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004232
Eric Anholt673a3942008-07-30 12:06:12 -07004233 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4234 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004235 ret = -ENOENT;
4236 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004237 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004238 obj_priv = to_intel_bo(obj);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004239
Chris Wilson0be555b2010-08-04 15:36:30 +01004240 /* Count all active objects as busy, even if they are currently not used
4241 * by the gpu. Users of this interface expect objects to eventually
4242 * become non-busy without any further actions, therefore emit any
4243 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004244 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004245 args->busy = obj_priv->active;
4246 if (args->busy) {
4247 /* Unconditionally flush objects, even when the gpu still uses this
4248 * object. Userspace calling this function indicates that it wants to
4249 * use this buffer rather sooner than later, so issuing the required
4250 * flush earlier is beneficial.
4251 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004252 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4253 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004254 obj_priv->ring,
4255 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004256
4257 /* Update the active list for the hardware's current position.
4258 * Otherwise this only updates on a delayed timer or when irqs
4259 * are actually unmasked, and our working set ends up being
4260 * larger than required.
4261 */
4262 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4263
4264 args->busy = obj_priv->active;
4265 }
Eric Anholt673a3942008-07-30 12:06:12 -07004266
4267 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004268unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004269 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004270 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004271}
4272
4273int
4274i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4275 struct drm_file *file_priv)
4276{
4277 return i915_gem_ring_throttle(dev, file_priv);
4278}
4279
Chris Wilson3ef94da2009-09-14 16:50:29 +01004280int
4281i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4282 struct drm_file *file_priv)
4283{
4284 struct drm_i915_gem_madvise *args = data;
4285 struct drm_gem_object *obj;
4286 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004287 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004288
4289 switch (args->madv) {
4290 case I915_MADV_DONTNEED:
4291 case I915_MADV_WILLNEED:
4292 break;
4293 default:
4294 return -EINVAL;
4295 }
4296
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004297 ret = i915_mutex_lock_interruptible(dev);
4298 if (ret)
4299 return ret;
4300
Chris Wilson3ef94da2009-09-14 16:50:29 +01004301 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4302 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004303 ret = -ENOENT;
4304 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004305 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004306 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004307
4308 if (obj_priv->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004309 ret = -EINVAL;
4310 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004311 }
4312
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004313 if (obj_priv->madv != __I915_MADV_PURGED)
4314 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004315
Chris Wilson2d7ef392009-09-20 23:13:10 +01004316 /* if the object is no longer bound, discard its backing storage */
4317 if (i915_gem_object_is_purgeable(obj_priv) &&
4318 obj_priv->gtt_space == NULL)
4319 i915_gem_object_truncate(obj);
4320
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004321 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4322
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004323out:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004324 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004325unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004326 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004327 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004328}
4329
Daniel Vetterac52bc52010-04-09 19:05:06 +00004330struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4331 size_t size)
4332{
Chris Wilson73aa8082010-09-30 11:46:12 +01004333 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004334 struct drm_i915_gem_object *obj;
4335
4336 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4337 if (obj == NULL)
4338 return NULL;
4339
4340 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4341 kfree(obj);
4342 return NULL;
4343 }
4344
Chris Wilson73aa8082010-09-30 11:46:12 +01004345 i915_gem_info_add_obj(dev_priv, size);
4346
Daniel Vetterc397b902010-04-09 19:05:07 +00004347 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4348 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4349
4350 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004351 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004352 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004353 INIT_LIST_HEAD(&obj->mm_list);
4354 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004355 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004356 obj->madv = I915_MADV_WILLNEED;
4357
Daniel Vetterc397b902010-04-09 19:05:07 +00004358 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004359}
4360
Eric Anholt673a3942008-07-30 12:06:12 -07004361int i915_gem_init_object(struct drm_gem_object *obj)
4362{
Daniel Vetterc397b902010-04-09 19:05:07 +00004363 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004364
Eric Anholt673a3942008-07-30 12:06:12 -07004365 return 0;
4366}
4367
Chris Wilsonbe726152010-07-23 23:18:50 +01004368static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4369{
4370 struct drm_device *dev = obj->dev;
4371 drm_i915_private_t *dev_priv = dev->dev_private;
4372 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4373 int ret;
4374
4375 ret = i915_gem_object_unbind(obj);
4376 if (ret == -ERESTARTSYS) {
Chris Wilson69dc4982010-10-19 10:36:51 +01004377 list_move(&obj_priv->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004378 &dev_priv->mm.deferred_free_list);
4379 return;
4380 }
4381
4382 if (obj_priv->mmap_offset)
4383 i915_gem_free_mmap_offset(obj);
4384
4385 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004386 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004387
4388 kfree(obj_priv->page_cpu_valid);
4389 kfree(obj_priv->bit_17);
4390 kfree(obj_priv);
4391}
4392
Eric Anholt673a3942008-07-30 12:06:12 -07004393void i915_gem_free_object(struct drm_gem_object *obj)
4394{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004395 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004396 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004397
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004398 trace_i915_gem_object_destroy(obj);
4399
Eric Anholt673a3942008-07-30 12:06:12 -07004400 while (obj_priv->pin_count > 0)
4401 i915_gem_object_unpin(obj);
4402
Dave Airlie71acb5e2008-12-30 20:31:46 +10004403 if (obj_priv->phys_obj)
4404 i915_gem_detach_phys_object(dev, obj);
4405
Chris Wilsonbe726152010-07-23 23:18:50 +01004406 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004407}
4408
Jesse Barnes5669fca2009-02-17 15:13:31 -08004409int
Eric Anholt673a3942008-07-30 12:06:12 -07004410i915_gem_idle(struct drm_device *dev)
4411{
4412 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004413 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004414
Keith Packard6dbe2772008-10-14 21:41:13 -07004415 mutex_lock(&dev->struct_mutex);
4416
Chris Wilson87acb0a2010-10-19 10:13:00 +01004417 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004418 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004419 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004420 }
Eric Anholt673a3942008-07-30 12:06:12 -07004421
Chris Wilson29105cc2010-01-07 10:39:13 +00004422 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004423 if (ret) {
4424 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004425 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004426 }
Eric Anholt673a3942008-07-30 12:06:12 -07004427
Chris Wilson29105cc2010-01-07 10:39:13 +00004428 /* Under UMS, be paranoid and evict. */
4429 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004430 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004431 if (ret) {
4432 mutex_unlock(&dev->struct_mutex);
4433 return ret;
4434 }
4435 }
4436
4437 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4438 * We need to replace this with a semaphore, or something.
4439 * And not confound mm.suspended!
4440 */
4441 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004442 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004443
4444 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004445 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004446
Keith Packard6dbe2772008-10-14 21:41:13 -07004447 mutex_unlock(&dev->struct_mutex);
4448
Chris Wilson29105cc2010-01-07 10:39:13 +00004449 /* Cancel the retire work handler, which should be idle now. */
4450 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4451
Eric Anholt673a3942008-07-30 12:06:12 -07004452 return 0;
4453}
4454
Jesse Barnese552eb72010-04-21 11:39:23 -07004455/*
4456 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4457 * over cache flushing.
4458 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004459static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004460i915_gem_init_pipe_control(struct drm_device *dev)
4461{
4462 drm_i915_private_t *dev_priv = dev->dev_private;
4463 struct drm_gem_object *obj;
4464 struct drm_i915_gem_object *obj_priv;
4465 int ret;
4466
Eric Anholt34dc4d42010-05-07 14:30:03 -07004467 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004468 if (obj == NULL) {
4469 DRM_ERROR("Failed to allocate seqno page\n");
4470 ret = -ENOMEM;
4471 goto err;
4472 }
4473 obj_priv = to_intel_bo(obj);
4474 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4475
4476 ret = i915_gem_object_pin(obj, 4096);
4477 if (ret)
4478 goto err_unref;
4479
4480 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4481 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4482 if (dev_priv->seqno_page == NULL)
4483 goto err_unpin;
4484
4485 dev_priv->seqno_obj = obj;
4486 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4487
4488 return 0;
4489
4490err_unpin:
4491 i915_gem_object_unpin(obj);
4492err_unref:
4493 drm_gem_object_unreference(obj);
4494err:
4495 return ret;
4496}
4497
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004498
4499static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004500i915_gem_cleanup_pipe_control(struct drm_device *dev)
4501{
4502 drm_i915_private_t *dev_priv = dev->dev_private;
4503 struct drm_gem_object *obj;
4504 struct drm_i915_gem_object *obj_priv;
4505
4506 obj = dev_priv->seqno_obj;
4507 obj_priv = to_intel_bo(obj);
4508 kunmap(obj_priv->pages[0]);
4509 i915_gem_object_unpin(obj);
4510 drm_gem_object_unreference(obj);
4511 dev_priv->seqno_obj = NULL;
4512
4513 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004514}
4515
Eric Anholt673a3942008-07-30 12:06:12 -07004516int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004517i915_gem_init_ringbuffer(struct drm_device *dev)
4518{
4519 drm_i915_private_t *dev_priv = dev->dev_private;
4520 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004521
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004522 if (HAS_PIPE_CONTROL(dev)) {
4523 ret = i915_gem_init_pipe_control(dev);
4524 if (ret)
4525 return ret;
4526 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004527
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004528 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004529 if (ret)
4530 goto cleanup_pipe_control;
4531
4532 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004533 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004534 if (ret)
4535 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004536 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004537
Chris Wilson549f7362010-10-19 11:19:32 +01004538 if (HAS_BLT(dev)) {
4539 ret = intel_init_blt_ring_buffer(dev);
4540 if (ret)
4541 goto cleanup_bsd_ring;
4542 }
4543
Chris Wilson6f392d52010-08-07 11:01:22 +01004544 dev_priv->next_seqno = 1;
4545
Chris Wilson68f95ba2010-05-27 13:18:22 +01004546 return 0;
4547
Chris Wilson549f7362010-10-19 11:19:32 +01004548cleanup_bsd_ring:
4549 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004550cleanup_render_ring:
4551 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4552cleanup_pipe_control:
4553 if (HAS_PIPE_CONTROL(dev))
4554 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004555 return ret;
4556}
4557
4558void
4559i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4560{
4561 drm_i915_private_t *dev_priv = dev->dev_private;
4562
4563 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01004564 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01004565 intel_cleanup_ring_buffer(dev, &dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004566 if (HAS_PIPE_CONTROL(dev))
4567 i915_gem_cleanup_pipe_control(dev);
4568}
4569
4570int
Eric Anholt673a3942008-07-30 12:06:12 -07004571i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4572 struct drm_file *file_priv)
4573{
4574 drm_i915_private_t *dev_priv = dev->dev_private;
4575 int ret;
4576
Jesse Barnes79e53942008-11-07 14:24:08 -08004577 if (drm_core_check_feature(dev, DRIVER_MODESET))
4578 return 0;
4579
Ben Gamariba1234d2009-09-14 17:48:47 -04004580 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004581 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004582 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004583 }
4584
Eric Anholt673a3942008-07-30 12:06:12 -07004585 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004586 dev_priv->mm.suspended = 0;
4587
4588 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004589 if (ret != 0) {
4590 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004591 return ret;
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004592 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004593
Chris Wilson69dc4982010-10-19 10:36:51 +01004594 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004595 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004596 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004597 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004598 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4599 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004600 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004601 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004602 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004603 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004604
Chris Wilson5f353082010-06-07 14:03:03 +01004605 ret = drm_irq_install(dev);
4606 if (ret)
4607 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004608
Eric Anholt673a3942008-07-30 12:06:12 -07004609 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004610
4611cleanup_ringbuffer:
4612 mutex_lock(&dev->struct_mutex);
4613 i915_gem_cleanup_ringbuffer(dev);
4614 dev_priv->mm.suspended = 1;
4615 mutex_unlock(&dev->struct_mutex);
4616
4617 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004618}
4619
4620int
4621i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4622 struct drm_file *file_priv)
4623{
Jesse Barnes79e53942008-11-07 14:24:08 -08004624 if (drm_core_check_feature(dev, DRIVER_MODESET))
4625 return 0;
4626
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004627 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004628 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004629}
4630
4631void
4632i915_gem_lastclose(struct drm_device *dev)
4633{
4634 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004635
Eric Anholte806b492009-01-22 09:56:58 -08004636 if (drm_core_check_feature(dev, DRIVER_MODESET))
4637 return;
4638
Keith Packard6dbe2772008-10-14 21:41:13 -07004639 ret = i915_gem_idle(dev);
4640 if (ret)
4641 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004642}
4643
Chris Wilson64193402010-10-24 12:38:05 +01004644static void
4645init_ring_lists(struct intel_ring_buffer *ring)
4646{
4647 INIT_LIST_HEAD(&ring->active_list);
4648 INIT_LIST_HEAD(&ring->request_list);
4649 INIT_LIST_HEAD(&ring->gpu_write_list);
4650}
4651
Eric Anholt673a3942008-07-30 12:06:12 -07004652void
4653i915_gem_load(struct drm_device *dev)
4654{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004655 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004656 drm_i915_private_t *dev_priv = dev->dev_private;
4657
Chris Wilson69dc4982010-10-19 10:36:51 +01004658 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004659 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4660 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004661 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004662 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004663 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Chris Wilson64193402010-10-24 12:38:05 +01004664 init_ring_lists(&dev_priv->render_ring);
4665 init_ring_lists(&dev_priv->bsd_ring);
4666 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004667 for (i = 0; i < 16; i++)
4668 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004669 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4670 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004671 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004672 spin_lock(&shrink_list_lock);
4673 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4674 spin_unlock(&shrink_list_lock);
4675
Dave Airlie94400122010-07-20 13:15:31 +10004676 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4677 if (IS_GEN3(dev)) {
4678 u32 tmp = I915_READ(MI_ARB_STATE);
4679 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4680 /* arb state is a masked write, so set bit + bit in mask */
4681 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4682 I915_WRITE(MI_ARB_STATE, tmp);
4683 }
4684 }
4685
Jesse Barnesde151cf2008-11-12 10:03:55 -08004686 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004687 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4688 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004689
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004690 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004691 dev_priv->num_fence_regs = 16;
4692 else
4693 dev_priv->num_fence_regs = 8;
4694
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004695 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004696 switch (INTEL_INFO(dev)->gen) {
4697 case 6:
4698 for (i = 0; i < 16; i++)
4699 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4700 break;
4701 case 5:
4702 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004703 for (i = 0; i < 16; i++)
4704 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004705 break;
4706 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004707 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4708 for (i = 0; i < 8; i++)
4709 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004710 case 2:
4711 for (i = 0; i < 8; i++)
4712 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4713 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004714 }
Eric Anholt673a3942008-07-30 12:06:12 -07004715 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004716 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004717}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004718
4719/*
4720 * Create a physically contiguous memory object for this object
4721 * e.g. for cursor + overlay regs
4722 */
Chris Wilson995b67622010-08-20 13:23:26 +01004723static int i915_gem_init_phys_object(struct drm_device *dev,
4724 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004725{
4726 drm_i915_private_t *dev_priv = dev->dev_private;
4727 struct drm_i915_gem_phys_object *phys_obj;
4728 int ret;
4729
4730 if (dev_priv->mm.phys_objs[id - 1] || !size)
4731 return 0;
4732
Eric Anholt9a298b22009-03-24 12:23:04 -07004733 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004734 if (!phys_obj)
4735 return -ENOMEM;
4736
4737 phys_obj->id = id;
4738
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004739 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004740 if (!phys_obj->handle) {
4741 ret = -ENOMEM;
4742 goto kfree_obj;
4743 }
4744#ifdef CONFIG_X86
4745 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4746#endif
4747
4748 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4749
4750 return 0;
4751kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004752 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004753 return ret;
4754}
4755
Chris Wilson995b67622010-08-20 13:23:26 +01004756static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004757{
4758 drm_i915_private_t *dev_priv = dev->dev_private;
4759 struct drm_i915_gem_phys_object *phys_obj;
4760
4761 if (!dev_priv->mm.phys_objs[id - 1])
4762 return;
4763
4764 phys_obj = dev_priv->mm.phys_objs[id - 1];
4765 if (phys_obj->cur_obj) {
4766 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4767 }
4768
4769#ifdef CONFIG_X86
4770 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4771#endif
4772 drm_pci_free(dev, phys_obj->handle);
4773 kfree(phys_obj);
4774 dev_priv->mm.phys_objs[id - 1] = NULL;
4775}
4776
4777void i915_gem_free_all_phys_object(struct drm_device *dev)
4778{
4779 int i;
4780
Dave Airlie260883c2009-01-22 17:58:49 +10004781 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004782 i915_gem_free_phys_object(dev, i);
4783}
4784
4785void i915_gem_detach_phys_object(struct drm_device *dev,
4786 struct drm_gem_object *obj)
4787{
4788 struct drm_i915_gem_object *obj_priv;
4789 int i;
4790 int ret;
4791 int page_count;
4792
Daniel Vetter23010e42010-03-08 13:35:02 +01004793 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004794 if (!obj_priv->phys_obj)
4795 return;
4796
Chris Wilson4bdadb92010-01-27 13:36:32 +00004797 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004798 if (ret)
4799 goto out;
4800
4801 page_count = obj->size / PAGE_SIZE;
4802
4803 for (i = 0; i < page_count; i++) {
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004804 char *dst = kmap_atomic(obj_priv->pages[i]);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004805 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4806
4807 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004808 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004809 }
Eric Anholt856fa192009-03-19 14:10:50 -07004810 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004811 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004812
4813 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004814out:
4815 obj_priv->phys_obj->cur_obj = NULL;
4816 obj_priv->phys_obj = NULL;
4817}
4818
4819int
4820i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004821 struct drm_gem_object *obj,
4822 int id,
4823 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004824{
4825 drm_i915_private_t *dev_priv = dev->dev_private;
4826 struct drm_i915_gem_object *obj_priv;
4827 int ret = 0;
4828 int page_count;
4829 int i;
4830
4831 if (id > I915_MAX_PHYS_OBJECT)
4832 return -EINVAL;
4833
Daniel Vetter23010e42010-03-08 13:35:02 +01004834 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004835
4836 if (obj_priv->phys_obj) {
4837 if (obj_priv->phys_obj->id == id)
4838 return 0;
4839 i915_gem_detach_phys_object(dev, obj);
4840 }
4841
Dave Airlie71acb5e2008-12-30 20:31:46 +10004842 /* create a new object */
4843 if (!dev_priv->mm.phys_objs[id - 1]) {
4844 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004845 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004846 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004847 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004848 goto out;
4849 }
4850 }
4851
4852 /* bind to the object */
4853 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4854 obj_priv->phys_obj->cur_obj = obj;
4855
Chris Wilson4bdadb92010-01-27 13:36:32 +00004856 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004857 if (ret) {
4858 DRM_ERROR("failed to get page list\n");
4859 goto out;
4860 }
4861
4862 page_count = obj->size / PAGE_SIZE;
4863
4864 for (i = 0; i < page_count; i++) {
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004865 char *src = kmap_atomic(obj_priv->pages[i]);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004866 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4867
4868 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004869 kunmap_atomic(src);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004870 }
4871
Chris Wilsond78b47b2009-06-17 21:52:49 +01004872 i915_gem_object_put_pages(obj);
4873
Dave Airlie71acb5e2008-12-30 20:31:46 +10004874 return 0;
4875out:
4876 return ret;
4877}
4878
4879static int
4880i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4881 struct drm_i915_gem_pwrite *args,
4882 struct drm_file *file_priv)
4883{
Daniel Vetter23010e42010-03-08 13:35:02 +01004884 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004885 void *vaddr = obj_priv->phys_obj->handle->vaddr + args->offset;
4886 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004887
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004888 DRM_DEBUG_DRIVER("vaddr %p, %lld\n", vaddr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004889
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004890 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4891 unsigned long unwritten;
4892
4893 /* The physical object once assigned is fixed for the lifetime
4894 * of the obj, so we can safely drop the lock and continue
4895 * to access vaddr.
4896 */
4897 mutex_unlock(&dev->struct_mutex);
4898 unwritten = copy_from_user(vaddr, user_data, args->size);
4899 mutex_lock(&dev->struct_mutex);
4900 if (unwritten)
4901 return -EFAULT;
4902 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004903
4904 drm_agp_chipset_flush(dev);
4905 return 0;
4906}
Eric Anholtb9624422009-06-03 07:27:35 +00004907
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004908void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004909{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004910 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004911
4912 /* Clean up our request list when the client is going away, so that
4913 * later retire_requests won't dereference our soon-to-be-gone
4914 * file_priv.
4915 */
Chris Wilson1c255952010-09-26 11:03:27 +01004916 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004917 while (!list_empty(&file_priv->mm.request_list)) {
4918 struct drm_i915_gem_request *request;
4919
4920 request = list_first_entry(&file_priv->mm.request_list,
4921 struct drm_i915_gem_request,
4922 client_list);
4923 list_del(&request->client_list);
4924 request->file_priv = NULL;
4925 }
Chris Wilson1c255952010-09-26 11:03:27 +01004926 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004927}
Chris Wilson31169712009-09-14 16:50:28 +01004928
Chris Wilson31169712009-09-14 16:50:28 +01004929static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004930i915_gpu_is_active(struct drm_device *dev)
4931{
4932 drm_i915_private_t *dev_priv = dev->dev_private;
4933 int lists_empty;
4934
Chris Wilson1637ef42010-04-20 17:10:35 +01004935 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01004936 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004937
4938 return !lists_empty;
4939}
4940
4941static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004942i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004943{
4944 drm_i915_private_t *dev_priv, *next_dev;
4945 struct drm_i915_gem_object *obj_priv, *next_obj;
4946 int cnt = 0;
4947 int would_deadlock = 1;
4948
4949 /* "fast-path" to count number of available objects */
4950 if (nr_to_scan == 0) {
4951 spin_lock(&shrink_list_lock);
4952 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4953 struct drm_device *dev = dev_priv->dev;
4954
4955 if (mutex_trylock(&dev->struct_mutex)) {
4956 list_for_each_entry(obj_priv,
4957 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01004958 mm_list)
Chris Wilson31169712009-09-14 16:50:28 +01004959 cnt++;
4960 mutex_unlock(&dev->struct_mutex);
4961 }
4962 }
4963 spin_unlock(&shrink_list_lock);
4964
4965 return (cnt / 100) * sysctl_vfs_cache_pressure;
4966 }
4967
4968 spin_lock(&shrink_list_lock);
4969
Chris Wilson1637ef42010-04-20 17:10:35 +01004970rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004971 /* first scan for clean buffers */
4972 list_for_each_entry_safe(dev_priv, next_dev,
4973 &shrink_list, mm.shrink_list) {
4974 struct drm_device *dev = dev_priv->dev;
4975
4976 if (! mutex_trylock(&dev->struct_mutex))
4977 continue;
4978
4979 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004980 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004981
Chris Wilson31169712009-09-14 16:50:28 +01004982 list_for_each_entry_safe(obj_priv, next_obj,
4983 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01004984 mm_list) {
Chris Wilson31169712009-09-14 16:50:28 +01004985 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004986 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004987 if (--nr_to_scan <= 0)
4988 break;
4989 }
4990 }
4991
4992 spin_lock(&shrink_list_lock);
4993 mutex_unlock(&dev->struct_mutex);
4994
Chris Wilson963b4832009-09-20 23:03:54 +01004995 would_deadlock = 0;
4996
Chris Wilson31169712009-09-14 16:50:28 +01004997 if (nr_to_scan <= 0)
4998 break;
4999 }
5000
5001 /* second pass, evict/count anything still on the inactive list */
5002 list_for_each_entry_safe(dev_priv, next_dev,
5003 &shrink_list, mm.shrink_list) {
5004 struct drm_device *dev = dev_priv->dev;
5005
5006 if (! mutex_trylock(&dev->struct_mutex))
5007 continue;
5008
5009 spin_unlock(&shrink_list_lock);
5010
5011 list_for_each_entry_safe(obj_priv, next_obj,
5012 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01005013 mm_list) {
Chris Wilson31169712009-09-14 16:50:28 +01005014 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005015 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005016 nr_to_scan--;
5017 } else
5018 cnt++;
5019 }
5020
5021 spin_lock(&shrink_list_lock);
5022 mutex_unlock(&dev->struct_mutex);
5023
5024 would_deadlock = 0;
5025 }
5026
Chris Wilson1637ef42010-04-20 17:10:35 +01005027 if (nr_to_scan) {
5028 int active = 0;
5029
5030 /*
5031 * We are desperate for pages, so as a last resort, wait
5032 * for the GPU to finish and discard whatever we can.
5033 * This has a dramatic impact to reduce the number of
5034 * OOM-killer events whilst running the GPU aggressively.
5035 */
5036 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5037 struct drm_device *dev = dev_priv->dev;
5038
5039 if (!mutex_trylock(&dev->struct_mutex))
5040 continue;
5041
5042 spin_unlock(&shrink_list_lock);
5043
5044 if (i915_gpu_is_active(dev)) {
5045 i915_gpu_idle(dev);
5046 active++;
5047 }
5048
5049 spin_lock(&shrink_list_lock);
5050 mutex_unlock(&dev->struct_mutex);
5051 }
5052
5053 if (active)
5054 goto rescan;
5055 }
5056
Chris Wilson31169712009-09-14 16:50:28 +01005057 spin_unlock(&shrink_list_lock);
5058
5059 if (would_deadlock)
5060 return -1;
5061 else if (cnt > 0)
5062 return (cnt / 100) * sysctl_vfs_cache_pressure;
5063 else
5064 return 0;
5065}
5066
5067static struct shrinker shrinker = {
5068 .shrink = i915_gem_shrink,
5069 .seeks = DEFAULT_SEEKS,
5070};
5071
5072__init void
5073i915_gem_shrinker_init(void)
5074{
5075 register_shrinker(&shrinker);
5076}
5077
5078__exit void
5079i915_gem_shrinker_exit(void)
5080{
5081 unregister_shrinker(&shrinker);
5082}