blob: 9a8e6752e009ce05872e911f7a48a549b03b0c1a [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 Wilson31169712009-09-14 16:50:28 +010061static LIST_HEAD(shrink_list);
62static DEFINE_SPINLOCK(shrink_list_lock);
63
Chris Wilson30dbf0c2010-09-25 10:19:17 +010064int
65i915_gem_check_is_wedged(struct drm_device *dev)
66{
67 struct drm_i915_private *dev_priv = dev->dev_private;
68 struct completion *x = &dev_priv->error_completion;
69 unsigned long flags;
70 int ret;
71
72 if (!atomic_read(&dev_priv->mm.wedged))
73 return 0;
74
75 ret = wait_for_completion_interruptible(x);
76 if (ret)
77 return ret;
78
79 /* Success, we reset the GPU! */
80 if (!atomic_read(&dev_priv->mm.wedged))
81 return 0;
82
83 /* GPU is hung, bump the completion count to account for
84 * the token we just consumed so that we never hit zero and
85 * end up waiting upon a subsequent completion event that
86 * will never happen.
87 */
88 spin_lock_irqsave(&x->wait.lock, flags);
89 x->done++;
90 spin_unlock_irqrestore(&x->wait.lock, flags);
91 return -EIO;
92}
93
Chris Wilson76c1dec2010-09-25 11:22:51 +010094static int i915_mutex_lock_interruptible(struct drm_device *dev)
95{
96 struct drm_i915_private *dev_priv = dev->dev_private;
97 int ret;
98
99 ret = i915_gem_check_is_wedged(dev);
100 if (ret)
101 return ret;
102
103 ret = mutex_lock_interruptible(&dev->struct_mutex);
104 if (ret)
105 return ret;
106
107 if (atomic_read(&dev_priv->mm.wedged)) {
108 mutex_unlock(&dev->struct_mutex);
109 return -EAGAIN;
110 }
111
112 return 0;
113}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100114
Chris Wilson7d1c4802010-08-07 21:45:03 +0100115static inline bool
116i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
117{
118 return obj_priv->gtt_space &&
119 !obj_priv->active &&
120 obj_priv->pin_count == 0;
121}
122
Jesse Barnes79e53942008-11-07 14:24:08 -0800123int i915_gem_do_init(struct drm_device *dev, unsigned long start,
124 unsigned long end)
125{
126 drm_i915_private_t *dev_priv = dev->dev_private;
127
128 if (start >= end ||
129 (start & (PAGE_SIZE - 1)) != 0 ||
130 (end & (PAGE_SIZE - 1)) != 0) {
131 return -EINVAL;
132 }
133
134 drm_mm_init(&dev_priv->mm.gtt_space, start,
135 end - start);
136
137 dev->gtt_total = (uint32_t) (end - start);
138
139 return 0;
140}
Keith Packard6dbe2772008-10-14 21:41:13 -0700141
Eric Anholt673a3942008-07-30 12:06:12 -0700142int
143i915_gem_init_ioctl(struct drm_device *dev, void *data,
144 struct drm_file *file_priv)
145{
Eric Anholt673a3942008-07-30 12:06:12 -0700146 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800147 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700148
149 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -0800150 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700151 mutex_unlock(&dev->struct_mutex);
152
Jesse Barnes79e53942008-11-07 14:24:08 -0800153 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700154}
155
Eric Anholt5a125c32008-10-22 21:40:13 -0700156int
157i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
158 struct drm_file *file_priv)
159{
Eric Anholt5a125c32008-10-22 21:40:13 -0700160 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700161
162 if (!(dev->driver->driver_features & DRIVER_GEM))
163 return -ENODEV;
164
165 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800166 args->aper_available_size = (args->aper_size -
167 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700168
169 return 0;
170}
171
Eric Anholt673a3942008-07-30 12:06:12 -0700172
173/**
174 * Creates a new mm object and returns a handle to it.
175 */
176int
177i915_gem_create_ioctl(struct drm_device *dev, void *data,
178 struct drm_file *file_priv)
179{
180 struct drm_i915_gem_create *args = data;
181 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300182 int ret;
183 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700184
185 args->size = roundup(args->size, PAGE_SIZE);
186
187 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000188 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700189 if (obj == NULL)
190 return -ENOMEM;
191
192 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100193 if (ret) {
194 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700195 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100196 }
197
198 /* Sink the floating reference from kref_init(handlecount) */
199 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700200
201 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700202 return 0;
203}
204
Eric Anholt40123c12009-03-09 13:42:30 -0700205static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700206fast_shmem_read(struct page **pages,
207 loff_t page_base, int page_offset,
208 char __user *data,
209 int length)
210{
211 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200212 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700213
214 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
215 if (vaddr == NULL)
216 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200217 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700218 kunmap_atomic(vaddr, KM_USER0);
219
Florian Mickler2bc43b52009-04-06 22:55:41 +0200220 if (unwritten)
221 return -EFAULT;
222
223 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700224}
225
Eric Anholt280b7132009-03-12 16:56:27 -0700226static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
227{
228 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100229 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700230
231 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
232 obj_priv->tiling_mode != I915_TILING_NONE;
233}
234
Chris Wilson99a03df2010-05-27 14:15:34 +0100235static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700236slow_shmem_copy(struct page *dst_page,
237 int dst_offset,
238 struct page *src_page,
239 int src_offset,
240 int length)
241{
242 char *dst_vaddr, *src_vaddr;
243
Chris Wilson99a03df2010-05-27 14:15:34 +0100244 dst_vaddr = kmap(dst_page);
245 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700246
247 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
248
Chris Wilson99a03df2010-05-27 14:15:34 +0100249 kunmap(src_page);
250 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700251}
252
Chris Wilson99a03df2010-05-27 14:15:34 +0100253static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700254slow_shmem_bit17_copy(struct page *gpu_page,
255 int gpu_offset,
256 struct page *cpu_page,
257 int cpu_offset,
258 int length,
259 int is_read)
260{
261 char *gpu_vaddr, *cpu_vaddr;
262
263 /* Use the unswizzled path if this page isn't affected. */
264 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
265 if (is_read)
266 return slow_shmem_copy(cpu_page, cpu_offset,
267 gpu_page, gpu_offset, length);
268 else
269 return slow_shmem_copy(gpu_page, gpu_offset,
270 cpu_page, cpu_offset, length);
271 }
272
Chris Wilson99a03df2010-05-27 14:15:34 +0100273 gpu_vaddr = kmap(gpu_page);
274 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700275
276 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
277 * XORing with the other bits (A9 for Y, A9 and A10 for X)
278 */
279 while (length > 0) {
280 int cacheline_end = ALIGN(gpu_offset + 1, 64);
281 int this_length = min(cacheline_end - gpu_offset, length);
282 int swizzled_gpu_offset = gpu_offset ^ 64;
283
284 if (is_read) {
285 memcpy(cpu_vaddr + cpu_offset,
286 gpu_vaddr + swizzled_gpu_offset,
287 this_length);
288 } else {
289 memcpy(gpu_vaddr + swizzled_gpu_offset,
290 cpu_vaddr + cpu_offset,
291 this_length);
292 }
293 cpu_offset += this_length;
294 gpu_offset += this_length;
295 length -= this_length;
296 }
297
Chris Wilson99a03df2010-05-27 14:15:34 +0100298 kunmap(cpu_page);
299 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700300}
301
Eric Anholt673a3942008-07-30 12:06:12 -0700302/**
Eric Anholteb014592009-03-10 11:44:52 -0700303 * This is the fast shmem pread path, which attempts to copy_from_user directly
304 * from the backing pages of the object to the user's address space. On a
305 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
306 */
307static int
308i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
309 struct drm_i915_gem_pread *args,
310 struct drm_file *file_priv)
311{
Daniel Vetter23010e42010-03-08 13:35:02 +0100312 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700313 ssize_t remain;
314 loff_t offset, page_base;
315 char __user *user_data;
316 int page_offset, page_length;
317 int ret;
318
319 user_data = (char __user *) (uintptr_t) args->data_ptr;
320 remain = args->size;
321
Chris Wilson76c1dec2010-09-25 11:22:51 +0100322 ret = i915_mutex_lock_interruptible(dev);
323 if (ret)
324 return ret;
Eric Anholteb014592009-03-10 11:44:52 -0700325
Chris Wilson4bdadb92010-01-27 13:36:32 +0000326 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700327 if (ret != 0)
328 goto fail_unlock;
329
330 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
331 args->size);
332 if (ret != 0)
333 goto fail_put_pages;
334
Daniel Vetter23010e42010-03-08 13:35:02 +0100335 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700336 offset = args->offset;
337
338 while (remain > 0) {
339 /* Operation in this page
340 *
341 * page_base = page offset within aperture
342 * page_offset = offset within page
343 * page_length = bytes to copy for this page
344 */
345 page_base = (offset & ~(PAGE_SIZE-1));
346 page_offset = offset & (PAGE_SIZE-1);
347 page_length = remain;
348 if ((page_offset + remain) > PAGE_SIZE)
349 page_length = PAGE_SIZE - page_offset;
350
351 ret = fast_shmem_read(obj_priv->pages,
352 page_base, page_offset,
353 user_data, page_length);
354 if (ret)
355 goto fail_put_pages;
356
357 remain -= page_length;
358 user_data += page_length;
359 offset += page_length;
360 }
361
362fail_put_pages:
363 i915_gem_object_put_pages(obj);
364fail_unlock:
365 mutex_unlock(&dev->struct_mutex);
366
367 return ret;
368}
369
Chris Wilson07f73f62009-09-14 16:50:30 +0100370static int
371i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
372{
373 int ret;
374
Chris Wilson4bdadb92010-01-27 13:36:32 +0000375 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100376
377 /* If we've insufficient memory to map in the pages, attempt
378 * to make some space by throwing out some old buffers.
379 */
380 if (ret == -ENOMEM) {
381 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100382
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100383 ret = i915_gem_evict_something(dev, obj->size,
384 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100385 if (ret)
386 return ret;
387
Chris Wilson4bdadb92010-01-27 13:36:32 +0000388 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100389 }
390
391 return ret;
392}
393
Eric Anholteb014592009-03-10 11:44:52 -0700394/**
395 * This is the fallback shmem pread path, which allocates temporary storage
396 * in kernel space to copy_to_user into outside of the struct_mutex, so we
397 * can copy out of the object's backing pages while holding the struct mutex
398 * and not take page faults.
399 */
400static int
401i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
402 struct drm_i915_gem_pread *args,
403 struct drm_file *file_priv)
404{
Daniel Vetter23010e42010-03-08 13:35:02 +0100405 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700406 struct mm_struct *mm = current->mm;
407 struct page **user_pages;
408 ssize_t remain;
409 loff_t offset, pinned_pages, i;
410 loff_t first_data_page, last_data_page, num_pages;
411 int shmem_page_index, shmem_page_offset;
412 int data_page_index, data_page_offset;
413 int page_length;
414 int ret;
415 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700416 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700417
418 remain = args->size;
419
420 /* Pin the user pages containing the data. We can't fault while
421 * holding the struct mutex, yet we want to hold it while
422 * dereferencing the user data.
423 */
424 first_data_page = data_ptr / PAGE_SIZE;
425 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
426 num_pages = last_data_page - first_data_page + 1;
427
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700428 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700429 if (user_pages == NULL)
430 return -ENOMEM;
431
432 down_read(&mm->mmap_sem);
433 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700434 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700435 up_read(&mm->mmap_sem);
436 if (pinned_pages < num_pages) {
437 ret = -EFAULT;
438 goto fail_put_user_pages;
439 }
440
Eric Anholt280b7132009-03-12 16:56:27 -0700441 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
442
Chris Wilson76c1dec2010-09-25 11:22:51 +0100443 ret = i915_mutex_lock_interruptible(dev);
444 if (ret)
445 goto fail_put_user_pages;
Eric Anholteb014592009-03-10 11:44:52 -0700446
Chris Wilson07f73f62009-09-14 16:50:30 +0100447 ret = i915_gem_object_get_pages_or_evict(obj);
448 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700449 goto fail_unlock;
450
451 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
452 args->size);
453 if (ret != 0)
454 goto fail_put_pages;
455
Daniel Vetter23010e42010-03-08 13:35:02 +0100456 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700457 offset = args->offset;
458
459 while (remain > 0) {
460 /* Operation in this page
461 *
462 * shmem_page_index = page number within shmem file
463 * shmem_page_offset = offset within page in shmem file
464 * data_page_index = page number in get_user_pages return
465 * data_page_offset = offset with data_page_index page.
466 * page_length = bytes to copy for this page
467 */
468 shmem_page_index = offset / PAGE_SIZE;
469 shmem_page_offset = offset & ~PAGE_MASK;
470 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
471 data_page_offset = data_ptr & ~PAGE_MASK;
472
473 page_length = remain;
474 if ((shmem_page_offset + page_length) > PAGE_SIZE)
475 page_length = PAGE_SIZE - shmem_page_offset;
476 if ((data_page_offset + page_length) > PAGE_SIZE)
477 page_length = PAGE_SIZE - data_page_offset;
478
Eric Anholt280b7132009-03-12 16:56:27 -0700479 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100480 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700481 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100482 user_pages[data_page_index],
483 data_page_offset,
484 page_length,
485 1);
486 } else {
487 slow_shmem_copy(user_pages[data_page_index],
488 data_page_offset,
489 obj_priv->pages[shmem_page_index],
490 shmem_page_offset,
491 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700492 }
Eric Anholteb014592009-03-10 11:44:52 -0700493
494 remain -= page_length;
495 data_ptr += page_length;
496 offset += page_length;
497 }
498
499fail_put_pages:
500 i915_gem_object_put_pages(obj);
501fail_unlock:
502 mutex_unlock(&dev->struct_mutex);
503fail_put_user_pages:
504 for (i = 0; i < pinned_pages; i++) {
505 SetPageDirty(user_pages[i]);
506 page_cache_release(user_pages[i]);
507 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700508 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700509
510 return ret;
511}
512
Eric Anholt673a3942008-07-30 12:06:12 -0700513/**
514 * Reads data from the object referenced by handle.
515 *
516 * On error, the contents of *data are undefined.
517 */
518int
519i915_gem_pread_ioctl(struct drm_device *dev, void *data,
520 struct drm_file *file_priv)
521{
522 struct drm_i915_gem_pread *args = data;
523 struct drm_gem_object *obj;
524 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700525 int ret;
526
527 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
528 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100529 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100530 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700531
532 /* Bounds check source.
533 *
534 * XXX: This could use review for overflow issues...
535 */
536 if (args->offset > obj->size || args->size > obj->size ||
537 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000538 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700539 return -EINVAL;
540 }
541
Eric Anholt280b7132009-03-12 16:56:27 -0700542 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700543 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700544 } else {
545 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
546 if (ret != 0)
547 ret = i915_gem_shmem_pread_slow(dev, obj, args,
548 file_priv);
549 }
Eric Anholt673a3942008-07-30 12:06:12 -0700550
Luca Barbieribc9025b2010-02-09 05:49:12 +0000551 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700552
Eric Anholteb014592009-03-10 11:44:52 -0700553 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700554}
555
Keith Packard0839ccb2008-10-30 19:38:48 -0700556/* This is the fast write path which cannot handle
557 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700558 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700559
Keith Packard0839ccb2008-10-30 19:38:48 -0700560static inline int
561fast_user_write(struct io_mapping *mapping,
562 loff_t page_base, int page_offset,
563 char __user *user_data,
564 int length)
565{
566 char *vaddr_atomic;
567 unsigned long unwritten;
568
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100569 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700570 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
571 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100572 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700573 if (unwritten)
574 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700575 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700576}
577
578/* Here's the write path which can sleep for
579 * page faults
580 */
581
Chris Wilsonab34c222010-05-27 14:15:35 +0100582static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700583slow_kernel_write(struct io_mapping *mapping,
584 loff_t gtt_base, int gtt_offset,
585 struct page *user_page, int user_offset,
586 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700587{
Chris Wilsonab34c222010-05-27 14:15:35 +0100588 char __iomem *dst_vaddr;
589 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700590
Chris Wilsonab34c222010-05-27 14:15:35 +0100591 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
592 src_vaddr = kmap(user_page);
593
594 memcpy_toio(dst_vaddr + gtt_offset,
595 src_vaddr + user_offset,
596 length);
597
598 kunmap(user_page);
599 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700600}
601
Eric Anholt40123c12009-03-09 13:42:30 -0700602static inline int
603fast_shmem_write(struct page **pages,
604 loff_t page_base, int page_offset,
605 char __user *data,
606 int length)
607{
608 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400609 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700610
611 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
612 if (vaddr == NULL)
613 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400614 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700615 kunmap_atomic(vaddr, KM_USER0);
616
Dave Airlied0088772009-03-28 20:29:48 -0400617 if (unwritten)
618 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700619 return 0;
620}
621
Eric Anholt3de09aa2009-03-09 09:42:23 -0700622/**
623 * This is the fast pwrite path, where we copy the data directly from the
624 * user into the GTT, uncached.
625 */
Eric Anholt673a3942008-07-30 12:06:12 -0700626static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700627i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
628 struct drm_i915_gem_pwrite *args,
629 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700630{
Daniel Vetter23010e42010-03-08 13:35:02 +0100631 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700632 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700633 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700634 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700635 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700636 int page_offset, page_length;
637 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700638
639 user_data = (char __user *) (uintptr_t) args->data_ptr;
640 remain = args->size;
641 if (!access_ok(VERIFY_READ, user_data, remain))
642 return -EFAULT;
643
Chris Wilson76c1dec2010-09-25 11:22:51 +0100644 ret = i915_mutex_lock_interruptible(dev);
645 if (ret)
646 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700647
Eric Anholt673a3942008-07-30 12:06:12 -0700648 ret = i915_gem_object_pin(obj, 0);
649 if (ret) {
650 mutex_unlock(&dev->struct_mutex);
651 return ret;
652 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800653 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700654 if (ret)
655 goto fail;
656
Daniel Vetter23010e42010-03-08 13:35:02 +0100657 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700658 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700659
660 while (remain > 0) {
661 /* Operation in this page
662 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700663 * page_base = page offset within aperture
664 * page_offset = offset within page
665 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700666 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700667 page_base = (offset & ~(PAGE_SIZE-1));
668 page_offset = offset & (PAGE_SIZE-1);
669 page_length = remain;
670 if ((page_offset + remain) > PAGE_SIZE)
671 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700672
Keith Packard0839ccb2008-10-30 19:38:48 -0700673 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
674 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700675
Keith Packard0839ccb2008-10-30 19:38:48 -0700676 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700677 * source page isn't available. Return the error and we'll
678 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700679 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700680 if (ret)
681 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700682
Keith Packard0839ccb2008-10-30 19:38:48 -0700683 remain -= page_length;
684 user_data += page_length;
685 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700686 }
Eric Anholt673a3942008-07-30 12:06:12 -0700687
688fail:
689 i915_gem_object_unpin(obj);
690 mutex_unlock(&dev->struct_mutex);
691
692 return ret;
693}
694
Eric Anholt3de09aa2009-03-09 09:42:23 -0700695/**
696 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
697 * the memory and maps it using kmap_atomic for copying.
698 *
699 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
700 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
701 */
Eric Anholt3043c602008-10-02 12:24:47 -0700702static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700703i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
704 struct drm_i915_gem_pwrite *args,
705 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700706{
Daniel Vetter23010e42010-03-08 13:35:02 +0100707 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700708 drm_i915_private_t *dev_priv = dev->dev_private;
709 ssize_t remain;
710 loff_t gtt_page_base, offset;
711 loff_t first_data_page, last_data_page, num_pages;
712 loff_t pinned_pages, i;
713 struct page **user_pages;
714 struct mm_struct *mm = current->mm;
715 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700716 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700717 uint64_t data_ptr = args->data_ptr;
718
719 remain = args->size;
720
721 /* Pin the user pages containing the data. We can't fault while
722 * holding the struct mutex, and all of the pwrite implementations
723 * want to hold it while dereferencing the user data.
724 */
725 first_data_page = data_ptr / PAGE_SIZE;
726 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
727 num_pages = last_data_page - first_data_page + 1;
728
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700729 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700730 if (user_pages == NULL)
731 return -ENOMEM;
732
733 down_read(&mm->mmap_sem);
734 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
735 num_pages, 0, 0, user_pages, NULL);
736 up_read(&mm->mmap_sem);
737 if (pinned_pages < num_pages) {
738 ret = -EFAULT;
739 goto out_unpin_pages;
740 }
741
Chris Wilson76c1dec2010-09-25 11:22:51 +0100742 ret = i915_mutex_lock_interruptible(dev);
743 if (ret)
744 goto out_unpin_pages;
745
Eric Anholt3de09aa2009-03-09 09:42:23 -0700746 ret = i915_gem_object_pin(obj, 0);
747 if (ret)
748 goto out_unlock;
749
750 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
751 if (ret)
752 goto out_unpin_object;
753
Daniel Vetter23010e42010-03-08 13:35:02 +0100754 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700755 offset = obj_priv->gtt_offset + args->offset;
756
757 while (remain > 0) {
758 /* Operation in this page
759 *
760 * gtt_page_base = page offset within aperture
761 * gtt_page_offset = offset within page in aperture
762 * data_page_index = page number in get_user_pages return
763 * data_page_offset = offset with data_page_index page.
764 * page_length = bytes to copy for this page
765 */
766 gtt_page_base = offset & PAGE_MASK;
767 gtt_page_offset = offset & ~PAGE_MASK;
768 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
769 data_page_offset = data_ptr & ~PAGE_MASK;
770
771 page_length = remain;
772 if ((gtt_page_offset + page_length) > PAGE_SIZE)
773 page_length = PAGE_SIZE - gtt_page_offset;
774 if ((data_page_offset + page_length) > PAGE_SIZE)
775 page_length = PAGE_SIZE - data_page_offset;
776
Chris Wilsonab34c222010-05-27 14:15:35 +0100777 slow_kernel_write(dev_priv->mm.gtt_mapping,
778 gtt_page_base, gtt_page_offset,
779 user_pages[data_page_index],
780 data_page_offset,
781 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700782
783 remain -= page_length;
784 offset += page_length;
785 data_ptr += page_length;
786 }
787
788out_unpin_object:
789 i915_gem_object_unpin(obj);
790out_unlock:
791 mutex_unlock(&dev->struct_mutex);
792out_unpin_pages:
793 for (i = 0; i < pinned_pages; i++)
794 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700795 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700796
797 return ret;
798}
799
Eric Anholt40123c12009-03-09 13:42:30 -0700800/**
801 * This is the fast shmem pwrite path, which attempts to directly
802 * copy_from_user into the kmapped pages backing the object.
803 */
Eric Anholt673a3942008-07-30 12:06:12 -0700804static int
Eric Anholt40123c12009-03-09 13:42:30 -0700805i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
806 struct drm_i915_gem_pwrite *args,
807 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700808{
Daniel Vetter23010e42010-03-08 13:35:02 +0100809 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700810 ssize_t remain;
811 loff_t offset, page_base;
812 char __user *user_data;
813 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700814 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700815
816 user_data = (char __user *) (uintptr_t) args->data_ptr;
817 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700818
Chris Wilson76c1dec2010-09-25 11:22:51 +0100819 ret = i915_mutex_lock_interruptible(dev);
820 if (ret)
821 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700822
Chris Wilson4bdadb92010-01-27 13:36:32 +0000823 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700824 if (ret != 0)
825 goto fail_unlock;
826
Eric Anholte47c68e2008-11-14 13:35:19 -0800827 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700828 if (ret != 0)
829 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700830
Daniel Vetter23010e42010-03-08 13:35:02 +0100831 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700832 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700833 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700834
Eric Anholt40123c12009-03-09 13:42:30 -0700835 while (remain > 0) {
836 /* Operation in this page
837 *
838 * page_base = page offset within aperture
839 * page_offset = offset within page
840 * page_length = bytes to copy for this page
841 */
842 page_base = (offset & ~(PAGE_SIZE-1));
843 page_offset = offset & (PAGE_SIZE-1);
844 page_length = remain;
845 if ((page_offset + remain) > PAGE_SIZE)
846 page_length = PAGE_SIZE - page_offset;
847
848 ret = fast_shmem_write(obj_priv->pages,
849 page_base, page_offset,
850 user_data, page_length);
851 if (ret)
852 goto fail_put_pages;
853
854 remain -= page_length;
855 user_data += page_length;
856 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700857 }
858
Eric Anholt40123c12009-03-09 13:42:30 -0700859fail_put_pages:
860 i915_gem_object_put_pages(obj);
861fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700862 mutex_unlock(&dev->struct_mutex);
863
Eric Anholt40123c12009-03-09 13:42:30 -0700864 return ret;
865}
866
867/**
868 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
869 * the memory and maps it using kmap_atomic for copying.
870 *
871 * This avoids taking mmap_sem for faulting on the user's address while the
872 * struct_mutex is held.
873 */
874static int
875i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
876 struct drm_i915_gem_pwrite *args,
877 struct drm_file *file_priv)
878{
Daniel Vetter23010e42010-03-08 13:35:02 +0100879 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700880 struct mm_struct *mm = current->mm;
881 struct page **user_pages;
882 ssize_t remain;
883 loff_t offset, pinned_pages, i;
884 loff_t first_data_page, last_data_page, num_pages;
885 int shmem_page_index, shmem_page_offset;
886 int data_page_index, data_page_offset;
887 int page_length;
888 int ret;
889 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700890 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700891
892 remain = args->size;
893
894 /* Pin the user pages containing the data. We can't fault while
895 * holding the struct mutex, and all of the pwrite implementations
896 * want to hold it while dereferencing the user data.
897 */
898 first_data_page = data_ptr / PAGE_SIZE;
899 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
900 num_pages = last_data_page - first_data_page + 1;
901
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700902 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700903 if (user_pages == NULL)
904 return -ENOMEM;
905
906 down_read(&mm->mmap_sem);
907 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
908 num_pages, 0, 0, user_pages, NULL);
909 up_read(&mm->mmap_sem);
910 if (pinned_pages < num_pages) {
911 ret = -EFAULT;
912 goto fail_put_user_pages;
913 }
914
Eric Anholt280b7132009-03-12 16:56:27 -0700915 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
916
Chris Wilson76c1dec2010-09-25 11:22:51 +0100917 ret = i915_mutex_lock_interruptible(dev);
918 if (ret)
919 goto fail_put_user_pages;
Eric Anholt40123c12009-03-09 13:42:30 -0700920
Chris Wilson07f73f62009-09-14 16:50:30 +0100921 ret = i915_gem_object_get_pages_or_evict(obj);
922 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700923 goto fail_unlock;
924
925 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
926 if (ret != 0)
927 goto fail_put_pages;
928
Daniel Vetter23010e42010-03-08 13:35:02 +0100929 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700930 offset = args->offset;
931 obj_priv->dirty = 1;
932
933 while (remain > 0) {
934 /* Operation in this page
935 *
936 * shmem_page_index = page number within shmem file
937 * shmem_page_offset = offset within page in shmem file
938 * data_page_index = page number in get_user_pages return
939 * data_page_offset = offset with data_page_index page.
940 * page_length = bytes to copy for this page
941 */
942 shmem_page_index = offset / PAGE_SIZE;
943 shmem_page_offset = offset & ~PAGE_MASK;
944 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
945 data_page_offset = data_ptr & ~PAGE_MASK;
946
947 page_length = remain;
948 if ((shmem_page_offset + page_length) > PAGE_SIZE)
949 page_length = PAGE_SIZE - shmem_page_offset;
950 if ((data_page_offset + page_length) > PAGE_SIZE)
951 page_length = PAGE_SIZE - data_page_offset;
952
Eric Anholt280b7132009-03-12 16:56:27 -0700953 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100954 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700955 shmem_page_offset,
956 user_pages[data_page_index],
957 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100958 page_length,
959 0);
960 } else {
961 slow_shmem_copy(obj_priv->pages[shmem_page_index],
962 shmem_page_offset,
963 user_pages[data_page_index],
964 data_page_offset,
965 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700966 }
Eric Anholt40123c12009-03-09 13:42:30 -0700967
968 remain -= page_length;
969 data_ptr += page_length;
970 offset += page_length;
971 }
972
973fail_put_pages:
974 i915_gem_object_put_pages(obj);
975fail_unlock:
976 mutex_unlock(&dev->struct_mutex);
977fail_put_user_pages:
978 for (i = 0; i < pinned_pages; i++)
979 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700980 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700981
982 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700983}
984
985/**
986 * Writes data to the object referenced by handle.
987 *
988 * On error, the contents of the buffer that were to be modified are undefined.
989 */
990int
991i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
992 struct drm_file *file_priv)
993{
994 struct drm_i915_gem_pwrite *args = data;
995 struct drm_gem_object *obj;
996 struct drm_i915_gem_object *obj_priv;
997 int ret = 0;
998
999 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1000 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001001 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001002 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001003
1004 /* Bounds check destination.
1005 *
1006 * XXX: This could use review for overflow issues...
1007 */
1008 if (args->offset > obj->size || args->size > obj->size ||
1009 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +00001010 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001011 return -EINVAL;
1012 }
1013
1014 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1015 * it would end up going through the fenced access, and we'll get
1016 * different detiling behavior between reading and writing.
1017 * pread/pwrite currently are reading and writing from the CPU
1018 * perspective, requiring manual detiling by the client.
1019 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001020 if (obj_priv->phys_obj)
1021 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
1022 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001023 dev->gtt_total != 0 &&
1024 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -07001025 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
1026 if (ret == -EFAULT) {
1027 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
1028 file_priv);
1029 }
Eric Anholt280b7132009-03-12 16:56:27 -07001030 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
1031 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -07001032 } else {
1033 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
1034 if (ret == -EFAULT) {
1035 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
1036 file_priv);
1037 }
1038 }
Eric Anholt673a3942008-07-30 12:06:12 -07001039
1040#if WATCH_PWRITE
1041 if (ret)
1042 DRM_INFO("pwrite failed %d\n", ret);
1043#endif
1044
Luca Barbieribc9025b2010-02-09 05:49:12 +00001045 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001046
1047 return ret;
1048}
1049
1050/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001051 * Called when user space prepares to use an object with the CPU, either
1052 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001053 */
1054int
1055i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1056 struct drm_file *file_priv)
1057{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001058 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001059 struct drm_i915_gem_set_domain *args = data;
1060 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001061 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001062 uint32_t read_domains = args->read_domains;
1063 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001064 int ret;
1065
1066 if (!(dev->driver->driver_features & DRIVER_GEM))
1067 return -ENODEV;
1068
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001069 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001070 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001071 return -EINVAL;
1072
Chris Wilson21d509e2009-06-06 09:46:02 +01001073 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001074 return -EINVAL;
1075
1076 /* Having something in the write domain implies it's in the read
1077 * domain, and only that read domain. Enforce that in the request.
1078 */
1079 if (write_domain != 0 && read_domains != write_domain)
1080 return -EINVAL;
1081
Eric Anholt673a3942008-07-30 12:06:12 -07001082 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1083 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001084 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001085 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001086
Chris Wilson76c1dec2010-09-25 11:22:51 +01001087 ret = i915_mutex_lock_interruptible(dev);
1088 if (ret) {
1089 drm_gem_object_unreference_unlocked(obj);
1090 return ret;
1091 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001092
1093 intel_mark_busy(dev, obj);
1094
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001095 if (read_domains & I915_GEM_DOMAIN_GTT) {
1096 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001097
Eric Anholta09ba7f2009-08-29 12:49:51 -07001098 /* Update the LRU on the fence for the CPU access that's
1099 * about to occur.
1100 */
1101 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001102 struct drm_i915_fence_reg *reg =
1103 &dev_priv->fence_regs[obj_priv->fence_reg];
1104 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001105 &dev_priv->mm.fence_list);
1106 }
1107
Eric Anholt02354392008-11-26 13:58:13 -08001108 /* Silently promote "you're not bound, there was nothing to do"
1109 * to success, since the client was just asking us to
1110 * make sure everything was done.
1111 */
1112 if (ret == -EINVAL)
1113 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001114 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001115 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001116 }
1117
Chris Wilson7d1c4802010-08-07 21:45:03 +01001118 /* Maintain LRU order of "inactive" objects */
1119 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1120 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1121
Eric Anholt673a3942008-07-30 12:06:12 -07001122 drm_gem_object_unreference(obj);
1123 mutex_unlock(&dev->struct_mutex);
1124 return ret;
1125}
1126
1127/**
1128 * Called when user space has done writes to this buffer
1129 */
1130int
1131i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1132 struct drm_file *file_priv)
1133{
1134 struct drm_i915_gem_sw_finish *args = data;
1135 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001136 int ret = 0;
1137
1138 if (!(dev->driver->driver_features & DRIVER_GEM))
1139 return -ENODEV;
1140
Eric Anholt673a3942008-07-30 12:06:12 -07001141 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson76c1dec2010-09-25 11:22:51 +01001142 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001143 return -ENOENT;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001144
1145 ret = i915_mutex_lock_interruptible(dev);
1146 if (ret) {
1147 drm_gem_object_unreference_unlocked(obj);
1148 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001149 }
1150
Eric Anholt673a3942008-07-30 12:06:12 -07001151 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001152 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001153 i915_gem_object_flush_cpu_write_domain(obj);
1154
Eric Anholt673a3942008-07-30 12:06:12 -07001155 drm_gem_object_unreference(obj);
1156 mutex_unlock(&dev->struct_mutex);
1157 return ret;
1158}
1159
1160/**
1161 * Maps the contents of an object, returning the address it is mapped
1162 * into.
1163 *
1164 * While the mapping holds a reference on the contents of the object, it doesn't
1165 * imply a ref on the object itself.
1166 */
1167int
1168i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1169 struct drm_file *file_priv)
1170{
1171 struct drm_i915_gem_mmap *args = data;
1172 struct drm_gem_object *obj;
1173 loff_t offset;
1174 unsigned long addr;
1175
1176 if (!(dev->driver->driver_features & DRIVER_GEM))
1177 return -ENODEV;
1178
1179 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1180 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001181 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001182
1183 offset = args->offset;
1184
1185 down_write(&current->mm->mmap_sem);
1186 addr = do_mmap(obj->filp, 0, args->size,
1187 PROT_READ | PROT_WRITE, MAP_SHARED,
1188 args->offset);
1189 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001190 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001191 if (IS_ERR((void *)addr))
1192 return addr;
1193
1194 args->addr_ptr = (uint64_t) addr;
1195
1196 return 0;
1197}
1198
Jesse Barnesde151cf2008-11-12 10:03:55 -08001199/**
1200 * i915_gem_fault - fault a page into the GTT
1201 * vma: VMA in question
1202 * vmf: fault info
1203 *
1204 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1205 * from userspace. The fault handler takes care of binding the object to
1206 * the GTT (if needed), allocating and programming a fence register (again,
1207 * only if needed based on whether the old reg is still valid or the object
1208 * is tiled) and inserting a new PTE into the faulting process.
1209 *
1210 * Note that the faulting process may involve evicting existing objects
1211 * from the GTT and/or fence registers to make room. So performance may
1212 * suffer if the GTT working set is large or there are few fence registers
1213 * left.
1214 */
1215int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1216{
1217 struct drm_gem_object *obj = vma->vm_private_data;
1218 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001219 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001220 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001221 pgoff_t page_offset;
1222 unsigned long pfn;
1223 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001224 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001225
1226 /* We don't use vmf->pgoff since that has the fake offset */
1227 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1228 PAGE_SHIFT;
1229
1230 /* Now bind it into the GTT if needed */
1231 mutex_lock(&dev->struct_mutex);
1232 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001233 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001234 if (ret)
1235 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001236
Jesse Barnesde151cf2008-11-12 10:03:55 -08001237 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001238 if (ret)
1239 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001240 }
1241
1242 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001243 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001244 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001245 if (ret)
1246 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001247 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001248
Chris Wilson7d1c4802010-08-07 21:45:03 +01001249 if (i915_gem_object_is_inactive(obj_priv))
1250 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1251
Jesse Barnesde151cf2008-11-12 10:03:55 -08001252 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1253 page_offset;
1254
1255 /* Finally, remap it using the new GTT offset */
1256 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001257unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001258 mutex_unlock(&dev->struct_mutex);
1259
1260 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001261 case 0:
1262 case -ERESTARTSYS:
1263 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001264 case -ENOMEM:
1265 case -EAGAIN:
1266 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001267 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001268 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001269 }
1270}
1271
1272/**
1273 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1274 * @obj: obj in question
1275 *
1276 * GEM memory mapping works by handing back to userspace a fake mmap offset
1277 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1278 * up the object based on the offset and sets up the various memory mapping
1279 * structures.
1280 *
1281 * This routine allocates and attaches a fake offset for @obj.
1282 */
1283static int
1284i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1285{
1286 struct drm_device *dev = obj->dev;
1287 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001288 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001289 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001290 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001291 int ret = 0;
1292
1293 /* Set the object up for mmap'ing */
1294 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001295 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001296 if (!list->map)
1297 return -ENOMEM;
1298
1299 map = list->map;
1300 map->type = _DRM_GEM;
1301 map->size = obj->size;
1302 map->handle = obj;
1303
1304 /* Get a DRM GEM mmap offset allocated... */
1305 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1306 obj->size / PAGE_SIZE, 0, 0);
1307 if (!list->file_offset_node) {
1308 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae532010-09-21 15:05:24 +01001309 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001310 goto out_free_list;
1311 }
1312
1313 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1314 obj->size / PAGE_SIZE, 0);
1315 if (!list->file_offset_node) {
1316 ret = -ENOMEM;
1317 goto out_free_list;
1318 }
1319
1320 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae532010-09-21 15:05:24 +01001321 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1322 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001323 DRM_ERROR("failed to add to map hash\n");
1324 goto out_free_mm;
1325 }
1326
1327 /* By now we should be all set, any drm_mmap request on the offset
1328 * below will get to our mmap & fault handler */
1329 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1330
1331 return 0;
1332
1333out_free_mm:
1334 drm_mm_put_block(list->file_offset_node);
1335out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001336 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001337
1338 return ret;
1339}
1340
Chris Wilson901782b2009-07-10 08:18:50 +01001341/**
1342 * i915_gem_release_mmap - remove physical page mappings
1343 * @obj: obj in question
1344 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001345 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001346 * relinquish ownership of the pages back to the system.
1347 *
1348 * It is vital that we remove the page mapping if we have mapped a tiled
1349 * object through the GTT and then lose the fence register due to
1350 * resource pressure. Similarly if the object has been moved out of the
1351 * aperture, than pages mapped into userspace must be revoked. Removing the
1352 * mapping will then trigger a page fault on the next user access, allowing
1353 * fixup by i915_gem_fault().
1354 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001355void
Chris Wilson901782b2009-07-10 08:18:50 +01001356i915_gem_release_mmap(struct drm_gem_object *obj)
1357{
1358 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001359 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001360
1361 if (dev->dev_mapping)
1362 unmap_mapping_range(dev->dev_mapping,
1363 obj_priv->mmap_offset, obj->size, 1);
1364}
1365
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001366static void
1367i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1368{
1369 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001370 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001371 struct drm_gem_mm *mm = dev->mm_private;
1372 struct drm_map_list *list;
1373
1374 list = &obj->map_list;
1375 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1376
1377 if (list->file_offset_node) {
1378 drm_mm_put_block(list->file_offset_node);
1379 list->file_offset_node = NULL;
1380 }
1381
1382 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001383 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001384 list->map = NULL;
1385 }
1386
1387 obj_priv->mmap_offset = 0;
1388}
1389
Jesse Barnesde151cf2008-11-12 10:03:55 -08001390/**
1391 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1392 * @obj: object to check
1393 *
1394 * Return the required GTT alignment for an object, taking into account
1395 * potential fence register mapping if needed.
1396 */
1397static uint32_t
1398i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1399{
1400 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001401 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001402 int start, i;
1403
1404 /*
1405 * Minimum alignment is 4k (GTT page size), but might be greater
1406 * if a fence register is needed for the object.
1407 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001408 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001409 return 4096;
1410
1411 /*
1412 * Previous chips need to be aligned to the size of the smallest
1413 * fence register that can contain the object.
1414 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001415 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001416 start = 1024*1024;
1417 else
1418 start = 512*1024;
1419
1420 for (i = start; i < obj->size; i <<= 1)
1421 ;
1422
1423 return i;
1424}
1425
1426/**
1427 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1428 * @dev: DRM device
1429 * @data: GTT mapping ioctl data
1430 * @file_priv: GEM object info
1431 *
1432 * Simply returns the fake offset to userspace so it can mmap it.
1433 * The mmap call will end up in drm_gem_mmap(), which will set things
1434 * up so we can get faults in the handler above.
1435 *
1436 * The fault handler will take care of binding the object into the GTT
1437 * (since it may have been evicted to make room for something), allocating
1438 * a fence register, and mapping the appropriate aperture address into
1439 * userspace.
1440 */
1441int
1442i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1443 struct drm_file *file_priv)
1444{
1445 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001446 struct drm_gem_object *obj;
1447 struct drm_i915_gem_object *obj_priv;
1448 int ret;
1449
1450 if (!(dev->driver->driver_features & DRIVER_GEM))
1451 return -ENODEV;
1452
1453 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1454 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001455 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001456
Chris Wilson76c1dec2010-09-25 11:22:51 +01001457 ret = i915_mutex_lock_interruptible(dev);
1458 if (ret) {
1459 drm_gem_object_unreference_unlocked(obj);
1460 return ret;
1461 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001462
Daniel Vetter23010e42010-03-08 13:35:02 +01001463 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001464
Chris Wilsonab182822009-09-22 18:46:17 +01001465 if (obj_priv->madv != I915_MADV_WILLNEED) {
1466 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1467 drm_gem_object_unreference(obj);
1468 mutex_unlock(&dev->struct_mutex);
1469 return -EINVAL;
1470 }
1471
1472
Jesse Barnesde151cf2008-11-12 10:03:55 -08001473 if (!obj_priv->mmap_offset) {
1474 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001475 if (ret) {
1476 drm_gem_object_unreference(obj);
1477 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001478 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001479 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001480 }
1481
1482 args->offset = obj_priv->mmap_offset;
1483
Jesse Barnesde151cf2008-11-12 10:03:55 -08001484 /*
1485 * Pull it into the GTT so that we have a page list (makes the
1486 * initial fault faster and any subsequent flushing possible).
1487 */
1488 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001489 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001490 if (ret) {
1491 drm_gem_object_unreference(obj);
1492 mutex_unlock(&dev->struct_mutex);
1493 return ret;
1494 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001495 }
1496
1497 drm_gem_object_unreference(obj);
1498 mutex_unlock(&dev->struct_mutex);
1499
1500 return 0;
1501}
1502
Ben Gamari6911a9b2009-04-02 11:24:54 -07001503void
Eric Anholt856fa192009-03-19 14:10:50 -07001504i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001505{
Daniel Vetter23010e42010-03-08 13:35:02 +01001506 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001507 int page_count = obj->size / PAGE_SIZE;
1508 int i;
1509
Eric Anholt856fa192009-03-19 14:10:50 -07001510 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001511 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001512
1513 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001514 return;
1515
Eric Anholt280b7132009-03-12 16:56:27 -07001516 if (obj_priv->tiling_mode != I915_TILING_NONE)
1517 i915_gem_object_save_bit_17_swizzle(obj);
1518
Chris Wilson3ef94da2009-09-14 16:50:29 +01001519 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001520 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001521
1522 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001523 if (obj_priv->dirty)
1524 set_page_dirty(obj_priv->pages[i]);
1525
1526 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001527 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001528
1529 page_cache_release(obj_priv->pages[i]);
1530 }
Eric Anholt673a3942008-07-30 12:06:12 -07001531 obj_priv->dirty = 0;
1532
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001533 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001534 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001535}
1536
Chris Wilsona56ba562010-09-28 10:07:56 +01001537static uint32_t
1538i915_gem_next_request_seqno(struct drm_device *dev,
1539 struct intel_ring_buffer *ring)
1540{
1541 drm_i915_private_t *dev_priv = dev->dev_private;
1542
1543 ring->outstanding_lazy_request = true;
1544 return dev_priv->next_seqno;
1545}
1546
Eric Anholt673a3942008-07-30 12:06:12 -07001547static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001548i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001549 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001550{
Chris Wilsona56ba562010-09-28 10:07:56 +01001551 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001552 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001553 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001554
Zou Nan hai852835f2010-05-21 09:08:56 +08001555 BUG_ON(ring == NULL);
1556 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001557
1558 /* Add a reference if we're newly entering the active list. */
1559 if (!obj_priv->active) {
1560 drm_gem_object_reference(obj);
1561 obj_priv->active = 1;
1562 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001563
Eric Anholt673a3942008-07-30 12:06:12 -07001564 /* Move from whatever list we were on to the tail of execution. */
Zou Nan hai852835f2010-05-21 09:08:56 +08001565 list_move_tail(&obj_priv->list, &ring->active_list);
Chris Wilsona56ba562010-09-28 10:07:56 +01001566 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001567}
1568
Eric Anholtce44b0e2008-11-06 16:00:31 -08001569static void
1570i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1571{
1572 struct drm_device *dev = obj->dev;
1573 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001574 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001575
1576 BUG_ON(!obj_priv->active);
1577 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1578 obj_priv->last_rendering_seqno = 0;
1579}
Eric Anholt673a3942008-07-30 12:06:12 -07001580
Chris Wilson963b4832009-09-20 23:03:54 +01001581/* Immediately discard the backing storage */
1582static void
1583i915_gem_object_truncate(struct drm_gem_object *obj)
1584{
Daniel Vetter23010e42010-03-08 13:35:02 +01001585 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001586 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001587
Chris Wilsonae9fed62010-08-07 11:01:30 +01001588 /* Our goal here is to return as much of the memory as
1589 * is possible back to the system as we are called from OOM.
1590 * To do this we must instruct the shmfs to drop all of its
1591 * backing pages, *now*. Here we mirror the actions taken
1592 * when by shmem_delete_inode() to release the backing store.
1593 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001594 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001595 truncate_inode_pages(inode->i_mapping, 0);
1596 if (inode->i_op->truncate_range)
1597 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001598
1599 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001600}
1601
1602static inline int
1603i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1604{
1605 return obj_priv->madv == I915_MADV_DONTNEED;
1606}
1607
Eric Anholt673a3942008-07-30 12:06:12 -07001608static void
1609i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1610{
1611 struct drm_device *dev = obj->dev;
1612 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001613 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001614
1615 i915_verify_inactive(dev, __FILE__, __LINE__);
1616 if (obj_priv->pin_count != 0)
Chris Wilsonf13d3f72010-09-20 17:36:15 +01001617 list_move_tail(&obj_priv->list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001618 else
1619 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1620
Daniel Vetter99fcb762010-02-07 16:20:18 +01001621 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1622
Eric Anholtce44b0e2008-11-06 16:00:31 -08001623 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001624 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001625 if (obj_priv->active) {
1626 obj_priv->active = 0;
1627 drm_gem_object_unreference(obj);
1628 }
1629 i915_verify_inactive(dev, __FILE__, __LINE__);
1630}
1631
Chris Wilson92204342010-09-18 11:02:01 +01001632static void
Daniel Vetter63560392010-02-19 11:51:59 +01001633i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001634 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001635 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001636{
1637 drm_i915_private_t *dev_priv = dev->dev_private;
1638 struct drm_i915_gem_object *obj_priv, *next;
1639
1640 list_for_each_entry_safe(obj_priv, next,
1641 &dev_priv->mm.gpu_write_list,
1642 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001643 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001644
Chris Wilson2b6efaa2010-09-14 17:04:02 +01001645 if (obj->write_domain & flush_domains &&
1646 obj_priv->ring == ring) {
Daniel Vetter63560392010-02-19 11:51:59 +01001647 uint32_t old_write_domain = obj->write_domain;
1648
1649 obj->write_domain = 0;
1650 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001651 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001652
1653 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001654 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1655 struct drm_i915_fence_reg *reg =
1656 &dev_priv->fence_regs[obj_priv->fence_reg];
1657 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001658 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001659 }
Daniel Vetter63560392010-02-19 11:51:59 +01001660
1661 trace_i915_gem_object_change_domain(obj,
1662 obj->read_domains,
1663 old_write_domain);
1664 }
1665 }
1666}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001667
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001668uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001669i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001670 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001671 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001672 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001673{
1674 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001675 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001676 uint32_t seqno;
1677 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001678
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001679 if (file != NULL)
1680 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001681
Chris Wilson8dc5d142010-08-12 12:36:12 +01001682 if (request == NULL) {
1683 request = kzalloc(sizeof(*request), GFP_KERNEL);
1684 if (request == NULL)
1685 return 0;
1686 }
Eric Anholt673a3942008-07-30 12:06:12 -07001687
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001688 seqno = ring->add_request(dev, ring, 0);
Chris Wilsona56ba562010-09-28 10:07:56 +01001689 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001690
1691 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001692 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001693 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001694 was_empty = list_empty(&ring->request_list);
1695 list_add_tail(&request->list, &ring->request_list);
1696
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001697 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001698 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001699 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001700 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001701 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001702 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001703 }
Eric Anholt673a3942008-07-30 12:06:12 -07001704
Ben Gamarif65d9422009-09-14 17:48:44 -04001705 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001706 mod_timer(&dev_priv->hangcheck_timer,
1707 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001708 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001709 queue_delayed_work(dev_priv->wq,
1710 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001711 }
Eric Anholt673a3942008-07-30 12:06:12 -07001712 return seqno;
1713}
1714
1715/**
1716 * Command execution barrier
1717 *
1718 * Ensures that all commands in the ring are finished
1719 * before signalling the CPU
1720 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001721static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001722i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001723{
Eric Anholt673a3942008-07-30 12:06:12 -07001724 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001725
1726 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001727 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001728 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001729
1730 ring->flush(dev, ring,
1731 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001732}
1733
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001734static inline void
1735i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001736{
Chris Wilson1c255952010-09-26 11:03:27 +01001737 struct drm_i915_file_private *file_priv = request->file_priv;
1738
1739 if (!file_priv)
1740 return;
1741
1742 spin_lock(&file_priv->mm.lock);
1743 list_del(&request->client_list);
1744 request->file_priv = NULL;
1745 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001746}
1747
Chris Wilsondfaae392010-09-22 10:31:52 +01001748static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1749 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001750{
Chris Wilsondfaae392010-09-22 10:31:52 +01001751 while (!list_empty(&ring->request_list)) {
1752 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001753
Chris Wilsondfaae392010-09-22 10:31:52 +01001754 request = list_first_entry(&ring->request_list,
1755 struct drm_i915_gem_request,
1756 list);
1757
1758 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001759 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001760 kfree(request);
1761 }
1762
1763 while (!list_empty(&ring->active_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001764 struct drm_i915_gem_object *obj_priv;
1765
Chris Wilsondfaae392010-09-22 10:31:52 +01001766 obj_priv = list_first_entry(&ring->active_list,
1767 struct drm_i915_gem_object,
1768 list);
1769
1770 obj_priv->base.write_domain = 0;
1771 list_del_init(&obj_priv->gpu_write_list);
1772 i915_gem_object_move_to_inactive(&obj_priv->base);
1773 }
1774}
1775
1776void i915_gem_reset_lists(struct drm_device *dev)
1777{
1778 struct drm_i915_private *dev_priv = dev->dev_private;
1779 struct drm_i915_gem_object *obj_priv;
1780
1781 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
1782 if (HAS_BSD(dev))
1783 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
1784
1785 /* Remove anything from the flushing lists. The GPU cache is likely
1786 * to be lost on reset along with the data, so simply move the
1787 * lost bo to the inactive list.
1788 */
1789 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001790 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1791 struct drm_i915_gem_object,
1792 list);
1793
1794 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001795 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001796 i915_gem_object_move_to_inactive(&obj_priv->base);
1797 }
Chris Wilson9375e442010-09-19 12:21:28 +01001798
Chris Wilsondfaae392010-09-22 10:31:52 +01001799 /* Move everything out of the GPU domains to ensure we do any
1800 * necessary invalidation upon reuse.
1801 */
Chris Wilson77f01232010-09-19 12:31:36 +01001802 list_for_each_entry(obj_priv,
1803 &dev_priv->mm.inactive_list,
1804 list)
1805 {
1806 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1807 }
1808}
1809
Eric Anholt673a3942008-07-30 12:06:12 -07001810/**
1811 * This function clears the request list as sequence numbers are passed.
1812 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001813static void
1814i915_gem_retire_requests_ring(struct drm_device *dev,
1815 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001816{
1817 drm_i915_private_t *dev_priv = dev->dev_private;
1818 uint32_t seqno;
1819
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001820 if (!ring->status_page.page_addr ||
1821 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001822 return;
1823
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001824 seqno = ring->get_seqno(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001825 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001826 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001827
Zou Nan hai852835f2010-05-21 09:08:56 +08001828 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001829 struct drm_i915_gem_request,
1830 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001831
Chris Wilsondfaae392010-09-22 10:31:52 +01001832 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001833 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001834
1835 trace_i915_gem_request_retire(dev, request->seqno);
1836
1837 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001838 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001839 kfree(request);
1840 }
1841
1842 /* Move any buffers on the active list that are no longer referenced
1843 * by the ringbuffer to the flushing/inactive lists as appropriate.
1844 */
1845 while (!list_empty(&ring->active_list)) {
1846 struct drm_gem_object *obj;
1847 struct drm_i915_gem_object *obj_priv;
1848
1849 obj_priv = list_first_entry(&ring->active_list,
1850 struct drm_i915_gem_object,
1851 list);
1852
Chris Wilsondfaae392010-09-22 10:31:52 +01001853 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001854 break;
1855
1856 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001857 if (obj->write_domain != 0)
1858 i915_gem_object_move_to_flushing(obj);
1859 else
1860 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001861 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001862
1863 if (unlikely (dev_priv->trace_irq_seqno &&
1864 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001865 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001866 dev_priv->trace_irq_seqno = 0;
1867 }
Eric Anholt673a3942008-07-30 12:06:12 -07001868}
1869
1870void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001871i915_gem_retire_requests(struct drm_device *dev)
1872{
1873 drm_i915_private_t *dev_priv = dev->dev_private;
1874
Chris Wilsonbe726152010-07-23 23:18:50 +01001875 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1876 struct drm_i915_gem_object *obj_priv, *tmp;
1877
1878 /* We must be careful that during unbind() we do not
1879 * accidentally infinitely recurse into retire requests.
1880 * Currently:
1881 * retire -> free -> unbind -> wait -> retire_ring
1882 */
1883 list_for_each_entry_safe(obj_priv, tmp,
1884 &dev_priv->mm.deferred_free_list,
1885 list)
1886 i915_gem_free_object_tail(&obj_priv->base);
1887 }
1888
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001889 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1890 if (HAS_BSD(dev))
1891 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1892}
1893
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001894static void
Eric Anholt673a3942008-07-30 12:06:12 -07001895i915_gem_retire_work_handler(struct work_struct *work)
1896{
1897 drm_i915_private_t *dev_priv;
1898 struct drm_device *dev;
1899
1900 dev_priv = container_of(work, drm_i915_private_t,
1901 mm.retire_work.work);
1902 dev = dev_priv->dev;
1903
1904 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001905 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001906
Keith Packard6dbe2772008-10-14 21:41:13 -07001907 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001908 (!list_empty(&dev_priv->render_ring.request_list) ||
1909 (HAS_BSD(dev) &&
1910 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001911 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001912 mutex_unlock(&dev->struct_mutex);
1913}
1914
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001915int
Zou Nan hai852835f2010-05-21 09:08:56 +08001916i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001917 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001918{
1919 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001920 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001921 int ret = 0;
1922
1923 BUG_ON(seqno == 0);
1924
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001925 if (atomic_read(&dev_priv->mm.wedged))
1926 return -EAGAIN;
1927
Chris Wilsona56ba562010-09-28 10:07:56 +01001928 if (ring->outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01001929 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001930 if (seqno == 0)
1931 return -ENOMEM;
1932 }
Chris Wilsona56ba562010-09-28 10:07:56 +01001933 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001934
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001935 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001936 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001937 ier = I915_READ(DEIER) | I915_READ(GTIER);
1938 else
1939 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001940 if (!ier) {
1941 DRM_ERROR("something (likely vbetool) disabled "
1942 "interrupts, re-enabling\n");
1943 i915_driver_irq_preinstall(dev);
1944 i915_driver_irq_postinstall(dev);
1945 }
1946
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001947 trace_i915_gem_request_wait_begin(dev, seqno);
1948
Zou Nan hai852835f2010-05-21 09:08:56 +08001949 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001950 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001951 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001952 ret = wait_event_interruptible(ring->irq_queue,
1953 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001954 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001955 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001956 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001957 wait_event(ring->irq_queue,
1958 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001959 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001960 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001961
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001962 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001963 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001964
1965 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001966 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001967 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001968 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07001969
1970 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01001971 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001972 __func__, ret, seqno, ring->get_seqno(dev, ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01001973 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001974
1975 /* Directly dispatch request retiring. While we have the work queue
1976 * to handle this, the waiter on a request often wants an associated
1977 * buffer to have made it to the inactive list, and we would need
1978 * a separate wait queue to handle that.
1979 */
1980 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001981 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001982
1983 return ret;
1984}
1985
Daniel Vetter48764bf2009-09-15 22:57:32 +02001986/**
1987 * Waits for a sequence number to be signaled, and cleans up the
1988 * request and object lists appropriately for that event.
1989 */
1990static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001991i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01001992 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001993{
Zou Nan hai852835f2010-05-21 09:08:56 +08001994 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001995}
1996
Chris Wilson20f0cd52010-09-23 11:00:38 +01001997static void
Chris Wilson92204342010-09-18 11:02:01 +01001998i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01001999 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002000 struct intel_ring_buffer *ring,
2001 uint32_t invalidate_domains,
2002 uint32_t flush_domains)
2003{
2004 ring->flush(dev, ring, invalidate_domains, flush_domains);
2005 i915_gem_process_flushing_list(dev, flush_domains, ring);
2006}
2007
2008static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002009i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002010 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002011 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002012 uint32_t flush_domains,
2013 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002014{
2015 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002016
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002017 if (flush_domains & I915_GEM_DOMAIN_CPU)
2018 drm_agp_chipset_flush(dev);
Daniel Vetter8bff9172010-02-11 22:19:40 +01002019
Chris Wilson92204342010-09-18 11:02:01 +01002020 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2021 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002022 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002023 &dev_priv->render_ring,
2024 invalidate_domains, flush_domains);
2025 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002026 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002027 &dev_priv->bsd_ring,
2028 invalidate_domains, flush_domains);
2029 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002030}
2031
Eric Anholt673a3942008-07-30 12:06:12 -07002032/**
2033 * Ensures that all rendering to the object has completed and the object is
2034 * safe to unbind from the GTT or access from the CPU.
2035 */
2036static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002037i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2038 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002039{
2040 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002041 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002042 int ret;
2043
Eric Anholte47c68e2008-11-14 13:35:19 -08002044 /* This function only exists to support waiting for existing rendering,
2045 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002046 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002047 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002048
2049 /* If there is rendering queued on the buffer being evicted, wait for
2050 * it.
2051 */
2052 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002053 ret = i915_do_wait_request(dev,
2054 obj_priv->last_rendering_seqno,
2055 interruptible,
2056 obj_priv->ring);
2057 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002058 return ret;
2059 }
2060
2061 return 0;
2062}
2063
2064/**
2065 * Unbinds an object from the GTT aperture.
2066 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002067int
Eric Anholt673a3942008-07-30 12:06:12 -07002068i915_gem_object_unbind(struct drm_gem_object *obj)
2069{
2070 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002071 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002072 int ret = 0;
2073
Eric Anholt673a3942008-07-30 12:06:12 -07002074 if (obj_priv->gtt_space == NULL)
2075 return 0;
2076
2077 if (obj_priv->pin_count != 0) {
2078 DRM_ERROR("Attempting to unbind pinned buffer\n");
2079 return -EINVAL;
2080 }
2081
Eric Anholt5323fd02009-09-09 11:50:45 -07002082 /* blow away mappings if mapped through GTT */
2083 i915_gem_release_mmap(obj);
2084
Eric Anholt673a3942008-07-30 12:06:12 -07002085 /* Move the object to the CPU domain to ensure that
2086 * any possible CPU writes while it's not in the GTT
2087 * are flushed when we go to remap it. This will
2088 * also ensure that all pending GPU writes are finished
2089 * before we unbind.
2090 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002091 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002092 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002093 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002094 /* Continue on if we fail due to EIO, the GPU is hung so we
2095 * should be safe and we need to cleanup or else we might
2096 * cause memory corruption through use-after-free.
2097 */
Eric Anholt673a3942008-07-30 12:06:12 -07002098
Daniel Vetter96b47b62009-12-15 17:50:00 +01002099 /* release the fence reg _after_ flushing */
2100 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2101 i915_gem_clear_fence_reg(obj);
2102
Eric Anholt673a3942008-07-30 12:06:12 -07002103 if (obj_priv->agp_mem != NULL) {
2104 drm_unbind_agp(obj_priv->agp_mem);
2105 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2106 obj_priv->agp_mem = NULL;
2107 }
2108
Eric Anholt856fa192009-03-19 14:10:50 -07002109 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002110 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002111
2112 if (obj_priv->gtt_space) {
2113 atomic_dec(&dev->gtt_count);
2114 atomic_sub(obj->size, &dev->gtt_memory);
2115
2116 drm_mm_put_block(obj_priv->gtt_space);
2117 obj_priv->gtt_space = NULL;
2118 }
2119
Chris Wilsonf13d3f72010-09-20 17:36:15 +01002120 list_del_init(&obj_priv->list);
Eric Anholt673a3942008-07-30 12:06:12 -07002121
Chris Wilson963b4832009-09-20 23:03:54 +01002122 if (i915_gem_object_is_purgeable(obj_priv))
2123 i915_gem_object_truncate(obj);
2124
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002125 trace_i915_gem_object_unbind(obj);
2126
Chris Wilson8dc17752010-07-23 23:18:51 +01002127 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002128}
2129
Chris Wilsona56ba562010-09-28 10:07:56 +01002130static int i915_ring_idle(struct drm_device *dev,
2131 struct intel_ring_buffer *ring)
2132{
2133 i915_gem_flush_ring(dev, NULL, ring,
2134 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2135 return i915_wait_request(dev,
2136 i915_gem_next_request_seqno(dev, ring),
2137 ring);
2138}
2139
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002140int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002141i915_gpu_idle(struct drm_device *dev)
2142{
2143 drm_i915_private_t *dev_priv = dev->dev_private;
2144 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002145 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002146
Zou Nan haid1b851f2010-05-21 09:08:57 +08002147 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2148 list_empty(&dev_priv->render_ring.active_list) &&
2149 (!HAS_BSD(dev) ||
2150 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002151 if (lists_empty)
2152 return 0;
2153
2154 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002155 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002156 if (ret)
2157 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002158
2159 if (HAS_BSD(dev)) {
Chris Wilsona56ba562010-09-28 10:07:56 +01002160 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002161 if (ret)
2162 return ret;
2163 }
2164
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002165 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002166}
2167
Ben Gamari6911a9b2009-04-02 11:24:54 -07002168int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002169i915_gem_object_get_pages(struct drm_gem_object *obj,
2170 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002171{
Daniel Vetter23010e42010-03-08 13:35:02 +01002172 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002173 int page_count, i;
2174 struct address_space *mapping;
2175 struct inode *inode;
2176 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002177
Daniel Vetter778c3542010-05-13 11:49:44 +02002178 BUG_ON(obj_priv->pages_refcount
2179 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2180
Eric Anholt856fa192009-03-19 14:10:50 -07002181 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002182 return 0;
2183
2184 /* Get the list of pages out of our struct file. They'll be pinned
2185 * at this point until we release them.
2186 */
2187 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002188 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002189 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002190 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002191 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002192 return -ENOMEM;
2193 }
2194
2195 inode = obj->filp->f_path.dentry->d_inode;
2196 mapping = inode->i_mapping;
2197 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002198 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002199 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002200 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002201 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002202 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002203 if (IS_ERR(page))
2204 goto err_pages;
2205
Eric Anholt856fa192009-03-19 14:10:50 -07002206 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002207 }
Eric Anholt280b7132009-03-12 16:56:27 -07002208
2209 if (obj_priv->tiling_mode != I915_TILING_NONE)
2210 i915_gem_object_do_bit_17_swizzle(obj);
2211
Eric Anholt673a3942008-07-30 12:06:12 -07002212 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002213
2214err_pages:
2215 while (i--)
2216 page_cache_release(obj_priv->pages[i]);
2217
2218 drm_free_large(obj_priv->pages);
2219 obj_priv->pages = NULL;
2220 obj_priv->pages_refcount--;
2221 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002222}
2223
Eric Anholt4e901fd2009-10-26 16:44:17 -07002224static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2225{
2226 struct drm_gem_object *obj = reg->obj;
2227 struct drm_device *dev = obj->dev;
2228 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002229 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002230 int regnum = obj_priv->fence_reg;
2231 uint64_t val;
2232
2233 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2234 0xfffff000) << 32;
2235 val |= obj_priv->gtt_offset & 0xfffff000;
2236 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2237 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2238
2239 if (obj_priv->tiling_mode == I915_TILING_Y)
2240 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2241 val |= I965_FENCE_REG_VALID;
2242
2243 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2244}
2245
Jesse Barnesde151cf2008-11-12 10:03:55 -08002246static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2247{
2248 struct drm_gem_object *obj = reg->obj;
2249 struct drm_device *dev = obj->dev;
2250 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002251 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002252 int regnum = obj_priv->fence_reg;
2253 uint64_t val;
2254
2255 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2256 0xfffff000) << 32;
2257 val |= obj_priv->gtt_offset & 0xfffff000;
2258 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2259 if (obj_priv->tiling_mode == I915_TILING_Y)
2260 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2261 val |= I965_FENCE_REG_VALID;
2262
2263 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2264}
2265
2266static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2267{
2268 struct drm_gem_object *obj = reg->obj;
2269 struct drm_device *dev = obj->dev;
2270 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002271 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002272 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002273 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002274 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002275 uint32_t pitch_val;
2276
2277 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2278 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002279 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002280 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002281 return;
2282 }
2283
Jesse Barnes0f973f22009-01-26 17:10:45 -08002284 if (obj_priv->tiling_mode == I915_TILING_Y &&
2285 HAS_128_BYTE_Y_TILING(dev))
2286 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002287 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002288 tile_width = 512;
2289
2290 /* Note: pitch better be a power of two tile widths */
2291 pitch_val = obj_priv->stride / tile_width;
2292 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002293
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002294 if (obj_priv->tiling_mode == I915_TILING_Y &&
2295 HAS_128_BYTE_Y_TILING(dev))
2296 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2297 else
2298 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2299
Jesse Barnesde151cf2008-11-12 10:03:55 -08002300 val = obj_priv->gtt_offset;
2301 if (obj_priv->tiling_mode == I915_TILING_Y)
2302 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2303 val |= I915_FENCE_SIZE_BITS(obj->size);
2304 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2305 val |= I830_FENCE_REG_VALID;
2306
Eric Anholtdc529a42009-03-10 22:34:49 -07002307 if (regnum < 8)
2308 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2309 else
2310 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2311 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002312}
2313
2314static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2315{
2316 struct drm_gem_object *obj = reg->obj;
2317 struct drm_device *dev = obj->dev;
2318 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002319 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002320 int regnum = obj_priv->fence_reg;
2321 uint32_t val;
2322 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002323 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002324
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002325 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002326 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002327 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002328 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002329 return;
2330 }
2331
Eric Anholte76a16d2009-05-26 17:44:56 -07002332 pitch_val = obj_priv->stride / 128;
2333 pitch_val = ffs(pitch_val) - 1;
2334 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2335
Jesse Barnesde151cf2008-11-12 10:03:55 -08002336 val = obj_priv->gtt_offset;
2337 if (obj_priv->tiling_mode == I915_TILING_Y)
2338 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002339 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2340 WARN_ON(fence_size_bits & ~0x00000f00);
2341 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002342 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2343 val |= I830_FENCE_REG_VALID;
2344
2345 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002346}
2347
Chris Wilson2cf34d72010-09-14 13:03:28 +01002348static int i915_find_fence_reg(struct drm_device *dev,
2349 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002350{
2351 struct drm_i915_fence_reg *reg = NULL;
2352 struct drm_i915_gem_object *obj_priv = NULL;
2353 struct drm_i915_private *dev_priv = dev->dev_private;
2354 struct drm_gem_object *obj = NULL;
2355 int i, avail, ret;
2356
2357 /* First try to find a free reg */
2358 avail = 0;
2359 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2360 reg = &dev_priv->fence_regs[i];
2361 if (!reg->obj)
2362 return i;
2363
Daniel Vetter23010e42010-03-08 13:35:02 +01002364 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002365 if (!obj_priv->pin_count)
2366 avail++;
2367 }
2368
2369 if (avail == 0)
2370 return -ENOSPC;
2371
2372 /* None available, try to steal one or wait for a user to finish */
2373 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002374 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2375 lru_list) {
2376 obj = reg->obj;
2377 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002378
2379 if (obj_priv->pin_count)
2380 continue;
2381
2382 /* found one! */
2383 i = obj_priv->fence_reg;
2384 break;
2385 }
2386
2387 BUG_ON(i == I915_FENCE_REG_NONE);
2388
2389 /* We only have a reference on obj from the active list. put_fence_reg
2390 * might drop that one, causing a use-after-free in it. So hold a
2391 * private reference to obj like the other callers of put_fence_reg
2392 * (set_tiling ioctl) do. */
2393 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002394 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002395 drm_gem_object_unreference(obj);
2396 if (ret != 0)
2397 return ret;
2398
2399 return i;
2400}
2401
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402/**
2403 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2404 * @obj: object to map through a fence reg
2405 *
2406 * When mapping objects through the GTT, userspace wants to be able to write
2407 * to them without having to worry about swizzling if the object is tiled.
2408 *
2409 * This function walks the fence regs looking for a free one for @obj,
2410 * stealing one if it can't find any.
2411 *
2412 * It then sets up the reg based on the object's properties: address, pitch
2413 * and tiling format.
2414 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002415int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002416i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2417 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002418{
2419 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002420 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002421 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002422 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002423 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002424
Eric Anholta09ba7f2009-08-29 12:49:51 -07002425 /* Just update our place in the LRU if our fence is getting used. */
2426 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002427 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2428 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002429 return 0;
2430 }
2431
Jesse Barnesde151cf2008-11-12 10:03:55 -08002432 switch (obj_priv->tiling_mode) {
2433 case I915_TILING_NONE:
2434 WARN(1, "allocating a fence for non-tiled object?\n");
2435 break;
2436 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002437 if (!obj_priv->stride)
2438 return -EINVAL;
2439 WARN((obj_priv->stride & (512 - 1)),
2440 "object 0x%08x is X tiled but has non-512B pitch\n",
2441 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002442 break;
2443 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002444 if (!obj_priv->stride)
2445 return -EINVAL;
2446 WARN((obj_priv->stride & (128 - 1)),
2447 "object 0x%08x is Y tiled but has non-128B pitch\n",
2448 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002449 break;
2450 }
2451
Chris Wilson2cf34d72010-09-14 13:03:28 +01002452 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002453 if (ret < 0)
2454 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002455
Daniel Vetterae3db242010-02-19 11:51:58 +01002456 obj_priv->fence_reg = ret;
2457 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002458 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002459
Jesse Barnesde151cf2008-11-12 10:03:55 -08002460 reg->obj = obj;
2461
Chris Wilsone259bef2010-09-17 00:32:02 +01002462 switch (INTEL_INFO(dev)->gen) {
2463 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002464 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002465 break;
2466 case 5:
2467 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002468 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002469 break;
2470 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002471 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002472 break;
2473 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002474 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002475 break;
2476 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002477
Daniel Vetterae3db242010-02-19 11:51:58 +01002478 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2479 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002480
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002481 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002482}
2483
2484/**
2485 * i915_gem_clear_fence_reg - clear out fence register info
2486 * @obj: object to clear
2487 *
2488 * Zeroes out the fence register itself and clears out the associated
2489 * data structures in dev_priv and obj_priv.
2490 */
2491static void
2492i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2493{
2494 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002495 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002496 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002497 struct drm_i915_fence_reg *reg =
2498 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002499 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002500
Chris Wilsone259bef2010-09-17 00:32:02 +01002501 switch (INTEL_INFO(dev)->gen) {
2502 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002503 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2504 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002505 break;
2506 case 5:
2507 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002508 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002509 break;
2510 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002511 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002512 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002513 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002514 case 2:
2515 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002516
2517 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002518 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002519 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002520
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002521 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002522 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002523 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002524}
2525
Eric Anholt673a3942008-07-30 12:06:12 -07002526/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002527 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2528 * to the buffer to finish, and then resets the fence register.
2529 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002530 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002531 *
2532 * Zeroes out the fence register itself and clears out the associated
2533 * data structures in dev_priv and obj_priv.
2534 */
2535int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002536i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2537 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002538{
2539 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002540 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002541 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002542 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002543
2544 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2545 return 0;
2546
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002547 /* If we've changed tiling, GTT-mappings of the object
2548 * need to re-fault to ensure that the correct fence register
2549 * setup is in place.
2550 */
2551 i915_gem_release_mmap(obj);
2552
Chris Wilson52dc7d32009-06-06 09:46:01 +01002553 /* On the i915, GPU access to tiled buffers is via a fence,
2554 * therefore we must wait for any outstanding access to complete
2555 * before clearing the fence.
2556 */
Chris Wilson53640e12010-09-20 11:40:50 +01002557 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2558 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002559 int ret;
2560
Chris Wilson2cf34d72010-09-14 13:03:28 +01002561 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002562 if (ret)
2563 return ret;
2564
Chris Wilson2cf34d72010-09-14 13:03:28 +01002565 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002566 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002567 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002568
2569 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002570 }
2571
Daniel Vetter4a726612010-02-01 13:59:16 +01002572 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002573 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002574
2575 return 0;
2576}
2577
2578/**
Eric Anholt673a3942008-07-30 12:06:12 -07002579 * Finds free space in the GTT aperture and binds the object there.
2580 */
2581static int
2582i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2583{
2584 struct drm_device *dev = obj->dev;
2585 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002586 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002587 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002588 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002589 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002590
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002591 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002592 DRM_ERROR("Attempting to bind a purgeable object\n");
2593 return -EINVAL;
2594 }
2595
Eric Anholt673a3942008-07-30 12:06:12 -07002596 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002597 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002598 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002599 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2600 return -EINVAL;
2601 }
2602
Chris Wilson654fc602010-05-27 13:18:21 +01002603 /* If the object is bigger than the entire aperture, reject it early
2604 * before evicting everything in a vain attempt to find space.
2605 */
2606 if (obj->size > dev->gtt_total) {
2607 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2608 return -E2BIG;
2609 }
2610
Eric Anholt673a3942008-07-30 12:06:12 -07002611 search_free:
2612 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2613 obj->size, alignment, 0);
2614 if (free_space != NULL) {
2615 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2616 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002617 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002618 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002619 }
2620 if (obj_priv->gtt_space == NULL) {
2621 /* If the gtt is empty and we're still having trouble
2622 * fitting our object in, we're out of memory.
2623 */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002624 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002625 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002626 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002627
Eric Anholt673a3942008-07-30 12:06:12 -07002628 goto search_free;
2629 }
2630
Chris Wilson4bdadb92010-01-27 13:36:32 +00002631 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002632 if (ret) {
2633 drm_mm_put_block(obj_priv->gtt_space);
2634 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002635
2636 if (ret == -ENOMEM) {
2637 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002638 ret = i915_gem_evict_something(dev, obj->size,
2639 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002640 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002641 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002642 if (gfpmask) {
2643 gfpmask = 0;
2644 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002645 }
2646
2647 return ret;
2648 }
2649
2650 goto search_free;
2651 }
2652
Eric Anholt673a3942008-07-30 12:06:12 -07002653 return ret;
2654 }
2655
Eric Anholt673a3942008-07-30 12:06:12 -07002656 /* Create an AGP memory structure pointing at our pages, and bind it
2657 * into the GTT.
2658 */
2659 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002660 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002661 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002662 obj_priv->gtt_offset,
2663 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002664 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002665 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002666 drm_mm_put_block(obj_priv->gtt_space);
2667 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002668
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002669 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002670 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002671 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002672
2673 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002674 }
2675 atomic_inc(&dev->gtt_count);
2676 atomic_add(obj->size, &dev->gtt_memory);
2677
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002678 /* keep track of bounds object by adding it to the inactive list */
2679 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2680
Eric Anholt673a3942008-07-30 12:06:12 -07002681 /* Assert that the object is not currently in any GPU domain. As it
2682 * wasn't in the GTT, there shouldn't be any way it could have been in
2683 * a GPU cache
2684 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002685 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2686 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002687
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002688 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2689
Eric Anholt673a3942008-07-30 12:06:12 -07002690 return 0;
2691}
2692
2693void
2694i915_gem_clflush_object(struct drm_gem_object *obj)
2695{
Daniel Vetter23010e42010-03-08 13:35:02 +01002696 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002697
2698 /* If we don't have a page list set up, then we're not pinned
2699 * to GPU, and we can ignore the cache flush because it'll happen
2700 * again at bind time.
2701 */
Eric Anholt856fa192009-03-19 14:10:50 -07002702 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002703 return;
2704
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002705 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002706
Eric Anholt856fa192009-03-19 14:10:50 -07002707 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002708}
2709
Eric Anholte47c68e2008-11-14 13:35:19 -08002710/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002711static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002712i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2713 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002714{
2715 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002716 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002717
2718 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002719 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002720
2721 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002722 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002723 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002724 to_intel_bo(obj)->ring,
2725 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002726 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002727
2728 trace_i915_gem_object_change_domain(obj,
2729 obj->read_domains,
2730 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002731
2732 if (pipelined)
2733 return 0;
2734
Chris Wilson2cf34d72010-09-14 13:03:28 +01002735 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002736}
2737
2738/** Flushes the GTT write domain for the object if it's dirty. */
2739static void
2740i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2741{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002742 uint32_t old_write_domain;
2743
Eric Anholte47c68e2008-11-14 13:35:19 -08002744 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2745 return;
2746
2747 /* No actual flushing is required for the GTT write domain. Writes
2748 * to it immediately go to main memory as far as we know, so there's
2749 * no chipset flush. It also doesn't land in render cache.
2750 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002751 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002752 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002753
2754 trace_i915_gem_object_change_domain(obj,
2755 obj->read_domains,
2756 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002757}
2758
2759/** Flushes the CPU write domain for the object if it's dirty. */
2760static void
2761i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2762{
2763 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002764 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002765
2766 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2767 return;
2768
2769 i915_gem_clflush_object(obj);
2770 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002771 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002772 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002773
2774 trace_i915_gem_object_change_domain(obj,
2775 obj->read_domains,
2776 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002777}
2778
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002779/**
2780 * Moves a single object to the GTT read, and possibly write domain.
2781 *
2782 * This function returns when the move is complete, including waiting on
2783 * flushes to occur.
2784 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002785int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002786i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2787{
Daniel Vetter23010e42010-03-08 13:35:02 +01002788 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002789 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002790 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002791
Eric Anholt02354392008-11-26 13:58:13 -08002792 /* Not valid to be called on unbound objects. */
2793 if (obj_priv->gtt_space == NULL)
2794 return -EINVAL;
2795
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002796 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002797 if (ret != 0)
2798 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002799
Chris Wilson72133422010-09-13 23:56:38 +01002800 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002801
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002802 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002803 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002804 if (ret)
2805 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002806 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002807
Chris Wilson72133422010-09-13 23:56:38 +01002808 old_write_domain = obj->write_domain;
2809 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002810
2811 /* It should now be out of any other write domains, and we can update
2812 * the domain values for our changes.
2813 */
2814 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2815 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002816 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002817 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002818 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002819 obj_priv->dirty = 1;
2820 }
2821
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002822 trace_i915_gem_object_change_domain(obj,
2823 old_read_domains,
2824 old_write_domain);
2825
Eric Anholte47c68e2008-11-14 13:35:19 -08002826 return 0;
2827}
2828
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002829/*
2830 * Prepare buffer for display plane. Use uninterruptible for possible flush
2831 * wait, as in modesetting process we're not supposed to be interrupted.
2832 */
2833int
Chris Wilson48b956c2010-09-14 12:50:34 +01002834i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2835 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002836{
Daniel Vetter23010e42010-03-08 13:35:02 +01002837 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002838 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002839 int ret;
2840
2841 /* Not valid to be called on unbound objects. */
2842 if (obj_priv->gtt_space == NULL)
2843 return -EINVAL;
2844
Chris Wilsonced270f2010-09-26 22:47:46 +01002845 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson48b956c2010-09-14 12:50:34 +01002846 if (ret)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002847 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002848
Chris Wilsonced270f2010-09-26 22:47:46 +01002849 /* Currently, we are always called from an non-interruptible context. */
2850 if (!pipelined) {
2851 ret = i915_gem_object_wait_rendering(obj, false);
2852 if (ret)
2853 return ret;
2854 }
2855
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002856 i915_gem_object_flush_cpu_write_domain(obj);
2857
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002858 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002859 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002860
2861 trace_i915_gem_object_change_domain(obj,
2862 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002863 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002864
2865 return 0;
2866}
2867
Eric Anholte47c68e2008-11-14 13:35:19 -08002868/**
2869 * Moves a single object to the CPU read, and possibly write domain.
2870 *
2871 * This function returns when the move is complete, including waiting on
2872 * flushes to occur.
2873 */
2874static int
2875i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2876{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002877 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002878 int ret;
2879
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002880 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002881 if (ret != 0)
2882 return ret;
2883
2884 i915_gem_object_flush_gtt_write_domain(obj);
2885
2886 /* If we have a partially-valid cache of the object in the CPU,
2887 * finish invalidating it and free the per-page flags.
2888 */
2889 i915_gem_object_set_to_full_cpu_read_domain(obj);
2890
Chris Wilson72133422010-09-13 23:56:38 +01002891 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002892 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002893 if (ret)
2894 return ret;
2895 }
2896
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002897 old_write_domain = obj->write_domain;
2898 old_read_domains = obj->read_domains;
2899
Eric Anholte47c68e2008-11-14 13:35:19 -08002900 /* Flush the CPU cache if it's still invalid. */
2901 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2902 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002903
2904 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2905 }
2906
2907 /* It should now be out of any other write domains, and we can update
2908 * the domain values for our changes.
2909 */
2910 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2911
2912 /* If we're writing through the CPU, then the GPU read domains will
2913 * need to be invalidated at next use.
2914 */
2915 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002916 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002917 obj->write_domain = I915_GEM_DOMAIN_CPU;
2918 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002919
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002920 trace_i915_gem_object_change_domain(obj,
2921 old_read_domains,
2922 old_write_domain);
2923
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002924 return 0;
2925}
2926
Eric Anholt673a3942008-07-30 12:06:12 -07002927/*
2928 * Set the next domain for the specified object. This
2929 * may not actually perform the necessary flushing/invaliding though,
2930 * as that may want to be batched with other set_domain operations
2931 *
2932 * This is (we hope) the only really tricky part of gem. The goal
2933 * is fairly simple -- track which caches hold bits of the object
2934 * and make sure they remain coherent. A few concrete examples may
2935 * help to explain how it works. For shorthand, we use the notation
2936 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2937 * a pair of read and write domain masks.
2938 *
2939 * Case 1: the batch buffer
2940 *
2941 * 1. Allocated
2942 * 2. Written by CPU
2943 * 3. Mapped to GTT
2944 * 4. Read by GPU
2945 * 5. Unmapped from GTT
2946 * 6. Freed
2947 *
2948 * Let's take these a step at a time
2949 *
2950 * 1. Allocated
2951 * Pages allocated from the kernel may still have
2952 * cache contents, so we set them to (CPU, CPU) always.
2953 * 2. Written by CPU (using pwrite)
2954 * The pwrite function calls set_domain (CPU, CPU) and
2955 * this function does nothing (as nothing changes)
2956 * 3. Mapped by GTT
2957 * This function asserts that the object is not
2958 * currently in any GPU-based read or write domains
2959 * 4. Read by GPU
2960 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2961 * As write_domain is zero, this function adds in the
2962 * current read domains (CPU+COMMAND, 0).
2963 * flush_domains is set to CPU.
2964 * invalidate_domains is set to COMMAND
2965 * clflush is run to get data out of the CPU caches
2966 * then i915_dev_set_domain calls i915_gem_flush to
2967 * emit an MI_FLUSH and drm_agp_chipset_flush
2968 * 5. Unmapped from GTT
2969 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2970 * flush_domains and invalidate_domains end up both zero
2971 * so no flushing/invalidating happens
2972 * 6. Freed
2973 * yay, done
2974 *
2975 * Case 2: The shared render buffer
2976 *
2977 * 1. Allocated
2978 * 2. Mapped to GTT
2979 * 3. Read/written by GPU
2980 * 4. set_domain to (CPU,CPU)
2981 * 5. Read/written by CPU
2982 * 6. Read/written by GPU
2983 *
2984 * 1. Allocated
2985 * Same as last example, (CPU, CPU)
2986 * 2. Mapped to GTT
2987 * Nothing changes (assertions find that it is not in the GPU)
2988 * 3. Read/written by GPU
2989 * execbuffer calls set_domain (RENDER, RENDER)
2990 * flush_domains gets CPU
2991 * invalidate_domains gets GPU
2992 * clflush (obj)
2993 * MI_FLUSH and drm_agp_chipset_flush
2994 * 4. set_domain (CPU, CPU)
2995 * flush_domains gets GPU
2996 * invalidate_domains gets CPU
2997 * wait_rendering (obj) to make sure all drawing is complete.
2998 * This will include an MI_FLUSH to get the data from GPU
2999 * to memory
3000 * clflush (obj) to invalidate the CPU cache
3001 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3002 * 5. Read/written by CPU
3003 * cache lines are loaded and dirtied
3004 * 6. Read written by GPU
3005 * Same as last GPU access
3006 *
3007 * Case 3: The constant buffer
3008 *
3009 * 1. Allocated
3010 * 2. Written by CPU
3011 * 3. Read by GPU
3012 * 4. Updated (written) by CPU again
3013 * 5. Read by GPU
3014 *
3015 * 1. Allocated
3016 * (CPU, CPU)
3017 * 2. Written by CPU
3018 * (CPU, CPU)
3019 * 3. Read by GPU
3020 * (CPU+RENDER, 0)
3021 * flush_domains = CPU
3022 * invalidate_domains = RENDER
3023 * clflush (obj)
3024 * MI_FLUSH
3025 * drm_agp_chipset_flush
3026 * 4. Updated (written) by CPU again
3027 * (CPU, CPU)
3028 * flush_domains = 0 (no previous write domain)
3029 * invalidate_domains = 0 (no new read domains)
3030 * 5. Read by GPU
3031 * (CPU+RENDER, 0)
3032 * flush_domains = CPU
3033 * invalidate_domains = RENDER
3034 * clflush (obj)
3035 * MI_FLUSH
3036 * drm_agp_chipset_flush
3037 */
Keith Packardc0d90822008-11-20 23:11:08 -08003038static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003039i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003040{
3041 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003042 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003043 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003044 uint32_t invalidate_domains = 0;
3045 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003046 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003047
Eric Anholt8b0e3782009-02-19 14:40:50 -08003048 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3049 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003050
Jesse Barnes652c3932009-08-17 13:31:43 -07003051 intel_mark_busy(dev, obj);
3052
Eric Anholt673a3942008-07-30 12:06:12 -07003053 /*
3054 * If the object isn't moving to a new write domain,
3055 * let the object stay in multiple read domains
3056 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003057 if (obj->pending_write_domain == 0)
3058 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003059 else
3060 obj_priv->dirty = 1;
3061
3062 /*
3063 * Flush the current write domain if
3064 * the new read domains don't match. Invalidate
3065 * any read domains which differ from the old
3066 * write domain
3067 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003068 if (obj->write_domain &&
3069 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003070 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003071 invalidate_domains |=
3072 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003073 }
3074 /*
3075 * Invalidate any read caches which may have
3076 * stale data. That is, any new read domains.
3077 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003078 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003079 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003080 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003081
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003082 old_read_domains = obj->read_domains;
3083
Eric Anholtefbeed92009-02-19 14:54:51 -08003084 /* The actual obj->write_domain will be updated with
3085 * pending_write_domain after we emit the accumulated flush for all
3086 * of our domain changes in execbuffers (which clears objects'
3087 * write_domains). So if we have a current write domain that we
3088 * aren't changing, set pending_write_domain to that.
3089 */
3090 if (flush_domains == 0 && obj->pending_write_domain == 0)
3091 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003092 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003093
3094 dev->invalidate_domains |= invalidate_domains;
3095 dev->flush_domains |= flush_domains;
Chris Wilson92204342010-09-18 11:02:01 +01003096 if (obj_priv->ring)
3097 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003098
3099 trace_i915_gem_object_change_domain(obj,
3100 old_read_domains,
3101 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003102}
3103
3104/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003105 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003106 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003107 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3108 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3109 */
3110static void
3111i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3112{
Daniel Vetter23010e42010-03-08 13:35:02 +01003113 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003114
3115 if (!obj_priv->page_cpu_valid)
3116 return;
3117
3118 /* If we're partially in the CPU read domain, finish moving it in.
3119 */
3120 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3121 int i;
3122
3123 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3124 if (obj_priv->page_cpu_valid[i])
3125 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003126 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003127 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003128 }
3129
3130 /* Free the page_cpu_valid mappings which are now stale, whether
3131 * or not we've got I915_GEM_DOMAIN_CPU.
3132 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003133 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003134 obj_priv->page_cpu_valid = NULL;
3135}
3136
3137/**
3138 * Set the CPU read domain on a range of the object.
3139 *
3140 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3141 * not entirely valid. The page_cpu_valid member of the object flags which
3142 * pages have been flushed, and will be respected by
3143 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3144 * of the whole object.
3145 *
3146 * This function returns when the move is complete, including waiting on
3147 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003148 */
3149static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003150i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3151 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003152{
Daniel Vetter23010e42010-03-08 13:35:02 +01003153 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003154 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003155 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003156
Eric Anholte47c68e2008-11-14 13:35:19 -08003157 if (offset == 0 && size == obj->size)
3158 return i915_gem_object_set_to_cpu_domain(obj, 0);
3159
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003160 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003161 if (ret != 0)
3162 return ret;
3163 i915_gem_object_flush_gtt_write_domain(obj);
3164
3165 /* If we're already fully in the CPU read domain, we're done. */
3166 if (obj_priv->page_cpu_valid == NULL &&
3167 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003168 return 0;
3169
Eric Anholte47c68e2008-11-14 13:35:19 -08003170 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3171 * newly adding I915_GEM_DOMAIN_CPU
3172 */
Eric Anholt673a3942008-07-30 12:06:12 -07003173 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003174 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3175 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003176 if (obj_priv->page_cpu_valid == NULL)
3177 return -ENOMEM;
3178 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3179 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003180
3181 /* Flush the cache on any pages that are still invalid from the CPU's
3182 * perspective.
3183 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003184 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3185 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003186 if (obj_priv->page_cpu_valid[i])
3187 continue;
3188
Eric Anholt856fa192009-03-19 14:10:50 -07003189 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003190
3191 obj_priv->page_cpu_valid[i] = 1;
3192 }
3193
Eric Anholte47c68e2008-11-14 13:35:19 -08003194 /* It should now be out of any other write domains, and we can update
3195 * the domain values for our changes.
3196 */
3197 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3198
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003199 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003200 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3201
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003202 trace_i915_gem_object_change_domain(obj,
3203 old_read_domains,
3204 obj->write_domain);
3205
Eric Anholt673a3942008-07-30 12:06:12 -07003206 return 0;
3207}
3208
3209/**
Eric Anholt673a3942008-07-30 12:06:12 -07003210 * Pin an object to the GTT and evaluate the relocations landing in it.
3211 */
3212static int
3213i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3214 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003215 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003216 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003217{
3218 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003219 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003220 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003221 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003222 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003223 bool need_fence;
3224
3225 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3226 obj_priv->tiling_mode != I915_TILING_NONE;
3227
3228 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d2010-05-27 13:18:15 +01003229 if (need_fence &&
3230 !i915_gem_object_fence_offset_ok(obj,
3231 obj_priv->tiling_mode)) {
3232 ret = i915_gem_object_unbind(obj);
3233 if (ret)
3234 return ret;
3235 }
Eric Anholt673a3942008-07-30 12:06:12 -07003236
3237 /* Choose the GTT offset for our buffer and put it there. */
3238 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3239 if (ret)
3240 return ret;
3241
Jesse Barnes76446ca2009-12-17 22:05:42 -05003242 /*
3243 * Pre-965 chips need a fence register set up in order to
3244 * properly handle blits to/from tiled surfaces.
3245 */
3246 if (need_fence) {
Chris Wilson53640e12010-09-20 11:40:50 +01003247 ret = i915_gem_object_get_fence_reg(obj, true);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003248 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003249 i915_gem_object_unpin(obj);
3250 return ret;
3251 }
Chris Wilson53640e12010-09-20 11:40:50 +01003252
3253 dev_priv->fence_regs[obj_priv->fence_reg].gpu = true;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003254 }
3255
Eric Anholt673a3942008-07-30 12:06:12 -07003256 entry->offset = obj_priv->gtt_offset;
3257
Eric Anholt673a3942008-07-30 12:06:12 -07003258 /* Apply the relocations, using the GTT aperture to avoid cache
3259 * flushing requirements.
3260 */
3261 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003262 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003263 struct drm_gem_object *target_obj;
3264 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003265 uint32_t reloc_val, reloc_offset;
3266 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003267
Eric Anholt673a3942008-07-30 12:06:12 -07003268 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003269 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003270 if (target_obj == NULL) {
3271 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003272 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003273 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003274 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003275
Chris Wilson8542a0b2009-09-09 21:15:15 +01003276#if WATCH_RELOC
3277 DRM_INFO("%s: obj %p offset %08x target %d "
3278 "read %08x write %08x gtt %08x "
3279 "presumed %08x delta %08x\n",
3280 __func__,
3281 obj,
3282 (int) reloc->offset,
3283 (int) reloc->target_handle,
3284 (int) reloc->read_domains,
3285 (int) reloc->write_domain,
3286 (int) target_obj_priv->gtt_offset,
3287 (int) reloc->presumed_offset,
3288 reloc->delta);
3289#endif
3290
Eric Anholt673a3942008-07-30 12:06:12 -07003291 /* The target buffer should have appeared before us in the
3292 * exec_object list, so it should have a GTT space bound by now.
3293 */
3294 if (target_obj_priv->gtt_space == NULL) {
3295 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003296 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003297 drm_gem_object_unreference(target_obj);
3298 i915_gem_object_unpin(obj);
3299 return -EINVAL;
3300 }
3301
Chris Wilson8542a0b2009-09-09 21:15:15 +01003302 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003303 if (reloc->write_domain & (reloc->write_domain - 1)) {
3304 DRM_ERROR("reloc with multiple write domains: "
3305 "obj %p target %d offset %d "
3306 "read %08x write %08x",
3307 obj, reloc->target_handle,
3308 (int) reloc->offset,
3309 reloc->read_domains,
3310 reloc->write_domain);
3311 return -EINVAL;
3312 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003313 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3314 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3315 DRM_ERROR("reloc with read/write CPU domains: "
3316 "obj %p target %d offset %d "
3317 "read %08x write %08x",
3318 obj, reloc->target_handle,
3319 (int) reloc->offset,
3320 reloc->read_domains,
3321 reloc->write_domain);
3322 drm_gem_object_unreference(target_obj);
3323 i915_gem_object_unpin(obj);
3324 return -EINVAL;
3325 }
3326 if (reloc->write_domain && target_obj->pending_write_domain &&
3327 reloc->write_domain != target_obj->pending_write_domain) {
3328 DRM_ERROR("Write domain conflict: "
3329 "obj %p target %d offset %d "
3330 "new %08x old %08x\n",
3331 obj, reloc->target_handle,
3332 (int) reloc->offset,
3333 reloc->write_domain,
3334 target_obj->pending_write_domain);
3335 drm_gem_object_unreference(target_obj);
3336 i915_gem_object_unpin(obj);
3337 return -EINVAL;
3338 }
3339
3340 target_obj->pending_read_domains |= reloc->read_domains;
3341 target_obj->pending_write_domain |= reloc->write_domain;
3342
3343 /* If the relocation already has the right value in it, no
3344 * more work needs to be done.
3345 */
3346 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3347 drm_gem_object_unreference(target_obj);
3348 continue;
3349 }
3350
3351 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003352 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003353 DRM_ERROR("Relocation beyond object bounds: "
3354 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003355 obj, reloc->target_handle,
3356 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003357 drm_gem_object_unreference(target_obj);
3358 i915_gem_object_unpin(obj);
3359 return -EINVAL;
3360 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003361 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003362 DRM_ERROR("Relocation not 4-byte aligned: "
3363 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003364 obj, reloc->target_handle,
3365 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003366 drm_gem_object_unreference(target_obj);
3367 i915_gem_object_unpin(obj);
3368 return -EINVAL;
3369 }
3370
Chris Wilson8542a0b2009-09-09 21:15:15 +01003371 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003372 if (reloc->delta >= target_obj->size) {
3373 DRM_ERROR("Relocation beyond target object bounds: "
3374 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003375 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003376 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003377 drm_gem_object_unreference(target_obj);
3378 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003379 return -EINVAL;
3380 }
3381
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003382 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3383 if (ret != 0) {
3384 drm_gem_object_unreference(target_obj);
3385 i915_gem_object_unpin(obj);
3386 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003387 }
3388
3389 /* Map the page containing the relocation we're going to
3390 * perform.
3391 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003392 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003393 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3394 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003395 ~(PAGE_SIZE - 1)),
3396 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003397 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003398 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003399 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003400
Eric Anholt673a3942008-07-30 12:06:12 -07003401 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003402 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003403
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003404 /* The updated presumed offset for this entry will be
3405 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003406 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003407 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003408
3409 drm_gem_object_unreference(target_obj);
3410 }
3411
Eric Anholt673a3942008-07-30 12:06:12 -07003412 return 0;
3413}
3414
Eric Anholt673a3942008-07-30 12:06:12 -07003415/* Throttle our rendering by waiting until the ring has completed our requests
3416 * emitted over 20 msec ago.
3417 *
Eric Anholtb9624422009-06-03 07:27:35 +00003418 * Note that if we were to use the current jiffies each time around the loop,
3419 * we wouldn't escape the function with any frames outstanding if the time to
3420 * render a frame was over 20ms.
3421 *
Eric Anholt673a3942008-07-30 12:06:12 -07003422 * This should get us reasonable parallelism between CPU and GPU but also
3423 * relatively low latency when blocking on a particular request to finish.
3424 */
3425static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003426i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003427{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003428 struct drm_i915_private *dev_priv = dev->dev_private;
3429 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003430 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003431 struct drm_i915_gem_request *request;
3432 struct intel_ring_buffer *ring = NULL;
3433 u32 seqno = 0;
3434 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003435
Chris Wilson1c255952010-09-26 11:03:27 +01003436 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003437 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003438 if (time_after_eq(request->emitted_jiffies, recent_enough))
3439 break;
3440
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003441 ring = request->ring;
3442 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003443 }
Chris Wilson1c255952010-09-26 11:03:27 +01003444 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003445
3446 if (seqno == 0)
3447 return 0;
3448
3449 ret = 0;
3450 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3451 /* And wait for the seqno passing without holding any locks and
3452 * causing extra latency for others. This is safe as the irq
3453 * generation is designed to be run atomically and so is
3454 * lockless.
3455 */
3456 ring->user_irq_get(dev, ring);
3457 ret = wait_event_interruptible(ring->irq_queue,
3458 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3459 || atomic_read(&dev_priv->mm.wedged));
3460 ring->user_irq_put(dev, ring);
3461
3462 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3463 ret = -EIO;
3464 }
3465
3466 if (ret == 0)
3467 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003468
Eric Anholt673a3942008-07-30 12:06:12 -07003469 return ret;
3470}
3471
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003472static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003473i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003474 uint32_t buffer_count,
3475 struct drm_i915_gem_relocation_entry **relocs)
3476{
3477 uint32_t reloc_count = 0, reloc_index = 0, i;
3478 int ret;
3479
3480 *relocs = NULL;
3481 for (i = 0; i < buffer_count; i++) {
3482 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3483 return -EINVAL;
3484 reloc_count += exec_list[i].relocation_count;
3485 }
3486
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003487 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003488 if (*relocs == NULL) {
3489 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003490 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003491 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003492
3493 for (i = 0; i < buffer_count; i++) {
3494 struct drm_i915_gem_relocation_entry __user *user_relocs;
3495
3496 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3497
3498 ret = copy_from_user(&(*relocs)[reloc_index],
3499 user_relocs,
3500 exec_list[i].relocation_count *
3501 sizeof(**relocs));
3502 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003503 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003504 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003505 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003506 }
3507
3508 reloc_index += exec_list[i].relocation_count;
3509 }
3510
Florian Mickler2bc43b52009-04-06 22:55:41 +02003511 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003512}
3513
3514static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003515i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003516 uint32_t buffer_count,
3517 struct drm_i915_gem_relocation_entry *relocs)
3518{
3519 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003520 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003521
Chris Wilson93533c22010-01-31 10:40:48 +00003522 if (relocs == NULL)
3523 return 0;
3524
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003525 for (i = 0; i < buffer_count; i++) {
3526 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003527 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003528
3529 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3530
Florian Mickler2bc43b52009-04-06 22:55:41 +02003531 unwritten = copy_to_user(user_relocs,
3532 &relocs[reloc_count],
3533 exec_list[i].relocation_count *
3534 sizeof(*relocs));
3535
3536 if (unwritten) {
3537 ret = -EFAULT;
3538 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003539 }
3540
3541 reloc_count += exec_list[i].relocation_count;
3542 }
3543
Florian Mickler2bc43b52009-04-06 22:55:41 +02003544err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003545 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003546
3547 return ret;
3548}
3549
Chris Wilson83d60792009-06-06 09:45:57 +01003550static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003551i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003552 uint64_t exec_offset)
3553{
3554 uint32_t exec_start, exec_len;
3555
3556 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3557 exec_len = (uint32_t) exec->batch_len;
3558
3559 if ((exec_start | exec_len) & 0x7)
3560 return -EINVAL;
3561
3562 if (!exec_start)
3563 return -EINVAL;
3564
3565 return 0;
3566}
3567
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003568static int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003569i915_gem_wait_for_pending_flip(struct drm_device *dev,
3570 struct drm_gem_object **object_list,
3571 int count)
3572{
3573 drm_i915_private_t *dev_priv = dev->dev_private;
3574 struct drm_i915_gem_object *obj_priv;
3575 DEFINE_WAIT(wait);
3576 int i, ret = 0;
3577
3578 for (;;) {
3579 prepare_to_wait(&dev_priv->pending_flip_queue,
3580 &wait, TASK_INTERRUPTIBLE);
3581 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003582 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003583 if (atomic_read(&obj_priv->pending_flip) > 0)
3584 break;
3585 }
3586 if (i == count)
3587 break;
3588
3589 if (!signal_pending(current)) {
3590 mutex_unlock(&dev->struct_mutex);
3591 schedule();
3592 mutex_lock(&dev->struct_mutex);
3593 continue;
3594 }
3595 ret = -ERESTARTSYS;
3596 break;
3597 }
3598 finish_wait(&dev_priv->pending_flip_queue, &wait);
3599
3600 return ret;
3601}
3602
Chris Wilson8dc5d142010-08-12 12:36:12 +01003603static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003604i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3605 struct drm_file *file_priv,
3606 struct drm_i915_gem_execbuffer2 *args,
3607 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003608{
3609 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003610 struct drm_gem_object **object_list = NULL;
3611 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003612 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003613 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003614 struct drm_i915_gem_relocation_entry *relocs = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003615 struct drm_i915_gem_request *request = NULL;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003616 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003617 uint64_t exec_offset;
Chris Wilson5c12a072010-09-22 11:22:30 +01003618 uint32_t reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003619 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003620
Zou Nan hai852835f2010-05-21 09:08:56 +08003621 struct intel_ring_buffer *ring = NULL;
3622
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003623 ret = i915_gem_check_is_wedged(dev);
3624 if (ret)
3625 return ret;
3626
Eric Anholt673a3942008-07-30 12:06:12 -07003627#if WATCH_EXEC
3628 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3629 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3630#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003631 if (args->flags & I915_EXEC_BSD) {
3632 if (!HAS_BSD(dev)) {
3633 DRM_ERROR("execbuf with wrong flag\n");
3634 return -EINVAL;
3635 }
3636 ring = &dev_priv->bsd_ring;
3637 } else {
3638 ring = &dev_priv->render_ring;
3639 }
3640
Eric Anholt4f481ed2008-09-10 14:22:49 -07003641 if (args->buffer_count < 1) {
3642 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3643 return -EINVAL;
3644 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003645 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003646 if (object_list == NULL) {
3647 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003648 args->buffer_count);
3649 ret = -ENOMEM;
3650 goto pre_mutex_err;
3651 }
Eric Anholt673a3942008-07-30 12:06:12 -07003652
Eric Anholt201361a2009-03-11 12:30:04 -07003653 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003654 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3655 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003656 if (cliprects == NULL) {
3657 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003658 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003659 }
Eric Anholt201361a2009-03-11 12:30:04 -07003660
3661 ret = copy_from_user(cliprects,
3662 (struct drm_clip_rect __user *)
3663 (uintptr_t) args->cliprects_ptr,
3664 sizeof(*cliprects) * args->num_cliprects);
3665 if (ret != 0) {
3666 DRM_ERROR("copy %d cliprects failed: %d\n",
3667 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003668 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003669 goto pre_mutex_err;
3670 }
3671 }
3672
Chris Wilson8dc5d142010-08-12 12:36:12 +01003673 request = kzalloc(sizeof(*request), GFP_KERNEL);
3674 if (request == NULL) {
3675 ret = -ENOMEM;
3676 goto pre_mutex_err;
3677 }
3678
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003679 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3680 &relocs);
3681 if (ret != 0)
3682 goto pre_mutex_err;
3683
Chris Wilson76c1dec2010-09-25 11:22:51 +01003684 ret = i915_mutex_lock_interruptible(dev);
3685 if (ret)
3686 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003687
3688 i915_verify_inactive(dev, __FILE__, __LINE__);
3689
Eric Anholt673a3942008-07-30 12:06:12 -07003690 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003691 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003692 ret = -EBUSY;
3693 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003694 }
3695
Keith Packardac94a962008-11-20 23:30:27 -08003696 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003697 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003698 for (i = 0; i < args->buffer_count; i++) {
3699 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3700 exec_list[i].handle);
3701 if (object_list[i] == NULL) {
3702 DRM_ERROR("Invalid object handle %d at index %d\n",
3703 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003704 /* prevent error path from reading uninitialized data */
3705 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003706 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003707 goto err;
3708 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003709
Daniel Vetter23010e42010-03-08 13:35:02 +01003710 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003711 if (obj_priv->in_execbuffer) {
3712 DRM_ERROR("Object %p appears more than once in object list\n",
3713 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003714 /* prevent error path from reading uninitialized data */
3715 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003716 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003717 goto err;
3718 }
3719 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003720 flips += atomic_read(&obj_priv->pending_flip);
3721 }
3722
3723 if (flips > 0) {
3724 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3725 args->buffer_count);
3726 if (ret)
3727 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003728 }
Eric Anholt673a3942008-07-30 12:06:12 -07003729
Keith Packardac94a962008-11-20 23:30:27 -08003730 /* Pin and relocate */
3731 for (pin_tries = 0; ; pin_tries++) {
3732 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003733 reloc_index = 0;
3734
Keith Packardac94a962008-11-20 23:30:27 -08003735 for (i = 0; i < args->buffer_count; i++) {
3736 object_list[i]->pending_read_domains = 0;
3737 object_list[i]->pending_write_domain = 0;
3738 ret = i915_gem_object_pin_and_relocate(object_list[i],
3739 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003740 &exec_list[i],
3741 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003742 if (ret)
3743 break;
3744 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003745 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003746 }
3747 /* success */
3748 if (ret == 0)
3749 break;
3750
3751 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003752 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003753 if (ret != -ERESTARTSYS) {
3754 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003755 int num_fences = 0;
3756 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003757 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003758
Chris Wilson07f73f62009-09-14 16:50:30 +01003759 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003760 num_fences +=
3761 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3762 obj_priv->tiling_mode != I915_TILING_NONE;
3763 }
3764 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003765 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003766 total_size, num_fences,
3767 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003768 DRM_ERROR("%d objects [%d pinned], "
3769 "%d object bytes [%d pinned], "
3770 "%d/%d gtt bytes\n",
3771 atomic_read(&dev->object_count),
3772 atomic_read(&dev->pin_count),
3773 atomic_read(&dev->object_memory),
3774 atomic_read(&dev->pin_memory),
3775 atomic_read(&dev->gtt_memory),
3776 dev->gtt_total);
3777 }
Eric Anholt673a3942008-07-30 12:06:12 -07003778 goto err;
3779 }
Keith Packardac94a962008-11-20 23:30:27 -08003780
3781 /* unpin all of our buffers */
3782 for (i = 0; i < pinned; i++)
3783 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003784 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003785
3786 /* evict everyone we can from the aperture */
3787 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003788 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003789 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003790 }
3791
3792 /* Set the pending read domains for the batch buffer to COMMAND */
3793 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003794 if (batch_obj->pending_write_domain) {
3795 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3796 ret = -EINVAL;
3797 goto err;
3798 }
3799 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003800
Chris Wilson83d60792009-06-06 09:45:57 +01003801 /* Sanity check the batch buffer, prior to moving objects */
3802 exec_offset = exec_list[args->buffer_count - 1].offset;
3803 ret = i915_gem_check_execbuffer (args, exec_offset);
3804 if (ret != 0) {
3805 DRM_ERROR("execbuf with invalid offset/length\n");
3806 goto err;
3807 }
3808
Eric Anholt673a3942008-07-30 12:06:12 -07003809 i915_verify_inactive(dev, __FILE__, __LINE__);
3810
Keith Packard646f0f62008-11-20 23:23:03 -08003811 /* Zero the global flush/invalidate flags. These
3812 * will be modified as new domains are computed
3813 * for each object
3814 */
3815 dev->invalidate_domains = 0;
3816 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003817 dev_priv->mm.flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003818
Eric Anholt673a3942008-07-30 12:06:12 -07003819 for (i = 0; i < args->buffer_count; i++) {
3820 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003821
Keith Packard646f0f62008-11-20 23:23:03 -08003822 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003823 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003824 }
3825
3826 i915_verify_inactive(dev, __FILE__, __LINE__);
3827
Keith Packard646f0f62008-11-20 23:23:03 -08003828 if (dev->invalidate_domains | dev->flush_domains) {
3829#if WATCH_EXEC
3830 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3831 __func__,
3832 dev->invalidate_domains,
3833 dev->flush_domains);
3834#endif
Chris Wilsonc78ec302010-09-20 12:50:23 +01003835 i915_gem_flush(dev, file_priv,
Keith Packard646f0f62008-11-20 23:23:03 -08003836 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003837 dev->flush_domains,
3838 dev_priv->mm.flush_rings);
Daniel Vettera6910432010-02-02 17:08:37 +01003839 }
3840
Eric Anholtefbeed92009-02-19 14:54:51 -08003841 for (i = 0; i < args->buffer_count; i++) {
3842 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003843 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003844 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003845
3846 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003847 if (obj->write_domain)
3848 list_move_tail(&obj_priv->gpu_write_list,
3849 &dev_priv->mm.gpu_write_list);
3850 else
3851 list_del_init(&obj_priv->gpu_write_list);
3852
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003853 trace_i915_gem_object_change_domain(obj,
3854 obj->read_domains,
3855 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003856 }
3857
Eric Anholt673a3942008-07-30 12:06:12 -07003858 i915_verify_inactive(dev, __FILE__, __LINE__);
3859
3860#if WATCH_COHERENCY
3861 for (i = 0; i < args->buffer_count; i++) {
3862 i915_gem_object_check_coherency(object_list[i],
3863 exec_list[i].handle);
3864 }
3865#endif
3866
Eric Anholt673a3942008-07-30 12:06:12 -07003867#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003868 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003869 args->batch_len,
3870 __func__,
3871 ~0);
3872#endif
3873
Eric Anholt673a3942008-07-30 12:06:12 -07003874 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003875 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3876 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003877 if (ret) {
3878 DRM_ERROR("dispatch failed %d\n", ret);
3879 goto err;
3880 }
3881
3882 /*
3883 * Ensure that the commands in the batch buffer are
3884 * finished before the interrupt fires
3885 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003886 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003887
3888 i915_verify_inactive(dev, __FILE__, __LINE__);
3889
Daniel Vetter617dbe22010-02-11 22:16:02 +01003890 for (i = 0; i < args->buffer_count; i++) {
3891 struct drm_gem_object *obj = object_list[i];
3892 obj_priv = to_intel_bo(obj);
3893
3894 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01003895 }
Chris Wilsona56ba562010-09-28 10:07:56 +01003896
Chris Wilson5c12a072010-09-22 11:22:30 +01003897 i915_add_request(dev, file_priv, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003898 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003899
Eric Anholt673a3942008-07-30 12:06:12 -07003900 i915_verify_inactive(dev, __FILE__, __LINE__);
3901
Eric Anholt673a3942008-07-30 12:06:12 -07003902err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003903 for (i = 0; i < pinned; i++)
3904 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003905
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003906 for (i = 0; i < args->buffer_count; i++) {
3907 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003908 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003909 obj_priv->in_execbuffer = false;
3910 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003911 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003912 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003913
Eric Anholt673a3942008-07-30 12:06:12 -07003914 mutex_unlock(&dev->struct_mutex);
3915
Chris Wilson93533c22010-01-31 10:40:48 +00003916pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003917 /* Copy the updated relocations out regardless of current error
3918 * state. Failure to update the relocs would mean that the next
3919 * time userland calls execbuf, it would do so with presumed offset
3920 * state that didn't match the actual object state.
3921 */
3922 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3923 relocs);
3924 if (ret2 != 0) {
3925 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3926
3927 if (ret == 0)
3928 ret = ret2;
3929 }
3930
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003931 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003932 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003933 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003934
3935 return ret;
3936}
3937
Jesse Barnes76446ca2009-12-17 22:05:42 -05003938/*
3939 * Legacy execbuffer just creates an exec2 list from the original exec object
3940 * list array and passes it to the real function.
3941 */
3942int
3943i915_gem_execbuffer(struct drm_device *dev, void *data,
3944 struct drm_file *file_priv)
3945{
3946 struct drm_i915_gem_execbuffer *args = data;
3947 struct drm_i915_gem_execbuffer2 exec2;
3948 struct drm_i915_gem_exec_object *exec_list = NULL;
3949 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3950 int ret, i;
3951
3952#if WATCH_EXEC
3953 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3954 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3955#endif
3956
3957 if (args->buffer_count < 1) {
3958 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3959 return -EINVAL;
3960 }
3961
3962 /* Copy in the exec list from userland */
3963 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3964 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3965 if (exec_list == NULL || exec2_list == NULL) {
3966 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3967 args->buffer_count);
3968 drm_free_large(exec_list);
3969 drm_free_large(exec2_list);
3970 return -ENOMEM;
3971 }
3972 ret = copy_from_user(exec_list,
3973 (struct drm_i915_relocation_entry __user *)
3974 (uintptr_t) args->buffers_ptr,
3975 sizeof(*exec_list) * args->buffer_count);
3976 if (ret != 0) {
3977 DRM_ERROR("copy %d exec entries failed %d\n",
3978 args->buffer_count, ret);
3979 drm_free_large(exec_list);
3980 drm_free_large(exec2_list);
3981 return -EFAULT;
3982 }
3983
3984 for (i = 0; i < args->buffer_count; i++) {
3985 exec2_list[i].handle = exec_list[i].handle;
3986 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3987 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3988 exec2_list[i].alignment = exec_list[i].alignment;
3989 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003990 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05003991 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3992 else
3993 exec2_list[i].flags = 0;
3994 }
3995
3996 exec2.buffers_ptr = args->buffers_ptr;
3997 exec2.buffer_count = args->buffer_count;
3998 exec2.batch_start_offset = args->batch_start_offset;
3999 exec2.batch_len = args->batch_len;
4000 exec2.DR1 = args->DR1;
4001 exec2.DR4 = args->DR4;
4002 exec2.num_cliprects = args->num_cliprects;
4003 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004004 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004005
4006 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4007 if (!ret) {
4008 /* Copy the new buffer offsets back to the user's exec list. */
4009 for (i = 0; i < args->buffer_count; i++)
4010 exec_list[i].offset = exec2_list[i].offset;
4011 /* ... and back out to userspace */
4012 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4013 (uintptr_t) args->buffers_ptr,
4014 exec_list,
4015 sizeof(*exec_list) * args->buffer_count);
4016 if (ret) {
4017 ret = -EFAULT;
4018 DRM_ERROR("failed to copy %d exec entries "
4019 "back to user (%d)\n",
4020 args->buffer_count, ret);
4021 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004022 }
4023
4024 drm_free_large(exec_list);
4025 drm_free_large(exec2_list);
4026 return ret;
4027}
4028
4029int
4030i915_gem_execbuffer2(struct drm_device *dev, void *data,
4031 struct drm_file *file_priv)
4032{
4033 struct drm_i915_gem_execbuffer2 *args = data;
4034 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4035 int ret;
4036
4037#if WATCH_EXEC
4038 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4039 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4040#endif
4041
4042 if (args->buffer_count < 1) {
4043 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4044 return -EINVAL;
4045 }
4046
4047 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4048 if (exec2_list == NULL) {
4049 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4050 args->buffer_count);
4051 return -ENOMEM;
4052 }
4053 ret = copy_from_user(exec2_list,
4054 (struct drm_i915_relocation_entry __user *)
4055 (uintptr_t) args->buffers_ptr,
4056 sizeof(*exec2_list) * args->buffer_count);
4057 if (ret != 0) {
4058 DRM_ERROR("copy %d exec entries failed %d\n",
4059 args->buffer_count, ret);
4060 drm_free_large(exec2_list);
4061 return -EFAULT;
4062 }
4063
4064 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4065 if (!ret) {
4066 /* Copy the new buffer offsets back to the user's exec list. */
4067 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4068 (uintptr_t) args->buffers_ptr,
4069 exec2_list,
4070 sizeof(*exec2_list) * args->buffer_count);
4071 if (ret) {
4072 ret = -EFAULT;
4073 DRM_ERROR("failed to copy %d exec entries "
4074 "back to user (%d)\n",
4075 args->buffer_count, ret);
4076 }
4077 }
4078
4079 drm_free_large(exec2_list);
4080 return ret;
4081}
4082
Eric Anholt673a3942008-07-30 12:06:12 -07004083int
4084i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4085{
4086 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004087 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004088 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004089 int ret;
4090
Daniel Vetter778c3542010-05-13 11:49:44 +02004091 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4092
Eric Anholt673a3942008-07-30 12:06:12 -07004093 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004094
4095 if (obj_priv->gtt_space != NULL) {
4096 if (alignment == 0)
4097 alignment = i915_gem_get_gtt_alignment(obj);
4098 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004099 WARN(obj_priv->pin_count,
4100 "bo is already pinned with incorrect alignment:"
4101 " offset=%x, req.alignment=%x\n",
4102 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004103 ret = i915_gem_object_unbind(obj);
4104 if (ret)
4105 return ret;
4106 }
4107 }
4108
Eric Anholt673a3942008-07-30 12:06:12 -07004109 if (obj_priv->gtt_space == NULL) {
4110 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004111 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004112 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004113 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004114
Eric Anholt673a3942008-07-30 12:06:12 -07004115 obj_priv->pin_count++;
4116
4117 /* If the object is not active and not pending a flush,
4118 * remove it from the inactive list
4119 */
4120 if (obj_priv->pin_count == 1) {
4121 atomic_inc(&dev->pin_count);
4122 atomic_add(obj->size, &dev->pin_memory);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004123 if (!obj_priv->active)
4124 list_move_tail(&obj_priv->list,
4125 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004126 }
4127 i915_verify_inactive(dev, __FILE__, __LINE__);
4128
4129 return 0;
4130}
4131
4132void
4133i915_gem_object_unpin(struct drm_gem_object *obj)
4134{
4135 struct drm_device *dev = obj->dev;
4136 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004137 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004138
4139 i915_verify_inactive(dev, __FILE__, __LINE__);
4140 obj_priv->pin_count--;
4141 BUG_ON(obj_priv->pin_count < 0);
4142 BUG_ON(obj_priv->gtt_space == NULL);
4143
4144 /* If the object is no longer pinned, and is
4145 * neither active nor being flushed, then stick it on
4146 * the inactive list
4147 */
4148 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004149 if (!obj_priv->active)
Eric Anholt673a3942008-07-30 12:06:12 -07004150 list_move_tail(&obj_priv->list,
4151 &dev_priv->mm.inactive_list);
4152 atomic_dec(&dev->pin_count);
4153 atomic_sub(obj->size, &dev->pin_memory);
4154 }
4155 i915_verify_inactive(dev, __FILE__, __LINE__);
4156}
4157
4158int
4159i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4160 struct drm_file *file_priv)
4161{
4162 struct drm_i915_gem_pin *args = data;
4163 struct drm_gem_object *obj;
4164 struct drm_i915_gem_object *obj_priv;
4165 int ret;
4166
Eric Anholt673a3942008-07-30 12:06:12 -07004167 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4168 if (obj == NULL) {
4169 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4170 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004171 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004172 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004173 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004174
Chris Wilson76c1dec2010-09-25 11:22:51 +01004175 ret = i915_mutex_lock_interruptible(dev);
4176 if (ret) {
4177 drm_gem_object_unreference_unlocked(obj);
4178 return ret;
4179 }
4180
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004181 if (obj_priv->madv != I915_MADV_WILLNEED) {
4182 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004183 drm_gem_object_unreference(obj);
4184 mutex_unlock(&dev->struct_mutex);
4185 return -EINVAL;
4186 }
4187
Jesse Barnes79e53942008-11-07 14:24:08 -08004188 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4189 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4190 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004191 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004192 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004193 return -EINVAL;
4194 }
4195
4196 obj_priv->user_pin_count++;
4197 obj_priv->pin_filp = file_priv;
4198 if (obj_priv->user_pin_count == 1) {
4199 ret = i915_gem_object_pin(obj, args->alignment);
4200 if (ret != 0) {
4201 drm_gem_object_unreference(obj);
4202 mutex_unlock(&dev->struct_mutex);
4203 return ret;
4204 }
Eric Anholt673a3942008-07-30 12:06:12 -07004205 }
4206
4207 /* XXX - flush the CPU caches for pinned objects
4208 * as the X server doesn't manage domains yet
4209 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004210 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004211 args->offset = obj_priv->gtt_offset;
4212 drm_gem_object_unreference(obj);
4213 mutex_unlock(&dev->struct_mutex);
4214
4215 return 0;
4216}
4217
4218int
4219i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4220 struct drm_file *file_priv)
4221{
4222 struct drm_i915_gem_pin *args = data;
4223 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004224 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004225 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004226
4227 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4228 if (obj == NULL) {
4229 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4230 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004231 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004232 }
4233
Daniel Vetter23010e42010-03-08 13:35:02 +01004234 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004235
4236 ret = i915_mutex_lock_interruptible(dev);
4237 if (ret) {
4238 drm_gem_object_unreference_unlocked(obj);
4239 return ret;
4240 }
4241
Jesse Barnes79e53942008-11-07 14:24:08 -08004242 if (obj_priv->pin_filp != file_priv) {
4243 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4244 args->handle);
4245 drm_gem_object_unreference(obj);
4246 mutex_unlock(&dev->struct_mutex);
4247 return -EINVAL;
4248 }
4249 obj_priv->user_pin_count--;
4250 if (obj_priv->user_pin_count == 0) {
4251 obj_priv->pin_filp = NULL;
4252 i915_gem_object_unpin(obj);
4253 }
Eric Anholt673a3942008-07-30 12:06:12 -07004254
4255 drm_gem_object_unreference(obj);
4256 mutex_unlock(&dev->struct_mutex);
4257 return 0;
4258}
4259
4260int
4261i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4262 struct drm_file *file_priv)
4263{
4264 struct drm_i915_gem_busy *args = data;
4265 struct drm_gem_object *obj;
4266 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004267 int ret;
4268
Eric Anholt673a3942008-07-30 12:06:12 -07004269 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4270 if (obj == NULL) {
4271 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4272 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004273 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004274 }
4275
Chris Wilson76c1dec2010-09-25 11:22:51 +01004276 ret = i915_mutex_lock_interruptible(dev);
4277 if (ret) {
4278 drm_gem_object_unreference_unlocked(obj);
4279 return ret;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004280 }
4281
Chris Wilson0be555b2010-08-04 15:36:30 +01004282 /* Count all active objects as busy, even if they are currently not used
4283 * by the gpu. Users of this interface expect objects to eventually
4284 * become non-busy without any further actions, therefore emit any
4285 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004286 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004287 obj_priv = to_intel_bo(obj);
4288 args->busy = obj_priv->active;
4289 if (args->busy) {
4290 /* Unconditionally flush objects, even when the gpu still uses this
4291 * object. Userspace calling this function indicates that it wants to
4292 * use this buffer rather sooner than later, so issuing the required
4293 * flush earlier is beneficial.
4294 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004295 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4296 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004297 obj_priv->ring,
4298 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004299
4300 /* Update the active list for the hardware's current position.
4301 * Otherwise this only updates on a delayed timer or when irqs
4302 * are actually unmasked, and our working set ends up being
4303 * larger than required.
4304 */
4305 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4306
4307 args->busy = obj_priv->active;
4308 }
Eric Anholt673a3942008-07-30 12:06:12 -07004309
4310 drm_gem_object_unreference(obj);
4311 mutex_unlock(&dev->struct_mutex);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004312 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004313}
4314
4315int
4316i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4317 struct drm_file *file_priv)
4318{
4319 return i915_gem_ring_throttle(dev, file_priv);
4320}
4321
Chris Wilson3ef94da2009-09-14 16:50:29 +01004322int
4323i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4324 struct drm_file *file_priv)
4325{
4326 struct drm_i915_gem_madvise *args = data;
4327 struct drm_gem_object *obj;
4328 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004329 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004330
4331 switch (args->madv) {
4332 case I915_MADV_DONTNEED:
4333 case I915_MADV_WILLNEED:
4334 break;
4335 default:
4336 return -EINVAL;
4337 }
4338
4339 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4340 if (obj == NULL) {
4341 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4342 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004343 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004344 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004345 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004346
Chris Wilson76c1dec2010-09-25 11:22:51 +01004347 ret = i915_mutex_lock_interruptible(dev);
4348 if (ret) {
4349 drm_gem_object_unreference_unlocked(obj);
4350 return ret;
4351 }
4352
Chris Wilson3ef94da2009-09-14 16:50:29 +01004353 if (obj_priv->pin_count) {
4354 drm_gem_object_unreference(obj);
4355 mutex_unlock(&dev->struct_mutex);
4356
4357 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4358 return -EINVAL;
4359 }
4360
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004361 if (obj_priv->madv != __I915_MADV_PURGED)
4362 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004363
Chris Wilson2d7ef392009-09-20 23:13:10 +01004364 /* if the object is no longer bound, discard its backing storage */
4365 if (i915_gem_object_is_purgeable(obj_priv) &&
4366 obj_priv->gtt_space == NULL)
4367 i915_gem_object_truncate(obj);
4368
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004369 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4370
Chris Wilson3ef94da2009-09-14 16:50:29 +01004371 drm_gem_object_unreference(obj);
4372 mutex_unlock(&dev->struct_mutex);
4373
4374 return 0;
4375}
4376
Daniel Vetterac52bc52010-04-09 19:05:06 +00004377struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4378 size_t size)
4379{
Daniel Vetterc397b902010-04-09 19:05:07 +00004380 struct drm_i915_gem_object *obj;
4381
4382 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4383 if (obj == NULL)
4384 return NULL;
4385
4386 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4387 kfree(obj);
4388 return NULL;
4389 }
4390
4391 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4392 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4393
4394 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004395 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004396 obj->fence_reg = I915_FENCE_REG_NONE;
4397 INIT_LIST_HEAD(&obj->list);
4398 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004399 obj->madv = I915_MADV_WILLNEED;
4400
4401 trace_i915_gem_object_create(&obj->base);
4402
4403 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004404}
4405
Eric Anholt673a3942008-07-30 12:06:12 -07004406int i915_gem_init_object(struct drm_gem_object *obj)
4407{
Daniel Vetterc397b902010-04-09 19:05:07 +00004408 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004409
Eric Anholt673a3942008-07-30 12:06:12 -07004410 return 0;
4411}
4412
Chris Wilsonbe726152010-07-23 23:18:50 +01004413static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4414{
4415 struct drm_device *dev = obj->dev;
4416 drm_i915_private_t *dev_priv = dev->dev_private;
4417 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4418 int ret;
4419
4420 ret = i915_gem_object_unbind(obj);
4421 if (ret == -ERESTARTSYS) {
4422 list_move(&obj_priv->list,
4423 &dev_priv->mm.deferred_free_list);
4424 return;
4425 }
4426
4427 if (obj_priv->mmap_offset)
4428 i915_gem_free_mmap_offset(obj);
4429
4430 drm_gem_object_release(obj);
4431
4432 kfree(obj_priv->page_cpu_valid);
4433 kfree(obj_priv->bit_17);
4434 kfree(obj_priv);
4435}
4436
Eric Anholt673a3942008-07-30 12:06:12 -07004437void i915_gem_free_object(struct drm_gem_object *obj)
4438{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004439 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004440 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004441
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004442 trace_i915_gem_object_destroy(obj);
4443
Eric Anholt673a3942008-07-30 12:06:12 -07004444 while (obj_priv->pin_count > 0)
4445 i915_gem_object_unpin(obj);
4446
Dave Airlie71acb5e2008-12-30 20:31:46 +10004447 if (obj_priv->phys_obj)
4448 i915_gem_detach_phys_object(dev, obj);
4449
Chris Wilsonbe726152010-07-23 23:18:50 +01004450 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004451}
4452
Jesse Barnes5669fca2009-02-17 15:13:31 -08004453int
Eric Anholt673a3942008-07-30 12:06:12 -07004454i915_gem_idle(struct drm_device *dev)
4455{
4456 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004457 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004458
Keith Packard6dbe2772008-10-14 21:41:13 -07004459 mutex_lock(&dev->struct_mutex);
4460
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004461 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004462 (dev_priv->render_ring.gem_object == NULL) ||
4463 (HAS_BSD(dev) &&
4464 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004465 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004466 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004467 }
Eric Anholt673a3942008-07-30 12:06:12 -07004468
Chris Wilson29105cc2010-01-07 10:39:13 +00004469 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004470 if (ret) {
4471 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004472 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004473 }
Eric Anholt673a3942008-07-30 12:06:12 -07004474
Chris Wilson29105cc2010-01-07 10:39:13 +00004475 /* Under UMS, be paranoid and evict. */
4476 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004477 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004478 if (ret) {
4479 mutex_unlock(&dev->struct_mutex);
4480 return ret;
4481 }
4482 }
4483
4484 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4485 * We need to replace this with a semaphore, or something.
4486 * And not confound mm.suspended!
4487 */
4488 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004489 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004490
4491 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004492 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004493
Keith Packard6dbe2772008-10-14 21:41:13 -07004494 mutex_unlock(&dev->struct_mutex);
4495
Chris Wilson29105cc2010-01-07 10:39:13 +00004496 /* Cancel the retire work handler, which should be idle now. */
4497 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4498
Eric Anholt673a3942008-07-30 12:06:12 -07004499 return 0;
4500}
4501
Jesse Barnese552eb72010-04-21 11:39:23 -07004502/*
4503 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4504 * over cache flushing.
4505 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004506static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004507i915_gem_init_pipe_control(struct drm_device *dev)
4508{
4509 drm_i915_private_t *dev_priv = dev->dev_private;
4510 struct drm_gem_object *obj;
4511 struct drm_i915_gem_object *obj_priv;
4512 int ret;
4513
Eric Anholt34dc4d42010-05-07 14:30:03 -07004514 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004515 if (obj == NULL) {
4516 DRM_ERROR("Failed to allocate seqno page\n");
4517 ret = -ENOMEM;
4518 goto err;
4519 }
4520 obj_priv = to_intel_bo(obj);
4521 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4522
4523 ret = i915_gem_object_pin(obj, 4096);
4524 if (ret)
4525 goto err_unref;
4526
4527 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4528 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4529 if (dev_priv->seqno_page == NULL)
4530 goto err_unpin;
4531
4532 dev_priv->seqno_obj = obj;
4533 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4534
4535 return 0;
4536
4537err_unpin:
4538 i915_gem_object_unpin(obj);
4539err_unref:
4540 drm_gem_object_unreference(obj);
4541err:
4542 return ret;
4543}
4544
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004545
4546static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004547i915_gem_cleanup_pipe_control(struct drm_device *dev)
4548{
4549 drm_i915_private_t *dev_priv = dev->dev_private;
4550 struct drm_gem_object *obj;
4551 struct drm_i915_gem_object *obj_priv;
4552
4553 obj = dev_priv->seqno_obj;
4554 obj_priv = to_intel_bo(obj);
4555 kunmap(obj_priv->pages[0]);
4556 i915_gem_object_unpin(obj);
4557 drm_gem_object_unreference(obj);
4558 dev_priv->seqno_obj = NULL;
4559
4560 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004561}
4562
Eric Anholt673a3942008-07-30 12:06:12 -07004563int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004564i915_gem_init_ringbuffer(struct drm_device *dev)
4565{
4566 drm_i915_private_t *dev_priv = dev->dev_private;
4567 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004568
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004569 if (HAS_PIPE_CONTROL(dev)) {
4570 ret = i915_gem_init_pipe_control(dev);
4571 if (ret)
4572 return ret;
4573 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004574
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004575 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004576 if (ret)
4577 goto cleanup_pipe_control;
4578
4579 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004580 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004581 if (ret)
4582 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004583 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004584
Chris Wilson6f392d52010-08-07 11:01:22 +01004585 dev_priv->next_seqno = 1;
4586
Chris Wilson68f95ba2010-05-27 13:18:22 +01004587 return 0;
4588
4589cleanup_render_ring:
4590 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4591cleanup_pipe_control:
4592 if (HAS_PIPE_CONTROL(dev))
4593 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004594 return ret;
4595}
4596
4597void
4598i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4599{
4600 drm_i915_private_t *dev_priv = dev->dev_private;
4601
4602 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004603 if (HAS_BSD(dev))
4604 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004605 if (HAS_PIPE_CONTROL(dev))
4606 i915_gem_cleanup_pipe_control(dev);
4607}
4608
4609int
Eric Anholt673a3942008-07-30 12:06:12 -07004610i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4611 struct drm_file *file_priv)
4612{
4613 drm_i915_private_t *dev_priv = dev->dev_private;
4614 int ret;
4615
Jesse Barnes79e53942008-11-07 14:24:08 -08004616 if (drm_core_check_feature(dev, DRIVER_MODESET))
4617 return 0;
4618
Ben Gamariba1234d2009-09-14 17:48:47 -04004619 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004620 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004621 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004622 }
4623
Eric Anholt673a3942008-07-30 12:06:12 -07004624 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004625 dev_priv->mm.suspended = 0;
4626
4627 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004628 if (ret != 0) {
4629 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004630 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004631 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004632
Zou Nan hai852835f2010-05-21 09:08:56 +08004633 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004634 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004635 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4636 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004637 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004638 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004639 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004640
Chris Wilson5f353082010-06-07 14:03:03 +01004641 ret = drm_irq_install(dev);
4642 if (ret)
4643 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004644
Eric Anholt673a3942008-07-30 12:06:12 -07004645 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004646
4647cleanup_ringbuffer:
4648 mutex_lock(&dev->struct_mutex);
4649 i915_gem_cleanup_ringbuffer(dev);
4650 dev_priv->mm.suspended = 1;
4651 mutex_unlock(&dev->struct_mutex);
4652
4653 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004654}
4655
4656int
4657i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4658 struct drm_file *file_priv)
4659{
Jesse Barnes79e53942008-11-07 14:24:08 -08004660 if (drm_core_check_feature(dev, DRIVER_MODESET))
4661 return 0;
4662
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004663 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004664 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004665}
4666
4667void
4668i915_gem_lastclose(struct drm_device *dev)
4669{
4670 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004671
Eric Anholte806b492009-01-22 09:56:58 -08004672 if (drm_core_check_feature(dev, DRIVER_MODESET))
4673 return;
4674
Keith Packard6dbe2772008-10-14 21:41:13 -07004675 ret = i915_gem_idle(dev);
4676 if (ret)
4677 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004678}
4679
4680void
4681i915_gem_load(struct drm_device *dev)
4682{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004683 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004684 drm_i915_private_t *dev_priv = dev->dev_private;
4685
Eric Anholt673a3942008-07-30 12:06:12 -07004686 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004687 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004688 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004689 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004690 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004691 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004692 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4693 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004694 if (HAS_BSD(dev)) {
4695 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4696 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4697 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004698 for (i = 0; i < 16; i++)
4699 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004700 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4701 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004702 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004703 spin_lock(&shrink_list_lock);
4704 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4705 spin_unlock(&shrink_list_lock);
4706
Dave Airlie94400122010-07-20 13:15:31 +10004707 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4708 if (IS_GEN3(dev)) {
4709 u32 tmp = I915_READ(MI_ARB_STATE);
4710 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4711 /* arb state is a masked write, so set bit + bit in mask */
4712 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4713 I915_WRITE(MI_ARB_STATE, tmp);
4714 }
4715 }
4716
Jesse Barnesde151cf2008-11-12 10:03:55 -08004717 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004718 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4719 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004720
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004721 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004722 dev_priv->num_fence_regs = 16;
4723 else
4724 dev_priv->num_fence_regs = 8;
4725
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004726 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004727 switch (INTEL_INFO(dev)->gen) {
4728 case 6:
4729 for (i = 0; i < 16; i++)
4730 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4731 break;
4732 case 5:
4733 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004734 for (i = 0; i < 16; i++)
4735 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004736 break;
4737 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004738 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4739 for (i = 0; i < 8; i++)
4740 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004741 case 2:
4742 for (i = 0; i < 8; i++)
4743 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4744 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004745 }
Eric Anholt673a3942008-07-30 12:06:12 -07004746 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004747 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004748}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004749
4750/*
4751 * Create a physically contiguous memory object for this object
4752 * e.g. for cursor + overlay regs
4753 */
Chris Wilson995b6762010-08-20 13:23:26 +01004754static int i915_gem_init_phys_object(struct drm_device *dev,
4755 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004756{
4757 drm_i915_private_t *dev_priv = dev->dev_private;
4758 struct drm_i915_gem_phys_object *phys_obj;
4759 int ret;
4760
4761 if (dev_priv->mm.phys_objs[id - 1] || !size)
4762 return 0;
4763
Eric Anholt9a298b22009-03-24 12:23:04 -07004764 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004765 if (!phys_obj)
4766 return -ENOMEM;
4767
4768 phys_obj->id = id;
4769
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004770 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004771 if (!phys_obj->handle) {
4772 ret = -ENOMEM;
4773 goto kfree_obj;
4774 }
4775#ifdef CONFIG_X86
4776 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4777#endif
4778
4779 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4780
4781 return 0;
4782kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004783 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004784 return ret;
4785}
4786
Chris Wilson995b6762010-08-20 13:23:26 +01004787static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004788{
4789 drm_i915_private_t *dev_priv = dev->dev_private;
4790 struct drm_i915_gem_phys_object *phys_obj;
4791
4792 if (!dev_priv->mm.phys_objs[id - 1])
4793 return;
4794
4795 phys_obj = dev_priv->mm.phys_objs[id - 1];
4796 if (phys_obj->cur_obj) {
4797 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4798 }
4799
4800#ifdef CONFIG_X86
4801 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4802#endif
4803 drm_pci_free(dev, phys_obj->handle);
4804 kfree(phys_obj);
4805 dev_priv->mm.phys_objs[id - 1] = NULL;
4806}
4807
4808void i915_gem_free_all_phys_object(struct drm_device *dev)
4809{
4810 int i;
4811
Dave Airlie260883c2009-01-22 17:58:49 +10004812 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004813 i915_gem_free_phys_object(dev, i);
4814}
4815
4816void i915_gem_detach_phys_object(struct drm_device *dev,
4817 struct drm_gem_object *obj)
4818{
4819 struct drm_i915_gem_object *obj_priv;
4820 int i;
4821 int ret;
4822 int page_count;
4823
Daniel Vetter23010e42010-03-08 13:35:02 +01004824 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004825 if (!obj_priv->phys_obj)
4826 return;
4827
Chris Wilson4bdadb92010-01-27 13:36:32 +00004828 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004829 if (ret)
4830 goto out;
4831
4832 page_count = obj->size / PAGE_SIZE;
4833
4834 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004835 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004836 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4837
4838 memcpy(dst, src, PAGE_SIZE);
4839 kunmap_atomic(dst, KM_USER0);
4840 }
Eric Anholt856fa192009-03-19 14:10:50 -07004841 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004842 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004843
4844 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004845out:
4846 obj_priv->phys_obj->cur_obj = NULL;
4847 obj_priv->phys_obj = NULL;
4848}
4849
4850int
4851i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004852 struct drm_gem_object *obj,
4853 int id,
4854 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004855{
4856 drm_i915_private_t *dev_priv = dev->dev_private;
4857 struct drm_i915_gem_object *obj_priv;
4858 int ret = 0;
4859 int page_count;
4860 int i;
4861
4862 if (id > I915_MAX_PHYS_OBJECT)
4863 return -EINVAL;
4864
Daniel Vetter23010e42010-03-08 13:35:02 +01004865 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004866
4867 if (obj_priv->phys_obj) {
4868 if (obj_priv->phys_obj->id == id)
4869 return 0;
4870 i915_gem_detach_phys_object(dev, obj);
4871 }
4872
Dave Airlie71acb5e2008-12-30 20:31:46 +10004873 /* create a new object */
4874 if (!dev_priv->mm.phys_objs[id - 1]) {
4875 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004876 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004877 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004878 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004879 goto out;
4880 }
4881 }
4882
4883 /* bind to the object */
4884 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4885 obj_priv->phys_obj->cur_obj = obj;
4886
Chris Wilson4bdadb92010-01-27 13:36:32 +00004887 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004888 if (ret) {
4889 DRM_ERROR("failed to get page list\n");
4890 goto out;
4891 }
4892
4893 page_count = obj->size / PAGE_SIZE;
4894
4895 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004896 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004897 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4898
4899 memcpy(dst, src, PAGE_SIZE);
4900 kunmap_atomic(src, KM_USER0);
4901 }
4902
Chris Wilsond78b47b2009-06-17 21:52:49 +01004903 i915_gem_object_put_pages(obj);
4904
Dave Airlie71acb5e2008-12-30 20:31:46 +10004905 return 0;
4906out:
4907 return ret;
4908}
4909
4910static int
4911i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4912 struct drm_i915_gem_pwrite *args,
4913 struct drm_file *file_priv)
4914{
Daniel Vetter23010e42010-03-08 13:35:02 +01004915 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004916 void *obj_addr;
4917 int ret;
4918 char __user *user_data;
4919
4920 user_data = (char __user *) (uintptr_t) args->data_ptr;
4921 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4922
Zhao Yakui44d98a62009-10-09 11:39:40 +08004923 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004924 ret = copy_from_user(obj_addr, user_data, args->size);
4925 if (ret)
4926 return -EFAULT;
4927
4928 drm_agp_chipset_flush(dev);
4929 return 0;
4930}
Eric Anholtb9624422009-06-03 07:27:35 +00004931
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004932void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004933{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004934 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004935
4936 /* Clean up our request list when the client is going away, so that
4937 * later retire_requests won't dereference our soon-to-be-gone
4938 * file_priv.
4939 */
Chris Wilson1c255952010-09-26 11:03:27 +01004940 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004941 while (!list_empty(&file_priv->mm.request_list)) {
4942 struct drm_i915_gem_request *request;
4943
4944 request = list_first_entry(&file_priv->mm.request_list,
4945 struct drm_i915_gem_request,
4946 client_list);
4947 list_del(&request->client_list);
4948 request->file_priv = NULL;
4949 }
Chris Wilson1c255952010-09-26 11:03:27 +01004950 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004951}
Chris Wilson31169712009-09-14 16:50:28 +01004952
Chris Wilson31169712009-09-14 16:50:28 +01004953static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004954i915_gpu_is_active(struct drm_device *dev)
4955{
4956 drm_i915_private_t *dev_priv = dev->dev_private;
4957 int lists_empty;
4958
Chris Wilson1637ef42010-04-20 17:10:35 +01004959 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004960 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004961 if (HAS_BSD(dev))
4962 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004963
4964 return !lists_empty;
4965}
4966
4967static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004968i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004969{
4970 drm_i915_private_t *dev_priv, *next_dev;
4971 struct drm_i915_gem_object *obj_priv, *next_obj;
4972 int cnt = 0;
4973 int would_deadlock = 1;
4974
4975 /* "fast-path" to count number of available objects */
4976 if (nr_to_scan == 0) {
4977 spin_lock(&shrink_list_lock);
4978 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4979 struct drm_device *dev = dev_priv->dev;
4980
4981 if (mutex_trylock(&dev->struct_mutex)) {
4982 list_for_each_entry(obj_priv,
4983 &dev_priv->mm.inactive_list,
4984 list)
4985 cnt++;
4986 mutex_unlock(&dev->struct_mutex);
4987 }
4988 }
4989 spin_unlock(&shrink_list_lock);
4990
4991 return (cnt / 100) * sysctl_vfs_cache_pressure;
4992 }
4993
4994 spin_lock(&shrink_list_lock);
4995
Chris Wilson1637ef42010-04-20 17:10:35 +01004996rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004997 /* first scan for clean buffers */
4998 list_for_each_entry_safe(dev_priv, next_dev,
4999 &shrink_list, mm.shrink_list) {
5000 struct drm_device *dev = dev_priv->dev;
5001
5002 if (! mutex_trylock(&dev->struct_mutex))
5003 continue;
5004
5005 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005006 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005007
Chris Wilson31169712009-09-14 16:50:28 +01005008 list_for_each_entry_safe(obj_priv, next_obj,
5009 &dev_priv->mm.inactive_list,
5010 list) {
5011 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005012 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005013 if (--nr_to_scan <= 0)
5014 break;
5015 }
5016 }
5017
5018 spin_lock(&shrink_list_lock);
5019 mutex_unlock(&dev->struct_mutex);
5020
Chris Wilson963b4832009-09-20 23:03:54 +01005021 would_deadlock = 0;
5022
Chris Wilson31169712009-09-14 16:50:28 +01005023 if (nr_to_scan <= 0)
5024 break;
5025 }
5026
5027 /* second pass, evict/count anything still on the inactive list */
5028 list_for_each_entry_safe(dev_priv, next_dev,
5029 &shrink_list, mm.shrink_list) {
5030 struct drm_device *dev = dev_priv->dev;
5031
5032 if (! mutex_trylock(&dev->struct_mutex))
5033 continue;
5034
5035 spin_unlock(&shrink_list_lock);
5036
5037 list_for_each_entry_safe(obj_priv, next_obj,
5038 &dev_priv->mm.inactive_list,
5039 list) {
5040 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005041 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005042 nr_to_scan--;
5043 } else
5044 cnt++;
5045 }
5046
5047 spin_lock(&shrink_list_lock);
5048 mutex_unlock(&dev->struct_mutex);
5049
5050 would_deadlock = 0;
5051 }
5052
Chris Wilson1637ef42010-04-20 17:10:35 +01005053 if (nr_to_scan) {
5054 int active = 0;
5055
5056 /*
5057 * We are desperate for pages, so as a last resort, wait
5058 * for the GPU to finish and discard whatever we can.
5059 * This has a dramatic impact to reduce the number of
5060 * OOM-killer events whilst running the GPU aggressively.
5061 */
5062 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5063 struct drm_device *dev = dev_priv->dev;
5064
5065 if (!mutex_trylock(&dev->struct_mutex))
5066 continue;
5067
5068 spin_unlock(&shrink_list_lock);
5069
5070 if (i915_gpu_is_active(dev)) {
5071 i915_gpu_idle(dev);
5072 active++;
5073 }
5074
5075 spin_lock(&shrink_list_lock);
5076 mutex_unlock(&dev->struct_mutex);
5077 }
5078
5079 if (active)
5080 goto rescan;
5081 }
5082
Chris Wilson31169712009-09-14 16:50:28 +01005083 spin_unlock(&shrink_list_lock);
5084
5085 if (would_deadlock)
5086 return -1;
5087 else if (cnt > 0)
5088 return (cnt / 100) * sysctl_vfs_cache_pressure;
5089 else
5090 return 0;
5091}
5092
5093static struct shrinker shrinker = {
5094 .shrink = i915_gem_shrink,
5095 .seeks = DEFAULT_SEEKS,
5096};
5097
5098__init void
5099i915_gem_shrinker_init(void)
5100{
5101 register_shrinker(&shrinker);
5102}
5103
5104__exit void
5105i915_gem_shrinker_exit(void)
5106{
5107 unregister_shrinker(&shrinker);
5108}