blob: c033c5a2e9fc9bc78c20c68737378a19607a99b5 [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;
Eric Anholt673a3942008-07-30 12:06:12 -0700579 int ret;
580
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
586 /* Bounds check source.
587 *
588 * XXX: This could use review for overflow issues...
589 */
590 if (args->offset > obj->size || args->size > obj->size ||
591 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000592 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700593 return -EINVAL;
594 }
595
Eric Anholt280b7132009-03-12 16:56:27 -0700596 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700597 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700598 } else {
599 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
600 if (ret != 0)
601 ret = i915_gem_shmem_pread_slow(dev, obj, args,
602 file_priv);
603 }
Eric Anholt673a3942008-07-30 12:06:12 -0700604
Luca Barbieribc9025b2010-02-09 05:49:12 +0000605 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700606
Eric Anholteb014592009-03-10 11:44:52 -0700607 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700608}
609
Keith Packard0839ccb2008-10-30 19:38:48 -0700610/* This is the fast write path which cannot handle
611 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700612 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700613
Keith Packard0839ccb2008-10-30 19:38:48 -0700614static inline int
615fast_user_write(struct io_mapping *mapping,
616 loff_t page_base, int page_offset,
617 char __user *user_data,
618 int length)
619{
620 char *vaddr_atomic;
621 unsigned long unwritten;
622
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100623 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700624 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
625 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100626 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700627 if (unwritten)
628 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700629 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700630}
631
632/* Here's the write path which can sleep for
633 * page faults
634 */
635
Chris Wilsonab34c222010-05-27 14:15:35 +0100636static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700637slow_kernel_write(struct io_mapping *mapping,
638 loff_t gtt_base, int gtt_offset,
639 struct page *user_page, int user_offset,
640 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700641{
Chris Wilsonab34c222010-05-27 14:15:35 +0100642 char __iomem *dst_vaddr;
643 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700644
Chris Wilsonab34c222010-05-27 14:15:35 +0100645 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
646 src_vaddr = kmap(user_page);
647
648 memcpy_toio(dst_vaddr + gtt_offset,
649 src_vaddr + user_offset,
650 length);
651
652 kunmap(user_page);
653 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700654}
655
Eric Anholt40123c12009-03-09 13:42:30 -0700656static inline int
657fast_shmem_write(struct page **pages,
658 loff_t page_base, int page_offset,
659 char __user *data,
660 int length)
661{
662 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400663 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700664
665 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
666 if (vaddr == NULL)
667 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400668 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700669 kunmap_atomic(vaddr, KM_USER0);
670
Dave Airlied0088772009-03-28 20:29:48 -0400671 if (unwritten)
672 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700673 return 0;
674}
675
Eric Anholt3de09aa2009-03-09 09:42:23 -0700676/**
677 * This is the fast pwrite path, where we copy the data directly from the
678 * user into the GTT, uncached.
679 */
Eric Anholt673a3942008-07-30 12:06:12 -0700680static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700681i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
682 struct drm_i915_gem_pwrite *args,
683 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700684{
Daniel Vetter23010e42010-03-08 13:35:02 +0100685 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700686 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700687 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700688 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700689 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700690 int page_offset, page_length;
691 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700692
693 user_data = (char __user *) (uintptr_t) args->data_ptr;
694 remain = args->size;
695 if (!access_ok(VERIFY_READ, user_data, remain))
696 return -EFAULT;
697
Chris Wilson76c1dec2010-09-25 11:22:51 +0100698 ret = i915_mutex_lock_interruptible(dev);
699 if (ret)
700 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700701
Eric Anholt673a3942008-07-30 12:06:12 -0700702 ret = i915_gem_object_pin(obj, 0);
703 if (ret) {
704 mutex_unlock(&dev->struct_mutex);
705 return ret;
706 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800707 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700708 if (ret)
709 goto fail;
710
Daniel Vetter23010e42010-03-08 13:35:02 +0100711 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700712 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700713
714 while (remain > 0) {
715 /* Operation in this page
716 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700717 * page_base = page offset within aperture
718 * page_offset = offset within page
719 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700720 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700721 page_base = (offset & ~(PAGE_SIZE-1));
722 page_offset = offset & (PAGE_SIZE-1);
723 page_length = remain;
724 if ((page_offset + remain) > PAGE_SIZE)
725 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700726
Keith Packard0839ccb2008-10-30 19:38:48 -0700727 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
728 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700729
Keith Packard0839ccb2008-10-30 19:38:48 -0700730 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700731 * source page isn't available. Return the error and we'll
732 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700733 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700734 if (ret)
735 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700736
Keith Packard0839ccb2008-10-30 19:38:48 -0700737 remain -= page_length;
738 user_data += page_length;
739 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700740 }
Eric Anholt673a3942008-07-30 12:06:12 -0700741
742fail:
743 i915_gem_object_unpin(obj);
744 mutex_unlock(&dev->struct_mutex);
745
746 return ret;
747}
748
Eric Anholt3de09aa2009-03-09 09:42:23 -0700749/**
750 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
751 * the memory and maps it using kmap_atomic for copying.
752 *
753 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
754 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
755 */
Eric Anholt3043c602008-10-02 12:24:47 -0700756static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700757i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
758 struct drm_i915_gem_pwrite *args,
759 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700760{
Daniel Vetter23010e42010-03-08 13:35:02 +0100761 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700762 drm_i915_private_t *dev_priv = dev->dev_private;
763 ssize_t remain;
764 loff_t gtt_page_base, offset;
765 loff_t first_data_page, last_data_page, num_pages;
766 loff_t pinned_pages, i;
767 struct page **user_pages;
768 struct mm_struct *mm = current->mm;
769 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700770 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700771 uint64_t data_ptr = args->data_ptr;
772
773 remain = args->size;
774
775 /* Pin the user pages containing the data. We can't fault while
776 * holding the struct mutex, and all of the pwrite implementations
777 * want to hold it while dereferencing the user data.
778 */
779 first_data_page = data_ptr / PAGE_SIZE;
780 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
781 num_pages = last_data_page - first_data_page + 1;
782
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700783 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700784 if (user_pages == NULL)
785 return -ENOMEM;
786
787 down_read(&mm->mmap_sem);
788 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
789 num_pages, 0, 0, user_pages, NULL);
790 up_read(&mm->mmap_sem);
791 if (pinned_pages < num_pages) {
792 ret = -EFAULT;
793 goto out_unpin_pages;
794 }
795
Chris Wilson76c1dec2010-09-25 11:22:51 +0100796 ret = i915_mutex_lock_interruptible(dev);
797 if (ret)
798 goto out_unpin_pages;
799
Eric Anholt3de09aa2009-03-09 09:42:23 -0700800 ret = i915_gem_object_pin(obj, 0);
801 if (ret)
802 goto out_unlock;
803
804 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
805 if (ret)
806 goto out_unpin_object;
807
Daniel Vetter23010e42010-03-08 13:35:02 +0100808 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700809 offset = obj_priv->gtt_offset + args->offset;
810
811 while (remain > 0) {
812 /* Operation in this page
813 *
814 * gtt_page_base = page offset within aperture
815 * gtt_page_offset = offset within page in aperture
816 * data_page_index = page number in get_user_pages return
817 * data_page_offset = offset with data_page_index page.
818 * page_length = bytes to copy for this page
819 */
820 gtt_page_base = offset & PAGE_MASK;
821 gtt_page_offset = offset & ~PAGE_MASK;
822 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
823 data_page_offset = data_ptr & ~PAGE_MASK;
824
825 page_length = remain;
826 if ((gtt_page_offset + page_length) > PAGE_SIZE)
827 page_length = PAGE_SIZE - gtt_page_offset;
828 if ((data_page_offset + page_length) > PAGE_SIZE)
829 page_length = PAGE_SIZE - data_page_offset;
830
Chris Wilsonab34c222010-05-27 14:15:35 +0100831 slow_kernel_write(dev_priv->mm.gtt_mapping,
832 gtt_page_base, gtt_page_offset,
833 user_pages[data_page_index],
834 data_page_offset,
835 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700836
837 remain -= page_length;
838 offset += page_length;
839 data_ptr += page_length;
840 }
841
842out_unpin_object:
843 i915_gem_object_unpin(obj);
844out_unlock:
845 mutex_unlock(&dev->struct_mutex);
846out_unpin_pages:
847 for (i = 0; i < pinned_pages; i++)
848 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700849 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700850
851 return ret;
852}
853
Eric Anholt40123c12009-03-09 13:42:30 -0700854/**
855 * This is the fast shmem pwrite path, which attempts to directly
856 * copy_from_user into the kmapped pages backing the object.
857 */
Eric Anholt673a3942008-07-30 12:06:12 -0700858static int
Eric Anholt40123c12009-03-09 13:42:30 -0700859i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
860 struct drm_i915_gem_pwrite *args,
861 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700862{
Daniel Vetter23010e42010-03-08 13:35:02 +0100863 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700864 ssize_t remain;
865 loff_t offset, page_base;
866 char __user *user_data;
867 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700868 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700869
870 user_data = (char __user *) (uintptr_t) args->data_ptr;
871 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700872
Chris Wilson76c1dec2010-09-25 11:22:51 +0100873 ret = i915_mutex_lock_interruptible(dev);
874 if (ret)
875 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700876
Chris Wilson4bdadb92010-01-27 13:36:32 +0000877 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700878 if (ret != 0)
879 goto fail_unlock;
880
Eric Anholte47c68e2008-11-14 13:35:19 -0800881 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700882 if (ret != 0)
883 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700884
Daniel Vetter23010e42010-03-08 13:35:02 +0100885 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700886 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700887 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700888
Eric Anholt40123c12009-03-09 13:42:30 -0700889 while (remain > 0) {
890 /* Operation in this page
891 *
892 * page_base = page offset within aperture
893 * page_offset = offset within page
894 * page_length = bytes to copy for this page
895 */
896 page_base = (offset & ~(PAGE_SIZE-1));
897 page_offset = offset & (PAGE_SIZE-1);
898 page_length = remain;
899 if ((page_offset + remain) > PAGE_SIZE)
900 page_length = PAGE_SIZE - page_offset;
901
902 ret = fast_shmem_write(obj_priv->pages,
903 page_base, page_offset,
904 user_data, page_length);
905 if (ret)
906 goto fail_put_pages;
907
908 remain -= page_length;
909 user_data += page_length;
910 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700911 }
912
Eric Anholt40123c12009-03-09 13:42:30 -0700913fail_put_pages:
914 i915_gem_object_put_pages(obj);
915fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700916 mutex_unlock(&dev->struct_mutex);
917
Eric Anholt40123c12009-03-09 13:42:30 -0700918 return ret;
919}
920
921/**
922 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
923 * the memory and maps it using kmap_atomic for copying.
924 *
925 * This avoids taking mmap_sem for faulting on the user's address while the
926 * struct_mutex is held.
927 */
928static int
929i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
930 struct drm_i915_gem_pwrite *args,
931 struct drm_file *file_priv)
932{
Daniel Vetter23010e42010-03-08 13:35:02 +0100933 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700934 struct mm_struct *mm = current->mm;
935 struct page **user_pages;
936 ssize_t remain;
937 loff_t offset, pinned_pages, i;
938 loff_t first_data_page, last_data_page, num_pages;
939 int shmem_page_index, shmem_page_offset;
940 int data_page_index, data_page_offset;
941 int page_length;
942 int ret;
943 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700944 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700945
946 remain = args->size;
947
948 /* Pin the user pages containing the data. We can't fault while
949 * holding the struct mutex, and all of the pwrite implementations
950 * want to hold it while dereferencing the user data.
951 */
952 first_data_page = data_ptr / PAGE_SIZE;
953 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
954 num_pages = last_data_page - first_data_page + 1;
955
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700956 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700957 if (user_pages == NULL)
958 return -ENOMEM;
959
960 down_read(&mm->mmap_sem);
961 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
962 num_pages, 0, 0, user_pages, NULL);
963 up_read(&mm->mmap_sem);
964 if (pinned_pages < num_pages) {
965 ret = -EFAULT;
966 goto fail_put_user_pages;
967 }
968
Eric Anholt280b7132009-03-12 16:56:27 -0700969 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
970
Chris Wilson76c1dec2010-09-25 11:22:51 +0100971 ret = i915_mutex_lock_interruptible(dev);
972 if (ret)
973 goto fail_put_user_pages;
Eric Anholt40123c12009-03-09 13:42:30 -0700974
Chris Wilson07f73f62009-09-14 16:50:30 +0100975 ret = i915_gem_object_get_pages_or_evict(obj);
976 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700977 goto fail_unlock;
978
979 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
980 if (ret != 0)
981 goto fail_put_pages;
982
Daniel Vetter23010e42010-03-08 13:35:02 +0100983 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700984 offset = args->offset;
985 obj_priv->dirty = 1;
986
987 while (remain > 0) {
988 /* Operation in this page
989 *
990 * shmem_page_index = page number within shmem file
991 * shmem_page_offset = offset within page in shmem file
992 * data_page_index = page number in get_user_pages return
993 * data_page_offset = offset with data_page_index page.
994 * page_length = bytes to copy for this page
995 */
996 shmem_page_index = offset / PAGE_SIZE;
997 shmem_page_offset = offset & ~PAGE_MASK;
998 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
999 data_page_offset = data_ptr & ~PAGE_MASK;
1000
1001 page_length = remain;
1002 if ((shmem_page_offset + page_length) > PAGE_SIZE)
1003 page_length = PAGE_SIZE - shmem_page_offset;
1004 if ((data_page_offset + page_length) > PAGE_SIZE)
1005 page_length = PAGE_SIZE - data_page_offset;
1006
Eric Anholt280b7132009-03-12 16:56:27 -07001007 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +01001008 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -07001009 shmem_page_offset,
1010 user_pages[data_page_index],
1011 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +01001012 page_length,
1013 0);
1014 } else {
1015 slow_shmem_copy(obj_priv->pages[shmem_page_index],
1016 shmem_page_offset,
1017 user_pages[data_page_index],
1018 data_page_offset,
1019 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -07001020 }
Eric Anholt40123c12009-03-09 13:42:30 -07001021
1022 remain -= page_length;
1023 data_ptr += page_length;
1024 offset += page_length;
1025 }
1026
1027fail_put_pages:
1028 i915_gem_object_put_pages(obj);
1029fail_unlock:
1030 mutex_unlock(&dev->struct_mutex);
1031fail_put_user_pages:
1032 for (i = 0; i < pinned_pages; i++)
1033 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001034 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -07001035
1036 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001037}
1038
1039/**
1040 * Writes data to the object referenced by handle.
1041 *
1042 * On error, the contents of the buffer that were to be modified are undefined.
1043 */
1044int
1045i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1046 struct drm_file *file_priv)
1047{
1048 struct drm_i915_gem_pwrite *args = data;
1049 struct drm_gem_object *obj;
1050 struct drm_i915_gem_object *obj_priv;
1051 int ret = 0;
1052
1053 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1054 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001055 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001056 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001057
1058 /* Bounds check destination.
1059 *
1060 * XXX: This could use review for overflow issues...
1061 */
1062 if (args->offset > obj->size || args->size > obj->size ||
1063 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +00001064 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001065 return -EINVAL;
1066 }
1067
1068 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1069 * it would end up going through the fenced access, and we'll get
1070 * different detiling behavior between reading and writing.
1071 * pread/pwrite currently are reading and writing from the CPU
1072 * perspective, requiring manual detiling by the client.
1073 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001074 if (obj_priv->phys_obj)
1075 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
1076 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001077 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001078 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -07001079 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
1080 if (ret == -EFAULT) {
1081 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
1082 file_priv);
1083 }
Eric Anholt280b7132009-03-12 16:56:27 -07001084 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
1085 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -07001086 } else {
1087 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
1088 if (ret == -EFAULT) {
1089 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
1090 file_priv);
1091 }
1092 }
Eric Anholt673a3942008-07-30 12:06:12 -07001093
1094#if WATCH_PWRITE
1095 if (ret)
1096 DRM_INFO("pwrite failed %d\n", ret);
1097#endif
1098
Luca Barbieribc9025b2010-02-09 05:49:12 +00001099 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001100
1101 return ret;
1102}
1103
1104/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001105 * Called when user space prepares to use an object with the CPU, either
1106 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001107 */
1108int
1109i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1110 struct drm_file *file_priv)
1111{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001112 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001113 struct drm_i915_gem_set_domain *args = data;
1114 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001115 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001116 uint32_t read_domains = args->read_domains;
1117 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001118 int ret;
1119
1120 if (!(dev->driver->driver_features & DRIVER_GEM))
1121 return -ENODEV;
1122
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001123 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001124 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001125 return -EINVAL;
1126
Chris Wilson21d509e2009-06-06 09:46:02 +01001127 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001128 return -EINVAL;
1129
1130 /* Having something in the write domain implies it's in the read
1131 * domain, and only that read domain. Enforce that in the request.
1132 */
1133 if (write_domain != 0 && read_domains != write_domain)
1134 return -EINVAL;
1135
Eric Anholt673a3942008-07-30 12:06:12 -07001136 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1137 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001138 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001139 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001140
Chris Wilson76c1dec2010-09-25 11:22:51 +01001141 ret = i915_mutex_lock_interruptible(dev);
1142 if (ret) {
1143 drm_gem_object_unreference_unlocked(obj);
1144 return ret;
1145 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001146
1147 intel_mark_busy(dev, obj);
1148
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001149 if (read_domains & I915_GEM_DOMAIN_GTT) {
1150 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001151
Eric Anholta09ba7f2009-08-29 12:49:51 -07001152 /* Update the LRU on the fence for the CPU access that's
1153 * about to occur.
1154 */
1155 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001156 struct drm_i915_fence_reg *reg =
1157 &dev_priv->fence_regs[obj_priv->fence_reg];
1158 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001159 &dev_priv->mm.fence_list);
1160 }
1161
Eric Anholt02354392008-11-26 13:58:13 -08001162 /* Silently promote "you're not bound, there was nothing to do"
1163 * to success, since the client was just asking us to
1164 * make sure everything was done.
1165 */
1166 if (ret == -EINVAL)
1167 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001168 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001169 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001170 }
1171
Chris Wilson7d1c4802010-08-07 21:45:03 +01001172 /* Maintain LRU order of "inactive" objects */
1173 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1174 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1175
Eric Anholt673a3942008-07-30 12:06:12 -07001176 drm_gem_object_unreference(obj);
1177 mutex_unlock(&dev->struct_mutex);
1178 return ret;
1179}
1180
1181/**
1182 * Called when user space has done writes to this buffer
1183 */
1184int
1185i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1186 struct drm_file *file_priv)
1187{
1188 struct drm_i915_gem_sw_finish *args = data;
1189 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001190 int ret = 0;
1191
1192 if (!(dev->driver->driver_features & DRIVER_GEM))
1193 return -ENODEV;
1194
Eric Anholt673a3942008-07-30 12:06:12 -07001195 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson76c1dec2010-09-25 11:22:51 +01001196 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001197 return -ENOENT;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001198
1199 ret = i915_mutex_lock_interruptible(dev);
1200 if (ret) {
1201 drm_gem_object_unreference_unlocked(obj);
1202 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001203 }
1204
Eric Anholt673a3942008-07-30 12:06:12 -07001205 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001206 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001207 i915_gem_object_flush_cpu_write_domain(obj);
1208
Eric Anholt673a3942008-07-30 12:06:12 -07001209 drm_gem_object_unreference(obj);
1210 mutex_unlock(&dev->struct_mutex);
1211 return ret;
1212}
1213
1214/**
1215 * Maps the contents of an object, returning the address it is mapped
1216 * into.
1217 *
1218 * While the mapping holds a reference on the contents of the object, it doesn't
1219 * imply a ref on the object itself.
1220 */
1221int
1222i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1223 struct drm_file *file_priv)
1224{
1225 struct drm_i915_gem_mmap *args = data;
1226 struct drm_gem_object *obj;
1227 loff_t offset;
1228 unsigned long addr;
1229
1230 if (!(dev->driver->driver_features & DRIVER_GEM))
1231 return -ENODEV;
1232
1233 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1234 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001235 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001236
1237 offset = args->offset;
1238
1239 down_write(&current->mm->mmap_sem);
1240 addr = do_mmap(obj->filp, 0, args->size,
1241 PROT_READ | PROT_WRITE, MAP_SHARED,
1242 args->offset);
1243 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001244 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001245 if (IS_ERR((void *)addr))
1246 return addr;
1247
1248 args->addr_ptr = (uint64_t) addr;
1249
1250 return 0;
1251}
1252
Jesse Barnesde151cf2008-11-12 10:03:55 -08001253/**
1254 * i915_gem_fault - fault a page into the GTT
1255 * vma: VMA in question
1256 * vmf: fault info
1257 *
1258 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1259 * from userspace. The fault handler takes care of binding the object to
1260 * the GTT (if needed), allocating and programming a fence register (again,
1261 * only if needed based on whether the old reg is still valid or the object
1262 * is tiled) and inserting a new PTE into the faulting process.
1263 *
1264 * Note that the faulting process may involve evicting existing objects
1265 * from the GTT and/or fence registers to make room. So performance may
1266 * suffer if the GTT working set is large or there are few fence registers
1267 * left.
1268 */
1269int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1270{
1271 struct drm_gem_object *obj = vma->vm_private_data;
1272 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001273 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001274 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001275 pgoff_t page_offset;
1276 unsigned long pfn;
1277 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001278 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001279
1280 /* We don't use vmf->pgoff since that has the fake offset */
1281 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1282 PAGE_SHIFT;
1283
1284 /* Now bind it into the GTT if needed */
1285 mutex_lock(&dev->struct_mutex);
1286 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001287 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001288 if (ret)
1289 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001290
Jesse Barnesde151cf2008-11-12 10:03:55 -08001291 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001292 if (ret)
1293 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001294 }
1295
1296 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001297 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001298 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001299 if (ret)
1300 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001301 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001302
Chris Wilson7d1c4802010-08-07 21:45:03 +01001303 if (i915_gem_object_is_inactive(obj_priv))
1304 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1305
Jesse Barnesde151cf2008-11-12 10:03:55 -08001306 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1307 page_offset;
1308
1309 /* Finally, remap it using the new GTT offset */
1310 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001311unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001312 mutex_unlock(&dev->struct_mutex);
1313
1314 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001315 case 0:
1316 case -ERESTARTSYS:
1317 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001318 case -ENOMEM:
1319 case -EAGAIN:
1320 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001322 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001323 }
1324}
1325
1326/**
1327 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1328 * @obj: obj in question
1329 *
1330 * GEM memory mapping works by handing back to userspace a fake mmap offset
1331 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1332 * up the object based on the offset and sets up the various memory mapping
1333 * structures.
1334 *
1335 * This routine allocates and attaches a fake offset for @obj.
1336 */
1337static int
1338i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1339{
1340 struct drm_device *dev = obj->dev;
1341 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001342 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001343 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001344 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001345 int ret = 0;
1346
1347 /* Set the object up for mmap'ing */
1348 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001349 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001350 if (!list->map)
1351 return -ENOMEM;
1352
1353 map = list->map;
1354 map->type = _DRM_GEM;
1355 map->size = obj->size;
1356 map->handle = obj;
1357
1358 /* Get a DRM GEM mmap offset allocated... */
1359 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1360 obj->size / PAGE_SIZE, 0, 0);
1361 if (!list->file_offset_node) {
1362 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001363 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001364 goto out_free_list;
1365 }
1366
1367 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1368 obj->size / PAGE_SIZE, 0);
1369 if (!list->file_offset_node) {
1370 ret = -ENOMEM;
1371 goto out_free_list;
1372 }
1373
1374 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001375 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1376 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001377 DRM_ERROR("failed to add to map hash\n");
1378 goto out_free_mm;
1379 }
1380
1381 /* By now we should be all set, any drm_mmap request on the offset
1382 * below will get to our mmap & fault handler */
1383 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1384
1385 return 0;
1386
1387out_free_mm:
1388 drm_mm_put_block(list->file_offset_node);
1389out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001390 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001391
1392 return ret;
1393}
1394
Chris Wilson901782b2009-07-10 08:18:50 +01001395/**
1396 * i915_gem_release_mmap - remove physical page mappings
1397 * @obj: obj in question
1398 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001399 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001400 * relinquish ownership of the pages back to the system.
1401 *
1402 * It is vital that we remove the page mapping if we have mapped a tiled
1403 * object through the GTT and then lose the fence register due to
1404 * resource pressure. Similarly if the object has been moved out of the
1405 * aperture, than pages mapped into userspace must be revoked. Removing the
1406 * mapping will then trigger a page fault on the next user access, allowing
1407 * fixup by i915_gem_fault().
1408 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001409void
Chris Wilson901782b2009-07-10 08:18:50 +01001410i915_gem_release_mmap(struct drm_gem_object *obj)
1411{
1412 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001413 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001414
1415 if (dev->dev_mapping)
1416 unmap_mapping_range(dev->dev_mapping,
1417 obj_priv->mmap_offset, obj->size, 1);
1418}
1419
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001420static void
1421i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1422{
1423 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001424 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001425 struct drm_gem_mm *mm = dev->mm_private;
1426 struct drm_map_list *list;
1427
1428 list = &obj->map_list;
1429 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1430
1431 if (list->file_offset_node) {
1432 drm_mm_put_block(list->file_offset_node);
1433 list->file_offset_node = NULL;
1434 }
1435
1436 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001437 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001438 list->map = NULL;
1439 }
1440
1441 obj_priv->mmap_offset = 0;
1442}
1443
Jesse Barnesde151cf2008-11-12 10:03:55 -08001444/**
1445 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1446 * @obj: object to check
1447 *
1448 * Return the required GTT alignment for an object, taking into account
1449 * potential fence register mapping if needed.
1450 */
1451static uint32_t
1452i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1453{
1454 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001455 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001456 int start, i;
1457
1458 /*
1459 * Minimum alignment is 4k (GTT page size), but might be greater
1460 * if a fence register is needed for the object.
1461 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001462 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001463 return 4096;
1464
1465 /*
1466 * Previous chips need to be aligned to the size of the smallest
1467 * fence register that can contain the object.
1468 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001469 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001470 start = 1024*1024;
1471 else
1472 start = 512*1024;
1473
1474 for (i = start; i < obj->size; i <<= 1)
1475 ;
1476
1477 return i;
1478}
1479
1480/**
1481 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1482 * @dev: DRM device
1483 * @data: GTT mapping ioctl data
1484 * @file_priv: GEM object info
1485 *
1486 * Simply returns the fake offset to userspace so it can mmap it.
1487 * The mmap call will end up in drm_gem_mmap(), which will set things
1488 * up so we can get faults in the handler above.
1489 *
1490 * The fault handler will take care of binding the object into the GTT
1491 * (since it may have been evicted to make room for something), allocating
1492 * a fence register, and mapping the appropriate aperture address into
1493 * userspace.
1494 */
1495int
1496i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1497 struct drm_file *file_priv)
1498{
1499 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001500 struct drm_gem_object *obj;
1501 struct drm_i915_gem_object *obj_priv;
1502 int ret;
1503
1504 if (!(dev->driver->driver_features & DRIVER_GEM))
1505 return -ENODEV;
1506
1507 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1508 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001509 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001510
Chris Wilson76c1dec2010-09-25 11:22:51 +01001511 ret = i915_mutex_lock_interruptible(dev);
1512 if (ret) {
1513 drm_gem_object_unreference_unlocked(obj);
1514 return ret;
1515 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001516
Daniel Vetter23010e42010-03-08 13:35:02 +01001517 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001518
Chris Wilsonab182822009-09-22 18:46:17 +01001519 if (obj_priv->madv != I915_MADV_WILLNEED) {
1520 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1521 drm_gem_object_unreference(obj);
1522 mutex_unlock(&dev->struct_mutex);
1523 return -EINVAL;
1524 }
1525
1526
Jesse Barnesde151cf2008-11-12 10:03:55 -08001527 if (!obj_priv->mmap_offset) {
1528 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001529 if (ret) {
1530 drm_gem_object_unreference(obj);
1531 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001532 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001533 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001534 }
1535
1536 args->offset = obj_priv->mmap_offset;
1537
Jesse Barnesde151cf2008-11-12 10:03:55 -08001538 /*
1539 * Pull it into the GTT so that we have a page list (makes the
1540 * initial fault faster and any subsequent flushing possible).
1541 */
1542 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001543 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001544 if (ret) {
1545 drm_gem_object_unreference(obj);
1546 mutex_unlock(&dev->struct_mutex);
1547 return ret;
1548 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001549 }
1550
1551 drm_gem_object_unreference(obj);
1552 mutex_unlock(&dev->struct_mutex);
1553
1554 return 0;
1555}
1556
Chris Wilson5cdf5882010-09-27 15:51:07 +01001557static void
Eric Anholt856fa192009-03-19 14:10:50 -07001558i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001559{
Daniel Vetter23010e42010-03-08 13:35:02 +01001560 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001561 int page_count = obj->size / PAGE_SIZE;
1562 int i;
1563
Eric Anholt856fa192009-03-19 14:10:50 -07001564 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001565 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001566
1567 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001568 return;
1569
Eric Anholt280b7132009-03-12 16:56:27 -07001570 if (obj_priv->tiling_mode != I915_TILING_NONE)
1571 i915_gem_object_save_bit_17_swizzle(obj);
1572
Chris Wilson3ef94da2009-09-14 16:50:29 +01001573 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001574 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001575
1576 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001577 if (obj_priv->dirty)
1578 set_page_dirty(obj_priv->pages[i]);
1579
1580 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001581 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001582
1583 page_cache_release(obj_priv->pages[i]);
1584 }
Eric Anholt673a3942008-07-30 12:06:12 -07001585 obj_priv->dirty = 0;
1586
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001587 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001588 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001589}
1590
Chris Wilsona56ba562010-09-28 10:07:56 +01001591static uint32_t
1592i915_gem_next_request_seqno(struct drm_device *dev,
1593 struct intel_ring_buffer *ring)
1594{
1595 drm_i915_private_t *dev_priv = dev->dev_private;
1596
1597 ring->outstanding_lazy_request = true;
1598 return dev_priv->next_seqno;
1599}
1600
Eric Anholt673a3942008-07-30 12:06:12 -07001601static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001602i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001603 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001604{
Chris Wilsona56ba562010-09-28 10:07:56 +01001605 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001606 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001607 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001608
Zou Nan hai852835f2010-05-21 09:08:56 +08001609 BUG_ON(ring == NULL);
1610 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001611
1612 /* Add a reference if we're newly entering the active list. */
1613 if (!obj_priv->active) {
1614 drm_gem_object_reference(obj);
1615 obj_priv->active = 1;
1616 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001617
Eric Anholt673a3942008-07-30 12:06:12 -07001618 /* Move from whatever list we were on to the tail of execution. */
Zou Nan hai852835f2010-05-21 09:08:56 +08001619 list_move_tail(&obj_priv->list, &ring->active_list);
Chris Wilsona56ba562010-09-28 10:07:56 +01001620 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001621}
1622
Eric Anholtce44b0e2008-11-06 16:00:31 -08001623static void
1624i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1625{
1626 struct drm_device *dev = obj->dev;
1627 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001628 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001629
1630 BUG_ON(!obj_priv->active);
1631 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1632 obj_priv->last_rendering_seqno = 0;
1633}
Eric Anholt673a3942008-07-30 12:06:12 -07001634
Chris Wilson963b4832009-09-20 23:03:54 +01001635/* Immediately discard the backing storage */
1636static void
1637i915_gem_object_truncate(struct drm_gem_object *obj)
1638{
Daniel Vetter23010e42010-03-08 13:35:02 +01001639 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001640 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001641
Chris Wilsonae9fed62010-08-07 11:01:30 +01001642 /* Our goal here is to return as much of the memory as
1643 * is possible back to the system as we are called from OOM.
1644 * To do this we must instruct the shmfs to drop all of its
1645 * backing pages, *now*. Here we mirror the actions taken
1646 * when by shmem_delete_inode() to release the backing store.
1647 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001648 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001649 truncate_inode_pages(inode->i_mapping, 0);
1650 if (inode->i_op->truncate_range)
1651 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001652
1653 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001654}
1655
1656static inline int
1657i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1658{
1659 return obj_priv->madv == I915_MADV_DONTNEED;
1660}
1661
Eric Anholt673a3942008-07-30 12:06:12 -07001662static void
1663i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1664{
1665 struct drm_device *dev = obj->dev;
1666 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001667 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001668
Eric Anholt673a3942008-07-30 12:06:12 -07001669 if (obj_priv->pin_count != 0)
Chris Wilsonf13d3f72010-09-20 17:36:15 +01001670 list_move_tail(&obj_priv->list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001671 else
1672 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1673
Daniel Vetter99fcb762010-02-07 16:20:18 +01001674 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1675
Eric Anholtce44b0e2008-11-06 16:00:31 -08001676 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001677 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001678 if (obj_priv->active) {
1679 obj_priv->active = 0;
1680 drm_gem_object_unreference(obj);
1681 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001682 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001683}
1684
Chris Wilson92204342010-09-18 11:02:01 +01001685static void
Daniel Vetter63560392010-02-19 11:51:59 +01001686i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001687 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001688 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001689{
1690 drm_i915_private_t *dev_priv = dev->dev_private;
1691 struct drm_i915_gem_object *obj_priv, *next;
1692
1693 list_for_each_entry_safe(obj_priv, next,
1694 &dev_priv->mm.gpu_write_list,
1695 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001696 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001697
Chris Wilson2b6efaa2010-09-14 17:04:02 +01001698 if (obj->write_domain & flush_domains &&
1699 obj_priv->ring == ring) {
Daniel Vetter63560392010-02-19 11:51:59 +01001700 uint32_t old_write_domain = obj->write_domain;
1701
1702 obj->write_domain = 0;
1703 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001704 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001705
1706 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001707 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1708 struct drm_i915_fence_reg *reg =
1709 &dev_priv->fence_regs[obj_priv->fence_reg];
1710 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001711 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001712 }
Daniel Vetter63560392010-02-19 11:51:59 +01001713
1714 trace_i915_gem_object_change_domain(obj,
1715 obj->read_domains,
1716 old_write_domain);
1717 }
1718 }
1719}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001720
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001721uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001722i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001723 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001724 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001725 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001726{
1727 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001728 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001729 uint32_t seqno;
1730 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001731
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001732 if (file != NULL)
1733 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001734
Chris Wilson8dc5d142010-08-12 12:36:12 +01001735 if (request == NULL) {
1736 request = kzalloc(sizeof(*request), GFP_KERNEL);
1737 if (request == NULL)
1738 return 0;
1739 }
Eric Anholt673a3942008-07-30 12:06:12 -07001740
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001741 seqno = ring->add_request(dev, ring, 0);
Chris Wilsona56ba562010-09-28 10:07:56 +01001742 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001743
1744 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001745 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001746 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001747 was_empty = list_empty(&ring->request_list);
1748 list_add_tail(&request->list, &ring->request_list);
1749
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001750 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001751 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001752 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001753 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001754 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001755 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001756 }
Eric Anholt673a3942008-07-30 12:06:12 -07001757
Ben Gamarif65d9422009-09-14 17:48:44 -04001758 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001759 mod_timer(&dev_priv->hangcheck_timer,
1760 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001761 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001762 queue_delayed_work(dev_priv->wq,
1763 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001764 }
Eric Anholt673a3942008-07-30 12:06:12 -07001765 return seqno;
1766}
1767
1768/**
1769 * Command execution barrier
1770 *
1771 * Ensures that all commands in the ring are finished
1772 * before signalling the CPU
1773 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001774static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001775i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001776{
Eric Anholt673a3942008-07-30 12:06:12 -07001777 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001778
1779 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001780 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001781 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001782
1783 ring->flush(dev, ring,
1784 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001785}
1786
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001787static inline void
1788i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001789{
Chris Wilson1c255952010-09-26 11:03:27 +01001790 struct drm_i915_file_private *file_priv = request->file_priv;
1791
1792 if (!file_priv)
1793 return;
1794
1795 spin_lock(&file_priv->mm.lock);
1796 list_del(&request->client_list);
1797 request->file_priv = NULL;
1798 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001799}
1800
Chris Wilsondfaae392010-09-22 10:31:52 +01001801static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1802 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001803{
Chris Wilsondfaae392010-09-22 10:31:52 +01001804 while (!list_empty(&ring->request_list)) {
1805 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001806
Chris Wilsondfaae392010-09-22 10:31:52 +01001807 request = list_first_entry(&ring->request_list,
1808 struct drm_i915_gem_request,
1809 list);
1810
1811 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001812 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001813 kfree(request);
1814 }
1815
1816 while (!list_empty(&ring->active_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001817 struct drm_i915_gem_object *obj_priv;
1818
Chris Wilsondfaae392010-09-22 10:31:52 +01001819 obj_priv = list_first_entry(&ring->active_list,
1820 struct drm_i915_gem_object,
1821 list);
1822
1823 obj_priv->base.write_domain = 0;
1824 list_del_init(&obj_priv->gpu_write_list);
1825 i915_gem_object_move_to_inactive(&obj_priv->base);
1826 }
1827}
1828
1829void i915_gem_reset_lists(struct drm_device *dev)
1830{
1831 struct drm_i915_private *dev_priv = dev->dev_private;
1832 struct drm_i915_gem_object *obj_priv;
1833
1834 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
1835 if (HAS_BSD(dev))
1836 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
1837
1838 /* Remove anything from the flushing lists. The GPU cache is likely
1839 * to be lost on reset along with the data, so simply move the
1840 * lost bo to the inactive list.
1841 */
1842 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001843 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1844 struct drm_i915_gem_object,
1845 list);
1846
1847 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001848 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001849 i915_gem_object_move_to_inactive(&obj_priv->base);
1850 }
Chris Wilson9375e442010-09-19 12:21:28 +01001851
Chris Wilsondfaae392010-09-22 10:31:52 +01001852 /* Move everything out of the GPU domains to ensure we do any
1853 * necessary invalidation upon reuse.
1854 */
Chris Wilson77f01232010-09-19 12:31:36 +01001855 list_for_each_entry(obj_priv,
1856 &dev_priv->mm.inactive_list,
1857 list)
1858 {
1859 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1860 }
1861}
1862
Eric Anholt673a3942008-07-30 12:06:12 -07001863/**
1864 * This function clears the request list as sequence numbers are passed.
1865 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001866static void
1867i915_gem_retire_requests_ring(struct drm_device *dev,
1868 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001869{
1870 drm_i915_private_t *dev_priv = dev->dev_private;
1871 uint32_t seqno;
1872
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001873 if (!ring->status_page.page_addr ||
1874 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001875 return;
1876
Chris Wilson23bc5982010-09-29 16:10:57 +01001877 WARN_ON(i915_verify_lists(dev));
1878
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001879 seqno = ring->get_seqno(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001880 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001881 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001882
Zou Nan hai852835f2010-05-21 09:08:56 +08001883 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001884 struct drm_i915_gem_request,
1885 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001886
Chris Wilsondfaae392010-09-22 10:31:52 +01001887 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001888 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001889
1890 trace_i915_gem_request_retire(dev, request->seqno);
1891
1892 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001893 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001894 kfree(request);
1895 }
1896
1897 /* Move any buffers on the active list that are no longer referenced
1898 * by the ringbuffer to the flushing/inactive lists as appropriate.
1899 */
1900 while (!list_empty(&ring->active_list)) {
1901 struct drm_gem_object *obj;
1902 struct drm_i915_gem_object *obj_priv;
1903
1904 obj_priv = list_first_entry(&ring->active_list,
1905 struct drm_i915_gem_object,
1906 list);
1907
Chris Wilsondfaae392010-09-22 10:31:52 +01001908 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001909 break;
1910
1911 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001912 if (obj->write_domain != 0)
1913 i915_gem_object_move_to_flushing(obj);
1914 else
1915 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001916 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001917
1918 if (unlikely (dev_priv->trace_irq_seqno &&
1919 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001920 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001921 dev_priv->trace_irq_seqno = 0;
1922 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001923
1924 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001925}
1926
1927void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001928i915_gem_retire_requests(struct drm_device *dev)
1929{
1930 drm_i915_private_t *dev_priv = dev->dev_private;
1931
Chris Wilsonbe726152010-07-23 23:18:50 +01001932 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1933 struct drm_i915_gem_object *obj_priv, *tmp;
1934
1935 /* We must be careful that during unbind() we do not
1936 * accidentally infinitely recurse into retire requests.
1937 * Currently:
1938 * retire -> free -> unbind -> wait -> retire_ring
1939 */
1940 list_for_each_entry_safe(obj_priv, tmp,
1941 &dev_priv->mm.deferred_free_list,
1942 list)
1943 i915_gem_free_object_tail(&obj_priv->base);
1944 }
1945
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001946 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1947 if (HAS_BSD(dev))
1948 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1949}
1950
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001951static void
Eric Anholt673a3942008-07-30 12:06:12 -07001952i915_gem_retire_work_handler(struct work_struct *work)
1953{
1954 drm_i915_private_t *dev_priv;
1955 struct drm_device *dev;
1956
1957 dev_priv = container_of(work, drm_i915_private_t,
1958 mm.retire_work.work);
1959 dev = dev_priv->dev;
1960
Chris Wilson891b48c2010-09-29 12:26:37 +01001961 /* Come back later if the device is busy... */
1962 if (!mutex_trylock(&dev->struct_mutex)) {
1963 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1964 return;
1965 }
1966
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001967 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001968
Keith Packard6dbe2772008-10-14 21:41:13 -07001969 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001970 (!list_empty(&dev_priv->render_ring.request_list) ||
1971 (HAS_BSD(dev) &&
1972 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001973 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001974 mutex_unlock(&dev->struct_mutex);
1975}
1976
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001977int
Zou Nan hai852835f2010-05-21 09:08:56 +08001978i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001979 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001980{
1981 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001982 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001983 int ret = 0;
1984
1985 BUG_ON(seqno == 0);
1986
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001987 if (atomic_read(&dev_priv->mm.wedged))
1988 return -EAGAIN;
1989
Chris Wilsona56ba562010-09-28 10:07:56 +01001990 if (ring->outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01001991 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001992 if (seqno == 0)
1993 return -ENOMEM;
1994 }
Chris Wilsona56ba562010-09-28 10:07:56 +01001995 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001996
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001997 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001998 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001999 ier = I915_READ(DEIER) | I915_READ(GTIER);
2000 else
2001 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002002 if (!ier) {
2003 DRM_ERROR("something (likely vbetool) disabled "
2004 "interrupts, re-enabling\n");
2005 i915_driver_irq_preinstall(dev);
2006 i915_driver_irq_postinstall(dev);
2007 }
2008
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002009 trace_i915_gem_request_wait_begin(dev, seqno);
2010
Zou Nan hai852835f2010-05-21 09:08:56 +08002011 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002012 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002013 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002014 ret = wait_event_interruptible(ring->irq_queue,
2015 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002016 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002017 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002018 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002019 wait_event(ring->irq_queue,
2020 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002021 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002022 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002023
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002024 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08002025 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002026
2027 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002028 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002029 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002030 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002031
2032 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002033 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002034 __func__, ret, seqno, ring->get_seqno(dev, ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002035 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002036
2037 /* Directly dispatch request retiring. While we have the work queue
2038 * to handle this, the waiter on a request often wants an associated
2039 * buffer to have made it to the inactive list, and we would need
2040 * a separate wait queue to handle that.
2041 */
2042 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002043 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002044
2045 return ret;
2046}
2047
Daniel Vetter48764bf2009-09-15 22:57:32 +02002048/**
2049 * Waits for a sequence number to be signaled, and cleans up the
2050 * request and object lists appropriately for that event.
2051 */
2052static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002053i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002054 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002055{
Zou Nan hai852835f2010-05-21 09:08:56 +08002056 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002057}
2058
Chris Wilson20f0cd52010-09-23 11:00:38 +01002059static void
Chris Wilson92204342010-09-18 11:02:01 +01002060i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002061 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002062 struct intel_ring_buffer *ring,
2063 uint32_t invalidate_domains,
2064 uint32_t flush_domains)
2065{
2066 ring->flush(dev, ring, invalidate_domains, flush_domains);
2067 i915_gem_process_flushing_list(dev, flush_domains, ring);
2068}
2069
2070static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002071i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002072 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002073 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002074 uint32_t flush_domains,
2075 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002076{
2077 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002078
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002079 if (flush_domains & I915_GEM_DOMAIN_CPU)
2080 drm_agp_chipset_flush(dev);
Daniel Vetter8bff9172010-02-11 22:19:40 +01002081
Chris Wilson92204342010-09-18 11:02:01 +01002082 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2083 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002084 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002085 &dev_priv->render_ring,
2086 invalidate_domains, flush_domains);
2087 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002088 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002089 &dev_priv->bsd_ring,
2090 invalidate_domains, flush_domains);
2091 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002092}
2093
Eric Anholt673a3942008-07-30 12:06:12 -07002094/**
2095 * Ensures that all rendering to the object has completed and the object is
2096 * safe to unbind from the GTT or access from the CPU.
2097 */
2098static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002099i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2100 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002101{
2102 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002103 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002104 int ret;
2105
Eric Anholte47c68e2008-11-14 13:35:19 -08002106 /* This function only exists to support waiting for existing rendering,
2107 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002108 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002109 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002110
2111 /* If there is rendering queued on the buffer being evicted, wait for
2112 * it.
2113 */
2114 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002115 ret = i915_do_wait_request(dev,
2116 obj_priv->last_rendering_seqno,
2117 interruptible,
2118 obj_priv->ring);
2119 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002120 return ret;
2121 }
2122
2123 return 0;
2124}
2125
2126/**
2127 * Unbinds an object from the GTT aperture.
2128 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002129int
Eric Anholt673a3942008-07-30 12:06:12 -07002130i915_gem_object_unbind(struct drm_gem_object *obj)
2131{
2132 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002133 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002134 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002135 int ret = 0;
2136
Eric Anholt673a3942008-07-30 12:06:12 -07002137 if (obj_priv->gtt_space == NULL)
2138 return 0;
2139
2140 if (obj_priv->pin_count != 0) {
2141 DRM_ERROR("Attempting to unbind pinned buffer\n");
2142 return -EINVAL;
2143 }
2144
Eric Anholt5323fd02009-09-09 11:50:45 -07002145 /* blow away mappings if mapped through GTT */
2146 i915_gem_release_mmap(obj);
2147
Eric Anholt673a3942008-07-30 12:06:12 -07002148 /* Move the object to the CPU domain to ensure that
2149 * any possible CPU writes while it's not in the GTT
2150 * are flushed when we go to remap it. This will
2151 * also ensure that all pending GPU writes are finished
2152 * before we unbind.
2153 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002154 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002155 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002156 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002157 /* Continue on if we fail due to EIO, the GPU is hung so we
2158 * should be safe and we need to cleanup or else we might
2159 * cause memory corruption through use-after-free.
2160 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002161 if (ret) {
2162 i915_gem_clflush_object(obj);
2163 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2164 }
Eric Anholt673a3942008-07-30 12:06:12 -07002165
Daniel Vetter96b47b62009-12-15 17:50:00 +01002166 /* release the fence reg _after_ flushing */
2167 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2168 i915_gem_clear_fence_reg(obj);
2169
Chris Wilson73aa8082010-09-30 11:46:12 +01002170 drm_unbind_agp(obj_priv->agp_mem);
2171 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002172
Eric Anholt856fa192009-03-19 14:10:50 -07002173 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002174 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002175
Chris Wilson73aa8082010-09-30 11:46:12 +01002176 i915_gem_info_remove_gtt(dev_priv, obj->size);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01002177 list_del_init(&obj_priv->list);
Eric Anholt673a3942008-07-30 12:06:12 -07002178
Chris Wilson73aa8082010-09-30 11:46:12 +01002179 drm_mm_put_block(obj_priv->gtt_space);
2180 obj_priv->gtt_space = NULL;
2181
Chris Wilson963b4832009-09-20 23:03:54 +01002182 if (i915_gem_object_is_purgeable(obj_priv))
2183 i915_gem_object_truncate(obj);
2184
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002185 trace_i915_gem_object_unbind(obj);
2186
Chris Wilson8dc17752010-07-23 23:18:51 +01002187 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002188}
2189
Chris Wilsona56ba562010-09-28 10:07:56 +01002190static int i915_ring_idle(struct drm_device *dev,
2191 struct intel_ring_buffer *ring)
2192{
2193 i915_gem_flush_ring(dev, NULL, ring,
2194 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2195 return i915_wait_request(dev,
2196 i915_gem_next_request_seqno(dev, ring),
2197 ring);
2198}
2199
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002200int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002201i915_gpu_idle(struct drm_device *dev)
2202{
2203 drm_i915_private_t *dev_priv = dev->dev_private;
2204 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002205 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002206
Zou Nan haid1b851f2010-05-21 09:08:57 +08002207 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2208 list_empty(&dev_priv->render_ring.active_list) &&
2209 (!HAS_BSD(dev) ||
2210 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002211 if (lists_empty)
2212 return 0;
2213
2214 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002215 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002216 if (ret)
2217 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002218
2219 if (HAS_BSD(dev)) {
Chris Wilsona56ba562010-09-28 10:07:56 +01002220 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002221 if (ret)
2222 return ret;
2223 }
2224
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002225 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002226}
2227
Chris Wilson5cdf5882010-09-27 15:51:07 +01002228static int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002229i915_gem_object_get_pages(struct drm_gem_object *obj,
2230 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002231{
Daniel Vetter23010e42010-03-08 13:35:02 +01002232 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002233 int page_count, i;
2234 struct address_space *mapping;
2235 struct inode *inode;
2236 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002237
Daniel Vetter778c3542010-05-13 11:49:44 +02002238 BUG_ON(obj_priv->pages_refcount
2239 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2240
Eric Anholt856fa192009-03-19 14:10:50 -07002241 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002242 return 0;
2243
2244 /* Get the list of pages out of our struct file. They'll be pinned
2245 * at this point until we release them.
2246 */
2247 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002248 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002249 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002250 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002251 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002252 return -ENOMEM;
2253 }
2254
2255 inode = obj->filp->f_path.dentry->d_inode;
2256 mapping = inode->i_mapping;
2257 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002258 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002259 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002260 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002261 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002262 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002263 if (IS_ERR(page))
2264 goto err_pages;
2265
Eric Anholt856fa192009-03-19 14:10:50 -07002266 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002267 }
Eric Anholt280b7132009-03-12 16:56:27 -07002268
2269 if (obj_priv->tiling_mode != I915_TILING_NONE)
2270 i915_gem_object_do_bit_17_swizzle(obj);
2271
Eric Anholt673a3942008-07-30 12:06:12 -07002272 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002273
2274err_pages:
2275 while (i--)
2276 page_cache_release(obj_priv->pages[i]);
2277
2278 drm_free_large(obj_priv->pages);
2279 obj_priv->pages = NULL;
2280 obj_priv->pages_refcount--;
2281 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002282}
2283
Eric Anholt4e901fd2009-10-26 16:44:17 -07002284static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2285{
2286 struct drm_gem_object *obj = reg->obj;
2287 struct drm_device *dev = obj->dev;
2288 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002289 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002290 int regnum = obj_priv->fence_reg;
2291 uint64_t val;
2292
2293 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2294 0xfffff000) << 32;
2295 val |= obj_priv->gtt_offset & 0xfffff000;
2296 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2297 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2298
2299 if (obj_priv->tiling_mode == I915_TILING_Y)
2300 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2301 val |= I965_FENCE_REG_VALID;
2302
2303 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2304}
2305
Jesse Barnesde151cf2008-11-12 10:03:55 -08002306static void i965_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);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002312 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 |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2319 if (obj_priv->tiling_mode == I915_TILING_Y)
2320 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2321 val |= I965_FENCE_REG_VALID;
2322
2323 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2324}
2325
2326static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2327{
2328 struct drm_gem_object *obj = reg->obj;
2329 struct drm_device *dev = obj->dev;
2330 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002331 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002332 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002333 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002334 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002335 uint32_t pitch_val;
2336
2337 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2338 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002339 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002340 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002341 return;
2342 }
2343
Jesse Barnes0f973f22009-01-26 17:10:45 -08002344 if (obj_priv->tiling_mode == I915_TILING_Y &&
2345 HAS_128_BYTE_Y_TILING(dev))
2346 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002347 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002348 tile_width = 512;
2349
2350 /* Note: pitch better be a power of two tile widths */
2351 pitch_val = obj_priv->stride / tile_width;
2352 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002353
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002354 if (obj_priv->tiling_mode == I915_TILING_Y &&
2355 HAS_128_BYTE_Y_TILING(dev))
2356 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2357 else
2358 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2359
Jesse Barnesde151cf2008-11-12 10:03:55 -08002360 val = obj_priv->gtt_offset;
2361 if (obj_priv->tiling_mode == I915_TILING_Y)
2362 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2363 val |= I915_FENCE_SIZE_BITS(obj->size);
2364 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2365 val |= I830_FENCE_REG_VALID;
2366
Eric Anholtdc529a42009-03-10 22:34:49 -07002367 if (regnum < 8)
2368 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2369 else
2370 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2371 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002372}
2373
2374static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2375{
2376 struct drm_gem_object *obj = reg->obj;
2377 struct drm_device *dev = obj->dev;
2378 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002379 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002380 int regnum = obj_priv->fence_reg;
2381 uint32_t val;
2382 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002383 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002384
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002385 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002386 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002387 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002388 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002389 return;
2390 }
2391
Eric Anholte76a16d2009-05-26 17:44:56 -07002392 pitch_val = obj_priv->stride / 128;
2393 pitch_val = ffs(pitch_val) - 1;
2394 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2395
Jesse Barnesde151cf2008-11-12 10:03:55 -08002396 val = obj_priv->gtt_offset;
2397 if (obj_priv->tiling_mode == I915_TILING_Y)
2398 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002399 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2400 WARN_ON(fence_size_bits & ~0x00000f00);
2401 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2403 val |= I830_FENCE_REG_VALID;
2404
2405 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002406}
2407
Chris Wilson2cf34d72010-09-14 13:03:28 +01002408static int i915_find_fence_reg(struct drm_device *dev,
2409 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002410{
2411 struct drm_i915_fence_reg *reg = NULL;
2412 struct drm_i915_gem_object *obj_priv = NULL;
2413 struct drm_i915_private *dev_priv = dev->dev_private;
2414 struct drm_gem_object *obj = NULL;
2415 int i, avail, ret;
2416
2417 /* First try to find a free reg */
2418 avail = 0;
2419 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2420 reg = &dev_priv->fence_regs[i];
2421 if (!reg->obj)
2422 return i;
2423
Daniel Vetter23010e42010-03-08 13:35:02 +01002424 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002425 if (!obj_priv->pin_count)
2426 avail++;
2427 }
2428
2429 if (avail == 0)
2430 return -ENOSPC;
2431
2432 /* None available, try to steal one or wait for a user to finish */
2433 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002434 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2435 lru_list) {
2436 obj = reg->obj;
2437 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002438
2439 if (obj_priv->pin_count)
2440 continue;
2441
2442 /* found one! */
2443 i = obj_priv->fence_reg;
2444 break;
2445 }
2446
2447 BUG_ON(i == I915_FENCE_REG_NONE);
2448
2449 /* We only have a reference on obj from the active list. put_fence_reg
2450 * might drop that one, causing a use-after-free in it. So hold a
2451 * private reference to obj like the other callers of put_fence_reg
2452 * (set_tiling ioctl) do. */
2453 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002454 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002455 drm_gem_object_unreference(obj);
2456 if (ret != 0)
2457 return ret;
2458
2459 return i;
2460}
2461
Jesse Barnesde151cf2008-11-12 10:03:55 -08002462/**
2463 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2464 * @obj: object to map through a fence reg
2465 *
2466 * When mapping objects through the GTT, userspace wants to be able to write
2467 * to them without having to worry about swizzling if the object is tiled.
2468 *
2469 * This function walks the fence regs looking for a free one for @obj,
2470 * stealing one if it can't find any.
2471 *
2472 * It then sets up the reg based on the object's properties: address, pitch
2473 * and tiling format.
2474 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002475int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002476i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2477 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002478{
2479 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002480 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002481 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002482 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002483 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002484
Eric Anholta09ba7f2009-08-29 12:49:51 -07002485 /* Just update our place in the LRU if our fence is getting used. */
2486 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002487 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2488 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002489 return 0;
2490 }
2491
Jesse Barnesde151cf2008-11-12 10:03:55 -08002492 switch (obj_priv->tiling_mode) {
2493 case I915_TILING_NONE:
2494 WARN(1, "allocating a fence for non-tiled object?\n");
2495 break;
2496 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002497 if (!obj_priv->stride)
2498 return -EINVAL;
2499 WARN((obj_priv->stride & (512 - 1)),
2500 "object 0x%08x is X tiled but has non-512B pitch\n",
2501 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002502 break;
2503 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002504 if (!obj_priv->stride)
2505 return -EINVAL;
2506 WARN((obj_priv->stride & (128 - 1)),
2507 "object 0x%08x is Y tiled but has non-128B pitch\n",
2508 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002509 break;
2510 }
2511
Chris Wilson2cf34d72010-09-14 13:03:28 +01002512 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002513 if (ret < 0)
2514 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002515
Daniel Vetterae3db242010-02-19 11:51:58 +01002516 obj_priv->fence_reg = ret;
2517 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002518 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002519
Jesse Barnesde151cf2008-11-12 10:03:55 -08002520 reg->obj = obj;
2521
Chris Wilsone259bef2010-09-17 00:32:02 +01002522 switch (INTEL_INFO(dev)->gen) {
2523 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002524 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002525 break;
2526 case 5:
2527 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002528 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002529 break;
2530 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002531 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002532 break;
2533 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002534 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002535 break;
2536 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002537
Daniel Vetterae3db242010-02-19 11:51:58 +01002538 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2539 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002540
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002541 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002542}
2543
2544/**
2545 * i915_gem_clear_fence_reg - clear out fence register info
2546 * @obj: object to clear
2547 *
2548 * Zeroes out the fence register itself and clears out the associated
2549 * data structures in dev_priv and obj_priv.
2550 */
2551static void
2552i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2553{
2554 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002555 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002556 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002557 struct drm_i915_fence_reg *reg =
2558 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002559 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002560
Chris Wilsone259bef2010-09-17 00:32:02 +01002561 switch (INTEL_INFO(dev)->gen) {
2562 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002563 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2564 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002565 break;
2566 case 5:
2567 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002568 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002569 break;
2570 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002571 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002572 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002573 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002574 case 2:
2575 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002576
2577 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002578 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002579 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002580
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002581 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002582 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002583 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002584}
2585
Eric Anholt673a3942008-07-30 12:06:12 -07002586/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002587 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2588 * to the buffer to finish, and then resets the fence register.
2589 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002590 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002591 *
2592 * Zeroes out the fence register itself and clears out the associated
2593 * data structures in dev_priv and obj_priv.
2594 */
2595int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002596i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2597 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002598{
2599 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002600 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002601 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002602 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002603
2604 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2605 return 0;
2606
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002607 /* If we've changed tiling, GTT-mappings of the object
2608 * need to re-fault to ensure that the correct fence register
2609 * setup is in place.
2610 */
2611 i915_gem_release_mmap(obj);
2612
Chris Wilson52dc7d32009-06-06 09:46:01 +01002613 /* On the i915, GPU access to tiled buffers is via a fence,
2614 * therefore we must wait for any outstanding access to complete
2615 * before clearing the fence.
2616 */
Chris Wilson53640e12010-09-20 11:40:50 +01002617 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2618 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002619 int ret;
2620
Chris Wilson2cf34d72010-09-14 13:03:28 +01002621 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002622 if (ret)
2623 return ret;
2624
Chris Wilson2cf34d72010-09-14 13:03:28 +01002625 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002626 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002627 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002628
2629 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002630 }
2631
Daniel Vetter4a726612010-02-01 13:59:16 +01002632 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002633 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002634
2635 return 0;
2636}
2637
2638/**
Eric Anholt673a3942008-07-30 12:06:12 -07002639 * Finds free space in the GTT aperture and binds the object there.
2640 */
2641static int
2642i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2643{
2644 struct drm_device *dev = obj->dev;
2645 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002646 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002647 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002648 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002649 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002650
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002651 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002652 DRM_ERROR("Attempting to bind a purgeable object\n");
2653 return -EINVAL;
2654 }
2655
Eric Anholt673a3942008-07-30 12:06:12 -07002656 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002657 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002658 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002659 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2660 return -EINVAL;
2661 }
2662
Chris Wilson654fc602010-05-27 13:18:21 +01002663 /* If the object is bigger than the entire aperture, reject it early
2664 * before evicting everything in a vain attempt to find space.
2665 */
Chris Wilson73aa8082010-09-30 11:46:12 +01002666 if (obj->size > dev_priv->mm.gtt_total) {
Chris Wilson654fc602010-05-27 13:18:21 +01002667 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2668 return -E2BIG;
2669 }
2670
Eric Anholt673a3942008-07-30 12:06:12 -07002671 search_free:
2672 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2673 obj->size, alignment, 0);
2674 if (free_space != NULL) {
2675 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2676 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002677 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002678 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002679 }
2680 if (obj_priv->gtt_space == NULL) {
2681 /* If the gtt is empty and we're still having trouble
2682 * fitting our object in, we're out of memory.
2683 */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002684 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002685 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002686 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002687
Eric Anholt673a3942008-07-30 12:06:12 -07002688 goto search_free;
2689 }
2690
Chris Wilson4bdadb92010-01-27 13:36:32 +00002691 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002692 if (ret) {
2693 drm_mm_put_block(obj_priv->gtt_space);
2694 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002695
2696 if (ret == -ENOMEM) {
2697 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002698 ret = i915_gem_evict_something(dev, obj->size,
2699 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002700 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002701 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002702 if (gfpmask) {
2703 gfpmask = 0;
2704 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002705 }
2706
2707 return ret;
2708 }
2709
2710 goto search_free;
2711 }
2712
Eric Anholt673a3942008-07-30 12:06:12 -07002713 return ret;
2714 }
2715
Eric Anholt673a3942008-07-30 12:06:12 -07002716 /* Create an AGP memory structure pointing at our pages, and bind it
2717 * into the GTT.
2718 */
2719 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002720 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002721 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002722 obj_priv->gtt_offset,
2723 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002724 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002725 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002726 drm_mm_put_block(obj_priv->gtt_space);
2727 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002728
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002729 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002730 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002731 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002732
2733 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002734 }
Eric Anholt673a3942008-07-30 12:06:12 -07002735
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002736 /* keep track of bounds object by adding it to the inactive list */
2737 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01002738 i915_gem_info_add_gtt(dev_priv, obj->size);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002739
Eric Anholt673a3942008-07-30 12:06:12 -07002740 /* Assert that the object is not currently in any GPU domain. As it
2741 * wasn't in the GTT, there shouldn't be any way it could have been in
2742 * a GPU cache
2743 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002744 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2745 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002746
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002747 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2748
Eric Anholt673a3942008-07-30 12:06:12 -07002749 return 0;
2750}
2751
2752void
2753i915_gem_clflush_object(struct drm_gem_object *obj)
2754{
Daniel Vetter23010e42010-03-08 13:35:02 +01002755 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002756
2757 /* If we don't have a page list set up, then we're not pinned
2758 * to GPU, and we can ignore the cache flush because it'll happen
2759 * again at bind time.
2760 */
Eric Anholt856fa192009-03-19 14:10:50 -07002761 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002762 return;
2763
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002764 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002765
Eric Anholt856fa192009-03-19 14:10:50 -07002766 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002767}
2768
Eric Anholte47c68e2008-11-14 13:35:19 -08002769/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002770static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002771i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2772 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002773{
2774 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002775 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002776
2777 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002778 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002779
2780 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002781 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002782 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002783 to_intel_bo(obj)->ring,
2784 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002785 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002786
2787 trace_i915_gem_object_change_domain(obj,
2788 obj->read_domains,
2789 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002790
2791 if (pipelined)
2792 return 0;
2793
Chris Wilson2cf34d72010-09-14 13:03:28 +01002794 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002795}
2796
2797/** Flushes the GTT write domain for the object if it's dirty. */
2798static void
2799i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2800{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002801 uint32_t old_write_domain;
2802
Eric Anholte47c68e2008-11-14 13:35:19 -08002803 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2804 return;
2805
2806 /* No actual flushing is required for the GTT write domain. Writes
2807 * to it immediately go to main memory as far as we know, so there's
2808 * no chipset flush. It also doesn't land in render cache.
2809 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002810 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002811 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002812
2813 trace_i915_gem_object_change_domain(obj,
2814 obj->read_domains,
2815 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002816}
2817
2818/** Flushes the CPU write domain for the object if it's dirty. */
2819static void
2820i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2821{
2822 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002823 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002824
2825 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2826 return;
2827
2828 i915_gem_clflush_object(obj);
2829 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002830 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002831 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002832
2833 trace_i915_gem_object_change_domain(obj,
2834 obj->read_domains,
2835 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002836}
2837
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002838/**
2839 * Moves a single object to the GTT read, and possibly write domain.
2840 *
2841 * This function returns when the move is complete, including waiting on
2842 * flushes to occur.
2843 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002844int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002845i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2846{
Daniel Vetter23010e42010-03-08 13:35:02 +01002847 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002848 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002849 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002850
Eric Anholt02354392008-11-26 13:58:13 -08002851 /* Not valid to be called on unbound objects. */
2852 if (obj_priv->gtt_space == NULL)
2853 return -EINVAL;
2854
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002855 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002856 if (ret != 0)
2857 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002858
Chris Wilson72133422010-09-13 23:56:38 +01002859 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002860
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002861 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002862 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002863 if (ret)
2864 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002865 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002866
Chris Wilson72133422010-09-13 23:56:38 +01002867 old_write_domain = obj->write_domain;
2868 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002869
2870 /* It should now be out of any other write domains, and we can update
2871 * the domain values for our changes.
2872 */
2873 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2874 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002875 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002876 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002877 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002878 obj_priv->dirty = 1;
2879 }
2880
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002881 trace_i915_gem_object_change_domain(obj,
2882 old_read_domains,
2883 old_write_domain);
2884
Eric Anholte47c68e2008-11-14 13:35:19 -08002885 return 0;
2886}
2887
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002888/*
2889 * Prepare buffer for display plane. Use uninterruptible for possible flush
2890 * wait, as in modesetting process we're not supposed to be interrupted.
2891 */
2892int
Chris Wilson48b956c2010-09-14 12:50:34 +01002893i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2894 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002895{
Daniel Vetter23010e42010-03-08 13:35:02 +01002896 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002897 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002898 int ret;
2899
2900 /* Not valid to be called on unbound objects. */
2901 if (obj_priv->gtt_space == NULL)
2902 return -EINVAL;
2903
Chris Wilsonced270f2010-09-26 22:47:46 +01002904 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson48b956c2010-09-14 12:50:34 +01002905 if (ret)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002906 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002907
Chris Wilsonced270f2010-09-26 22:47:46 +01002908 /* Currently, we are always called from an non-interruptible context. */
2909 if (!pipelined) {
2910 ret = i915_gem_object_wait_rendering(obj, false);
2911 if (ret)
2912 return ret;
2913 }
2914
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002915 i915_gem_object_flush_cpu_write_domain(obj);
2916
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002917 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002918 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002919
2920 trace_i915_gem_object_change_domain(obj,
2921 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002922 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002923
2924 return 0;
2925}
2926
Eric Anholte47c68e2008-11-14 13:35:19 -08002927/**
2928 * Moves a single object to the CPU read, and possibly write domain.
2929 *
2930 * This function returns when the move is complete, including waiting on
2931 * flushes to occur.
2932 */
2933static int
2934i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2935{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002936 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002937 int ret;
2938
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002939 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002940 if (ret != 0)
2941 return ret;
2942
2943 i915_gem_object_flush_gtt_write_domain(obj);
2944
2945 /* If we have a partially-valid cache of the object in the CPU,
2946 * finish invalidating it and free the per-page flags.
2947 */
2948 i915_gem_object_set_to_full_cpu_read_domain(obj);
2949
Chris Wilson72133422010-09-13 23:56:38 +01002950 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002951 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002952 if (ret)
2953 return ret;
2954 }
2955
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002956 old_write_domain = obj->write_domain;
2957 old_read_domains = obj->read_domains;
2958
Eric Anholte47c68e2008-11-14 13:35:19 -08002959 /* Flush the CPU cache if it's still invalid. */
2960 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2961 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002962
2963 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2964 }
2965
2966 /* It should now be out of any other write domains, and we can update
2967 * the domain values for our changes.
2968 */
2969 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2970
2971 /* If we're writing through the CPU, then the GPU read domains will
2972 * need to be invalidated at next use.
2973 */
2974 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002975 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002976 obj->write_domain = I915_GEM_DOMAIN_CPU;
2977 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002978
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002979 trace_i915_gem_object_change_domain(obj,
2980 old_read_domains,
2981 old_write_domain);
2982
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002983 return 0;
2984}
2985
Eric Anholt673a3942008-07-30 12:06:12 -07002986/*
2987 * Set the next domain for the specified object. This
2988 * may not actually perform the necessary flushing/invaliding though,
2989 * as that may want to be batched with other set_domain operations
2990 *
2991 * This is (we hope) the only really tricky part of gem. The goal
2992 * is fairly simple -- track which caches hold bits of the object
2993 * and make sure they remain coherent. A few concrete examples may
2994 * help to explain how it works. For shorthand, we use the notation
2995 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2996 * a pair of read and write domain masks.
2997 *
2998 * Case 1: the batch buffer
2999 *
3000 * 1. Allocated
3001 * 2. Written by CPU
3002 * 3. Mapped to GTT
3003 * 4. Read by GPU
3004 * 5. Unmapped from GTT
3005 * 6. Freed
3006 *
3007 * Let's take these a step at a time
3008 *
3009 * 1. Allocated
3010 * Pages allocated from the kernel may still have
3011 * cache contents, so we set them to (CPU, CPU) always.
3012 * 2. Written by CPU (using pwrite)
3013 * The pwrite function calls set_domain (CPU, CPU) and
3014 * this function does nothing (as nothing changes)
3015 * 3. Mapped by GTT
3016 * This function asserts that the object is not
3017 * currently in any GPU-based read or write domains
3018 * 4. Read by GPU
3019 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3020 * As write_domain is zero, this function adds in the
3021 * current read domains (CPU+COMMAND, 0).
3022 * flush_domains is set to CPU.
3023 * invalidate_domains is set to COMMAND
3024 * clflush is run to get data out of the CPU caches
3025 * then i915_dev_set_domain calls i915_gem_flush to
3026 * emit an MI_FLUSH and drm_agp_chipset_flush
3027 * 5. Unmapped from GTT
3028 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3029 * flush_domains and invalidate_domains end up both zero
3030 * so no flushing/invalidating happens
3031 * 6. Freed
3032 * yay, done
3033 *
3034 * Case 2: The shared render buffer
3035 *
3036 * 1. Allocated
3037 * 2. Mapped to GTT
3038 * 3. Read/written by GPU
3039 * 4. set_domain to (CPU,CPU)
3040 * 5. Read/written by CPU
3041 * 6. Read/written by GPU
3042 *
3043 * 1. Allocated
3044 * Same as last example, (CPU, CPU)
3045 * 2. Mapped to GTT
3046 * Nothing changes (assertions find that it is not in the GPU)
3047 * 3. Read/written by GPU
3048 * execbuffer calls set_domain (RENDER, RENDER)
3049 * flush_domains gets CPU
3050 * invalidate_domains gets GPU
3051 * clflush (obj)
3052 * MI_FLUSH and drm_agp_chipset_flush
3053 * 4. set_domain (CPU, CPU)
3054 * flush_domains gets GPU
3055 * invalidate_domains gets CPU
3056 * wait_rendering (obj) to make sure all drawing is complete.
3057 * This will include an MI_FLUSH to get the data from GPU
3058 * to memory
3059 * clflush (obj) to invalidate the CPU cache
3060 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3061 * 5. Read/written by CPU
3062 * cache lines are loaded and dirtied
3063 * 6. Read written by GPU
3064 * Same as last GPU access
3065 *
3066 * Case 3: The constant buffer
3067 *
3068 * 1. Allocated
3069 * 2. Written by CPU
3070 * 3. Read by GPU
3071 * 4. Updated (written) by CPU again
3072 * 5. Read by GPU
3073 *
3074 * 1. Allocated
3075 * (CPU, CPU)
3076 * 2. Written by CPU
3077 * (CPU, CPU)
3078 * 3. Read by GPU
3079 * (CPU+RENDER, 0)
3080 * flush_domains = CPU
3081 * invalidate_domains = RENDER
3082 * clflush (obj)
3083 * MI_FLUSH
3084 * drm_agp_chipset_flush
3085 * 4. Updated (written) by CPU again
3086 * (CPU, CPU)
3087 * flush_domains = 0 (no previous write domain)
3088 * invalidate_domains = 0 (no new read domains)
3089 * 5. Read by GPU
3090 * (CPU+RENDER, 0)
3091 * flush_domains = CPU
3092 * invalidate_domains = RENDER
3093 * clflush (obj)
3094 * MI_FLUSH
3095 * drm_agp_chipset_flush
3096 */
Keith Packardc0d90822008-11-20 23:11:08 -08003097static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003098i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003099{
3100 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003101 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003102 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003103 uint32_t invalidate_domains = 0;
3104 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003105 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003106
Eric Anholt8b0e3782009-02-19 14:40:50 -08003107 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3108 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003109
Jesse Barnes652c3932009-08-17 13:31:43 -07003110 intel_mark_busy(dev, obj);
3111
Eric Anholt673a3942008-07-30 12:06:12 -07003112 /*
3113 * If the object isn't moving to a new write domain,
3114 * let the object stay in multiple read domains
3115 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003116 if (obj->pending_write_domain == 0)
3117 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003118 else
3119 obj_priv->dirty = 1;
3120
3121 /*
3122 * Flush the current write domain if
3123 * the new read domains don't match. Invalidate
3124 * any read domains which differ from the old
3125 * write domain
3126 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003127 if (obj->write_domain &&
3128 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003129 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003130 invalidate_domains |=
3131 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003132 }
3133 /*
3134 * Invalidate any read caches which may have
3135 * stale data. That is, any new read domains.
3136 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003137 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003138 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003139 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003140
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003141 old_read_domains = obj->read_domains;
3142
Eric Anholtefbeed92009-02-19 14:54:51 -08003143 /* The actual obj->write_domain will be updated with
3144 * pending_write_domain after we emit the accumulated flush for all
3145 * of our domain changes in execbuffers (which clears objects'
3146 * write_domains). So if we have a current write domain that we
3147 * aren't changing, set pending_write_domain to that.
3148 */
3149 if (flush_domains == 0 && obj->pending_write_domain == 0)
3150 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003151 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003152
3153 dev->invalidate_domains |= invalidate_domains;
3154 dev->flush_domains |= flush_domains;
Chris Wilson92204342010-09-18 11:02:01 +01003155 if (obj_priv->ring)
3156 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003157
3158 trace_i915_gem_object_change_domain(obj,
3159 old_read_domains,
3160 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003161}
3162
3163/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003164 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003165 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003166 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3167 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3168 */
3169static void
3170i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3171{
Daniel Vetter23010e42010-03-08 13:35:02 +01003172 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003173
3174 if (!obj_priv->page_cpu_valid)
3175 return;
3176
3177 /* If we're partially in the CPU read domain, finish moving it in.
3178 */
3179 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3180 int i;
3181
3182 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3183 if (obj_priv->page_cpu_valid[i])
3184 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003185 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003186 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003187 }
3188
3189 /* Free the page_cpu_valid mappings which are now stale, whether
3190 * or not we've got I915_GEM_DOMAIN_CPU.
3191 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003192 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003193 obj_priv->page_cpu_valid = NULL;
3194}
3195
3196/**
3197 * Set the CPU read domain on a range of the object.
3198 *
3199 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3200 * not entirely valid. The page_cpu_valid member of the object flags which
3201 * pages have been flushed, and will be respected by
3202 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3203 * of the whole object.
3204 *
3205 * This function returns when the move is complete, including waiting on
3206 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003207 */
3208static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003209i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3210 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003211{
Daniel Vetter23010e42010-03-08 13:35:02 +01003212 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003213 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003214 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003215
Eric Anholte47c68e2008-11-14 13:35:19 -08003216 if (offset == 0 && size == obj->size)
3217 return i915_gem_object_set_to_cpu_domain(obj, 0);
3218
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003219 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003220 if (ret != 0)
3221 return ret;
3222 i915_gem_object_flush_gtt_write_domain(obj);
3223
3224 /* If we're already fully in the CPU read domain, we're done. */
3225 if (obj_priv->page_cpu_valid == NULL &&
3226 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003227 return 0;
3228
Eric Anholte47c68e2008-11-14 13:35:19 -08003229 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3230 * newly adding I915_GEM_DOMAIN_CPU
3231 */
Eric Anholt673a3942008-07-30 12:06:12 -07003232 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003233 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3234 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003235 if (obj_priv->page_cpu_valid == NULL)
3236 return -ENOMEM;
3237 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3238 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003239
3240 /* Flush the cache on any pages that are still invalid from the CPU's
3241 * perspective.
3242 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003243 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3244 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003245 if (obj_priv->page_cpu_valid[i])
3246 continue;
3247
Eric Anholt856fa192009-03-19 14:10:50 -07003248 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003249
3250 obj_priv->page_cpu_valid[i] = 1;
3251 }
3252
Eric Anholte47c68e2008-11-14 13:35:19 -08003253 /* It should now be out of any other write domains, and we can update
3254 * the domain values for our changes.
3255 */
3256 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3257
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003258 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003259 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3260
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003261 trace_i915_gem_object_change_domain(obj,
3262 old_read_domains,
3263 obj->write_domain);
3264
Eric Anholt673a3942008-07-30 12:06:12 -07003265 return 0;
3266}
3267
3268/**
Eric Anholt673a3942008-07-30 12:06:12 -07003269 * Pin an object to the GTT and evaluate the relocations landing in it.
3270 */
3271static int
3272i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3273 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003274 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003275 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003276{
3277 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003278 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003279 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003280 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003281 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003282 bool need_fence;
3283
3284 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3285 obj_priv->tiling_mode != I915_TILING_NONE;
3286
3287 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003288 if (need_fence &&
3289 !i915_gem_object_fence_offset_ok(obj,
3290 obj_priv->tiling_mode)) {
3291 ret = i915_gem_object_unbind(obj);
3292 if (ret)
3293 return ret;
3294 }
Eric Anholt673a3942008-07-30 12:06:12 -07003295
3296 /* Choose the GTT offset for our buffer and put it there. */
3297 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3298 if (ret)
3299 return ret;
3300
Jesse Barnes76446ca2009-12-17 22:05:42 -05003301 /*
3302 * Pre-965 chips need a fence register set up in order to
3303 * properly handle blits to/from tiled surfaces.
3304 */
3305 if (need_fence) {
Chris Wilson53640e12010-09-20 11:40:50 +01003306 ret = i915_gem_object_get_fence_reg(obj, true);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003307 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003308 i915_gem_object_unpin(obj);
3309 return ret;
3310 }
Chris Wilson53640e12010-09-20 11:40:50 +01003311
3312 dev_priv->fence_regs[obj_priv->fence_reg].gpu = true;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003313 }
3314
Eric Anholt673a3942008-07-30 12:06:12 -07003315 entry->offset = obj_priv->gtt_offset;
3316
Eric Anholt673a3942008-07-30 12:06:12 -07003317 /* Apply the relocations, using the GTT aperture to avoid cache
3318 * flushing requirements.
3319 */
3320 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003321 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003322 struct drm_gem_object *target_obj;
3323 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003324 uint32_t reloc_val, reloc_offset;
3325 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003326
Eric Anholt673a3942008-07-30 12:06:12 -07003327 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003328 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003329 if (target_obj == NULL) {
3330 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003331 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003332 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003333 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003334
Chris Wilson8542a0b2009-09-09 21:15:15 +01003335#if WATCH_RELOC
3336 DRM_INFO("%s: obj %p offset %08x target %d "
3337 "read %08x write %08x gtt %08x "
3338 "presumed %08x delta %08x\n",
3339 __func__,
3340 obj,
3341 (int) reloc->offset,
3342 (int) reloc->target_handle,
3343 (int) reloc->read_domains,
3344 (int) reloc->write_domain,
3345 (int) target_obj_priv->gtt_offset,
3346 (int) reloc->presumed_offset,
3347 reloc->delta);
3348#endif
3349
Eric Anholt673a3942008-07-30 12:06:12 -07003350 /* The target buffer should have appeared before us in the
3351 * exec_object list, so it should have a GTT space bound by now.
3352 */
3353 if (target_obj_priv->gtt_space == NULL) {
3354 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003355 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003356 drm_gem_object_unreference(target_obj);
3357 i915_gem_object_unpin(obj);
3358 return -EINVAL;
3359 }
3360
Chris Wilson8542a0b2009-09-09 21:15:15 +01003361 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003362 if (reloc->write_domain & (reloc->write_domain - 1)) {
3363 DRM_ERROR("reloc with multiple write domains: "
3364 "obj %p target %d offset %d "
3365 "read %08x write %08x",
3366 obj, reloc->target_handle,
3367 (int) reloc->offset,
3368 reloc->read_domains,
3369 reloc->write_domain);
3370 return -EINVAL;
3371 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003372 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3373 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3374 DRM_ERROR("reloc with read/write CPU domains: "
3375 "obj %p target %d offset %d "
3376 "read %08x write %08x",
3377 obj, reloc->target_handle,
3378 (int) reloc->offset,
3379 reloc->read_domains,
3380 reloc->write_domain);
3381 drm_gem_object_unreference(target_obj);
3382 i915_gem_object_unpin(obj);
3383 return -EINVAL;
3384 }
3385 if (reloc->write_domain && target_obj->pending_write_domain &&
3386 reloc->write_domain != target_obj->pending_write_domain) {
3387 DRM_ERROR("Write domain conflict: "
3388 "obj %p target %d offset %d "
3389 "new %08x old %08x\n",
3390 obj, reloc->target_handle,
3391 (int) reloc->offset,
3392 reloc->write_domain,
3393 target_obj->pending_write_domain);
3394 drm_gem_object_unreference(target_obj);
3395 i915_gem_object_unpin(obj);
3396 return -EINVAL;
3397 }
3398
3399 target_obj->pending_read_domains |= reloc->read_domains;
3400 target_obj->pending_write_domain |= reloc->write_domain;
3401
3402 /* If the relocation already has the right value in it, no
3403 * more work needs to be done.
3404 */
3405 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3406 drm_gem_object_unreference(target_obj);
3407 continue;
3408 }
3409
3410 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003411 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003412 DRM_ERROR("Relocation beyond object bounds: "
3413 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003414 obj, reloc->target_handle,
3415 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003416 drm_gem_object_unreference(target_obj);
3417 i915_gem_object_unpin(obj);
3418 return -EINVAL;
3419 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003420 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003421 DRM_ERROR("Relocation not 4-byte aligned: "
3422 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003423 obj, reloc->target_handle,
3424 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003425 drm_gem_object_unreference(target_obj);
3426 i915_gem_object_unpin(obj);
3427 return -EINVAL;
3428 }
3429
Chris Wilson8542a0b2009-09-09 21:15:15 +01003430 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003431 if (reloc->delta >= target_obj->size) {
3432 DRM_ERROR("Relocation beyond target object bounds: "
3433 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003434 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003435 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003436 drm_gem_object_unreference(target_obj);
3437 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003438 return -EINVAL;
3439 }
3440
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003441 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3442 if (ret != 0) {
3443 drm_gem_object_unreference(target_obj);
3444 i915_gem_object_unpin(obj);
3445 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003446 }
3447
3448 /* Map the page containing the relocation we're going to
3449 * perform.
3450 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003451 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003452 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3453 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003454 ~(PAGE_SIZE - 1)),
3455 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003456 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003457 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003458 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003459
Eric Anholt673a3942008-07-30 12:06:12 -07003460 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003461 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003462
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003463 /* The updated presumed offset for this entry will be
3464 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003465 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003466 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003467
3468 drm_gem_object_unreference(target_obj);
3469 }
3470
Eric Anholt673a3942008-07-30 12:06:12 -07003471 return 0;
3472}
3473
Eric Anholt673a3942008-07-30 12:06:12 -07003474/* Throttle our rendering by waiting until the ring has completed our requests
3475 * emitted over 20 msec ago.
3476 *
Eric Anholtb9624422009-06-03 07:27:35 +00003477 * Note that if we were to use the current jiffies each time around the loop,
3478 * we wouldn't escape the function with any frames outstanding if the time to
3479 * render a frame was over 20ms.
3480 *
Eric Anholt673a3942008-07-30 12:06:12 -07003481 * This should get us reasonable parallelism between CPU and GPU but also
3482 * relatively low latency when blocking on a particular request to finish.
3483 */
3484static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003485i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003486{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003487 struct drm_i915_private *dev_priv = dev->dev_private;
3488 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003489 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003490 struct drm_i915_gem_request *request;
3491 struct intel_ring_buffer *ring = NULL;
3492 u32 seqno = 0;
3493 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003494
Chris Wilson1c255952010-09-26 11:03:27 +01003495 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003496 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003497 if (time_after_eq(request->emitted_jiffies, recent_enough))
3498 break;
3499
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003500 ring = request->ring;
3501 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003502 }
Chris Wilson1c255952010-09-26 11:03:27 +01003503 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003504
3505 if (seqno == 0)
3506 return 0;
3507
3508 ret = 0;
3509 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3510 /* And wait for the seqno passing without holding any locks and
3511 * causing extra latency for others. This is safe as the irq
3512 * generation is designed to be run atomically and so is
3513 * lockless.
3514 */
3515 ring->user_irq_get(dev, ring);
3516 ret = wait_event_interruptible(ring->irq_queue,
3517 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3518 || atomic_read(&dev_priv->mm.wedged));
3519 ring->user_irq_put(dev, ring);
3520
3521 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3522 ret = -EIO;
3523 }
3524
3525 if (ret == 0)
3526 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003527
Eric Anholt673a3942008-07-30 12:06:12 -07003528 return ret;
3529}
3530
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003531static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003532i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003533 uint32_t buffer_count,
3534 struct drm_i915_gem_relocation_entry **relocs)
3535{
3536 uint32_t reloc_count = 0, reloc_index = 0, i;
3537 int ret;
3538
3539 *relocs = NULL;
3540 for (i = 0; i < buffer_count; i++) {
3541 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3542 return -EINVAL;
3543 reloc_count += exec_list[i].relocation_count;
3544 }
3545
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003546 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003547 if (*relocs == NULL) {
3548 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003549 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003550 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003551
3552 for (i = 0; i < buffer_count; i++) {
3553 struct drm_i915_gem_relocation_entry __user *user_relocs;
3554
3555 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3556
3557 ret = copy_from_user(&(*relocs)[reloc_index],
3558 user_relocs,
3559 exec_list[i].relocation_count *
3560 sizeof(**relocs));
3561 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003562 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003563 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003564 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003565 }
3566
3567 reloc_index += exec_list[i].relocation_count;
3568 }
3569
Florian Mickler2bc43b52009-04-06 22:55:41 +02003570 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003571}
3572
3573static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003574i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003575 uint32_t buffer_count,
3576 struct drm_i915_gem_relocation_entry *relocs)
3577{
3578 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003579 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003580
Chris Wilson93533c22010-01-31 10:40:48 +00003581 if (relocs == NULL)
3582 return 0;
3583
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003584 for (i = 0; i < buffer_count; i++) {
3585 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003586 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003587
3588 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3589
Florian Mickler2bc43b52009-04-06 22:55:41 +02003590 unwritten = copy_to_user(user_relocs,
3591 &relocs[reloc_count],
3592 exec_list[i].relocation_count *
3593 sizeof(*relocs));
3594
3595 if (unwritten) {
3596 ret = -EFAULT;
3597 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003598 }
3599
3600 reloc_count += exec_list[i].relocation_count;
3601 }
3602
Florian Mickler2bc43b52009-04-06 22:55:41 +02003603err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003604 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003605
3606 return ret;
3607}
3608
Chris Wilson83d60792009-06-06 09:45:57 +01003609static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003610i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003611 uint64_t exec_offset)
3612{
3613 uint32_t exec_start, exec_len;
3614
3615 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3616 exec_len = (uint32_t) exec->batch_len;
3617
3618 if ((exec_start | exec_len) & 0x7)
3619 return -EINVAL;
3620
3621 if (!exec_start)
3622 return -EINVAL;
3623
3624 return 0;
3625}
3626
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003627static int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003628i915_gem_wait_for_pending_flip(struct drm_device *dev,
3629 struct drm_gem_object **object_list,
3630 int count)
3631{
3632 drm_i915_private_t *dev_priv = dev->dev_private;
3633 struct drm_i915_gem_object *obj_priv;
3634 DEFINE_WAIT(wait);
3635 int i, ret = 0;
3636
3637 for (;;) {
3638 prepare_to_wait(&dev_priv->pending_flip_queue,
3639 &wait, TASK_INTERRUPTIBLE);
3640 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003641 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003642 if (atomic_read(&obj_priv->pending_flip) > 0)
3643 break;
3644 }
3645 if (i == count)
3646 break;
3647
3648 if (!signal_pending(current)) {
3649 mutex_unlock(&dev->struct_mutex);
3650 schedule();
3651 mutex_lock(&dev->struct_mutex);
3652 continue;
3653 }
3654 ret = -ERESTARTSYS;
3655 break;
3656 }
3657 finish_wait(&dev_priv->pending_flip_queue, &wait);
3658
3659 return ret;
3660}
3661
Chris Wilson8dc5d142010-08-12 12:36:12 +01003662static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003663i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3664 struct drm_file *file_priv,
3665 struct drm_i915_gem_execbuffer2 *args,
3666 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003667{
3668 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003669 struct drm_gem_object **object_list = NULL;
3670 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003671 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003672 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003673 struct drm_i915_gem_relocation_entry *relocs = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003674 struct drm_i915_gem_request *request = NULL;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003675 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003676 uint64_t exec_offset;
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003677 uint32_t reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003678 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003679
Zou Nan hai852835f2010-05-21 09:08:56 +08003680 struct intel_ring_buffer *ring = NULL;
3681
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003682 ret = i915_gem_check_is_wedged(dev);
3683 if (ret)
3684 return ret;
3685
Eric Anholt673a3942008-07-30 12:06:12 -07003686#if WATCH_EXEC
3687 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3688 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3689#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003690 if (args->flags & I915_EXEC_BSD) {
3691 if (!HAS_BSD(dev)) {
3692 DRM_ERROR("execbuf with wrong flag\n");
3693 return -EINVAL;
3694 }
3695 ring = &dev_priv->bsd_ring;
3696 } else {
3697 ring = &dev_priv->render_ring;
3698 }
3699
Eric Anholt4f481ed2008-09-10 14:22:49 -07003700 if (args->buffer_count < 1) {
3701 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3702 return -EINVAL;
3703 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003704 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003705 if (object_list == NULL) {
3706 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003707 args->buffer_count);
3708 ret = -ENOMEM;
3709 goto pre_mutex_err;
3710 }
Eric Anholt673a3942008-07-30 12:06:12 -07003711
Eric Anholt201361a2009-03-11 12:30:04 -07003712 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003713 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3714 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003715 if (cliprects == NULL) {
3716 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003717 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003718 }
Eric Anholt201361a2009-03-11 12:30:04 -07003719
3720 ret = copy_from_user(cliprects,
3721 (struct drm_clip_rect __user *)
3722 (uintptr_t) args->cliprects_ptr,
3723 sizeof(*cliprects) * args->num_cliprects);
3724 if (ret != 0) {
3725 DRM_ERROR("copy %d cliprects failed: %d\n",
3726 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003727 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003728 goto pre_mutex_err;
3729 }
3730 }
3731
Chris Wilson8dc5d142010-08-12 12:36:12 +01003732 request = kzalloc(sizeof(*request), GFP_KERNEL);
3733 if (request == NULL) {
3734 ret = -ENOMEM;
3735 goto pre_mutex_err;
3736 }
3737
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003738 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3739 &relocs);
3740 if (ret != 0)
3741 goto pre_mutex_err;
3742
Chris Wilson76c1dec2010-09-25 11:22:51 +01003743 ret = i915_mutex_lock_interruptible(dev);
3744 if (ret)
3745 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003746
Eric Anholt673a3942008-07-30 12:06:12 -07003747 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003748 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003749 ret = -EBUSY;
3750 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003751 }
3752
Keith Packardac94a962008-11-20 23:30:27 -08003753 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003754 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003755 for (i = 0; i < args->buffer_count; i++) {
3756 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3757 exec_list[i].handle);
3758 if (object_list[i] == NULL) {
3759 DRM_ERROR("Invalid object handle %d at index %d\n",
3760 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003761 /* prevent error path from reading uninitialized data */
3762 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003763 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003764 goto err;
3765 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003766
Daniel Vetter23010e42010-03-08 13:35:02 +01003767 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003768 if (obj_priv->in_execbuffer) {
3769 DRM_ERROR("Object %p appears more than once in object list\n",
3770 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003771 /* prevent error path from reading uninitialized data */
3772 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003773 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003774 goto err;
3775 }
3776 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003777 flips += atomic_read(&obj_priv->pending_flip);
3778 }
3779
3780 if (flips > 0) {
3781 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3782 args->buffer_count);
3783 if (ret)
3784 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003785 }
Eric Anholt673a3942008-07-30 12:06:12 -07003786
Keith Packardac94a962008-11-20 23:30:27 -08003787 /* Pin and relocate */
3788 for (pin_tries = 0; ; pin_tries++) {
3789 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003790 reloc_index = 0;
3791
Keith Packardac94a962008-11-20 23:30:27 -08003792 for (i = 0; i < args->buffer_count; i++) {
3793 object_list[i]->pending_read_domains = 0;
3794 object_list[i]->pending_write_domain = 0;
3795 ret = i915_gem_object_pin_and_relocate(object_list[i],
3796 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003797 &exec_list[i],
3798 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003799 if (ret)
3800 break;
3801 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003802 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003803 }
3804 /* success */
3805 if (ret == 0)
3806 break;
3807
3808 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003809 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003810 if (ret != -ERESTARTSYS) {
3811 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003812 int num_fences = 0;
3813 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003814 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003815
Chris Wilson07f73f62009-09-14 16:50:30 +01003816 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003817 num_fences +=
3818 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3819 obj_priv->tiling_mode != I915_TILING_NONE;
3820 }
3821 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003822 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003823 total_size, num_fences,
3824 ret);
Chris Wilson73aa8082010-09-30 11:46:12 +01003825 DRM_ERROR("%u objects [%u pinned, %u GTT], "
3826 "%zu object bytes [%zu pinned], "
3827 "%zu /%zu gtt bytes\n",
3828 dev_priv->mm.object_count,
3829 dev_priv->mm.pin_count,
3830 dev_priv->mm.gtt_count,
3831 dev_priv->mm.object_memory,
3832 dev_priv->mm.pin_memory,
3833 dev_priv->mm.gtt_memory,
3834 dev_priv->mm.gtt_total);
Chris Wilson07f73f62009-09-14 16:50:30 +01003835 }
Eric Anholt673a3942008-07-30 12:06:12 -07003836 goto err;
3837 }
Keith Packardac94a962008-11-20 23:30:27 -08003838
3839 /* unpin all of our buffers */
3840 for (i = 0; i < pinned; i++)
3841 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003842 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003843
3844 /* evict everyone we can from the aperture */
3845 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003846 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003847 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003848 }
3849
3850 /* Set the pending read domains for the batch buffer to COMMAND */
3851 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003852 if (batch_obj->pending_write_domain) {
3853 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3854 ret = -EINVAL;
3855 goto err;
3856 }
3857 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003858
Chris Wilson83d60792009-06-06 09:45:57 +01003859 /* Sanity check the batch buffer, prior to moving objects */
3860 exec_offset = exec_list[args->buffer_count - 1].offset;
3861 ret = i915_gem_check_execbuffer (args, exec_offset);
3862 if (ret != 0) {
3863 DRM_ERROR("execbuf with invalid offset/length\n");
3864 goto err;
3865 }
3866
Keith Packard646f0f62008-11-20 23:23:03 -08003867 /* Zero the global flush/invalidate flags. These
3868 * will be modified as new domains are computed
3869 * for each object
3870 */
3871 dev->invalidate_domains = 0;
3872 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003873 dev_priv->mm.flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003874
Eric Anholt673a3942008-07-30 12:06:12 -07003875 for (i = 0; i < args->buffer_count; i++) {
3876 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003877
Keith Packard646f0f62008-11-20 23:23:03 -08003878 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003879 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003880 }
3881
Keith Packard646f0f62008-11-20 23:23:03 -08003882 if (dev->invalidate_domains | dev->flush_domains) {
3883#if WATCH_EXEC
3884 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3885 __func__,
3886 dev->invalidate_domains,
3887 dev->flush_domains);
3888#endif
Chris Wilsonc78ec302010-09-20 12:50:23 +01003889 i915_gem_flush(dev, file_priv,
Keith Packard646f0f62008-11-20 23:23:03 -08003890 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003891 dev->flush_domains,
3892 dev_priv->mm.flush_rings);
Daniel Vettera6910432010-02-02 17:08:37 +01003893 }
3894
Eric Anholtefbeed92009-02-19 14:54:51 -08003895 for (i = 0; i < args->buffer_count; i++) {
3896 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003897 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003898 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003899
3900 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003901 if (obj->write_domain)
3902 list_move_tail(&obj_priv->gpu_write_list,
3903 &dev_priv->mm.gpu_write_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01003904
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003905 trace_i915_gem_object_change_domain(obj,
3906 obj->read_domains,
3907 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003908 }
3909
Eric Anholt673a3942008-07-30 12:06:12 -07003910#if WATCH_COHERENCY
3911 for (i = 0; i < args->buffer_count; i++) {
3912 i915_gem_object_check_coherency(object_list[i],
3913 exec_list[i].handle);
3914 }
3915#endif
3916
Eric Anholt673a3942008-07-30 12:06:12 -07003917#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003918 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003919 args->batch_len,
3920 __func__,
3921 ~0);
3922#endif
3923
Eric Anholt673a3942008-07-30 12:06:12 -07003924 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003925 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3926 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003927 if (ret) {
3928 DRM_ERROR("dispatch failed %d\n", ret);
3929 goto err;
3930 }
3931
3932 /*
3933 * Ensure that the commands in the batch buffer are
3934 * finished before the interrupt fires
3935 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003936 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003937
Daniel Vetter617dbe22010-02-11 22:16:02 +01003938 for (i = 0; i < args->buffer_count; i++) {
3939 struct drm_gem_object *obj = object_list[i];
3940 obj_priv = to_intel_bo(obj);
3941
3942 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01003943 }
Chris Wilsona56ba562010-09-28 10:07:56 +01003944
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003945 i915_add_request(dev, file_priv, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003946 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003947
Eric Anholt673a3942008-07-30 12:06:12 -07003948err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003949 for (i = 0; i < pinned; i++)
3950 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003951
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003952 for (i = 0; i < args->buffer_count; i++) {
3953 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003954 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003955 obj_priv->in_execbuffer = false;
3956 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003957 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003958 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003959
Eric Anholt673a3942008-07-30 12:06:12 -07003960 mutex_unlock(&dev->struct_mutex);
3961
Chris Wilson93533c22010-01-31 10:40:48 +00003962pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003963 /* Copy the updated relocations out regardless of current error
3964 * state. Failure to update the relocs would mean that the next
3965 * time userland calls execbuf, it would do so with presumed offset
3966 * state that didn't match the actual object state.
3967 */
3968 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3969 relocs);
3970 if (ret2 != 0) {
3971 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3972
3973 if (ret == 0)
3974 ret = ret2;
3975 }
3976
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003977 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003978 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003979 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003980
3981 return ret;
3982}
3983
Jesse Barnes76446ca2009-12-17 22:05:42 -05003984/*
3985 * Legacy execbuffer just creates an exec2 list from the original exec object
3986 * list array and passes it to the real function.
3987 */
3988int
3989i915_gem_execbuffer(struct drm_device *dev, void *data,
3990 struct drm_file *file_priv)
3991{
3992 struct drm_i915_gem_execbuffer *args = data;
3993 struct drm_i915_gem_execbuffer2 exec2;
3994 struct drm_i915_gem_exec_object *exec_list = NULL;
3995 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3996 int ret, i;
3997
3998#if WATCH_EXEC
3999 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4000 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4001#endif
4002
4003 if (args->buffer_count < 1) {
4004 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4005 return -EINVAL;
4006 }
4007
4008 /* Copy in the exec list from userland */
4009 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4010 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4011 if (exec_list == NULL || exec2_list == NULL) {
4012 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4013 args->buffer_count);
4014 drm_free_large(exec_list);
4015 drm_free_large(exec2_list);
4016 return -ENOMEM;
4017 }
4018 ret = copy_from_user(exec_list,
4019 (struct drm_i915_relocation_entry __user *)
4020 (uintptr_t) args->buffers_ptr,
4021 sizeof(*exec_list) * args->buffer_count);
4022 if (ret != 0) {
4023 DRM_ERROR("copy %d exec entries failed %d\n",
4024 args->buffer_count, ret);
4025 drm_free_large(exec_list);
4026 drm_free_large(exec2_list);
4027 return -EFAULT;
4028 }
4029
4030 for (i = 0; i < args->buffer_count; i++) {
4031 exec2_list[i].handle = exec_list[i].handle;
4032 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4033 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4034 exec2_list[i].alignment = exec_list[i].alignment;
4035 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004036 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004037 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4038 else
4039 exec2_list[i].flags = 0;
4040 }
4041
4042 exec2.buffers_ptr = args->buffers_ptr;
4043 exec2.buffer_count = args->buffer_count;
4044 exec2.batch_start_offset = args->batch_start_offset;
4045 exec2.batch_len = args->batch_len;
4046 exec2.DR1 = args->DR1;
4047 exec2.DR4 = args->DR4;
4048 exec2.num_cliprects = args->num_cliprects;
4049 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004050 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004051
4052 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4053 if (!ret) {
4054 /* Copy the new buffer offsets back to the user's exec list. */
4055 for (i = 0; i < args->buffer_count; i++)
4056 exec_list[i].offset = exec2_list[i].offset;
4057 /* ... and back out to userspace */
4058 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4059 (uintptr_t) args->buffers_ptr,
4060 exec_list,
4061 sizeof(*exec_list) * args->buffer_count);
4062 if (ret) {
4063 ret = -EFAULT;
4064 DRM_ERROR("failed to copy %d exec entries "
4065 "back to user (%d)\n",
4066 args->buffer_count, ret);
4067 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004068 }
4069
4070 drm_free_large(exec_list);
4071 drm_free_large(exec2_list);
4072 return ret;
4073}
4074
4075int
4076i915_gem_execbuffer2(struct drm_device *dev, void *data,
4077 struct drm_file *file_priv)
4078{
4079 struct drm_i915_gem_execbuffer2 *args = data;
4080 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4081 int ret;
4082
4083#if WATCH_EXEC
4084 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4085 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4086#endif
4087
4088 if (args->buffer_count < 1) {
4089 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4090 return -EINVAL;
4091 }
4092
4093 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4094 if (exec2_list == NULL) {
4095 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4096 args->buffer_count);
4097 return -ENOMEM;
4098 }
4099 ret = copy_from_user(exec2_list,
4100 (struct drm_i915_relocation_entry __user *)
4101 (uintptr_t) args->buffers_ptr,
4102 sizeof(*exec2_list) * args->buffer_count);
4103 if (ret != 0) {
4104 DRM_ERROR("copy %d exec entries failed %d\n",
4105 args->buffer_count, ret);
4106 drm_free_large(exec2_list);
4107 return -EFAULT;
4108 }
4109
4110 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4111 if (!ret) {
4112 /* Copy the new buffer offsets back to the user's exec list. */
4113 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4114 (uintptr_t) args->buffers_ptr,
4115 exec2_list,
4116 sizeof(*exec2_list) * args->buffer_count);
4117 if (ret) {
4118 ret = -EFAULT;
4119 DRM_ERROR("failed to copy %d exec entries "
4120 "back to user (%d)\n",
4121 args->buffer_count, ret);
4122 }
4123 }
4124
4125 drm_free_large(exec2_list);
4126 return ret;
4127}
4128
Eric Anholt673a3942008-07-30 12:06:12 -07004129int
4130i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4131{
4132 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004133 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004134 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004135 int ret;
4136
Daniel Vetter778c3542010-05-13 11:49:44 +02004137 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004138 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004139
4140 if (obj_priv->gtt_space != NULL) {
4141 if (alignment == 0)
4142 alignment = i915_gem_get_gtt_alignment(obj);
4143 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004144 WARN(obj_priv->pin_count,
4145 "bo is already pinned with incorrect alignment:"
4146 " offset=%x, req.alignment=%x\n",
4147 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004148 ret = i915_gem_object_unbind(obj);
4149 if (ret)
4150 return ret;
4151 }
4152 }
4153
Eric Anholt673a3942008-07-30 12:06:12 -07004154 if (obj_priv->gtt_space == NULL) {
4155 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004156 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004157 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004158 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004159
Eric Anholt673a3942008-07-30 12:06:12 -07004160 obj_priv->pin_count++;
4161
4162 /* If the object is not active and not pending a flush,
4163 * remove it from the inactive list
4164 */
4165 if (obj_priv->pin_count == 1) {
Chris Wilson73aa8082010-09-30 11:46:12 +01004166 i915_gem_info_add_pin(dev_priv, obj->size);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004167 if (!obj_priv->active)
4168 list_move_tail(&obj_priv->list,
4169 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004170 }
Eric Anholt673a3942008-07-30 12:06:12 -07004171
Chris Wilson23bc5982010-09-29 16:10:57 +01004172 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004173 return 0;
4174}
4175
4176void
4177i915_gem_object_unpin(struct drm_gem_object *obj)
4178{
4179 struct drm_device *dev = obj->dev;
4180 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004181 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004182
Chris Wilson23bc5982010-09-29 16:10:57 +01004183 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004184 obj_priv->pin_count--;
4185 BUG_ON(obj_priv->pin_count < 0);
4186 BUG_ON(obj_priv->gtt_space == NULL);
4187
4188 /* If the object is no longer pinned, and is
4189 * neither active nor being flushed, then stick it on
4190 * the inactive list
4191 */
4192 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004193 if (!obj_priv->active)
Eric Anholt673a3942008-07-30 12:06:12 -07004194 list_move_tail(&obj_priv->list,
4195 &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01004196 i915_gem_info_remove_pin(dev_priv, obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07004197 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004198 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004199}
4200
4201int
4202i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4203 struct drm_file *file_priv)
4204{
4205 struct drm_i915_gem_pin *args = data;
4206 struct drm_gem_object *obj;
4207 struct drm_i915_gem_object *obj_priv;
4208 int ret;
4209
Eric Anholt673a3942008-07-30 12:06:12 -07004210 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4211 if (obj == NULL) {
4212 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4213 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004214 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004215 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004216 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004217
Chris Wilson76c1dec2010-09-25 11:22:51 +01004218 ret = i915_mutex_lock_interruptible(dev);
4219 if (ret) {
4220 drm_gem_object_unreference_unlocked(obj);
4221 return ret;
4222 }
4223
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004224 if (obj_priv->madv != I915_MADV_WILLNEED) {
4225 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004226 drm_gem_object_unreference(obj);
4227 mutex_unlock(&dev->struct_mutex);
4228 return -EINVAL;
4229 }
4230
Jesse Barnes79e53942008-11-07 14:24:08 -08004231 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4232 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4233 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004234 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004235 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004236 return -EINVAL;
4237 }
4238
4239 obj_priv->user_pin_count++;
4240 obj_priv->pin_filp = file_priv;
4241 if (obj_priv->user_pin_count == 1) {
4242 ret = i915_gem_object_pin(obj, args->alignment);
4243 if (ret != 0) {
4244 drm_gem_object_unreference(obj);
4245 mutex_unlock(&dev->struct_mutex);
4246 return ret;
4247 }
Eric Anholt673a3942008-07-30 12:06:12 -07004248 }
4249
4250 /* XXX - flush the CPU caches for pinned objects
4251 * as the X server doesn't manage domains yet
4252 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004253 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004254 args->offset = obj_priv->gtt_offset;
4255 drm_gem_object_unreference(obj);
4256 mutex_unlock(&dev->struct_mutex);
4257
4258 return 0;
4259}
4260
4261int
4262i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4263 struct drm_file *file_priv)
4264{
4265 struct drm_i915_gem_pin *args = data;
4266 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004267 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004268 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004269
4270 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4271 if (obj == NULL) {
4272 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4273 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004274 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004275 }
4276
Daniel Vetter23010e42010-03-08 13:35:02 +01004277 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004278
4279 ret = i915_mutex_lock_interruptible(dev);
4280 if (ret) {
4281 drm_gem_object_unreference_unlocked(obj);
4282 return ret;
4283 }
4284
Jesse Barnes79e53942008-11-07 14:24:08 -08004285 if (obj_priv->pin_filp != file_priv) {
4286 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4287 args->handle);
4288 drm_gem_object_unreference(obj);
4289 mutex_unlock(&dev->struct_mutex);
4290 return -EINVAL;
4291 }
4292 obj_priv->user_pin_count--;
4293 if (obj_priv->user_pin_count == 0) {
4294 obj_priv->pin_filp = NULL;
4295 i915_gem_object_unpin(obj);
4296 }
Eric Anholt673a3942008-07-30 12:06:12 -07004297
4298 drm_gem_object_unreference(obj);
4299 mutex_unlock(&dev->struct_mutex);
4300 return 0;
4301}
4302
4303int
4304i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4305 struct drm_file *file_priv)
4306{
4307 struct drm_i915_gem_busy *args = data;
4308 struct drm_gem_object *obj;
4309 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004310 int ret;
4311
Eric Anholt673a3942008-07-30 12:06:12 -07004312 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4313 if (obj == NULL) {
4314 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4315 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004316 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004317 }
4318
Chris Wilson76c1dec2010-09-25 11:22:51 +01004319 ret = i915_mutex_lock_interruptible(dev);
4320 if (ret) {
4321 drm_gem_object_unreference_unlocked(obj);
4322 return ret;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004323 }
4324
Chris Wilson0be555b2010-08-04 15:36:30 +01004325 /* Count all active objects as busy, even if they are currently not used
4326 * by the gpu. Users of this interface expect objects to eventually
4327 * become non-busy without any further actions, therefore emit any
4328 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004329 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004330 obj_priv = to_intel_bo(obj);
4331 args->busy = obj_priv->active;
4332 if (args->busy) {
4333 /* Unconditionally flush objects, even when the gpu still uses this
4334 * object. Userspace calling this function indicates that it wants to
4335 * use this buffer rather sooner than later, so issuing the required
4336 * flush earlier is beneficial.
4337 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004338 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4339 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004340 obj_priv->ring,
4341 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004342
4343 /* Update the active list for the hardware's current position.
4344 * Otherwise this only updates on a delayed timer or when irqs
4345 * are actually unmasked, and our working set ends up being
4346 * larger than required.
4347 */
4348 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4349
4350 args->busy = obj_priv->active;
4351 }
Eric Anholt673a3942008-07-30 12:06:12 -07004352
4353 drm_gem_object_unreference(obj);
4354 mutex_unlock(&dev->struct_mutex);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004355 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004356}
4357
4358int
4359i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4360 struct drm_file *file_priv)
4361{
4362 return i915_gem_ring_throttle(dev, file_priv);
4363}
4364
Chris Wilson3ef94da2009-09-14 16:50:29 +01004365int
4366i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4367 struct drm_file *file_priv)
4368{
4369 struct drm_i915_gem_madvise *args = data;
4370 struct drm_gem_object *obj;
4371 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004372 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004373
4374 switch (args->madv) {
4375 case I915_MADV_DONTNEED:
4376 case I915_MADV_WILLNEED:
4377 break;
4378 default:
4379 return -EINVAL;
4380 }
4381
4382 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4383 if (obj == NULL) {
4384 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4385 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004386 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004387 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004388 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004389
Chris Wilson76c1dec2010-09-25 11:22:51 +01004390 ret = i915_mutex_lock_interruptible(dev);
4391 if (ret) {
4392 drm_gem_object_unreference_unlocked(obj);
4393 return ret;
4394 }
4395
Chris Wilson3ef94da2009-09-14 16:50:29 +01004396 if (obj_priv->pin_count) {
4397 drm_gem_object_unreference(obj);
4398 mutex_unlock(&dev->struct_mutex);
4399
4400 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4401 return -EINVAL;
4402 }
4403
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004404 if (obj_priv->madv != __I915_MADV_PURGED)
4405 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004406
Chris Wilson2d7ef392009-09-20 23:13:10 +01004407 /* if the object is no longer bound, discard its backing storage */
4408 if (i915_gem_object_is_purgeable(obj_priv) &&
4409 obj_priv->gtt_space == NULL)
4410 i915_gem_object_truncate(obj);
4411
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004412 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4413
Chris Wilson3ef94da2009-09-14 16:50:29 +01004414 drm_gem_object_unreference(obj);
4415 mutex_unlock(&dev->struct_mutex);
4416
4417 return 0;
4418}
4419
Daniel Vetterac52bc52010-04-09 19:05:06 +00004420struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4421 size_t size)
4422{
Chris Wilson73aa8082010-09-30 11:46:12 +01004423 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004424 struct drm_i915_gem_object *obj;
4425
4426 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4427 if (obj == NULL)
4428 return NULL;
4429
4430 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4431 kfree(obj);
4432 return NULL;
4433 }
4434
Chris Wilson73aa8082010-09-30 11:46:12 +01004435 i915_gem_info_add_obj(dev_priv, size);
4436
Daniel Vetterc397b902010-04-09 19:05:07 +00004437 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4438 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4439
4440 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004441 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004442 obj->fence_reg = I915_FENCE_REG_NONE;
4443 INIT_LIST_HEAD(&obj->list);
4444 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004445 obj->madv = I915_MADV_WILLNEED;
4446
4447 trace_i915_gem_object_create(&obj->base);
4448
4449 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004450}
4451
Eric Anholt673a3942008-07-30 12:06:12 -07004452int i915_gem_init_object(struct drm_gem_object *obj)
4453{
Daniel Vetterc397b902010-04-09 19:05:07 +00004454 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004455
Eric Anholt673a3942008-07-30 12:06:12 -07004456 return 0;
4457}
4458
Chris Wilsonbe726152010-07-23 23:18:50 +01004459static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4460{
4461 struct drm_device *dev = obj->dev;
4462 drm_i915_private_t *dev_priv = dev->dev_private;
4463 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4464 int ret;
4465
4466 ret = i915_gem_object_unbind(obj);
4467 if (ret == -ERESTARTSYS) {
4468 list_move(&obj_priv->list,
4469 &dev_priv->mm.deferred_free_list);
4470 return;
4471 }
4472
4473 if (obj_priv->mmap_offset)
4474 i915_gem_free_mmap_offset(obj);
4475
4476 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004477 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004478
4479 kfree(obj_priv->page_cpu_valid);
4480 kfree(obj_priv->bit_17);
4481 kfree(obj_priv);
4482}
4483
Eric Anholt673a3942008-07-30 12:06:12 -07004484void i915_gem_free_object(struct drm_gem_object *obj)
4485{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004486 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004487 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004488
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004489 trace_i915_gem_object_destroy(obj);
4490
Eric Anholt673a3942008-07-30 12:06:12 -07004491 while (obj_priv->pin_count > 0)
4492 i915_gem_object_unpin(obj);
4493
Dave Airlie71acb5e2008-12-30 20:31:46 +10004494 if (obj_priv->phys_obj)
4495 i915_gem_detach_phys_object(dev, obj);
4496
Chris Wilsonbe726152010-07-23 23:18:50 +01004497 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004498}
4499
Jesse Barnes5669fca2009-02-17 15:13:31 -08004500int
Eric Anholt673a3942008-07-30 12:06:12 -07004501i915_gem_idle(struct drm_device *dev)
4502{
4503 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004504 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004505
Keith Packard6dbe2772008-10-14 21:41:13 -07004506 mutex_lock(&dev->struct_mutex);
4507
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004508 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004509 (dev_priv->render_ring.gem_object == NULL) ||
4510 (HAS_BSD(dev) &&
4511 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004512 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004513 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004514 }
Eric Anholt673a3942008-07-30 12:06:12 -07004515
Chris Wilson29105cc2010-01-07 10:39:13 +00004516 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004517 if (ret) {
4518 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004519 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004520 }
Eric Anholt673a3942008-07-30 12:06:12 -07004521
Chris Wilson29105cc2010-01-07 10:39:13 +00004522 /* Under UMS, be paranoid and evict. */
4523 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004524 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004525 if (ret) {
4526 mutex_unlock(&dev->struct_mutex);
4527 return ret;
4528 }
4529 }
4530
4531 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4532 * We need to replace this with a semaphore, or something.
4533 * And not confound mm.suspended!
4534 */
4535 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004536 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004537
4538 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004539 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004540
Keith Packard6dbe2772008-10-14 21:41:13 -07004541 mutex_unlock(&dev->struct_mutex);
4542
Chris Wilson29105cc2010-01-07 10:39:13 +00004543 /* Cancel the retire work handler, which should be idle now. */
4544 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4545
Eric Anholt673a3942008-07-30 12:06:12 -07004546 return 0;
4547}
4548
Jesse Barnese552eb72010-04-21 11:39:23 -07004549/*
4550 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4551 * over cache flushing.
4552 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004553static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004554i915_gem_init_pipe_control(struct drm_device *dev)
4555{
4556 drm_i915_private_t *dev_priv = dev->dev_private;
4557 struct drm_gem_object *obj;
4558 struct drm_i915_gem_object *obj_priv;
4559 int ret;
4560
Eric Anholt34dc4d42010-05-07 14:30:03 -07004561 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004562 if (obj == NULL) {
4563 DRM_ERROR("Failed to allocate seqno page\n");
4564 ret = -ENOMEM;
4565 goto err;
4566 }
4567 obj_priv = to_intel_bo(obj);
4568 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4569
4570 ret = i915_gem_object_pin(obj, 4096);
4571 if (ret)
4572 goto err_unref;
4573
4574 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4575 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4576 if (dev_priv->seqno_page == NULL)
4577 goto err_unpin;
4578
4579 dev_priv->seqno_obj = obj;
4580 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4581
4582 return 0;
4583
4584err_unpin:
4585 i915_gem_object_unpin(obj);
4586err_unref:
4587 drm_gem_object_unreference(obj);
4588err:
4589 return ret;
4590}
4591
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004592
4593static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004594i915_gem_cleanup_pipe_control(struct drm_device *dev)
4595{
4596 drm_i915_private_t *dev_priv = dev->dev_private;
4597 struct drm_gem_object *obj;
4598 struct drm_i915_gem_object *obj_priv;
4599
4600 obj = dev_priv->seqno_obj;
4601 obj_priv = to_intel_bo(obj);
4602 kunmap(obj_priv->pages[0]);
4603 i915_gem_object_unpin(obj);
4604 drm_gem_object_unreference(obj);
4605 dev_priv->seqno_obj = NULL;
4606
4607 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004608}
4609
Eric Anholt673a3942008-07-30 12:06:12 -07004610int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004611i915_gem_init_ringbuffer(struct drm_device *dev)
4612{
4613 drm_i915_private_t *dev_priv = dev->dev_private;
4614 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004615
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004616 if (HAS_PIPE_CONTROL(dev)) {
4617 ret = i915_gem_init_pipe_control(dev);
4618 if (ret)
4619 return ret;
4620 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004621
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004622 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004623 if (ret)
4624 goto cleanup_pipe_control;
4625
4626 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004627 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004628 if (ret)
4629 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004630 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004631
Chris Wilson6f392d5482010-08-07 11:01:22 +01004632 dev_priv->next_seqno = 1;
4633
Chris Wilson68f95ba2010-05-27 13:18:22 +01004634 return 0;
4635
4636cleanup_render_ring:
4637 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4638cleanup_pipe_control:
4639 if (HAS_PIPE_CONTROL(dev))
4640 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004641 return ret;
4642}
4643
4644void
4645i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4646{
4647 drm_i915_private_t *dev_priv = dev->dev_private;
4648
4649 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004650 if (HAS_BSD(dev))
4651 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004652 if (HAS_PIPE_CONTROL(dev))
4653 i915_gem_cleanup_pipe_control(dev);
4654}
4655
4656int
Eric Anholt673a3942008-07-30 12:06:12 -07004657i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4658 struct drm_file *file_priv)
4659{
4660 drm_i915_private_t *dev_priv = dev->dev_private;
4661 int ret;
4662
Jesse Barnes79e53942008-11-07 14:24:08 -08004663 if (drm_core_check_feature(dev, DRIVER_MODESET))
4664 return 0;
4665
Ben Gamariba1234d2009-09-14 17:48:47 -04004666 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004667 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004668 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004669 }
4670
Eric Anholt673a3942008-07-30 12:06:12 -07004671 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004672 dev_priv->mm.suspended = 0;
4673
4674 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004675 if (ret != 0) {
4676 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004677 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004678 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004679
Zou Nan hai852835f2010-05-21 09:08:56 +08004680 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004681 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004682 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4683 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004684 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004685 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004686 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004687
Chris Wilson5f353082010-06-07 14:03:03 +01004688 ret = drm_irq_install(dev);
4689 if (ret)
4690 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004691
Eric Anholt673a3942008-07-30 12:06:12 -07004692 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004693
4694cleanup_ringbuffer:
4695 mutex_lock(&dev->struct_mutex);
4696 i915_gem_cleanup_ringbuffer(dev);
4697 dev_priv->mm.suspended = 1;
4698 mutex_unlock(&dev->struct_mutex);
4699
4700 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004701}
4702
4703int
4704i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4705 struct drm_file *file_priv)
4706{
Jesse Barnes79e53942008-11-07 14:24:08 -08004707 if (drm_core_check_feature(dev, DRIVER_MODESET))
4708 return 0;
4709
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004710 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004711 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004712}
4713
4714void
4715i915_gem_lastclose(struct drm_device *dev)
4716{
4717 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004718
Eric Anholte806b492009-01-22 09:56:58 -08004719 if (drm_core_check_feature(dev, DRIVER_MODESET))
4720 return;
4721
Keith Packard6dbe2772008-10-14 21:41:13 -07004722 ret = i915_gem_idle(dev);
4723 if (ret)
4724 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004725}
4726
4727void
4728i915_gem_load(struct drm_device *dev)
4729{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004730 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004731 drm_i915_private_t *dev_priv = dev->dev_private;
4732
Eric Anholt673a3942008-07-30 12:06:12 -07004733 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004734 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004735 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004736 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004737 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004738 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004739 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4740 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004741 if (HAS_BSD(dev)) {
4742 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4743 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4744 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004745 for (i = 0; i < 16; i++)
4746 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004747 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4748 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004749 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004750 spin_lock(&shrink_list_lock);
4751 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4752 spin_unlock(&shrink_list_lock);
4753
Dave Airlie94400122010-07-20 13:15:31 +10004754 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4755 if (IS_GEN3(dev)) {
4756 u32 tmp = I915_READ(MI_ARB_STATE);
4757 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4758 /* arb state is a masked write, so set bit + bit in mask */
4759 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4760 I915_WRITE(MI_ARB_STATE, tmp);
4761 }
4762 }
4763
Jesse Barnesde151cf2008-11-12 10:03:55 -08004764 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004765 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4766 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004767
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004768 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004769 dev_priv->num_fence_regs = 16;
4770 else
4771 dev_priv->num_fence_regs = 8;
4772
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004773 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004774 switch (INTEL_INFO(dev)->gen) {
4775 case 6:
4776 for (i = 0; i < 16; i++)
4777 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4778 break;
4779 case 5:
4780 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004781 for (i = 0; i < 16; i++)
4782 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004783 break;
4784 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004785 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4786 for (i = 0; i < 8; i++)
4787 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004788 case 2:
4789 for (i = 0; i < 8; i++)
4790 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4791 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004792 }
Eric Anholt673a3942008-07-30 12:06:12 -07004793 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004794 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004795}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004796
4797/*
4798 * Create a physically contiguous memory object for this object
4799 * e.g. for cursor + overlay regs
4800 */
Chris Wilson995b6762010-08-20 13:23:26 +01004801static int i915_gem_init_phys_object(struct drm_device *dev,
4802 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004803{
4804 drm_i915_private_t *dev_priv = dev->dev_private;
4805 struct drm_i915_gem_phys_object *phys_obj;
4806 int ret;
4807
4808 if (dev_priv->mm.phys_objs[id - 1] || !size)
4809 return 0;
4810
Eric Anholt9a298b22009-03-24 12:23:04 -07004811 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004812 if (!phys_obj)
4813 return -ENOMEM;
4814
4815 phys_obj->id = id;
4816
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004817 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004818 if (!phys_obj->handle) {
4819 ret = -ENOMEM;
4820 goto kfree_obj;
4821 }
4822#ifdef CONFIG_X86
4823 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4824#endif
4825
4826 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4827
4828 return 0;
4829kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004830 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004831 return ret;
4832}
4833
Chris Wilson995b6762010-08-20 13:23:26 +01004834static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004835{
4836 drm_i915_private_t *dev_priv = dev->dev_private;
4837 struct drm_i915_gem_phys_object *phys_obj;
4838
4839 if (!dev_priv->mm.phys_objs[id - 1])
4840 return;
4841
4842 phys_obj = dev_priv->mm.phys_objs[id - 1];
4843 if (phys_obj->cur_obj) {
4844 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4845 }
4846
4847#ifdef CONFIG_X86
4848 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4849#endif
4850 drm_pci_free(dev, phys_obj->handle);
4851 kfree(phys_obj);
4852 dev_priv->mm.phys_objs[id - 1] = NULL;
4853}
4854
4855void i915_gem_free_all_phys_object(struct drm_device *dev)
4856{
4857 int i;
4858
Dave Airlie260883c2009-01-22 17:58:49 +10004859 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004860 i915_gem_free_phys_object(dev, i);
4861}
4862
4863void i915_gem_detach_phys_object(struct drm_device *dev,
4864 struct drm_gem_object *obj)
4865{
4866 struct drm_i915_gem_object *obj_priv;
4867 int i;
4868 int ret;
4869 int page_count;
4870
Daniel Vetter23010e42010-03-08 13:35:02 +01004871 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004872 if (!obj_priv->phys_obj)
4873 return;
4874
Chris Wilson4bdadb92010-01-27 13:36:32 +00004875 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004876 if (ret)
4877 goto out;
4878
4879 page_count = obj->size / PAGE_SIZE;
4880
4881 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004882 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004883 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4884
4885 memcpy(dst, src, PAGE_SIZE);
4886 kunmap_atomic(dst, KM_USER0);
4887 }
Eric Anholt856fa192009-03-19 14:10:50 -07004888 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004889 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004890
4891 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004892out:
4893 obj_priv->phys_obj->cur_obj = NULL;
4894 obj_priv->phys_obj = NULL;
4895}
4896
4897int
4898i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004899 struct drm_gem_object *obj,
4900 int id,
4901 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004902{
4903 drm_i915_private_t *dev_priv = dev->dev_private;
4904 struct drm_i915_gem_object *obj_priv;
4905 int ret = 0;
4906 int page_count;
4907 int i;
4908
4909 if (id > I915_MAX_PHYS_OBJECT)
4910 return -EINVAL;
4911
Daniel Vetter23010e42010-03-08 13:35:02 +01004912 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004913
4914 if (obj_priv->phys_obj) {
4915 if (obj_priv->phys_obj->id == id)
4916 return 0;
4917 i915_gem_detach_phys_object(dev, obj);
4918 }
4919
Dave Airlie71acb5e2008-12-30 20:31:46 +10004920 /* create a new object */
4921 if (!dev_priv->mm.phys_objs[id - 1]) {
4922 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004923 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004924 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004925 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004926 goto out;
4927 }
4928 }
4929
4930 /* bind to the object */
4931 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4932 obj_priv->phys_obj->cur_obj = obj;
4933
Chris Wilson4bdadb92010-01-27 13:36:32 +00004934 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004935 if (ret) {
4936 DRM_ERROR("failed to get page list\n");
4937 goto out;
4938 }
4939
4940 page_count = obj->size / PAGE_SIZE;
4941
4942 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004943 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004944 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4945
4946 memcpy(dst, src, PAGE_SIZE);
4947 kunmap_atomic(src, KM_USER0);
4948 }
4949
Chris Wilsond78b47b2009-06-17 21:52:49 +01004950 i915_gem_object_put_pages(obj);
4951
Dave Airlie71acb5e2008-12-30 20:31:46 +10004952 return 0;
4953out:
4954 return ret;
4955}
4956
4957static int
4958i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4959 struct drm_i915_gem_pwrite *args,
4960 struct drm_file *file_priv)
4961{
Daniel Vetter23010e42010-03-08 13:35:02 +01004962 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004963 void *obj_addr;
4964 int ret;
4965 char __user *user_data;
4966
4967 user_data = (char __user *) (uintptr_t) args->data_ptr;
4968 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4969
Zhao Yakui44d98a62009-10-09 11:39:40 +08004970 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004971 ret = copy_from_user(obj_addr, user_data, args->size);
4972 if (ret)
4973 return -EFAULT;
4974
4975 drm_agp_chipset_flush(dev);
4976 return 0;
4977}
Eric Anholtb9624422009-06-03 07:27:35 +00004978
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004979void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004980{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004981 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004982
4983 /* Clean up our request list when the client is going away, so that
4984 * later retire_requests won't dereference our soon-to-be-gone
4985 * file_priv.
4986 */
Chris Wilson1c255952010-09-26 11:03:27 +01004987 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004988 while (!list_empty(&file_priv->mm.request_list)) {
4989 struct drm_i915_gem_request *request;
4990
4991 request = list_first_entry(&file_priv->mm.request_list,
4992 struct drm_i915_gem_request,
4993 client_list);
4994 list_del(&request->client_list);
4995 request->file_priv = NULL;
4996 }
Chris Wilson1c255952010-09-26 11:03:27 +01004997 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004998}
Chris Wilson31169712009-09-14 16:50:28 +01004999
Chris Wilson31169712009-09-14 16:50:28 +01005000static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005001i915_gpu_is_active(struct drm_device *dev)
5002{
5003 drm_i915_private_t *dev_priv = dev->dev_private;
5004 int lists_empty;
5005
Chris Wilson1637ef42010-04-20 17:10:35 +01005006 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08005007 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005008 if (HAS_BSD(dev))
5009 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005010
5011 return !lists_empty;
5012}
5013
5014static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10005015i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005016{
5017 drm_i915_private_t *dev_priv, *next_dev;
5018 struct drm_i915_gem_object *obj_priv, *next_obj;
5019 int cnt = 0;
5020 int would_deadlock = 1;
5021
5022 /* "fast-path" to count number of available objects */
5023 if (nr_to_scan == 0) {
5024 spin_lock(&shrink_list_lock);
5025 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5026 struct drm_device *dev = dev_priv->dev;
5027
5028 if (mutex_trylock(&dev->struct_mutex)) {
5029 list_for_each_entry(obj_priv,
5030 &dev_priv->mm.inactive_list,
5031 list)
5032 cnt++;
5033 mutex_unlock(&dev->struct_mutex);
5034 }
5035 }
5036 spin_unlock(&shrink_list_lock);
5037
5038 return (cnt / 100) * sysctl_vfs_cache_pressure;
5039 }
5040
5041 spin_lock(&shrink_list_lock);
5042
Chris Wilson1637ef42010-04-20 17:10:35 +01005043rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005044 /* first scan for clean buffers */
5045 list_for_each_entry_safe(dev_priv, next_dev,
5046 &shrink_list, mm.shrink_list) {
5047 struct drm_device *dev = dev_priv->dev;
5048
5049 if (! mutex_trylock(&dev->struct_mutex))
5050 continue;
5051
5052 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005053 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005054
Chris Wilson31169712009-09-14 16:50:28 +01005055 list_for_each_entry_safe(obj_priv, next_obj,
5056 &dev_priv->mm.inactive_list,
5057 list) {
5058 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005059 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005060 if (--nr_to_scan <= 0)
5061 break;
5062 }
5063 }
5064
5065 spin_lock(&shrink_list_lock);
5066 mutex_unlock(&dev->struct_mutex);
5067
Chris Wilson963b4832009-09-20 23:03:54 +01005068 would_deadlock = 0;
5069
Chris Wilson31169712009-09-14 16:50:28 +01005070 if (nr_to_scan <= 0)
5071 break;
5072 }
5073
5074 /* second pass, evict/count anything still on the inactive list */
5075 list_for_each_entry_safe(dev_priv, next_dev,
5076 &shrink_list, mm.shrink_list) {
5077 struct drm_device *dev = dev_priv->dev;
5078
5079 if (! mutex_trylock(&dev->struct_mutex))
5080 continue;
5081
5082 spin_unlock(&shrink_list_lock);
5083
5084 list_for_each_entry_safe(obj_priv, next_obj,
5085 &dev_priv->mm.inactive_list,
5086 list) {
5087 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005088 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005089 nr_to_scan--;
5090 } else
5091 cnt++;
5092 }
5093
5094 spin_lock(&shrink_list_lock);
5095 mutex_unlock(&dev->struct_mutex);
5096
5097 would_deadlock = 0;
5098 }
5099
Chris Wilson1637ef42010-04-20 17:10:35 +01005100 if (nr_to_scan) {
5101 int active = 0;
5102
5103 /*
5104 * We are desperate for pages, so as a last resort, wait
5105 * for the GPU to finish and discard whatever we can.
5106 * This has a dramatic impact to reduce the number of
5107 * OOM-killer events whilst running the GPU aggressively.
5108 */
5109 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5110 struct drm_device *dev = dev_priv->dev;
5111
5112 if (!mutex_trylock(&dev->struct_mutex))
5113 continue;
5114
5115 spin_unlock(&shrink_list_lock);
5116
5117 if (i915_gpu_is_active(dev)) {
5118 i915_gpu_idle(dev);
5119 active++;
5120 }
5121
5122 spin_lock(&shrink_list_lock);
5123 mutex_unlock(&dev->struct_mutex);
5124 }
5125
5126 if (active)
5127 goto rescan;
5128 }
5129
Chris Wilson31169712009-09-14 16:50:28 +01005130 spin_unlock(&shrink_list_lock);
5131
5132 if (would_deadlock)
5133 return -1;
5134 else if (cnt > 0)
5135 return (cnt / 100) * sysctl_vfs_cache_pressure;
5136 else
5137 return 0;
5138}
5139
5140static struct shrinker shrinker = {
5141 .shrink = i915_gem_shrink,
5142 .seeks = DEFAULT_SEEKS,
5143};
5144
5145__init void
5146i915_gem_shrinker_init(void)
5147{
5148 register_shrinker(&shrinker);
5149}
5150
5151__exit void
5152i915_gem_shrinker_exit(void)
5153{
5154 unregister_shrinker(&shrinker);
5155}