blob: 29e97c075421f2776bfd2eb2bd06062d68080778 [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) {
248 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700249 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100250 }
251
252 /* Sink the floating reference from kref_init(handlecount) */
253 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700254
255 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700256 return 0;
257}
258
Eric Anholt40123c12009-03-09 13:42:30 -0700259static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700260fast_shmem_read(struct page **pages,
261 loff_t page_base, int page_offset,
262 char __user *data,
263 int length)
264{
265 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200266 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700267
268 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
269 if (vaddr == NULL)
270 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200271 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700272 kunmap_atomic(vaddr, KM_USER0);
273
Florian Mickler2bc43b52009-04-06 22:55:41 +0200274 if (unwritten)
275 return -EFAULT;
276
277 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700278}
279
Eric Anholt280b7132009-03-12 16:56:27 -0700280static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
281{
282 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100283 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700284
285 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
286 obj_priv->tiling_mode != I915_TILING_NONE;
287}
288
Chris Wilson99a03df2010-05-27 14:15:34 +0100289static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700290slow_shmem_copy(struct page *dst_page,
291 int dst_offset,
292 struct page *src_page,
293 int src_offset,
294 int length)
295{
296 char *dst_vaddr, *src_vaddr;
297
Chris Wilson99a03df2010-05-27 14:15:34 +0100298 dst_vaddr = kmap(dst_page);
299 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700300
301 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
302
Chris Wilson99a03df2010-05-27 14:15:34 +0100303 kunmap(src_page);
304 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700305}
306
Chris Wilson99a03df2010-05-27 14:15:34 +0100307static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700308slow_shmem_bit17_copy(struct page *gpu_page,
309 int gpu_offset,
310 struct page *cpu_page,
311 int cpu_offset,
312 int length,
313 int is_read)
314{
315 char *gpu_vaddr, *cpu_vaddr;
316
317 /* Use the unswizzled path if this page isn't affected. */
318 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
319 if (is_read)
320 return slow_shmem_copy(cpu_page, cpu_offset,
321 gpu_page, gpu_offset, length);
322 else
323 return slow_shmem_copy(gpu_page, gpu_offset,
324 cpu_page, cpu_offset, length);
325 }
326
Chris Wilson99a03df2010-05-27 14:15:34 +0100327 gpu_vaddr = kmap(gpu_page);
328 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700329
330 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
331 * XORing with the other bits (A9 for Y, A9 and A10 for X)
332 */
333 while (length > 0) {
334 int cacheline_end = ALIGN(gpu_offset + 1, 64);
335 int this_length = min(cacheline_end - gpu_offset, length);
336 int swizzled_gpu_offset = gpu_offset ^ 64;
337
338 if (is_read) {
339 memcpy(cpu_vaddr + cpu_offset,
340 gpu_vaddr + swizzled_gpu_offset,
341 this_length);
342 } else {
343 memcpy(gpu_vaddr + swizzled_gpu_offset,
344 cpu_vaddr + cpu_offset,
345 this_length);
346 }
347 cpu_offset += this_length;
348 gpu_offset += this_length;
349 length -= this_length;
350 }
351
Chris Wilson99a03df2010-05-27 14:15:34 +0100352 kunmap(cpu_page);
353 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700354}
355
Eric Anholt673a3942008-07-30 12:06:12 -0700356/**
Eric Anholteb014592009-03-10 11:44:52 -0700357 * This is the fast shmem pread path, which attempts to copy_from_user directly
358 * from the backing pages of the object to the user's address space. On a
359 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
360 */
361static int
362i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
363 struct drm_i915_gem_pread *args,
364 struct drm_file *file_priv)
365{
Daniel Vetter23010e42010-03-08 13:35:02 +0100366 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700367 ssize_t remain;
368 loff_t offset, page_base;
369 char __user *user_data;
370 int page_offset, page_length;
371 int ret;
372
373 user_data = (char __user *) (uintptr_t) args->data_ptr;
374 remain = args->size;
375
Chris Wilson76c1dec2010-09-25 11:22:51 +0100376 ret = i915_mutex_lock_interruptible(dev);
377 if (ret)
378 return ret;
Eric Anholteb014592009-03-10 11:44:52 -0700379
Chris Wilson4bdadb92010-01-27 13:36:32 +0000380 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700381 if (ret != 0)
382 goto fail_unlock;
383
384 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
385 args->size);
386 if (ret != 0)
387 goto fail_put_pages;
388
Daniel Vetter23010e42010-03-08 13:35:02 +0100389 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700390 offset = args->offset;
391
392 while (remain > 0) {
393 /* Operation in this page
394 *
395 * page_base = page offset within aperture
396 * page_offset = offset within page
397 * page_length = bytes to copy for this page
398 */
399 page_base = (offset & ~(PAGE_SIZE-1));
400 page_offset = offset & (PAGE_SIZE-1);
401 page_length = remain;
402 if ((page_offset + remain) > PAGE_SIZE)
403 page_length = PAGE_SIZE - page_offset;
404
405 ret = fast_shmem_read(obj_priv->pages,
406 page_base, page_offset,
407 user_data, page_length);
408 if (ret)
409 goto fail_put_pages;
410
411 remain -= page_length;
412 user_data += page_length;
413 offset += page_length;
414 }
415
416fail_put_pages:
417 i915_gem_object_put_pages(obj);
418fail_unlock:
419 mutex_unlock(&dev->struct_mutex);
420
421 return ret;
422}
423
Chris Wilson07f73f62009-09-14 16:50:30 +0100424static int
425i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
426{
427 int ret;
428
Chris Wilson4bdadb92010-01-27 13:36:32 +0000429 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100430
431 /* If we've insufficient memory to map in the pages, attempt
432 * to make some space by throwing out some old buffers.
433 */
434 if (ret == -ENOMEM) {
435 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100436
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100437 ret = i915_gem_evict_something(dev, obj->size,
438 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100439 if (ret)
440 return ret;
441
Chris Wilson4bdadb92010-01-27 13:36:32 +0000442 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100443 }
444
445 return ret;
446}
447
Eric Anholteb014592009-03-10 11:44:52 -0700448/**
449 * This is the fallback shmem pread path, which allocates temporary storage
450 * in kernel space to copy_to_user into outside of the struct_mutex, so we
451 * can copy out of the object's backing pages while holding the struct mutex
452 * and not take page faults.
453 */
454static int
455i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
456 struct drm_i915_gem_pread *args,
457 struct drm_file *file_priv)
458{
Daniel Vetter23010e42010-03-08 13:35:02 +0100459 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700460 struct mm_struct *mm = current->mm;
461 struct page **user_pages;
462 ssize_t remain;
463 loff_t offset, pinned_pages, i;
464 loff_t first_data_page, last_data_page, num_pages;
465 int shmem_page_index, shmem_page_offset;
466 int data_page_index, data_page_offset;
467 int page_length;
468 int ret;
469 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700470 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700471
472 remain = args->size;
473
474 /* Pin the user pages containing the data. We can't fault while
475 * holding the struct mutex, yet we want to hold it while
476 * dereferencing the user data.
477 */
478 first_data_page = data_ptr / PAGE_SIZE;
479 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
480 num_pages = last_data_page - first_data_page + 1;
481
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700482 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700483 if (user_pages == NULL)
484 return -ENOMEM;
485
486 down_read(&mm->mmap_sem);
487 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700488 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700489 up_read(&mm->mmap_sem);
490 if (pinned_pages < num_pages) {
491 ret = -EFAULT;
492 goto fail_put_user_pages;
493 }
494
Eric Anholt280b7132009-03-12 16:56:27 -0700495 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
496
Chris Wilson76c1dec2010-09-25 11:22:51 +0100497 ret = i915_mutex_lock_interruptible(dev);
498 if (ret)
499 goto fail_put_user_pages;
Eric Anholteb014592009-03-10 11:44:52 -0700500
Chris Wilson07f73f62009-09-14 16:50:30 +0100501 ret = i915_gem_object_get_pages_or_evict(obj);
502 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700503 goto fail_unlock;
504
505 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
506 args->size);
507 if (ret != 0)
508 goto fail_put_pages;
509
Daniel Vetter23010e42010-03-08 13:35:02 +0100510 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700511 offset = args->offset;
512
513 while (remain > 0) {
514 /* Operation in this page
515 *
516 * shmem_page_index = page number within shmem file
517 * shmem_page_offset = offset within page in shmem file
518 * data_page_index = page number in get_user_pages return
519 * data_page_offset = offset with data_page_index page.
520 * page_length = bytes to copy for this page
521 */
522 shmem_page_index = offset / PAGE_SIZE;
523 shmem_page_offset = offset & ~PAGE_MASK;
524 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
525 data_page_offset = data_ptr & ~PAGE_MASK;
526
527 page_length = remain;
528 if ((shmem_page_offset + page_length) > PAGE_SIZE)
529 page_length = PAGE_SIZE - shmem_page_offset;
530 if ((data_page_offset + page_length) > PAGE_SIZE)
531 page_length = PAGE_SIZE - data_page_offset;
532
Eric Anholt280b7132009-03-12 16:56:27 -0700533 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100534 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700535 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100536 user_pages[data_page_index],
537 data_page_offset,
538 page_length,
539 1);
540 } else {
541 slow_shmem_copy(user_pages[data_page_index],
542 data_page_offset,
543 obj_priv->pages[shmem_page_index],
544 shmem_page_offset,
545 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700546 }
Eric Anholteb014592009-03-10 11:44:52 -0700547
548 remain -= page_length;
549 data_ptr += page_length;
550 offset += page_length;
551 }
552
553fail_put_pages:
554 i915_gem_object_put_pages(obj);
555fail_unlock:
556 mutex_unlock(&dev->struct_mutex);
557fail_put_user_pages:
558 for (i = 0; i < pinned_pages; i++) {
559 SetPageDirty(user_pages[i]);
560 page_cache_release(user_pages[i]);
561 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700562 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700563
564 return ret;
565}
566
Eric Anholt673a3942008-07-30 12:06:12 -0700567/**
568 * Reads data from the object referenced by handle.
569 *
570 * On error, the contents of *data are undefined.
571 */
572int
573i915_gem_pread_ioctl(struct drm_device *dev, void *data,
574 struct drm_file *file_priv)
575{
576 struct drm_i915_gem_pread *args = data;
577 struct drm_gem_object *obj;
578 struct drm_i915_gem_object *obj_priv;
Chris Wilson35b62a82010-09-26 20:23:38 +0100579 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700580
581 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
582 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100583 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100584 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700585
Chris Wilson7dcd2492010-09-26 20:21:44 +0100586 /* Bounds check source. */
587 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100588 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100589 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100590 }
591
Chris Wilson35b62a82010-09-26 20:23:38 +0100592 if (args->size == 0)
593 goto out;
594
Chris Wilsonce9d4192010-09-26 20:50:05 +0100595 if (!access_ok(VERIFY_WRITE,
596 (char __user *)(uintptr_t)args->data_ptr,
597 args->size)) {
598 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +0100599 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -0700600 }
601
Eric Anholt280b7132009-03-12 16:56:27 -0700602 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700603 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700604 } else {
605 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
606 if (ret != 0)
607 ret = i915_gem_shmem_pread_slow(dev, obj, args,
608 file_priv);
609 }
Eric Anholt673a3942008-07-30 12:06:12 -0700610
Chris Wilson35b62a82010-09-26 20:23:38 +0100611out:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000612 drm_gem_object_unreference_unlocked(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700613 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700614}
615
Keith Packard0839ccb2008-10-30 19:38:48 -0700616/* This is the fast write path which cannot handle
617 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700618 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700619
Keith Packard0839ccb2008-10-30 19:38:48 -0700620static inline int
621fast_user_write(struct io_mapping *mapping,
622 loff_t page_base, int page_offset,
623 char __user *user_data,
624 int length)
625{
626 char *vaddr_atomic;
627 unsigned long unwritten;
628
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100629 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700630 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
631 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100632 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700633 if (unwritten)
634 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700635 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700636}
637
638/* Here's the write path which can sleep for
639 * page faults
640 */
641
Chris Wilsonab34c222010-05-27 14:15:35 +0100642static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700643slow_kernel_write(struct io_mapping *mapping,
644 loff_t gtt_base, int gtt_offset,
645 struct page *user_page, int user_offset,
646 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700647{
Chris Wilsonab34c222010-05-27 14:15:35 +0100648 char __iomem *dst_vaddr;
649 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700650
Chris Wilsonab34c222010-05-27 14:15:35 +0100651 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
652 src_vaddr = kmap(user_page);
653
654 memcpy_toio(dst_vaddr + gtt_offset,
655 src_vaddr + user_offset,
656 length);
657
658 kunmap(user_page);
659 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700660}
661
Eric Anholt40123c12009-03-09 13:42:30 -0700662static inline int
663fast_shmem_write(struct page **pages,
664 loff_t page_base, int page_offset,
665 char __user *data,
666 int length)
667{
668 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400669 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700670
671 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
672 if (vaddr == NULL)
673 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400674 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700675 kunmap_atomic(vaddr, KM_USER0);
676
Dave Airlied0088772009-03-28 20:29:48 -0400677 if (unwritten)
678 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700679 return 0;
680}
681
Eric Anholt3de09aa2009-03-09 09:42:23 -0700682/**
683 * This is the fast pwrite path, where we copy the data directly from the
684 * user into the GTT, uncached.
685 */
Eric Anholt673a3942008-07-30 12:06:12 -0700686static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700687i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
688 struct drm_i915_gem_pwrite *args,
689 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700690{
Daniel Vetter23010e42010-03-08 13:35:02 +0100691 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700692 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700693 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700694 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700695 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700696 int page_offset, page_length;
697 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700698
699 user_data = (char __user *) (uintptr_t) args->data_ptr;
700 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700701
Chris Wilson76c1dec2010-09-25 11:22:51 +0100702 ret = i915_mutex_lock_interruptible(dev);
703 if (ret)
704 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700705
Eric Anholt673a3942008-07-30 12:06:12 -0700706 ret = i915_gem_object_pin(obj, 0);
707 if (ret) {
708 mutex_unlock(&dev->struct_mutex);
709 return ret;
710 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800711 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700712 if (ret)
713 goto fail;
714
Daniel Vetter23010e42010-03-08 13:35:02 +0100715 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700716 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700717
718 while (remain > 0) {
719 /* Operation in this page
720 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700721 * page_base = page offset within aperture
722 * page_offset = offset within page
723 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700724 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700725 page_base = (offset & ~(PAGE_SIZE-1));
726 page_offset = offset & (PAGE_SIZE-1);
727 page_length = remain;
728 if ((page_offset + remain) > PAGE_SIZE)
729 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700730
Keith Packard0839ccb2008-10-30 19:38:48 -0700731 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
732 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700733
Keith Packard0839ccb2008-10-30 19:38:48 -0700734 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700735 * source page isn't available. Return the error and we'll
736 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700737 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700738 if (ret)
739 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700740
Keith Packard0839ccb2008-10-30 19:38:48 -0700741 remain -= page_length;
742 user_data += page_length;
743 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700744 }
Eric Anholt673a3942008-07-30 12:06:12 -0700745
746fail:
747 i915_gem_object_unpin(obj);
748 mutex_unlock(&dev->struct_mutex);
749
750 return ret;
751}
752
Eric Anholt3de09aa2009-03-09 09:42:23 -0700753/**
754 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
755 * the memory and maps it using kmap_atomic for copying.
756 *
757 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
758 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
759 */
Eric Anholt3043c602008-10-02 12:24:47 -0700760static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700761i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
762 struct drm_i915_gem_pwrite *args,
763 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700764{
Daniel Vetter23010e42010-03-08 13:35:02 +0100765 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700766 drm_i915_private_t *dev_priv = dev->dev_private;
767 ssize_t remain;
768 loff_t gtt_page_base, offset;
769 loff_t first_data_page, last_data_page, num_pages;
770 loff_t pinned_pages, i;
771 struct page **user_pages;
772 struct mm_struct *mm = current->mm;
773 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700774 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700775 uint64_t data_ptr = args->data_ptr;
776
777 remain = args->size;
778
779 /* Pin the user pages containing the data. We can't fault while
780 * holding the struct mutex, and all of the pwrite implementations
781 * want to hold it while dereferencing the user data.
782 */
783 first_data_page = data_ptr / PAGE_SIZE;
784 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
785 num_pages = last_data_page - first_data_page + 1;
786
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700787 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700788 if (user_pages == NULL)
789 return -ENOMEM;
790
791 down_read(&mm->mmap_sem);
792 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
793 num_pages, 0, 0, user_pages, NULL);
794 up_read(&mm->mmap_sem);
795 if (pinned_pages < num_pages) {
796 ret = -EFAULT;
797 goto out_unpin_pages;
798 }
799
Chris Wilson76c1dec2010-09-25 11:22:51 +0100800 ret = i915_mutex_lock_interruptible(dev);
801 if (ret)
802 goto out_unpin_pages;
803
Eric Anholt3de09aa2009-03-09 09:42:23 -0700804 ret = i915_gem_object_pin(obj, 0);
805 if (ret)
806 goto out_unlock;
807
808 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
809 if (ret)
810 goto out_unpin_object;
811
Daniel Vetter23010e42010-03-08 13:35:02 +0100812 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700813 offset = obj_priv->gtt_offset + args->offset;
814
815 while (remain > 0) {
816 /* Operation in this page
817 *
818 * gtt_page_base = page offset within aperture
819 * gtt_page_offset = offset within page in aperture
820 * data_page_index = page number in get_user_pages return
821 * data_page_offset = offset with data_page_index page.
822 * page_length = bytes to copy for this page
823 */
824 gtt_page_base = offset & PAGE_MASK;
825 gtt_page_offset = offset & ~PAGE_MASK;
826 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
827 data_page_offset = data_ptr & ~PAGE_MASK;
828
829 page_length = remain;
830 if ((gtt_page_offset + page_length) > PAGE_SIZE)
831 page_length = PAGE_SIZE - gtt_page_offset;
832 if ((data_page_offset + page_length) > PAGE_SIZE)
833 page_length = PAGE_SIZE - data_page_offset;
834
Chris Wilsonab34c222010-05-27 14:15:35 +0100835 slow_kernel_write(dev_priv->mm.gtt_mapping,
836 gtt_page_base, gtt_page_offset,
837 user_pages[data_page_index],
838 data_page_offset,
839 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700840
841 remain -= page_length;
842 offset += page_length;
843 data_ptr += page_length;
844 }
845
846out_unpin_object:
847 i915_gem_object_unpin(obj);
848out_unlock:
849 mutex_unlock(&dev->struct_mutex);
850out_unpin_pages:
851 for (i = 0; i < pinned_pages; i++)
852 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700853 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700854
855 return ret;
856}
857
Eric Anholt40123c12009-03-09 13:42:30 -0700858/**
859 * This is the fast shmem pwrite path, which attempts to directly
860 * copy_from_user into the kmapped pages backing the object.
861 */
Eric Anholt673a3942008-07-30 12:06:12 -0700862static int
Eric Anholt40123c12009-03-09 13:42:30 -0700863i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
864 struct drm_i915_gem_pwrite *args,
865 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700866{
Daniel Vetter23010e42010-03-08 13:35:02 +0100867 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700868 ssize_t remain;
869 loff_t offset, page_base;
870 char __user *user_data;
871 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700872 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700873
874 user_data = (char __user *) (uintptr_t) args->data_ptr;
875 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700876
Chris Wilson76c1dec2010-09-25 11:22:51 +0100877 ret = i915_mutex_lock_interruptible(dev);
878 if (ret)
879 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700880
Chris Wilson4bdadb92010-01-27 13:36:32 +0000881 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700882 if (ret != 0)
883 goto fail_unlock;
884
Eric Anholte47c68e2008-11-14 13:35:19 -0800885 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700886 if (ret != 0)
887 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700888
Daniel Vetter23010e42010-03-08 13:35:02 +0100889 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700890 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700891 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700892
Eric Anholt40123c12009-03-09 13:42:30 -0700893 while (remain > 0) {
894 /* Operation in this page
895 *
896 * page_base = page offset within aperture
897 * page_offset = offset within page
898 * page_length = bytes to copy for this page
899 */
900 page_base = (offset & ~(PAGE_SIZE-1));
901 page_offset = offset & (PAGE_SIZE-1);
902 page_length = remain;
903 if ((page_offset + remain) > PAGE_SIZE)
904 page_length = PAGE_SIZE - page_offset;
905
906 ret = fast_shmem_write(obj_priv->pages,
907 page_base, page_offset,
908 user_data, page_length);
909 if (ret)
910 goto fail_put_pages;
911
912 remain -= page_length;
913 user_data += page_length;
914 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700915 }
916
Eric Anholt40123c12009-03-09 13:42:30 -0700917fail_put_pages:
918 i915_gem_object_put_pages(obj);
919fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700920 mutex_unlock(&dev->struct_mutex);
921
Eric Anholt40123c12009-03-09 13:42:30 -0700922 return ret;
923}
924
925/**
926 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
927 * the memory and maps it using kmap_atomic for copying.
928 *
929 * This avoids taking mmap_sem for faulting on the user's address while the
930 * struct_mutex is held.
931 */
932static int
933i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
934 struct drm_i915_gem_pwrite *args,
935 struct drm_file *file_priv)
936{
Daniel Vetter23010e42010-03-08 13:35:02 +0100937 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700938 struct mm_struct *mm = current->mm;
939 struct page **user_pages;
940 ssize_t remain;
941 loff_t offset, pinned_pages, i;
942 loff_t first_data_page, last_data_page, num_pages;
943 int shmem_page_index, shmem_page_offset;
944 int data_page_index, data_page_offset;
945 int page_length;
946 int ret;
947 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700948 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700949
950 remain = args->size;
951
952 /* Pin the user pages containing the data. We can't fault while
953 * holding the struct mutex, and all of the pwrite implementations
954 * want to hold it while dereferencing the user data.
955 */
956 first_data_page = data_ptr / PAGE_SIZE;
957 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
958 num_pages = last_data_page - first_data_page + 1;
959
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700960 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700961 if (user_pages == NULL)
962 return -ENOMEM;
963
964 down_read(&mm->mmap_sem);
965 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
966 num_pages, 0, 0, user_pages, NULL);
967 up_read(&mm->mmap_sem);
968 if (pinned_pages < num_pages) {
969 ret = -EFAULT;
970 goto fail_put_user_pages;
971 }
972
Eric Anholt280b7132009-03-12 16:56:27 -0700973 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
974
Chris Wilson76c1dec2010-09-25 11:22:51 +0100975 ret = i915_mutex_lock_interruptible(dev);
976 if (ret)
977 goto fail_put_user_pages;
Eric Anholt40123c12009-03-09 13:42:30 -0700978
Chris Wilson07f73f62009-09-14 16:50:30 +0100979 ret = i915_gem_object_get_pages_or_evict(obj);
980 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700981 goto fail_unlock;
982
983 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
984 if (ret != 0)
985 goto fail_put_pages;
986
Daniel Vetter23010e42010-03-08 13:35:02 +0100987 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700988 offset = args->offset;
989 obj_priv->dirty = 1;
990
991 while (remain > 0) {
992 /* Operation in this page
993 *
994 * shmem_page_index = page number within shmem file
995 * shmem_page_offset = offset within page in shmem file
996 * data_page_index = page number in get_user_pages return
997 * data_page_offset = offset with data_page_index page.
998 * page_length = bytes to copy for this page
999 */
1000 shmem_page_index = offset / PAGE_SIZE;
1001 shmem_page_offset = offset & ~PAGE_MASK;
1002 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
1003 data_page_offset = data_ptr & ~PAGE_MASK;
1004
1005 page_length = remain;
1006 if ((shmem_page_offset + page_length) > PAGE_SIZE)
1007 page_length = PAGE_SIZE - shmem_page_offset;
1008 if ((data_page_offset + page_length) > PAGE_SIZE)
1009 page_length = PAGE_SIZE - data_page_offset;
1010
Eric Anholt280b7132009-03-12 16:56:27 -07001011 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +01001012 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -07001013 shmem_page_offset,
1014 user_pages[data_page_index],
1015 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +01001016 page_length,
1017 0);
1018 } else {
1019 slow_shmem_copy(obj_priv->pages[shmem_page_index],
1020 shmem_page_offset,
1021 user_pages[data_page_index],
1022 data_page_offset,
1023 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -07001024 }
Eric Anholt40123c12009-03-09 13:42:30 -07001025
1026 remain -= page_length;
1027 data_ptr += page_length;
1028 offset += page_length;
1029 }
1030
1031fail_put_pages:
1032 i915_gem_object_put_pages(obj);
1033fail_unlock:
1034 mutex_unlock(&dev->struct_mutex);
1035fail_put_user_pages:
1036 for (i = 0; i < pinned_pages; i++)
1037 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001038 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -07001039
1040 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001041}
1042
1043/**
1044 * Writes data to the object referenced by handle.
1045 *
1046 * On error, the contents of the buffer that were to be modified are undefined.
1047 */
1048int
1049i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1050 struct drm_file *file_priv)
1051{
1052 struct drm_i915_gem_pwrite *args = data;
1053 struct drm_gem_object *obj;
1054 struct drm_i915_gem_object *obj_priv;
1055 int ret = 0;
1056
1057 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1058 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001059 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001060 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001061
Chris Wilson7dcd2492010-09-26 20:21:44 +01001062 /* Bounds check destination. */
1063 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001064 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001065 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001066 }
1067
Chris Wilson35b62a82010-09-26 20:23:38 +01001068 if (args->size == 0)
1069 goto out;
1070
Chris Wilsonce9d4192010-09-26 20:50:05 +01001071 if (!access_ok(VERIFY_READ,
1072 (char __user *)(uintptr_t)args->data_ptr,
1073 args->size)) {
1074 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +01001075 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001076 }
1077
1078 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1079 * it would end up going through the fenced access, and we'll get
1080 * different detiling behavior between reading and writing.
1081 * pread/pwrite currently are reading and writing from the CPU
1082 * perspective, requiring manual detiling by the client.
1083 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001084 if (obj_priv->phys_obj)
1085 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
1086 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001087 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001088 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -07001089 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
1090 if (ret == -EFAULT) {
1091 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
1092 file_priv);
1093 }
Eric Anholt280b7132009-03-12 16:56:27 -07001094 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
1095 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -07001096 } else {
1097 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
1098 if (ret == -EFAULT) {
1099 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
1100 file_priv);
1101 }
1102 }
Eric Anholt673a3942008-07-30 12:06:12 -07001103
1104#if WATCH_PWRITE
1105 if (ret)
1106 DRM_INFO("pwrite failed %d\n", ret);
1107#endif
1108
Chris Wilson35b62a82010-09-26 20:23:38 +01001109out:
Luca Barbieribc9025b2010-02-09 05:49:12 +00001110 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001111 return ret;
1112}
1113
1114/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001115 * Called when user space prepares to use an object with the CPU, either
1116 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001117 */
1118int
1119i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1120 struct drm_file *file_priv)
1121{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001122 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001123 struct drm_i915_gem_set_domain *args = data;
1124 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001125 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001126 uint32_t read_domains = args->read_domains;
1127 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001128 int ret;
1129
1130 if (!(dev->driver->driver_features & DRIVER_GEM))
1131 return -ENODEV;
1132
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001133 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001134 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001135 return -EINVAL;
1136
Chris Wilson21d509e2009-06-06 09:46:02 +01001137 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001138 return -EINVAL;
1139
1140 /* Having something in the write domain implies it's in the read
1141 * domain, and only that read domain. Enforce that in the request.
1142 */
1143 if (write_domain != 0 && read_domains != write_domain)
1144 return -EINVAL;
1145
Eric Anholt673a3942008-07-30 12:06:12 -07001146 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1147 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001148 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001149 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001150
Chris Wilson76c1dec2010-09-25 11:22:51 +01001151 ret = i915_mutex_lock_interruptible(dev);
1152 if (ret) {
1153 drm_gem_object_unreference_unlocked(obj);
1154 return ret;
1155 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001156
1157 intel_mark_busy(dev, obj);
1158
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001159 if (read_domains & I915_GEM_DOMAIN_GTT) {
1160 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001161
Eric Anholta09ba7f2009-08-29 12:49:51 -07001162 /* Update the LRU on the fence for the CPU access that's
1163 * about to occur.
1164 */
1165 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001166 struct drm_i915_fence_reg *reg =
1167 &dev_priv->fence_regs[obj_priv->fence_reg];
1168 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001169 &dev_priv->mm.fence_list);
1170 }
1171
Eric Anholt02354392008-11-26 13:58:13 -08001172 /* Silently promote "you're not bound, there was nothing to do"
1173 * to success, since the client was just asking us to
1174 * make sure everything was done.
1175 */
1176 if (ret == -EINVAL)
1177 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001178 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001179 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001180 }
1181
Chris Wilson7d1c4802010-08-07 21:45:03 +01001182 /* Maintain LRU order of "inactive" objects */
1183 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1184 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1185
Eric Anholt673a3942008-07-30 12:06:12 -07001186 drm_gem_object_unreference(obj);
1187 mutex_unlock(&dev->struct_mutex);
1188 return ret;
1189}
1190
1191/**
1192 * Called when user space has done writes to this buffer
1193 */
1194int
1195i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1196 struct drm_file *file_priv)
1197{
1198 struct drm_i915_gem_sw_finish *args = data;
1199 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001200 int ret = 0;
1201
1202 if (!(dev->driver->driver_features & DRIVER_GEM))
1203 return -ENODEV;
1204
Eric Anholt673a3942008-07-30 12:06:12 -07001205 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson76c1dec2010-09-25 11:22:51 +01001206 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001207 return -ENOENT;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001208
1209 ret = i915_mutex_lock_interruptible(dev);
1210 if (ret) {
1211 drm_gem_object_unreference_unlocked(obj);
1212 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001213 }
1214
Eric Anholt673a3942008-07-30 12:06:12 -07001215 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001216 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001217 i915_gem_object_flush_cpu_write_domain(obj);
1218
Eric Anholt673a3942008-07-30 12:06:12 -07001219 drm_gem_object_unreference(obj);
1220 mutex_unlock(&dev->struct_mutex);
1221 return ret;
1222}
1223
1224/**
1225 * Maps the contents of an object, returning the address it is mapped
1226 * into.
1227 *
1228 * While the mapping holds a reference on the contents of the object, it doesn't
1229 * imply a ref on the object itself.
1230 */
1231int
1232i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1233 struct drm_file *file_priv)
1234{
1235 struct drm_i915_gem_mmap *args = data;
1236 struct drm_gem_object *obj;
1237 loff_t offset;
1238 unsigned long addr;
1239
1240 if (!(dev->driver->driver_features & DRIVER_GEM))
1241 return -ENODEV;
1242
1243 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1244 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001245 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001246
1247 offset = args->offset;
1248
1249 down_write(&current->mm->mmap_sem);
1250 addr = do_mmap(obj->filp, 0, args->size,
1251 PROT_READ | PROT_WRITE, MAP_SHARED,
1252 args->offset);
1253 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001254 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001255 if (IS_ERR((void *)addr))
1256 return addr;
1257
1258 args->addr_ptr = (uint64_t) addr;
1259
1260 return 0;
1261}
1262
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263/**
1264 * i915_gem_fault - fault a page into the GTT
1265 * vma: VMA in question
1266 * vmf: fault info
1267 *
1268 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1269 * from userspace. The fault handler takes care of binding the object to
1270 * the GTT (if needed), allocating and programming a fence register (again,
1271 * only if needed based on whether the old reg is still valid or the object
1272 * is tiled) and inserting a new PTE into the faulting process.
1273 *
1274 * Note that the faulting process may involve evicting existing objects
1275 * from the GTT and/or fence registers to make room. So performance may
1276 * suffer if the GTT working set is large or there are few fence registers
1277 * left.
1278 */
1279int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1280{
1281 struct drm_gem_object *obj = vma->vm_private_data;
1282 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001283 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001284 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001285 pgoff_t page_offset;
1286 unsigned long pfn;
1287 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001288 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001289
1290 /* We don't use vmf->pgoff since that has the fake offset */
1291 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1292 PAGE_SHIFT;
1293
1294 /* Now bind it into the GTT if needed */
1295 mutex_lock(&dev->struct_mutex);
1296 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001297 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001298 if (ret)
1299 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001300
Jesse Barnesde151cf2008-11-12 10:03:55 -08001301 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001302 if (ret)
1303 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001304 }
1305
1306 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001307 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001308 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001309 if (ret)
1310 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001311 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001312
Chris Wilson7d1c4802010-08-07 21:45:03 +01001313 if (i915_gem_object_is_inactive(obj_priv))
1314 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1315
Jesse Barnesde151cf2008-11-12 10:03:55 -08001316 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1317 page_offset;
1318
1319 /* Finally, remap it using the new GTT offset */
1320 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001321unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001322 mutex_unlock(&dev->struct_mutex);
1323
1324 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001325 case 0:
1326 case -ERESTARTSYS:
1327 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001328 case -ENOMEM:
1329 case -EAGAIN:
1330 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001331 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001332 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001333 }
1334}
1335
1336/**
1337 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1338 * @obj: obj in question
1339 *
1340 * GEM memory mapping works by handing back to userspace a fake mmap offset
1341 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1342 * up the object based on the offset and sets up the various memory mapping
1343 * structures.
1344 *
1345 * This routine allocates and attaches a fake offset for @obj.
1346 */
1347static int
1348i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1349{
1350 struct drm_device *dev = obj->dev;
1351 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001352 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001353 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001354 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001355 int ret = 0;
1356
1357 /* Set the object up for mmap'ing */
1358 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001359 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001360 if (!list->map)
1361 return -ENOMEM;
1362
1363 map = list->map;
1364 map->type = _DRM_GEM;
1365 map->size = obj->size;
1366 map->handle = obj;
1367
1368 /* Get a DRM GEM mmap offset allocated... */
1369 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1370 obj->size / PAGE_SIZE, 0, 0);
1371 if (!list->file_offset_node) {
1372 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001373 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001374 goto out_free_list;
1375 }
1376
1377 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1378 obj->size / PAGE_SIZE, 0);
1379 if (!list->file_offset_node) {
1380 ret = -ENOMEM;
1381 goto out_free_list;
1382 }
1383
1384 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001385 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1386 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001387 DRM_ERROR("failed to add to map hash\n");
1388 goto out_free_mm;
1389 }
1390
1391 /* By now we should be all set, any drm_mmap request on the offset
1392 * below will get to our mmap & fault handler */
1393 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1394
1395 return 0;
1396
1397out_free_mm:
1398 drm_mm_put_block(list->file_offset_node);
1399out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001400 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401
1402 return ret;
1403}
1404
Chris Wilson901782b2009-07-10 08:18:50 +01001405/**
1406 * i915_gem_release_mmap - remove physical page mappings
1407 * @obj: obj in question
1408 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001409 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001410 * relinquish ownership of the pages back to the system.
1411 *
1412 * It is vital that we remove the page mapping if we have mapped a tiled
1413 * object through the GTT and then lose the fence register due to
1414 * resource pressure. Similarly if the object has been moved out of the
1415 * aperture, than pages mapped into userspace must be revoked. Removing the
1416 * mapping will then trigger a page fault on the next user access, allowing
1417 * fixup by i915_gem_fault().
1418 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001419void
Chris Wilson901782b2009-07-10 08:18:50 +01001420i915_gem_release_mmap(struct drm_gem_object *obj)
1421{
1422 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001423 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001424
1425 if (dev->dev_mapping)
1426 unmap_mapping_range(dev->dev_mapping,
1427 obj_priv->mmap_offset, obj->size, 1);
1428}
1429
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001430static void
1431i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1432{
1433 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001434 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001435 struct drm_gem_mm *mm = dev->mm_private;
1436 struct drm_map_list *list;
1437
1438 list = &obj->map_list;
1439 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1440
1441 if (list->file_offset_node) {
1442 drm_mm_put_block(list->file_offset_node);
1443 list->file_offset_node = NULL;
1444 }
1445
1446 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001447 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001448 list->map = NULL;
1449 }
1450
1451 obj_priv->mmap_offset = 0;
1452}
1453
Jesse Barnesde151cf2008-11-12 10:03:55 -08001454/**
1455 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1456 * @obj: object to check
1457 *
1458 * Return the required GTT alignment for an object, taking into account
1459 * potential fence register mapping if needed.
1460 */
1461static uint32_t
1462i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1463{
1464 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001465 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001466 int start, i;
1467
1468 /*
1469 * Minimum alignment is 4k (GTT page size), but might be greater
1470 * if a fence register is needed for the object.
1471 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001472 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001473 return 4096;
1474
1475 /*
1476 * Previous chips need to be aligned to the size of the smallest
1477 * fence register that can contain the object.
1478 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001479 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001480 start = 1024*1024;
1481 else
1482 start = 512*1024;
1483
1484 for (i = start; i < obj->size; i <<= 1)
1485 ;
1486
1487 return i;
1488}
1489
1490/**
1491 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1492 * @dev: DRM device
1493 * @data: GTT mapping ioctl data
1494 * @file_priv: GEM object info
1495 *
1496 * Simply returns the fake offset to userspace so it can mmap it.
1497 * The mmap call will end up in drm_gem_mmap(), which will set things
1498 * up so we can get faults in the handler above.
1499 *
1500 * The fault handler will take care of binding the object into the GTT
1501 * (since it may have been evicted to make room for something), allocating
1502 * a fence register, and mapping the appropriate aperture address into
1503 * userspace.
1504 */
1505int
1506i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1507 struct drm_file *file_priv)
1508{
1509 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001510 struct drm_gem_object *obj;
1511 struct drm_i915_gem_object *obj_priv;
1512 int ret;
1513
1514 if (!(dev->driver->driver_features & DRIVER_GEM))
1515 return -ENODEV;
1516
1517 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1518 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001519 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001520
Chris Wilson76c1dec2010-09-25 11:22:51 +01001521 ret = i915_mutex_lock_interruptible(dev);
1522 if (ret) {
1523 drm_gem_object_unreference_unlocked(obj);
1524 return ret;
1525 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001526
Daniel Vetter23010e42010-03-08 13:35:02 +01001527 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001528
Chris Wilsonab182822009-09-22 18:46:17 +01001529 if (obj_priv->madv != I915_MADV_WILLNEED) {
1530 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1531 drm_gem_object_unreference(obj);
1532 mutex_unlock(&dev->struct_mutex);
1533 return -EINVAL;
1534 }
1535
1536
Jesse Barnesde151cf2008-11-12 10:03:55 -08001537 if (!obj_priv->mmap_offset) {
1538 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001539 if (ret) {
1540 drm_gem_object_unreference(obj);
1541 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001542 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001543 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001544 }
1545
1546 args->offset = obj_priv->mmap_offset;
1547
Jesse Barnesde151cf2008-11-12 10:03:55 -08001548 /*
1549 * Pull it into the GTT so that we have a page list (makes the
1550 * initial fault faster and any subsequent flushing possible).
1551 */
1552 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001553 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001554 if (ret) {
1555 drm_gem_object_unreference(obj);
1556 mutex_unlock(&dev->struct_mutex);
1557 return ret;
1558 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001559 }
1560
1561 drm_gem_object_unreference(obj);
1562 mutex_unlock(&dev->struct_mutex);
1563
1564 return 0;
1565}
1566
Chris Wilson5cdf5882010-09-27 15:51:07 +01001567static void
Eric Anholt856fa192009-03-19 14:10:50 -07001568i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001569{
Daniel Vetter23010e42010-03-08 13:35:02 +01001570 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001571 int page_count = obj->size / PAGE_SIZE;
1572 int i;
1573
Eric Anholt856fa192009-03-19 14:10:50 -07001574 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001575 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001576
1577 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001578 return;
1579
Eric Anholt280b7132009-03-12 16:56:27 -07001580 if (obj_priv->tiling_mode != I915_TILING_NONE)
1581 i915_gem_object_save_bit_17_swizzle(obj);
1582
Chris Wilson3ef94da2009-09-14 16:50:29 +01001583 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001584 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001585
1586 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001587 if (obj_priv->dirty)
1588 set_page_dirty(obj_priv->pages[i]);
1589
1590 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001591 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001592
1593 page_cache_release(obj_priv->pages[i]);
1594 }
Eric Anholt673a3942008-07-30 12:06:12 -07001595 obj_priv->dirty = 0;
1596
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001597 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001598 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001599}
1600
Chris Wilsona56ba562010-09-28 10:07:56 +01001601static uint32_t
1602i915_gem_next_request_seqno(struct drm_device *dev,
1603 struct intel_ring_buffer *ring)
1604{
1605 drm_i915_private_t *dev_priv = dev->dev_private;
1606
1607 ring->outstanding_lazy_request = true;
1608 return dev_priv->next_seqno;
1609}
1610
Eric Anholt673a3942008-07-30 12:06:12 -07001611static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001612i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001613 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001614{
Chris Wilsona56ba562010-09-28 10:07:56 +01001615 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001616 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001617 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001618
Zou Nan hai852835f2010-05-21 09:08:56 +08001619 BUG_ON(ring == NULL);
1620 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001621
1622 /* Add a reference if we're newly entering the active list. */
1623 if (!obj_priv->active) {
1624 drm_gem_object_reference(obj);
1625 obj_priv->active = 1;
1626 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001627
Eric Anholt673a3942008-07-30 12:06:12 -07001628 /* Move from whatever list we were on to the tail of execution. */
Zou Nan hai852835f2010-05-21 09:08:56 +08001629 list_move_tail(&obj_priv->list, &ring->active_list);
Chris Wilsona56ba562010-09-28 10:07:56 +01001630 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001631}
1632
Eric Anholtce44b0e2008-11-06 16:00:31 -08001633static void
1634i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1635{
1636 struct drm_device *dev = obj->dev;
1637 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001638 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001639
1640 BUG_ON(!obj_priv->active);
1641 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1642 obj_priv->last_rendering_seqno = 0;
1643}
Eric Anholt673a3942008-07-30 12:06:12 -07001644
Chris Wilson963b4832009-09-20 23:03:54 +01001645/* Immediately discard the backing storage */
1646static void
1647i915_gem_object_truncate(struct drm_gem_object *obj)
1648{
Daniel Vetter23010e42010-03-08 13:35:02 +01001649 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001650 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001651
Chris Wilsonae9fed62010-08-07 11:01:30 +01001652 /* Our goal here is to return as much of the memory as
1653 * is possible back to the system as we are called from OOM.
1654 * To do this we must instruct the shmfs to drop all of its
1655 * backing pages, *now*. Here we mirror the actions taken
1656 * when by shmem_delete_inode() to release the backing store.
1657 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001658 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001659 truncate_inode_pages(inode->i_mapping, 0);
1660 if (inode->i_op->truncate_range)
1661 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001662
1663 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001664}
1665
1666static inline int
1667i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1668{
1669 return obj_priv->madv == I915_MADV_DONTNEED;
1670}
1671
Eric Anholt673a3942008-07-30 12:06:12 -07001672static void
1673i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1674{
1675 struct drm_device *dev = obj->dev;
1676 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001677 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001678
Eric Anholt673a3942008-07-30 12:06:12 -07001679 if (obj_priv->pin_count != 0)
Chris Wilsonf13d3f72010-09-20 17:36:15 +01001680 list_move_tail(&obj_priv->list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001681 else
1682 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1683
Daniel Vetter99fcb762010-02-07 16:20:18 +01001684 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1685
Eric Anholtce44b0e2008-11-06 16:00:31 -08001686 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001687 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001688 if (obj_priv->active) {
1689 obj_priv->active = 0;
1690 drm_gem_object_unreference(obj);
1691 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001692 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001693}
1694
Chris Wilson92204342010-09-18 11:02:01 +01001695static void
Daniel Vetter63560392010-02-19 11:51:59 +01001696i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001697 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001698 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001699{
1700 drm_i915_private_t *dev_priv = dev->dev_private;
1701 struct drm_i915_gem_object *obj_priv, *next;
1702
1703 list_for_each_entry_safe(obj_priv, next,
1704 &dev_priv->mm.gpu_write_list,
1705 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001706 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001707
Chris Wilson2b6efaa2010-09-14 17:04:02 +01001708 if (obj->write_domain & flush_domains &&
1709 obj_priv->ring == ring) {
Daniel Vetter63560392010-02-19 11:51:59 +01001710 uint32_t old_write_domain = obj->write_domain;
1711
1712 obj->write_domain = 0;
1713 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001714 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001715
1716 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001717 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1718 struct drm_i915_fence_reg *reg =
1719 &dev_priv->fence_regs[obj_priv->fence_reg];
1720 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001721 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001722 }
Daniel Vetter63560392010-02-19 11:51:59 +01001723
1724 trace_i915_gem_object_change_domain(obj,
1725 obj->read_domains,
1726 old_write_domain);
1727 }
1728 }
1729}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001730
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001731uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001732i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001733 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001734 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001735 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001736{
1737 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001738 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001739 uint32_t seqno;
1740 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001741
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001742 if (file != NULL)
1743 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001744
Chris Wilson8dc5d142010-08-12 12:36:12 +01001745 if (request == NULL) {
1746 request = kzalloc(sizeof(*request), GFP_KERNEL);
1747 if (request == NULL)
1748 return 0;
1749 }
Eric Anholt673a3942008-07-30 12:06:12 -07001750
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001751 seqno = ring->add_request(dev, ring, 0);
Chris Wilsona56ba562010-09-28 10:07:56 +01001752 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001753
1754 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001755 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001756 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001757 was_empty = list_empty(&ring->request_list);
1758 list_add_tail(&request->list, &ring->request_list);
1759
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001760 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001761 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001762 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001763 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001764 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001765 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001766 }
Eric Anholt673a3942008-07-30 12:06:12 -07001767
Ben Gamarif65d9422009-09-14 17:48:44 -04001768 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001769 mod_timer(&dev_priv->hangcheck_timer,
1770 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001771 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001772 queue_delayed_work(dev_priv->wq,
1773 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001774 }
Eric Anholt673a3942008-07-30 12:06:12 -07001775 return seqno;
1776}
1777
1778/**
1779 * Command execution barrier
1780 *
1781 * Ensures that all commands in the ring are finished
1782 * before signalling the CPU
1783 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001784static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001785i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001786{
Eric Anholt673a3942008-07-30 12:06:12 -07001787 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001788
1789 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001790 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001791 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001792
1793 ring->flush(dev, ring,
1794 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001795}
1796
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001797static inline void
1798i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001799{
Chris Wilson1c255952010-09-26 11:03:27 +01001800 struct drm_i915_file_private *file_priv = request->file_priv;
1801
1802 if (!file_priv)
1803 return;
1804
1805 spin_lock(&file_priv->mm.lock);
1806 list_del(&request->client_list);
1807 request->file_priv = NULL;
1808 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001809}
1810
Chris Wilsondfaae392010-09-22 10:31:52 +01001811static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1812 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001813{
Chris Wilsondfaae392010-09-22 10:31:52 +01001814 while (!list_empty(&ring->request_list)) {
1815 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001816
Chris Wilsondfaae392010-09-22 10:31:52 +01001817 request = list_first_entry(&ring->request_list,
1818 struct drm_i915_gem_request,
1819 list);
1820
1821 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001822 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001823 kfree(request);
1824 }
1825
1826 while (!list_empty(&ring->active_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001827 struct drm_i915_gem_object *obj_priv;
1828
Chris Wilsondfaae392010-09-22 10:31:52 +01001829 obj_priv = list_first_entry(&ring->active_list,
1830 struct drm_i915_gem_object,
1831 list);
1832
1833 obj_priv->base.write_domain = 0;
1834 list_del_init(&obj_priv->gpu_write_list);
1835 i915_gem_object_move_to_inactive(&obj_priv->base);
1836 }
1837}
1838
Chris Wilson069efc12010-09-30 16:53:18 +01001839void i915_gem_reset(struct drm_device *dev)
Chris Wilsondfaae392010-09-22 10:31:52 +01001840{
1841 struct drm_i915_private *dev_priv = dev->dev_private;
1842 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001843 int i;
Chris Wilsondfaae392010-09-22 10:31:52 +01001844
1845 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
1846 if (HAS_BSD(dev))
1847 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
1848
1849 /* Remove anything from the flushing lists. The GPU cache is likely
1850 * to be lost on reset along with the data, so simply move the
1851 * lost bo to the inactive list.
1852 */
1853 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001854 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1855 struct drm_i915_gem_object,
1856 list);
1857
1858 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001859 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001860 i915_gem_object_move_to_inactive(&obj_priv->base);
1861 }
Chris Wilson9375e442010-09-19 12:21:28 +01001862
Chris Wilsondfaae392010-09-22 10:31:52 +01001863 /* Move everything out of the GPU domains to ensure we do any
1864 * necessary invalidation upon reuse.
1865 */
Chris Wilson77f01232010-09-19 12:31:36 +01001866 list_for_each_entry(obj_priv,
1867 &dev_priv->mm.inactive_list,
1868 list)
1869 {
1870 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1871 }
Chris Wilson069efc12010-09-30 16:53:18 +01001872
1873 /* The fence registers are invalidated so clear them out */
1874 for (i = 0; i < 16; i++) {
1875 struct drm_i915_fence_reg *reg;
1876
1877 reg = &dev_priv->fence_regs[i];
1878 if (!reg->obj)
1879 continue;
1880
1881 i915_gem_clear_fence_reg(reg->obj);
1882 }
Chris Wilson77f01232010-09-19 12:31:36 +01001883}
1884
Eric Anholt673a3942008-07-30 12:06:12 -07001885/**
1886 * This function clears the request list as sequence numbers are passed.
1887 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001888static void
1889i915_gem_retire_requests_ring(struct drm_device *dev,
1890 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001891{
1892 drm_i915_private_t *dev_priv = dev->dev_private;
1893 uint32_t seqno;
1894
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001895 if (!ring->status_page.page_addr ||
1896 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001897 return;
1898
Chris Wilson23bc5982010-09-29 16:10:57 +01001899 WARN_ON(i915_verify_lists(dev));
1900
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001901 seqno = ring->get_seqno(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001902 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001903 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001904
Zou Nan hai852835f2010-05-21 09:08:56 +08001905 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001906 struct drm_i915_gem_request,
1907 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001908
Chris Wilsondfaae392010-09-22 10:31:52 +01001909 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001910 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001911
1912 trace_i915_gem_request_retire(dev, request->seqno);
1913
1914 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001915 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001916 kfree(request);
1917 }
1918
1919 /* Move any buffers on the active list that are no longer referenced
1920 * by the ringbuffer to the flushing/inactive lists as appropriate.
1921 */
1922 while (!list_empty(&ring->active_list)) {
1923 struct drm_gem_object *obj;
1924 struct drm_i915_gem_object *obj_priv;
1925
1926 obj_priv = list_first_entry(&ring->active_list,
1927 struct drm_i915_gem_object,
1928 list);
1929
Chris Wilsondfaae392010-09-22 10:31:52 +01001930 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001931 break;
1932
1933 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001934 if (obj->write_domain != 0)
1935 i915_gem_object_move_to_flushing(obj);
1936 else
1937 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001938 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001939
1940 if (unlikely (dev_priv->trace_irq_seqno &&
1941 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001942 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001943 dev_priv->trace_irq_seqno = 0;
1944 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001945
1946 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001947}
1948
1949void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001950i915_gem_retire_requests(struct drm_device *dev)
1951{
1952 drm_i915_private_t *dev_priv = dev->dev_private;
1953
Chris Wilsonbe726152010-07-23 23:18:50 +01001954 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1955 struct drm_i915_gem_object *obj_priv, *tmp;
1956
1957 /* We must be careful that during unbind() we do not
1958 * accidentally infinitely recurse into retire requests.
1959 * Currently:
1960 * retire -> free -> unbind -> wait -> retire_ring
1961 */
1962 list_for_each_entry_safe(obj_priv, tmp,
1963 &dev_priv->mm.deferred_free_list,
1964 list)
1965 i915_gem_free_object_tail(&obj_priv->base);
1966 }
1967
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001968 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1969 if (HAS_BSD(dev))
1970 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1971}
1972
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001973static void
Eric Anholt673a3942008-07-30 12:06:12 -07001974i915_gem_retire_work_handler(struct work_struct *work)
1975{
1976 drm_i915_private_t *dev_priv;
1977 struct drm_device *dev;
1978
1979 dev_priv = container_of(work, drm_i915_private_t,
1980 mm.retire_work.work);
1981 dev = dev_priv->dev;
1982
Chris Wilson891b48c2010-09-29 12:26:37 +01001983 /* Come back later if the device is busy... */
1984 if (!mutex_trylock(&dev->struct_mutex)) {
1985 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1986 return;
1987 }
1988
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001989 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001990
Keith Packard6dbe2772008-10-14 21:41:13 -07001991 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001992 (!list_empty(&dev_priv->render_ring.request_list) ||
1993 (HAS_BSD(dev) &&
1994 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001995 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001996 mutex_unlock(&dev->struct_mutex);
1997}
1998
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001999int
Zou Nan hai852835f2010-05-21 09:08:56 +08002000i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002001 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002002{
2003 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002004 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002005 int ret = 0;
2006
2007 BUG_ON(seqno == 0);
2008
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002009 if (atomic_read(&dev_priv->mm.wedged))
2010 return -EAGAIN;
2011
Chris Wilsona56ba562010-09-28 10:07:56 +01002012 if (ring->outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01002013 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01002014 if (seqno == 0)
2015 return -ENOMEM;
2016 }
Chris Wilsona56ba562010-09-28 10:07:56 +01002017 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01002018
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002019 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07002020 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002021 ier = I915_READ(DEIER) | I915_READ(GTIER);
2022 else
2023 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002024 if (!ier) {
2025 DRM_ERROR("something (likely vbetool) disabled "
2026 "interrupts, re-enabling\n");
2027 i915_driver_irq_preinstall(dev);
2028 i915_driver_irq_postinstall(dev);
2029 }
2030
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002031 trace_i915_gem_request_wait_begin(dev, seqno);
2032
Zou Nan hai852835f2010-05-21 09:08:56 +08002033 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002034 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002035 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002036 ret = wait_event_interruptible(ring->irq_queue,
2037 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002038 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002039 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002040 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002041 wait_event(ring->irq_queue,
2042 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002043 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002044 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002045
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002046 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08002047 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002048
2049 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002050 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002051 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002052 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002053
2054 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002055 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002056 __func__, ret, seqno, ring->get_seqno(dev, ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002057 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002058
2059 /* Directly dispatch request retiring. While we have the work queue
2060 * to handle this, the waiter on a request often wants an associated
2061 * buffer to have made it to the inactive list, and we would need
2062 * a separate wait queue to handle that.
2063 */
2064 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002065 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002066
2067 return ret;
2068}
2069
Daniel Vetter48764bf2009-09-15 22:57:32 +02002070/**
2071 * Waits for a sequence number to be signaled, and cleans up the
2072 * request and object lists appropriately for that event.
2073 */
2074static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002075i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002076 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002077{
Zou Nan hai852835f2010-05-21 09:08:56 +08002078 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002079}
2080
Chris Wilson20f0cd52010-09-23 11:00:38 +01002081static void
Chris Wilson92204342010-09-18 11:02:01 +01002082i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002083 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002084 struct intel_ring_buffer *ring,
2085 uint32_t invalidate_domains,
2086 uint32_t flush_domains)
2087{
2088 ring->flush(dev, ring, invalidate_domains, flush_domains);
2089 i915_gem_process_flushing_list(dev, flush_domains, ring);
2090}
2091
2092static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002093i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002094 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002095 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002096 uint32_t flush_domains,
2097 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002098{
2099 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002100
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002101 if (flush_domains & I915_GEM_DOMAIN_CPU)
2102 drm_agp_chipset_flush(dev);
Daniel Vetter8bff9172010-02-11 22:19:40 +01002103
Chris Wilson92204342010-09-18 11:02:01 +01002104 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2105 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002106 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002107 &dev_priv->render_ring,
2108 invalidate_domains, flush_domains);
2109 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002110 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002111 &dev_priv->bsd_ring,
2112 invalidate_domains, flush_domains);
2113 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002114}
2115
Eric Anholt673a3942008-07-30 12:06:12 -07002116/**
2117 * Ensures that all rendering to the object has completed and the object is
2118 * safe to unbind from the GTT or access from the CPU.
2119 */
2120static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002121i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2122 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002123{
2124 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002125 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002126 int ret;
2127
Eric Anholte47c68e2008-11-14 13:35:19 -08002128 /* This function only exists to support waiting for existing rendering,
2129 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002130 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002131 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002132
2133 /* If there is rendering queued on the buffer being evicted, wait for
2134 * it.
2135 */
2136 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002137 ret = i915_do_wait_request(dev,
2138 obj_priv->last_rendering_seqno,
2139 interruptible,
2140 obj_priv->ring);
2141 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002142 return ret;
2143 }
2144
2145 return 0;
2146}
2147
2148/**
2149 * Unbinds an object from the GTT aperture.
2150 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002151int
Eric Anholt673a3942008-07-30 12:06:12 -07002152i915_gem_object_unbind(struct drm_gem_object *obj)
2153{
2154 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002155 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002156 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002157 int ret = 0;
2158
Eric Anholt673a3942008-07-30 12:06:12 -07002159 if (obj_priv->gtt_space == NULL)
2160 return 0;
2161
2162 if (obj_priv->pin_count != 0) {
2163 DRM_ERROR("Attempting to unbind pinned buffer\n");
2164 return -EINVAL;
2165 }
2166
Eric Anholt5323fd02009-09-09 11:50:45 -07002167 /* blow away mappings if mapped through GTT */
2168 i915_gem_release_mmap(obj);
2169
Eric Anholt673a3942008-07-30 12:06:12 -07002170 /* Move the object to the CPU domain to ensure that
2171 * any possible CPU writes while it's not in the GTT
2172 * are flushed when we go to remap it. This will
2173 * also ensure that all pending GPU writes are finished
2174 * before we unbind.
2175 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002176 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002177 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002178 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002179 /* Continue on if we fail due to EIO, the GPU is hung so we
2180 * should be safe and we need to cleanup or else we might
2181 * cause memory corruption through use-after-free.
2182 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002183 if (ret) {
2184 i915_gem_clflush_object(obj);
2185 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2186 }
Eric Anholt673a3942008-07-30 12:06:12 -07002187
Daniel Vetter96b47b62009-12-15 17:50:00 +01002188 /* release the fence reg _after_ flushing */
2189 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2190 i915_gem_clear_fence_reg(obj);
2191
Chris Wilson73aa8082010-09-30 11:46:12 +01002192 drm_unbind_agp(obj_priv->agp_mem);
2193 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002194
Eric Anholt856fa192009-03-19 14:10:50 -07002195 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002196 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002197
Chris Wilson73aa8082010-09-30 11:46:12 +01002198 i915_gem_info_remove_gtt(dev_priv, obj->size);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01002199 list_del_init(&obj_priv->list);
Eric Anholt673a3942008-07-30 12:06:12 -07002200
Chris Wilson73aa8082010-09-30 11:46:12 +01002201 drm_mm_put_block(obj_priv->gtt_space);
2202 obj_priv->gtt_space = NULL;
2203
Chris Wilson963b4832009-09-20 23:03:54 +01002204 if (i915_gem_object_is_purgeable(obj_priv))
2205 i915_gem_object_truncate(obj);
2206
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002207 trace_i915_gem_object_unbind(obj);
2208
Chris Wilson8dc17752010-07-23 23:18:51 +01002209 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002210}
2211
Chris Wilsona56ba562010-09-28 10:07:56 +01002212static int i915_ring_idle(struct drm_device *dev,
2213 struct intel_ring_buffer *ring)
2214{
2215 i915_gem_flush_ring(dev, NULL, ring,
2216 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2217 return i915_wait_request(dev,
2218 i915_gem_next_request_seqno(dev, ring),
2219 ring);
2220}
2221
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002222int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002223i915_gpu_idle(struct drm_device *dev)
2224{
2225 drm_i915_private_t *dev_priv = dev->dev_private;
2226 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002227 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002228
Zou Nan haid1b851f2010-05-21 09:08:57 +08002229 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2230 list_empty(&dev_priv->render_ring.active_list) &&
2231 (!HAS_BSD(dev) ||
2232 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002233 if (lists_empty)
2234 return 0;
2235
2236 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002237 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002238 if (ret)
2239 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002240
2241 if (HAS_BSD(dev)) {
Chris Wilsona56ba562010-09-28 10:07:56 +01002242 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002243 if (ret)
2244 return ret;
2245 }
2246
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002247 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002248}
2249
Chris Wilson5cdf5882010-09-27 15:51:07 +01002250static int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002251i915_gem_object_get_pages(struct drm_gem_object *obj,
2252 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002253{
Daniel Vetter23010e42010-03-08 13:35:02 +01002254 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002255 int page_count, i;
2256 struct address_space *mapping;
2257 struct inode *inode;
2258 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002259
Daniel Vetter778c3542010-05-13 11:49:44 +02002260 BUG_ON(obj_priv->pages_refcount
2261 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2262
Eric Anholt856fa192009-03-19 14:10:50 -07002263 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002264 return 0;
2265
2266 /* Get the list of pages out of our struct file. They'll be pinned
2267 * at this point until we release them.
2268 */
2269 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002270 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002271 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002272 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002273 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002274 return -ENOMEM;
2275 }
2276
2277 inode = obj->filp->f_path.dentry->d_inode;
2278 mapping = inode->i_mapping;
2279 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002280 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002281 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002282 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002283 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002284 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002285 if (IS_ERR(page))
2286 goto err_pages;
2287
Eric Anholt856fa192009-03-19 14:10:50 -07002288 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002289 }
Eric Anholt280b7132009-03-12 16:56:27 -07002290
2291 if (obj_priv->tiling_mode != I915_TILING_NONE)
2292 i915_gem_object_do_bit_17_swizzle(obj);
2293
Eric Anholt673a3942008-07-30 12:06:12 -07002294 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002295
2296err_pages:
2297 while (i--)
2298 page_cache_release(obj_priv->pages[i]);
2299
2300 drm_free_large(obj_priv->pages);
2301 obj_priv->pages = NULL;
2302 obj_priv->pages_refcount--;
2303 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002304}
2305
Eric Anholt4e901fd2009-10-26 16:44:17 -07002306static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2307{
2308 struct drm_gem_object *obj = reg->obj;
2309 struct drm_device *dev = obj->dev;
2310 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002311 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002312 int regnum = obj_priv->fence_reg;
2313 uint64_t val;
2314
2315 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2316 0xfffff000) << 32;
2317 val |= obj_priv->gtt_offset & 0xfffff000;
2318 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2319 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2320
2321 if (obj_priv->tiling_mode == I915_TILING_Y)
2322 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2323 val |= I965_FENCE_REG_VALID;
2324
2325 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2326}
2327
Jesse Barnesde151cf2008-11-12 10:03:55 -08002328static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2329{
2330 struct drm_gem_object *obj = reg->obj;
2331 struct drm_device *dev = obj->dev;
2332 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002333 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002334 int regnum = obj_priv->fence_reg;
2335 uint64_t val;
2336
2337 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2338 0xfffff000) << 32;
2339 val |= obj_priv->gtt_offset & 0xfffff000;
2340 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2341 if (obj_priv->tiling_mode == I915_TILING_Y)
2342 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2343 val |= I965_FENCE_REG_VALID;
2344
2345 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2346}
2347
2348static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2349{
2350 struct drm_gem_object *obj = reg->obj;
2351 struct drm_device *dev = obj->dev;
2352 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002353 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002354 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002355 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002356 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002357 uint32_t pitch_val;
2358
2359 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2360 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002361 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002362 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002363 return;
2364 }
2365
Jesse Barnes0f973f22009-01-26 17:10:45 -08002366 if (obj_priv->tiling_mode == I915_TILING_Y &&
2367 HAS_128_BYTE_Y_TILING(dev))
2368 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002369 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002370 tile_width = 512;
2371
2372 /* Note: pitch better be a power of two tile widths */
2373 pitch_val = obj_priv->stride / tile_width;
2374 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002375
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002376 if (obj_priv->tiling_mode == I915_TILING_Y &&
2377 HAS_128_BYTE_Y_TILING(dev))
2378 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2379 else
2380 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2381
Jesse Barnesde151cf2008-11-12 10:03:55 -08002382 val = obj_priv->gtt_offset;
2383 if (obj_priv->tiling_mode == I915_TILING_Y)
2384 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2385 val |= I915_FENCE_SIZE_BITS(obj->size);
2386 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2387 val |= I830_FENCE_REG_VALID;
2388
Eric Anholtdc529a42009-03-10 22:34:49 -07002389 if (regnum < 8)
2390 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2391 else
2392 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2393 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002394}
2395
2396static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2397{
2398 struct drm_gem_object *obj = reg->obj;
2399 struct drm_device *dev = obj->dev;
2400 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002401 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402 int regnum = obj_priv->fence_reg;
2403 uint32_t val;
2404 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002405 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002406
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002407 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002408 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002409 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002410 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002411 return;
2412 }
2413
Eric Anholte76a16d2009-05-26 17:44:56 -07002414 pitch_val = obj_priv->stride / 128;
2415 pitch_val = ffs(pitch_val) - 1;
2416 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2417
Jesse Barnesde151cf2008-11-12 10:03:55 -08002418 val = obj_priv->gtt_offset;
2419 if (obj_priv->tiling_mode == I915_TILING_Y)
2420 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002421 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2422 WARN_ON(fence_size_bits & ~0x00000f00);
2423 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002424 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2425 val |= I830_FENCE_REG_VALID;
2426
2427 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002428}
2429
Chris Wilson2cf34d72010-09-14 13:03:28 +01002430static int i915_find_fence_reg(struct drm_device *dev,
2431 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002432{
2433 struct drm_i915_fence_reg *reg = NULL;
2434 struct drm_i915_gem_object *obj_priv = NULL;
2435 struct drm_i915_private *dev_priv = dev->dev_private;
2436 struct drm_gem_object *obj = NULL;
2437 int i, avail, ret;
2438
2439 /* First try to find a free reg */
2440 avail = 0;
2441 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2442 reg = &dev_priv->fence_regs[i];
2443 if (!reg->obj)
2444 return i;
2445
Daniel Vetter23010e42010-03-08 13:35:02 +01002446 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002447 if (!obj_priv->pin_count)
2448 avail++;
2449 }
2450
2451 if (avail == 0)
2452 return -ENOSPC;
2453
2454 /* None available, try to steal one or wait for a user to finish */
2455 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002456 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2457 lru_list) {
2458 obj = reg->obj;
2459 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002460
2461 if (obj_priv->pin_count)
2462 continue;
2463
2464 /* found one! */
2465 i = obj_priv->fence_reg;
2466 break;
2467 }
2468
2469 BUG_ON(i == I915_FENCE_REG_NONE);
2470
2471 /* We only have a reference on obj from the active list. put_fence_reg
2472 * might drop that one, causing a use-after-free in it. So hold a
2473 * private reference to obj like the other callers of put_fence_reg
2474 * (set_tiling ioctl) do. */
2475 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002476 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002477 drm_gem_object_unreference(obj);
2478 if (ret != 0)
2479 return ret;
2480
2481 return i;
2482}
2483
Jesse Barnesde151cf2008-11-12 10:03:55 -08002484/**
2485 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2486 * @obj: object to map through a fence reg
2487 *
2488 * When mapping objects through the GTT, userspace wants to be able to write
2489 * to them without having to worry about swizzling if the object is tiled.
2490 *
2491 * This function walks the fence regs looking for a free one for @obj,
2492 * stealing one if it can't find any.
2493 *
2494 * It then sets up the reg based on the object's properties: address, pitch
2495 * and tiling format.
2496 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002497int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002498i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2499 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002500{
2501 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002502 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002503 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002504 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002505 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002506
Eric Anholta09ba7f2009-08-29 12:49:51 -07002507 /* Just update our place in the LRU if our fence is getting used. */
2508 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002509 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2510 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002511 return 0;
2512 }
2513
Jesse Barnesde151cf2008-11-12 10:03:55 -08002514 switch (obj_priv->tiling_mode) {
2515 case I915_TILING_NONE:
2516 WARN(1, "allocating a fence for non-tiled object?\n");
2517 break;
2518 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002519 if (!obj_priv->stride)
2520 return -EINVAL;
2521 WARN((obj_priv->stride & (512 - 1)),
2522 "object 0x%08x is X tiled but has non-512B pitch\n",
2523 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002524 break;
2525 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002526 if (!obj_priv->stride)
2527 return -EINVAL;
2528 WARN((obj_priv->stride & (128 - 1)),
2529 "object 0x%08x is Y tiled but has non-128B pitch\n",
2530 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002531 break;
2532 }
2533
Chris Wilson2cf34d72010-09-14 13:03:28 +01002534 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002535 if (ret < 0)
2536 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002537
Daniel Vetterae3db242010-02-19 11:51:58 +01002538 obj_priv->fence_reg = ret;
2539 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002540 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002541
Jesse Barnesde151cf2008-11-12 10:03:55 -08002542 reg->obj = obj;
2543
Chris Wilsone259bef2010-09-17 00:32:02 +01002544 switch (INTEL_INFO(dev)->gen) {
2545 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002546 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002547 break;
2548 case 5:
2549 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002550 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002551 break;
2552 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002553 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002554 break;
2555 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002556 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002557 break;
2558 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002559
Daniel Vetterae3db242010-02-19 11:51:58 +01002560 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2561 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002562
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002563 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002564}
2565
2566/**
2567 * i915_gem_clear_fence_reg - clear out fence register info
2568 * @obj: object to clear
2569 *
2570 * Zeroes out the fence register itself and clears out the associated
2571 * data structures in dev_priv and obj_priv.
2572 */
2573static void
2574i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2575{
2576 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002577 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002578 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002579 struct drm_i915_fence_reg *reg =
2580 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002581 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002582
Chris Wilsone259bef2010-09-17 00:32:02 +01002583 switch (INTEL_INFO(dev)->gen) {
2584 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002585 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2586 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002587 break;
2588 case 5:
2589 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002590 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002591 break;
2592 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002593 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002594 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002595 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002596 case 2:
2597 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002598
2599 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002600 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002601 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002602
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002603 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002604 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002605 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002606}
2607
Eric Anholt673a3942008-07-30 12:06:12 -07002608/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002609 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2610 * to the buffer to finish, and then resets the fence register.
2611 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002612 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002613 *
2614 * Zeroes out the fence register itself and clears out the associated
2615 * data structures in dev_priv and obj_priv.
2616 */
2617int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002618i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2619 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002620{
2621 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002622 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002623 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002624 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002625
2626 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2627 return 0;
2628
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002629 /* If we've changed tiling, GTT-mappings of the object
2630 * need to re-fault to ensure that the correct fence register
2631 * setup is in place.
2632 */
2633 i915_gem_release_mmap(obj);
2634
Chris Wilson52dc7d32009-06-06 09:46:01 +01002635 /* On the i915, GPU access to tiled buffers is via a fence,
2636 * therefore we must wait for any outstanding access to complete
2637 * before clearing the fence.
2638 */
Chris Wilson53640e12010-09-20 11:40:50 +01002639 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2640 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002641 int ret;
2642
Chris Wilson2cf34d72010-09-14 13:03:28 +01002643 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002644 if (ret)
2645 return ret;
2646
Chris Wilson2cf34d72010-09-14 13:03:28 +01002647 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002648 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002649 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002650
2651 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002652 }
2653
Daniel Vetter4a726612010-02-01 13:59:16 +01002654 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002655 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002656
2657 return 0;
2658}
2659
2660/**
Eric Anholt673a3942008-07-30 12:06:12 -07002661 * Finds free space in the GTT aperture and binds the object there.
2662 */
2663static int
2664i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2665{
2666 struct drm_device *dev = obj->dev;
2667 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002668 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002669 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002670 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002671 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002672
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002673 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002674 DRM_ERROR("Attempting to bind a purgeable object\n");
2675 return -EINVAL;
2676 }
2677
Eric Anholt673a3942008-07-30 12:06:12 -07002678 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002679 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002680 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002681 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2682 return -EINVAL;
2683 }
2684
Chris Wilson654fc602010-05-27 13:18:21 +01002685 /* If the object is bigger than the entire aperture, reject it early
2686 * before evicting everything in a vain attempt to find space.
2687 */
Chris Wilson73aa8082010-09-30 11:46:12 +01002688 if (obj->size > dev_priv->mm.gtt_total) {
Chris Wilson654fc602010-05-27 13:18:21 +01002689 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2690 return -E2BIG;
2691 }
2692
Eric Anholt673a3942008-07-30 12:06:12 -07002693 search_free:
2694 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2695 obj->size, alignment, 0);
2696 if (free_space != NULL) {
2697 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2698 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002699 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002700 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002701 }
2702 if (obj_priv->gtt_space == NULL) {
2703 /* If the gtt is empty and we're still having trouble
2704 * fitting our object in, we're out of memory.
2705 */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002706 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002707 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002708 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002709
Eric Anholt673a3942008-07-30 12:06:12 -07002710 goto search_free;
2711 }
2712
Chris Wilson4bdadb92010-01-27 13:36:32 +00002713 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002714 if (ret) {
2715 drm_mm_put_block(obj_priv->gtt_space);
2716 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002717
2718 if (ret == -ENOMEM) {
2719 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002720 ret = i915_gem_evict_something(dev, obj->size,
2721 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002722 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002723 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002724 if (gfpmask) {
2725 gfpmask = 0;
2726 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002727 }
2728
2729 return ret;
2730 }
2731
2732 goto search_free;
2733 }
2734
Eric Anholt673a3942008-07-30 12:06:12 -07002735 return ret;
2736 }
2737
Eric Anholt673a3942008-07-30 12:06:12 -07002738 /* Create an AGP memory structure pointing at our pages, and bind it
2739 * into the GTT.
2740 */
2741 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002742 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002743 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002744 obj_priv->gtt_offset,
2745 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002746 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002747 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002748 drm_mm_put_block(obj_priv->gtt_space);
2749 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002750
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002751 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002752 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002753 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002754
2755 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002756 }
Eric Anholt673a3942008-07-30 12:06:12 -07002757
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002758 /* keep track of bounds object by adding it to the inactive list */
2759 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01002760 i915_gem_info_add_gtt(dev_priv, obj->size);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002761
Eric Anholt673a3942008-07-30 12:06:12 -07002762 /* Assert that the object is not currently in any GPU domain. As it
2763 * wasn't in the GTT, there shouldn't be any way it could have been in
2764 * a GPU cache
2765 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002766 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2767 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002768
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002769 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2770
Eric Anholt673a3942008-07-30 12:06:12 -07002771 return 0;
2772}
2773
2774void
2775i915_gem_clflush_object(struct drm_gem_object *obj)
2776{
Daniel Vetter23010e42010-03-08 13:35:02 +01002777 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002778
2779 /* If we don't have a page list set up, then we're not pinned
2780 * to GPU, and we can ignore the cache flush because it'll happen
2781 * again at bind time.
2782 */
Eric Anholt856fa192009-03-19 14:10:50 -07002783 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002784 return;
2785
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002786 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002787
Eric Anholt856fa192009-03-19 14:10:50 -07002788 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002789}
2790
Eric Anholte47c68e2008-11-14 13:35:19 -08002791/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002792static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002793i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2794 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002795{
2796 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002797 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002798
2799 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002800 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002801
2802 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002803 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002804 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002805 to_intel_bo(obj)->ring,
2806 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002807 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002808
2809 trace_i915_gem_object_change_domain(obj,
2810 obj->read_domains,
2811 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002812
2813 if (pipelined)
2814 return 0;
2815
Chris Wilson2cf34d72010-09-14 13:03:28 +01002816 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002817}
2818
2819/** Flushes the GTT write domain for the object if it's dirty. */
2820static void
2821i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2822{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002823 uint32_t old_write_domain;
2824
Eric Anholte47c68e2008-11-14 13:35:19 -08002825 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2826 return;
2827
2828 /* No actual flushing is required for the GTT write domain. Writes
2829 * to it immediately go to main memory as far as we know, so there's
2830 * no chipset flush. It also doesn't land in render cache.
2831 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002832 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002833 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002834
2835 trace_i915_gem_object_change_domain(obj,
2836 obj->read_domains,
2837 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002838}
2839
2840/** Flushes the CPU write domain for the object if it's dirty. */
2841static void
2842i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2843{
2844 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002845 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002846
2847 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2848 return;
2849
2850 i915_gem_clflush_object(obj);
2851 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002852 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002853 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002854
2855 trace_i915_gem_object_change_domain(obj,
2856 obj->read_domains,
2857 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002858}
2859
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002860/**
2861 * Moves a single object to the GTT read, and possibly write domain.
2862 *
2863 * This function returns when the move is complete, including waiting on
2864 * flushes to occur.
2865 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002866int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002867i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2868{
Daniel Vetter23010e42010-03-08 13:35:02 +01002869 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002870 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002871 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002872
Eric Anholt02354392008-11-26 13:58:13 -08002873 /* Not valid to be called on unbound objects. */
2874 if (obj_priv->gtt_space == NULL)
2875 return -EINVAL;
2876
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002877 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002878 if (ret != 0)
2879 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002880
Chris Wilson72133422010-09-13 23:56:38 +01002881 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002882
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002883 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002884 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002885 if (ret)
2886 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002887 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002888
Chris Wilson72133422010-09-13 23:56:38 +01002889 old_write_domain = obj->write_domain;
2890 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002891
2892 /* It should now be out of any other write domains, and we can update
2893 * the domain values for our changes.
2894 */
2895 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2896 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002897 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002898 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002899 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002900 obj_priv->dirty = 1;
2901 }
2902
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002903 trace_i915_gem_object_change_domain(obj,
2904 old_read_domains,
2905 old_write_domain);
2906
Eric Anholte47c68e2008-11-14 13:35:19 -08002907 return 0;
2908}
2909
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002910/*
2911 * Prepare buffer for display plane. Use uninterruptible for possible flush
2912 * wait, as in modesetting process we're not supposed to be interrupted.
2913 */
2914int
Chris Wilson48b956c2010-09-14 12:50:34 +01002915i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2916 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002917{
Daniel Vetter23010e42010-03-08 13:35:02 +01002918 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002919 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002920 int ret;
2921
2922 /* Not valid to be called on unbound objects. */
2923 if (obj_priv->gtt_space == NULL)
2924 return -EINVAL;
2925
Chris Wilsonced270f2010-09-26 22:47:46 +01002926 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson48b956c2010-09-14 12:50:34 +01002927 if (ret)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002928 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002929
Chris Wilsonced270f2010-09-26 22:47:46 +01002930 /* Currently, we are always called from an non-interruptible context. */
2931 if (!pipelined) {
2932 ret = i915_gem_object_wait_rendering(obj, false);
2933 if (ret)
2934 return ret;
2935 }
2936
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002937 i915_gem_object_flush_cpu_write_domain(obj);
2938
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002939 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002940 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002941
2942 trace_i915_gem_object_change_domain(obj,
2943 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002944 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002945
2946 return 0;
2947}
2948
Eric Anholte47c68e2008-11-14 13:35:19 -08002949/**
2950 * Moves a single object to the CPU read, and possibly write domain.
2951 *
2952 * This function returns when the move is complete, including waiting on
2953 * flushes to occur.
2954 */
2955static int
2956i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2957{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002958 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002959 int ret;
2960
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002961 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002962 if (ret != 0)
2963 return ret;
2964
2965 i915_gem_object_flush_gtt_write_domain(obj);
2966
2967 /* If we have a partially-valid cache of the object in the CPU,
2968 * finish invalidating it and free the per-page flags.
2969 */
2970 i915_gem_object_set_to_full_cpu_read_domain(obj);
2971
Chris Wilson72133422010-09-13 23:56:38 +01002972 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002973 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002974 if (ret)
2975 return ret;
2976 }
2977
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002978 old_write_domain = obj->write_domain;
2979 old_read_domains = obj->read_domains;
2980
Eric Anholte47c68e2008-11-14 13:35:19 -08002981 /* Flush the CPU cache if it's still invalid. */
2982 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2983 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002984
2985 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2986 }
2987
2988 /* It should now be out of any other write domains, and we can update
2989 * the domain values for our changes.
2990 */
2991 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2992
2993 /* If we're writing through the CPU, then the GPU read domains will
2994 * need to be invalidated at next use.
2995 */
2996 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002997 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002998 obj->write_domain = I915_GEM_DOMAIN_CPU;
2999 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003000
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003001 trace_i915_gem_object_change_domain(obj,
3002 old_read_domains,
3003 old_write_domain);
3004
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003005 return 0;
3006}
3007
Eric Anholt673a3942008-07-30 12:06:12 -07003008/*
3009 * Set the next domain for the specified object. This
3010 * may not actually perform the necessary flushing/invaliding though,
3011 * as that may want to be batched with other set_domain operations
3012 *
3013 * This is (we hope) the only really tricky part of gem. The goal
3014 * is fairly simple -- track which caches hold bits of the object
3015 * and make sure they remain coherent. A few concrete examples may
3016 * help to explain how it works. For shorthand, we use the notation
3017 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3018 * a pair of read and write domain masks.
3019 *
3020 * Case 1: the batch buffer
3021 *
3022 * 1. Allocated
3023 * 2. Written by CPU
3024 * 3. Mapped to GTT
3025 * 4. Read by GPU
3026 * 5. Unmapped from GTT
3027 * 6. Freed
3028 *
3029 * Let's take these a step at a time
3030 *
3031 * 1. Allocated
3032 * Pages allocated from the kernel may still have
3033 * cache contents, so we set them to (CPU, CPU) always.
3034 * 2. Written by CPU (using pwrite)
3035 * The pwrite function calls set_domain (CPU, CPU) and
3036 * this function does nothing (as nothing changes)
3037 * 3. Mapped by GTT
3038 * This function asserts that the object is not
3039 * currently in any GPU-based read or write domains
3040 * 4. Read by GPU
3041 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3042 * As write_domain is zero, this function adds in the
3043 * current read domains (CPU+COMMAND, 0).
3044 * flush_domains is set to CPU.
3045 * invalidate_domains is set to COMMAND
3046 * clflush is run to get data out of the CPU caches
3047 * then i915_dev_set_domain calls i915_gem_flush to
3048 * emit an MI_FLUSH and drm_agp_chipset_flush
3049 * 5. Unmapped from GTT
3050 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3051 * flush_domains and invalidate_domains end up both zero
3052 * so no flushing/invalidating happens
3053 * 6. Freed
3054 * yay, done
3055 *
3056 * Case 2: The shared render buffer
3057 *
3058 * 1. Allocated
3059 * 2. Mapped to GTT
3060 * 3. Read/written by GPU
3061 * 4. set_domain to (CPU,CPU)
3062 * 5. Read/written by CPU
3063 * 6. Read/written by GPU
3064 *
3065 * 1. Allocated
3066 * Same as last example, (CPU, CPU)
3067 * 2. Mapped to GTT
3068 * Nothing changes (assertions find that it is not in the GPU)
3069 * 3. Read/written by GPU
3070 * execbuffer calls set_domain (RENDER, RENDER)
3071 * flush_domains gets CPU
3072 * invalidate_domains gets GPU
3073 * clflush (obj)
3074 * MI_FLUSH and drm_agp_chipset_flush
3075 * 4. set_domain (CPU, CPU)
3076 * flush_domains gets GPU
3077 * invalidate_domains gets CPU
3078 * wait_rendering (obj) to make sure all drawing is complete.
3079 * This will include an MI_FLUSH to get the data from GPU
3080 * to memory
3081 * clflush (obj) to invalidate the CPU cache
3082 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3083 * 5. Read/written by CPU
3084 * cache lines are loaded and dirtied
3085 * 6. Read written by GPU
3086 * Same as last GPU access
3087 *
3088 * Case 3: The constant buffer
3089 *
3090 * 1. Allocated
3091 * 2. Written by CPU
3092 * 3. Read by GPU
3093 * 4. Updated (written) by CPU again
3094 * 5. Read by GPU
3095 *
3096 * 1. Allocated
3097 * (CPU, CPU)
3098 * 2. Written by CPU
3099 * (CPU, CPU)
3100 * 3. Read by GPU
3101 * (CPU+RENDER, 0)
3102 * flush_domains = CPU
3103 * invalidate_domains = RENDER
3104 * clflush (obj)
3105 * MI_FLUSH
3106 * drm_agp_chipset_flush
3107 * 4. Updated (written) by CPU again
3108 * (CPU, CPU)
3109 * flush_domains = 0 (no previous write domain)
3110 * invalidate_domains = 0 (no new read domains)
3111 * 5. Read by GPU
3112 * (CPU+RENDER, 0)
3113 * flush_domains = CPU
3114 * invalidate_domains = RENDER
3115 * clflush (obj)
3116 * MI_FLUSH
3117 * drm_agp_chipset_flush
3118 */
Keith Packardc0d90822008-11-20 23:11:08 -08003119static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003120i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003121{
3122 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003123 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003124 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003125 uint32_t invalidate_domains = 0;
3126 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003127 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003128
Eric Anholt8b0e3782009-02-19 14:40:50 -08003129 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3130 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003131
Jesse Barnes652c3932009-08-17 13:31:43 -07003132 intel_mark_busy(dev, obj);
3133
Eric Anholt673a3942008-07-30 12:06:12 -07003134 /*
3135 * If the object isn't moving to a new write domain,
3136 * let the object stay in multiple read domains
3137 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003138 if (obj->pending_write_domain == 0)
3139 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003140 else
3141 obj_priv->dirty = 1;
3142
3143 /*
3144 * Flush the current write domain if
3145 * the new read domains don't match. Invalidate
3146 * any read domains which differ from the old
3147 * write domain
3148 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003149 if (obj->write_domain &&
3150 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003151 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003152 invalidate_domains |=
3153 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003154 }
3155 /*
3156 * Invalidate any read caches which may have
3157 * stale data. That is, any new read domains.
3158 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003159 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003160 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003161 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003162
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003163 old_read_domains = obj->read_domains;
3164
Eric Anholtefbeed92009-02-19 14:54:51 -08003165 /* The actual obj->write_domain will be updated with
3166 * pending_write_domain after we emit the accumulated flush for all
3167 * of our domain changes in execbuffers (which clears objects'
3168 * write_domains). So if we have a current write domain that we
3169 * aren't changing, set pending_write_domain to that.
3170 */
3171 if (flush_domains == 0 && obj->pending_write_domain == 0)
3172 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003173 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003174
3175 dev->invalidate_domains |= invalidate_domains;
3176 dev->flush_domains |= flush_domains;
Chris Wilson92204342010-09-18 11:02:01 +01003177 if (obj_priv->ring)
3178 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003179
3180 trace_i915_gem_object_change_domain(obj,
3181 old_read_domains,
3182 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003183}
3184
3185/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003186 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003187 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003188 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3189 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3190 */
3191static void
3192i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3193{
Daniel Vetter23010e42010-03-08 13:35:02 +01003194 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003195
3196 if (!obj_priv->page_cpu_valid)
3197 return;
3198
3199 /* If we're partially in the CPU read domain, finish moving it in.
3200 */
3201 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3202 int i;
3203
3204 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3205 if (obj_priv->page_cpu_valid[i])
3206 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003207 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003208 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003209 }
3210
3211 /* Free the page_cpu_valid mappings which are now stale, whether
3212 * or not we've got I915_GEM_DOMAIN_CPU.
3213 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003214 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003215 obj_priv->page_cpu_valid = NULL;
3216}
3217
3218/**
3219 * Set the CPU read domain on a range of the object.
3220 *
3221 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3222 * not entirely valid. The page_cpu_valid member of the object flags which
3223 * pages have been flushed, and will be respected by
3224 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3225 * of the whole object.
3226 *
3227 * This function returns when the move is complete, including waiting on
3228 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003229 */
3230static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003231i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3232 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003233{
Daniel Vetter23010e42010-03-08 13:35:02 +01003234 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003235 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003236 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003237
Eric Anholte47c68e2008-11-14 13:35:19 -08003238 if (offset == 0 && size == obj->size)
3239 return i915_gem_object_set_to_cpu_domain(obj, 0);
3240
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003241 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003242 if (ret != 0)
3243 return ret;
3244 i915_gem_object_flush_gtt_write_domain(obj);
3245
3246 /* If we're already fully in the CPU read domain, we're done. */
3247 if (obj_priv->page_cpu_valid == NULL &&
3248 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003249 return 0;
3250
Eric Anholte47c68e2008-11-14 13:35:19 -08003251 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3252 * newly adding I915_GEM_DOMAIN_CPU
3253 */
Eric Anholt673a3942008-07-30 12:06:12 -07003254 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003255 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3256 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003257 if (obj_priv->page_cpu_valid == NULL)
3258 return -ENOMEM;
3259 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3260 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003261
3262 /* Flush the cache on any pages that are still invalid from the CPU's
3263 * perspective.
3264 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003265 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3266 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003267 if (obj_priv->page_cpu_valid[i])
3268 continue;
3269
Eric Anholt856fa192009-03-19 14:10:50 -07003270 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003271
3272 obj_priv->page_cpu_valid[i] = 1;
3273 }
3274
Eric Anholte47c68e2008-11-14 13:35:19 -08003275 /* It should now be out of any other write domains, and we can update
3276 * the domain values for our changes.
3277 */
3278 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3279
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003280 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003281 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3282
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003283 trace_i915_gem_object_change_domain(obj,
3284 old_read_domains,
3285 obj->write_domain);
3286
Eric Anholt673a3942008-07-30 12:06:12 -07003287 return 0;
3288}
3289
3290/**
Eric Anholt673a3942008-07-30 12:06:12 -07003291 * Pin an object to the GTT and evaluate the relocations landing in it.
3292 */
3293static int
3294i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3295 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003296 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003297 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003298{
3299 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003300 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003301 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003302 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003303 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003304 bool need_fence;
3305
3306 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3307 obj_priv->tiling_mode != I915_TILING_NONE;
3308
3309 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003310 if (need_fence &&
3311 !i915_gem_object_fence_offset_ok(obj,
3312 obj_priv->tiling_mode)) {
3313 ret = i915_gem_object_unbind(obj);
3314 if (ret)
3315 return ret;
3316 }
Eric Anholt673a3942008-07-30 12:06:12 -07003317
3318 /* Choose the GTT offset for our buffer and put it there. */
3319 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3320 if (ret)
3321 return ret;
3322
Jesse Barnes76446ca2009-12-17 22:05:42 -05003323 /*
3324 * Pre-965 chips need a fence register set up in order to
3325 * properly handle blits to/from tiled surfaces.
3326 */
3327 if (need_fence) {
Chris Wilson53640e12010-09-20 11:40:50 +01003328 ret = i915_gem_object_get_fence_reg(obj, true);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003329 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003330 i915_gem_object_unpin(obj);
3331 return ret;
3332 }
Chris Wilson53640e12010-09-20 11:40:50 +01003333
3334 dev_priv->fence_regs[obj_priv->fence_reg].gpu = true;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003335 }
3336
Eric Anholt673a3942008-07-30 12:06:12 -07003337 entry->offset = obj_priv->gtt_offset;
3338
Eric Anholt673a3942008-07-30 12:06:12 -07003339 /* Apply the relocations, using the GTT aperture to avoid cache
3340 * flushing requirements.
3341 */
3342 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003343 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003344 struct drm_gem_object *target_obj;
3345 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003346 uint32_t reloc_val, reloc_offset;
3347 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003348
Eric Anholt673a3942008-07-30 12:06:12 -07003349 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003350 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003351 if (target_obj == NULL) {
3352 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003353 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003354 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003355 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003356
Chris Wilson8542a0b2009-09-09 21:15:15 +01003357#if WATCH_RELOC
3358 DRM_INFO("%s: obj %p offset %08x target %d "
3359 "read %08x write %08x gtt %08x "
3360 "presumed %08x delta %08x\n",
3361 __func__,
3362 obj,
3363 (int) reloc->offset,
3364 (int) reloc->target_handle,
3365 (int) reloc->read_domains,
3366 (int) reloc->write_domain,
3367 (int) target_obj_priv->gtt_offset,
3368 (int) reloc->presumed_offset,
3369 reloc->delta);
3370#endif
3371
Eric Anholt673a3942008-07-30 12:06:12 -07003372 /* The target buffer should have appeared before us in the
3373 * exec_object list, so it should have a GTT space bound by now.
3374 */
3375 if (target_obj_priv->gtt_space == NULL) {
3376 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003377 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003378 drm_gem_object_unreference(target_obj);
3379 i915_gem_object_unpin(obj);
3380 return -EINVAL;
3381 }
3382
Chris Wilson8542a0b2009-09-09 21:15:15 +01003383 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003384 if (reloc->write_domain & (reloc->write_domain - 1)) {
3385 DRM_ERROR("reloc with multiple write domains: "
3386 "obj %p target %d offset %d "
3387 "read %08x write %08x",
3388 obj, reloc->target_handle,
3389 (int) reloc->offset,
3390 reloc->read_domains,
3391 reloc->write_domain);
Julia Lawall929f49b2010-10-02 15:59:17 +02003392 drm_gem_object_unreference(target_obj);
3393 i915_gem_object_unpin(obj);
Daniel Vetter16edd552010-02-19 11:52:02 +01003394 return -EINVAL;
3395 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003396 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3397 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3398 DRM_ERROR("reloc with read/write CPU domains: "
3399 "obj %p target %d offset %d "
3400 "read %08x write %08x",
3401 obj, reloc->target_handle,
3402 (int) reloc->offset,
3403 reloc->read_domains,
3404 reloc->write_domain);
3405 drm_gem_object_unreference(target_obj);
3406 i915_gem_object_unpin(obj);
3407 return -EINVAL;
3408 }
3409 if (reloc->write_domain && target_obj->pending_write_domain &&
3410 reloc->write_domain != target_obj->pending_write_domain) {
3411 DRM_ERROR("Write domain conflict: "
3412 "obj %p target %d offset %d "
3413 "new %08x old %08x\n",
3414 obj, reloc->target_handle,
3415 (int) reloc->offset,
3416 reloc->write_domain,
3417 target_obj->pending_write_domain);
3418 drm_gem_object_unreference(target_obj);
3419 i915_gem_object_unpin(obj);
3420 return -EINVAL;
3421 }
3422
3423 target_obj->pending_read_domains |= reloc->read_domains;
3424 target_obj->pending_write_domain |= reloc->write_domain;
3425
3426 /* If the relocation already has the right value in it, no
3427 * more work needs to be done.
3428 */
3429 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3430 drm_gem_object_unreference(target_obj);
3431 continue;
3432 }
3433
3434 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003435 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003436 DRM_ERROR("Relocation beyond object bounds: "
3437 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003438 obj, reloc->target_handle,
3439 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003440 drm_gem_object_unreference(target_obj);
3441 i915_gem_object_unpin(obj);
3442 return -EINVAL;
3443 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003444 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003445 DRM_ERROR("Relocation not 4-byte aligned: "
3446 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003447 obj, reloc->target_handle,
3448 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003449 drm_gem_object_unreference(target_obj);
3450 i915_gem_object_unpin(obj);
3451 return -EINVAL;
3452 }
3453
Chris Wilson8542a0b2009-09-09 21:15:15 +01003454 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003455 if (reloc->delta >= target_obj->size) {
3456 DRM_ERROR("Relocation beyond target object bounds: "
3457 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003458 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003459 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003460 drm_gem_object_unreference(target_obj);
3461 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003462 return -EINVAL;
3463 }
3464
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003465 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3466 if (ret != 0) {
3467 drm_gem_object_unreference(target_obj);
3468 i915_gem_object_unpin(obj);
Chris Wilson1cdf7fef2010-10-02 15:12:41 +01003469 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003470 }
3471
3472 /* Map the page containing the relocation we're going to
3473 * perform.
3474 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003475 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003476 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3477 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003478 ~(PAGE_SIZE - 1)),
3479 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003480 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003481 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003482 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003483
Eric Anholt673a3942008-07-30 12:06:12 -07003484 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003485 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003486
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003487 /* The updated presumed offset for this entry will be
3488 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003489 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003490 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003491
3492 drm_gem_object_unreference(target_obj);
3493 }
3494
Eric Anholt673a3942008-07-30 12:06:12 -07003495 return 0;
3496}
3497
Eric Anholt673a3942008-07-30 12:06:12 -07003498/* Throttle our rendering by waiting until the ring has completed our requests
3499 * emitted over 20 msec ago.
3500 *
Eric Anholtb9624422009-06-03 07:27:35 +00003501 * Note that if we were to use the current jiffies each time around the loop,
3502 * we wouldn't escape the function with any frames outstanding if the time to
3503 * render a frame was over 20ms.
3504 *
Eric Anholt673a3942008-07-30 12:06:12 -07003505 * This should get us reasonable parallelism between CPU and GPU but also
3506 * relatively low latency when blocking on a particular request to finish.
3507 */
3508static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003509i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003510{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003511 struct drm_i915_private *dev_priv = dev->dev_private;
3512 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003513 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003514 struct drm_i915_gem_request *request;
3515 struct intel_ring_buffer *ring = NULL;
3516 u32 seqno = 0;
3517 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003518
Chris Wilson1c255952010-09-26 11:03:27 +01003519 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003520 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003521 if (time_after_eq(request->emitted_jiffies, recent_enough))
3522 break;
3523
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003524 ring = request->ring;
3525 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003526 }
Chris Wilson1c255952010-09-26 11:03:27 +01003527 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003528
3529 if (seqno == 0)
3530 return 0;
3531
3532 ret = 0;
3533 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3534 /* And wait for the seqno passing without holding any locks and
3535 * causing extra latency for others. This is safe as the irq
3536 * generation is designed to be run atomically and so is
3537 * lockless.
3538 */
3539 ring->user_irq_get(dev, ring);
3540 ret = wait_event_interruptible(ring->irq_queue,
3541 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3542 || atomic_read(&dev_priv->mm.wedged));
3543 ring->user_irq_put(dev, ring);
3544
3545 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3546 ret = -EIO;
3547 }
3548
3549 if (ret == 0)
3550 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003551
Eric Anholt673a3942008-07-30 12:06:12 -07003552 return ret;
3553}
3554
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003555static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003556i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003557 uint32_t buffer_count,
3558 struct drm_i915_gem_relocation_entry **relocs)
3559{
3560 uint32_t reloc_count = 0, reloc_index = 0, i;
3561 int ret;
3562
3563 *relocs = NULL;
3564 for (i = 0; i < buffer_count; i++) {
3565 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3566 return -EINVAL;
3567 reloc_count += exec_list[i].relocation_count;
3568 }
3569
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003570 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003571 if (*relocs == NULL) {
3572 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003573 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003574 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003575
3576 for (i = 0; i < buffer_count; i++) {
3577 struct drm_i915_gem_relocation_entry __user *user_relocs;
3578
3579 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3580
3581 ret = copy_from_user(&(*relocs)[reloc_index],
3582 user_relocs,
3583 exec_list[i].relocation_count *
3584 sizeof(**relocs));
3585 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003586 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003587 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003588 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003589 }
3590
3591 reloc_index += exec_list[i].relocation_count;
3592 }
3593
Florian Mickler2bc43b52009-04-06 22:55:41 +02003594 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003595}
3596
3597static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003598i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003599 uint32_t buffer_count,
3600 struct drm_i915_gem_relocation_entry *relocs)
3601{
3602 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003603 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003604
Chris Wilson93533c22010-01-31 10:40:48 +00003605 if (relocs == NULL)
3606 return 0;
3607
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003608 for (i = 0; i < buffer_count; i++) {
3609 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003610 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003611
3612 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3613
Florian Mickler2bc43b52009-04-06 22:55:41 +02003614 unwritten = copy_to_user(user_relocs,
3615 &relocs[reloc_count],
3616 exec_list[i].relocation_count *
3617 sizeof(*relocs));
3618
3619 if (unwritten) {
3620 ret = -EFAULT;
3621 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003622 }
3623
3624 reloc_count += exec_list[i].relocation_count;
3625 }
3626
Florian Mickler2bc43b52009-04-06 22:55:41 +02003627err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003628 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003629
3630 return ret;
3631}
3632
Chris Wilson83d60792009-06-06 09:45:57 +01003633static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003634i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003635 uint64_t exec_offset)
3636{
3637 uint32_t exec_start, exec_len;
3638
3639 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3640 exec_len = (uint32_t) exec->batch_len;
3641
3642 if ((exec_start | exec_len) & 0x7)
3643 return -EINVAL;
3644
3645 if (!exec_start)
3646 return -EINVAL;
3647
3648 return 0;
3649}
3650
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003651static int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003652i915_gem_wait_for_pending_flip(struct drm_device *dev,
3653 struct drm_gem_object **object_list,
3654 int count)
3655{
3656 drm_i915_private_t *dev_priv = dev->dev_private;
3657 struct drm_i915_gem_object *obj_priv;
3658 DEFINE_WAIT(wait);
3659 int i, ret = 0;
3660
3661 for (;;) {
3662 prepare_to_wait(&dev_priv->pending_flip_queue,
3663 &wait, TASK_INTERRUPTIBLE);
3664 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003665 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003666 if (atomic_read(&obj_priv->pending_flip) > 0)
3667 break;
3668 }
3669 if (i == count)
3670 break;
3671
3672 if (!signal_pending(current)) {
3673 mutex_unlock(&dev->struct_mutex);
3674 schedule();
3675 mutex_lock(&dev->struct_mutex);
3676 continue;
3677 }
3678 ret = -ERESTARTSYS;
3679 break;
3680 }
3681 finish_wait(&dev_priv->pending_flip_queue, &wait);
3682
3683 return ret;
3684}
3685
Chris Wilson8dc5d142010-08-12 12:36:12 +01003686static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003687i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3688 struct drm_file *file_priv,
3689 struct drm_i915_gem_execbuffer2 *args,
3690 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003691{
3692 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003693 struct drm_gem_object **object_list = NULL;
3694 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003695 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003696 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003697 struct drm_i915_gem_relocation_entry *relocs = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003698 struct drm_i915_gem_request *request = NULL;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003699 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003700 uint64_t exec_offset;
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003701 uint32_t reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003702 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003703
Zou Nan hai852835f2010-05-21 09:08:56 +08003704 struct intel_ring_buffer *ring = NULL;
3705
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003706 ret = i915_gem_check_is_wedged(dev);
3707 if (ret)
3708 return ret;
3709
Eric Anholt673a3942008-07-30 12:06:12 -07003710#if WATCH_EXEC
3711 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3712 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3713#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003714 if (args->flags & I915_EXEC_BSD) {
3715 if (!HAS_BSD(dev)) {
3716 DRM_ERROR("execbuf with wrong flag\n");
3717 return -EINVAL;
3718 }
3719 ring = &dev_priv->bsd_ring;
3720 } else {
3721 ring = &dev_priv->render_ring;
3722 }
3723
Eric Anholt4f481ed2008-09-10 14:22:49 -07003724 if (args->buffer_count < 1) {
3725 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3726 return -EINVAL;
3727 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003728 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003729 if (object_list == NULL) {
3730 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003731 args->buffer_count);
3732 ret = -ENOMEM;
3733 goto pre_mutex_err;
3734 }
Eric Anholt673a3942008-07-30 12:06:12 -07003735
Eric Anholt201361a2009-03-11 12:30:04 -07003736 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003737 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3738 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003739 if (cliprects == NULL) {
3740 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003741 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003742 }
Eric Anholt201361a2009-03-11 12:30:04 -07003743
3744 ret = copy_from_user(cliprects,
3745 (struct drm_clip_rect __user *)
3746 (uintptr_t) args->cliprects_ptr,
3747 sizeof(*cliprects) * args->num_cliprects);
3748 if (ret != 0) {
3749 DRM_ERROR("copy %d cliprects failed: %d\n",
3750 args->num_cliprects, ret);
Dan Carpenterc877cdce2010-06-23 19:03:01 +02003751 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003752 goto pre_mutex_err;
3753 }
3754 }
3755
Chris Wilson8dc5d142010-08-12 12:36:12 +01003756 request = kzalloc(sizeof(*request), GFP_KERNEL);
3757 if (request == NULL) {
3758 ret = -ENOMEM;
3759 goto pre_mutex_err;
3760 }
3761
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003762 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3763 &relocs);
3764 if (ret != 0)
3765 goto pre_mutex_err;
3766
Chris Wilson76c1dec2010-09-25 11:22:51 +01003767 ret = i915_mutex_lock_interruptible(dev);
3768 if (ret)
3769 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003770
Eric Anholt673a3942008-07-30 12:06:12 -07003771 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003772 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003773 ret = -EBUSY;
3774 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003775 }
3776
Keith Packardac94a962008-11-20 23:30:27 -08003777 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003778 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003779 for (i = 0; i < args->buffer_count; i++) {
3780 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3781 exec_list[i].handle);
3782 if (object_list[i] == NULL) {
3783 DRM_ERROR("Invalid object handle %d at index %d\n",
3784 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003785 /* prevent error path from reading uninitialized data */
3786 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003787 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003788 goto err;
3789 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003790
Daniel Vetter23010e42010-03-08 13:35:02 +01003791 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003792 if (obj_priv->in_execbuffer) {
3793 DRM_ERROR("Object %p appears more than once in object list\n",
3794 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003795 /* prevent error path from reading uninitialized data */
3796 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003797 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003798 goto err;
3799 }
3800 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003801 flips += atomic_read(&obj_priv->pending_flip);
3802 }
3803
3804 if (flips > 0) {
3805 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3806 args->buffer_count);
3807 if (ret)
3808 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003809 }
Eric Anholt673a3942008-07-30 12:06:12 -07003810
Keith Packardac94a962008-11-20 23:30:27 -08003811 /* Pin and relocate */
3812 for (pin_tries = 0; ; pin_tries++) {
3813 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003814 reloc_index = 0;
3815
Keith Packardac94a962008-11-20 23:30:27 -08003816 for (i = 0; i < args->buffer_count; i++) {
3817 object_list[i]->pending_read_domains = 0;
3818 object_list[i]->pending_write_domain = 0;
3819 ret = i915_gem_object_pin_and_relocate(object_list[i],
3820 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003821 &exec_list[i],
3822 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003823 if (ret)
3824 break;
3825 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003826 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003827 }
3828 /* success */
3829 if (ret == 0)
3830 break;
3831
3832 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003833 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003834 if (ret != -ERESTARTSYS) {
3835 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003836 int num_fences = 0;
3837 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003838 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003839
Chris Wilson07f73f62009-09-14 16:50:30 +01003840 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003841 num_fences +=
3842 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3843 obj_priv->tiling_mode != I915_TILING_NONE;
3844 }
3845 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003846 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003847 total_size, num_fences,
3848 ret);
Chris Wilson73aa8082010-09-30 11:46:12 +01003849 DRM_ERROR("%u objects [%u pinned, %u GTT], "
3850 "%zu object bytes [%zu pinned], "
3851 "%zu /%zu gtt bytes\n",
3852 dev_priv->mm.object_count,
3853 dev_priv->mm.pin_count,
3854 dev_priv->mm.gtt_count,
3855 dev_priv->mm.object_memory,
3856 dev_priv->mm.pin_memory,
3857 dev_priv->mm.gtt_memory,
3858 dev_priv->mm.gtt_total);
Chris Wilson07f73f62009-09-14 16:50:30 +01003859 }
Eric Anholt673a3942008-07-30 12:06:12 -07003860 goto err;
3861 }
Keith Packardac94a962008-11-20 23:30:27 -08003862
3863 /* unpin all of our buffers */
3864 for (i = 0; i < pinned; i++)
3865 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003866 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003867
3868 /* evict everyone we can from the aperture */
3869 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003870 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003871 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003872 }
3873
3874 /* Set the pending read domains for the batch buffer to COMMAND */
3875 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003876 if (batch_obj->pending_write_domain) {
3877 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3878 ret = -EINVAL;
3879 goto err;
3880 }
3881 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003882
Chris Wilson83d60792009-06-06 09:45:57 +01003883 /* Sanity check the batch buffer, prior to moving objects */
3884 exec_offset = exec_list[args->buffer_count - 1].offset;
3885 ret = i915_gem_check_execbuffer (args, exec_offset);
3886 if (ret != 0) {
3887 DRM_ERROR("execbuf with invalid offset/length\n");
3888 goto err;
3889 }
3890
Keith Packard646f0f62008-11-20 23:23:03 -08003891 /* Zero the global flush/invalidate flags. These
3892 * will be modified as new domains are computed
3893 * for each object
3894 */
3895 dev->invalidate_domains = 0;
3896 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003897 dev_priv->mm.flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003898
Eric Anholt673a3942008-07-30 12:06:12 -07003899 for (i = 0; i < args->buffer_count; i++) {
3900 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003901
Keith Packard646f0f62008-11-20 23:23:03 -08003902 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003903 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003904 }
3905
Keith Packard646f0f62008-11-20 23:23:03 -08003906 if (dev->invalidate_domains | dev->flush_domains) {
3907#if WATCH_EXEC
3908 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3909 __func__,
3910 dev->invalidate_domains,
3911 dev->flush_domains);
3912#endif
Chris Wilsonc78ec302010-09-20 12:50:23 +01003913 i915_gem_flush(dev, file_priv,
Keith Packard646f0f62008-11-20 23:23:03 -08003914 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003915 dev->flush_domains,
3916 dev_priv->mm.flush_rings);
Daniel Vettera6910432010-02-02 17:08:37 +01003917 }
3918
Eric Anholtefbeed92009-02-19 14:54:51 -08003919 for (i = 0; i < args->buffer_count; i++) {
3920 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003921 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003922 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003923
3924 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003925 if (obj->write_domain)
3926 list_move_tail(&obj_priv->gpu_write_list,
3927 &dev_priv->mm.gpu_write_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01003928
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003929 trace_i915_gem_object_change_domain(obj,
3930 obj->read_domains,
3931 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003932 }
3933
Eric Anholt673a3942008-07-30 12:06:12 -07003934#if WATCH_COHERENCY
3935 for (i = 0; i < args->buffer_count; i++) {
3936 i915_gem_object_check_coherency(object_list[i],
3937 exec_list[i].handle);
3938 }
3939#endif
3940
Eric Anholt673a3942008-07-30 12:06:12 -07003941#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003942 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003943 args->batch_len,
3944 __func__,
3945 ~0);
3946#endif
3947
Eric Anholt673a3942008-07-30 12:06:12 -07003948 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003949 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3950 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003951 if (ret) {
3952 DRM_ERROR("dispatch failed %d\n", ret);
3953 goto err;
3954 }
3955
3956 /*
3957 * Ensure that the commands in the batch buffer are
3958 * finished before the interrupt fires
3959 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003960 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003961
Daniel Vetter617dbe22010-02-11 22:16:02 +01003962 for (i = 0; i < args->buffer_count; i++) {
3963 struct drm_gem_object *obj = object_list[i];
3964 obj_priv = to_intel_bo(obj);
3965
3966 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01003967 }
Chris Wilsona56ba562010-09-28 10:07:56 +01003968
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003969 i915_add_request(dev, file_priv, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003970 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003971
Eric Anholt673a3942008-07-30 12:06:12 -07003972err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003973 for (i = 0; i < pinned; i++)
3974 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003975
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003976 for (i = 0; i < args->buffer_count; i++) {
3977 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003978 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003979 obj_priv->in_execbuffer = false;
3980 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003981 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003982 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003983
Eric Anholt673a3942008-07-30 12:06:12 -07003984 mutex_unlock(&dev->struct_mutex);
3985
Chris Wilson93533c22010-01-31 10:40:48 +00003986pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003987 /* Copy the updated relocations out regardless of current error
3988 * state. Failure to update the relocs would mean that the next
3989 * time userland calls execbuf, it would do so with presumed offset
3990 * state that didn't match the actual object state.
3991 */
3992 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3993 relocs);
3994 if (ret2 != 0) {
3995 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3996
3997 if (ret == 0)
3998 ret = ret2;
3999 }
4000
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004001 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004002 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004003 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07004004
4005 return ret;
4006}
4007
Jesse Barnes76446ca2009-12-17 22:05:42 -05004008/*
4009 * Legacy execbuffer just creates an exec2 list from the original exec object
4010 * list array and passes it to the real function.
4011 */
4012int
4013i915_gem_execbuffer(struct drm_device *dev, void *data,
4014 struct drm_file *file_priv)
4015{
4016 struct drm_i915_gem_execbuffer *args = data;
4017 struct drm_i915_gem_execbuffer2 exec2;
4018 struct drm_i915_gem_exec_object *exec_list = NULL;
4019 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4020 int ret, i;
4021
4022#if WATCH_EXEC
4023 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4024 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4025#endif
4026
4027 if (args->buffer_count < 1) {
4028 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4029 return -EINVAL;
4030 }
4031
4032 /* Copy in the exec list from userland */
4033 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4034 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4035 if (exec_list == NULL || exec2_list == NULL) {
4036 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4037 args->buffer_count);
4038 drm_free_large(exec_list);
4039 drm_free_large(exec2_list);
4040 return -ENOMEM;
4041 }
4042 ret = copy_from_user(exec_list,
4043 (struct drm_i915_relocation_entry __user *)
4044 (uintptr_t) args->buffers_ptr,
4045 sizeof(*exec_list) * args->buffer_count);
4046 if (ret != 0) {
4047 DRM_ERROR("copy %d exec entries failed %d\n",
4048 args->buffer_count, ret);
4049 drm_free_large(exec_list);
4050 drm_free_large(exec2_list);
4051 return -EFAULT;
4052 }
4053
4054 for (i = 0; i < args->buffer_count; i++) {
4055 exec2_list[i].handle = exec_list[i].handle;
4056 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4057 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4058 exec2_list[i].alignment = exec_list[i].alignment;
4059 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004060 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004061 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4062 else
4063 exec2_list[i].flags = 0;
4064 }
4065
4066 exec2.buffers_ptr = args->buffers_ptr;
4067 exec2.buffer_count = args->buffer_count;
4068 exec2.batch_start_offset = args->batch_start_offset;
4069 exec2.batch_len = args->batch_len;
4070 exec2.DR1 = args->DR1;
4071 exec2.DR4 = args->DR4;
4072 exec2.num_cliprects = args->num_cliprects;
4073 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004074 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004075
4076 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4077 if (!ret) {
4078 /* Copy the new buffer offsets back to the user's exec list. */
4079 for (i = 0; i < args->buffer_count; i++)
4080 exec_list[i].offset = exec2_list[i].offset;
4081 /* ... and back out to userspace */
4082 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4083 (uintptr_t) args->buffers_ptr,
4084 exec_list,
4085 sizeof(*exec_list) * args->buffer_count);
4086 if (ret) {
4087 ret = -EFAULT;
4088 DRM_ERROR("failed to copy %d exec entries "
4089 "back to user (%d)\n",
4090 args->buffer_count, ret);
4091 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004092 }
4093
4094 drm_free_large(exec_list);
4095 drm_free_large(exec2_list);
4096 return ret;
4097}
4098
4099int
4100i915_gem_execbuffer2(struct drm_device *dev, void *data,
4101 struct drm_file *file_priv)
4102{
4103 struct drm_i915_gem_execbuffer2 *args = data;
4104 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4105 int ret;
4106
4107#if WATCH_EXEC
4108 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4109 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4110#endif
4111
4112 if (args->buffer_count < 1) {
4113 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4114 return -EINVAL;
4115 }
4116
4117 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4118 if (exec2_list == NULL) {
4119 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4120 args->buffer_count);
4121 return -ENOMEM;
4122 }
4123 ret = copy_from_user(exec2_list,
4124 (struct drm_i915_relocation_entry __user *)
4125 (uintptr_t) args->buffers_ptr,
4126 sizeof(*exec2_list) * args->buffer_count);
4127 if (ret != 0) {
4128 DRM_ERROR("copy %d exec entries failed %d\n",
4129 args->buffer_count, ret);
4130 drm_free_large(exec2_list);
4131 return -EFAULT;
4132 }
4133
4134 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4135 if (!ret) {
4136 /* Copy the new buffer offsets back to the user's exec list. */
4137 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4138 (uintptr_t) args->buffers_ptr,
4139 exec2_list,
4140 sizeof(*exec2_list) * args->buffer_count);
4141 if (ret) {
4142 ret = -EFAULT;
4143 DRM_ERROR("failed to copy %d exec entries "
4144 "back to user (%d)\n",
4145 args->buffer_count, ret);
4146 }
4147 }
4148
4149 drm_free_large(exec2_list);
4150 return ret;
4151}
4152
Eric Anholt673a3942008-07-30 12:06:12 -07004153int
4154i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4155{
4156 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004157 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004158 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004159 int ret;
4160
Daniel Vetter778c3542010-05-13 11:49:44 +02004161 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004162 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004163
4164 if (obj_priv->gtt_space != NULL) {
4165 if (alignment == 0)
4166 alignment = i915_gem_get_gtt_alignment(obj);
4167 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004168 WARN(obj_priv->pin_count,
4169 "bo is already pinned with incorrect alignment:"
4170 " offset=%x, req.alignment=%x\n",
4171 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004172 ret = i915_gem_object_unbind(obj);
4173 if (ret)
4174 return ret;
4175 }
4176 }
4177
Eric Anholt673a3942008-07-30 12:06:12 -07004178 if (obj_priv->gtt_space == NULL) {
4179 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004180 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004181 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004182 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004183
Eric Anholt673a3942008-07-30 12:06:12 -07004184 obj_priv->pin_count++;
4185
4186 /* If the object is not active and not pending a flush,
4187 * remove it from the inactive list
4188 */
4189 if (obj_priv->pin_count == 1) {
Chris Wilson73aa8082010-09-30 11:46:12 +01004190 i915_gem_info_add_pin(dev_priv, obj->size);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004191 if (!obj_priv->active)
4192 list_move_tail(&obj_priv->list,
4193 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004194 }
Eric Anholt673a3942008-07-30 12:06:12 -07004195
Chris Wilson23bc5982010-09-29 16:10:57 +01004196 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004197 return 0;
4198}
4199
4200void
4201i915_gem_object_unpin(struct drm_gem_object *obj)
4202{
4203 struct drm_device *dev = obj->dev;
4204 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004205 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004206
Chris Wilson23bc5982010-09-29 16:10:57 +01004207 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004208 obj_priv->pin_count--;
4209 BUG_ON(obj_priv->pin_count < 0);
4210 BUG_ON(obj_priv->gtt_space == NULL);
4211
4212 /* If the object is no longer pinned, and is
4213 * neither active nor being flushed, then stick it on
4214 * the inactive list
4215 */
4216 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004217 if (!obj_priv->active)
Eric Anholt673a3942008-07-30 12:06:12 -07004218 list_move_tail(&obj_priv->list,
4219 &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01004220 i915_gem_info_remove_pin(dev_priv, obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07004221 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004222 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004223}
4224
4225int
4226i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4227 struct drm_file *file_priv)
4228{
4229 struct drm_i915_gem_pin *args = data;
4230 struct drm_gem_object *obj;
4231 struct drm_i915_gem_object *obj_priv;
4232 int ret;
4233
Eric Anholt673a3942008-07-30 12:06:12 -07004234 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4235 if (obj == NULL) {
4236 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4237 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004238 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004239 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004240 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004241
Chris Wilson76c1dec2010-09-25 11:22:51 +01004242 ret = i915_mutex_lock_interruptible(dev);
4243 if (ret) {
4244 drm_gem_object_unreference_unlocked(obj);
4245 return ret;
4246 }
4247
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004248 if (obj_priv->madv != I915_MADV_WILLNEED) {
4249 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004250 drm_gem_object_unreference(obj);
4251 mutex_unlock(&dev->struct_mutex);
4252 return -EINVAL;
4253 }
4254
Jesse Barnes79e53942008-11-07 14:24:08 -08004255 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4256 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4257 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004258 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004259 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004260 return -EINVAL;
4261 }
4262
4263 obj_priv->user_pin_count++;
4264 obj_priv->pin_filp = file_priv;
4265 if (obj_priv->user_pin_count == 1) {
4266 ret = i915_gem_object_pin(obj, args->alignment);
4267 if (ret != 0) {
4268 drm_gem_object_unreference(obj);
4269 mutex_unlock(&dev->struct_mutex);
4270 return ret;
4271 }
Eric Anholt673a3942008-07-30 12:06:12 -07004272 }
4273
4274 /* XXX - flush the CPU caches for pinned objects
4275 * as the X server doesn't manage domains yet
4276 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004277 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004278 args->offset = obj_priv->gtt_offset;
4279 drm_gem_object_unreference(obj);
4280 mutex_unlock(&dev->struct_mutex);
4281
4282 return 0;
4283}
4284
4285int
4286i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4287 struct drm_file *file_priv)
4288{
4289 struct drm_i915_gem_pin *args = data;
4290 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004291 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004292 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004293
4294 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4295 if (obj == NULL) {
4296 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4297 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004298 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004299 }
4300
Daniel Vetter23010e42010-03-08 13:35:02 +01004301 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004302
4303 ret = i915_mutex_lock_interruptible(dev);
4304 if (ret) {
4305 drm_gem_object_unreference_unlocked(obj);
4306 return ret;
4307 }
4308
Jesse Barnes79e53942008-11-07 14:24:08 -08004309 if (obj_priv->pin_filp != file_priv) {
4310 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4311 args->handle);
4312 drm_gem_object_unreference(obj);
4313 mutex_unlock(&dev->struct_mutex);
4314 return -EINVAL;
4315 }
4316 obj_priv->user_pin_count--;
4317 if (obj_priv->user_pin_count == 0) {
4318 obj_priv->pin_filp = NULL;
4319 i915_gem_object_unpin(obj);
4320 }
Eric Anholt673a3942008-07-30 12:06:12 -07004321
4322 drm_gem_object_unreference(obj);
4323 mutex_unlock(&dev->struct_mutex);
4324 return 0;
4325}
4326
4327int
4328i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4329 struct drm_file *file_priv)
4330{
4331 struct drm_i915_gem_busy *args = data;
4332 struct drm_gem_object *obj;
4333 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004334 int ret;
4335
Eric Anholt673a3942008-07-30 12:06:12 -07004336 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4337 if (obj == NULL) {
4338 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4339 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004340 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004341 }
4342
Chris Wilson76c1dec2010-09-25 11:22:51 +01004343 ret = i915_mutex_lock_interruptible(dev);
4344 if (ret) {
4345 drm_gem_object_unreference_unlocked(obj);
4346 return ret;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004347 }
4348
Chris Wilson0be555b2010-08-04 15:36:30 +01004349 /* Count all active objects as busy, even if they are currently not used
4350 * by the gpu. Users of this interface expect objects to eventually
4351 * become non-busy without any further actions, therefore emit any
4352 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004353 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004354 obj_priv = to_intel_bo(obj);
4355 args->busy = obj_priv->active;
4356 if (args->busy) {
4357 /* Unconditionally flush objects, even when the gpu still uses this
4358 * object. Userspace calling this function indicates that it wants to
4359 * use this buffer rather sooner than later, so issuing the required
4360 * flush earlier is beneficial.
4361 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004362 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4363 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004364 obj_priv->ring,
4365 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004366
4367 /* Update the active list for the hardware's current position.
4368 * Otherwise this only updates on a delayed timer or when irqs
4369 * are actually unmasked, and our working set ends up being
4370 * larger than required.
4371 */
4372 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4373
4374 args->busy = obj_priv->active;
4375 }
Eric Anholt673a3942008-07-30 12:06:12 -07004376
4377 drm_gem_object_unreference(obj);
4378 mutex_unlock(&dev->struct_mutex);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004379 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004380}
4381
4382int
4383i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4384 struct drm_file *file_priv)
4385{
4386 return i915_gem_ring_throttle(dev, file_priv);
4387}
4388
Chris Wilson3ef94da2009-09-14 16:50:29 +01004389int
4390i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4391 struct drm_file *file_priv)
4392{
4393 struct drm_i915_gem_madvise *args = data;
4394 struct drm_gem_object *obj;
4395 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004396 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004397
4398 switch (args->madv) {
4399 case I915_MADV_DONTNEED:
4400 case I915_MADV_WILLNEED:
4401 break;
4402 default:
4403 return -EINVAL;
4404 }
4405
4406 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4407 if (obj == NULL) {
4408 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4409 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004410 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004411 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004412 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004413
Chris Wilson76c1dec2010-09-25 11:22:51 +01004414 ret = i915_mutex_lock_interruptible(dev);
4415 if (ret) {
4416 drm_gem_object_unreference_unlocked(obj);
4417 return ret;
4418 }
4419
Chris Wilson3ef94da2009-09-14 16:50:29 +01004420 if (obj_priv->pin_count) {
4421 drm_gem_object_unreference(obj);
4422 mutex_unlock(&dev->struct_mutex);
4423
4424 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4425 return -EINVAL;
4426 }
4427
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004428 if (obj_priv->madv != __I915_MADV_PURGED)
4429 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004430
Chris Wilson2d7ef392009-09-20 23:13:10 +01004431 /* if the object is no longer bound, discard its backing storage */
4432 if (i915_gem_object_is_purgeable(obj_priv) &&
4433 obj_priv->gtt_space == NULL)
4434 i915_gem_object_truncate(obj);
4435
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004436 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4437
Chris Wilson3ef94da2009-09-14 16:50:29 +01004438 drm_gem_object_unreference(obj);
4439 mutex_unlock(&dev->struct_mutex);
4440
4441 return 0;
4442}
4443
Daniel Vetterac52bc52010-04-09 19:05:06 +00004444struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4445 size_t size)
4446{
Chris Wilson73aa8082010-09-30 11:46:12 +01004447 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004448 struct drm_i915_gem_object *obj;
4449
4450 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4451 if (obj == NULL)
4452 return NULL;
4453
4454 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4455 kfree(obj);
4456 return NULL;
4457 }
4458
Chris Wilson73aa8082010-09-30 11:46:12 +01004459 i915_gem_info_add_obj(dev_priv, size);
4460
Daniel Vetterc397b902010-04-09 19:05:07 +00004461 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4462 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4463
4464 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004465 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004466 obj->fence_reg = I915_FENCE_REG_NONE;
4467 INIT_LIST_HEAD(&obj->list);
4468 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004469 obj->madv = I915_MADV_WILLNEED;
4470
4471 trace_i915_gem_object_create(&obj->base);
4472
4473 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004474}
4475
Eric Anholt673a3942008-07-30 12:06:12 -07004476int i915_gem_init_object(struct drm_gem_object *obj)
4477{
Daniel Vetterc397b902010-04-09 19:05:07 +00004478 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004479
Eric Anholt673a3942008-07-30 12:06:12 -07004480 return 0;
4481}
4482
Chris Wilsonbe726152010-07-23 23:18:50 +01004483static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4484{
4485 struct drm_device *dev = obj->dev;
4486 drm_i915_private_t *dev_priv = dev->dev_private;
4487 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4488 int ret;
4489
4490 ret = i915_gem_object_unbind(obj);
4491 if (ret == -ERESTARTSYS) {
4492 list_move(&obj_priv->list,
4493 &dev_priv->mm.deferred_free_list);
4494 return;
4495 }
4496
4497 if (obj_priv->mmap_offset)
4498 i915_gem_free_mmap_offset(obj);
4499
4500 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004501 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004502
4503 kfree(obj_priv->page_cpu_valid);
4504 kfree(obj_priv->bit_17);
4505 kfree(obj_priv);
4506}
4507
Eric Anholt673a3942008-07-30 12:06:12 -07004508void i915_gem_free_object(struct drm_gem_object *obj)
4509{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004510 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004511 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004512
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004513 trace_i915_gem_object_destroy(obj);
4514
Eric Anholt673a3942008-07-30 12:06:12 -07004515 while (obj_priv->pin_count > 0)
4516 i915_gem_object_unpin(obj);
4517
Dave Airlie71acb5e2008-12-30 20:31:46 +10004518 if (obj_priv->phys_obj)
4519 i915_gem_detach_phys_object(dev, obj);
4520
Chris Wilsonbe726152010-07-23 23:18:50 +01004521 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004522}
4523
Jesse Barnes5669fca2009-02-17 15:13:31 -08004524int
Eric Anholt673a3942008-07-30 12:06:12 -07004525i915_gem_idle(struct drm_device *dev)
4526{
4527 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004528 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004529
Keith Packard6dbe2772008-10-14 21:41:13 -07004530 mutex_lock(&dev->struct_mutex);
4531
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004532 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004533 (dev_priv->render_ring.gem_object == NULL) ||
4534 (HAS_BSD(dev) &&
4535 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004536 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004537 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004538 }
Eric Anholt673a3942008-07-30 12:06:12 -07004539
Chris Wilson29105cc2010-01-07 10:39:13 +00004540 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004541 if (ret) {
4542 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004543 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004544 }
Eric Anholt673a3942008-07-30 12:06:12 -07004545
Chris Wilson29105cc2010-01-07 10:39:13 +00004546 /* Under UMS, be paranoid and evict. */
4547 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004548 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004549 if (ret) {
4550 mutex_unlock(&dev->struct_mutex);
4551 return ret;
4552 }
4553 }
4554
4555 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4556 * We need to replace this with a semaphore, or something.
4557 * And not confound mm.suspended!
4558 */
4559 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004560 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004561
4562 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004563 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004564
Keith Packard6dbe2772008-10-14 21:41:13 -07004565 mutex_unlock(&dev->struct_mutex);
4566
Chris Wilson29105cc2010-01-07 10:39:13 +00004567 /* Cancel the retire work handler, which should be idle now. */
4568 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4569
Eric Anholt673a3942008-07-30 12:06:12 -07004570 return 0;
4571}
4572
Jesse Barnese552eb72010-04-21 11:39:23 -07004573/*
4574 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4575 * over cache flushing.
4576 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004577static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004578i915_gem_init_pipe_control(struct drm_device *dev)
4579{
4580 drm_i915_private_t *dev_priv = dev->dev_private;
4581 struct drm_gem_object *obj;
4582 struct drm_i915_gem_object *obj_priv;
4583 int ret;
4584
Eric Anholt34dc4d42010-05-07 14:30:03 -07004585 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004586 if (obj == NULL) {
4587 DRM_ERROR("Failed to allocate seqno page\n");
4588 ret = -ENOMEM;
4589 goto err;
4590 }
4591 obj_priv = to_intel_bo(obj);
4592 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4593
4594 ret = i915_gem_object_pin(obj, 4096);
4595 if (ret)
4596 goto err_unref;
4597
4598 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4599 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4600 if (dev_priv->seqno_page == NULL)
4601 goto err_unpin;
4602
4603 dev_priv->seqno_obj = obj;
4604 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4605
4606 return 0;
4607
4608err_unpin:
4609 i915_gem_object_unpin(obj);
4610err_unref:
4611 drm_gem_object_unreference(obj);
4612err:
4613 return ret;
4614}
4615
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004616
4617static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004618i915_gem_cleanup_pipe_control(struct drm_device *dev)
4619{
4620 drm_i915_private_t *dev_priv = dev->dev_private;
4621 struct drm_gem_object *obj;
4622 struct drm_i915_gem_object *obj_priv;
4623
4624 obj = dev_priv->seqno_obj;
4625 obj_priv = to_intel_bo(obj);
4626 kunmap(obj_priv->pages[0]);
4627 i915_gem_object_unpin(obj);
4628 drm_gem_object_unreference(obj);
4629 dev_priv->seqno_obj = NULL;
4630
4631 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004632}
4633
Eric Anholt673a3942008-07-30 12:06:12 -07004634int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004635i915_gem_init_ringbuffer(struct drm_device *dev)
4636{
4637 drm_i915_private_t *dev_priv = dev->dev_private;
4638 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004639
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004640 if (HAS_PIPE_CONTROL(dev)) {
4641 ret = i915_gem_init_pipe_control(dev);
4642 if (ret)
4643 return ret;
4644 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004645
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004646 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004647 if (ret)
4648 goto cleanup_pipe_control;
4649
4650 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004651 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004652 if (ret)
4653 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004654 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004655
Chris Wilson6f392d52010-08-07 11:01:22 +01004656 dev_priv->next_seqno = 1;
4657
Chris Wilson68f95ba2010-05-27 13:18:22 +01004658 return 0;
4659
4660cleanup_render_ring:
4661 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4662cleanup_pipe_control:
4663 if (HAS_PIPE_CONTROL(dev))
4664 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004665 return ret;
4666}
4667
4668void
4669i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4670{
4671 drm_i915_private_t *dev_priv = dev->dev_private;
4672
4673 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004674 if (HAS_BSD(dev))
4675 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004676 if (HAS_PIPE_CONTROL(dev))
4677 i915_gem_cleanup_pipe_control(dev);
4678}
4679
4680int
Eric Anholt673a3942008-07-30 12:06:12 -07004681i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4682 struct drm_file *file_priv)
4683{
4684 drm_i915_private_t *dev_priv = dev->dev_private;
4685 int ret;
4686
Jesse Barnes79e53942008-11-07 14:24:08 -08004687 if (drm_core_check_feature(dev, DRIVER_MODESET))
4688 return 0;
4689
Ben Gamariba1234d2009-09-14 17:48:47 -04004690 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004691 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004692 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004693 }
4694
Eric Anholt673a3942008-07-30 12:06:12 -07004695 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004696 dev_priv->mm.suspended = 0;
4697
4698 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004699 if (ret != 0) {
4700 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004701 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004702 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004703
Zou Nan hai852835f2010-05-21 09:08:56 +08004704 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004705 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004706 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4707 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004708 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004709 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004710 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004711
Chris Wilson5f353082010-06-07 14:03:03 +01004712 ret = drm_irq_install(dev);
4713 if (ret)
4714 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004715
Eric Anholt673a3942008-07-30 12:06:12 -07004716 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004717
4718cleanup_ringbuffer:
4719 mutex_lock(&dev->struct_mutex);
4720 i915_gem_cleanup_ringbuffer(dev);
4721 dev_priv->mm.suspended = 1;
4722 mutex_unlock(&dev->struct_mutex);
4723
4724 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004725}
4726
4727int
4728i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4729 struct drm_file *file_priv)
4730{
Jesse Barnes79e53942008-11-07 14:24:08 -08004731 if (drm_core_check_feature(dev, DRIVER_MODESET))
4732 return 0;
4733
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004734 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004735 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004736}
4737
4738void
4739i915_gem_lastclose(struct drm_device *dev)
4740{
4741 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004742
Eric Anholte806b492009-01-22 09:56:58 -08004743 if (drm_core_check_feature(dev, DRIVER_MODESET))
4744 return;
4745
Keith Packard6dbe2772008-10-14 21:41:13 -07004746 ret = i915_gem_idle(dev);
4747 if (ret)
4748 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004749}
4750
4751void
4752i915_gem_load(struct drm_device *dev)
4753{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004754 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004755 drm_i915_private_t *dev_priv = dev->dev_private;
4756
Eric Anholt673a3942008-07-30 12:06:12 -07004757 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004758 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004759 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004760 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004761 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004762 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004763 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4764 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004765 if (HAS_BSD(dev)) {
4766 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4767 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4768 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004769 for (i = 0; i < 16; i++)
4770 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004771 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4772 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004773 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004774 spin_lock(&shrink_list_lock);
4775 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4776 spin_unlock(&shrink_list_lock);
4777
Dave Airlie94400122010-07-20 13:15:31 +10004778 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4779 if (IS_GEN3(dev)) {
4780 u32 tmp = I915_READ(MI_ARB_STATE);
4781 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4782 /* arb state is a masked write, so set bit + bit in mask */
4783 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4784 I915_WRITE(MI_ARB_STATE, tmp);
4785 }
4786 }
4787
Jesse Barnesde151cf2008-11-12 10:03:55 -08004788 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004789 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4790 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004791
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004792 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004793 dev_priv->num_fence_regs = 16;
4794 else
4795 dev_priv->num_fence_regs = 8;
4796
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004797 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004798 switch (INTEL_INFO(dev)->gen) {
4799 case 6:
4800 for (i = 0; i < 16; i++)
4801 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4802 break;
4803 case 5:
4804 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004805 for (i = 0; i < 16; i++)
4806 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004807 break;
4808 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004809 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4810 for (i = 0; i < 8; i++)
4811 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004812 case 2:
4813 for (i = 0; i < 8; i++)
4814 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4815 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004816 }
Eric Anholt673a3942008-07-30 12:06:12 -07004817 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004818 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004819}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004820
4821/*
4822 * Create a physically contiguous memory object for this object
4823 * e.g. for cursor + overlay regs
4824 */
Chris Wilson995b6762010-08-20 13:23:26 +01004825static int i915_gem_init_phys_object(struct drm_device *dev,
4826 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004827{
4828 drm_i915_private_t *dev_priv = dev->dev_private;
4829 struct drm_i915_gem_phys_object *phys_obj;
4830 int ret;
4831
4832 if (dev_priv->mm.phys_objs[id - 1] || !size)
4833 return 0;
4834
Eric Anholt9a298b22009-03-24 12:23:04 -07004835 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004836 if (!phys_obj)
4837 return -ENOMEM;
4838
4839 phys_obj->id = id;
4840
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004841 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004842 if (!phys_obj->handle) {
4843 ret = -ENOMEM;
4844 goto kfree_obj;
4845 }
4846#ifdef CONFIG_X86
4847 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4848#endif
4849
4850 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4851
4852 return 0;
4853kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004854 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004855 return ret;
4856}
4857
Chris Wilson995b6762010-08-20 13:23:26 +01004858static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004859{
4860 drm_i915_private_t *dev_priv = dev->dev_private;
4861 struct drm_i915_gem_phys_object *phys_obj;
4862
4863 if (!dev_priv->mm.phys_objs[id - 1])
4864 return;
4865
4866 phys_obj = dev_priv->mm.phys_objs[id - 1];
4867 if (phys_obj->cur_obj) {
4868 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4869 }
4870
4871#ifdef CONFIG_X86
4872 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4873#endif
4874 drm_pci_free(dev, phys_obj->handle);
4875 kfree(phys_obj);
4876 dev_priv->mm.phys_objs[id - 1] = NULL;
4877}
4878
4879void i915_gem_free_all_phys_object(struct drm_device *dev)
4880{
4881 int i;
4882
Dave Airlie260883c2009-01-22 17:58:49 +10004883 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004884 i915_gem_free_phys_object(dev, i);
4885}
4886
4887void i915_gem_detach_phys_object(struct drm_device *dev,
4888 struct drm_gem_object *obj)
4889{
4890 struct drm_i915_gem_object *obj_priv;
4891 int i;
4892 int ret;
4893 int page_count;
4894
Daniel Vetter23010e42010-03-08 13:35:02 +01004895 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004896 if (!obj_priv->phys_obj)
4897 return;
4898
Chris Wilson4bdadb92010-01-27 13:36:32 +00004899 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004900 if (ret)
4901 goto out;
4902
4903 page_count = obj->size / PAGE_SIZE;
4904
4905 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004906 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004907 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4908
4909 memcpy(dst, src, PAGE_SIZE);
4910 kunmap_atomic(dst, KM_USER0);
4911 }
Eric Anholt856fa192009-03-19 14:10:50 -07004912 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004913 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004914
4915 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004916out:
4917 obj_priv->phys_obj->cur_obj = NULL;
4918 obj_priv->phys_obj = NULL;
4919}
4920
4921int
4922i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004923 struct drm_gem_object *obj,
4924 int id,
4925 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004926{
4927 drm_i915_private_t *dev_priv = dev->dev_private;
4928 struct drm_i915_gem_object *obj_priv;
4929 int ret = 0;
4930 int page_count;
4931 int i;
4932
4933 if (id > I915_MAX_PHYS_OBJECT)
4934 return -EINVAL;
4935
Daniel Vetter23010e42010-03-08 13:35:02 +01004936 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004937
4938 if (obj_priv->phys_obj) {
4939 if (obj_priv->phys_obj->id == id)
4940 return 0;
4941 i915_gem_detach_phys_object(dev, obj);
4942 }
4943
Dave Airlie71acb5e2008-12-30 20:31:46 +10004944 /* create a new object */
4945 if (!dev_priv->mm.phys_objs[id - 1]) {
4946 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004947 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004948 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004949 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004950 goto out;
4951 }
4952 }
4953
4954 /* bind to the object */
4955 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4956 obj_priv->phys_obj->cur_obj = obj;
4957
Chris Wilson4bdadb92010-01-27 13:36:32 +00004958 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004959 if (ret) {
4960 DRM_ERROR("failed to get page list\n");
4961 goto out;
4962 }
4963
4964 page_count = obj->size / PAGE_SIZE;
4965
4966 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004967 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004968 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4969
4970 memcpy(dst, src, PAGE_SIZE);
4971 kunmap_atomic(src, KM_USER0);
4972 }
4973
Chris Wilsond78b47b2009-06-17 21:52:49 +01004974 i915_gem_object_put_pages(obj);
4975
Dave Airlie71acb5e2008-12-30 20:31:46 +10004976 return 0;
4977out:
4978 return ret;
4979}
4980
4981static int
4982i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4983 struct drm_i915_gem_pwrite *args,
4984 struct drm_file *file_priv)
4985{
Daniel Vetter23010e42010-03-08 13:35:02 +01004986 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004987 void *obj_addr;
4988 int ret;
4989 char __user *user_data;
4990
4991 user_data = (char __user *) (uintptr_t) args->data_ptr;
4992 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4993
Zhao Yakui44d98a62009-10-09 11:39:40 +08004994 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004995 ret = copy_from_user(obj_addr, user_data, args->size);
4996 if (ret)
4997 return -EFAULT;
4998
4999 drm_agp_chipset_flush(dev);
5000 return 0;
5001}
Eric Anholtb9624422009-06-03 07:27:35 +00005002
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005003void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005004{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005005 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005006
5007 /* Clean up our request list when the client is going away, so that
5008 * later retire_requests won't dereference our soon-to-be-gone
5009 * file_priv.
5010 */
Chris Wilson1c255952010-09-26 11:03:27 +01005011 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005012 while (!list_empty(&file_priv->mm.request_list)) {
5013 struct drm_i915_gem_request *request;
5014
5015 request = list_first_entry(&file_priv->mm.request_list,
5016 struct drm_i915_gem_request,
5017 client_list);
5018 list_del(&request->client_list);
5019 request->file_priv = NULL;
5020 }
Chris Wilson1c255952010-09-26 11:03:27 +01005021 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00005022}
Chris Wilson31169712009-09-14 16:50:28 +01005023
Chris Wilson31169712009-09-14 16:50:28 +01005024static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005025i915_gpu_is_active(struct drm_device *dev)
5026{
5027 drm_i915_private_t *dev_priv = dev->dev_private;
5028 int lists_empty;
5029
Chris Wilson1637ef42010-04-20 17:10:35 +01005030 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08005031 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005032 if (HAS_BSD(dev))
5033 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005034
5035 return !lists_empty;
5036}
5037
5038static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10005039i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005040{
5041 drm_i915_private_t *dev_priv, *next_dev;
5042 struct drm_i915_gem_object *obj_priv, *next_obj;
5043 int cnt = 0;
5044 int would_deadlock = 1;
5045
5046 /* "fast-path" to count number of available objects */
5047 if (nr_to_scan == 0) {
5048 spin_lock(&shrink_list_lock);
5049 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5050 struct drm_device *dev = dev_priv->dev;
5051
5052 if (mutex_trylock(&dev->struct_mutex)) {
5053 list_for_each_entry(obj_priv,
5054 &dev_priv->mm.inactive_list,
5055 list)
5056 cnt++;
5057 mutex_unlock(&dev->struct_mutex);
5058 }
5059 }
5060 spin_unlock(&shrink_list_lock);
5061
5062 return (cnt / 100) * sysctl_vfs_cache_pressure;
5063 }
5064
5065 spin_lock(&shrink_list_lock);
5066
Chris Wilson1637ef42010-04-20 17:10:35 +01005067rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005068 /* first scan for clean buffers */
5069 list_for_each_entry_safe(dev_priv, next_dev,
5070 &shrink_list, mm.shrink_list) {
5071 struct drm_device *dev = dev_priv->dev;
5072
5073 if (! mutex_trylock(&dev->struct_mutex))
5074 continue;
5075
5076 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005077 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005078
Chris Wilson31169712009-09-14 16:50:28 +01005079 list_for_each_entry_safe(obj_priv, next_obj,
5080 &dev_priv->mm.inactive_list,
5081 list) {
5082 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005083 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005084 if (--nr_to_scan <= 0)
5085 break;
5086 }
5087 }
5088
5089 spin_lock(&shrink_list_lock);
5090 mutex_unlock(&dev->struct_mutex);
5091
Chris Wilson963b4832009-09-20 23:03:54 +01005092 would_deadlock = 0;
5093
Chris Wilson31169712009-09-14 16:50:28 +01005094 if (nr_to_scan <= 0)
5095 break;
5096 }
5097
5098 /* second pass, evict/count anything still on the inactive list */
5099 list_for_each_entry_safe(dev_priv, next_dev,
5100 &shrink_list, mm.shrink_list) {
5101 struct drm_device *dev = dev_priv->dev;
5102
5103 if (! mutex_trylock(&dev->struct_mutex))
5104 continue;
5105
5106 spin_unlock(&shrink_list_lock);
5107
5108 list_for_each_entry_safe(obj_priv, next_obj,
5109 &dev_priv->mm.inactive_list,
5110 list) {
5111 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005112 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005113 nr_to_scan--;
5114 } else
5115 cnt++;
5116 }
5117
5118 spin_lock(&shrink_list_lock);
5119 mutex_unlock(&dev->struct_mutex);
5120
5121 would_deadlock = 0;
5122 }
5123
Chris Wilson1637ef42010-04-20 17:10:35 +01005124 if (nr_to_scan) {
5125 int active = 0;
5126
5127 /*
5128 * We are desperate for pages, so as a last resort, wait
5129 * for the GPU to finish and discard whatever we can.
5130 * This has a dramatic impact to reduce the number of
5131 * OOM-killer events whilst running the GPU aggressively.
5132 */
5133 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5134 struct drm_device *dev = dev_priv->dev;
5135
5136 if (!mutex_trylock(&dev->struct_mutex))
5137 continue;
5138
5139 spin_unlock(&shrink_list_lock);
5140
5141 if (i915_gpu_is_active(dev)) {
5142 i915_gpu_idle(dev);
5143 active++;
5144 }
5145
5146 spin_lock(&shrink_list_lock);
5147 mutex_unlock(&dev->struct_mutex);
5148 }
5149
5150 if (active)
5151 goto rescan;
5152 }
5153
Chris Wilson31169712009-09-14 16:50:28 +01005154 spin_unlock(&shrink_list_lock);
5155
5156 if (would_deadlock)
5157 return -1;
5158 else if (cnt > 0)
5159 return (cnt / 100) * sysctl_vfs_cache_pressure;
5160 else
5161 return 0;
5162}
5163
5164static struct shrinker shrinker = {
5165 .shrink = i915_gem_shrink,
5166 .seeks = DEFAULT_SEEKS,
5167};
5168
5169__init void
5170i915_gem_shrinker_init(void)
5171{
5172 register_shrinker(&shrinker);
5173}
5174
5175__exit void
5176i915_gem_shrinker_exit(void)
5177{
5178 unregister_shrinker(&shrinker);
5179}