blob: 1177ff5779141b07e36223b3682234b1753f967c [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{
Florian Mickler2bc43b52009-04-06 22:55:41 +0200268 int unwritten;
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100269 char *vaddr;
Eric Anholteb014592009-03-10 11:44:52 -0700270
271 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
Florian Mickler2bc43b52009-04-06 22:55:41 +0200272 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700273 kunmap_atomic(vaddr, KM_USER0);
274
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100275 return unwritten ? -EFAULT : 0;
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;
369 int ret;
370
371 user_data = (char __user *) (uintptr_t) args->data_ptr;
372 remain = args->size;
373
Chris Wilson76c1dec2010-09-25 11:22:51 +0100374 ret = i915_mutex_lock_interruptible(dev);
375 if (ret)
376 return ret;
Eric Anholteb014592009-03-10 11:44:52 -0700377
Chris Wilson4bdadb92010-01-27 13:36:32 +0000378 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700379 if (ret != 0)
380 goto fail_unlock;
381
382 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
383 args->size);
384 if (ret != 0)
385 goto fail_put_pages;
386
Daniel Vetter23010e42010-03-08 13:35:02 +0100387 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700388 offset = args->offset;
389
390 while (remain > 0) {
391 /* Operation in this page
392 *
393 * page_base = page offset within aperture
394 * page_offset = offset within page
395 * page_length = bytes to copy for this page
396 */
397 page_base = (offset & ~(PAGE_SIZE-1));
398 page_offset = offset & (PAGE_SIZE-1);
399 page_length = remain;
400 if ((page_offset + remain) > PAGE_SIZE)
401 page_length = PAGE_SIZE - page_offset;
402
403 ret = fast_shmem_read(obj_priv->pages,
404 page_base, page_offset,
405 user_data, page_length);
406 if (ret)
407 goto fail_put_pages;
408
409 remain -= page_length;
410 user_data += page_length;
411 offset += page_length;
412 }
413
414fail_put_pages:
415 i915_gem_object_put_pages(obj);
416fail_unlock:
417 mutex_unlock(&dev->struct_mutex);
418
419 return ret;
420}
421
Chris Wilson07f73f62009-09-14 16:50:30 +0100422static int
423i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
424{
425 int ret;
426
Chris Wilson4bdadb92010-01-27 13:36:32 +0000427 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100428
429 /* If we've insufficient memory to map in the pages, attempt
430 * to make some space by throwing out some old buffers.
431 */
432 if (ret == -ENOMEM) {
433 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100434
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100435 ret = i915_gem_evict_something(dev, obj->size,
436 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100437 if (ret)
438 return ret;
439
Chris Wilson4bdadb92010-01-27 13:36:32 +0000440 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100441 }
442
443 return ret;
444}
445
Eric Anholteb014592009-03-10 11:44:52 -0700446/**
447 * This is the fallback shmem pread path, which allocates temporary storage
448 * in kernel space to copy_to_user into outside of the struct_mutex, so we
449 * can copy out of the object's backing pages while holding the struct mutex
450 * and not take page faults.
451 */
452static int
453i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
454 struct drm_i915_gem_pread *args,
455 struct drm_file *file_priv)
456{
Daniel Vetter23010e42010-03-08 13:35:02 +0100457 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700458 struct mm_struct *mm = current->mm;
459 struct page **user_pages;
460 ssize_t remain;
461 loff_t offset, pinned_pages, i;
462 loff_t first_data_page, last_data_page, num_pages;
463 int shmem_page_index, shmem_page_offset;
464 int data_page_index, data_page_offset;
465 int page_length;
466 int ret;
467 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700468 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700469
470 remain = args->size;
471
472 /* Pin the user pages containing the data. We can't fault while
473 * holding the struct mutex, yet we want to hold it while
474 * dereferencing the user data.
475 */
476 first_data_page = data_ptr / PAGE_SIZE;
477 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
478 num_pages = last_data_page - first_data_page + 1;
479
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700480 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700481 if (user_pages == NULL)
482 return -ENOMEM;
483
484 down_read(&mm->mmap_sem);
485 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700486 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700487 up_read(&mm->mmap_sem);
488 if (pinned_pages < num_pages) {
489 ret = -EFAULT;
490 goto fail_put_user_pages;
491 }
492
Eric Anholt280b7132009-03-12 16:56:27 -0700493 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
494
Chris Wilson76c1dec2010-09-25 11:22:51 +0100495 ret = i915_mutex_lock_interruptible(dev);
496 if (ret)
497 goto fail_put_user_pages;
Eric Anholteb014592009-03-10 11:44:52 -0700498
Chris Wilson07f73f62009-09-14 16:50:30 +0100499 ret = i915_gem_object_get_pages_or_evict(obj);
500 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700501 goto fail_unlock;
502
503 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
504 args->size);
505 if (ret != 0)
506 goto fail_put_pages;
507
Daniel Vetter23010e42010-03-08 13:35:02 +0100508 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700509 offset = args->offset;
510
511 while (remain > 0) {
512 /* Operation in this page
513 *
514 * shmem_page_index = page number within shmem file
515 * shmem_page_offset = offset within page in shmem file
516 * data_page_index = page number in get_user_pages return
517 * data_page_offset = offset with data_page_index page.
518 * page_length = bytes to copy for this page
519 */
520 shmem_page_index = offset / PAGE_SIZE;
521 shmem_page_offset = offset & ~PAGE_MASK;
522 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
523 data_page_offset = data_ptr & ~PAGE_MASK;
524
525 page_length = remain;
526 if ((shmem_page_offset + page_length) > PAGE_SIZE)
527 page_length = PAGE_SIZE - shmem_page_offset;
528 if ((data_page_offset + page_length) > PAGE_SIZE)
529 page_length = PAGE_SIZE - data_page_offset;
530
Eric Anholt280b7132009-03-12 16:56:27 -0700531 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100532 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700533 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100534 user_pages[data_page_index],
535 data_page_offset,
536 page_length,
537 1);
538 } else {
539 slow_shmem_copy(user_pages[data_page_index],
540 data_page_offset,
541 obj_priv->pages[shmem_page_index],
542 shmem_page_offset,
543 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700544 }
Eric Anholteb014592009-03-10 11:44:52 -0700545
546 remain -= page_length;
547 data_ptr += page_length;
548 offset += page_length;
549 }
550
551fail_put_pages:
552 i915_gem_object_put_pages(obj);
553fail_unlock:
554 mutex_unlock(&dev->struct_mutex);
555fail_put_user_pages:
556 for (i = 0; i < pinned_pages; i++) {
557 SetPageDirty(user_pages[i]);
558 page_cache_release(user_pages[i]);
559 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700560 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700561
562 return ret;
563}
564
Eric Anholt673a3942008-07-30 12:06:12 -0700565/**
566 * Reads data from the object referenced by handle.
567 *
568 * On error, the contents of *data are undefined.
569 */
570int
571i915_gem_pread_ioctl(struct drm_device *dev, void *data,
572 struct drm_file *file_priv)
573{
574 struct drm_i915_gem_pread *args = data;
575 struct drm_gem_object *obj;
576 struct drm_i915_gem_object *obj_priv;
Chris Wilson35b62a82010-09-26 20:23:38 +0100577 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700578
579 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
580 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100581 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100582 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700583
Chris Wilson7dcd2492010-09-26 20:21:44 +0100584 /* Bounds check source. */
585 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100586 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100587 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100588 }
589
Chris Wilson35b62a82010-09-26 20:23:38 +0100590 if (args->size == 0)
591 goto out;
592
Chris Wilsonce9d4192010-09-26 20:50:05 +0100593 if (!access_ok(VERIFY_WRITE,
594 (char __user *)(uintptr_t)args->data_ptr,
595 args->size)) {
596 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +0100597 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -0700598 }
599
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100600 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
601 args->size);
602 if (ret) {
603 ret = -EFAULT;
604 goto out;
605 }
606
Eric Anholt280b7132009-03-12 16:56:27 -0700607 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700608 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700609 } else {
610 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
611 if (ret != 0)
612 ret = i915_gem_shmem_pread_slow(dev, obj, args,
613 file_priv);
614 }
Eric Anholt673a3942008-07-30 12:06:12 -0700615
Chris Wilson35b62a82010-09-26 20:23:38 +0100616out:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000617 drm_gem_object_unreference_unlocked(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700618 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700619}
620
Keith Packard0839ccb2008-10-30 19:38:48 -0700621/* This is the fast write path which cannot handle
622 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700623 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700624
Keith Packard0839ccb2008-10-30 19:38:48 -0700625static inline int
626fast_user_write(struct io_mapping *mapping,
627 loff_t page_base, int page_offset,
628 char __user *user_data,
629 int length)
630{
631 char *vaddr_atomic;
632 unsigned long unwritten;
633
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100634 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700635 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
636 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100637 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100638 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700639}
640
641/* Here's the write path which can sleep for
642 * page faults
643 */
644
Chris Wilsonab34c222010-05-27 14:15:35 +0100645static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700646slow_kernel_write(struct io_mapping *mapping,
647 loff_t gtt_base, int gtt_offset,
648 struct page *user_page, int user_offset,
649 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700650{
Chris Wilsonab34c222010-05-27 14:15:35 +0100651 char __iomem *dst_vaddr;
652 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700653
Chris Wilsonab34c222010-05-27 14:15:35 +0100654 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
655 src_vaddr = kmap(user_page);
656
657 memcpy_toio(dst_vaddr + gtt_offset,
658 src_vaddr + user_offset,
659 length);
660
661 kunmap(user_page);
662 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700663}
664
Eric Anholt40123c12009-03-09 13:42:30 -0700665static inline int
666fast_shmem_write(struct page **pages,
667 loff_t page_base, int page_offset,
668 char __user *data,
669 int length)
670{
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100671 char *vaddr;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100672 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700673
674 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100675 ret = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700676 kunmap_atomic(vaddr, KM_USER0);
677
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100678 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700679}
680
Eric Anholt3de09aa2009-03-09 09:42:23 -0700681/**
682 * This is the fast pwrite path, where we copy the data directly from the
683 * user into the GTT, uncached.
684 */
Eric Anholt673a3942008-07-30 12:06:12 -0700685static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700686i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
687 struct drm_i915_gem_pwrite *args,
688 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700689{
Daniel Vetter23010e42010-03-08 13:35:02 +0100690 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700691 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700692 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700693 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700694 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700695 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700696
697 user_data = (char __user *) (uintptr_t) args->data_ptr;
698 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700699
Daniel Vetter23010e42010-03-08 13:35:02 +0100700 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700701 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700702
703 while (remain > 0) {
704 /* Operation in this page
705 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700706 * page_base = page offset within aperture
707 * page_offset = offset within page
708 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700709 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700710 page_base = (offset & ~(PAGE_SIZE-1));
711 page_offset = offset & (PAGE_SIZE-1);
712 page_length = remain;
713 if ((page_offset + remain) > PAGE_SIZE)
714 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700715
Keith Packard0839ccb2008-10-30 19:38:48 -0700716 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700717 * source page isn't available. Return the error and we'll
718 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700719 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100720 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
721 page_offset, user_data, page_length))
722
723 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700724
Keith Packard0839ccb2008-10-30 19:38:48 -0700725 remain -= page_length;
726 user_data += page_length;
727 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700728 }
Eric Anholt673a3942008-07-30 12:06:12 -0700729
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100730 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700731}
732
Eric Anholt3de09aa2009-03-09 09:42:23 -0700733/**
734 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
735 * the memory and maps it using kmap_atomic for copying.
736 *
737 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
738 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
739 */
Eric Anholt3043c602008-10-02 12:24:47 -0700740static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700741i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
742 struct drm_i915_gem_pwrite *args,
743 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700744{
Daniel Vetter23010e42010-03-08 13:35:02 +0100745 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700746 drm_i915_private_t *dev_priv = dev->dev_private;
747 ssize_t remain;
748 loff_t gtt_page_base, offset;
749 loff_t first_data_page, last_data_page, num_pages;
750 loff_t pinned_pages, i;
751 struct page **user_pages;
752 struct mm_struct *mm = current->mm;
753 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700754 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700755 uint64_t data_ptr = args->data_ptr;
756
757 remain = args->size;
758
759 /* Pin the user pages containing the data. We can't fault while
760 * holding the struct mutex, and all of the pwrite implementations
761 * want to hold it while dereferencing the user data.
762 */
763 first_data_page = data_ptr / PAGE_SIZE;
764 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
765 num_pages = last_data_page - first_data_page + 1;
766
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100767 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700768 if (user_pages == NULL)
769 return -ENOMEM;
770
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100771 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700772 down_read(&mm->mmap_sem);
773 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
774 num_pages, 0, 0, user_pages, NULL);
775 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100776 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700777 if (pinned_pages < num_pages) {
778 ret = -EFAULT;
779 goto out_unpin_pages;
780 }
781
Eric Anholt3de09aa2009-03-09 09:42:23 -0700782 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
783 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100784 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700785
Daniel Vetter23010e42010-03-08 13:35:02 +0100786 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700787 offset = obj_priv->gtt_offset + args->offset;
788
789 while (remain > 0) {
790 /* Operation in this page
791 *
792 * gtt_page_base = page offset within aperture
793 * gtt_page_offset = offset within page in aperture
794 * data_page_index = page number in get_user_pages return
795 * data_page_offset = offset with data_page_index page.
796 * page_length = bytes to copy for this page
797 */
798 gtt_page_base = offset & PAGE_MASK;
799 gtt_page_offset = offset & ~PAGE_MASK;
800 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
801 data_page_offset = data_ptr & ~PAGE_MASK;
802
803 page_length = remain;
804 if ((gtt_page_offset + page_length) > PAGE_SIZE)
805 page_length = PAGE_SIZE - gtt_page_offset;
806 if ((data_page_offset + page_length) > PAGE_SIZE)
807 page_length = PAGE_SIZE - data_page_offset;
808
Chris Wilsonab34c222010-05-27 14:15:35 +0100809 slow_kernel_write(dev_priv->mm.gtt_mapping,
810 gtt_page_base, gtt_page_offset,
811 user_pages[data_page_index],
812 data_page_offset,
813 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700814
815 remain -= page_length;
816 offset += page_length;
817 data_ptr += page_length;
818 }
819
Eric Anholt3de09aa2009-03-09 09:42:23 -0700820out_unpin_pages:
821 for (i = 0; i < pinned_pages; i++)
822 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700823 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700824
825 return ret;
826}
827
Eric Anholt40123c12009-03-09 13:42:30 -0700828/**
829 * This is the fast shmem pwrite path, which attempts to directly
830 * copy_from_user into the kmapped pages backing the object.
831 */
Eric Anholt673a3942008-07-30 12:06:12 -0700832static int
Eric Anholt40123c12009-03-09 13:42:30 -0700833i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
834 struct drm_i915_gem_pwrite *args,
835 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700836{
Daniel Vetter23010e42010-03-08 13:35:02 +0100837 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700838 ssize_t remain;
839 loff_t offset, page_base;
840 char __user *user_data;
841 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700842
843 user_data = (char __user *) (uintptr_t) args->data_ptr;
844 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700845
Daniel Vetter23010e42010-03-08 13:35:02 +0100846 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700847 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700848 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700849
Eric Anholt40123c12009-03-09 13:42:30 -0700850 while (remain > 0) {
851 /* Operation in this page
852 *
853 * page_base = page offset within aperture
854 * page_offset = offset within page
855 * page_length = bytes to copy for this page
856 */
857 page_base = (offset & ~(PAGE_SIZE-1));
858 page_offset = offset & (PAGE_SIZE-1);
859 page_length = remain;
860 if ((page_offset + remain) > PAGE_SIZE)
861 page_length = PAGE_SIZE - page_offset;
862
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100863 if (fast_shmem_write(obj_priv->pages,
Eric Anholt40123c12009-03-09 13:42:30 -0700864 page_base, page_offset,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100865 user_data, page_length))
866 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700867
868 remain -= page_length;
869 user_data += page_length;
870 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700871 }
872
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100873 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700874}
875
876/**
877 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
878 * the memory and maps it using kmap_atomic for copying.
879 *
880 * This avoids taking mmap_sem for faulting on the user's address while the
881 * struct_mutex is held.
882 */
883static int
884i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
885 struct drm_i915_gem_pwrite *args,
886 struct drm_file *file_priv)
887{
Daniel Vetter23010e42010-03-08 13:35:02 +0100888 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700889 struct mm_struct *mm = current->mm;
890 struct page **user_pages;
891 ssize_t remain;
892 loff_t offset, pinned_pages, i;
893 loff_t first_data_page, last_data_page, num_pages;
894 int shmem_page_index, shmem_page_offset;
895 int data_page_index, data_page_offset;
896 int page_length;
897 int ret;
898 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700899 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700900
901 remain = args->size;
902
903 /* Pin the user pages containing the data. We can't fault while
904 * holding the struct mutex, and all of the pwrite implementations
905 * want to hold it while dereferencing the user data.
906 */
907 first_data_page = data_ptr / PAGE_SIZE;
908 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
909 num_pages = last_data_page - first_data_page + 1;
910
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700911 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700912 if (user_pages == NULL)
913 return -ENOMEM;
914
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100915 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700916 down_read(&mm->mmap_sem);
917 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
918 num_pages, 0, 0, user_pages, NULL);
919 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100920 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700921 if (pinned_pages < num_pages) {
922 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100923 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700924 }
925
Eric Anholt40123c12009-03-09 13:42:30 -0700926 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100927 if (ret)
928 goto out;
929
930 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700931
Daniel Vetter23010e42010-03-08 13:35:02 +0100932 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700933 offset = args->offset;
934 obj_priv->dirty = 1;
935
936 while (remain > 0) {
937 /* Operation in this page
938 *
939 * shmem_page_index = page number within shmem file
940 * shmem_page_offset = offset within page in shmem file
941 * data_page_index = page number in get_user_pages return
942 * data_page_offset = offset with data_page_index page.
943 * page_length = bytes to copy for this page
944 */
945 shmem_page_index = offset / PAGE_SIZE;
946 shmem_page_offset = offset & ~PAGE_MASK;
947 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
948 data_page_offset = data_ptr & ~PAGE_MASK;
949
950 page_length = remain;
951 if ((shmem_page_offset + page_length) > PAGE_SIZE)
952 page_length = PAGE_SIZE - shmem_page_offset;
953 if ((data_page_offset + page_length) > PAGE_SIZE)
954 page_length = PAGE_SIZE - data_page_offset;
955
Eric Anholt280b7132009-03-12 16:56:27 -0700956 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100957 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700958 shmem_page_offset,
959 user_pages[data_page_index],
960 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100961 page_length,
962 0);
963 } else {
964 slow_shmem_copy(obj_priv->pages[shmem_page_index],
965 shmem_page_offset,
966 user_pages[data_page_index],
967 data_page_offset,
968 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700969 }
Eric Anholt40123c12009-03-09 13:42:30 -0700970
971 remain -= page_length;
972 data_ptr += page_length;
973 offset += page_length;
974 }
975
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100976out:
Eric Anholt40123c12009-03-09 13:42:30 -0700977 for (i = 0; i < pinned_pages; i++)
978 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700979 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700980
981 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700982}
983
984/**
985 * Writes data to the object referenced by handle.
986 *
987 * On error, the contents of the buffer that were to be modified are undefined.
988 */
989int
990i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100991 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700992{
993 struct drm_i915_gem_pwrite *args = data;
994 struct drm_gem_object *obj;
995 struct drm_i915_gem_object *obj_priv;
996 int ret = 0;
997
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100998 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -0700999 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001000 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001001 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001002
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001003 ret = i915_mutex_lock_interruptible(dev);
1004 if (ret) {
1005 drm_gem_object_unreference_unlocked(obj);
1006 return ret;
1007 }
1008
Chris Wilson7dcd2492010-09-26 20:21:44 +01001009 /* Bounds check destination. */
1010 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001011 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001012 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001013 }
1014
Chris Wilson35b62a82010-09-26 20:23:38 +01001015 if (args->size == 0)
1016 goto out;
1017
Chris Wilsonce9d4192010-09-26 20:50:05 +01001018 if (!access_ok(VERIFY_READ,
1019 (char __user *)(uintptr_t)args->data_ptr,
1020 args->size)) {
1021 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +01001022 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001023 }
1024
Chris Wilsonb5e4feb2010-10-14 13:47:43 +01001025 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1026 args->size);
1027 if (ret) {
1028 ret = -EFAULT;
1029 goto out;
1030 }
1031
Eric Anholt673a3942008-07-30 12:06:12 -07001032 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1033 * it would end up going through the fenced access, and we'll get
1034 * different detiling behavior between reading and writing.
1035 * pread/pwrite currently are reading and writing from the CPU
1036 * perspective, requiring manual detiling by the client.
1037 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001038 if (obj_priv->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001039 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Dave Airlie71acb5e2008-12-30 20:31:46 +10001040 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001041 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001042 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001043 ret = i915_gem_object_pin(obj, 0);
1044 if (ret)
1045 goto out;
1046
1047 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1048 if (ret)
1049 goto out_unpin;
1050
1051 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1052 if (ret == -EFAULT)
1053 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1054
1055out_unpin:
1056 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001057 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001058 ret = i915_gem_object_get_pages_or_evict(obj);
1059 if (ret)
1060 goto out;
1061
1062 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1063 if (ret)
1064 goto out_put;
1065
1066 ret = -EFAULT;
1067 if (!i915_gem_object_needs_bit17_swizzle(obj))
1068 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1069 if (ret == -EFAULT)
1070 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
1071
1072out_put:
1073 i915_gem_object_put_pages(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001074 }
Eric Anholt673a3942008-07-30 12:06:12 -07001075
Chris Wilson35b62a82010-09-26 20:23:38 +01001076out:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001077 drm_gem_object_unreference(obj);
1078 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001079 return ret;
1080}
1081
1082/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001083 * Called when user space prepares to use an object with the CPU, either
1084 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001085 */
1086int
1087i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1088 struct drm_file *file_priv)
1089{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001090 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001091 struct drm_i915_gem_set_domain *args = data;
1092 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001093 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001094 uint32_t read_domains = args->read_domains;
1095 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001096 int ret;
1097
1098 if (!(dev->driver->driver_features & DRIVER_GEM))
1099 return -ENODEV;
1100
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001101 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001102 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001103 return -EINVAL;
1104
Chris Wilson21d509e2009-06-06 09:46:02 +01001105 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001106 return -EINVAL;
1107
1108 /* Having something in the write domain implies it's in the read
1109 * domain, and only that read domain. Enforce that in the request.
1110 */
1111 if (write_domain != 0 && read_domains != write_domain)
1112 return -EINVAL;
1113
Eric Anholt673a3942008-07-30 12:06:12 -07001114 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1115 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001116 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001117 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001118
Chris Wilson76c1dec2010-09-25 11:22:51 +01001119 ret = i915_mutex_lock_interruptible(dev);
1120 if (ret) {
1121 drm_gem_object_unreference_unlocked(obj);
1122 return ret;
1123 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001124
1125 intel_mark_busy(dev, obj);
1126
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001127 if (read_domains & I915_GEM_DOMAIN_GTT) {
1128 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001129
Eric Anholta09ba7f2009-08-29 12:49:51 -07001130 /* Update the LRU on the fence for the CPU access that's
1131 * about to occur.
1132 */
1133 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001134 struct drm_i915_fence_reg *reg =
1135 &dev_priv->fence_regs[obj_priv->fence_reg];
1136 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001137 &dev_priv->mm.fence_list);
1138 }
1139
Eric Anholt02354392008-11-26 13:58:13 -08001140 /* Silently promote "you're not bound, there was nothing to do"
1141 * to success, since the client was just asking us to
1142 * make sure everything was done.
1143 */
1144 if (ret == -EINVAL)
1145 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001146 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001147 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001148 }
1149
Chris Wilson7d1c4802010-08-07 21:45:03 +01001150 /* Maintain LRU order of "inactive" objects */
1151 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1152 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1153
Eric Anholt673a3942008-07-30 12:06:12 -07001154 drm_gem_object_unreference(obj);
1155 mutex_unlock(&dev->struct_mutex);
1156 return ret;
1157}
1158
1159/**
1160 * Called when user space has done writes to this buffer
1161 */
1162int
1163i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1164 struct drm_file *file_priv)
1165{
1166 struct drm_i915_gem_sw_finish *args = data;
1167 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001168 int ret = 0;
1169
1170 if (!(dev->driver->driver_features & DRIVER_GEM))
1171 return -ENODEV;
1172
Eric Anholt673a3942008-07-30 12:06:12 -07001173 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson76c1dec2010-09-25 11:22:51 +01001174 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001175 return -ENOENT;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001176
1177 ret = i915_mutex_lock_interruptible(dev);
1178 if (ret) {
1179 drm_gem_object_unreference_unlocked(obj);
1180 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001181 }
1182
Eric Anholt673a3942008-07-30 12:06:12 -07001183 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001184 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001185 i915_gem_object_flush_cpu_write_domain(obj);
1186
Eric Anholt673a3942008-07-30 12:06:12 -07001187 drm_gem_object_unreference(obj);
1188 mutex_unlock(&dev->struct_mutex);
1189 return ret;
1190}
1191
1192/**
1193 * Maps the contents of an object, returning the address it is mapped
1194 * into.
1195 *
1196 * While the mapping holds a reference on the contents of the object, it doesn't
1197 * imply a ref on the object itself.
1198 */
1199int
1200i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1201 struct drm_file *file_priv)
1202{
1203 struct drm_i915_gem_mmap *args = data;
1204 struct drm_gem_object *obj;
1205 loff_t offset;
1206 unsigned long addr;
1207
1208 if (!(dev->driver->driver_features & DRIVER_GEM))
1209 return -ENODEV;
1210
1211 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1212 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001213 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001214
1215 offset = args->offset;
1216
1217 down_write(&current->mm->mmap_sem);
1218 addr = do_mmap(obj->filp, 0, args->size,
1219 PROT_READ | PROT_WRITE, MAP_SHARED,
1220 args->offset);
1221 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001222 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001223 if (IS_ERR((void *)addr))
1224 return addr;
1225
1226 args->addr_ptr = (uint64_t) addr;
1227
1228 return 0;
1229}
1230
Jesse Barnesde151cf2008-11-12 10:03:55 -08001231/**
1232 * i915_gem_fault - fault a page into the GTT
1233 * vma: VMA in question
1234 * vmf: fault info
1235 *
1236 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1237 * from userspace. The fault handler takes care of binding the object to
1238 * the GTT (if needed), allocating and programming a fence register (again,
1239 * only if needed based on whether the old reg is still valid or the object
1240 * is tiled) and inserting a new PTE into the faulting process.
1241 *
1242 * Note that the faulting process may involve evicting existing objects
1243 * from the GTT and/or fence registers to make room. So performance may
1244 * suffer if the GTT working set is large or there are few fence registers
1245 * left.
1246 */
1247int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1248{
1249 struct drm_gem_object *obj = vma->vm_private_data;
1250 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001251 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001252 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001253 pgoff_t page_offset;
1254 unsigned long pfn;
1255 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001256 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001257
1258 /* We don't use vmf->pgoff since that has the fake offset */
1259 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1260 PAGE_SHIFT;
1261
1262 /* Now bind it into the GTT if needed */
1263 mutex_lock(&dev->struct_mutex);
1264 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001265 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001266 if (ret)
1267 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001268
Jesse Barnesde151cf2008-11-12 10:03:55 -08001269 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001270 if (ret)
1271 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001272 }
1273
1274 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001275 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001276 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001277 if (ret)
1278 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001279 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001280
Chris Wilson7d1c4802010-08-07 21:45:03 +01001281 if (i915_gem_object_is_inactive(obj_priv))
1282 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1283
Jesse Barnesde151cf2008-11-12 10:03:55 -08001284 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1285 page_offset;
1286
1287 /* Finally, remap it using the new GTT offset */
1288 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001289unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001290 mutex_unlock(&dev->struct_mutex);
1291
1292 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001293 case 0:
1294 case -ERESTARTSYS:
1295 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001296 case -ENOMEM:
1297 case -EAGAIN:
1298 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001299 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001300 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001301 }
1302}
1303
1304/**
1305 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1306 * @obj: obj in question
1307 *
1308 * GEM memory mapping works by handing back to userspace a fake mmap offset
1309 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1310 * up the object based on the offset and sets up the various memory mapping
1311 * structures.
1312 *
1313 * This routine allocates and attaches a fake offset for @obj.
1314 */
1315static int
1316i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1317{
1318 struct drm_device *dev = obj->dev;
1319 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001320 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001322 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001323 int ret = 0;
1324
1325 /* Set the object up for mmap'ing */
1326 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001327 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001328 if (!list->map)
1329 return -ENOMEM;
1330
1331 map = list->map;
1332 map->type = _DRM_GEM;
1333 map->size = obj->size;
1334 map->handle = obj;
1335
1336 /* Get a DRM GEM mmap offset allocated... */
1337 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1338 obj->size / PAGE_SIZE, 0, 0);
1339 if (!list->file_offset_node) {
1340 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001341 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001342 goto out_free_list;
1343 }
1344
1345 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1346 obj->size / PAGE_SIZE, 0);
1347 if (!list->file_offset_node) {
1348 ret = -ENOMEM;
1349 goto out_free_list;
1350 }
1351
1352 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001353 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1354 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001355 DRM_ERROR("failed to add to map hash\n");
1356 goto out_free_mm;
1357 }
1358
1359 /* By now we should be all set, any drm_mmap request on the offset
1360 * below will get to our mmap & fault handler */
1361 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1362
1363 return 0;
1364
1365out_free_mm:
1366 drm_mm_put_block(list->file_offset_node);
1367out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001368 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001369
1370 return ret;
1371}
1372
Chris Wilson901782b2009-07-10 08:18:50 +01001373/**
1374 * i915_gem_release_mmap - remove physical page mappings
1375 * @obj: obj in question
1376 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001377 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001378 * relinquish ownership of the pages back to the system.
1379 *
1380 * It is vital that we remove the page mapping if we have mapped a tiled
1381 * object through the GTT and then lose the fence register due to
1382 * resource pressure. Similarly if the object has been moved out of the
1383 * aperture, than pages mapped into userspace must be revoked. Removing the
1384 * mapping will then trigger a page fault on the next user access, allowing
1385 * fixup by i915_gem_fault().
1386 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001387void
Chris Wilson901782b2009-07-10 08:18:50 +01001388i915_gem_release_mmap(struct drm_gem_object *obj)
1389{
1390 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001391 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001392
1393 if (dev->dev_mapping)
1394 unmap_mapping_range(dev->dev_mapping,
1395 obj_priv->mmap_offset, obj->size, 1);
1396}
1397
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001398static void
1399i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1400{
1401 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001402 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001403 struct drm_gem_mm *mm = dev->mm_private;
1404 struct drm_map_list *list;
1405
1406 list = &obj->map_list;
1407 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1408
1409 if (list->file_offset_node) {
1410 drm_mm_put_block(list->file_offset_node);
1411 list->file_offset_node = NULL;
1412 }
1413
1414 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001415 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001416 list->map = NULL;
1417 }
1418
1419 obj_priv->mmap_offset = 0;
1420}
1421
Jesse Barnesde151cf2008-11-12 10:03:55 -08001422/**
1423 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1424 * @obj: object to check
1425 *
1426 * Return the required GTT alignment for an object, taking into account
1427 * potential fence register mapping if needed.
1428 */
1429static uint32_t
1430i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1431{
1432 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001433 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001434 int start, i;
1435
1436 /*
1437 * Minimum alignment is 4k (GTT page size), but might be greater
1438 * if a fence register is needed for the object.
1439 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001440 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001441 return 4096;
1442
1443 /*
1444 * Previous chips need to be aligned to the size of the smallest
1445 * fence register that can contain the object.
1446 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001447 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001448 start = 1024*1024;
1449 else
1450 start = 512*1024;
1451
1452 for (i = start; i < obj->size; i <<= 1)
1453 ;
1454
1455 return i;
1456}
1457
1458/**
1459 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1460 * @dev: DRM device
1461 * @data: GTT mapping ioctl data
1462 * @file_priv: GEM object info
1463 *
1464 * Simply returns the fake offset to userspace so it can mmap it.
1465 * The mmap call will end up in drm_gem_mmap(), which will set things
1466 * up so we can get faults in the handler above.
1467 *
1468 * The fault handler will take care of binding the object into the GTT
1469 * (since it may have been evicted to make room for something), allocating
1470 * a fence register, and mapping the appropriate aperture address into
1471 * userspace.
1472 */
1473int
1474i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1475 struct drm_file *file_priv)
1476{
1477 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001478 struct drm_gem_object *obj;
1479 struct drm_i915_gem_object *obj_priv;
1480 int ret;
1481
1482 if (!(dev->driver->driver_features & DRIVER_GEM))
1483 return -ENODEV;
1484
1485 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1486 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001487 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001488
Chris Wilson76c1dec2010-09-25 11:22:51 +01001489 ret = i915_mutex_lock_interruptible(dev);
1490 if (ret) {
1491 drm_gem_object_unreference_unlocked(obj);
1492 return ret;
1493 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001494
Daniel Vetter23010e42010-03-08 13:35:02 +01001495 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001496
Chris Wilsonab182822009-09-22 18:46:17 +01001497 if (obj_priv->madv != I915_MADV_WILLNEED) {
1498 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1499 drm_gem_object_unreference(obj);
1500 mutex_unlock(&dev->struct_mutex);
1501 return -EINVAL;
1502 }
1503
1504
Jesse Barnesde151cf2008-11-12 10:03:55 -08001505 if (!obj_priv->mmap_offset) {
1506 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001507 if (ret) {
1508 drm_gem_object_unreference(obj);
1509 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001510 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001511 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001512 }
1513
1514 args->offset = obj_priv->mmap_offset;
1515
Jesse Barnesde151cf2008-11-12 10:03:55 -08001516 /*
1517 * Pull it into the GTT so that we have a page list (makes the
1518 * initial fault faster and any subsequent flushing possible).
1519 */
1520 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001521 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001522 if (ret) {
1523 drm_gem_object_unreference(obj);
1524 mutex_unlock(&dev->struct_mutex);
1525 return ret;
1526 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001527 }
1528
1529 drm_gem_object_unreference(obj);
1530 mutex_unlock(&dev->struct_mutex);
1531
1532 return 0;
1533}
1534
Chris Wilson5cdf5882010-09-27 15:51:07 +01001535static void
Eric Anholt856fa192009-03-19 14:10:50 -07001536i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001537{
Daniel Vetter23010e42010-03-08 13:35:02 +01001538 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001539 int page_count = obj->size / PAGE_SIZE;
1540 int i;
1541
Eric Anholt856fa192009-03-19 14:10:50 -07001542 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001543 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001544
1545 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001546 return;
1547
Eric Anholt280b7132009-03-12 16:56:27 -07001548 if (obj_priv->tiling_mode != I915_TILING_NONE)
1549 i915_gem_object_save_bit_17_swizzle(obj);
1550
Chris Wilson3ef94da2009-09-14 16:50:29 +01001551 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001552 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001553
1554 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001555 if (obj_priv->dirty)
1556 set_page_dirty(obj_priv->pages[i]);
1557
1558 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001559 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001560
1561 page_cache_release(obj_priv->pages[i]);
1562 }
Eric Anholt673a3942008-07-30 12:06:12 -07001563 obj_priv->dirty = 0;
1564
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001565 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001566 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001567}
1568
Chris Wilsona56ba562010-09-28 10:07:56 +01001569static uint32_t
1570i915_gem_next_request_seqno(struct drm_device *dev,
1571 struct intel_ring_buffer *ring)
1572{
1573 drm_i915_private_t *dev_priv = dev->dev_private;
1574
1575 ring->outstanding_lazy_request = true;
1576 return dev_priv->next_seqno;
1577}
1578
Eric Anholt673a3942008-07-30 12:06:12 -07001579static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001580i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001581 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001582{
Chris Wilsona56ba562010-09-28 10:07:56 +01001583 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001584 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001585 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001586
Zou Nan hai852835f2010-05-21 09:08:56 +08001587 BUG_ON(ring == NULL);
1588 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001589
1590 /* Add a reference if we're newly entering the active list. */
1591 if (!obj_priv->active) {
1592 drm_gem_object_reference(obj);
1593 obj_priv->active = 1;
1594 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001595
Eric Anholt673a3942008-07-30 12:06:12 -07001596 /* Move from whatever list we were on to the tail of execution. */
Zou Nan hai852835f2010-05-21 09:08:56 +08001597 list_move_tail(&obj_priv->list, &ring->active_list);
Chris Wilsona56ba562010-09-28 10:07:56 +01001598 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001599}
1600
Eric Anholtce44b0e2008-11-06 16:00:31 -08001601static void
1602i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1603{
1604 struct drm_device *dev = obj->dev;
1605 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001606 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001607
1608 BUG_ON(!obj_priv->active);
1609 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1610 obj_priv->last_rendering_seqno = 0;
1611}
Eric Anholt673a3942008-07-30 12:06:12 -07001612
Chris Wilson963b4832009-09-20 23:03:54 +01001613/* Immediately discard the backing storage */
1614static void
1615i915_gem_object_truncate(struct drm_gem_object *obj)
1616{
Daniel Vetter23010e42010-03-08 13:35:02 +01001617 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001618 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001619
Chris Wilsonae9fed62010-08-07 11:01:30 +01001620 /* Our goal here is to return as much of the memory as
1621 * is possible back to the system as we are called from OOM.
1622 * To do this we must instruct the shmfs to drop all of its
1623 * backing pages, *now*. Here we mirror the actions taken
1624 * when by shmem_delete_inode() to release the backing store.
1625 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001626 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001627 truncate_inode_pages(inode->i_mapping, 0);
1628 if (inode->i_op->truncate_range)
1629 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001630
1631 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001632}
1633
1634static inline int
1635i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1636{
1637 return obj_priv->madv == I915_MADV_DONTNEED;
1638}
1639
Eric Anholt673a3942008-07-30 12:06:12 -07001640static void
1641i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1642{
1643 struct drm_device *dev = obj->dev;
1644 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001645 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001646
Eric Anholt673a3942008-07-30 12:06:12 -07001647 if (obj_priv->pin_count != 0)
Chris Wilsonf13d3f72010-09-20 17:36:15 +01001648 list_move_tail(&obj_priv->list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001649 else
1650 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1651
Daniel Vetter99fcb762010-02-07 16:20:18 +01001652 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1653
Eric Anholtce44b0e2008-11-06 16:00:31 -08001654 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001655 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001656 if (obj_priv->active) {
1657 obj_priv->active = 0;
1658 drm_gem_object_unreference(obj);
1659 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001660 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001661}
1662
Chris Wilson92204342010-09-18 11:02:01 +01001663static void
Daniel Vetter63560392010-02-19 11:51:59 +01001664i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001665 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001666 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001667{
1668 drm_i915_private_t *dev_priv = dev->dev_private;
1669 struct drm_i915_gem_object *obj_priv, *next;
1670
1671 list_for_each_entry_safe(obj_priv, next,
1672 &dev_priv->mm.gpu_write_list,
1673 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001674 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001675
Chris Wilson2b6efaa2010-09-14 17:04:02 +01001676 if (obj->write_domain & flush_domains &&
1677 obj_priv->ring == ring) {
Daniel Vetter63560392010-02-19 11:51:59 +01001678 uint32_t old_write_domain = obj->write_domain;
1679
1680 obj->write_domain = 0;
1681 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001682 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001683
1684 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001685 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1686 struct drm_i915_fence_reg *reg =
1687 &dev_priv->fence_regs[obj_priv->fence_reg];
1688 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001689 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001690 }
Daniel Vetter63560392010-02-19 11:51:59 +01001691
1692 trace_i915_gem_object_change_domain(obj,
1693 obj->read_domains,
1694 old_write_domain);
1695 }
1696 }
1697}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001698
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001699uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001700i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001701 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001702 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001703 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001704{
1705 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001706 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001707 uint32_t seqno;
1708 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001709
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001710 if (file != NULL)
1711 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001712
Chris Wilson8dc5d142010-08-12 12:36:12 +01001713 if (request == NULL) {
1714 request = kzalloc(sizeof(*request), GFP_KERNEL);
1715 if (request == NULL)
1716 return 0;
1717 }
Eric Anholt673a3942008-07-30 12:06:12 -07001718
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001719 seqno = ring->add_request(dev, ring, 0);
Chris Wilsona56ba562010-09-28 10:07:56 +01001720 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001721
1722 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001723 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001724 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001725 was_empty = list_empty(&ring->request_list);
1726 list_add_tail(&request->list, &ring->request_list);
1727
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001728 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001729 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001730 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001731 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001732 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001733 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001734 }
Eric Anholt673a3942008-07-30 12:06:12 -07001735
Ben Gamarif65d9422009-09-14 17:48:44 -04001736 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001737 mod_timer(&dev_priv->hangcheck_timer,
1738 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001739 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001740 queue_delayed_work(dev_priv->wq,
1741 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001742 }
Eric Anholt673a3942008-07-30 12:06:12 -07001743 return seqno;
1744}
1745
1746/**
1747 * Command execution barrier
1748 *
1749 * Ensures that all commands in the ring are finished
1750 * before signalling the CPU
1751 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001752static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001753i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001754{
Eric Anholt673a3942008-07-30 12:06:12 -07001755 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001756
1757 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001758 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001759 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001760
1761 ring->flush(dev, ring,
1762 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001763}
1764
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001765static inline void
1766i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001767{
Chris Wilson1c255952010-09-26 11:03:27 +01001768 struct drm_i915_file_private *file_priv = request->file_priv;
1769
1770 if (!file_priv)
1771 return;
1772
1773 spin_lock(&file_priv->mm.lock);
1774 list_del(&request->client_list);
1775 request->file_priv = NULL;
1776 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001777}
1778
Chris Wilsondfaae392010-09-22 10:31:52 +01001779static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1780 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001781{
Chris Wilsondfaae392010-09-22 10:31:52 +01001782 while (!list_empty(&ring->request_list)) {
1783 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001784
Chris Wilsondfaae392010-09-22 10:31:52 +01001785 request = list_first_entry(&ring->request_list,
1786 struct drm_i915_gem_request,
1787 list);
1788
1789 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001790 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001791 kfree(request);
1792 }
1793
1794 while (!list_empty(&ring->active_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001795 struct drm_i915_gem_object *obj_priv;
1796
Chris Wilsondfaae392010-09-22 10:31:52 +01001797 obj_priv = list_first_entry(&ring->active_list,
1798 struct drm_i915_gem_object,
1799 list);
1800
1801 obj_priv->base.write_domain = 0;
1802 list_del_init(&obj_priv->gpu_write_list);
1803 i915_gem_object_move_to_inactive(&obj_priv->base);
1804 }
1805}
1806
Chris Wilson069efc12010-09-30 16:53:18 +01001807void i915_gem_reset(struct drm_device *dev)
Chris Wilsondfaae392010-09-22 10:31:52 +01001808{
1809 struct drm_i915_private *dev_priv = dev->dev_private;
1810 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001811 int i;
Chris Wilsondfaae392010-09-22 10:31:52 +01001812
1813 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
1814 if (HAS_BSD(dev))
1815 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
1816
1817 /* Remove anything from the flushing lists. The GPU cache is likely
1818 * to be lost on reset along with the data, so simply move the
1819 * lost bo to the inactive list.
1820 */
1821 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001822 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1823 struct drm_i915_gem_object,
1824 list);
1825
1826 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001827 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001828 i915_gem_object_move_to_inactive(&obj_priv->base);
1829 }
Chris Wilson9375e442010-09-19 12:21:28 +01001830
Chris Wilsondfaae392010-09-22 10:31:52 +01001831 /* Move everything out of the GPU domains to ensure we do any
1832 * necessary invalidation upon reuse.
1833 */
Chris Wilson77f01232010-09-19 12:31:36 +01001834 list_for_each_entry(obj_priv,
1835 &dev_priv->mm.inactive_list,
1836 list)
1837 {
1838 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1839 }
Chris Wilson069efc12010-09-30 16:53:18 +01001840
1841 /* The fence registers are invalidated so clear them out */
1842 for (i = 0; i < 16; i++) {
1843 struct drm_i915_fence_reg *reg;
1844
1845 reg = &dev_priv->fence_regs[i];
1846 if (!reg->obj)
1847 continue;
1848
1849 i915_gem_clear_fence_reg(reg->obj);
1850 }
Chris Wilson77f01232010-09-19 12:31:36 +01001851}
1852
Eric Anholt673a3942008-07-30 12:06:12 -07001853/**
1854 * This function clears the request list as sequence numbers are passed.
1855 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001856static void
1857i915_gem_retire_requests_ring(struct drm_device *dev,
1858 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001859{
1860 drm_i915_private_t *dev_priv = dev->dev_private;
1861 uint32_t seqno;
1862
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001863 if (!ring->status_page.page_addr ||
1864 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001865 return;
1866
Chris Wilson23bc5982010-09-29 16:10:57 +01001867 WARN_ON(i915_verify_lists(dev));
1868
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001869 seqno = ring->get_seqno(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001870 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001871 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001872
Zou Nan hai852835f2010-05-21 09:08:56 +08001873 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001874 struct drm_i915_gem_request,
1875 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001876
Chris Wilsondfaae392010-09-22 10:31:52 +01001877 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001878 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001879
1880 trace_i915_gem_request_retire(dev, request->seqno);
1881
1882 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001883 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001884 kfree(request);
1885 }
1886
1887 /* Move any buffers on the active list that are no longer referenced
1888 * by the ringbuffer to the flushing/inactive lists as appropriate.
1889 */
1890 while (!list_empty(&ring->active_list)) {
1891 struct drm_gem_object *obj;
1892 struct drm_i915_gem_object *obj_priv;
1893
1894 obj_priv = list_first_entry(&ring->active_list,
1895 struct drm_i915_gem_object,
1896 list);
1897
Chris Wilsondfaae392010-09-22 10:31:52 +01001898 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001899 break;
1900
1901 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001902 if (obj->write_domain != 0)
1903 i915_gem_object_move_to_flushing(obj);
1904 else
1905 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001906 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001907
1908 if (unlikely (dev_priv->trace_irq_seqno &&
1909 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001910 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001911 dev_priv->trace_irq_seqno = 0;
1912 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001913
1914 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001915}
1916
1917void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001918i915_gem_retire_requests(struct drm_device *dev)
1919{
1920 drm_i915_private_t *dev_priv = dev->dev_private;
1921
Chris Wilsonbe726152010-07-23 23:18:50 +01001922 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1923 struct drm_i915_gem_object *obj_priv, *tmp;
1924
1925 /* We must be careful that during unbind() we do not
1926 * accidentally infinitely recurse into retire requests.
1927 * Currently:
1928 * retire -> free -> unbind -> wait -> retire_ring
1929 */
1930 list_for_each_entry_safe(obj_priv, tmp,
1931 &dev_priv->mm.deferred_free_list,
1932 list)
1933 i915_gem_free_object_tail(&obj_priv->base);
1934 }
1935
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001936 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1937 if (HAS_BSD(dev))
1938 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1939}
1940
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001941static void
Eric Anholt673a3942008-07-30 12:06:12 -07001942i915_gem_retire_work_handler(struct work_struct *work)
1943{
1944 drm_i915_private_t *dev_priv;
1945 struct drm_device *dev;
1946
1947 dev_priv = container_of(work, drm_i915_private_t,
1948 mm.retire_work.work);
1949 dev = dev_priv->dev;
1950
Chris Wilson891b48c2010-09-29 12:26:37 +01001951 /* Come back later if the device is busy... */
1952 if (!mutex_trylock(&dev->struct_mutex)) {
1953 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1954 return;
1955 }
1956
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001957 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001958
Keith Packard6dbe2772008-10-14 21:41:13 -07001959 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001960 (!list_empty(&dev_priv->render_ring.request_list) ||
1961 (HAS_BSD(dev) &&
1962 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001963 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001964 mutex_unlock(&dev->struct_mutex);
1965}
1966
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001967int
Zou Nan hai852835f2010-05-21 09:08:56 +08001968i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001969 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001970{
1971 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001972 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001973 int ret = 0;
1974
1975 BUG_ON(seqno == 0);
1976
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001977 if (atomic_read(&dev_priv->mm.wedged))
1978 return -EAGAIN;
1979
Chris Wilsona56ba562010-09-28 10:07:56 +01001980 if (ring->outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01001981 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001982 if (seqno == 0)
1983 return -ENOMEM;
1984 }
Chris Wilsona56ba562010-09-28 10:07:56 +01001985 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001986
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001987 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001988 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001989 ier = I915_READ(DEIER) | I915_READ(GTIER);
1990 else
1991 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001992 if (!ier) {
1993 DRM_ERROR("something (likely vbetool) disabled "
1994 "interrupts, re-enabling\n");
1995 i915_driver_irq_preinstall(dev);
1996 i915_driver_irq_postinstall(dev);
1997 }
1998
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001999 trace_i915_gem_request_wait_begin(dev, seqno);
2000
Zou Nan hai852835f2010-05-21 09:08:56 +08002001 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002002 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002003 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002004 ret = wait_event_interruptible(ring->irq_queue,
2005 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002006 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002007 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002008 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002009 wait_event(ring->irq_queue,
2010 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002011 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002012 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002013
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002014 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08002015 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002016
2017 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002018 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002019 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002020 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002021
2022 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002023 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002024 __func__, ret, seqno, ring->get_seqno(dev, ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002025 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002026
2027 /* Directly dispatch request retiring. While we have the work queue
2028 * to handle this, the waiter on a request often wants an associated
2029 * buffer to have made it to the inactive list, and we would need
2030 * a separate wait queue to handle that.
2031 */
2032 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002033 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002034
2035 return ret;
2036}
2037
Daniel Vetter48764bf2009-09-15 22:57:32 +02002038/**
2039 * Waits for a sequence number to be signaled, and cleans up the
2040 * request and object lists appropriately for that event.
2041 */
2042static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002043i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002044 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002045{
Zou Nan hai852835f2010-05-21 09:08:56 +08002046 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002047}
2048
Chris Wilson20f0cd52010-09-23 11:00:38 +01002049static void
Chris Wilson92204342010-09-18 11:02:01 +01002050i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002051 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002052 struct intel_ring_buffer *ring,
2053 uint32_t invalidate_domains,
2054 uint32_t flush_domains)
2055{
2056 ring->flush(dev, ring, invalidate_domains, flush_domains);
2057 i915_gem_process_flushing_list(dev, flush_domains, ring);
2058}
2059
2060static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002061i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002062 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002063 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002064 uint32_t flush_domains,
2065 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002066{
2067 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002068
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002069 if (flush_domains & I915_GEM_DOMAIN_CPU)
2070 drm_agp_chipset_flush(dev);
Daniel Vetter8bff9172010-02-11 22:19:40 +01002071
Chris Wilson92204342010-09-18 11:02:01 +01002072 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2073 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002074 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002075 &dev_priv->render_ring,
2076 invalidate_domains, flush_domains);
2077 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002078 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002079 &dev_priv->bsd_ring,
2080 invalidate_domains, flush_domains);
2081 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002082}
2083
Eric Anholt673a3942008-07-30 12:06:12 -07002084/**
2085 * Ensures that all rendering to the object has completed and the object is
2086 * safe to unbind from the GTT or access from the CPU.
2087 */
2088static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002089i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2090 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002091{
2092 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002093 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002094 int ret;
2095
Eric Anholte47c68e2008-11-14 13:35:19 -08002096 /* This function only exists to support waiting for existing rendering,
2097 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002098 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002099 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002100
2101 /* If there is rendering queued on the buffer being evicted, wait for
2102 * it.
2103 */
2104 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002105 ret = i915_do_wait_request(dev,
2106 obj_priv->last_rendering_seqno,
2107 interruptible,
2108 obj_priv->ring);
2109 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002110 return ret;
2111 }
2112
2113 return 0;
2114}
2115
2116/**
2117 * Unbinds an object from the GTT aperture.
2118 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002119int
Eric Anholt673a3942008-07-30 12:06:12 -07002120i915_gem_object_unbind(struct drm_gem_object *obj)
2121{
2122 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002123 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002124 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002125 int ret = 0;
2126
Eric Anholt673a3942008-07-30 12:06:12 -07002127 if (obj_priv->gtt_space == NULL)
2128 return 0;
2129
2130 if (obj_priv->pin_count != 0) {
2131 DRM_ERROR("Attempting to unbind pinned buffer\n");
2132 return -EINVAL;
2133 }
2134
Eric Anholt5323fd02009-09-09 11:50:45 -07002135 /* blow away mappings if mapped through GTT */
2136 i915_gem_release_mmap(obj);
2137
Eric Anholt673a3942008-07-30 12:06:12 -07002138 /* Move the object to the CPU domain to ensure that
2139 * any possible CPU writes while it's not in the GTT
2140 * are flushed when we go to remap it. This will
2141 * also ensure that all pending GPU writes are finished
2142 * before we unbind.
2143 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002144 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002145 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002146 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002147 /* Continue on if we fail due to EIO, the GPU is hung so we
2148 * should be safe and we need to cleanup or else we might
2149 * cause memory corruption through use-after-free.
2150 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002151 if (ret) {
2152 i915_gem_clflush_object(obj);
2153 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2154 }
Eric Anholt673a3942008-07-30 12:06:12 -07002155
Daniel Vetter96b47b62009-12-15 17:50:00 +01002156 /* release the fence reg _after_ flushing */
2157 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2158 i915_gem_clear_fence_reg(obj);
2159
Chris Wilson73aa8082010-09-30 11:46:12 +01002160 drm_unbind_agp(obj_priv->agp_mem);
2161 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002162
Eric Anholt856fa192009-03-19 14:10:50 -07002163 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002164 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002165
Chris Wilson73aa8082010-09-30 11:46:12 +01002166 i915_gem_info_remove_gtt(dev_priv, obj->size);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01002167 list_del_init(&obj_priv->list);
Eric Anholt673a3942008-07-30 12:06:12 -07002168
Chris Wilson73aa8082010-09-30 11:46:12 +01002169 drm_mm_put_block(obj_priv->gtt_space);
2170 obj_priv->gtt_space = NULL;
2171
Chris Wilson963b4832009-09-20 23:03:54 +01002172 if (i915_gem_object_is_purgeable(obj_priv))
2173 i915_gem_object_truncate(obj);
2174
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002175 trace_i915_gem_object_unbind(obj);
2176
Chris Wilson8dc17752010-07-23 23:18:51 +01002177 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002178}
2179
Chris Wilsona56ba562010-09-28 10:07:56 +01002180static int i915_ring_idle(struct drm_device *dev,
2181 struct intel_ring_buffer *ring)
2182{
2183 i915_gem_flush_ring(dev, NULL, ring,
2184 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2185 return i915_wait_request(dev,
2186 i915_gem_next_request_seqno(dev, ring),
2187 ring);
2188}
2189
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002190int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002191i915_gpu_idle(struct drm_device *dev)
2192{
2193 drm_i915_private_t *dev_priv = dev->dev_private;
2194 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002195 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002196
Zou Nan haid1b851f2010-05-21 09:08:57 +08002197 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2198 list_empty(&dev_priv->render_ring.active_list) &&
2199 (!HAS_BSD(dev) ||
2200 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002201 if (lists_empty)
2202 return 0;
2203
2204 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002205 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002206 if (ret)
2207 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002208
2209 if (HAS_BSD(dev)) {
Chris Wilsona56ba562010-09-28 10:07:56 +01002210 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002211 if (ret)
2212 return ret;
2213 }
2214
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002215 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002216}
2217
Chris Wilson5cdf5882010-09-27 15:51:07 +01002218static int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002219i915_gem_object_get_pages(struct drm_gem_object *obj,
2220 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002221{
Daniel Vetter23010e42010-03-08 13:35:02 +01002222 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002223 int page_count, i;
2224 struct address_space *mapping;
2225 struct inode *inode;
2226 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002227
Daniel Vetter778c3542010-05-13 11:49:44 +02002228 BUG_ON(obj_priv->pages_refcount
2229 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2230
Eric Anholt856fa192009-03-19 14:10:50 -07002231 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002232 return 0;
2233
2234 /* Get the list of pages out of our struct file. They'll be pinned
2235 * at this point until we release them.
2236 */
2237 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002238 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002239 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002240 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002241 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002242 return -ENOMEM;
2243 }
2244
2245 inode = obj->filp->f_path.dentry->d_inode;
2246 mapping = inode->i_mapping;
2247 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002248 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002249 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002250 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002251 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002252 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002253 if (IS_ERR(page))
2254 goto err_pages;
2255
Eric Anholt856fa192009-03-19 14:10:50 -07002256 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002257 }
Eric Anholt280b7132009-03-12 16:56:27 -07002258
2259 if (obj_priv->tiling_mode != I915_TILING_NONE)
2260 i915_gem_object_do_bit_17_swizzle(obj);
2261
Eric Anholt673a3942008-07-30 12:06:12 -07002262 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002263
2264err_pages:
2265 while (i--)
2266 page_cache_release(obj_priv->pages[i]);
2267
2268 drm_free_large(obj_priv->pages);
2269 obj_priv->pages = NULL;
2270 obj_priv->pages_refcount--;
2271 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002272}
2273
Eric Anholt4e901fd2009-10-26 16:44:17 -07002274static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2275{
2276 struct drm_gem_object *obj = reg->obj;
2277 struct drm_device *dev = obj->dev;
2278 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002279 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002280 int regnum = obj_priv->fence_reg;
2281 uint64_t val;
2282
2283 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2284 0xfffff000) << 32;
2285 val |= obj_priv->gtt_offset & 0xfffff000;
2286 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2287 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2288
2289 if (obj_priv->tiling_mode == I915_TILING_Y)
2290 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2291 val |= I965_FENCE_REG_VALID;
2292
2293 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2294}
2295
Jesse Barnesde151cf2008-11-12 10:03:55 -08002296static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2297{
2298 struct drm_gem_object *obj = reg->obj;
2299 struct drm_device *dev = obj->dev;
2300 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002301 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002302 int regnum = obj_priv->fence_reg;
2303 uint64_t val;
2304
2305 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2306 0xfffff000) << 32;
2307 val |= obj_priv->gtt_offset & 0xfffff000;
2308 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2309 if (obj_priv->tiling_mode == I915_TILING_Y)
2310 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2311 val |= I965_FENCE_REG_VALID;
2312
2313 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2314}
2315
2316static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2317{
2318 struct drm_gem_object *obj = reg->obj;
2319 struct drm_device *dev = obj->dev;
2320 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002321 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002322 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002323 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002324 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002325 uint32_t pitch_val;
2326
2327 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2328 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002329 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002330 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002331 return;
2332 }
2333
Jesse Barnes0f973f22009-01-26 17:10:45 -08002334 if (obj_priv->tiling_mode == I915_TILING_Y &&
2335 HAS_128_BYTE_Y_TILING(dev))
2336 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002337 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002338 tile_width = 512;
2339
2340 /* Note: pitch better be a power of two tile widths */
2341 pitch_val = obj_priv->stride / tile_width;
2342 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002344 if (obj_priv->tiling_mode == I915_TILING_Y &&
2345 HAS_128_BYTE_Y_TILING(dev))
2346 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2347 else
2348 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2349
Jesse Barnesde151cf2008-11-12 10:03:55 -08002350 val = obj_priv->gtt_offset;
2351 if (obj_priv->tiling_mode == I915_TILING_Y)
2352 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2353 val |= I915_FENCE_SIZE_BITS(obj->size);
2354 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2355 val |= I830_FENCE_REG_VALID;
2356
Eric Anholtdc529a42009-03-10 22:34:49 -07002357 if (regnum < 8)
2358 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2359 else
2360 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2361 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362}
2363
2364static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2365{
2366 struct drm_gem_object *obj = reg->obj;
2367 struct drm_device *dev = obj->dev;
2368 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002369 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002370 int regnum = obj_priv->fence_reg;
2371 uint32_t val;
2372 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002373 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002374
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002375 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002376 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002377 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002378 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002379 return;
2380 }
2381
Eric Anholte76a16d2009-05-26 17:44:56 -07002382 pitch_val = obj_priv->stride / 128;
2383 pitch_val = ffs(pitch_val) - 1;
2384 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2385
Jesse Barnesde151cf2008-11-12 10:03:55 -08002386 val = obj_priv->gtt_offset;
2387 if (obj_priv->tiling_mode == I915_TILING_Y)
2388 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002389 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2390 WARN_ON(fence_size_bits & ~0x00000f00);
2391 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002392 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2393 val |= I830_FENCE_REG_VALID;
2394
2395 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002396}
2397
Chris Wilson2cf34d72010-09-14 13:03:28 +01002398static int i915_find_fence_reg(struct drm_device *dev,
2399 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002400{
2401 struct drm_i915_fence_reg *reg = NULL;
2402 struct drm_i915_gem_object *obj_priv = NULL;
2403 struct drm_i915_private *dev_priv = dev->dev_private;
2404 struct drm_gem_object *obj = NULL;
2405 int i, avail, ret;
2406
2407 /* First try to find a free reg */
2408 avail = 0;
2409 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2410 reg = &dev_priv->fence_regs[i];
2411 if (!reg->obj)
2412 return i;
2413
Daniel Vetter23010e42010-03-08 13:35:02 +01002414 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002415 if (!obj_priv->pin_count)
2416 avail++;
2417 }
2418
2419 if (avail == 0)
2420 return -ENOSPC;
2421
2422 /* None available, try to steal one or wait for a user to finish */
2423 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002424 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2425 lru_list) {
2426 obj = reg->obj;
2427 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002428
2429 if (obj_priv->pin_count)
2430 continue;
2431
2432 /* found one! */
2433 i = obj_priv->fence_reg;
2434 break;
2435 }
2436
2437 BUG_ON(i == I915_FENCE_REG_NONE);
2438
2439 /* We only have a reference on obj from the active list. put_fence_reg
2440 * might drop that one, causing a use-after-free in it. So hold a
2441 * private reference to obj like the other callers of put_fence_reg
2442 * (set_tiling ioctl) do. */
2443 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002444 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002445 drm_gem_object_unreference(obj);
2446 if (ret != 0)
2447 return ret;
2448
2449 return i;
2450}
2451
Jesse Barnesde151cf2008-11-12 10:03:55 -08002452/**
2453 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2454 * @obj: object to map through a fence reg
2455 *
2456 * When mapping objects through the GTT, userspace wants to be able to write
2457 * to them without having to worry about swizzling if the object is tiled.
2458 *
2459 * This function walks the fence regs looking for a free one for @obj,
2460 * stealing one if it can't find any.
2461 *
2462 * It then sets up the reg based on the object's properties: address, pitch
2463 * and tiling format.
2464 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002465int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002466i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2467 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002468{
2469 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002470 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002471 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002472 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002473 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002474
Eric Anholta09ba7f2009-08-29 12:49:51 -07002475 /* Just update our place in the LRU if our fence is getting used. */
2476 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002477 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2478 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002479 return 0;
2480 }
2481
Jesse Barnesde151cf2008-11-12 10:03:55 -08002482 switch (obj_priv->tiling_mode) {
2483 case I915_TILING_NONE:
2484 WARN(1, "allocating a fence for non-tiled object?\n");
2485 break;
2486 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002487 if (!obj_priv->stride)
2488 return -EINVAL;
2489 WARN((obj_priv->stride & (512 - 1)),
2490 "object 0x%08x is X tiled but has non-512B pitch\n",
2491 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002492 break;
2493 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002494 if (!obj_priv->stride)
2495 return -EINVAL;
2496 WARN((obj_priv->stride & (128 - 1)),
2497 "object 0x%08x is Y tiled but has non-128B pitch\n",
2498 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002499 break;
2500 }
2501
Chris Wilson2cf34d72010-09-14 13:03:28 +01002502 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002503 if (ret < 0)
2504 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002505
Daniel Vetterae3db242010-02-19 11:51:58 +01002506 obj_priv->fence_reg = ret;
2507 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002508 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002509
Jesse Barnesde151cf2008-11-12 10:03:55 -08002510 reg->obj = obj;
2511
Chris Wilsone259bef2010-09-17 00:32:02 +01002512 switch (INTEL_INFO(dev)->gen) {
2513 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002514 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002515 break;
2516 case 5:
2517 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002518 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002519 break;
2520 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002521 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002522 break;
2523 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002524 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002525 break;
2526 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002527
Daniel Vetterae3db242010-02-19 11:51:58 +01002528 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2529 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002530
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002531 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002532}
2533
2534/**
2535 * i915_gem_clear_fence_reg - clear out fence register info
2536 * @obj: object to clear
2537 *
2538 * Zeroes out the fence register itself and clears out the associated
2539 * data structures in dev_priv and obj_priv.
2540 */
2541static void
2542i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2543{
2544 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002545 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002546 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002547 struct drm_i915_fence_reg *reg =
2548 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002549 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002550
Chris Wilsone259bef2010-09-17 00:32:02 +01002551 switch (INTEL_INFO(dev)->gen) {
2552 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002553 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2554 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002555 break;
2556 case 5:
2557 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002558 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002559 break;
2560 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002561 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002562 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002563 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002564 case 2:
2565 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002566
2567 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002568 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002569 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002570
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002571 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002572 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002573 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002574}
2575
Eric Anholt673a3942008-07-30 12:06:12 -07002576/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002577 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2578 * to the buffer to finish, and then resets the fence register.
2579 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002580 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002581 *
2582 * Zeroes out the fence register itself and clears out the associated
2583 * data structures in dev_priv and obj_priv.
2584 */
2585int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002586i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2587 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002588{
2589 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002590 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002591 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002592 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002593
2594 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2595 return 0;
2596
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002597 /* If we've changed tiling, GTT-mappings of the object
2598 * need to re-fault to ensure that the correct fence register
2599 * setup is in place.
2600 */
2601 i915_gem_release_mmap(obj);
2602
Chris Wilson52dc7d32009-06-06 09:46:01 +01002603 /* On the i915, GPU access to tiled buffers is via a fence,
2604 * therefore we must wait for any outstanding access to complete
2605 * before clearing the fence.
2606 */
Chris Wilson53640e12010-09-20 11:40:50 +01002607 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2608 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002609 int ret;
2610
Chris Wilson2cf34d72010-09-14 13:03:28 +01002611 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002612 if (ret)
2613 return ret;
2614
Chris Wilson2cf34d72010-09-14 13:03:28 +01002615 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002616 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002617 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002618
2619 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002620 }
2621
Daniel Vetter4a726612010-02-01 13:59:16 +01002622 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002623 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002624
2625 return 0;
2626}
2627
2628/**
Eric Anholt673a3942008-07-30 12:06:12 -07002629 * Finds free space in the GTT aperture and binds the object there.
2630 */
2631static int
2632i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2633{
2634 struct drm_device *dev = obj->dev;
2635 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002636 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002637 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002638 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002639 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002640
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002641 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002642 DRM_ERROR("Attempting to bind a purgeable object\n");
2643 return -EINVAL;
2644 }
2645
Eric Anholt673a3942008-07-30 12:06:12 -07002646 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002647 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002648 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002649 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2650 return -EINVAL;
2651 }
2652
Chris Wilson654fc602010-05-27 13:18:21 +01002653 /* If the object is bigger than the entire aperture, reject it early
2654 * before evicting everything in a vain attempt to find space.
2655 */
Chris Wilson73aa8082010-09-30 11:46:12 +01002656 if (obj->size > dev_priv->mm.gtt_total) {
Chris Wilson654fc602010-05-27 13:18:21 +01002657 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2658 return -E2BIG;
2659 }
2660
Eric Anholt673a3942008-07-30 12:06:12 -07002661 search_free:
2662 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2663 obj->size, alignment, 0);
2664 if (free_space != NULL) {
2665 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2666 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002667 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002668 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002669 }
2670 if (obj_priv->gtt_space == NULL) {
2671 /* If the gtt is empty and we're still having trouble
2672 * fitting our object in, we're out of memory.
2673 */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002674 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002675 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002676 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002677
Eric Anholt673a3942008-07-30 12:06:12 -07002678 goto search_free;
2679 }
2680
Chris Wilson4bdadb92010-01-27 13:36:32 +00002681 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002682 if (ret) {
2683 drm_mm_put_block(obj_priv->gtt_space);
2684 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002685
2686 if (ret == -ENOMEM) {
2687 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002688 ret = i915_gem_evict_something(dev, obj->size,
2689 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002690 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002691 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002692 if (gfpmask) {
2693 gfpmask = 0;
2694 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002695 }
2696
2697 return ret;
2698 }
2699
2700 goto search_free;
2701 }
2702
Eric Anholt673a3942008-07-30 12:06:12 -07002703 return ret;
2704 }
2705
Eric Anholt673a3942008-07-30 12:06:12 -07002706 /* Create an AGP memory structure pointing at our pages, and bind it
2707 * into the GTT.
2708 */
2709 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002710 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002711 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002712 obj_priv->gtt_offset,
2713 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002714 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002715 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002716 drm_mm_put_block(obj_priv->gtt_space);
2717 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002718
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002719 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002720 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002721 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002722
2723 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002724 }
Eric Anholt673a3942008-07-30 12:06:12 -07002725
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002726 /* keep track of bounds object by adding it to the inactive list */
2727 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01002728 i915_gem_info_add_gtt(dev_priv, obj->size);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002729
Eric Anholt673a3942008-07-30 12:06:12 -07002730 /* Assert that the object is not currently in any GPU domain. As it
2731 * wasn't in the GTT, there shouldn't be any way it could have been in
2732 * a GPU cache
2733 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002734 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2735 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002736
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002737 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2738
Eric Anholt673a3942008-07-30 12:06:12 -07002739 return 0;
2740}
2741
2742void
2743i915_gem_clflush_object(struct drm_gem_object *obj)
2744{
Daniel Vetter23010e42010-03-08 13:35:02 +01002745 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002746
2747 /* If we don't have a page list set up, then we're not pinned
2748 * to GPU, and we can ignore the cache flush because it'll happen
2749 * again at bind time.
2750 */
Eric Anholt856fa192009-03-19 14:10:50 -07002751 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002752 return;
2753
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002754 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002755
Eric Anholt856fa192009-03-19 14:10:50 -07002756 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002757}
2758
Eric Anholte47c68e2008-11-14 13:35:19 -08002759/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002760static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002761i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2762 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002763{
2764 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002765 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002766
2767 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002768 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002769
2770 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002771 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002772 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002773 to_intel_bo(obj)->ring,
2774 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002775 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002776
2777 trace_i915_gem_object_change_domain(obj,
2778 obj->read_domains,
2779 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002780
2781 if (pipelined)
2782 return 0;
2783
Chris Wilson2cf34d72010-09-14 13:03:28 +01002784 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002785}
2786
2787/** Flushes the GTT write domain for the object if it's dirty. */
2788static void
2789i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2790{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002791 uint32_t old_write_domain;
2792
Eric Anholte47c68e2008-11-14 13:35:19 -08002793 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2794 return;
2795
2796 /* No actual flushing is required for the GTT write domain. Writes
2797 * to it immediately go to main memory as far as we know, so there's
2798 * no chipset flush. It also doesn't land in render cache.
2799 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002800 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002801 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002802
2803 trace_i915_gem_object_change_domain(obj,
2804 obj->read_domains,
2805 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002806}
2807
2808/** Flushes the CPU write domain for the object if it's dirty. */
2809static void
2810i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2811{
2812 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002813 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002814
2815 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2816 return;
2817
2818 i915_gem_clflush_object(obj);
2819 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002820 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002821 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002822
2823 trace_i915_gem_object_change_domain(obj,
2824 obj->read_domains,
2825 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002826}
2827
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002828/**
2829 * Moves a single object to the GTT read, and possibly write domain.
2830 *
2831 * This function returns when the move is complete, including waiting on
2832 * flushes to occur.
2833 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002834int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002835i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2836{
Daniel Vetter23010e42010-03-08 13:35:02 +01002837 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002838 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002839 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002840
Eric Anholt02354392008-11-26 13:58:13 -08002841 /* Not valid to be called on unbound objects. */
2842 if (obj_priv->gtt_space == NULL)
2843 return -EINVAL;
2844
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002845 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002846 if (ret != 0)
2847 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002848
Chris Wilson72133422010-09-13 23:56:38 +01002849 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002850
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002851 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002852 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002853 if (ret)
2854 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002855 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002856
Chris Wilson72133422010-09-13 23:56:38 +01002857 old_write_domain = obj->write_domain;
2858 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002859
2860 /* It should now be out of any other write domains, and we can update
2861 * the domain values for our changes.
2862 */
2863 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2864 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002865 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002866 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002867 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002868 obj_priv->dirty = 1;
2869 }
2870
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002871 trace_i915_gem_object_change_domain(obj,
2872 old_read_domains,
2873 old_write_domain);
2874
Eric Anholte47c68e2008-11-14 13:35:19 -08002875 return 0;
2876}
2877
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002878/*
2879 * Prepare buffer for display plane. Use uninterruptible for possible flush
2880 * wait, as in modesetting process we're not supposed to be interrupted.
2881 */
2882int
Chris Wilson48b956c2010-09-14 12:50:34 +01002883i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2884 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002885{
Daniel Vetter23010e42010-03-08 13:35:02 +01002886 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002887 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002888 int ret;
2889
2890 /* Not valid to be called on unbound objects. */
2891 if (obj_priv->gtt_space == NULL)
2892 return -EINVAL;
2893
Chris Wilsonced270f2010-09-26 22:47:46 +01002894 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson48b956c2010-09-14 12:50:34 +01002895 if (ret)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002896 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002897
Chris Wilsonced270f2010-09-26 22:47:46 +01002898 /* Currently, we are always called from an non-interruptible context. */
2899 if (!pipelined) {
2900 ret = i915_gem_object_wait_rendering(obj, false);
2901 if (ret)
2902 return ret;
2903 }
2904
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002905 i915_gem_object_flush_cpu_write_domain(obj);
2906
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002907 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002908 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002909
2910 trace_i915_gem_object_change_domain(obj,
2911 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002912 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002913
2914 return 0;
2915}
2916
Eric Anholte47c68e2008-11-14 13:35:19 -08002917/**
2918 * Moves a single object to the CPU read, and possibly write domain.
2919 *
2920 * This function returns when the move is complete, including waiting on
2921 * flushes to occur.
2922 */
2923static int
2924i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2925{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002926 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002927 int ret;
2928
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002929 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002930 if (ret != 0)
2931 return ret;
2932
2933 i915_gem_object_flush_gtt_write_domain(obj);
2934
2935 /* If we have a partially-valid cache of the object in the CPU,
2936 * finish invalidating it and free the per-page flags.
2937 */
2938 i915_gem_object_set_to_full_cpu_read_domain(obj);
2939
Chris Wilson72133422010-09-13 23:56:38 +01002940 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002941 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002942 if (ret)
2943 return ret;
2944 }
2945
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002946 old_write_domain = obj->write_domain;
2947 old_read_domains = obj->read_domains;
2948
Eric Anholte47c68e2008-11-14 13:35:19 -08002949 /* Flush the CPU cache if it's still invalid. */
2950 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2951 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002952
2953 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2954 }
2955
2956 /* It should now be out of any other write domains, and we can update
2957 * the domain values for our changes.
2958 */
2959 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2960
2961 /* If we're writing through the CPU, then the GPU read domains will
2962 * need to be invalidated at next use.
2963 */
2964 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002965 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002966 obj->write_domain = I915_GEM_DOMAIN_CPU;
2967 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002968
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002969 trace_i915_gem_object_change_domain(obj,
2970 old_read_domains,
2971 old_write_domain);
2972
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002973 return 0;
2974}
2975
Eric Anholt673a3942008-07-30 12:06:12 -07002976/*
2977 * Set the next domain for the specified object. This
2978 * may not actually perform the necessary flushing/invaliding though,
2979 * as that may want to be batched with other set_domain operations
2980 *
2981 * This is (we hope) the only really tricky part of gem. The goal
2982 * is fairly simple -- track which caches hold bits of the object
2983 * and make sure they remain coherent. A few concrete examples may
2984 * help to explain how it works. For shorthand, we use the notation
2985 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2986 * a pair of read and write domain masks.
2987 *
2988 * Case 1: the batch buffer
2989 *
2990 * 1. Allocated
2991 * 2. Written by CPU
2992 * 3. Mapped to GTT
2993 * 4. Read by GPU
2994 * 5. Unmapped from GTT
2995 * 6. Freed
2996 *
2997 * Let's take these a step at a time
2998 *
2999 * 1. Allocated
3000 * Pages allocated from the kernel may still have
3001 * cache contents, so we set them to (CPU, CPU) always.
3002 * 2. Written by CPU (using pwrite)
3003 * The pwrite function calls set_domain (CPU, CPU) and
3004 * this function does nothing (as nothing changes)
3005 * 3. Mapped by GTT
3006 * This function asserts that the object is not
3007 * currently in any GPU-based read or write domains
3008 * 4. Read by GPU
3009 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3010 * As write_domain is zero, this function adds in the
3011 * current read domains (CPU+COMMAND, 0).
3012 * flush_domains is set to CPU.
3013 * invalidate_domains is set to COMMAND
3014 * clflush is run to get data out of the CPU caches
3015 * then i915_dev_set_domain calls i915_gem_flush to
3016 * emit an MI_FLUSH and drm_agp_chipset_flush
3017 * 5. Unmapped from GTT
3018 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3019 * flush_domains and invalidate_domains end up both zero
3020 * so no flushing/invalidating happens
3021 * 6. Freed
3022 * yay, done
3023 *
3024 * Case 2: The shared render buffer
3025 *
3026 * 1. Allocated
3027 * 2. Mapped to GTT
3028 * 3. Read/written by GPU
3029 * 4. set_domain to (CPU,CPU)
3030 * 5. Read/written by CPU
3031 * 6. Read/written by GPU
3032 *
3033 * 1. Allocated
3034 * Same as last example, (CPU, CPU)
3035 * 2. Mapped to GTT
3036 * Nothing changes (assertions find that it is not in the GPU)
3037 * 3. Read/written by GPU
3038 * execbuffer calls set_domain (RENDER, RENDER)
3039 * flush_domains gets CPU
3040 * invalidate_domains gets GPU
3041 * clflush (obj)
3042 * MI_FLUSH and drm_agp_chipset_flush
3043 * 4. set_domain (CPU, CPU)
3044 * flush_domains gets GPU
3045 * invalidate_domains gets CPU
3046 * wait_rendering (obj) to make sure all drawing is complete.
3047 * This will include an MI_FLUSH to get the data from GPU
3048 * to memory
3049 * clflush (obj) to invalidate the CPU cache
3050 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3051 * 5. Read/written by CPU
3052 * cache lines are loaded and dirtied
3053 * 6. Read written by GPU
3054 * Same as last GPU access
3055 *
3056 * Case 3: The constant buffer
3057 *
3058 * 1. Allocated
3059 * 2. Written by CPU
3060 * 3. Read by GPU
3061 * 4. Updated (written) by CPU again
3062 * 5. Read by GPU
3063 *
3064 * 1. Allocated
3065 * (CPU, CPU)
3066 * 2. Written by CPU
3067 * (CPU, CPU)
3068 * 3. Read by GPU
3069 * (CPU+RENDER, 0)
3070 * flush_domains = CPU
3071 * invalidate_domains = RENDER
3072 * clflush (obj)
3073 * MI_FLUSH
3074 * drm_agp_chipset_flush
3075 * 4. Updated (written) by CPU again
3076 * (CPU, CPU)
3077 * flush_domains = 0 (no previous write domain)
3078 * invalidate_domains = 0 (no new read domains)
3079 * 5. Read by GPU
3080 * (CPU+RENDER, 0)
3081 * flush_domains = CPU
3082 * invalidate_domains = RENDER
3083 * clflush (obj)
3084 * MI_FLUSH
3085 * drm_agp_chipset_flush
3086 */
Keith Packardc0d90822008-11-20 23:11:08 -08003087static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003088i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003089{
3090 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003091 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003092 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003093 uint32_t invalidate_domains = 0;
3094 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003095 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003096
Jesse Barnes652c3932009-08-17 13:31:43 -07003097 intel_mark_busy(dev, obj);
3098
Eric Anholt673a3942008-07-30 12:06:12 -07003099 /*
3100 * If the object isn't moving to a new write domain,
3101 * let the object stay in multiple read domains
3102 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003103 if (obj->pending_write_domain == 0)
3104 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003105 else
3106 obj_priv->dirty = 1;
3107
3108 /*
3109 * Flush the current write domain if
3110 * the new read domains don't match. Invalidate
3111 * any read domains which differ from the old
3112 * write domain
3113 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003114 if (obj->write_domain &&
3115 obj->write_domain != obj->pending_read_domains) {
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 Wilson92204342010-09-18 11:02:01 +01003142 if (obj_priv->ring)
3143 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003144
3145 trace_i915_gem_object_change_domain(obj,
3146 old_read_domains,
3147 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003148}
3149
3150/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003151 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003152 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003153 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3154 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3155 */
3156static void
3157i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3158{
Daniel Vetter23010e42010-03-08 13:35:02 +01003159 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003160
3161 if (!obj_priv->page_cpu_valid)
3162 return;
3163
3164 /* If we're partially in the CPU read domain, finish moving it in.
3165 */
3166 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3167 int i;
3168
3169 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3170 if (obj_priv->page_cpu_valid[i])
3171 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003172 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003173 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003174 }
3175
3176 /* Free the page_cpu_valid mappings which are now stale, whether
3177 * or not we've got I915_GEM_DOMAIN_CPU.
3178 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003179 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003180 obj_priv->page_cpu_valid = NULL;
3181}
3182
3183/**
3184 * Set the CPU read domain on a range of the object.
3185 *
3186 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3187 * not entirely valid. The page_cpu_valid member of the object flags which
3188 * pages have been flushed, and will be respected by
3189 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3190 * of the whole object.
3191 *
3192 * This function returns when the move is complete, including waiting on
3193 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003194 */
3195static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003196i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3197 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003198{
Daniel Vetter23010e42010-03-08 13:35:02 +01003199 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003200 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003201 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003202
Eric Anholte47c68e2008-11-14 13:35:19 -08003203 if (offset == 0 && size == obj->size)
3204 return i915_gem_object_set_to_cpu_domain(obj, 0);
3205
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003206 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003207 if (ret != 0)
3208 return ret;
3209 i915_gem_object_flush_gtt_write_domain(obj);
3210
3211 /* If we're already fully in the CPU read domain, we're done. */
3212 if (obj_priv->page_cpu_valid == NULL &&
3213 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003214 return 0;
3215
Eric Anholte47c68e2008-11-14 13:35:19 -08003216 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3217 * newly adding I915_GEM_DOMAIN_CPU
3218 */
Eric Anholt673a3942008-07-30 12:06:12 -07003219 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003220 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3221 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003222 if (obj_priv->page_cpu_valid == NULL)
3223 return -ENOMEM;
3224 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3225 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003226
3227 /* Flush the cache on any pages that are still invalid from the CPU's
3228 * perspective.
3229 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003230 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3231 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003232 if (obj_priv->page_cpu_valid[i])
3233 continue;
3234
Eric Anholt856fa192009-03-19 14:10:50 -07003235 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003236
3237 obj_priv->page_cpu_valid[i] = 1;
3238 }
3239
Eric Anholte47c68e2008-11-14 13:35:19 -08003240 /* It should now be out of any other write domains, and we can update
3241 * the domain values for our changes.
3242 */
3243 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3244
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003245 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003246 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3247
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003248 trace_i915_gem_object_change_domain(obj,
3249 old_read_domains,
3250 obj->write_domain);
3251
Eric Anholt673a3942008-07-30 12:06:12 -07003252 return 0;
3253}
3254
3255/**
Eric Anholt673a3942008-07-30 12:06:12 -07003256 * Pin an object to the GTT and evaluate the relocations landing in it.
3257 */
3258static int
3259i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3260 struct drm_file *file_priv,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003261 struct drm_i915_gem_exec_object2 *entry)
Eric Anholt673a3942008-07-30 12:06:12 -07003262{
3263 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003264 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003265 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson2549d6c2010-10-14 12:10:41 +01003266 struct drm_i915_gem_relocation_entry __user *user_relocs;
Eric Anholt673a3942008-07-30 12:06:12 -07003267 int i, ret;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003268 bool need_fence;
3269
3270 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3271 obj_priv->tiling_mode != I915_TILING_NONE;
3272
3273 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003274 if (need_fence &&
3275 !i915_gem_object_fence_offset_ok(obj,
3276 obj_priv->tiling_mode)) {
3277 ret = i915_gem_object_unbind(obj);
3278 if (ret)
3279 return ret;
3280 }
Eric Anholt673a3942008-07-30 12:06:12 -07003281
3282 /* Choose the GTT offset for our buffer and put it there. */
3283 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3284 if (ret)
3285 return ret;
3286
Jesse Barnes76446ca2009-12-17 22:05:42 -05003287 /*
3288 * Pre-965 chips need a fence register set up in order to
3289 * properly handle blits to/from tiled surfaces.
3290 */
3291 if (need_fence) {
Chris Wilson53640e12010-09-20 11:40:50 +01003292 ret = i915_gem_object_get_fence_reg(obj, true);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003293 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003294 i915_gem_object_unpin(obj);
3295 return ret;
3296 }
Chris Wilson53640e12010-09-20 11:40:50 +01003297
3298 dev_priv->fence_regs[obj_priv->fence_reg].gpu = true;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003299 }
3300
Eric Anholt673a3942008-07-30 12:06:12 -07003301 entry->offset = obj_priv->gtt_offset;
3302
Eric Anholt673a3942008-07-30 12:06:12 -07003303 /* Apply the relocations, using the GTT aperture to avoid cache
3304 * flushing requirements.
3305 */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003306 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Eric Anholt673a3942008-07-30 12:06:12 -07003307 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2549d6c2010-10-14 12:10:41 +01003308 struct drm_i915_gem_relocation_entry reloc;
Eric Anholt673a3942008-07-30 12:06:12 -07003309 struct drm_gem_object *target_obj;
3310 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003311
Chris Wilson2549d6c2010-10-14 12:10:41 +01003312 ret = __copy_from_user_inatomic(&reloc,
3313 user_relocs+i,
3314 sizeof(reloc));
3315 if (ret) {
3316 i915_gem_object_unpin(obj);
3317 return -EFAULT;
3318 }
3319
Eric Anholt673a3942008-07-30 12:06:12 -07003320 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003321 reloc.target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003322 if (target_obj == NULL) {
3323 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003324 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003325 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003326 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003327
Chris Wilson8542a0b2009-09-09 21:15:15 +01003328#if WATCH_RELOC
3329 DRM_INFO("%s: obj %p offset %08x target %d "
3330 "read %08x write %08x gtt %08x "
3331 "presumed %08x delta %08x\n",
3332 __func__,
3333 obj,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003334 (int) reloc.offset,
3335 (int) reloc.target_handle,
3336 (int) reloc.read_domains,
3337 (int) reloc.write_domain,
Chris Wilson8542a0b2009-09-09 21:15:15 +01003338 (int) target_obj_priv->gtt_offset,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003339 (int) reloc.presumed_offset,
3340 reloc.delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003341#endif
3342
Eric Anholt673a3942008-07-30 12:06:12 -07003343 /* The target buffer should have appeared before us in the
3344 * exec_object list, so it should have a GTT space bound by now.
3345 */
3346 if (target_obj_priv->gtt_space == NULL) {
3347 DRM_ERROR("No GTT space found for object %d\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003348 reloc.target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003349 drm_gem_object_unreference(target_obj);
3350 i915_gem_object_unpin(obj);
3351 return -EINVAL;
3352 }
3353
Chris Wilson8542a0b2009-09-09 21:15:15 +01003354 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003355 if (reloc.write_domain & (reloc.write_domain - 1)) {
Daniel Vetter16edd552010-02-19 11:52:02 +01003356 DRM_ERROR("reloc with multiple write domains: "
3357 "obj %p target %d offset %d "
3358 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003359 obj, reloc.target_handle,
3360 (int) reloc.offset,
3361 reloc.read_domains,
3362 reloc.write_domain);
Julia Lawall929f49b2010-10-02 15:59:17 +02003363 drm_gem_object_unreference(target_obj);
3364 i915_gem_object_unpin(obj);
Daniel Vetter16edd552010-02-19 11:52:02 +01003365 return -EINVAL;
3366 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003367 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
3368 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003369 DRM_ERROR("reloc with read/write CPU domains: "
3370 "obj %p target %d offset %d "
3371 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003372 obj, reloc.target_handle,
3373 (int) reloc.offset,
3374 reloc.read_domains,
3375 reloc.write_domain);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003376 drm_gem_object_unreference(target_obj);
3377 i915_gem_object_unpin(obj);
3378 return -EINVAL;
3379 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003380 if (reloc.write_domain && target_obj->pending_write_domain &&
3381 reloc.write_domain != target_obj->pending_write_domain) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003382 DRM_ERROR("Write domain conflict: "
3383 "obj %p target %d offset %d "
3384 "new %08x old %08x\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003385 obj, reloc.target_handle,
3386 (int) reloc.offset,
3387 reloc.write_domain,
Chris Wilson8542a0b2009-09-09 21:15:15 +01003388 target_obj->pending_write_domain);
3389 drm_gem_object_unreference(target_obj);
3390 i915_gem_object_unpin(obj);
3391 return -EINVAL;
3392 }
3393
Chris Wilson2549d6c2010-10-14 12:10:41 +01003394 target_obj->pending_read_domains |= reloc.read_domains;
3395 target_obj->pending_write_domain |= reloc.write_domain;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003396
3397 /* If the relocation already has the right value in it, no
3398 * more work needs to be done.
3399 */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003400 if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003401 drm_gem_object_unreference(target_obj);
3402 continue;
3403 }
3404
3405 /* Check that the relocation address is valid... */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003406 if (reloc.offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003407 DRM_ERROR("Relocation beyond object bounds: "
3408 "obj %p target %d offset %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003409 obj, reloc.target_handle,
3410 (int) reloc.offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003411 drm_gem_object_unreference(target_obj);
3412 i915_gem_object_unpin(obj);
3413 return -EINVAL;
3414 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003415 if (reloc.offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003416 DRM_ERROR("Relocation not 4-byte aligned: "
3417 "obj %p target %d offset %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003418 obj, reloc.target_handle,
3419 (int) reloc.offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003420 drm_gem_object_unreference(target_obj);
3421 i915_gem_object_unpin(obj);
3422 return -EINVAL;
3423 }
3424
Chris Wilson8542a0b2009-09-09 21:15:15 +01003425 /* and points to somewhere within the target object. */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003426 if (reloc.delta >= target_obj->size) {
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003427 DRM_ERROR("Relocation beyond target object bounds: "
3428 "obj %p target %d delta %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003429 obj, reloc.target_handle,
3430 (int) reloc.delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003431 drm_gem_object_unreference(target_obj);
3432 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003433 return -EINVAL;
3434 }
3435
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003436 reloc.delta += target_obj_priv->gtt_offset;
3437 if (obj->write_domain == I915_GEM_DOMAIN_CPU) {
3438 uint32_t page_offset = reloc.offset & ~PAGE_MASK;
3439 char *vaddr;
3440
3441 vaddr = kmap_atomic(obj_priv->pages[reloc.offset >> PAGE_SHIFT], KM_USER0);
3442 *(uint32_t *)(vaddr + page_offset) = reloc.delta;
3443 kunmap_atomic(vaddr, KM_USER0);
3444 } else {
3445 uint32_t __iomem *reloc_entry;
3446 void __iomem *reloc_page;
3447 int ret;
3448
3449 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3450 if (ret) {
3451 drm_gem_object_unreference(target_obj);
3452 i915_gem_object_unpin(obj);
3453 return ret;
3454 }
3455
3456 /* Map the page containing the relocation we're going to perform. */
3457 reloc.offset += obj_priv->gtt_offset;
3458 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3459 reloc.offset & PAGE_MASK,
3460 KM_USER0);
3461 reloc_entry = (uint32_t __iomem *)
3462 (reloc_page + (reloc.offset & ~PAGE_MASK));
3463 iowrite32(reloc.delta, reloc_entry);
3464 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003465 }
3466
Eric Anholt673a3942008-07-30 12:06:12 -07003467 drm_gem_object_unreference(target_obj);
3468 }
3469
Eric Anholt673a3942008-07-30 12:06:12 -07003470 return 0;
3471}
3472
Eric Anholt673a3942008-07-30 12:06:12 -07003473/* Throttle our rendering by waiting until the ring has completed our requests
3474 * emitted over 20 msec ago.
3475 *
Eric Anholtb9624422009-06-03 07:27:35 +00003476 * Note that if we were to use the current jiffies each time around the loop,
3477 * we wouldn't escape the function with any frames outstanding if the time to
3478 * render a frame was over 20ms.
3479 *
Eric Anholt673a3942008-07-30 12:06:12 -07003480 * This should get us reasonable parallelism between CPU and GPU but also
3481 * relatively low latency when blocking on a particular request to finish.
3482 */
3483static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003484i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003485{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003486 struct drm_i915_private *dev_priv = dev->dev_private;
3487 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003488 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003489 struct drm_i915_gem_request *request;
3490 struct intel_ring_buffer *ring = NULL;
3491 u32 seqno = 0;
3492 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003493
Chris Wilson1c255952010-09-26 11:03:27 +01003494 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003495 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003496 if (time_after_eq(request->emitted_jiffies, recent_enough))
3497 break;
3498
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003499 ring = request->ring;
3500 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003501 }
Chris Wilson1c255952010-09-26 11:03:27 +01003502 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003503
3504 if (seqno == 0)
3505 return 0;
3506
3507 ret = 0;
3508 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3509 /* And wait for the seqno passing without holding any locks and
3510 * causing extra latency for others. This is safe as the irq
3511 * generation is designed to be run atomically and so is
3512 * lockless.
3513 */
3514 ring->user_irq_get(dev, ring);
3515 ret = wait_event_interruptible(ring->irq_queue,
3516 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3517 || atomic_read(&dev_priv->mm.wedged));
3518 ring->user_irq_put(dev, ring);
3519
3520 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3521 ret = -EIO;
3522 }
3523
3524 if (ret == 0)
3525 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003526
Eric Anholt673a3942008-07-30 12:06:12 -07003527 return ret;
3528}
3529
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003530static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003531i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3532 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003533{
3534 uint32_t exec_start, exec_len;
3535
3536 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3537 exec_len = (uint32_t) exec->batch_len;
3538
3539 if ((exec_start | exec_len) & 0x7)
3540 return -EINVAL;
3541
3542 if (!exec_start)
3543 return -EINVAL;
3544
3545 return 0;
3546}
3547
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003548static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003549validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3550 int count)
3551{
3552 int i;
3553
3554 for (i = 0; i < count; i++) {
3555 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3556 size_t length = exec[i].relocation_count * sizeof(struct drm_i915_gem_relocation_entry);
3557
3558 if (!access_ok(VERIFY_READ, ptr, length))
3559 return -EFAULT;
3560
3561 if (fault_in_pages_readable(ptr, length))
3562 return -EFAULT;
3563 }
3564
3565 return 0;
3566}
3567
3568static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003569i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3570 struct drm_file *file_priv,
3571 struct drm_i915_gem_execbuffer2 *args,
3572 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003573{
3574 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003575 struct drm_gem_object **object_list = NULL;
3576 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003577 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003578 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003579 struct drm_i915_gem_request *request = NULL;
Chris Wilson2549d6c2010-10-14 12:10:41 +01003580 int ret, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003581 uint64_t exec_offset;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003582 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003583
Zou Nan hai852835f2010-05-21 09:08:56 +08003584 struct intel_ring_buffer *ring = NULL;
3585
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003586 ret = i915_gem_check_is_wedged(dev);
3587 if (ret)
3588 return ret;
3589
Chris Wilson2549d6c2010-10-14 12:10:41 +01003590 ret = validate_exec_list(exec_list, args->buffer_count);
3591 if (ret)
3592 return ret;
3593
Eric Anholt673a3942008-07-30 12:06:12 -07003594#if WATCH_EXEC
3595 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3596 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3597#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003598 if (args->flags & I915_EXEC_BSD) {
3599 if (!HAS_BSD(dev)) {
3600 DRM_ERROR("execbuf with wrong flag\n");
3601 return -EINVAL;
3602 }
3603 ring = &dev_priv->bsd_ring;
3604 } else {
3605 ring = &dev_priv->render_ring;
3606 }
3607
Eric Anholt4f481ed2008-09-10 14:22:49 -07003608 if (args->buffer_count < 1) {
3609 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3610 return -EINVAL;
3611 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003612 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003613 if (object_list == NULL) {
3614 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003615 args->buffer_count);
3616 ret = -ENOMEM;
3617 goto pre_mutex_err;
3618 }
Eric Anholt673a3942008-07-30 12:06:12 -07003619
Eric Anholt201361a2009-03-11 12:30:04 -07003620 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003621 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3622 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003623 if (cliprects == NULL) {
3624 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003625 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003626 }
Eric Anholt201361a2009-03-11 12:30:04 -07003627
3628 ret = copy_from_user(cliprects,
3629 (struct drm_clip_rect __user *)
3630 (uintptr_t) args->cliprects_ptr,
3631 sizeof(*cliprects) * args->num_cliprects);
3632 if (ret != 0) {
3633 DRM_ERROR("copy %d cliprects failed: %d\n",
3634 args->num_cliprects, ret);
Dan Carpenterc877cdce2010-06-23 19:03:01 +02003635 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003636 goto pre_mutex_err;
3637 }
3638 }
3639
Chris Wilson8dc5d142010-08-12 12:36:12 +01003640 request = kzalloc(sizeof(*request), GFP_KERNEL);
3641 if (request == NULL) {
3642 ret = -ENOMEM;
3643 goto pre_mutex_err;
3644 }
3645
Chris Wilson76c1dec2010-09-25 11:22:51 +01003646 ret = i915_mutex_lock_interruptible(dev);
3647 if (ret)
3648 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003649
Eric Anholt673a3942008-07-30 12:06:12 -07003650 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003651 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003652 ret = -EBUSY;
3653 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003654 }
3655
Keith Packardac94a962008-11-20 23:30:27 -08003656 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003657 for (i = 0; i < args->buffer_count; i++) {
3658 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3659 exec_list[i].handle);
3660 if (object_list[i] == NULL) {
3661 DRM_ERROR("Invalid object handle %d at index %d\n",
3662 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003663 /* prevent error path from reading uninitialized data */
3664 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003665 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003666 goto err;
3667 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003668
Daniel Vetter23010e42010-03-08 13:35:02 +01003669 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003670 if (obj_priv->in_execbuffer) {
3671 DRM_ERROR("Object %p appears more than once in object list\n",
3672 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003673 /* prevent error path from reading uninitialized data */
3674 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003675 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003676 goto err;
3677 }
3678 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003679 }
Eric Anholt673a3942008-07-30 12:06:12 -07003680
Keith Packardac94a962008-11-20 23:30:27 -08003681 /* Pin and relocate */
3682 for (pin_tries = 0; ; pin_tries++) {
3683 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003684
Keith Packardac94a962008-11-20 23:30:27 -08003685 for (i = 0; i < args->buffer_count; i++) {
3686 object_list[i]->pending_read_domains = 0;
3687 object_list[i]->pending_write_domain = 0;
3688 ret = i915_gem_object_pin_and_relocate(object_list[i],
3689 file_priv,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003690 &exec_list[i]);
Keith Packardac94a962008-11-20 23:30:27 -08003691 if (ret)
3692 break;
3693 pinned = i + 1;
3694 }
3695 /* success */
3696 if (ret == 0)
3697 break;
3698
3699 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003700 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003701 if (ret != -ERESTARTSYS) {
3702 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003703 int num_fences = 0;
3704 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003705 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003706
Chris Wilson07f73f62009-09-14 16:50:30 +01003707 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003708 num_fences +=
3709 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3710 obj_priv->tiling_mode != I915_TILING_NONE;
3711 }
3712 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003713 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003714 total_size, num_fences,
3715 ret);
Chris Wilson73aa8082010-09-30 11:46:12 +01003716 DRM_ERROR("%u objects [%u pinned, %u GTT], "
3717 "%zu object bytes [%zu pinned], "
3718 "%zu /%zu gtt bytes\n",
3719 dev_priv->mm.object_count,
3720 dev_priv->mm.pin_count,
3721 dev_priv->mm.gtt_count,
3722 dev_priv->mm.object_memory,
3723 dev_priv->mm.pin_memory,
3724 dev_priv->mm.gtt_memory,
3725 dev_priv->mm.gtt_total);
Chris Wilson07f73f62009-09-14 16:50:30 +01003726 }
Eric Anholt673a3942008-07-30 12:06:12 -07003727 goto err;
3728 }
Keith Packardac94a962008-11-20 23:30:27 -08003729
3730 /* unpin all of our buffers */
3731 for (i = 0; i < pinned; i++)
3732 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003733 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003734
3735 /* evict everyone we can from the aperture */
3736 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003737 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003738 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003739 }
3740
3741 /* Set the pending read domains for the batch buffer to COMMAND */
3742 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003743 if (batch_obj->pending_write_domain) {
3744 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3745 ret = -EINVAL;
3746 goto err;
3747 }
3748 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003749
Chris Wilson83d60792009-06-06 09:45:57 +01003750 /* Sanity check the batch buffer, prior to moving objects */
3751 exec_offset = exec_list[args->buffer_count - 1].offset;
3752 ret = i915_gem_check_execbuffer (args, exec_offset);
3753 if (ret != 0) {
3754 DRM_ERROR("execbuf with invalid offset/length\n");
3755 goto err;
3756 }
3757
Keith Packard646f0f62008-11-20 23:23:03 -08003758 /* Zero the global flush/invalidate flags. These
3759 * will be modified as new domains are computed
3760 * for each object
3761 */
3762 dev->invalidate_domains = 0;
3763 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003764 dev_priv->mm.flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003765
Eric Anholt673a3942008-07-30 12:06:12 -07003766 for (i = 0; i < args->buffer_count; i++) {
3767 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003768
Keith Packard646f0f62008-11-20 23:23:03 -08003769 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003770 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003771 }
3772
Keith Packard646f0f62008-11-20 23:23:03 -08003773 if (dev->invalidate_domains | dev->flush_domains) {
3774#if WATCH_EXEC
3775 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3776 __func__,
3777 dev->invalidate_domains,
3778 dev->flush_domains);
3779#endif
Chris Wilsonc78ec302010-09-20 12:50:23 +01003780 i915_gem_flush(dev, file_priv,
Keith Packard646f0f62008-11-20 23:23:03 -08003781 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003782 dev->flush_domains,
3783 dev_priv->mm.flush_rings);
Daniel Vettera6910432010-02-02 17:08:37 +01003784 }
3785
Eric Anholtefbeed92009-02-19 14:54:51 -08003786 for (i = 0; i < args->buffer_count; i++) {
3787 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003788 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003789 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003790
3791 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003792 if (obj->write_domain)
3793 list_move_tail(&obj_priv->gpu_write_list,
3794 &dev_priv->mm.gpu_write_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01003795
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003796 trace_i915_gem_object_change_domain(obj,
3797 obj->read_domains,
3798 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003799 }
3800
Eric Anholt673a3942008-07-30 12:06:12 -07003801#if WATCH_COHERENCY
3802 for (i = 0; i < args->buffer_count; i++) {
3803 i915_gem_object_check_coherency(object_list[i],
3804 exec_list[i].handle);
3805 }
3806#endif
3807
Eric Anholt673a3942008-07-30 12:06:12 -07003808#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003809 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003810 args->batch_len,
3811 __func__,
3812 ~0);
3813#endif
3814
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003815 /* Check for any pending flips. As we only maintain a flip queue depth
3816 * of 1, we can simply insert a WAIT for the next display flip prior
3817 * to executing the batch and avoid stalling the CPU.
3818 */
3819 flips = 0;
3820 for (i = 0; i < args->buffer_count; i++) {
3821 if (object_list[i]->write_domain)
3822 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
3823 }
3824 if (flips) {
3825 int plane, flip_mask;
3826
3827 for (plane = 0; flips >> plane; plane++) {
3828 if (((flips >> plane) & 1) == 0)
3829 continue;
3830
3831 if (plane)
3832 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
3833 else
3834 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
3835
3836 intel_ring_begin(dev, ring, 2);
3837 intel_ring_emit(dev, ring,
3838 MI_WAIT_FOR_EVENT | flip_mask);
3839 intel_ring_emit(dev, ring, MI_NOOP);
3840 intel_ring_advance(dev, ring);
3841 }
3842 }
3843
Eric Anholt673a3942008-07-30 12:06:12 -07003844 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003845 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003846 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003847 if (ret) {
3848 DRM_ERROR("dispatch failed %d\n", ret);
3849 goto err;
3850 }
3851
3852 /*
3853 * Ensure that the commands in the batch buffer are
3854 * finished before the interrupt fires
3855 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003856 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003857
Daniel Vetter617dbe22010-02-11 22:16:02 +01003858 for (i = 0; i < args->buffer_count; i++) {
3859 struct drm_gem_object *obj = object_list[i];
3860 obj_priv = to_intel_bo(obj);
3861
3862 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01003863 }
Chris Wilsona56ba562010-09-28 10:07:56 +01003864
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003865 i915_add_request(dev, file_priv, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003866 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003867
Eric Anholt673a3942008-07-30 12:06:12 -07003868err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003869 for (i = 0; i < pinned; i++)
3870 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003871
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003872 for (i = 0; i < args->buffer_count; i++) {
3873 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003874 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003875 obj_priv->in_execbuffer = false;
3876 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003877 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003878 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003879
Eric Anholt673a3942008-07-30 12:06:12 -07003880 mutex_unlock(&dev->struct_mutex);
3881
Chris Wilson93533c22010-01-31 10:40:48 +00003882pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003883 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003884 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003885 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003886
3887 return ret;
3888}
3889
Jesse Barnes76446ca2009-12-17 22:05:42 -05003890/*
3891 * Legacy execbuffer just creates an exec2 list from the original exec object
3892 * list array and passes it to the real function.
3893 */
3894int
3895i915_gem_execbuffer(struct drm_device *dev, void *data,
3896 struct drm_file *file_priv)
3897{
3898 struct drm_i915_gem_execbuffer *args = data;
3899 struct drm_i915_gem_execbuffer2 exec2;
3900 struct drm_i915_gem_exec_object *exec_list = NULL;
3901 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3902 int ret, i;
3903
3904#if WATCH_EXEC
3905 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3906 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3907#endif
3908
3909 if (args->buffer_count < 1) {
3910 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3911 return -EINVAL;
3912 }
3913
3914 /* Copy in the exec list from userland */
3915 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3916 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3917 if (exec_list == NULL || exec2_list == NULL) {
3918 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3919 args->buffer_count);
3920 drm_free_large(exec_list);
3921 drm_free_large(exec2_list);
3922 return -ENOMEM;
3923 }
3924 ret = copy_from_user(exec_list,
3925 (struct drm_i915_relocation_entry __user *)
3926 (uintptr_t) args->buffers_ptr,
3927 sizeof(*exec_list) * args->buffer_count);
3928 if (ret != 0) {
3929 DRM_ERROR("copy %d exec entries failed %d\n",
3930 args->buffer_count, ret);
3931 drm_free_large(exec_list);
3932 drm_free_large(exec2_list);
3933 return -EFAULT;
3934 }
3935
3936 for (i = 0; i < args->buffer_count; i++) {
3937 exec2_list[i].handle = exec_list[i].handle;
3938 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3939 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3940 exec2_list[i].alignment = exec_list[i].alignment;
3941 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003942 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05003943 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3944 else
3945 exec2_list[i].flags = 0;
3946 }
3947
3948 exec2.buffers_ptr = args->buffers_ptr;
3949 exec2.buffer_count = args->buffer_count;
3950 exec2.batch_start_offset = args->batch_start_offset;
3951 exec2.batch_len = args->batch_len;
3952 exec2.DR1 = args->DR1;
3953 exec2.DR4 = args->DR4;
3954 exec2.num_cliprects = args->num_cliprects;
3955 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003956 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003957
3958 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3959 if (!ret) {
3960 /* Copy the new buffer offsets back to the user's exec list. */
3961 for (i = 0; i < args->buffer_count; i++)
3962 exec_list[i].offset = exec2_list[i].offset;
3963 /* ... and back out to userspace */
3964 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3965 (uintptr_t) args->buffers_ptr,
3966 exec_list,
3967 sizeof(*exec_list) * args->buffer_count);
3968 if (ret) {
3969 ret = -EFAULT;
3970 DRM_ERROR("failed to copy %d exec entries "
3971 "back to user (%d)\n",
3972 args->buffer_count, ret);
3973 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003974 }
3975
3976 drm_free_large(exec_list);
3977 drm_free_large(exec2_list);
3978 return ret;
3979}
3980
3981int
3982i915_gem_execbuffer2(struct drm_device *dev, void *data,
3983 struct drm_file *file_priv)
3984{
3985 struct drm_i915_gem_execbuffer2 *args = data;
3986 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3987 int ret;
3988
3989#if WATCH_EXEC
3990 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3991 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3992#endif
3993
3994 if (args->buffer_count < 1) {
3995 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
3996 return -EINVAL;
3997 }
3998
3999 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4000 if (exec2_list == NULL) {
4001 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4002 args->buffer_count);
4003 return -ENOMEM;
4004 }
4005 ret = copy_from_user(exec2_list,
4006 (struct drm_i915_relocation_entry __user *)
4007 (uintptr_t) args->buffers_ptr,
4008 sizeof(*exec2_list) * args->buffer_count);
4009 if (ret != 0) {
4010 DRM_ERROR("copy %d exec entries failed %d\n",
4011 args->buffer_count, ret);
4012 drm_free_large(exec2_list);
4013 return -EFAULT;
4014 }
4015
4016 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4017 if (!ret) {
4018 /* Copy the new buffer offsets back to the user's exec list. */
4019 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4020 (uintptr_t) args->buffers_ptr,
4021 exec2_list,
4022 sizeof(*exec2_list) * args->buffer_count);
4023 if (ret) {
4024 ret = -EFAULT;
4025 DRM_ERROR("failed to copy %d exec entries "
4026 "back to user (%d)\n",
4027 args->buffer_count, ret);
4028 }
4029 }
4030
4031 drm_free_large(exec2_list);
4032 return ret;
4033}
4034
Eric Anholt673a3942008-07-30 12:06:12 -07004035int
4036i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4037{
4038 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004039 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004040 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004041 int ret;
4042
Daniel Vetter778c3542010-05-13 11:49:44 +02004043 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004044 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004045
4046 if (obj_priv->gtt_space != NULL) {
4047 if (alignment == 0)
4048 alignment = i915_gem_get_gtt_alignment(obj);
4049 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004050 WARN(obj_priv->pin_count,
4051 "bo is already pinned with incorrect alignment:"
4052 " offset=%x, req.alignment=%x\n",
4053 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004054 ret = i915_gem_object_unbind(obj);
4055 if (ret)
4056 return ret;
4057 }
4058 }
4059
Eric Anholt673a3942008-07-30 12:06:12 -07004060 if (obj_priv->gtt_space == NULL) {
4061 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004062 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004063 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004064 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004065
Eric Anholt673a3942008-07-30 12:06:12 -07004066 obj_priv->pin_count++;
4067
4068 /* If the object is not active and not pending a flush,
4069 * remove it from the inactive list
4070 */
4071 if (obj_priv->pin_count == 1) {
Chris Wilson73aa8082010-09-30 11:46:12 +01004072 i915_gem_info_add_pin(dev_priv, obj->size);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004073 if (!obj_priv->active)
4074 list_move_tail(&obj_priv->list,
4075 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004076 }
Eric Anholt673a3942008-07-30 12:06:12 -07004077
Chris Wilson23bc5982010-09-29 16:10:57 +01004078 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004079 return 0;
4080}
4081
4082void
4083i915_gem_object_unpin(struct drm_gem_object *obj)
4084{
4085 struct drm_device *dev = obj->dev;
4086 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004087 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004088
Chris Wilson23bc5982010-09-29 16:10:57 +01004089 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004090 obj_priv->pin_count--;
4091 BUG_ON(obj_priv->pin_count < 0);
4092 BUG_ON(obj_priv->gtt_space == NULL);
4093
4094 /* If the object is no longer pinned, and is
4095 * neither active nor being flushed, then stick it on
4096 * the inactive list
4097 */
4098 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004099 if (!obj_priv->active)
Eric Anholt673a3942008-07-30 12:06:12 -07004100 list_move_tail(&obj_priv->list,
4101 &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01004102 i915_gem_info_remove_pin(dev_priv, obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07004103 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004104 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004105}
4106
4107int
4108i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4109 struct drm_file *file_priv)
4110{
4111 struct drm_i915_gem_pin *args = data;
4112 struct drm_gem_object *obj;
4113 struct drm_i915_gem_object *obj_priv;
4114 int ret;
4115
Eric Anholt673a3942008-07-30 12:06:12 -07004116 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4117 if (obj == NULL) {
4118 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4119 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004120 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004121 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004122 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004123
Chris Wilson76c1dec2010-09-25 11:22:51 +01004124 ret = i915_mutex_lock_interruptible(dev);
4125 if (ret) {
4126 drm_gem_object_unreference_unlocked(obj);
4127 return ret;
4128 }
4129
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004130 if (obj_priv->madv != I915_MADV_WILLNEED) {
4131 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004132 drm_gem_object_unreference(obj);
4133 mutex_unlock(&dev->struct_mutex);
4134 return -EINVAL;
4135 }
4136
Jesse Barnes79e53942008-11-07 14:24:08 -08004137 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4138 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4139 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004140 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004141 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004142 return -EINVAL;
4143 }
4144
4145 obj_priv->user_pin_count++;
4146 obj_priv->pin_filp = file_priv;
4147 if (obj_priv->user_pin_count == 1) {
4148 ret = i915_gem_object_pin(obj, args->alignment);
4149 if (ret != 0) {
4150 drm_gem_object_unreference(obj);
4151 mutex_unlock(&dev->struct_mutex);
4152 return ret;
4153 }
Eric Anholt673a3942008-07-30 12:06:12 -07004154 }
4155
4156 /* XXX - flush the CPU caches for pinned objects
4157 * as the X server doesn't manage domains yet
4158 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004159 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004160 args->offset = obj_priv->gtt_offset;
4161 drm_gem_object_unreference(obj);
4162 mutex_unlock(&dev->struct_mutex);
4163
4164 return 0;
4165}
4166
4167int
4168i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4169 struct drm_file *file_priv)
4170{
4171 struct drm_i915_gem_pin *args = data;
4172 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004173 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004174 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004175
4176 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4177 if (obj == NULL) {
4178 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4179 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004180 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004181 }
4182
Daniel Vetter23010e42010-03-08 13:35:02 +01004183 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004184
4185 ret = i915_mutex_lock_interruptible(dev);
4186 if (ret) {
4187 drm_gem_object_unreference_unlocked(obj);
4188 return ret;
4189 }
4190
Jesse Barnes79e53942008-11-07 14:24:08 -08004191 if (obj_priv->pin_filp != file_priv) {
4192 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4193 args->handle);
4194 drm_gem_object_unreference(obj);
4195 mutex_unlock(&dev->struct_mutex);
4196 return -EINVAL;
4197 }
4198 obj_priv->user_pin_count--;
4199 if (obj_priv->user_pin_count == 0) {
4200 obj_priv->pin_filp = NULL;
4201 i915_gem_object_unpin(obj);
4202 }
Eric Anholt673a3942008-07-30 12:06:12 -07004203
4204 drm_gem_object_unreference(obj);
4205 mutex_unlock(&dev->struct_mutex);
4206 return 0;
4207}
4208
4209int
4210i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4211 struct drm_file *file_priv)
4212{
4213 struct drm_i915_gem_busy *args = data;
4214 struct drm_gem_object *obj;
4215 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004216 int ret;
4217
Eric Anholt673a3942008-07-30 12:06:12 -07004218 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4219 if (obj == NULL) {
4220 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4221 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004222 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004223 }
4224
Chris Wilson76c1dec2010-09-25 11:22:51 +01004225 ret = i915_mutex_lock_interruptible(dev);
4226 if (ret) {
4227 drm_gem_object_unreference_unlocked(obj);
4228 return ret;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004229 }
4230
Chris Wilson0be555b2010-08-04 15:36:30 +01004231 /* Count all active objects as busy, even if they are currently not used
4232 * by the gpu. Users of this interface expect objects to eventually
4233 * become non-busy without any further actions, therefore emit any
4234 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004235 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004236 obj_priv = to_intel_bo(obj);
4237 args->busy = obj_priv->active;
4238 if (args->busy) {
4239 /* Unconditionally flush objects, even when the gpu still uses this
4240 * object. Userspace calling this function indicates that it wants to
4241 * use this buffer rather sooner than later, so issuing the required
4242 * flush earlier is beneficial.
4243 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004244 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4245 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004246 obj_priv->ring,
4247 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004248
4249 /* Update the active list for the hardware's current position.
4250 * Otherwise this only updates on a delayed timer or when irqs
4251 * are actually unmasked, and our working set ends up being
4252 * larger than required.
4253 */
4254 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4255
4256 args->busy = obj_priv->active;
4257 }
Eric Anholt673a3942008-07-30 12:06:12 -07004258
4259 drm_gem_object_unreference(obj);
4260 mutex_unlock(&dev->struct_mutex);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004261 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004262}
4263
4264int
4265i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4266 struct drm_file *file_priv)
4267{
4268 return i915_gem_ring_throttle(dev, file_priv);
4269}
4270
Chris Wilson3ef94da2009-09-14 16:50:29 +01004271int
4272i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4273 struct drm_file *file_priv)
4274{
4275 struct drm_i915_gem_madvise *args = data;
4276 struct drm_gem_object *obj;
4277 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004278 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004279
4280 switch (args->madv) {
4281 case I915_MADV_DONTNEED:
4282 case I915_MADV_WILLNEED:
4283 break;
4284 default:
4285 return -EINVAL;
4286 }
4287
4288 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4289 if (obj == NULL) {
4290 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4291 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004292 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004293 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004294 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004295
Chris Wilson76c1dec2010-09-25 11:22:51 +01004296 ret = i915_mutex_lock_interruptible(dev);
4297 if (ret) {
4298 drm_gem_object_unreference_unlocked(obj);
4299 return ret;
4300 }
4301
Chris Wilson3ef94da2009-09-14 16:50:29 +01004302 if (obj_priv->pin_count) {
4303 drm_gem_object_unreference(obj);
4304 mutex_unlock(&dev->struct_mutex);
4305
4306 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4307 return -EINVAL;
4308 }
4309
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004310 if (obj_priv->madv != __I915_MADV_PURGED)
4311 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004312
Chris Wilson2d7ef392009-09-20 23:13:10 +01004313 /* if the object is no longer bound, discard its backing storage */
4314 if (i915_gem_object_is_purgeable(obj_priv) &&
4315 obj_priv->gtt_space == NULL)
4316 i915_gem_object_truncate(obj);
4317
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004318 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4319
Chris Wilson3ef94da2009-09-14 16:50:29 +01004320 drm_gem_object_unreference(obj);
4321 mutex_unlock(&dev->struct_mutex);
4322
4323 return 0;
4324}
4325
Daniel Vetterac52bc52010-04-09 19:05:06 +00004326struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4327 size_t size)
4328{
Chris Wilson73aa8082010-09-30 11:46:12 +01004329 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004330 struct drm_i915_gem_object *obj;
4331
4332 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4333 if (obj == NULL)
4334 return NULL;
4335
4336 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4337 kfree(obj);
4338 return NULL;
4339 }
4340
Chris Wilson73aa8082010-09-30 11:46:12 +01004341 i915_gem_info_add_obj(dev_priv, size);
4342
Daniel Vetterc397b902010-04-09 19:05:07 +00004343 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4344 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4345
4346 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004347 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004348 obj->fence_reg = I915_FENCE_REG_NONE;
4349 INIT_LIST_HEAD(&obj->list);
4350 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004351 obj->madv = I915_MADV_WILLNEED;
4352
Daniel Vetterc397b902010-04-09 19:05:07 +00004353 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004354}
4355
Eric Anholt673a3942008-07-30 12:06:12 -07004356int i915_gem_init_object(struct drm_gem_object *obj)
4357{
Daniel Vetterc397b902010-04-09 19:05:07 +00004358 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004359
Eric Anholt673a3942008-07-30 12:06:12 -07004360 return 0;
4361}
4362
Chris Wilsonbe726152010-07-23 23:18:50 +01004363static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4364{
4365 struct drm_device *dev = obj->dev;
4366 drm_i915_private_t *dev_priv = dev->dev_private;
4367 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4368 int ret;
4369
4370 ret = i915_gem_object_unbind(obj);
4371 if (ret == -ERESTARTSYS) {
4372 list_move(&obj_priv->list,
4373 &dev_priv->mm.deferred_free_list);
4374 return;
4375 }
4376
4377 if (obj_priv->mmap_offset)
4378 i915_gem_free_mmap_offset(obj);
4379
4380 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004381 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004382
4383 kfree(obj_priv->page_cpu_valid);
4384 kfree(obj_priv->bit_17);
4385 kfree(obj_priv);
4386}
4387
Eric Anholt673a3942008-07-30 12:06:12 -07004388void i915_gem_free_object(struct drm_gem_object *obj)
4389{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004390 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004391 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004392
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004393 trace_i915_gem_object_destroy(obj);
4394
Eric Anholt673a3942008-07-30 12:06:12 -07004395 while (obj_priv->pin_count > 0)
4396 i915_gem_object_unpin(obj);
4397
Dave Airlie71acb5e2008-12-30 20:31:46 +10004398 if (obj_priv->phys_obj)
4399 i915_gem_detach_phys_object(dev, obj);
4400
Chris Wilsonbe726152010-07-23 23:18:50 +01004401 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004402}
4403
Jesse Barnes5669fca2009-02-17 15:13:31 -08004404int
Eric Anholt673a3942008-07-30 12:06:12 -07004405i915_gem_idle(struct drm_device *dev)
4406{
4407 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004408 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004409
Keith Packard6dbe2772008-10-14 21:41:13 -07004410 mutex_lock(&dev->struct_mutex);
4411
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004412 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004413 (dev_priv->render_ring.gem_object == NULL) ||
4414 (HAS_BSD(dev) &&
4415 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004416 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004417 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004418 }
Eric Anholt673a3942008-07-30 12:06:12 -07004419
Chris Wilson29105cc2010-01-07 10:39:13 +00004420 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004421 if (ret) {
4422 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004423 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004424 }
Eric Anholt673a3942008-07-30 12:06:12 -07004425
Chris Wilson29105cc2010-01-07 10:39:13 +00004426 /* Under UMS, be paranoid and evict. */
4427 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004428 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004429 if (ret) {
4430 mutex_unlock(&dev->struct_mutex);
4431 return ret;
4432 }
4433 }
4434
4435 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4436 * We need to replace this with a semaphore, or something.
4437 * And not confound mm.suspended!
4438 */
4439 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004440 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004441
4442 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004443 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004444
Keith Packard6dbe2772008-10-14 21:41:13 -07004445 mutex_unlock(&dev->struct_mutex);
4446
Chris Wilson29105cc2010-01-07 10:39:13 +00004447 /* Cancel the retire work handler, which should be idle now. */
4448 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4449
Eric Anholt673a3942008-07-30 12:06:12 -07004450 return 0;
4451}
4452
Jesse Barnese552eb72010-04-21 11:39:23 -07004453/*
4454 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4455 * over cache flushing.
4456 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004457static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004458i915_gem_init_pipe_control(struct drm_device *dev)
4459{
4460 drm_i915_private_t *dev_priv = dev->dev_private;
4461 struct drm_gem_object *obj;
4462 struct drm_i915_gem_object *obj_priv;
4463 int ret;
4464
Eric Anholt34dc4d42010-05-07 14:30:03 -07004465 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004466 if (obj == NULL) {
4467 DRM_ERROR("Failed to allocate seqno page\n");
4468 ret = -ENOMEM;
4469 goto err;
4470 }
4471 obj_priv = to_intel_bo(obj);
4472 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4473
4474 ret = i915_gem_object_pin(obj, 4096);
4475 if (ret)
4476 goto err_unref;
4477
4478 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4479 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4480 if (dev_priv->seqno_page == NULL)
4481 goto err_unpin;
4482
4483 dev_priv->seqno_obj = obj;
4484 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4485
4486 return 0;
4487
4488err_unpin:
4489 i915_gem_object_unpin(obj);
4490err_unref:
4491 drm_gem_object_unreference(obj);
4492err:
4493 return ret;
4494}
4495
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004496
4497static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004498i915_gem_cleanup_pipe_control(struct drm_device *dev)
4499{
4500 drm_i915_private_t *dev_priv = dev->dev_private;
4501 struct drm_gem_object *obj;
4502 struct drm_i915_gem_object *obj_priv;
4503
4504 obj = dev_priv->seqno_obj;
4505 obj_priv = to_intel_bo(obj);
4506 kunmap(obj_priv->pages[0]);
4507 i915_gem_object_unpin(obj);
4508 drm_gem_object_unreference(obj);
4509 dev_priv->seqno_obj = NULL;
4510
4511 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004512}
4513
Eric Anholt673a3942008-07-30 12:06:12 -07004514int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004515i915_gem_init_ringbuffer(struct drm_device *dev)
4516{
4517 drm_i915_private_t *dev_priv = dev->dev_private;
4518 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004519
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004520 if (HAS_PIPE_CONTROL(dev)) {
4521 ret = i915_gem_init_pipe_control(dev);
4522 if (ret)
4523 return ret;
4524 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004525
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004526 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004527 if (ret)
4528 goto cleanup_pipe_control;
4529
4530 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004531 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004532 if (ret)
4533 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004534 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004535
Chris Wilson6f392d52010-08-07 11:01:22 +01004536 dev_priv->next_seqno = 1;
4537
Chris Wilson68f95ba2010-05-27 13:18:22 +01004538 return 0;
4539
4540cleanup_render_ring:
4541 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4542cleanup_pipe_control:
4543 if (HAS_PIPE_CONTROL(dev))
4544 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004545 return ret;
4546}
4547
4548void
4549i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4550{
4551 drm_i915_private_t *dev_priv = dev->dev_private;
4552
4553 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004554 if (HAS_BSD(dev))
4555 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004556 if (HAS_PIPE_CONTROL(dev))
4557 i915_gem_cleanup_pipe_control(dev);
4558}
4559
4560int
Eric Anholt673a3942008-07-30 12:06:12 -07004561i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4562 struct drm_file *file_priv)
4563{
4564 drm_i915_private_t *dev_priv = dev->dev_private;
4565 int ret;
4566
Jesse Barnes79e53942008-11-07 14:24:08 -08004567 if (drm_core_check_feature(dev, DRIVER_MODESET))
4568 return 0;
4569
Ben Gamariba1234d2009-09-14 17:48:47 -04004570 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004571 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004572 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004573 }
4574
Eric Anholt673a3942008-07-30 12:06:12 -07004575 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004576 dev_priv->mm.suspended = 0;
4577
4578 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004579 if (ret != 0) {
4580 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004581 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004582 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004583
Zou Nan hai852835f2010-05-21 09:08:56 +08004584 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004585 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004586 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4587 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004588 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004589 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004590 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004591
Chris Wilson5f353082010-06-07 14:03:03 +01004592 ret = drm_irq_install(dev);
4593 if (ret)
4594 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004595
Eric Anholt673a3942008-07-30 12:06:12 -07004596 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004597
4598cleanup_ringbuffer:
4599 mutex_lock(&dev->struct_mutex);
4600 i915_gem_cleanup_ringbuffer(dev);
4601 dev_priv->mm.suspended = 1;
4602 mutex_unlock(&dev->struct_mutex);
4603
4604 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004605}
4606
4607int
4608i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4609 struct drm_file *file_priv)
4610{
Jesse Barnes79e53942008-11-07 14:24:08 -08004611 if (drm_core_check_feature(dev, DRIVER_MODESET))
4612 return 0;
4613
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004614 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004615 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004616}
4617
4618void
4619i915_gem_lastclose(struct drm_device *dev)
4620{
4621 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004622
Eric Anholte806b492009-01-22 09:56:58 -08004623 if (drm_core_check_feature(dev, DRIVER_MODESET))
4624 return;
4625
Keith Packard6dbe2772008-10-14 21:41:13 -07004626 ret = i915_gem_idle(dev);
4627 if (ret)
4628 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004629}
4630
4631void
4632i915_gem_load(struct drm_device *dev)
4633{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004634 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004635 drm_i915_private_t *dev_priv = dev->dev_private;
4636
Eric Anholt673a3942008-07-30 12:06:12 -07004637 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004638 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004639 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004640 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004641 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004642 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004643 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4644 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004645 if (HAS_BSD(dev)) {
4646 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4647 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4648 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004649 for (i = 0; i < 16; i++)
4650 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004651 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4652 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004653 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004654 spin_lock(&shrink_list_lock);
4655 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4656 spin_unlock(&shrink_list_lock);
4657
Dave Airlie94400122010-07-20 13:15:31 +10004658 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4659 if (IS_GEN3(dev)) {
4660 u32 tmp = I915_READ(MI_ARB_STATE);
4661 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4662 /* arb state is a masked write, so set bit + bit in mask */
4663 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4664 I915_WRITE(MI_ARB_STATE, tmp);
4665 }
4666 }
4667
Jesse Barnesde151cf2008-11-12 10:03:55 -08004668 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004669 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4670 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004671
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004672 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004673 dev_priv->num_fence_regs = 16;
4674 else
4675 dev_priv->num_fence_regs = 8;
4676
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004677 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004678 switch (INTEL_INFO(dev)->gen) {
4679 case 6:
4680 for (i = 0; i < 16; i++)
4681 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4682 break;
4683 case 5:
4684 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004685 for (i = 0; i < 16; i++)
4686 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004687 break;
4688 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004689 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4690 for (i = 0; i < 8; i++)
4691 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004692 case 2:
4693 for (i = 0; i < 8; i++)
4694 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4695 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004696 }
Eric Anholt673a3942008-07-30 12:06:12 -07004697 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004698 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004699}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004700
4701/*
4702 * Create a physically contiguous memory object for this object
4703 * e.g. for cursor + overlay regs
4704 */
Chris Wilson995b6762010-08-20 13:23:26 +01004705static int i915_gem_init_phys_object(struct drm_device *dev,
4706 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004707{
4708 drm_i915_private_t *dev_priv = dev->dev_private;
4709 struct drm_i915_gem_phys_object *phys_obj;
4710 int ret;
4711
4712 if (dev_priv->mm.phys_objs[id - 1] || !size)
4713 return 0;
4714
Eric Anholt9a298b22009-03-24 12:23:04 -07004715 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004716 if (!phys_obj)
4717 return -ENOMEM;
4718
4719 phys_obj->id = id;
4720
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004721 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004722 if (!phys_obj->handle) {
4723 ret = -ENOMEM;
4724 goto kfree_obj;
4725 }
4726#ifdef CONFIG_X86
4727 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4728#endif
4729
4730 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4731
4732 return 0;
4733kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004734 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004735 return ret;
4736}
4737
Chris Wilson995b6762010-08-20 13:23:26 +01004738static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004739{
4740 drm_i915_private_t *dev_priv = dev->dev_private;
4741 struct drm_i915_gem_phys_object *phys_obj;
4742
4743 if (!dev_priv->mm.phys_objs[id - 1])
4744 return;
4745
4746 phys_obj = dev_priv->mm.phys_objs[id - 1];
4747 if (phys_obj->cur_obj) {
4748 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4749 }
4750
4751#ifdef CONFIG_X86
4752 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4753#endif
4754 drm_pci_free(dev, phys_obj->handle);
4755 kfree(phys_obj);
4756 dev_priv->mm.phys_objs[id - 1] = NULL;
4757}
4758
4759void i915_gem_free_all_phys_object(struct drm_device *dev)
4760{
4761 int i;
4762
Dave Airlie260883c2009-01-22 17:58:49 +10004763 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004764 i915_gem_free_phys_object(dev, i);
4765}
4766
4767void i915_gem_detach_phys_object(struct drm_device *dev,
4768 struct drm_gem_object *obj)
4769{
4770 struct drm_i915_gem_object *obj_priv;
4771 int i;
4772 int ret;
4773 int page_count;
4774
Daniel Vetter23010e42010-03-08 13:35:02 +01004775 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004776 if (!obj_priv->phys_obj)
4777 return;
4778
Chris Wilson4bdadb92010-01-27 13:36:32 +00004779 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004780 if (ret)
4781 goto out;
4782
4783 page_count = obj->size / PAGE_SIZE;
4784
4785 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004786 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004787 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4788
4789 memcpy(dst, src, PAGE_SIZE);
4790 kunmap_atomic(dst, KM_USER0);
4791 }
Eric Anholt856fa192009-03-19 14:10:50 -07004792 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004793 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004794
4795 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004796out:
4797 obj_priv->phys_obj->cur_obj = NULL;
4798 obj_priv->phys_obj = NULL;
4799}
4800
4801int
4802i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004803 struct drm_gem_object *obj,
4804 int id,
4805 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004806{
4807 drm_i915_private_t *dev_priv = dev->dev_private;
4808 struct drm_i915_gem_object *obj_priv;
4809 int ret = 0;
4810 int page_count;
4811 int i;
4812
4813 if (id > I915_MAX_PHYS_OBJECT)
4814 return -EINVAL;
4815
Daniel Vetter23010e42010-03-08 13:35:02 +01004816 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004817
4818 if (obj_priv->phys_obj) {
4819 if (obj_priv->phys_obj->id == id)
4820 return 0;
4821 i915_gem_detach_phys_object(dev, obj);
4822 }
4823
Dave Airlie71acb5e2008-12-30 20:31:46 +10004824 /* create a new object */
4825 if (!dev_priv->mm.phys_objs[id - 1]) {
4826 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004827 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004828 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004829 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004830 goto out;
4831 }
4832 }
4833
4834 /* bind to the object */
4835 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4836 obj_priv->phys_obj->cur_obj = obj;
4837
Chris Wilson4bdadb92010-01-27 13:36:32 +00004838 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004839 if (ret) {
4840 DRM_ERROR("failed to get page list\n");
4841 goto out;
4842 }
4843
4844 page_count = obj->size / PAGE_SIZE;
4845
4846 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004847 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004848 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4849
4850 memcpy(dst, src, PAGE_SIZE);
4851 kunmap_atomic(src, KM_USER0);
4852 }
4853
Chris Wilsond78b47b2009-06-17 21:52:49 +01004854 i915_gem_object_put_pages(obj);
4855
Dave Airlie71acb5e2008-12-30 20:31:46 +10004856 return 0;
4857out:
4858 return ret;
4859}
4860
4861static int
4862i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4863 struct drm_i915_gem_pwrite *args,
4864 struct drm_file *file_priv)
4865{
Daniel Vetter23010e42010-03-08 13:35:02 +01004866 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004867 void *obj_addr;
4868 int ret;
4869 char __user *user_data;
4870
4871 user_data = (char __user *) (uintptr_t) args->data_ptr;
4872 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4873
Zhao Yakui44d98a62009-10-09 11:39:40 +08004874 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004875 ret = copy_from_user(obj_addr, user_data, args->size);
4876 if (ret)
4877 return -EFAULT;
4878
4879 drm_agp_chipset_flush(dev);
4880 return 0;
4881}
Eric Anholtb9624422009-06-03 07:27:35 +00004882
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004883void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004884{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004885 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004886
4887 /* Clean up our request list when the client is going away, so that
4888 * later retire_requests won't dereference our soon-to-be-gone
4889 * file_priv.
4890 */
Chris Wilson1c255952010-09-26 11:03:27 +01004891 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004892 while (!list_empty(&file_priv->mm.request_list)) {
4893 struct drm_i915_gem_request *request;
4894
4895 request = list_first_entry(&file_priv->mm.request_list,
4896 struct drm_i915_gem_request,
4897 client_list);
4898 list_del(&request->client_list);
4899 request->file_priv = NULL;
4900 }
Chris Wilson1c255952010-09-26 11:03:27 +01004901 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004902}
Chris Wilson31169712009-09-14 16:50:28 +01004903
Chris Wilson31169712009-09-14 16:50:28 +01004904static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004905i915_gpu_is_active(struct drm_device *dev)
4906{
4907 drm_i915_private_t *dev_priv = dev->dev_private;
4908 int lists_empty;
4909
Chris Wilson1637ef42010-04-20 17:10:35 +01004910 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004911 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004912 if (HAS_BSD(dev))
4913 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004914
4915 return !lists_empty;
4916}
4917
4918static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004919i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004920{
4921 drm_i915_private_t *dev_priv, *next_dev;
4922 struct drm_i915_gem_object *obj_priv, *next_obj;
4923 int cnt = 0;
4924 int would_deadlock = 1;
4925
4926 /* "fast-path" to count number of available objects */
4927 if (nr_to_scan == 0) {
4928 spin_lock(&shrink_list_lock);
4929 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4930 struct drm_device *dev = dev_priv->dev;
4931
4932 if (mutex_trylock(&dev->struct_mutex)) {
4933 list_for_each_entry(obj_priv,
4934 &dev_priv->mm.inactive_list,
4935 list)
4936 cnt++;
4937 mutex_unlock(&dev->struct_mutex);
4938 }
4939 }
4940 spin_unlock(&shrink_list_lock);
4941
4942 return (cnt / 100) * sysctl_vfs_cache_pressure;
4943 }
4944
4945 spin_lock(&shrink_list_lock);
4946
Chris Wilson1637ef42010-04-20 17:10:35 +01004947rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004948 /* first scan for clean buffers */
4949 list_for_each_entry_safe(dev_priv, next_dev,
4950 &shrink_list, mm.shrink_list) {
4951 struct drm_device *dev = dev_priv->dev;
4952
4953 if (! mutex_trylock(&dev->struct_mutex))
4954 continue;
4955
4956 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004957 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004958
Chris Wilson31169712009-09-14 16:50:28 +01004959 list_for_each_entry_safe(obj_priv, next_obj,
4960 &dev_priv->mm.inactive_list,
4961 list) {
4962 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004963 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004964 if (--nr_to_scan <= 0)
4965 break;
4966 }
4967 }
4968
4969 spin_lock(&shrink_list_lock);
4970 mutex_unlock(&dev->struct_mutex);
4971
Chris Wilson963b4832009-09-20 23:03:54 +01004972 would_deadlock = 0;
4973
Chris Wilson31169712009-09-14 16:50:28 +01004974 if (nr_to_scan <= 0)
4975 break;
4976 }
4977
4978 /* second pass, evict/count anything still on the inactive list */
4979 list_for_each_entry_safe(dev_priv, next_dev,
4980 &shrink_list, mm.shrink_list) {
4981 struct drm_device *dev = dev_priv->dev;
4982
4983 if (! mutex_trylock(&dev->struct_mutex))
4984 continue;
4985
4986 spin_unlock(&shrink_list_lock);
4987
4988 list_for_each_entry_safe(obj_priv, next_obj,
4989 &dev_priv->mm.inactive_list,
4990 list) {
4991 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004992 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004993 nr_to_scan--;
4994 } else
4995 cnt++;
4996 }
4997
4998 spin_lock(&shrink_list_lock);
4999 mutex_unlock(&dev->struct_mutex);
5000
5001 would_deadlock = 0;
5002 }
5003
Chris Wilson1637ef42010-04-20 17:10:35 +01005004 if (nr_to_scan) {
5005 int active = 0;
5006
5007 /*
5008 * We are desperate for pages, so as a last resort, wait
5009 * for the GPU to finish and discard whatever we can.
5010 * This has a dramatic impact to reduce the number of
5011 * OOM-killer events whilst running the GPU aggressively.
5012 */
5013 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5014 struct drm_device *dev = dev_priv->dev;
5015
5016 if (!mutex_trylock(&dev->struct_mutex))
5017 continue;
5018
5019 spin_unlock(&shrink_list_lock);
5020
5021 if (i915_gpu_is_active(dev)) {
5022 i915_gpu_idle(dev);
5023 active++;
5024 }
5025
5026 spin_lock(&shrink_list_lock);
5027 mutex_unlock(&dev->struct_mutex);
5028 }
5029
5030 if (active)
5031 goto rescan;
5032 }
5033
Chris Wilson31169712009-09-14 16:50:28 +01005034 spin_unlock(&shrink_list_lock);
5035
5036 if (would_deadlock)
5037 return -1;
5038 else if (cnt > 0)
5039 return (cnt / 100) * sysctl_vfs_cache_pressure;
5040 else
5041 return 0;
5042}
5043
5044static struct shrinker shrinker = {
5045 .shrink = i915_gem_shrink,
5046 .seeks = DEFAULT_SEEKS,
5047};
5048
5049__init void
5050i915_gem_shrinker_init(void)
5051{
5052 register_shrinker(&shrinker);
5053}
5054
5055__exit void
5056i915_gem_shrinker_exit(void)
5057{
5058 unregister_shrinker(&shrinker);
5059}