blob: 16c4b7b9602c5a31d138385f969983a598de8309 [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 */
Eric Anholt673a3942008-07-30 12:06:12 -07002161
Daniel Vetter96b47b62009-12-15 17:50:00 +01002162 /* release the fence reg _after_ flushing */
2163 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2164 i915_gem_clear_fence_reg(obj);
2165
Chris Wilson73aa8082010-09-30 11:46:12 +01002166 drm_unbind_agp(obj_priv->agp_mem);
2167 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002168
Eric Anholt856fa192009-03-19 14:10:50 -07002169 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002170 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002171
Chris Wilson73aa8082010-09-30 11:46:12 +01002172 i915_gem_info_remove_gtt(dev_priv, obj->size);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01002173 list_del_init(&obj_priv->list);
Eric Anholt673a3942008-07-30 12:06:12 -07002174
Chris Wilson73aa8082010-09-30 11:46:12 +01002175 drm_mm_put_block(obj_priv->gtt_space);
2176 obj_priv->gtt_space = NULL;
2177
Chris Wilson963b4832009-09-20 23:03:54 +01002178 if (i915_gem_object_is_purgeable(obj_priv))
2179 i915_gem_object_truncate(obj);
2180
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002181 trace_i915_gem_object_unbind(obj);
2182
Chris Wilson8dc17752010-07-23 23:18:51 +01002183 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002184}
2185
Chris Wilsona56ba562010-09-28 10:07:56 +01002186static int i915_ring_idle(struct drm_device *dev,
2187 struct intel_ring_buffer *ring)
2188{
2189 i915_gem_flush_ring(dev, NULL, ring,
2190 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2191 return i915_wait_request(dev,
2192 i915_gem_next_request_seqno(dev, ring),
2193 ring);
2194}
2195
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002196int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002197i915_gpu_idle(struct drm_device *dev)
2198{
2199 drm_i915_private_t *dev_priv = dev->dev_private;
2200 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002201 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002202
Zou Nan haid1b851f2010-05-21 09:08:57 +08002203 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2204 list_empty(&dev_priv->render_ring.active_list) &&
2205 (!HAS_BSD(dev) ||
2206 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002207 if (lists_empty)
2208 return 0;
2209
2210 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002211 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002212 if (ret)
2213 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002214
2215 if (HAS_BSD(dev)) {
Chris Wilsona56ba562010-09-28 10:07:56 +01002216 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002217 if (ret)
2218 return ret;
2219 }
2220
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002221 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002222}
2223
Chris Wilson5cdf5882010-09-27 15:51:07 +01002224static int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002225i915_gem_object_get_pages(struct drm_gem_object *obj,
2226 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002227{
Daniel Vetter23010e42010-03-08 13:35:02 +01002228 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002229 int page_count, i;
2230 struct address_space *mapping;
2231 struct inode *inode;
2232 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002233
Daniel Vetter778c3542010-05-13 11:49:44 +02002234 BUG_ON(obj_priv->pages_refcount
2235 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2236
Eric Anholt856fa192009-03-19 14:10:50 -07002237 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002238 return 0;
2239
2240 /* Get the list of pages out of our struct file. They'll be pinned
2241 * at this point until we release them.
2242 */
2243 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002244 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002245 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002246 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002247 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002248 return -ENOMEM;
2249 }
2250
2251 inode = obj->filp->f_path.dentry->d_inode;
2252 mapping = inode->i_mapping;
2253 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002254 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002255 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002256 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002257 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002258 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002259 if (IS_ERR(page))
2260 goto err_pages;
2261
Eric Anholt856fa192009-03-19 14:10:50 -07002262 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002263 }
Eric Anholt280b7132009-03-12 16:56:27 -07002264
2265 if (obj_priv->tiling_mode != I915_TILING_NONE)
2266 i915_gem_object_do_bit_17_swizzle(obj);
2267
Eric Anholt673a3942008-07-30 12:06:12 -07002268 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002269
2270err_pages:
2271 while (i--)
2272 page_cache_release(obj_priv->pages[i]);
2273
2274 drm_free_large(obj_priv->pages);
2275 obj_priv->pages = NULL;
2276 obj_priv->pages_refcount--;
2277 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002278}
2279
Eric Anholt4e901fd2009-10-26 16:44:17 -07002280static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2281{
2282 struct drm_gem_object *obj = reg->obj;
2283 struct drm_device *dev = obj->dev;
2284 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002285 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002286 int regnum = obj_priv->fence_reg;
2287 uint64_t val;
2288
2289 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2290 0xfffff000) << 32;
2291 val |= obj_priv->gtt_offset & 0xfffff000;
2292 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2293 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2294
2295 if (obj_priv->tiling_mode == I915_TILING_Y)
2296 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2297 val |= I965_FENCE_REG_VALID;
2298
2299 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2300}
2301
Jesse Barnesde151cf2008-11-12 10:03:55 -08002302static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2303{
2304 struct drm_gem_object *obj = reg->obj;
2305 struct drm_device *dev = obj->dev;
2306 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002307 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308 int regnum = obj_priv->fence_reg;
2309 uint64_t val;
2310
2311 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2312 0xfffff000) << 32;
2313 val |= obj_priv->gtt_offset & 0xfffff000;
2314 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2315 if (obj_priv->tiling_mode == I915_TILING_Y)
2316 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2317 val |= I965_FENCE_REG_VALID;
2318
2319 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2320}
2321
2322static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2323{
2324 struct drm_gem_object *obj = reg->obj;
2325 struct drm_device *dev = obj->dev;
2326 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002327 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002328 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002329 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002330 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002331 uint32_t pitch_val;
2332
2333 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2334 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002335 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002336 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002337 return;
2338 }
2339
Jesse Barnes0f973f22009-01-26 17:10:45 -08002340 if (obj_priv->tiling_mode == I915_TILING_Y &&
2341 HAS_128_BYTE_Y_TILING(dev))
2342 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002344 tile_width = 512;
2345
2346 /* Note: pitch better be a power of two tile widths */
2347 pitch_val = obj_priv->stride / tile_width;
2348 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002349
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002350 if (obj_priv->tiling_mode == I915_TILING_Y &&
2351 HAS_128_BYTE_Y_TILING(dev))
2352 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2353 else
2354 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2355
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356 val = obj_priv->gtt_offset;
2357 if (obj_priv->tiling_mode == I915_TILING_Y)
2358 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2359 val |= I915_FENCE_SIZE_BITS(obj->size);
2360 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2361 val |= I830_FENCE_REG_VALID;
2362
Eric Anholtdc529a42009-03-10 22:34:49 -07002363 if (regnum < 8)
2364 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2365 else
2366 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2367 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002368}
2369
2370static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2371{
2372 struct drm_gem_object *obj = reg->obj;
2373 struct drm_device *dev = obj->dev;
2374 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002375 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002376 int regnum = obj_priv->fence_reg;
2377 uint32_t val;
2378 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002379 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002380
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002381 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002382 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002383 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002384 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002385 return;
2386 }
2387
Eric Anholte76a16d2009-05-26 17:44:56 -07002388 pitch_val = obj_priv->stride / 128;
2389 pitch_val = ffs(pitch_val) - 1;
2390 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2391
Jesse Barnesde151cf2008-11-12 10:03:55 -08002392 val = obj_priv->gtt_offset;
2393 if (obj_priv->tiling_mode == I915_TILING_Y)
2394 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002395 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2396 WARN_ON(fence_size_bits & ~0x00000f00);
2397 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002398 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2399 val |= I830_FENCE_REG_VALID;
2400
2401 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402}
2403
Chris Wilson2cf34d72010-09-14 13:03:28 +01002404static int i915_find_fence_reg(struct drm_device *dev,
2405 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002406{
2407 struct drm_i915_fence_reg *reg = NULL;
2408 struct drm_i915_gem_object *obj_priv = NULL;
2409 struct drm_i915_private *dev_priv = dev->dev_private;
2410 struct drm_gem_object *obj = NULL;
2411 int i, avail, ret;
2412
2413 /* First try to find a free reg */
2414 avail = 0;
2415 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2416 reg = &dev_priv->fence_regs[i];
2417 if (!reg->obj)
2418 return i;
2419
Daniel Vetter23010e42010-03-08 13:35:02 +01002420 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002421 if (!obj_priv->pin_count)
2422 avail++;
2423 }
2424
2425 if (avail == 0)
2426 return -ENOSPC;
2427
2428 /* None available, try to steal one or wait for a user to finish */
2429 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002430 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2431 lru_list) {
2432 obj = reg->obj;
2433 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002434
2435 if (obj_priv->pin_count)
2436 continue;
2437
2438 /* found one! */
2439 i = obj_priv->fence_reg;
2440 break;
2441 }
2442
2443 BUG_ON(i == I915_FENCE_REG_NONE);
2444
2445 /* We only have a reference on obj from the active list. put_fence_reg
2446 * might drop that one, causing a use-after-free in it. So hold a
2447 * private reference to obj like the other callers of put_fence_reg
2448 * (set_tiling ioctl) do. */
2449 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002450 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002451 drm_gem_object_unreference(obj);
2452 if (ret != 0)
2453 return ret;
2454
2455 return i;
2456}
2457
Jesse Barnesde151cf2008-11-12 10:03:55 -08002458/**
2459 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2460 * @obj: object to map through a fence reg
2461 *
2462 * When mapping objects through the GTT, userspace wants to be able to write
2463 * to them without having to worry about swizzling if the object is tiled.
2464 *
2465 * This function walks the fence regs looking for a free one for @obj,
2466 * stealing one if it can't find any.
2467 *
2468 * It then sets up the reg based on the object's properties: address, pitch
2469 * and tiling format.
2470 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002471int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002472i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2473 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002474{
2475 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002476 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002477 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002478 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002479 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002480
Eric Anholta09ba7f2009-08-29 12:49:51 -07002481 /* Just update our place in the LRU if our fence is getting used. */
2482 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002483 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2484 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002485 return 0;
2486 }
2487
Jesse Barnesde151cf2008-11-12 10:03:55 -08002488 switch (obj_priv->tiling_mode) {
2489 case I915_TILING_NONE:
2490 WARN(1, "allocating a fence for non-tiled object?\n");
2491 break;
2492 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002493 if (!obj_priv->stride)
2494 return -EINVAL;
2495 WARN((obj_priv->stride & (512 - 1)),
2496 "object 0x%08x is X tiled but has non-512B pitch\n",
2497 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002498 break;
2499 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002500 if (!obj_priv->stride)
2501 return -EINVAL;
2502 WARN((obj_priv->stride & (128 - 1)),
2503 "object 0x%08x is Y tiled but has non-128B pitch\n",
2504 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002505 break;
2506 }
2507
Chris Wilson2cf34d72010-09-14 13:03:28 +01002508 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002509 if (ret < 0)
2510 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002511
Daniel Vetterae3db242010-02-19 11:51:58 +01002512 obj_priv->fence_reg = ret;
2513 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002514 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002515
Jesse Barnesde151cf2008-11-12 10:03:55 -08002516 reg->obj = obj;
2517
Chris Wilsone259bef2010-09-17 00:32:02 +01002518 switch (INTEL_INFO(dev)->gen) {
2519 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002520 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002521 break;
2522 case 5:
2523 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002524 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002525 break;
2526 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002527 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002528 break;
2529 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002530 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002531 break;
2532 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002533
Daniel Vetterae3db242010-02-19 11:51:58 +01002534 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2535 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002536
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002537 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002538}
2539
2540/**
2541 * i915_gem_clear_fence_reg - clear out fence register info
2542 * @obj: object to clear
2543 *
2544 * Zeroes out the fence register itself and clears out the associated
2545 * data structures in dev_priv and obj_priv.
2546 */
2547static void
2548i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2549{
2550 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002551 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002552 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002553 struct drm_i915_fence_reg *reg =
2554 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002555 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002556
Chris Wilsone259bef2010-09-17 00:32:02 +01002557 switch (INTEL_INFO(dev)->gen) {
2558 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002559 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2560 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002561 break;
2562 case 5:
2563 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002564 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002565 break;
2566 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002567 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002568 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002569 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002570 case 2:
2571 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002572
2573 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002574 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002575 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002576
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002577 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002578 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002579 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002580}
2581
Eric Anholt673a3942008-07-30 12:06:12 -07002582/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002583 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2584 * to the buffer to finish, and then resets the fence register.
2585 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002586 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002587 *
2588 * Zeroes out the fence register itself and clears out the associated
2589 * data structures in dev_priv and obj_priv.
2590 */
2591int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002592i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2593 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002594{
2595 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002596 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002597 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002598 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002599
2600 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2601 return 0;
2602
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002603 /* If we've changed tiling, GTT-mappings of the object
2604 * need to re-fault to ensure that the correct fence register
2605 * setup is in place.
2606 */
2607 i915_gem_release_mmap(obj);
2608
Chris Wilson52dc7d32009-06-06 09:46:01 +01002609 /* On the i915, GPU access to tiled buffers is via a fence,
2610 * therefore we must wait for any outstanding access to complete
2611 * before clearing the fence.
2612 */
Chris Wilson53640e12010-09-20 11:40:50 +01002613 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2614 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002615 int ret;
2616
Chris Wilson2cf34d72010-09-14 13:03:28 +01002617 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002618 if (ret)
2619 return ret;
2620
Chris Wilson2cf34d72010-09-14 13:03:28 +01002621 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002622 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002623 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002624
2625 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002626 }
2627
Daniel Vetter4a726612010-02-01 13:59:16 +01002628 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002629 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002630
2631 return 0;
2632}
2633
2634/**
Eric Anholt673a3942008-07-30 12:06:12 -07002635 * Finds free space in the GTT aperture and binds the object there.
2636 */
2637static int
2638i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2639{
2640 struct drm_device *dev = obj->dev;
2641 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002642 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002643 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002644 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002645 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002646
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002647 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002648 DRM_ERROR("Attempting to bind a purgeable object\n");
2649 return -EINVAL;
2650 }
2651
Eric Anholt673a3942008-07-30 12:06:12 -07002652 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002653 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002654 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002655 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2656 return -EINVAL;
2657 }
2658
Chris Wilson654fc602010-05-27 13:18:21 +01002659 /* If the object is bigger than the entire aperture, reject it early
2660 * before evicting everything in a vain attempt to find space.
2661 */
Chris Wilson73aa8082010-09-30 11:46:12 +01002662 if (obj->size > dev_priv->mm.gtt_total) {
Chris Wilson654fc602010-05-27 13:18:21 +01002663 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2664 return -E2BIG;
2665 }
2666
Eric Anholt673a3942008-07-30 12:06:12 -07002667 search_free:
2668 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2669 obj->size, alignment, 0);
2670 if (free_space != NULL) {
2671 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2672 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002673 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002674 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002675 }
2676 if (obj_priv->gtt_space == NULL) {
2677 /* If the gtt is empty and we're still having trouble
2678 * fitting our object in, we're out of memory.
2679 */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002680 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002681 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002682 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002683
Eric Anholt673a3942008-07-30 12:06:12 -07002684 goto search_free;
2685 }
2686
Chris Wilson4bdadb92010-01-27 13:36:32 +00002687 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002688 if (ret) {
2689 drm_mm_put_block(obj_priv->gtt_space);
2690 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002691
2692 if (ret == -ENOMEM) {
2693 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002694 ret = i915_gem_evict_something(dev, obj->size,
2695 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002696 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002697 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002698 if (gfpmask) {
2699 gfpmask = 0;
2700 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002701 }
2702
2703 return ret;
2704 }
2705
2706 goto search_free;
2707 }
2708
Eric Anholt673a3942008-07-30 12:06:12 -07002709 return ret;
2710 }
2711
Eric Anholt673a3942008-07-30 12:06:12 -07002712 /* Create an AGP memory structure pointing at our pages, and bind it
2713 * into the GTT.
2714 */
2715 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002716 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002717 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002718 obj_priv->gtt_offset,
2719 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002720 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002721 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002722 drm_mm_put_block(obj_priv->gtt_space);
2723 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002724
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002725 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002726 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002727 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002728
2729 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002730 }
Eric Anholt673a3942008-07-30 12:06:12 -07002731
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002732 /* keep track of bounds object by adding it to the inactive list */
2733 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01002734 i915_gem_info_add_gtt(dev_priv, obj->size);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002735
Eric Anholt673a3942008-07-30 12:06:12 -07002736 /* Assert that the object is not currently in any GPU domain. As it
2737 * wasn't in the GTT, there shouldn't be any way it could have been in
2738 * a GPU cache
2739 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002740 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2741 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002742
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002743 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2744
Eric Anholt673a3942008-07-30 12:06:12 -07002745 return 0;
2746}
2747
2748void
2749i915_gem_clflush_object(struct drm_gem_object *obj)
2750{
Daniel Vetter23010e42010-03-08 13:35:02 +01002751 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002752
2753 /* If we don't have a page list set up, then we're not pinned
2754 * to GPU, and we can ignore the cache flush because it'll happen
2755 * again at bind time.
2756 */
Eric Anholt856fa192009-03-19 14:10:50 -07002757 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002758 return;
2759
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002760 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002761
Eric Anholt856fa192009-03-19 14:10:50 -07002762 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002763}
2764
Eric Anholte47c68e2008-11-14 13:35:19 -08002765/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002766static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002767i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2768 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002769{
2770 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002771 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002772
2773 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002774 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002775
2776 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002777 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002778 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002779 to_intel_bo(obj)->ring,
2780 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002781 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002782
2783 trace_i915_gem_object_change_domain(obj,
2784 obj->read_domains,
2785 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002786
2787 if (pipelined)
2788 return 0;
2789
Chris Wilson2cf34d72010-09-14 13:03:28 +01002790 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002791}
2792
2793/** Flushes the GTT write domain for the object if it's dirty. */
2794static void
2795i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2796{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002797 uint32_t old_write_domain;
2798
Eric Anholte47c68e2008-11-14 13:35:19 -08002799 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2800 return;
2801
2802 /* No actual flushing is required for the GTT write domain. Writes
2803 * to it immediately go to main memory as far as we know, so there's
2804 * no chipset flush. It also doesn't land in render cache.
2805 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002806 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002807 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002808
2809 trace_i915_gem_object_change_domain(obj,
2810 obj->read_domains,
2811 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002812}
2813
2814/** Flushes the CPU write domain for the object if it's dirty. */
2815static void
2816i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2817{
2818 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002819 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002820
2821 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2822 return;
2823
2824 i915_gem_clflush_object(obj);
2825 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002826 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002827 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002828
2829 trace_i915_gem_object_change_domain(obj,
2830 obj->read_domains,
2831 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002832}
2833
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002834/**
2835 * Moves a single object to the GTT read, and possibly write domain.
2836 *
2837 * This function returns when the move is complete, including waiting on
2838 * flushes to occur.
2839 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002840int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002841i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2842{
Daniel Vetter23010e42010-03-08 13:35:02 +01002843 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002844 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002845 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002846
Eric Anholt02354392008-11-26 13:58:13 -08002847 /* Not valid to be called on unbound objects. */
2848 if (obj_priv->gtt_space == NULL)
2849 return -EINVAL;
2850
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002851 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002852 if (ret != 0)
2853 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002854
Chris Wilson72133422010-09-13 23:56:38 +01002855 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002856
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002857 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002858 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002859 if (ret)
2860 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002861 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002862
Chris Wilson72133422010-09-13 23:56:38 +01002863 old_write_domain = obj->write_domain;
2864 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002865
2866 /* It should now be out of any other write domains, and we can update
2867 * the domain values for our changes.
2868 */
2869 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2870 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002871 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002872 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002873 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002874 obj_priv->dirty = 1;
2875 }
2876
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002877 trace_i915_gem_object_change_domain(obj,
2878 old_read_domains,
2879 old_write_domain);
2880
Eric Anholte47c68e2008-11-14 13:35:19 -08002881 return 0;
2882}
2883
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002884/*
2885 * Prepare buffer for display plane. Use uninterruptible for possible flush
2886 * wait, as in modesetting process we're not supposed to be interrupted.
2887 */
2888int
Chris Wilson48b956c2010-09-14 12:50:34 +01002889i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2890 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002891{
Daniel Vetter23010e42010-03-08 13:35:02 +01002892 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002893 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002894 int ret;
2895
2896 /* Not valid to be called on unbound objects. */
2897 if (obj_priv->gtt_space == NULL)
2898 return -EINVAL;
2899
Chris Wilsonced270f2010-09-26 22:47:46 +01002900 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson48b956c2010-09-14 12:50:34 +01002901 if (ret)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002902 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002903
Chris Wilsonced270f2010-09-26 22:47:46 +01002904 /* Currently, we are always called from an non-interruptible context. */
2905 if (!pipelined) {
2906 ret = i915_gem_object_wait_rendering(obj, false);
2907 if (ret)
2908 return ret;
2909 }
2910
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002911 i915_gem_object_flush_cpu_write_domain(obj);
2912
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002913 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002914 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002915
2916 trace_i915_gem_object_change_domain(obj,
2917 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002918 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002919
2920 return 0;
2921}
2922
Eric Anholte47c68e2008-11-14 13:35:19 -08002923/**
2924 * Moves a single object to the CPU read, and possibly write domain.
2925 *
2926 * This function returns when the move is complete, including waiting on
2927 * flushes to occur.
2928 */
2929static int
2930i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2931{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002932 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002933 int ret;
2934
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002935 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002936 if (ret != 0)
2937 return ret;
2938
2939 i915_gem_object_flush_gtt_write_domain(obj);
2940
2941 /* If we have a partially-valid cache of the object in the CPU,
2942 * finish invalidating it and free the per-page flags.
2943 */
2944 i915_gem_object_set_to_full_cpu_read_domain(obj);
2945
Chris Wilson72133422010-09-13 23:56:38 +01002946 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002947 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002948 if (ret)
2949 return ret;
2950 }
2951
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002952 old_write_domain = obj->write_domain;
2953 old_read_domains = obj->read_domains;
2954
Eric Anholte47c68e2008-11-14 13:35:19 -08002955 /* Flush the CPU cache if it's still invalid. */
2956 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2957 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002958
2959 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2960 }
2961
2962 /* It should now be out of any other write domains, and we can update
2963 * the domain values for our changes.
2964 */
2965 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2966
2967 /* If we're writing through the CPU, then the GPU read domains will
2968 * need to be invalidated at next use.
2969 */
2970 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002971 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002972 obj->write_domain = I915_GEM_DOMAIN_CPU;
2973 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002974
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002975 trace_i915_gem_object_change_domain(obj,
2976 old_read_domains,
2977 old_write_domain);
2978
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002979 return 0;
2980}
2981
Eric Anholt673a3942008-07-30 12:06:12 -07002982/*
2983 * Set the next domain for the specified object. This
2984 * may not actually perform the necessary flushing/invaliding though,
2985 * as that may want to be batched with other set_domain operations
2986 *
2987 * This is (we hope) the only really tricky part of gem. The goal
2988 * is fairly simple -- track which caches hold bits of the object
2989 * and make sure they remain coherent. A few concrete examples may
2990 * help to explain how it works. For shorthand, we use the notation
2991 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2992 * a pair of read and write domain masks.
2993 *
2994 * Case 1: the batch buffer
2995 *
2996 * 1. Allocated
2997 * 2. Written by CPU
2998 * 3. Mapped to GTT
2999 * 4. Read by GPU
3000 * 5. Unmapped from GTT
3001 * 6. Freed
3002 *
3003 * Let's take these a step at a time
3004 *
3005 * 1. Allocated
3006 * Pages allocated from the kernel may still have
3007 * cache contents, so we set them to (CPU, CPU) always.
3008 * 2. Written by CPU (using pwrite)
3009 * The pwrite function calls set_domain (CPU, CPU) and
3010 * this function does nothing (as nothing changes)
3011 * 3. Mapped by GTT
3012 * This function asserts that the object is not
3013 * currently in any GPU-based read or write domains
3014 * 4. Read by GPU
3015 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3016 * As write_domain is zero, this function adds in the
3017 * current read domains (CPU+COMMAND, 0).
3018 * flush_domains is set to CPU.
3019 * invalidate_domains is set to COMMAND
3020 * clflush is run to get data out of the CPU caches
3021 * then i915_dev_set_domain calls i915_gem_flush to
3022 * emit an MI_FLUSH and drm_agp_chipset_flush
3023 * 5. Unmapped from GTT
3024 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3025 * flush_domains and invalidate_domains end up both zero
3026 * so no flushing/invalidating happens
3027 * 6. Freed
3028 * yay, done
3029 *
3030 * Case 2: The shared render buffer
3031 *
3032 * 1. Allocated
3033 * 2. Mapped to GTT
3034 * 3. Read/written by GPU
3035 * 4. set_domain to (CPU,CPU)
3036 * 5. Read/written by CPU
3037 * 6. Read/written by GPU
3038 *
3039 * 1. Allocated
3040 * Same as last example, (CPU, CPU)
3041 * 2. Mapped to GTT
3042 * Nothing changes (assertions find that it is not in the GPU)
3043 * 3. Read/written by GPU
3044 * execbuffer calls set_domain (RENDER, RENDER)
3045 * flush_domains gets CPU
3046 * invalidate_domains gets GPU
3047 * clflush (obj)
3048 * MI_FLUSH and drm_agp_chipset_flush
3049 * 4. set_domain (CPU, CPU)
3050 * flush_domains gets GPU
3051 * invalidate_domains gets CPU
3052 * wait_rendering (obj) to make sure all drawing is complete.
3053 * This will include an MI_FLUSH to get the data from GPU
3054 * to memory
3055 * clflush (obj) to invalidate the CPU cache
3056 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3057 * 5. Read/written by CPU
3058 * cache lines are loaded and dirtied
3059 * 6. Read written by GPU
3060 * Same as last GPU access
3061 *
3062 * Case 3: The constant buffer
3063 *
3064 * 1. Allocated
3065 * 2. Written by CPU
3066 * 3. Read by GPU
3067 * 4. Updated (written) by CPU again
3068 * 5. Read by GPU
3069 *
3070 * 1. Allocated
3071 * (CPU, CPU)
3072 * 2. Written by CPU
3073 * (CPU, CPU)
3074 * 3. Read by GPU
3075 * (CPU+RENDER, 0)
3076 * flush_domains = CPU
3077 * invalidate_domains = RENDER
3078 * clflush (obj)
3079 * MI_FLUSH
3080 * drm_agp_chipset_flush
3081 * 4. Updated (written) by CPU again
3082 * (CPU, CPU)
3083 * flush_domains = 0 (no previous write domain)
3084 * invalidate_domains = 0 (no new read domains)
3085 * 5. Read by GPU
3086 * (CPU+RENDER, 0)
3087 * flush_domains = CPU
3088 * invalidate_domains = RENDER
3089 * clflush (obj)
3090 * MI_FLUSH
3091 * drm_agp_chipset_flush
3092 */
Keith Packardc0d90822008-11-20 23:11:08 -08003093static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003094i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003095{
3096 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003097 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003098 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003099 uint32_t invalidate_domains = 0;
3100 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003101 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003102
Eric Anholt8b0e3782009-02-19 14:40:50 -08003103 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3104 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003105
Jesse Barnes652c3932009-08-17 13:31:43 -07003106 intel_mark_busy(dev, obj);
3107
Eric Anholt673a3942008-07-30 12:06:12 -07003108 /*
3109 * If the object isn't moving to a new write domain,
3110 * let the object stay in multiple read domains
3111 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003112 if (obj->pending_write_domain == 0)
3113 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003114 else
3115 obj_priv->dirty = 1;
3116
3117 /*
3118 * Flush the current write domain if
3119 * the new read domains don't match. Invalidate
3120 * any read domains which differ from the old
3121 * write domain
3122 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003123 if (obj->write_domain &&
3124 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003125 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003126 invalidate_domains |=
3127 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003128 }
3129 /*
3130 * Invalidate any read caches which may have
3131 * stale data. That is, any new read domains.
3132 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003133 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003134 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003135 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003136
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003137 old_read_domains = obj->read_domains;
3138
Eric Anholtefbeed92009-02-19 14:54:51 -08003139 /* The actual obj->write_domain will be updated with
3140 * pending_write_domain after we emit the accumulated flush for all
3141 * of our domain changes in execbuffers (which clears objects'
3142 * write_domains). So if we have a current write domain that we
3143 * aren't changing, set pending_write_domain to that.
3144 */
3145 if (flush_domains == 0 && obj->pending_write_domain == 0)
3146 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003147 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003148
3149 dev->invalidate_domains |= invalidate_domains;
3150 dev->flush_domains |= flush_domains;
Chris Wilson92204342010-09-18 11:02:01 +01003151 if (obj_priv->ring)
3152 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003153
3154 trace_i915_gem_object_change_domain(obj,
3155 old_read_domains,
3156 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003157}
3158
3159/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003160 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003161 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003162 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3163 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3164 */
3165static void
3166i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3167{
Daniel Vetter23010e42010-03-08 13:35:02 +01003168 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003169
3170 if (!obj_priv->page_cpu_valid)
3171 return;
3172
3173 /* If we're partially in the CPU read domain, finish moving it in.
3174 */
3175 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3176 int i;
3177
3178 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3179 if (obj_priv->page_cpu_valid[i])
3180 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003181 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003182 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003183 }
3184
3185 /* Free the page_cpu_valid mappings which are now stale, whether
3186 * or not we've got I915_GEM_DOMAIN_CPU.
3187 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003188 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003189 obj_priv->page_cpu_valid = NULL;
3190}
3191
3192/**
3193 * Set the CPU read domain on a range of the object.
3194 *
3195 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3196 * not entirely valid. The page_cpu_valid member of the object flags which
3197 * pages have been flushed, and will be respected by
3198 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3199 * of the whole object.
3200 *
3201 * This function returns when the move is complete, including waiting on
3202 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003203 */
3204static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003205i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3206 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003207{
Daniel Vetter23010e42010-03-08 13:35:02 +01003208 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003209 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003210 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003211
Eric Anholte47c68e2008-11-14 13:35:19 -08003212 if (offset == 0 && size == obj->size)
3213 return i915_gem_object_set_to_cpu_domain(obj, 0);
3214
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003215 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003216 if (ret != 0)
3217 return ret;
3218 i915_gem_object_flush_gtt_write_domain(obj);
3219
3220 /* If we're already fully in the CPU read domain, we're done. */
3221 if (obj_priv->page_cpu_valid == NULL &&
3222 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003223 return 0;
3224
Eric Anholte47c68e2008-11-14 13:35:19 -08003225 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3226 * newly adding I915_GEM_DOMAIN_CPU
3227 */
Eric Anholt673a3942008-07-30 12:06:12 -07003228 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003229 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3230 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003231 if (obj_priv->page_cpu_valid == NULL)
3232 return -ENOMEM;
3233 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3234 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003235
3236 /* Flush the cache on any pages that are still invalid from the CPU's
3237 * perspective.
3238 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003239 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3240 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003241 if (obj_priv->page_cpu_valid[i])
3242 continue;
3243
Eric Anholt856fa192009-03-19 14:10:50 -07003244 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003245
3246 obj_priv->page_cpu_valid[i] = 1;
3247 }
3248
Eric Anholte47c68e2008-11-14 13:35:19 -08003249 /* It should now be out of any other write domains, and we can update
3250 * the domain values for our changes.
3251 */
3252 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3253
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003254 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003255 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3256
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003257 trace_i915_gem_object_change_domain(obj,
3258 old_read_domains,
3259 obj->write_domain);
3260
Eric Anholt673a3942008-07-30 12:06:12 -07003261 return 0;
3262}
3263
3264/**
Eric Anholt673a3942008-07-30 12:06:12 -07003265 * Pin an object to the GTT and evaluate the relocations landing in it.
3266 */
3267static int
3268i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3269 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003270 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003271 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003272{
3273 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003274 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003275 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003276 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003277 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003278 bool need_fence;
3279
3280 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3281 obj_priv->tiling_mode != I915_TILING_NONE;
3282
3283 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003284 if (need_fence &&
3285 !i915_gem_object_fence_offset_ok(obj,
3286 obj_priv->tiling_mode)) {
3287 ret = i915_gem_object_unbind(obj);
3288 if (ret)
3289 return ret;
3290 }
Eric Anholt673a3942008-07-30 12:06:12 -07003291
3292 /* Choose the GTT offset for our buffer and put it there. */
3293 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3294 if (ret)
3295 return ret;
3296
Jesse Barnes76446ca2009-12-17 22:05:42 -05003297 /*
3298 * Pre-965 chips need a fence register set up in order to
3299 * properly handle blits to/from tiled surfaces.
3300 */
3301 if (need_fence) {
Chris Wilson53640e12010-09-20 11:40:50 +01003302 ret = i915_gem_object_get_fence_reg(obj, true);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003303 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003304 i915_gem_object_unpin(obj);
3305 return ret;
3306 }
Chris Wilson53640e12010-09-20 11:40:50 +01003307
3308 dev_priv->fence_regs[obj_priv->fence_reg].gpu = true;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003309 }
3310
Eric Anholt673a3942008-07-30 12:06:12 -07003311 entry->offset = obj_priv->gtt_offset;
3312
Eric Anholt673a3942008-07-30 12:06:12 -07003313 /* Apply the relocations, using the GTT aperture to avoid cache
3314 * flushing requirements.
3315 */
3316 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003317 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003318 struct drm_gem_object *target_obj;
3319 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003320 uint32_t reloc_val, reloc_offset;
3321 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003322
Eric Anholt673a3942008-07-30 12:06:12 -07003323 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003324 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003325 if (target_obj == NULL) {
3326 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003327 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003328 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003329 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003330
Chris Wilson8542a0b2009-09-09 21:15:15 +01003331#if WATCH_RELOC
3332 DRM_INFO("%s: obj %p offset %08x target %d "
3333 "read %08x write %08x gtt %08x "
3334 "presumed %08x delta %08x\n",
3335 __func__,
3336 obj,
3337 (int) reloc->offset,
3338 (int) reloc->target_handle,
3339 (int) reloc->read_domains,
3340 (int) reloc->write_domain,
3341 (int) target_obj_priv->gtt_offset,
3342 (int) reloc->presumed_offset,
3343 reloc->delta);
3344#endif
3345
Eric Anholt673a3942008-07-30 12:06:12 -07003346 /* The target buffer should have appeared before us in the
3347 * exec_object list, so it should have a GTT space bound by now.
3348 */
3349 if (target_obj_priv->gtt_space == NULL) {
3350 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003351 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003352 drm_gem_object_unreference(target_obj);
3353 i915_gem_object_unpin(obj);
3354 return -EINVAL;
3355 }
3356
Chris Wilson8542a0b2009-09-09 21:15:15 +01003357 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003358 if (reloc->write_domain & (reloc->write_domain - 1)) {
3359 DRM_ERROR("reloc with multiple write domains: "
3360 "obj %p target %d offset %d "
3361 "read %08x write %08x",
3362 obj, reloc->target_handle,
3363 (int) reloc->offset,
3364 reloc->read_domains,
3365 reloc->write_domain);
3366 return -EINVAL;
3367 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003368 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3369 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3370 DRM_ERROR("reloc with read/write CPU domains: "
3371 "obj %p target %d offset %d "
3372 "read %08x write %08x",
3373 obj, reloc->target_handle,
3374 (int) reloc->offset,
3375 reloc->read_domains,
3376 reloc->write_domain);
3377 drm_gem_object_unreference(target_obj);
3378 i915_gem_object_unpin(obj);
3379 return -EINVAL;
3380 }
3381 if (reloc->write_domain && target_obj->pending_write_domain &&
3382 reloc->write_domain != target_obj->pending_write_domain) {
3383 DRM_ERROR("Write domain conflict: "
3384 "obj %p target %d offset %d "
3385 "new %08x old %08x\n",
3386 obj, reloc->target_handle,
3387 (int) reloc->offset,
3388 reloc->write_domain,
3389 target_obj->pending_write_domain);
3390 drm_gem_object_unreference(target_obj);
3391 i915_gem_object_unpin(obj);
3392 return -EINVAL;
3393 }
3394
3395 target_obj->pending_read_domains |= reloc->read_domains;
3396 target_obj->pending_write_domain |= reloc->write_domain;
3397
3398 /* If the relocation already has the right value in it, no
3399 * more work needs to be done.
3400 */
3401 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3402 drm_gem_object_unreference(target_obj);
3403 continue;
3404 }
3405
3406 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003407 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003408 DRM_ERROR("Relocation beyond object bounds: "
3409 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003410 obj, reloc->target_handle,
3411 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003412 drm_gem_object_unreference(target_obj);
3413 i915_gem_object_unpin(obj);
3414 return -EINVAL;
3415 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003416 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003417 DRM_ERROR("Relocation not 4-byte aligned: "
3418 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003419 obj, reloc->target_handle,
3420 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003421 drm_gem_object_unreference(target_obj);
3422 i915_gem_object_unpin(obj);
3423 return -EINVAL;
3424 }
3425
Chris Wilson8542a0b2009-09-09 21:15:15 +01003426 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003427 if (reloc->delta >= target_obj->size) {
3428 DRM_ERROR("Relocation beyond target object bounds: "
3429 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003430 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003431 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003432 drm_gem_object_unreference(target_obj);
3433 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003434 return -EINVAL;
3435 }
3436
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003437 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3438 if (ret != 0) {
3439 drm_gem_object_unreference(target_obj);
3440 i915_gem_object_unpin(obj);
3441 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003442 }
3443
3444 /* Map the page containing the relocation we're going to
3445 * perform.
3446 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003447 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003448 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3449 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003450 ~(PAGE_SIZE - 1)),
3451 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003452 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003453 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003454 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003455
Eric Anholt673a3942008-07-30 12:06:12 -07003456 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003457 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003458
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003459 /* The updated presumed offset for this entry will be
3460 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003461 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003462 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003463
3464 drm_gem_object_unreference(target_obj);
3465 }
3466
Eric Anholt673a3942008-07-30 12:06:12 -07003467 return 0;
3468}
3469
Eric Anholt673a3942008-07-30 12:06:12 -07003470/* Throttle our rendering by waiting until the ring has completed our requests
3471 * emitted over 20 msec ago.
3472 *
Eric Anholtb9624422009-06-03 07:27:35 +00003473 * Note that if we were to use the current jiffies each time around the loop,
3474 * we wouldn't escape the function with any frames outstanding if the time to
3475 * render a frame was over 20ms.
3476 *
Eric Anholt673a3942008-07-30 12:06:12 -07003477 * This should get us reasonable parallelism between CPU and GPU but also
3478 * relatively low latency when blocking on a particular request to finish.
3479 */
3480static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003481i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003482{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003483 struct drm_i915_private *dev_priv = dev->dev_private;
3484 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003485 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003486 struct drm_i915_gem_request *request;
3487 struct intel_ring_buffer *ring = NULL;
3488 u32 seqno = 0;
3489 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003490
Chris Wilson1c255952010-09-26 11:03:27 +01003491 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003492 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003493 if (time_after_eq(request->emitted_jiffies, recent_enough))
3494 break;
3495
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003496 ring = request->ring;
3497 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003498 }
Chris Wilson1c255952010-09-26 11:03:27 +01003499 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003500
3501 if (seqno == 0)
3502 return 0;
3503
3504 ret = 0;
3505 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3506 /* And wait for the seqno passing without holding any locks and
3507 * causing extra latency for others. This is safe as the irq
3508 * generation is designed to be run atomically and so is
3509 * lockless.
3510 */
3511 ring->user_irq_get(dev, ring);
3512 ret = wait_event_interruptible(ring->irq_queue,
3513 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3514 || atomic_read(&dev_priv->mm.wedged));
3515 ring->user_irq_put(dev, ring);
3516
3517 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3518 ret = -EIO;
3519 }
3520
3521 if (ret == 0)
3522 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003523
Eric Anholt673a3942008-07-30 12:06:12 -07003524 return ret;
3525}
3526
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003527static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003528i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003529 uint32_t buffer_count,
3530 struct drm_i915_gem_relocation_entry **relocs)
3531{
3532 uint32_t reloc_count = 0, reloc_index = 0, i;
3533 int ret;
3534
3535 *relocs = NULL;
3536 for (i = 0; i < buffer_count; i++) {
3537 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3538 return -EINVAL;
3539 reloc_count += exec_list[i].relocation_count;
3540 }
3541
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003542 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003543 if (*relocs == NULL) {
3544 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003545 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003546 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003547
3548 for (i = 0; i < buffer_count; i++) {
3549 struct drm_i915_gem_relocation_entry __user *user_relocs;
3550
3551 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3552
3553 ret = copy_from_user(&(*relocs)[reloc_index],
3554 user_relocs,
3555 exec_list[i].relocation_count *
3556 sizeof(**relocs));
3557 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003558 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003559 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003560 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003561 }
3562
3563 reloc_index += exec_list[i].relocation_count;
3564 }
3565
Florian Mickler2bc43b52009-04-06 22:55:41 +02003566 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003567}
3568
3569static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003570i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003571 uint32_t buffer_count,
3572 struct drm_i915_gem_relocation_entry *relocs)
3573{
3574 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003575 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003576
Chris Wilson93533c22010-01-31 10:40:48 +00003577 if (relocs == NULL)
3578 return 0;
3579
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003580 for (i = 0; i < buffer_count; i++) {
3581 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003582 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003583
3584 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3585
Florian Mickler2bc43b52009-04-06 22:55:41 +02003586 unwritten = copy_to_user(user_relocs,
3587 &relocs[reloc_count],
3588 exec_list[i].relocation_count *
3589 sizeof(*relocs));
3590
3591 if (unwritten) {
3592 ret = -EFAULT;
3593 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003594 }
3595
3596 reloc_count += exec_list[i].relocation_count;
3597 }
3598
Florian Mickler2bc43b52009-04-06 22:55:41 +02003599err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003600 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003601
3602 return ret;
3603}
3604
Chris Wilson83d60792009-06-06 09:45:57 +01003605static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003606i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003607 uint64_t exec_offset)
3608{
3609 uint32_t exec_start, exec_len;
3610
3611 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3612 exec_len = (uint32_t) exec->batch_len;
3613
3614 if ((exec_start | exec_len) & 0x7)
3615 return -EINVAL;
3616
3617 if (!exec_start)
3618 return -EINVAL;
3619
3620 return 0;
3621}
3622
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003623static int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003624i915_gem_wait_for_pending_flip(struct drm_device *dev,
3625 struct drm_gem_object **object_list,
3626 int count)
3627{
3628 drm_i915_private_t *dev_priv = dev->dev_private;
3629 struct drm_i915_gem_object *obj_priv;
3630 DEFINE_WAIT(wait);
3631 int i, ret = 0;
3632
3633 for (;;) {
3634 prepare_to_wait(&dev_priv->pending_flip_queue,
3635 &wait, TASK_INTERRUPTIBLE);
3636 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003637 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003638 if (atomic_read(&obj_priv->pending_flip) > 0)
3639 break;
3640 }
3641 if (i == count)
3642 break;
3643
3644 if (!signal_pending(current)) {
3645 mutex_unlock(&dev->struct_mutex);
3646 schedule();
3647 mutex_lock(&dev->struct_mutex);
3648 continue;
3649 }
3650 ret = -ERESTARTSYS;
3651 break;
3652 }
3653 finish_wait(&dev_priv->pending_flip_queue, &wait);
3654
3655 return ret;
3656}
3657
Chris Wilson8dc5d142010-08-12 12:36:12 +01003658static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003659i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3660 struct drm_file *file_priv,
3661 struct drm_i915_gem_execbuffer2 *args,
3662 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003663{
3664 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003665 struct drm_gem_object **object_list = NULL;
3666 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003667 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003668 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003669 struct drm_i915_gem_relocation_entry *relocs = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003670 struct drm_i915_gem_request *request = NULL;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003671 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003672 uint64_t exec_offset;
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003673 uint32_t reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003674 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003675
Zou Nan hai852835f2010-05-21 09:08:56 +08003676 struct intel_ring_buffer *ring = NULL;
3677
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003678 ret = i915_gem_check_is_wedged(dev);
3679 if (ret)
3680 return ret;
3681
Eric Anholt673a3942008-07-30 12:06:12 -07003682#if WATCH_EXEC
3683 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3684 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3685#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003686 if (args->flags & I915_EXEC_BSD) {
3687 if (!HAS_BSD(dev)) {
3688 DRM_ERROR("execbuf with wrong flag\n");
3689 return -EINVAL;
3690 }
3691 ring = &dev_priv->bsd_ring;
3692 } else {
3693 ring = &dev_priv->render_ring;
3694 }
3695
Eric Anholt4f481ed2008-09-10 14:22:49 -07003696 if (args->buffer_count < 1) {
3697 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3698 return -EINVAL;
3699 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003700 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003701 if (object_list == NULL) {
3702 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003703 args->buffer_count);
3704 ret = -ENOMEM;
3705 goto pre_mutex_err;
3706 }
Eric Anholt673a3942008-07-30 12:06:12 -07003707
Eric Anholt201361a2009-03-11 12:30:04 -07003708 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003709 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3710 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003711 if (cliprects == NULL) {
3712 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003713 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003714 }
Eric Anholt201361a2009-03-11 12:30:04 -07003715
3716 ret = copy_from_user(cliprects,
3717 (struct drm_clip_rect __user *)
3718 (uintptr_t) args->cliprects_ptr,
3719 sizeof(*cliprects) * args->num_cliprects);
3720 if (ret != 0) {
3721 DRM_ERROR("copy %d cliprects failed: %d\n",
3722 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003723 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003724 goto pre_mutex_err;
3725 }
3726 }
3727
Chris Wilson8dc5d142010-08-12 12:36:12 +01003728 request = kzalloc(sizeof(*request), GFP_KERNEL);
3729 if (request == NULL) {
3730 ret = -ENOMEM;
3731 goto pre_mutex_err;
3732 }
3733
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003734 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3735 &relocs);
3736 if (ret != 0)
3737 goto pre_mutex_err;
3738
Chris Wilson76c1dec2010-09-25 11:22:51 +01003739 ret = i915_mutex_lock_interruptible(dev);
3740 if (ret)
3741 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003742
Eric Anholt673a3942008-07-30 12:06:12 -07003743 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003744 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003745 ret = -EBUSY;
3746 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003747 }
3748
Keith Packardac94a962008-11-20 23:30:27 -08003749 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003750 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003751 for (i = 0; i < args->buffer_count; i++) {
3752 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3753 exec_list[i].handle);
3754 if (object_list[i] == NULL) {
3755 DRM_ERROR("Invalid object handle %d at index %d\n",
3756 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003757 /* prevent error path from reading uninitialized data */
3758 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003759 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003760 goto err;
3761 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003762
Daniel Vetter23010e42010-03-08 13:35:02 +01003763 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003764 if (obj_priv->in_execbuffer) {
3765 DRM_ERROR("Object %p appears more than once in object list\n",
3766 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003767 /* prevent error path from reading uninitialized data */
3768 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003769 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003770 goto err;
3771 }
3772 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003773 flips += atomic_read(&obj_priv->pending_flip);
3774 }
3775
3776 if (flips > 0) {
3777 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3778 args->buffer_count);
3779 if (ret)
3780 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003781 }
Eric Anholt673a3942008-07-30 12:06:12 -07003782
Keith Packardac94a962008-11-20 23:30:27 -08003783 /* Pin and relocate */
3784 for (pin_tries = 0; ; pin_tries++) {
3785 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003786 reloc_index = 0;
3787
Keith Packardac94a962008-11-20 23:30:27 -08003788 for (i = 0; i < args->buffer_count; i++) {
3789 object_list[i]->pending_read_domains = 0;
3790 object_list[i]->pending_write_domain = 0;
3791 ret = i915_gem_object_pin_and_relocate(object_list[i],
3792 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003793 &exec_list[i],
3794 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003795 if (ret)
3796 break;
3797 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003798 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003799 }
3800 /* success */
3801 if (ret == 0)
3802 break;
3803
3804 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003805 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003806 if (ret != -ERESTARTSYS) {
3807 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003808 int num_fences = 0;
3809 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003810 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003811
Chris Wilson07f73f62009-09-14 16:50:30 +01003812 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003813 num_fences +=
3814 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3815 obj_priv->tiling_mode != I915_TILING_NONE;
3816 }
3817 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003818 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003819 total_size, num_fences,
3820 ret);
Chris Wilson73aa8082010-09-30 11:46:12 +01003821 DRM_ERROR("%u objects [%u pinned, %u GTT], "
3822 "%zu object bytes [%zu pinned], "
3823 "%zu /%zu gtt bytes\n",
3824 dev_priv->mm.object_count,
3825 dev_priv->mm.pin_count,
3826 dev_priv->mm.gtt_count,
3827 dev_priv->mm.object_memory,
3828 dev_priv->mm.pin_memory,
3829 dev_priv->mm.gtt_memory,
3830 dev_priv->mm.gtt_total);
Chris Wilson07f73f62009-09-14 16:50:30 +01003831 }
Eric Anholt673a3942008-07-30 12:06:12 -07003832 goto err;
3833 }
Keith Packardac94a962008-11-20 23:30:27 -08003834
3835 /* unpin all of our buffers */
3836 for (i = 0; i < pinned; i++)
3837 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003838 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003839
3840 /* evict everyone we can from the aperture */
3841 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003842 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003843 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003844 }
3845
3846 /* Set the pending read domains for the batch buffer to COMMAND */
3847 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003848 if (batch_obj->pending_write_domain) {
3849 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3850 ret = -EINVAL;
3851 goto err;
3852 }
3853 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003854
Chris Wilson83d60792009-06-06 09:45:57 +01003855 /* Sanity check the batch buffer, prior to moving objects */
3856 exec_offset = exec_list[args->buffer_count - 1].offset;
3857 ret = i915_gem_check_execbuffer (args, exec_offset);
3858 if (ret != 0) {
3859 DRM_ERROR("execbuf with invalid offset/length\n");
3860 goto err;
3861 }
3862
Keith Packard646f0f62008-11-20 23:23:03 -08003863 /* Zero the global flush/invalidate flags. These
3864 * will be modified as new domains are computed
3865 * for each object
3866 */
3867 dev->invalidate_domains = 0;
3868 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003869 dev_priv->mm.flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003870
Eric Anholt673a3942008-07-30 12:06:12 -07003871 for (i = 0; i < args->buffer_count; i++) {
3872 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003873
Keith Packard646f0f62008-11-20 23:23:03 -08003874 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003875 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003876 }
3877
Keith Packard646f0f62008-11-20 23:23:03 -08003878 if (dev->invalidate_domains | dev->flush_domains) {
3879#if WATCH_EXEC
3880 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3881 __func__,
3882 dev->invalidate_domains,
3883 dev->flush_domains);
3884#endif
Chris Wilsonc78ec302010-09-20 12:50:23 +01003885 i915_gem_flush(dev, file_priv,
Keith Packard646f0f62008-11-20 23:23:03 -08003886 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003887 dev->flush_domains,
3888 dev_priv->mm.flush_rings);
Daniel Vettera6910432010-02-02 17:08:37 +01003889 }
3890
Eric Anholtefbeed92009-02-19 14:54:51 -08003891 for (i = 0; i < args->buffer_count; i++) {
3892 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003893 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003894 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003895
3896 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003897 if (obj->write_domain)
3898 list_move_tail(&obj_priv->gpu_write_list,
3899 &dev_priv->mm.gpu_write_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01003900
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003901 trace_i915_gem_object_change_domain(obj,
3902 obj->read_domains,
3903 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003904 }
3905
Eric Anholt673a3942008-07-30 12:06:12 -07003906#if WATCH_COHERENCY
3907 for (i = 0; i < args->buffer_count; i++) {
3908 i915_gem_object_check_coherency(object_list[i],
3909 exec_list[i].handle);
3910 }
3911#endif
3912
Eric Anholt673a3942008-07-30 12:06:12 -07003913#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003914 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003915 args->batch_len,
3916 __func__,
3917 ~0);
3918#endif
3919
Eric Anholt673a3942008-07-30 12:06:12 -07003920 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003921 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3922 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003923 if (ret) {
3924 DRM_ERROR("dispatch failed %d\n", ret);
3925 goto err;
3926 }
3927
3928 /*
3929 * Ensure that the commands in the batch buffer are
3930 * finished before the interrupt fires
3931 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003932 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003933
Daniel Vetter617dbe22010-02-11 22:16:02 +01003934 for (i = 0; i < args->buffer_count; i++) {
3935 struct drm_gem_object *obj = object_list[i];
3936 obj_priv = to_intel_bo(obj);
3937
3938 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01003939 }
Chris Wilsona56ba562010-09-28 10:07:56 +01003940
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003941 i915_add_request(dev, file_priv, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003942 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003943
Eric Anholt673a3942008-07-30 12:06:12 -07003944err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003945 for (i = 0; i < pinned; i++)
3946 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003947
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003948 for (i = 0; i < args->buffer_count; i++) {
3949 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003950 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003951 obj_priv->in_execbuffer = false;
3952 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003953 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003954 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003955
Eric Anholt673a3942008-07-30 12:06:12 -07003956 mutex_unlock(&dev->struct_mutex);
3957
Chris Wilson93533c22010-01-31 10:40:48 +00003958pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003959 /* Copy the updated relocations out regardless of current error
3960 * state. Failure to update the relocs would mean that the next
3961 * time userland calls execbuf, it would do so with presumed offset
3962 * state that didn't match the actual object state.
3963 */
3964 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3965 relocs);
3966 if (ret2 != 0) {
3967 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3968
3969 if (ret == 0)
3970 ret = ret2;
3971 }
3972
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003973 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003974 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003975 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003976
3977 return ret;
3978}
3979
Jesse Barnes76446ca2009-12-17 22:05:42 -05003980/*
3981 * Legacy execbuffer just creates an exec2 list from the original exec object
3982 * list array and passes it to the real function.
3983 */
3984int
3985i915_gem_execbuffer(struct drm_device *dev, void *data,
3986 struct drm_file *file_priv)
3987{
3988 struct drm_i915_gem_execbuffer *args = data;
3989 struct drm_i915_gem_execbuffer2 exec2;
3990 struct drm_i915_gem_exec_object *exec_list = NULL;
3991 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3992 int ret, i;
3993
3994#if WATCH_EXEC
3995 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3996 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3997#endif
3998
3999 if (args->buffer_count < 1) {
4000 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4001 return -EINVAL;
4002 }
4003
4004 /* Copy in the exec list from userland */
4005 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4006 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4007 if (exec_list == NULL || exec2_list == NULL) {
4008 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4009 args->buffer_count);
4010 drm_free_large(exec_list);
4011 drm_free_large(exec2_list);
4012 return -ENOMEM;
4013 }
4014 ret = copy_from_user(exec_list,
4015 (struct drm_i915_relocation_entry __user *)
4016 (uintptr_t) args->buffers_ptr,
4017 sizeof(*exec_list) * args->buffer_count);
4018 if (ret != 0) {
4019 DRM_ERROR("copy %d exec entries failed %d\n",
4020 args->buffer_count, ret);
4021 drm_free_large(exec_list);
4022 drm_free_large(exec2_list);
4023 return -EFAULT;
4024 }
4025
4026 for (i = 0; i < args->buffer_count; i++) {
4027 exec2_list[i].handle = exec_list[i].handle;
4028 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4029 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4030 exec2_list[i].alignment = exec_list[i].alignment;
4031 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004032 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004033 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4034 else
4035 exec2_list[i].flags = 0;
4036 }
4037
4038 exec2.buffers_ptr = args->buffers_ptr;
4039 exec2.buffer_count = args->buffer_count;
4040 exec2.batch_start_offset = args->batch_start_offset;
4041 exec2.batch_len = args->batch_len;
4042 exec2.DR1 = args->DR1;
4043 exec2.DR4 = args->DR4;
4044 exec2.num_cliprects = args->num_cliprects;
4045 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004046 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004047
4048 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4049 if (!ret) {
4050 /* Copy the new buffer offsets back to the user's exec list. */
4051 for (i = 0; i < args->buffer_count; i++)
4052 exec_list[i].offset = exec2_list[i].offset;
4053 /* ... and back out to userspace */
4054 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4055 (uintptr_t) args->buffers_ptr,
4056 exec_list,
4057 sizeof(*exec_list) * args->buffer_count);
4058 if (ret) {
4059 ret = -EFAULT;
4060 DRM_ERROR("failed to copy %d exec entries "
4061 "back to user (%d)\n",
4062 args->buffer_count, ret);
4063 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004064 }
4065
4066 drm_free_large(exec_list);
4067 drm_free_large(exec2_list);
4068 return ret;
4069}
4070
4071int
4072i915_gem_execbuffer2(struct drm_device *dev, void *data,
4073 struct drm_file *file_priv)
4074{
4075 struct drm_i915_gem_execbuffer2 *args = data;
4076 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4077 int ret;
4078
4079#if WATCH_EXEC
4080 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4081 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4082#endif
4083
4084 if (args->buffer_count < 1) {
4085 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4086 return -EINVAL;
4087 }
4088
4089 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4090 if (exec2_list == NULL) {
4091 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4092 args->buffer_count);
4093 return -ENOMEM;
4094 }
4095 ret = copy_from_user(exec2_list,
4096 (struct drm_i915_relocation_entry __user *)
4097 (uintptr_t) args->buffers_ptr,
4098 sizeof(*exec2_list) * args->buffer_count);
4099 if (ret != 0) {
4100 DRM_ERROR("copy %d exec entries failed %d\n",
4101 args->buffer_count, ret);
4102 drm_free_large(exec2_list);
4103 return -EFAULT;
4104 }
4105
4106 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4107 if (!ret) {
4108 /* Copy the new buffer offsets back to the user's exec list. */
4109 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4110 (uintptr_t) args->buffers_ptr,
4111 exec2_list,
4112 sizeof(*exec2_list) * args->buffer_count);
4113 if (ret) {
4114 ret = -EFAULT;
4115 DRM_ERROR("failed to copy %d exec entries "
4116 "back to user (%d)\n",
4117 args->buffer_count, ret);
4118 }
4119 }
4120
4121 drm_free_large(exec2_list);
4122 return ret;
4123}
4124
Eric Anholt673a3942008-07-30 12:06:12 -07004125int
4126i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4127{
4128 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004129 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004130 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004131 int ret;
4132
Daniel Vetter778c3542010-05-13 11:49:44 +02004133 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004134 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004135
4136 if (obj_priv->gtt_space != NULL) {
4137 if (alignment == 0)
4138 alignment = i915_gem_get_gtt_alignment(obj);
4139 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004140 WARN(obj_priv->pin_count,
4141 "bo is already pinned with incorrect alignment:"
4142 " offset=%x, req.alignment=%x\n",
4143 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004144 ret = i915_gem_object_unbind(obj);
4145 if (ret)
4146 return ret;
4147 }
4148 }
4149
Eric Anholt673a3942008-07-30 12:06:12 -07004150 if (obj_priv->gtt_space == NULL) {
4151 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004152 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004153 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004154 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004155
Eric Anholt673a3942008-07-30 12:06:12 -07004156 obj_priv->pin_count++;
4157
4158 /* If the object is not active and not pending a flush,
4159 * remove it from the inactive list
4160 */
4161 if (obj_priv->pin_count == 1) {
Chris Wilson73aa8082010-09-30 11:46:12 +01004162 i915_gem_info_add_pin(dev_priv, obj->size);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004163 if (!obj_priv->active)
4164 list_move_tail(&obj_priv->list,
4165 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004166 }
Eric Anholt673a3942008-07-30 12:06:12 -07004167
Chris Wilson23bc5982010-09-29 16:10:57 +01004168 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004169 return 0;
4170}
4171
4172void
4173i915_gem_object_unpin(struct drm_gem_object *obj)
4174{
4175 struct drm_device *dev = obj->dev;
4176 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004177 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004178
Chris Wilson23bc5982010-09-29 16:10:57 +01004179 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004180 obj_priv->pin_count--;
4181 BUG_ON(obj_priv->pin_count < 0);
4182 BUG_ON(obj_priv->gtt_space == NULL);
4183
4184 /* If the object is no longer pinned, and is
4185 * neither active nor being flushed, then stick it on
4186 * the inactive list
4187 */
4188 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004189 if (!obj_priv->active)
Eric Anholt673a3942008-07-30 12:06:12 -07004190 list_move_tail(&obj_priv->list,
4191 &dev_priv->mm.inactive_list);
Chris Wilson73aa8082010-09-30 11:46:12 +01004192 i915_gem_info_remove_pin(dev_priv, obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07004193 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004194 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004195}
4196
4197int
4198i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4199 struct drm_file *file_priv)
4200{
4201 struct drm_i915_gem_pin *args = data;
4202 struct drm_gem_object *obj;
4203 struct drm_i915_gem_object *obj_priv;
4204 int ret;
4205
Eric Anholt673a3942008-07-30 12:06:12 -07004206 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4207 if (obj == NULL) {
4208 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4209 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004210 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004211 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004212 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004213
Chris Wilson76c1dec2010-09-25 11:22:51 +01004214 ret = i915_mutex_lock_interruptible(dev);
4215 if (ret) {
4216 drm_gem_object_unreference_unlocked(obj);
4217 return ret;
4218 }
4219
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004220 if (obj_priv->madv != I915_MADV_WILLNEED) {
4221 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004222 drm_gem_object_unreference(obj);
4223 mutex_unlock(&dev->struct_mutex);
4224 return -EINVAL;
4225 }
4226
Jesse Barnes79e53942008-11-07 14:24:08 -08004227 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4228 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4229 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004230 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004231 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004232 return -EINVAL;
4233 }
4234
4235 obj_priv->user_pin_count++;
4236 obj_priv->pin_filp = file_priv;
4237 if (obj_priv->user_pin_count == 1) {
4238 ret = i915_gem_object_pin(obj, args->alignment);
4239 if (ret != 0) {
4240 drm_gem_object_unreference(obj);
4241 mutex_unlock(&dev->struct_mutex);
4242 return ret;
4243 }
Eric Anholt673a3942008-07-30 12:06:12 -07004244 }
4245
4246 /* XXX - flush the CPU caches for pinned objects
4247 * as the X server doesn't manage domains yet
4248 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004249 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004250 args->offset = obj_priv->gtt_offset;
4251 drm_gem_object_unreference(obj);
4252 mutex_unlock(&dev->struct_mutex);
4253
4254 return 0;
4255}
4256
4257int
4258i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4259 struct drm_file *file_priv)
4260{
4261 struct drm_i915_gem_pin *args = data;
4262 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004263 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004264 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004265
4266 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4267 if (obj == NULL) {
4268 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4269 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004270 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004271 }
4272
Daniel Vetter23010e42010-03-08 13:35:02 +01004273 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004274
4275 ret = i915_mutex_lock_interruptible(dev);
4276 if (ret) {
4277 drm_gem_object_unreference_unlocked(obj);
4278 return ret;
4279 }
4280
Jesse Barnes79e53942008-11-07 14:24:08 -08004281 if (obj_priv->pin_filp != file_priv) {
4282 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4283 args->handle);
4284 drm_gem_object_unreference(obj);
4285 mutex_unlock(&dev->struct_mutex);
4286 return -EINVAL;
4287 }
4288 obj_priv->user_pin_count--;
4289 if (obj_priv->user_pin_count == 0) {
4290 obj_priv->pin_filp = NULL;
4291 i915_gem_object_unpin(obj);
4292 }
Eric Anholt673a3942008-07-30 12:06:12 -07004293
4294 drm_gem_object_unreference(obj);
4295 mutex_unlock(&dev->struct_mutex);
4296 return 0;
4297}
4298
4299int
4300i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4301 struct drm_file *file_priv)
4302{
4303 struct drm_i915_gem_busy *args = data;
4304 struct drm_gem_object *obj;
4305 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004306 int ret;
4307
Eric Anholt673a3942008-07-30 12:06:12 -07004308 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4309 if (obj == NULL) {
4310 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4311 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004312 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004313 }
4314
Chris Wilson76c1dec2010-09-25 11:22:51 +01004315 ret = i915_mutex_lock_interruptible(dev);
4316 if (ret) {
4317 drm_gem_object_unreference_unlocked(obj);
4318 return ret;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004319 }
4320
Chris Wilson0be555b2010-08-04 15:36:30 +01004321 /* Count all active objects as busy, even if they are currently not used
4322 * by the gpu. Users of this interface expect objects to eventually
4323 * become non-busy without any further actions, therefore emit any
4324 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004325 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004326 obj_priv = to_intel_bo(obj);
4327 args->busy = obj_priv->active;
4328 if (args->busy) {
4329 /* Unconditionally flush objects, even when the gpu still uses this
4330 * object. Userspace calling this function indicates that it wants to
4331 * use this buffer rather sooner than later, so issuing the required
4332 * flush earlier is beneficial.
4333 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004334 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4335 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004336 obj_priv->ring,
4337 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004338
4339 /* Update the active list for the hardware's current position.
4340 * Otherwise this only updates on a delayed timer or when irqs
4341 * are actually unmasked, and our working set ends up being
4342 * larger than required.
4343 */
4344 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4345
4346 args->busy = obj_priv->active;
4347 }
Eric Anholt673a3942008-07-30 12:06:12 -07004348
4349 drm_gem_object_unreference(obj);
4350 mutex_unlock(&dev->struct_mutex);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004351 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004352}
4353
4354int
4355i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4356 struct drm_file *file_priv)
4357{
4358 return i915_gem_ring_throttle(dev, file_priv);
4359}
4360
Chris Wilson3ef94da2009-09-14 16:50:29 +01004361int
4362i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4363 struct drm_file *file_priv)
4364{
4365 struct drm_i915_gem_madvise *args = data;
4366 struct drm_gem_object *obj;
4367 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004368 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004369
4370 switch (args->madv) {
4371 case I915_MADV_DONTNEED:
4372 case I915_MADV_WILLNEED:
4373 break;
4374 default:
4375 return -EINVAL;
4376 }
4377
4378 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4379 if (obj == NULL) {
4380 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4381 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004382 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004383 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004384 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004385
Chris Wilson76c1dec2010-09-25 11:22:51 +01004386 ret = i915_mutex_lock_interruptible(dev);
4387 if (ret) {
4388 drm_gem_object_unreference_unlocked(obj);
4389 return ret;
4390 }
4391
Chris Wilson3ef94da2009-09-14 16:50:29 +01004392 if (obj_priv->pin_count) {
4393 drm_gem_object_unreference(obj);
4394 mutex_unlock(&dev->struct_mutex);
4395
4396 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4397 return -EINVAL;
4398 }
4399
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004400 if (obj_priv->madv != __I915_MADV_PURGED)
4401 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004402
Chris Wilson2d7ef392009-09-20 23:13:10 +01004403 /* if the object is no longer bound, discard its backing storage */
4404 if (i915_gem_object_is_purgeable(obj_priv) &&
4405 obj_priv->gtt_space == NULL)
4406 i915_gem_object_truncate(obj);
4407
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004408 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4409
Chris Wilson3ef94da2009-09-14 16:50:29 +01004410 drm_gem_object_unreference(obj);
4411 mutex_unlock(&dev->struct_mutex);
4412
4413 return 0;
4414}
4415
Daniel Vetterac52bc52010-04-09 19:05:06 +00004416struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4417 size_t size)
4418{
Chris Wilson73aa8082010-09-30 11:46:12 +01004419 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004420 struct drm_i915_gem_object *obj;
4421
4422 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4423 if (obj == NULL)
4424 return NULL;
4425
4426 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4427 kfree(obj);
4428 return NULL;
4429 }
4430
Chris Wilson73aa8082010-09-30 11:46:12 +01004431 i915_gem_info_add_obj(dev_priv, size);
4432
Daniel Vetterc397b902010-04-09 19:05:07 +00004433 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4434 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4435
4436 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004437 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004438 obj->fence_reg = I915_FENCE_REG_NONE;
4439 INIT_LIST_HEAD(&obj->list);
4440 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004441 obj->madv = I915_MADV_WILLNEED;
4442
4443 trace_i915_gem_object_create(&obj->base);
4444
4445 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004446}
4447
Eric Anholt673a3942008-07-30 12:06:12 -07004448int i915_gem_init_object(struct drm_gem_object *obj)
4449{
Daniel Vetterc397b902010-04-09 19:05:07 +00004450 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004451
Eric Anholt673a3942008-07-30 12:06:12 -07004452 return 0;
4453}
4454
Chris Wilsonbe726152010-07-23 23:18:50 +01004455static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4456{
4457 struct drm_device *dev = obj->dev;
4458 drm_i915_private_t *dev_priv = dev->dev_private;
4459 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4460 int ret;
4461
4462 ret = i915_gem_object_unbind(obj);
4463 if (ret == -ERESTARTSYS) {
4464 list_move(&obj_priv->list,
4465 &dev_priv->mm.deferred_free_list);
4466 return;
4467 }
4468
4469 if (obj_priv->mmap_offset)
4470 i915_gem_free_mmap_offset(obj);
4471
4472 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004473 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004474
4475 kfree(obj_priv->page_cpu_valid);
4476 kfree(obj_priv->bit_17);
4477 kfree(obj_priv);
4478}
4479
Eric Anholt673a3942008-07-30 12:06:12 -07004480void i915_gem_free_object(struct drm_gem_object *obj)
4481{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004482 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004483 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004484
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004485 trace_i915_gem_object_destroy(obj);
4486
Eric Anholt673a3942008-07-30 12:06:12 -07004487 while (obj_priv->pin_count > 0)
4488 i915_gem_object_unpin(obj);
4489
Dave Airlie71acb5e2008-12-30 20:31:46 +10004490 if (obj_priv->phys_obj)
4491 i915_gem_detach_phys_object(dev, obj);
4492
Chris Wilsonbe726152010-07-23 23:18:50 +01004493 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004494}
4495
Jesse Barnes5669fca2009-02-17 15:13:31 -08004496int
Eric Anholt673a3942008-07-30 12:06:12 -07004497i915_gem_idle(struct drm_device *dev)
4498{
4499 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004500 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004501
Keith Packard6dbe2772008-10-14 21:41:13 -07004502 mutex_lock(&dev->struct_mutex);
4503
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004504 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004505 (dev_priv->render_ring.gem_object == NULL) ||
4506 (HAS_BSD(dev) &&
4507 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004508 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004509 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004510 }
Eric Anholt673a3942008-07-30 12:06:12 -07004511
Chris Wilson29105cc2010-01-07 10:39:13 +00004512 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004513 if (ret) {
4514 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004515 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004516 }
Eric Anholt673a3942008-07-30 12:06:12 -07004517
Chris Wilson29105cc2010-01-07 10:39:13 +00004518 /* Under UMS, be paranoid and evict. */
4519 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004520 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004521 if (ret) {
4522 mutex_unlock(&dev->struct_mutex);
4523 return ret;
4524 }
4525 }
4526
4527 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4528 * We need to replace this with a semaphore, or something.
4529 * And not confound mm.suspended!
4530 */
4531 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004532 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004533
4534 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004535 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004536
Keith Packard6dbe2772008-10-14 21:41:13 -07004537 mutex_unlock(&dev->struct_mutex);
4538
Chris Wilson29105cc2010-01-07 10:39:13 +00004539 /* Cancel the retire work handler, which should be idle now. */
4540 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4541
Eric Anholt673a3942008-07-30 12:06:12 -07004542 return 0;
4543}
4544
Jesse Barnese552eb72010-04-21 11:39:23 -07004545/*
4546 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4547 * over cache flushing.
4548 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004549static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004550i915_gem_init_pipe_control(struct drm_device *dev)
4551{
4552 drm_i915_private_t *dev_priv = dev->dev_private;
4553 struct drm_gem_object *obj;
4554 struct drm_i915_gem_object *obj_priv;
4555 int ret;
4556
Eric Anholt34dc4d42010-05-07 14:30:03 -07004557 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004558 if (obj == NULL) {
4559 DRM_ERROR("Failed to allocate seqno page\n");
4560 ret = -ENOMEM;
4561 goto err;
4562 }
4563 obj_priv = to_intel_bo(obj);
4564 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4565
4566 ret = i915_gem_object_pin(obj, 4096);
4567 if (ret)
4568 goto err_unref;
4569
4570 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4571 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4572 if (dev_priv->seqno_page == NULL)
4573 goto err_unpin;
4574
4575 dev_priv->seqno_obj = obj;
4576 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4577
4578 return 0;
4579
4580err_unpin:
4581 i915_gem_object_unpin(obj);
4582err_unref:
4583 drm_gem_object_unreference(obj);
4584err:
4585 return ret;
4586}
4587
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004588
4589static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004590i915_gem_cleanup_pipe_control(struct drm_device *dev)
4591{
4592 drm_i915_private_t *dev_priv = dev->dev_private;
4593 struct drm_gem_object *obj;
4594 struct drm_i915_gem_object *obj_priv;
4595
4596 obj = dev_priv->seqno_obj;
4597 obj_priv = to_intel_bo(obj);
4598 kunmap(obj_priv->pages[0]);
4599 i915_gem_object_unpin(obj);
4600 drm_gem_object_unreference(obj);
4601 dev_priv->seqno_obj = NULL;
4602
4603 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004604}
4605
Eric Anholt673a3942008-07-30 12:06:12 -07004606int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004607i915_gem_init_ringbuffer(struct drm_device *dev)
4608{
4609 drm_i915_private_t *dev_priv = dev->dev_private;
4610 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004611
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004612 if (HAS_PIPE_CONTROL(dev)) {
4613 ret = i915_gem_init_pipe_control(dev);
4614 if (ret)
4615 return ret;
4616 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004617
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004618 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004619 if (ret)
4620 goto cleanup_pipe_control;
4621
4622 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004623 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004624 if (ret)
4625 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004626 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004627
Chris Wilson6f392d5482010-08-07 11:01:22 +01004628 dev_priv->next_seqno = 1;
4629
Chris Wilson68f95ba2010-05-27 13:18:22 +01004630 return 0;
4631
4632cleanup_render_ring:
4633 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4634cleanup_pipe_control:
4635 if (HAS_PIPE_CONTROL(dev))
4636 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004637 return ret;
4638}
4639
4640void
4641i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4642{
4643 drm_i915_private_t *dev_priv = dev->dev_private;
4644
4645 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004646 if (HAS_BSD(dev))
4647 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004648 if (HAS_PIPE_CONTROL(dev))
4649 i915_gem_cleanup_pipe_control(dev);
4650}
4651
4652int
Eric Anholt673a3942008-07-30 12:06:12 -07004653i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4654 struct drm_file *file_priv)
4655{
4656 drm_i915_private_t *dev_priv = dev->dev_private;
4657 int ret;
4658
Jesse Barnes79e53942008-11-07 14:24:08 -08004659 if (drm_core_check_feature(dev, DRIVER_MODESET))
4660 return 0;
4661
Ben Gamariba1234d2009-09-14 17:48:47 -04004662 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004663 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004664 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004665 }
4666
Eric Anholt673a3942008-07-30 12:06:12 -07004667 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004668 dev_priv->mm.suspended = 0;
4669
4670 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004671 if (ret != 0) {
4672 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004673 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004674 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004675
Zou Nan hai852835f2010-05-21 09:08:56 +08004676 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004677 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004678 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4679 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004680 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004681 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004682 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004683
Chris Wilson5f353082010-06-07 14:03:03 +01004684 ret = drm_irq_install(dev);
4685 if (ret)
4686 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004687
Eric Anholt673a3942008-07-30 12:06:12 -07004688 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004689
4690cleanup_ringbuffer:
4691 mutex_lock(&dev->struct_mutex);
4692 i915_gem_cleanup_ringbuffer(dev);
4693 dev_priv->mm.suspended = 1;
4694 mutex_unlock(&dev->struct_mutex);
4695
4696 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004697}
4698
4699int
4700i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4701 struct drm_file *file_priv)
4702{
Jesse Barnes79e53942008-11-07 14:24:08 -08004703 if (drm_core_check_feature(dev, DRIVER_MODESET))
4704 return 0;
4705
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004706 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004707 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004708}
4709
4710void
4711i915_gem_lastclose(struct drm_device *dev)
4712{
4713 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004714
Eric Anholte806b492009-01-22 09:56:58 -08004715 if (drm_core_check_feature(dev, DRIVER_MODESET))
4716 return;
4717
Keith Packard6dbe2772008-10-14 21:41:13 -07004718 ret = i915_gem_idle(dev);
4719 if (ret)
4720 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004721}
4722
4723void
4724i915_gem_load(struct drm_device *dev)
4725{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004726 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004727 drm_i915_private_t *dev_priv = dev->dev_private;
4728
Eric Anholt673a3942008-07-30 12:06:12 -07004729 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004730 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004731 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004732 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004733 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004734 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004735 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4736 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004737 if (HAS_BSD(dev)) {
4738 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4739 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4740 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004741 for (i = 0; i < 16; i++)
4742 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004743 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4744 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004745 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004746 spin_lock(&shrink_list_lock);
4747 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4748 spin_unlock(&shrink_list_lock);
4749
Dave Airlie94400122010-07-20 13:15:31 +10004750 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4751 if (IS_GEN3(dev)) {
4752 u32 tmp = I915_READ(MI_ARB_STATE);
4753 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4754 /* arb state is a masked write, so set bit + bit in mask */
4755 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4756 I915_WRITE(MI_ARB_STATE, tmp);
4757 }
4758 }
4759
Jesse Barnesde151cf2008-11-12 10:03:55 -08004760 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004761 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4762 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004763
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004764 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004765 dev_priv->num_fence_regs = 16;
4766 else
4767 dev_priv->num_fence_regs = 8;
4768
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004769 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004770 switch (INTEL_INFO(dev)->gen) {
4771 case 6:
4772 for (i = 0; i < 16; i++)
4773 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4774 break;
4775 case 5:
4776 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004777 for (i = 0; i < 16; i++)
4778 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004779 break;
4780 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004781 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4782 for (i = 0; i < 8; i++)
4783 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004784 case 2:
4785 for (i = 0; i < 8; i++)
4786 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4787 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004788 }
Eric Anholt673a3942008-07-30 12:06:12 -07004789 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004790 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004791}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004792
4793/*
4794 * Create a physically contiguous memory object for this object
4795 * e.g. for cursor + overlay regs
4796 */
Chris Wilson995b6762010-08-20 13:23:26 +01004797static int i915_gem_init_phys_object(struct drm_device *dev,
4798 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004799{
4800 drm_i915_private_t *dev_priv = dev->dev_private;
4801 struct drm_i915_gem_phys_object *phys_obj;
4802 int ret;
4803
4804 if (dev_priv->mm.phys_objs[id - 1] || !size)
4805 return 0;
4806
Eric Anholt9a298b22009-03-24 12:23:04 -07004807 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004808 if (!phys_obj)
4809 return -ENOMEM;
4810
4811 phys_obj->id = id;
4812
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004813 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004814 if (!phys_obj->handle) {
4815 ret = -ENOMEM;
4816 goto kfree_obj;
4817 }
4818#ifdef CONFIG_X86
4819 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4820#endif
4821
4822 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4823
4824 return 0;
4825kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004826 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004827 return ret;
4828}
4829
Chris Wilson995b6762010-08-20 13:23:26 +01004830static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004831{
4832 drm_i915_private_t *dev_priv = dev->dev_private;
4833 struct drm_i915_gem_phys_object *phys_obj;
4834
4835 if (!dev_priv->mm.phys_objs[id - 1])
4836 return;
4837
4838 phys_obj = dev_priv->mm.phys_objs[id - 1];
4839 if (phys_obj->cur_obj) {
4840 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4841 }
4842
4843#ifdef CONFIG_X86
4844 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4845#endif
4846 drm_pci_free(dev, phys_obj->handle);
4847 kfree(phys_obj);
4848 dev_priv->mm.phys_objs[id - 1] = NULL;
4849}
4850
4851void i915_gem_free_all_phys_object(struct drm_device *dev)
4852{
4853 int i;
4854
Dave Airlie260883c2009-01-22 17:58:49 +10004855 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004856 i915_gem_free_phys_object(dev, i);
4857}
4858
4859void i915_gem_detach_phys_object(struct drm_device *dev,
4860 struct drm_gem_object *obj)
4861{
4862 struct drm_i915_gem_object *obj_priv;
4863 int i;
4864 int ret;
4865 int page_count;
4866
Daniel Vetter23010e42010-03-08 13:35:02 +01004867 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004868 if (!obj_priv->phys_obj)
4869 return;
4870
Chris Wilson4bdadb92010-01-27 13:36:32 +00004871 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004872 if (ret)
4873 goto out;
4874
4875 page_count = obj->size / PAGE_SIZE;
4876
4877 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004878 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004879 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4880
4881 memcpy(dst, src, PAGE_SIZE);
4882 kunmap_atomic(dst, KM_USER0);
4883 }
Eric Anholt856fa192009-03-19 14:10:50 -07004884 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004885 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004886
4887 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004888out:
4889 obj_priv->phys_obj->cur_obj = NULL;
4890 obj_priv->phys_obj = NULL;
4891}
4892
4893int
4894i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004895 struct drm_gem_object *obj,
4896 int id,
4897 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004898{
4899 drm_i915_private_t *dev_priv = dev->dev_private;
4900 struct drm_i915_gem_object *obj_priv;
4901 int ret = 0;
4902 int page_count;
4903 int i;
4904
4905 if (id > I915_MAX_PHYS_OBJECT)
4906 return -EINVAL;
4907
Daniel Vetter23010e42010-03-08 13:35:02 +01004908 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004909
4910 if (obj_priv->phys_obj) {
4911 if (obj_priv->phys_obj->id == id)
4912 return 0;
4913 i915_gem_detach_phys_object(dev, obj);
4914 }
4915
Dave Airlie71acb5e2008-12-30 20:31:46 +10004916 /* create a new object */
4917 if (!dev_priv->mm.phys_objs[id - 1]) {
4918 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004919 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004920 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004921 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004922 goto out;
4923 }
4924 }
4925
4926 /* bind to the object */
4927 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4928 obj_priv->phys_obj->cur_obj = obj;
4929
Chris Wilson4bdadb92010-01-27 13:36:32 +00004930 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004931 if (ret) {
4932 DRM_ERROR("failed to get page list\n");
4933 goto out;
4934 }
4935
4936 page_count = obj->size / PAGE_SIZE;
4937
4938 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004939 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004940 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4941
4942 memcpy(dst, src, PAGE_SIZE);
4943 kunmap_atomic(src, KM_USER0);
4944 }
4945
Chris Wilsond78b47b2009-06-17 21:52:49 +01004946 i915_gem_object_put_pages(obj);
4947
Dave Airlie71acb5e2008-12-30 20:31:46 +10004948 return 0;
4949out:
4950 return ret;
4951}
4952
4953static int
4954i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4955 struct drm_i915_gem_pwrite *args,
4956 struct drm_file *file_priv)
4957{
Daniel Vetter23010e42010-03-08 13:35:02 +01004958 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004959 void *obj_addr;
4960 int ret;
4961 char __user *user_data;
4962
4963 user_data = (char __user *) (uintptr_t) args->data_ptr;
4964 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4965
Zhao Yakui44d98a62009-10-09 11:39:40 +08004966 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004967 ret = copy_from_user(obj_addr, user_data, args->size);
4968 if (ret)
4969 return -EFAULT;
4970
4971 drm_agp_chipset_flush(dev);
4972 return 0;
4973}
Eric Anholtb9624422009-06-03 07:27:35 +00004974
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004975void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004976{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004977 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004978
4979 /* Clean up our request list when the client is going away, so that
4980 * later retire_requests won't dereference our soon-to-be-gone
4981 * file_priv.
4982 */
Chris Wilson1c255952010-09-26 11:03:27 +01004983 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004984 while (!list_empty(&file_priv->mm.request_list)) {
4985 struct drm_i915_gem_request *request;
4986
4987 request = list_first_entry(&file_priv->mm.request_list,
4988 struct drm_i915_gem_request,
4989 client_list);
4990 list_del(&request->client_list);
4991 request->file_priv = NULL;
4992 }
Chris Wilson1c255952010-09-26 11:03:27 +01004993 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004994}
Chris Wilson31169712009-09-14 16:50:28 +01004995
Chris Wilson31169712009-09-14 16:50:28 +01004996static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004997i915_gpu_is_active(struct drm_device *dev)
4998{
4999 drm_i915_private_t *dev_priv = dev->dev_private;
5000 int lists_empty;
5001
Chris Wilson1637ef42010-04-20 17:10:35 +01005002 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08005003 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005004 if (HAS_BSD(dev))
5005 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005006
5007 return !lists_empty;
5008}
5009
5010static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10005011i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005012{
5013 drm_i915_private_t *dev_priv, *next_dev;
5014 struct drm_i915_gem_object *obj_priv, *next_obj;
5015 int cnt = 0;
5016 int would_deadlock = 1;
5017
5018 /* "fast-path" to count number of available objects */
5019 if (nr_to_scan == 0) {
5020 spin_lock(&shrink_list_lock);
5021 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5022 struct drm_device *dev = dev_priv->dev;
5023
5024 if (mutex_trylock(&dev->struct_mutex)) {
5025 list_for_each_entry(obj_priv,
5026 &dev_priv->mm.inactive_list,
5027 list)
5028 cnt++;
5029 mutex_unlock(&dev->struct_mutex);
5030 }
5031 }
5032 spin_unlock(&shrink_list_lock);
5033
5034 return (cnt / 100) * sysctl_vfs_cache_pressure;
5035 }
5036
5037 spin_lock(&shrink_list_lock);
5038
Chris Wilson1637ef42010-04-20 17:10:35 +01005039rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005040 /* first scan for clean buffers */
5041 list_for_each_entry_safe(dev_priv, next_dev,
5042 &shrink_list, mm.shrink_list) {
5043 struct drm_device *dev = dev_priv->dev;
5044
5045 if (! mutex_trylock(&dev->struct_mutex))
5046 continue;
5047
5048 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005049 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005050
Chris Wilson31169712009-09-14 16:50:28 +01005051 list_for_each_entry_safe(obj_priv, next_obj,
5052 &dev_priv->mm.inactive_list,
5053 list) {
5054 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005055 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005056 if (--nr_to_scan <= 0)
5057 break;
5058 }
5059 }
5060
5061 spin_lock(&shrink_list_lock);
5062 mutex_unlock(&dev->struct_mutex);
5063
Chris Wilson963b4832009-09-20 23:03:54 +01005064 would_deadlock = 0;
5065
Chris Wilson31169712009-09-14 16:50:28 +01005066 if (nr_to_scan <= 0)
5067 break;
5068 }
5069
5070 /* second pass, evict/count anything still on the inactive list */
5071 list_for_each_entry_safe(dev_priv, next_dev,
5072 &shrink_list, mm.shrink_list) {
5073 struct drm_device *dev = dev_priv->dev;
5074
5075 if (! mutex_trylock(&dev->struct_mutex))
5076 continue;
5077
5078 spin_unlock(&shrink_list_lock);
5079
5080 list_for_each_entry_safe(obj_priv, next_obj,
5081 &dev_priv->mm.inactive_list,
5082 list) {
5083 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005084 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005085 nr_to_scan--;
5086 } else
5087 cnt++;
5088 }
5089
5090 spin_lock(&shrink_list_lock);
5091 mutex_unlock(&dev->struct_mutex);
5092
5093 would_deadlock = 0;
5094 }
5095
Chris Wilson1637ef42010-04-20 17:10:35 +01005096 if (nr_to_scan) {
5097 int active = 0;
5098
5099 /*
5100 * We are desperate for pages, so as a last resort, wait
5101 * for the GPU to finish and discard whatever we can.
5102 * This has a dramatic impact to reduce the number of
5103 * OOM-killer events whilst running the GPU aggressively.
5104 */
5105 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5106 struct drm_device *dev = dev_priv->dev;
5107
5108 if (!mutex_trylock(&dev->struct_mutex))
5109 continue;
5110
5111 spin_unlock(&shrink_list_lock);
5112
5113 if (i915_gpu_is_active(dev)) {
5114 i915_gpu_idle(dev);
5115 active++;
5116 }
5117
5118 spin_lock(&shrink_list_lock);
5119 mutex_unlock(&dev->struct_mutex);
5120 }
5121
5122 if (active)
5123 goto rescan;
5124 }
5125
Chris Wilson31169712009-09-14 16:50:28 +01005126 spin_unlock(&shrink_list_lock);
5127
5128 if (would_deadlock)
5129 return -1;
5130 else if (cnt > 0)
5131 return (cnt / 100) * sysctl_vfs_cache_pressure;
5132 else
5133 return 0;
5134}
5135
5136static struct shrinker shrinker = {
5137 .shrink = i915_gem_shrink,
5138 .seeks = DEFAULT_SEEKS,
5139};
5140
5141__init void
5142i915_gem_shrinker_init(void)
5143{
5144 register_shrinker(&shrinker);
5145}
5146
5147__exit void
5148i915_gem_shrinker_exit(void)
5149{
5150 unregister_shrinker(&shrinker);
5151}