blob: fe1424c6c3fa502e28ce00b43e921ed317327d5f [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 Wilson9e0ae5342010-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 Wilson9e0ae5342010-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
Chris Wilson891b48c2010-09-29 12:26:37 +01001904 /* Come back later if the device is busy... */
1905 if (!mutex_trylock(&dev->struct_mutex)) {
1906 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1907 return;
1908 }
1909
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001910 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001911
Keith Packard6dbe2772008-10-14 21:41:13 -07001912 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001913 (!list_empty(&dev_priv->render_ring.request_list) ||
1914 (HAS_BSD(dev) &&
1915 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001916 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001917 mutex_unlock(&dev->struct_mutex);
1918}
1919
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001920int
Zou Nan hai852835f2010-05-21 09:08:56 +08001921i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001922 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001923{
1924 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001925 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001926 int ret = 0;
1927
1928 BUG_ON(seqno == 0);
1929
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001930 if (atomic_read(&dev_priv->mm.wedged))
1931 return -EAGAIN;
1932
Chris Wilsona56ba562010-09-28 10:07:56 +01001933 if (ring->outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01001934 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001935 if (seqno == 0)
1936 return -ENOMEM;
1937 }
Chris Wilsona56ba562010-09-28 10:07:56 +01001938 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001939
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001940 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001941 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001942 ier = I915_READ(DEIER) | I915_READ(GTIER);
1943 else
1944 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001945 if (!ier) {
1946 DRM_ERROR("something (likely vbetool) disabled "
1947 "interrupts, re-enabling\n");
1948 i915_driver_irq_preinstall(dev);
1949 i915_driver_irq_postinstall(dev);
1950 }
1951
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001952 trace_i915_gem_request_wait_begin(dev, seqno);
1953
Zou Nan hai852835f2010-05-21 09:08:56 +08001954 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001955 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001956 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001957 ret = wait_event_interruptible(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 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001962 wait_event(ring->irq_queue,
1963 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001964 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001965 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001966
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001967 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001968 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001969
1970 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001971 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001972 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001973 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07001974
1975 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01001976 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001977 __func__, ret, seqno, ring->get_seqno(dev, ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01001978 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001979
1980 /* Directly dispatch request retiring. While we have the work queue
1981 * to handle this, the waiter on a request often wants an associated
1982 * buffer to have made it to the inactive list, and we would need
1983 * a separate wait queue to handle that.
1984 */
1985 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001986 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001987
1988 return ret;
1989}
1990
Daniel Vetter48764bf2009-09-15 22:57:32 +02001991/**
1992 * Waits for a sequence number to be signaled, and cleans up the
1993 * request and object lists appropriately for that event.
1994 */
1995static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001996i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01001997 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001998{
Zou Nan hai852835f2010-05-21 09:08:56 +08001999 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002000}
2001
Chris Wilson20f0cd52010-09-23 11:00:38 +01002002static void
Chris Wilson92204342010-09-18 11:02:01 +01002003i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002004 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002005 struct intel_ring_buffer *ring,
2006 uint32_t invalidate_domains,
2007 uint32_t flush_domains)
2008{
2009 ring->flush(dev, ring, invalidate_domains, flush_domains);
2010 i915_gem_process_flushing_list(dev, flush_domains, ring);
2011}
2012
2013static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002014i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002015 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002016 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002017 uint32_t flush_domains,
2018 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002019{
2020 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002021
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002022 if (flush_domains & I915_GEM_DOMAIN_CPU)
2023 drm_agp_chipset_flush(dev);
Daniel Vetter8bff9172010-02-11 22:19:40 +01002024
Chris Wilson92204342010-09-18 11:02:01 +01002025 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2026 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002027 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002028 &dev_priv->render_ring,
2029 invalidate_domains, flush_domains);
2030 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002031 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002032 &dev_priv->bsd_ring,
2033 invalidate_domains, flush_domains);
2034 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002035}
2036
Eric Anholt673a3942008-07-30 12:06:12 -07002037/**
2038 * Ensures that all rendering to the object has completed and the object is
2039 * safe to unbind from the GTT or access from the CPU.
2040 */
2041static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002042i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2043 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002044{
2045 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002046 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002047 int ret;
2048
Eric Anholte47c68e2008-11-14 13:35:19 -08002049 /* This function only exists to support waiting for existing rendering,
2050 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002051 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002052 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002053
2054 /* If there is rendering queued on the buffer being evicted, wait for
2055 * it.
2056 */
2057 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002058 ret = i915_do_wait_request(dev,
2059 obj_priv->last_rendering_seqno,
2060 interruptible,
2061 obj_priv->ring);
2062 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002063 return ret;
2064 }
2065
2066 return 0;
2067}
2068
2069/**
2070 * Unbinds an object from the GTT aperture.
2071 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002072int
Eric Anholt673a3942008-07-30 12:06:12 -07002073i915_gem_object_unbind(struct drm_gem_object *obj)
2074{
2075 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002076 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002077 int ret = 0;
2078
Eric Anholt673a3942008-07-30 12:06:12 -07002079 if (obj_priv->gtt_space == NULL)
2080 return 0;
2081
2082 if (obj_priv->pin_count != 0) {
2083 DRM_ERROR("Attempting to unbind pinned buffer\n");
2084 return -EINVAL;
2085 }
2086
Eric Anholt5323fd02009-09-09 11:50:45 -07002087 /* blow away mappings if mapped through GTT */
2088 i915_gem_release_mmap(obj);
2089
Eric Anholt673a3942008-07-30 12:06:12 -07002090 /* Move the object to the CPU domain to ensure that
2091 * any possible CPU writes while it's not in the GTT
2092 * are flushed when we go to remap it. This will
2093 * also ensure that all pending GPU writes are finished
2094 * before we unbind.
2095 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002096 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002097 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002098 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002099 /* Continue on if we fail due to EIO, the GPU is hung so we
2100 * should be safe and we need to cleanup or else we might
2101 * cause memory corruption through use-after-free.
2102 */
Eric Anholt673a3942008-07-30 12:06:12 -07002103
Daniel Vetter96b47b62009-12-15 17:50:00 +01002104 /* release the fence reg _after_ flushing */
2105 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2106 i915_gem_clear_fence_reg(obj);
2107
Eric Anholt673a3942008-07-30 12:06:12 -07002108 if (obj_priv->agp_mem != NULL) {
2109 drm_unbind_agp(obj_priv->agp_mem);
2110 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2111 obj_priv->agp_mem = NULL;
2112 }
2113
Eric Anholt856fa192009-03-19 14:10:50 -07002114 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002115 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002116
2117 if (obj_priv->gtt_space) {
2118 atomic_dec(&dev->gtt_count);
2119 atomic_sub(obj->size, &dev->gtt_memory);
2120
2121 drm_mm_put_block(obj_priv->gtt_space);
2122 obj_priv->gtt_space = NULL;
2123 }
2124
Chris Wilsonf13d3f72010-09-20 17:36:15 +01002125 list_del_init(&obj_priv->list);
Eric Anholt673a3942008-07-30 12:06:12 -07002126
Chris Wilson963b4832009-09-20 23:03:54 +01002127 if (i915_gem_object_is_purgeable(obj_priv))
2128 i915_gem_object_truncate(obj);
2129
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002130 trace_i915_gem_object_unbind(obj);
2131
Chris Wilson8dc17752010-07-23 23:18:51 +01002132 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002133}
2134
Chris Wilsona56ba562010-09-28 10:07:56 +01002135static int i915_ring_idle(struct drm_device *dev,
2136 struct intel_ring_buffer *ring)
2137{
2138 i915_gem_flush_ring(dev, NULL, ring,
2139 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2140 return i915_wait_request(dev,
2141 i915_gem_next_request_seqno(dev, ring),
2142 ring);
2143}
2144
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002145int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002146i915_gpu_idle(struct drm_device *dev)
2147{
2148 drm_i915_private_t *dev_priv = dev->dev_private;
2149 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002150 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002151
Zou Nan haid1b851f2010-05-21 09:08:57 +08002152 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2153 list_empty(&dev_priv->render_ring.active_list) &&
2154 (!HAS_BSD(dev) ||
2155 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002156 if (lists_empty)
2157 return 0;
2158
2159 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002160 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002161 if (ret)
2162 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002163
2164 if (HAS_BSD(dev)) {
Chris Wilsona56ba562010-09-28 10:07:56 +01002165 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002166 if (ret)
2167 return ret;
2168 }
2169
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002170 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002171}
2172
Ben Gamari6911a9b2009-04-02 11:24:54 -07002173int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002174i915_gem_object_get_pages(struct drm_gem_object *obj,
2175 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002176{
Daniel Vetter23010e42010-03-08 13:35:02 +01002177 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002178 int page_count, i;
2179 struct address_space *mapping;
2180 struct inode *inode;
2181 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002182
Daniel Vetter778c3542010-05-13 11:49:44 +02002183 BUG_ON(obj_priv->pages_refcount
2184 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2185
Eric Anholt856fa192009-03-19 14:10:50 -07002186 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002187 return 0;
2188
2189 /* Get the list of pages out of our struct file. They'll be pinned
2190 * at this point until we release them.
2191 */
2192 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002193 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002194 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002195 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002196 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002197 return -ENOMEM;
2198 }
2199
2200 inode = obj->filp->f_path.dentry->d_inode;
2201 mapping = inode->i_mapping;
2202 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002203 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002204 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002205 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002206 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002207 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002208 if (IS_ERR(page))
2209 goto err_pages;
2210
Eric Anholt856fa192009-03-19 14:10:50 -07002211 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002212 }
Eric Anholt280b7132009-03-12 16:56:27 -07002213
2214 if (obj_priv->tiling_mode != I915_TILING_NONE)
2215 i915_gem_object_do_bit_17_swizzle(obj);
2216
Eric Anholt673a3942008-07-30 12:06:12 -07002217 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002218
2219err_pages:
2220 while (i--)
2221 page_cache_release(obj_priv->pages[i]);
2222
2223 drm_free_large(obj_priv->pages);
2224 obj_priv->pages = NULL;
2225 obj_priv->pages_refcount--;
2226 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002227}
2228
Eric Anholt4e901fd2009-10-26 16:44:17 -07002229static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2230{
2231 struct drm_gem_object *obj = reg->obj;
2232 struct drm_device *dev = obj->dev;
2233 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002234 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002235 int regnum = obj_priv->fence_reg;
2236 uint64_t val;
2237
2238 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2239 0xfffff000) << 32;
2240 val |= obj_priv->gtt_offset & 0xfffff000;
2241 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2242 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2243
2244 if (obj_priv->tiling_mode == I915_TILING_Y)
2245 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2246 val |= I965_FENCE_REG_VALID;
2247
2248 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2249}
2250
Jesse Barnesde151cf2008-11-12 10:03:55 -08002251static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2252{
2253 struct drm_gem_object *obj = reg->obj;
2254 struct drm_device *dev = obj->dev;
2255 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002256 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002257 int regnum = obj_priv->fence_reg;
2258 uint64_t val;
2259
2260 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2261 0xfffff000) << 32;
2262 val |= obj_priv->gtt_offset & 0xfffff000;
2263 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2264 if (obj_priv->tiling_mode == I915_TILING_Y)
2265 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2266 val |= I965_FENCE_REG_VALID;
2267
2268 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2269}
2270
2271static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2272{
2273 struct drm_gem_object *obj = reg->obj;
2274 struct drm_device *dev = obj->dev;
2275 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002276 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002277 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002278 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002279 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002280 uint32_t pitch_val;
2281
2282 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2283 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002284 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002285 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002286 return;
2287 }
2288
Jesse Barnes0f973f22009-01-26 17:10:45 -08002289 if (obj_priv->tiling_mode == I915_TILING_Y &&
2290 HAS_128_BYTE_Y_TILING(dev))
2291 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002292 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002293 tile_width = 512;
2294
2295 /* Note: pitch better be a power of two tile widths */
2296 pitch_val = obj_priv->stride / tile_width;
2297 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002298
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002299 if (obj_priv->tiling_mode == I915_TILING_Y &&
2300 HAS_128_BYTE_Y_TILING(dev))
2301 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2302 else
2303 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2304
Jesse Barnesde151cf2008-11-12 10:03:55 -08002305 val = obj_priv->gtt_offset;
2306 if (obj_priv->tiling_mode == I915_TILING_Y)
2307 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2308 val |= I915_FENCE_SIZE_BITS(obj->size);
2309 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2310 val |= I830_FENCE_REG_VALID;
2311
Eric Anholtdc529a42009-03-10 22:34:49 -07002312 if (regnum < 8)
2313 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2314 else
2315 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2316 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317}
2318
2319static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2320{
2321 struct drm_gem_object *obj = reg->obj;
2322 struct drm_device *dev = obj->dev;
2323 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002324 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002325 int regnum = obj_priv->fence_reg;
2326 uint32_t val;
2327 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002328 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002329
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002330 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002331 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002332 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002333 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002334 return;
2335 }
2336
Eric Anholte76a16d2009-05-26 17:44:56 -07002337 pitch_val = obj_priv->stride / 128;
2338 pitch_val = ffs(pitch_val) - 1;
2339 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2340
Jesse Barnesde151cf2008-11-12 10:03:55 -08002341 val = obj_priv->gtt_offset;
2342 if (obj_priv->tiling_mode == I915_TILING_Y)
2343 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002344 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2345 WARN_ON(fence_size_bits & ~0x00000f00);
2346 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002347 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2348 val |= I830_FENCE_REG_VALID;
2349
2350 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002351}
2352
Chris Wilson2cf34d72010-09-14 13:03:28 +01002353static int i915_find_fence_reg(struct drm_device *dev,
2354 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002355{
2356 struct drm_i915_fence_reg *reg = NULL;
2357 struct drm_i915_gem_object *obj_priv = NULL;
2358 struct drm_i915_private *dev_priv = dev->dev_private;
2359 struct drm_gem_object *obj = NULL;
2360 int i, avail, ret;
2361
2362 /* First try to find a free reg */
2363 avail = 0;
2364 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2365 reg = &dev_priv->fence_regs[i];
2366 if (!reg->obj)
2367 return i;
2368
Daniel Vetter23010e42010-03-08 13:35:02 +01002369 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002370 if (!obj_priv->pin_count)
2371 avail++;
2372 }
2373
2374 if (avail == 0)
2375 return -ENOSPC;
2376
2377 /* None available, try to steal one or wait for a user to finish */
2378 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002379 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2380 lru_list) {
2381 obj = reg->obj;
2382 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002383
2384 if (obj_priv->pin_count)
2385 continue;
2386
2387 /* found one! */
2388 i = obj_priv->fence_reg;
2389 break;
2390 }
2391
2392 BUG_ON(i == I915_FENCE_REG_NONE);
2393
2394 /* We only have a reference on obj from the active list. put_fence_reg
2395 * might drop that one, causing a use-after-free in it. So hold a
2396 * private reference to obj like the other callers of put_fence_reg
2397 * (set_tiling ioctl) do. */
2398 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002399 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002400 drm_gem_object_unreference(obj);
2401 if (ret != 0)
2402 return ret;
2403
2404 return i;
2405}
2406
Jesse Barnesde151cf2008-11-12 10:03:55 -08002407/**
2408 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2409 * @obj: object to map through a fence reg
2410 *
2411 * When mapping objects through the GTT, userspace wants to be able to write
2412 * to them without having to worry about swizzling if the object is tiled.
2413 *
2414 * This function walks the fence regs looking for a free one for @obj,
2415 * stealing one if it can't find any.
2416 *
2417 * It then sets up the reg based on the object's properties: address, pitch
2418 * and tiling format.
2419 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002420int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002421i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2422 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002423{
2424 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002425 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002426 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002427 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002428 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002429
Eric Anholta09ba7f2009-08-29 12:49:51 -07002430 /* Just update our place in the LRU if our fence is getting used. */
2431 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002432 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2433 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002434 return 0;
2435 }
2436
Jesse Barnesde151cf2008-11-12 10:03:55 -08002437 switch (obj_priv->tiling_mode) {
2438 case I915_TILING_NONE:
2439 WARN(1, "allocating a fence for non-tiled object?\n");
2440 break;
2441 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002442 if (!obj_priv->stride)
2443 return -EINVAL;
2444 WARN((obj_priv->stride & (512 - 1)),
2445 "object 0x%08x is X tiled but has non-512B pitch\n",
2446 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002447 break;
2448 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002449 if (!obj_priv->stride)
2450 return -EINVAL;
2451 WARN((obj_priv->stride & (128 - 1)),
2452 "object 0x%08x is Y tiled but has non-128B pitch\n",
2453 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002454 break;
2455 }
2456
Chris Wilson2cf34d72010-09-14 13:03:28 +01002457 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002458 if (ret < 0)
2459 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002460
Daniel Vetterae3db242010-02-19 11:51:58 +01002461 obj_priv->fence_reg = ret;
2462 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002463 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002464
Jesse Barnesde151cf2008-11-12 10:03:55 -08002465 reg->obj = obj;
2466
Chris Wilsone259bef2010-09-17 00:32:02 +01002467 switch (INTEL_INFO(dev)->gen) {
2468 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002469 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002470 break;
2471 case 5:
2472 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002473 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002474 break;
2475 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002476 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002477 break;
2478 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002479 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002480 break;
2481 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002482
Daniel Vetterae3db242010-02-19 11:51:58 +01002483 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2484 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002485
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002486 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002487}
2488
2489/**
2490 * i915_gem_clear_fence_reg - clear out fence register info
2491 * @obj: object to clear
2492 *
2493 * Zeroes out the fence register itself and clears out the associated
2494 * data structures in dev_priv and obj_priv.
2495 */
2496static void
2497i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2498{
2499 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002500 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002501 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002502 struct drm_i915_fence_reg *reg =
2503 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002504 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002505
Chris Wilsone259bef2010-09-17 00:32:02 +01002506 switch (INTEL_INFO(dev)->gen) {
2507 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002508 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2509 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002510 break;
2511 case 5:
2512 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002513 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002514 break;
2515 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002516 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002517 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002518 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002519 case 2:
2520 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002521
2522 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002523 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002524 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002525
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002526 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002527 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002528 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002529}
2530
Eric Anholt673a3942008-07-30 12:06:12 -07002531/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002532 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2533 * to the buffer to finish, and then resets the fence register.
2534 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002535 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002536 *
2537 * Zeroes out the fence register itself and clears out the associated
2538 * data structures in dev_priv and obj_priv.
2539 */
2540int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002541i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2542 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002543{
2544 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002545 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002546 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002547 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002548
2549 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2550 return 0;
2551
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002552 /* If we've changed tiling, GTT-mappings of the object
2553 * need to re-fault to ensure that the correct fence register
2554 * setup is in place.
2555 */
2556 i915_gem_release_mmap(obj);
2557
Chris Wilson52dc7d32009-06-06 09:46:01 +01002558 /* On the i915, GPU access to tiled buffers is via a fence,
2559 * therefore we must wait for any outstanding access to complete
2560 * before clearing the fence.
2561 */
Chris Wilson53640e12010-09-20 11:40:50 +01002562 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2563 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002564 int ret;
2565
Chris Wilson2cf34d72010-09-14 13:03:28 +01002566 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002567 if (ret)
2568 return ret;
2569
Chris Wilson2cf34d72010-09-14 13:03:28 +01002570 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002571 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002572 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002573
2574 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002575 }
2576
Daniel Vetter4a726612010-02-01 13:59:16 +01002577 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002578 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002579
2580 return 0;
2581}
2582
2583/**
Eric Anholt673a3942008-07-30 12:06:12 -07002584 * Finds free space in the GTT aperture and binds the object there.
2585 */
2586static int
2587i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2588{
2589 struct drm_device *dev = obj->dev;
2590 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002591 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002592 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002593 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002594 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002595
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002596 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002597 DRM_ERROR("Attempting to bind a purgeable object\n");
2598 return -EINVAL;
2599 }
2600
Eric Anholt673a3942008-07-30 12:06:12 -07002601 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002602 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002603 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002604 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2605 return -EINVAL;
2606 }
2607
Chris Wilson654fc602010-05-27 13:18:21 +01002608 /* If the object is bigger than the entire aperture, reject it early
2609 * before evicting everything in a vain attempt to find space.
2610 */
2611 if (obj->size > dev->gtt_total) {
2612 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2613 return -E2BIG;
2614 }
2615
Eric Anholt673a3942008-07-30 12:06:12 -07002616 search_free:
2617 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2618 obj->size, alignment, 0);
2619 if (free_space != NULL) {
2620 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2621 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002622 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002623 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002624 }
2625 if (obj_priv->gtt_space == NULL) {
2626 /* If the gtt is empty and we're still having trouble
2627 * fitting our object in, we're out of memory.
2628 */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002629 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002630 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002631 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002632
Eric Anholt673a3942008-07-30 12:06:12 -07002633 goto search_free;
2634 }
2635
Chris Wilson4bdadb92010-01-27 13:36:32 +00002636 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002637 if (ret) {
2638 drm_mm_put_block(obj_priv->gtt_space);
2639 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002640
2641 if (ret == -ENOMEM) {
2642 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002643 ret = i915_gem_evict_something(dev, obj->size,
2644 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002645 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002646 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002647 if (gfpmask) {
2648 gfpmask = 0;
2649 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002650 }
2651
2652 return ret;
2653 }
2654
2655 goto search_free;
2656 }
2657
Eric Anholt673a3942008-07-30 12:06:12 -07002658 return ret;
2659 }
2660
Eric Anholt673a3942008-07-30 12:06:12 -07002661 /* Create an AGP memory structure pointing at our pages, and bind it
2662 * into the GTT.
2663 */
2664 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002665 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002666 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002667 obj_priv->gtt_offset,
2668 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002669 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002670 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002671 drm_mm_put_block(obj_priv->gtt_space);
2672 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002673
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002674 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002675 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002676 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002677
2678 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002679 }
2680 atomic_inc(&dev->gtt_count);
2681 atomic_add(obj->size, &dev->gtt_memory);
2682
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002683 /* keep track of bounds object by adding it to the inactive list */
2684 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2685
Eric Anholt673a3942008-07-30 12:06:12 -07002686 /* Assert that the object is not currently in any GPU domain. As it
2687 * wasn't in the GTT, there shouldn't be any way it could have been in
2688 * a GPU cache
2689 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002690 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2691 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002692
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002693 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2694
Eric Anholt673a3942008-07-30 12:06:12 -07002695 return 0;
2696}
2697
2698void
2699i915_gem_clflush_object(struct drm_gem_object *obj)
2700{
Daniel Vetter23010e42010-03-08 13:35:02 +01002701 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002702
2703 /* If we don't have a page list set up, then we're not pinned
2704 * to GPU, and we can ignore the cache flush because it'll happen
2705 * again at bind time.
2706 */
Eric Anholt856fa192009-03-19 14:10:50 -07002707 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002708 return;
2709
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002710 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002711
Eric Anholt856fa192009-03-19 14:10:50 -07002712 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002713}
2714
Eric Anholte47c68e2008-11-14 13:35:19 -08002715/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002716static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002717i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2718 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002719{
2720 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002721 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002722
2723 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002724 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002725
2726 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002727 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002728 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002729 to_intel_bo(obj)->ring,
2730 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002731 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002732
2733 trace_i915_gem_object_change_domain(obj,
2734 obj->read_domains,
2735 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002736
2737 if (pipelined)
2738 return 0;
2739
Chris Wilson2cf34d72010-09-14 13:03:28 +01002740 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002741}
2742
2743/** Flushes the GTT write domain for the object if it's dirty. */
2744static void
2745i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2746{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002747 uint32_t old_write_domain;
2748
Eric Anholte47c68e2008-11-14 13:35:19 -08002749 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2750 return;
2751
2752 /* No actual flushing is required for the GTT write domain. Writes
2753 * to it immediately go to main memory as far as we know, so there's
2754 * no chipset flush. It also doesn't land in render cache.
2755 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002756 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002757 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002758
2759 trace_i915_gem_object_change_domain(obj,
2760 obj->read_domains,
2761 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002762}
2763
2764/** Flushes the CPU write domain for the object if it's dirty. */
2765static void
2766i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2767{
2768 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002769 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002770
2771 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2772 return;
2773
2774 i915_gem_clflush_object(obj);
2775 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002776 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002777 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002778
2779 trace_i915_gem_object_change_domain(obj,
2780 obj->read_domains,
2781 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002782}
2783
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002784/**
2785 * Moves a single object to the GTT read, and possibly write domain.
2786 *
2787 * This function returns when the move is complete, including waiting on
2788 * flushes to occur.
2789 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002790int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002791i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2792{
Daniel Vetter23010e42010-03-08 13:35:02 +01002793 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002794 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002795 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002796
Eric Anholt02354392008-11-26 13:58:13 -08002797 /* Not valid to be called on unbound objects. */
2798 if (obj_priv->gtt_space == NULL)
2799 return -EINVAL;
2800
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002801 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002802 if (ret != 0)
2803 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002804
Chris Wilson72133422010-09-13 23:56:38 +01002805 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002806
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002807 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002808 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002809 if (ret)
2810 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002811 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002812
Chris Wilson72133422010-09-13 23:56:38 +01002813 old_write_domain = obj->write_domain;
2814 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002815
2816 /* It should now be out of any other write domains, and we can update
2817 * the domain values for our changes.
2818 */
2819 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2820 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002821 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002822 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002823 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002824 obj_priv->dirty = 1;
2825 }
2826
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002827 trace_i915_gem_object_change_domain(obj,
2828 old_read_domains,
2829 old_write_domain);
2830
Eric Anholte47c68e2008-11-14 13:35:19 -08002831 return 0;
2832}
2833
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002834/*
2835 * Prepare buffer for display plane. Use uninterruptible for possible flush
2836 * wait, as in modesetting process we're not supposed to be interrupted.
2837 */
2838int
Chris Wilson48b956c2010-09-14 12:50:34 +01002839i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2840 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002841{
Daniel Vetter23010e42010-03-08 13:35:02 +01002842 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002843 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002844 int ret;
2845
2846 /* Not valid to be called on unbound objects. */
2847 if (obj_priv->gtt_space == NULL)
2848 return -EINVAL;
2849
Chris Wilsonced270f2010-09-26 22:47:46 +01002850 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson48b956c2010-09-14 12:50:34 +01002851 if (ret)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002852 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002853
Chris Wilsonced270f2010-09-26 22:47:46 +01002854 /* Currently, we are always called from an non-interruptible context. */
2855 if (!pipelined) {
2856 ret = i915_gem_object_wait_rendering(obj, false);
2857 if (ret)
2858 return ret;
2859 }
2860
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002861 i915_gem_object_flush_cpu_write_domain(obj);
2862
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002863 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002864 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002865
2866 trace_i915_gem_object_change_domain(obj,
2867 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002868 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002869
2870 return 0;
2871}
2872
Eric Anholte47c68e2008-11-14 13:35:19 -08002873/**
2874 * Moves a single object to the CPU read, and possibly write domain.
2875 *
2876 * This function returns when the move is complete, including waiting on
2877 * flushes to occur.
2878 */
2879static int
2880i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2881{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002882 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002883 int ret;
2884
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002885 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002886 if (ret != 0)
2887 return ret;
2888
2889 i915_gem_object_flush_gtt_write_domain(obj);
2890
2891 /* If we have a partially-valid cache of the object in the CPU,
2892 * finish invalidating it and free the per-page flags.
2893 */
2894 i915_gem_object_set_to_full_cpu_read_domain(obj);
2895
Chris Wilson72133422010-09-13 23:56:38 +01002896 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002897 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002898 if (ret)
2899 return ret;
2900 }
2901
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002902 old_write_domain = obj->write_domain;
2903 old_read_domains = obj->read_domains;
2904
Eric Anholte47c68e2008-11-14 13:35:19 -08002905 /* Flush the CPU cache if it's still invalid. */
2906 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2907 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002908
2909 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2910 }
2911
2912 /* It should now be out of any other write domains, and we can update
2913 * the domain values for our changes.
2914 */
2915 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2916
2917 /* If we're writing through the CPU, then the GPU read domains will
2918 * need to be invalidated at next use.
2919 */
2920 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002921 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002922 obj->write_domain = I915_GEM_DOMAIN_CPU;
2923 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002924
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002925 trace_i915_gem_object_change_domain(obj,
2926 old_read_domains,
2927 old_write_domain);
2928
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002929 return 0;
2930}
2931
Eric Anholt673a3942008-07-30 12:06:12 -07002932/*
2933 * Set the next domain for the specified object. This
2934 * may not actually perform the necessary flushing/invaliding though,
2935 * as that may want to be batched with other set_domain operations
2936 *
2937 * This is (we hope) the only really tricky part of gem. The goal
2938 * is fairly simple -- track which caches hold bits of the object
2939 * and make sure they remain coherent. A few concrete examples may
2940 * help to explain how it works. For shorthand, we use the notation
2941 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2942 * a pair of read and write domain masks.
2943 *
2944 * Case 1: the batch buffer
2945 *
2946 * 1. Allocated
2947 * 2. Written by CPU
2948 * 3. Mapped to GTT
2949 * 4. Read by GPU
2950 * 5. Unmapped from GTT
2951 * 6. Freed
2952 *
2953 * Let's take these a step at a time
2954 *
2955 * 1. Allocated
2956 * Pages allocated from the kernel may still have
2957 * cache contents, so we set them to (CPU, CPU) always.
2958 * 2. Written by CPU (using pwrite)
2959 * The pwrite function calls set_domain (CPU, CPU) and
2960 * this function does nothing (as nothing changes)
2961 * 3. Mapped by GTT
2962 * This function asserts that the object is not
2963 * currently in any GPU-based read or write domains
2964 * 4. Read by GPU
2965 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2966 * As write_domain is zero, this function adds in the
2967 * current read domains (CPU+COMMAND, 0).
2968 * flush_domains is set to CPU.
2969 * invalidate_domains is set to COMMAND
2970 * clflush is run to get data out of the CPU caches
2971 * then i915_dev_set_domain calls i915_gem_flush to
2972 * emit an MI_FLUSH and drm_agp_chipset_flush
2973 * 5. Unmapped from GTT
2974 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2975 * flush_domains and invalidate_domains end up both zero
2976 * so no flushing/invalidating happens
2977 * 6. Freed
2978 * yay, done
2979 *
2980 * Case 2: The shared render buffer
2981 *
2982 * 1. Allocated
2983 * 2. Mapped to GTT
2984 * 3. Read/written by GPU
2985 * 4. set_domain to (CPU,CPU)
2986 * 5. Read/written by CPU
2987 * 6. Read/written by GPU
2988 *
2989 * 1. Allocated
2990 * Same as last example, (CPU, CPU)
2991 * 2. Mapped to GTT
2992 * Nothing changes (assertions find that it is not in the GPU)
2993 * 3. Read/written by GPU
2994 * execbuffer calls set_domain (RENDER, RENDER)
2995 * flush_domains gets CPU
2996 * invalidate_domains gets GPU
2997 * clflush (obj)
2998 * MI_FLUSH and drm_agp_chipset_flush
2999 * 4. set_domain (CPU, CPU)
3000 * flush_domains gets GPU
3001 * invalidate_domains gets CPU
3002 * wait_rendering (obj) to make sure all drawing is complete.
3003 * This will include an MI_FLUSH to get the data from GPU
3004 * to memory
3005 * clflush (obj) to invalidate the CPU cache
3006 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3007 * 5. Read/written by CPU
3008 * cache lines are loaded and dirtied
3009 * 6. Read written by GPU
3010 * Same as last GPU access
3011 *
3012 * Case 3: The constant buffer
3013 *
3014 * 1. Allocated
3015 * 2. Written by CPU
3016 * 3. Read by GPU
3017 * 4. Updated (written) by CPU again
3018 * 5. Read by GPU
3019 *
3020 * 1. Allocated
3021 * (CPU, CPU)
3022 * 2. Written by CPU
3023 * (CPU, CPU)
3024 * 3. Read by GPU
3025 * (CPU+RENDER, 0)
3026 * flush_domains = CPU
3027 * invalidate_domains = RENDER
3028 * clflush (obj)
3029 * MI_FLUSH
3030 * drm_agp_chipset_flush
3031 * 4. Updated (written) by CPU again
3032 * (CPU, CPU)
3033 * flush_domains = 0 (no previous write domain)
3034 * invalidate_domains = 0 (no new read domains)
3035 * 5. Read by GPU
3036 * (CPU+RENDER, 0)
3037 * flush_domains = CPU
3038 * invalidate_domains = RENDER
3039 * clflush (obj)
3040 * MI_FLUSH
3041 * drm_agp_chipset_flush
3042 */
Keith Packardc0d90822008-11-20 23:11:08 -08003043static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003044i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003045{
3046 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003047 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003048 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003049 uint32_t invalidate_domains = 0;
3050 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003051 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003052
Eric Anholt8b0e3782009-02-19 14:40:50 -08003053 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3054 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003055
Jesse Barnes652c3932009-08-17 13:31:43 -07003056 intel_mark_busy(dev, obj);
3057
Eric Anholt673a3942008-07-30 12:06:12 -07003058 /*
3059 * If the object isn't moving to a new write domain,
3060 * let the object stay in multiple read domains
3061 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003062 if (obj->pending_write_domain == 0)
3063 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003064 else
3065 obj_priv->dirty = 1;
3066
3067 /*
3068 * Flush the current write domain if
3069 * the new read domains don't match. Invalidate
3070 * any read domains which differ from the old
3071 * write domain
3072 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003073 if (obj->write_domain &&
3074 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003075 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003076 invalidate_domains |=
3077 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003078 }
3079 /*
3080 * Invalidate any read caches which may have
3081 * stale data. That is, any new read domains.
3082 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003083 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003084 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003085 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003086
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003087 old_read_domains = obj->read_domains;
3088
Eric Anholtefbeed92009-02-19 14:54:51 -08003089 /* The actual obj->write_domain will be updated with
3090 * pending_write_domain after we emit the accumulated flush for all
3091 * of our domain changes in execbuffers (which clears objects'
3092 * write_domains). So if we have a current write domain that we
3093 * aren't changing, set pending_write_domain to that.
3094 */
3095 if (flush_domains == 0 && obj->pending_write_domain == 0)
3096 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003097 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003098
3099 dev->invalidate_domains |= invalidate_domains;
3100 dev->flush_domains |= flush_domains;
Chris Wilson92204342010-09-18 11:02:01 +01003101 if (obj_priv->ring)
3102 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003103
3104 trace_i915_gem_object_change_domain(obj,
3105 old_read_domains,
3106 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003107}
3108
3109/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003110 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003111 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003112 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3113 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3114 */
3115static void
3116i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3117{
Daniel Vetter23010e42010-03-08 13:35:02 +01003118 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003119
3120 if (!obj_priv->page_cpu_valid)
3121 return;
3122
3123 /* If we're partially in the CPU read domain, finish moving it in.
3124 */
3125 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3126 int i;
3127
3128 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3129 if (obj_priv->page_cpu_valid[i])
3130 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003131 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003132 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003133 }
3134
3135 /* Free the page_cpu_valid mappings which are now stale, whether
3136 * or not we've got I915_GEM_DOMAIN_CPU.
3137 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003138 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003139 obj_priv->page_cpu_valid = NULL;
3140}
3141
3142/**
3143 * Set the CPU read domain on a range of the object.
3144 *
3145 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3146 * not entirely valid. The page_cpu_valid member of the object flags which
3147 * pages have been flushed, and will be respected by
3148 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3149 * of the whole object.
3150 *
3151 * This function returns when the move is complete, including waiting on
3152 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003153 */
3154static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003155i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3156 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003157{
Daniel Vetter23010e42010-03-08 13:35:02 +01003158 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003159 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003160 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003161
Eric Anholte47c68e2008-11-14 13:35:19 -08003162 if (offset == 0 && size == obj->size)
3163 return i915_gem_object_set_to_cpu_domain(obj, 0);
3164
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003165 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003166 if (ret != 0)
3167 return ret;
3168 i915_gem_object_flush_gtt_write_domain(obj);
3169
3170 /* If we're already fully in the CPU read domain, we're done. */
3171 if (obj_priv->page_cpu_valid == NULL &&
3172 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003173 return 0;
3174
Eric Anholte47c68e2008-11-14 13:35:19 -08003175 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3176 * newly adding I915_GEM_DOMAIN_CPU
3177 */
Eric Anholt673a3942008-07-30 12:06:12 -07003178 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003179 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3180 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003181 if (obj_priv->page_cpu_valid == NULL)
3182 return -ENOMEM;
3183 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3184 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003185
3186 /* Flush the cache on any pages that are still invalid from the CPU's
3187 * perspective.
3188 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003189 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3190 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003191 if (obj_priv->page_cpu_valid[i])
3192 continue;
3193
Eric Anholt856fa192009-03-19 14:10:50 -07003194 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003195
3196 obj_priv->page_cpu_valid[i] = 1;
3197 }
3198
Eric Anholte47c68e2008-11-14 13:35:19 -08003199 /* It should now be out of any other write domains, and we can update
3200 * the domain values for our changes.
3201 */
3202 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3203
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003204 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003205 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3206
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003207 trace_i915_gem_object_change_domain(obj,
3208 old_read_domains,
3209 obj->write_domain);
3210
Eric Anholt673a3942008-07-30 12:06:12 -07003211 return 0;
3212}
3213
3214/**
Eric Anholt673a3942008-07-30 12:06:12 -07003215 * Pin an object to the GTT and evaluate the relocations landing in it.
3216 */
3217static int
3218i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3219 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003220 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003221 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003222{
3223 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003224 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003225 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003226 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003227 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003228 bool need_fence;
3229
3230 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3231 obj_priv->tiling_mode != I915_TILING_NONE;
3232
3233 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003234 if (need_fence &&
3235 !i915_gem_object_fence_offset_ok(obj,
3236 obj_priv->tiling_mode)) {
3237 ret = i915_gem_object_unbind(obj);
3238 if (ret)
3239 return ret;
3240 }
Eric Anholt673a3942008-07-30 12:06:12 -07003241
3242 /* Choose the GTT offset for our buffer and put it there. */
3243 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3244 if (ret)
3245 return ret;
3246
Jesse Barnes76446ca2009-12-17 22:05:42 -05003247 /*
3248 * Pre-965 chips need a fence register set up in order to
3249 * properly handle blits to/from tiled surfaces.
3250 */
3251 if (need_fence) {
Chris Wilson53640e12010-09-20 11:40:50 +01003252 ret = i915_gem_object_get_fence_reg(obj, true);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003253 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003254 i915_gem_object_unpin(obj);
3255 return ret;
3256 }
Chris Wilson53640e12010-09-20 11:40:50 +01003257
3258 dev_priv->fence_regs[obj_priv->fence_reg].gpu = true;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003259 }
3260
Eric Anholt673a3942008-07-30 12:06:12 -07003261 entry->offset = obj_priv->gtt_offset;
3262
Eric Anholt673a3942008-07-30 12:06:12 -07003263 /* Apply the relocations, using the GTT aperture to avoid cache
3264 * flushing requirements.
3265 */
3266 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003267 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003268 struct drm_gem_object *target_obj;
3269 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003270 uint32_t reloc_val, reloc_offset;
3271 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003272
Eric Anholt673a3942008-07-30 12:06:12 -07003273 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003274 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003275 if (target_obj == NULL) {
3276 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003277 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003278 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003279 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003280
Chris Wilson8542a0b2009-09-09 21:15:15 +01003281#if WATCH_RELOC
3282 DRM_INFO("%s: obj %p offset %08x target %d "
3283 "read %08x write %08x gtt %08x "
3284 "presumed %08x delta %08x\n",
3285 __func__,
3286 obj,
3287 (int) reloc->offset,
3288 (int) reloc->target_handle,
3289 (int) reloc->read_domains,
3290 (int) reloc->write_domain,
3291 (int) target_obj_priv->gtt_offset,
3292 (int) reloc->presumed_offset,
3293 reloc->delta);
3294#endif
3295
Eric Anholt673a3942008-07-30 12:06:12 -07003296 /* The target buffer should have appeared before us in the
3297 * exec_object list, so it should have a GTT space bound by now.
3298 */
3299 if (target_obj_priv->gtt_space == NULL) {
3300 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003301 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003302 drm_gem_object_unreference(target_obj);
3303 i915_gem_object_unpin(obj);
3304 return -EINVAL;
3305 }
3306
Chris Wilson8542a0b2009-09-09 21:15:15 +01003307 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003308 if (reloc->write_domain & (reloc->write_domain - 1)) {
3309 DRM_ERROR("reloc with multiple write domains: "
3310 "obj %p target %d offset %d "
3311 "read %08x write %08x",
3312 obj, reloc->target_handle,
3313 (int) reloc->offset,
3314 reloc->read_domains,
3315 reloc->write_domain);
3316 return -EINVAL;
3317 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003318 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3319 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3320 DRM_ERROR("reloc with read/write CPU domains: "
3321 "obj %p target %d offset %d "
3322 "read %08x write %08x",
3323 obj, reloc->target_handle,
3324 (int) reloc->offset,
3325 reloc->read_domains,
3326 reloc->write_domain);
3327 drm_gem_object_unreference(target_obj);
3328 i915_gem_object_unpin(obj);
3329 return -EINVAL;
3330 }
3331 if (reloc->write_domain && target_obj->pending_write_domain &&
3332 reloc->write_domain != target_obj->pending_write_domain) {
3333 DRM_ERROR("Write domain conflict: "
3334 "obj %p target %d offset %d "
3335 "new %08x old %08x\n",
3336 obj, reloc->target_handle,
3337 (int) reloc->offset,
3338 reloc->write_domain,
3339 target_obj->pending_write_domain);
3340 drm_gem_object_unreference(target_obj);
3341 i915_gem_object_unpin(obj);
3342 return -EINVAL;
3343 }
3344
3345 target_obj->pending_read_domains |= reloc->read_domains;
3346 target_obj->pending_write_domain |= reloc->write_domain;
3347
3348 /* If the relocation already has the right value in it, no
3349 * more work needs to be done.
3350 */
3351 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3352 drm_gem_object_unreference(target_obj);
3353 continue;
3354 }
3355
3356 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003357 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003358 DRM_ERROR("Relocation beyond object bounds: "
3359 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003360 obj, reloc->target_handle,
3361 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003362 drm_gem_object_unreference(target_obj);
3363 i915_gem_object_unpin(obj);
3364 return -EINVAL;
3365 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003366 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003367 DRM_ERROR("Relocation not 4-byte aligned: "
3368 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003369 obj, reloc->target_handle,
3370 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003371 drm_gem_object_unreference(target_obj);
3372 i915_gem_object_unpin(obj);
3373 return -EINVAL;
3374 }
3375
Chris Wilson8542a0b2009-09-09 21:15:15 +01003376 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003377 if (reloc->delta >= target_obj->size) {
3378 DRM_ERROR("Relocation beyond target object bounds: "
3379 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003380 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003381 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003382 drm_gem_object_unreference(target_obj);
3383 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003384 return -EINVAL;
3385 }
3386
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003387 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3388 if (ret != 0) {
3389 drm_gem_object_unreference(target_obj);
3390 i915_gem_object_unpin(obj);
3391 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003392 }
3393
3394 /* Map the page containing the relocation we're going to
3395 * perform.
3396 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003397 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003398 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3399 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003400 ~(PAGE_SIZE - 1)),
3401 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003402 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003403 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003404 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003405
Eric Anholt673a3942008-07-30 12:06:12 -07003406 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003407 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003408
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003409 /* The updated presumed offset for this entry will be
3410 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003411 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003412 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003413
3414 drm_gem_object_unreference(target_obj);
3415 }
3416
Eric Anholt673a3942008-07-30 12:06:12 -07003417 return 0;
3418}
3419
Eric Anholt673a3942008-07-30 12:06:12 -07003420/* Throttle our rendering by waiting until the ring has completed our requests
3421 * emitted over 20 msec ago.
3422 *
Eric Anholtb9624422009-06-03 07:27:35 +00003423 * Note that if we were to use the current jiffies each time around the loop,
3424 * we wouldn't escape the function with any frames outstanding if the time to
3425 * render a frame was over 20ms.
3426 *
Eric Anholt673a3942008-07-30 12:06:12 -07003427 * This should get us reasonable parallelism between CPU and GPU but also
3428 * relatively low latency when blocking on a particular request to finish.
3429 */
3430static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003431i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003432{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003433 struct drm_i915_private *dev_priv = dev->dev_private;
3434 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003435 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003436 struct drm_i915_gem_request *request;
3437 struct intel_ring_buffer *ring = NULL;
3438 u32 seqno = 0;
3439 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003440
Chris Wilson1c255952010-09-26 11:03:27 +01003441 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003442 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003443 if (time_after_eq(request->emitted_jiffies, recent_enough))
3444 break;
3445
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003446 ring = request->ring;
3447 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003448 }
Chris Wilson1c255952010-09-26 11:03:27 +01003449 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003450
3451 if (seqno == 0)
3452 return 0;
3453
3454 ret = 0;
3455 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3456 /* And wait for the seqno passing without holding any locks and
3457 * causing extra latency for others. This is safe as the irq
3458 * generation is designed to be run atomically and so is
3459 * lockless.
3460 */
3461 ring->user_irq_get(dev, ring);
3462 ret = wait_event_interruptible(ring->irq_queue,
3463 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3464 || atomic_read(&dev_priv->mm.wedged));
3465 ring->user_irq_put(dev, ring);
3466
3467 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3468 ret = -EIO;
3469 }
3470
3471 if (ret == 0)
3472 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003473
Eric Anholt673a3942008-07-30 12:06:12 -07003474 return ret;
3475}
3476
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003477static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003478i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003479 uint32_t buffer_count,
3480 struct drm_i915_gem_relocation_entry **relocs)
3481{
3482 uint32_t reloc_count = 0, reloc_index = 0, i;
3483 int ret;
3484
3485 *relocs = NULL;
3486 for (i = 0; i < buffer_count; i++) {
3487 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3488 return -EINVAL;
3489 reloc_count += exec_list[i].relocation_count;
3490 }
3491
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003492 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003493 if (*relocs == NULL) {
3494 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003495 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003496 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003497
3498 for (i = 0; i < buffer_count; i++) {
3499 struct drm_i915_gem_relocation_entry __user *user_relocs;
3500
3501 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3502
3503 ret = copy_from_user(&(*relocs)[reloc_index],
3504 user_relocs,
3505 exec_list[i].relocation_count *
3506 sizeof(**relocs));
3507 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003508 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003509 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003510 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003511 }
3512
3513 reloc_index += exec_list[i].relocation_count;
3514 }
3515
Florian Mickler2bc43b52009-04-06 22:55:41 +02003516 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003517}
3518
3519static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003520i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003521 uint32_t buffer_count,
3522 struct drm_i915_gem_relocation_entry *relocs)
3523{
3524 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003525 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003526
Chris Wilson93533c22010-01-31 10:40:48 +00003527 if (relocs == NULL)
3528 return 0;
3529
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003530 for (i = 0; i < buffer_count; i++) {
3531 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003532 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003533
3534 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3535
Florian Mickler2bc43b52009-04-06 22:55:41 +02003536 unwritten = copy_to_user(user_relocs,
3537 &relocs[reloc_count],
3538 exec_list[i].relocation_count *
3539 sizeof(*relocs));
3540
3541 if (unwritten) {
3542 ret = -EFAULT;
3543 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003544 }
3545
3546 reloc_count += exec_list[i].relocation_count;
3547 }
3548
Florian Mickler2bc43b52009-04-06 22:55:41 +02003549err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003550 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003551
3552 return ret;
3553}
3554
Chris Wilson83d60792009-06-06 09:45:57 +01003555static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003556i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003557 uint64_t exec_offset)
3558{
3559 uint32_t exec_start, exec_len;
3560
3561 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3562 exec_len = (uint32_t) exec->batch_len;
3563
3564 if ((exec_start | exec_len) & 0x7)
3565 return -EINVAL;
3566
3567 if (!exec_start)
3568 return -EINVAL;
3569
3570 return 0;
3571}
3572
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003573static int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003574i915_gem_wait_for_pending_flip(struct drm_device *dev,
3575 struct drm_gem_object **object_list,
3576 int count)
3577{
3578 drm_i915_private_t *dev_priv = dev->dev_private;
3579 struct drm_i915_gem_object *obj_priv;
3580 DEFINE_WAIT(wait);
3581 int i, ret = 0;
3582
3583 for (;;) {
3584 prepare_to_wait(&dev_priv->pending_flip_queue,
3585 &wait, TASK_INTERRUPTIBLE);
3586 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003587 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003588 if (atomic_read(&obj_priv->pending_flip) > 0)
3589 break;
3590 }
3591 if (i == count)
3592 break;
3593
3594 if (!signal_pending(current)) {
3595 mutex_unlock(&dev->struct_mutex);
3596 schedule();
3597 mutex_lock(&dev->struct_mutex);
3598 continue;
3599 }
3600 ret = -ERESTARTSYS;
3601 break;
3602 }
3603 finish_wait(&dev_priv->pending_flip_queue, &wait);
3604
3605 return ret;
3606}
3607
Chris Wilson8dc5d142010-08-12 12:36:12 +01003608static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003609i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3610 struct drm_file *file_priv,
3611 struct drm_i915_gem_execbuffer2 *args,
3612 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003613{
3614 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003615 struct drm_gem_object **object_list = NULL;
3616 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003617 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003618 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003619 struct drm_i915_gem_relocation_entry *relocs = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003620 struct drm_i915_gem_request *request = NULL;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003621 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003622 uint64_t exec_offset;
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003623 uint32_t reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003624 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003625
Zou Nan hai852835f2010-05-21 09:08:56 +08003626 struct intel_ring_buffer *ring = NULL;
3627
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003628 ret = i915_gem_check_is_wedged(dev);
3629 if (ret)
3630 return ret;
3631
Eric Anholt673a3942008-07-30 12:06:12 -07003632#if WATCH_EXEC
3633 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3634 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3635#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003636 if (args->flags & I915_EXEC_BSD) {
3637 if (!HAS_BSD(dev)) {
3638 DRM_ERROR("execbuf with wrong flag\n");
3639 return -EINVAL;
3640 }
3641 ring = &dev_priv->bsd_ring;
3642 } else {
3643 ring = &dev_priv->render_ring;
3644 }
3645
Eric Anholt4f481ed2008-09-10 14:22:49 -07003646 if (args->buffer_count < 1) {
3647 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3648 return -EINVAL;
3649 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003650 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003651 if (object_list == NULL) {
3652 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003653 args->buffer_count);
3654 ret = -ENOMEM;
3655 goto pre_mutex_err;
3656 }
Eric Anholt673a3942008-07-30 12:06:12 -07003657
Eric Anholt201361a2009-03-11 12:30:04 -07003658 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003659 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3660 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003661 if (cliprects == NULL) {
3662 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003663 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003664 }
Eric Anholt201361a2009-03-11 12:30:04 -07003665
3666 ret = copy_from_user(cliprects,
3667 (struct drm_clip_rect __user *)
3668 (uintptr_t) args->cliprects_ptr,
3669 sizeof(*cliprects) * args->num_cliprects);
3670 if (ret != 0) {
3671 DRM_ERROR("copy %d cliprects failed: %d\n",
3672 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003673 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003674 goto pre_mutex_err;
3675 }
3676 }
3677
Chris Wilson8dc5d142010-08-12 12:36:12 +01003678 request = kzalloc(sizeof(*request), GFP_KERNEL);
3679 if (request == NULL) {
3680 ret = -ENOMEM;
3681 goto pre_mutex_err;
3682 }
3683
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003684 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3685 &relocs);
3686 if (ret != 0)
3687 goto pre_mutex_err;
3688
Chris Wilson76c1dec2010-09-25 11:22:51 +01003689 ret = i915_mutex_lock_interruptible(dev);
3690 if (ret)
3691 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003692
3693 i915_verify_inactive(dev, __FILE__, __LINE__);
3694
Eric Anholt673a3942008-07-30 12:06:12 -07003695 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003696 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003697 ret = -EBUSY;
3698 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003699 }
3700
Keith Packardac94a962008-11-20 23:30:27 -08003701 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003702 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003703 for (i = 0; i < args->buffer_count; i++) {
3704 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3705 exec_list[i].handle);
3706 if (object_list[i] == NULL) {
3707 DRM_ERROR("Invalid object handle %d at index %d\n",
3708 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003709 /* prevent error path from reading uninitialized data */
3710 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003711 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003712 goto err;
3713 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003714
Daniel Vetter23010e42010-03-08 13:35:02 +01003715 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003716 if (obj_priv->in_execbuffer) {
3717 DRM_ERROR("Object %p appears more than once in object list\n",
3718 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003719 /* prevent error path from reading uninitialized data */
3720 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003721 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003722 goto err;
3723 }
3724 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003725 flips += atomic_read(&obj_priv->pending_flip);
3726 }
3727
3728 if (flips > 0) {
3729 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3730 args->buffer_count);
3731 if (ret)
3732 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003733 }
Eric Anholt673a3942008-07-30 12:06:12 -07003734
Keith Packardac94a962008-11-20 23:30:27 -08003735 /* Pin and relocate */
3736 for (pin_tries = 0; ; pin_tries++) {
3737 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003738 reloc_index = 0;
3739
Keith Packardac94a962008-11-20 23:30:27 -08003740 for (i = 0; i < args->buffer_count; i++) {
3741 object_list[i]->pending_read_domains = 0;
3742 object_list[i]->pending_write_domain = 0;
3743 ret = i915_gem_object_pin_and_relocate(object_list[i],
3744 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003745 &exec_list[i],
3746 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003747 if (ret)
3748 break;
3749 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003750 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003751 }
3752 /* success */
3753 if (ret == 0)
3754 break;
3755
3756 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003757 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003758 if (ret != -ERESTARTSYS) {
3759 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003760 int num_fences = 0;
3761 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003762 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003763
Chris Wilson07f73f62009-09-14 16:50:30 +01003764 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003765 num_fences +=
3766 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3767 obj_priv->tiling_mode != I915_TILING_NONE;
3768 }
3769 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003770 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003771 total_size, num_fences,
3772 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003773 DRM_ERROR("%d objects [%d pinned], "
3774 "%d object bytes [%d pinned], "
3775 "%d/%d gtt bytes\n",
3776 atomic_read(&dev->object_count),
3777 atomic_read(&dev->pin_count),
3778 atomic_read(&dev->object_memory),
3779 atomic_read(&dev->pin_memory),
3780 atomic_read(&dev->gtt_memory),
3781 dev->gtt_total);
3782 }
Eric Anholt673a3942008-07-30 12:06:12 -07003783 goto err;
3784 }
Keith Packardac94a962008-11-20 23:30:27 -08003785
3786 /* unpin all of our buffers */
3787 for (i = 0; i < pinned; i++)
3788 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003789 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003790
3791 /* evict everyone we can from the aperture */
3792 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003793 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003794 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003795 }
3796
3797 /* Set the pending read domains for the batch buffer to COMMAND */
3798 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003799 if (batch_obj->pending_write_domain) {
3800 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3801 ret = -EINVAL;
3802 goto err;
3803 }
3804 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003805
Chris Wilson83d60792009-06-06 09:45:57 +01003806 /* Sanity check the batch buffer, prior to moving objects */
3807 exec_offset = exec_list[args->buffer_count - 1].offset;
3808 ret = i915_gem_check_execbuffer (args, exec_offset);
3809 if (ret != 0) {
3810 DRM_ERROR("execbuf with invalid offset/length\n");
3811 goto err;
3812 }
3813
Eric Anholt673a3942008-07-30 12:06:12 -07003814 i915_verify_inactive(dev, __FILE__, __LINE__);
3815
Keith Packard646f0f62008-11-20 23:23:03 -08003816 /* Zero the global flush/invalidate flags. These
3817 * will be modified as new domains are computed
3818 * for each object
3819 */
3820 dev->invalidate_domains = 0;
3821 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003822 dev_priv->mm.flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003823
Eric Anholt673a3942008-07-30 12:06:12 -07003824 for (i = 0; i < args->buffer_count; i++) {
3825 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003826
Keith Packard646f0f62008-11-20 23:23:03 -08003827 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003828 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003829 }
3830
3831 i915_verify_inactive(dev, __FILE__, __LINE__);
3832
Keith Packard646f0f62008-11-20 23:23:03 -08003833 if (dev->invalidate_domains | dev->flush_domains) {
3834#if WATCH_EXEC
3835 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3836 __func__,
3837 dev->invalidate_domains,
3838 dev->flush_domains);
3839#endif
Chris Wilsonc78ec302010-09-20 12:50:23 +01003840 i915_gem_flush(dev, file_priv,
Keith Packard646f0f62008-11-20 23:23:03 -08003841 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003842 dev->flush_domains,
3843 dev_priv->mm.flush_rings);
Daniel Vettera6910432010-02-02 17:08:37 +01003844 }
3845
Eric Anholtefbeed92009-02-19 14:54:51 -08003846 for (i = 0; i < args->buffer_count; i++) {
3847 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003848 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003849 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003850
3851 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003852 if (obj->write_domain)
3853 list_move_tail(&obj_priv->gpu_write_list,
3854 &dev_priv->mm.gpu_write_list);
3855 else
3856 list_del_init(&obj_priv->gpu_write_list);
3857
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003858 trace_i915_gem_object_change_domain(obj,
3859 obj->read_domains,
3860 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003861 }
3862
Eric Anholt673a3942008-07-30 12:06:12 -07003863 i915_verify_inactive(dev, __FILE__, __LINE__);
3864
3865#if WATCH_COHERENCY
3866 for (i = 0; i < args->buffer_count; i++) {
3867 i915_gem_object_check_coherency(object_list[i],
3868 exec_list[i].handle);
3869 }
3870#endif
3871
Eric Anholt673a3942008-07-30 12:06:12 -07003872#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003873 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003874 args->batch_len,
3875 __func__,
3876 ~0);
3877#endif
3878
Eric Anholt673a3942008-07-30 12:06:12 -07003879 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003880 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3881 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003882 if (ret) {
3883 DRM_ERROR("dispatch failed %d\n", ret);
3884 goto err;
3885 }
3886
3887 /*
3888 * Ensure that the commands in the batch buffer are
3889 * finished before the interrupt fires
3890 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003891 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003892
3893 i915_verify_inactive(dev, __FILE__, __LINE__);
3894
Daniel Vetter617dbe22010-02-11 22:16:02 +01003895 for (i = 0; i < args->buffer_count; i++) {
3896 struct drm_gem_object *obj = object_list[i];
3897 obj_priv = to_intel_bo(obj);
3898
3899 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01003900 }
Chris Wilsona56ba562010-09-28 10:07:56 +01003901
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003902 i915_add_request(dev, file_priv, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003903 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003904
Eric Anholt673a3942008-07-30 12:06:12 -07003905 i915_verify_inactive(dev, __FILE__, __LINE__);
3906
Eric Anholt673a3942008-07-30 12:06:12 -07003907err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003908 for (i = 0; i < pinned; i++)
3909 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003910
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003911 for (i = 0; i < args->buffer_count; i++) {
3912 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003913 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003914 obj_priv->in_execbuffer = false;
3915 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003916 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003917 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003918
Eric Anholt673a3942008-07-30 12:06:12 -07003919 mutex_unlock(&dev->struct_mutex);
3920
Chris Wilson93533c22010-01-31 10:40:48 +00003921pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003922 /* Copy the updated relocations out regardless of current error
3923 * state. Failure to update the relocs would mean that the next
3924 * time userland calls execbuf, it would do so with presumed offset
3925 * state that didn't match the actual object state.
3926 */
3927 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3928 relocs);
3929 if (ret2 != 0) {
3930 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3931
3932 if (ret == 0)
3933 ret = ret2;
3934 }
3935
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003936 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003937 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003938 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003939
3940 return ret;
3941}
3942
Jesse Barnes76446ca2009-12-17 22:05:42 -05003943/*
3944 * Legacy execbuffer just creates an exec2 list from the original exec object
3945 * list array and passes it to the real function.
3946 */
3947int
3948i915_gem_execbuffer(struct drm_device *dev, void *data,
3949 struct drm_file *file_priv)
3950{
3951 struct drm_i915_gem_execbuffer *args = data;
3952 struct drm_i915_gem_execbuffer2 exec2;
3953 struct drm_i915_gem_exec_object *exec_list = NULL;
3954 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3955 int ret, i;
3956
3957#if WATCH_EXEC
3958 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3959 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3960#endif
3961
3962 if (args->buffer_count < 1) {
3963 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3964 return -EINVAL;
3965 }
3966
3967 /* Copy in the exec list from userland */
3968 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3969 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3970 if (exec_list == NULL || exec2_list == NULL) {
3971 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3972 args->buffer_count);
3973 drm_free_large(exec_list);
3974 drm_free_large(exec2_list);
3975 return -ENOMEM;
3976 }
3977 ret = copy_from_user(exec_list,
3978 (struct drm_i915_relocation_entry __user *)
3979 (uintptr_t) args->buffers_ptr,
3980 sizeof(*exec_list) * args->buffer_count);
3981 if (ret != 0) {
3982 DRM_ERROR("copy %d exec entries failed %d\n",
3983 args->buffer_count, ret);
3984 drm_free_large(exec_list);
3985 drm_free_large(exec2_list);
3986 return -EFAULT;
3987 }
3988
3989 for (i = 0; i < args->buffer_count; i++) {
3990 exec2_list[i].handle = exec_list[i].handle;
3991 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3992 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3993 exec2_list[i].alignment = exec_list[i].alignment;
3994 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003995 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05003996 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3997 else
3998 exec2_list[i].flags = 0;
3999 }
4000
4001 exec2.buffers_ptr = args->buffers_ptr;
4002 exec2.buffer_count = args->buffer_count;
4003 exec2.batch_start_offset = args->batch_start_offset;
4004 exec2.batch_len = args->batch_len;
4005 exec2.DR1 = args->DR1;
4006 exec2.DR4 = args->DR4;
4007 exec2.num_cliprects = args->num_cliprects;
4008 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004009 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004010
4011 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4012 if (!ret) {
4013 /* Copy the new buffer offsets back to the user's exec list. */
4014 for (i = 0; i < args->buffer_count; i++)
4015 exec_list[i].offset = exec2_list[i].offset;
4016 /* ... and back out to userspace */
4017 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4018 (uintptr_t) args->buffers_ptr,
4019 exec_list,
4020 sizeof(*exec_list) * args->buffer_count);
4021 if (ret) {
4022 ret = -EFAULT;
4023 DRM_ERROR("failed to copy %d exec entries "
4024 "back to user (%d)\n",
4025 args->buffer_count, ret);
4026 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004027 }
4028
4029 drm_free_large(exec_list);
4030 drm_free_large(exec2_list);
4031 return ret;
4032}
4033
4034int
4035i915_gem_execbuffer2(struct drm_device *dev, void *data,
4036 struct drm_file *file_priv)
4037{
4038 struct drm_i915_gem_execbuffer2 *args = data;
4039 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4040 int ret;
4041
4042#if WATCH_EXEC
4043 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4044 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4045#endif
4046
4047 if (args->buffer_count < 1) {
4048 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4049 return -EINVAL;
4050 }
4051
4052 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4053 if (exec2_list == NULL) {
4054 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4055 args->buffer_count);
4056 return -ENOMEM;
4057 }
4058 ret = copy_from_user(exec2_list,
4059 (struct drm_i915_relocation_entry __user *)
4060 (uintptr_t) args->buffers_ptr,
4061 sizeof(*exec2_list) * args->buffer_count);
4062 if (ret != 0) {
4063 DRM_ERROR("copy %d exec entries failed %d\n",
4064 args->buffer_count, ret);
4065 drm_free_large(exec2_list);
4066 return -EFAULT;
4067 }
4068
4069 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4070 if (!ret) {
4071 /* Copy the new buffer offsets back to the user's exec list. */
4072 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4073 (uintptr_t) args->buffers_ptr,
4074 exec2_list,
4075 sizeof(*exec2_list) * args->buffer_count);
4076 if (ret) {
4077 ret = -EFAULT;
4078 DRM_ERROR("failed to copy %d exec entries "
4079 "back to user (%d)\n",
4080 args->buffer_count, ret);
4081 }
4082 }
4083
4084 drm_free_large(exec2_list);
4085 return ret;
4086}
4087
Eric Anholt673a3942008-07-30 12:06:12 -07004088int
4089i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4090{
4091 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004092 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004093 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004094 int ret;
4095
Daniel Vetter778c3542010-05-13 11:49:44 +02004096 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4097
Eric Anholt673a3942008-07-30 12:06:12 -07004098 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004099
4100 if (obj_priv->gtt_space != NULL) {
4101 if (alignment == 0)
4102 alignment = i915_gem_get_gtt_alignment(obj);
4103 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004104 WARN(obj_priv->pin_count,
4105 "bo is already pinned with incorrect alignment:"
4106 " offset=%x, req.alignment=%x\n",
4107 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004108 ret = i915_gem_object_unbind(obj);
4109 if (ret)
4110 return ret;
4111 }
4112 }
4113
Eric Anholt673a3942008-07-30 12:06:12 -07004114 if (obj_priv->gtt_space == NULL) {
4115 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004116 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004117 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004118 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004119
Eric Anholt673a3942008-07-30 12:06:12 -07004120 obj_priv->pin_count++;
4121
4122 /* If the object is not active and not pending a flush,
4123 * remove it from the inactive list
4124 */
4125 if (obj_priv->pin_count == 1) {
4126 atomic_inc(&dev->pin_count);
4127 atomic_add(obj->size, &dev->pin_memory);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004128 if (!obj_priv->active)
4129 list_move_tail(&obj_priv->list,
4130 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004131 }
4132 i915_verify_inactive(dev, __FILE__, __LINE__);
4133
4134 return 0;
4135}
4136
4137void
4138i915_gem_object_unpin(struct drm_gem_object *obj)
4139{
4140 struct drm_device *dev = obj->dev;
4141 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004142 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004143
4144 i915_verify_inactive(dev, __FILE__, __LINE__);
4145 obj_priv->pin_count--;
4146 BUG_ON(obj_priv->pin_count < 0);
4147 BUG_ON(obj_priv->gtt_space == NULL);
4148
4149 /* If the object is no longer pinned, and is
4150 * neither active nor being flushed, then stick it on
4151 * the inactive list
4152 */
4153 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004154 if (!obj_priv->active)
Eric Anholt673a3942008-07-30 12:06:12 -07004155 list_move_tail(&obj_priv->list,
4156 &dev_priv->mm.inactive_list);
4157 atomic_dec(&dev->pin_count);
4158 atomic_sub(obj->size, &dev->pin_memory);
4159 }
4160 i915_verify_inactive(dev, __FILE__, __LINE__);
4161}
4162
4163int
4164i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4165 struct drm_file *file_priv)
4166{
4167 struct drm_i915_gem_pin *args = data;
4168 struct drm_gem_object *obj;
4169 struct drm_i915_gem_object *obj_priv;
4170 int ret;
4171
Eric Anholt673a3942008-07-30 12:06:12 -07004172 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4173 if (obj == NULL) {
4174 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4175 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004176 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004177 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004178 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004179
Chris Wilson76c1dec2010-09-25 11:22:51 +01004180 ret = i915_mutex_lock_interruptible(dev);
4181 if (ret) {
4182 drm_gem_object_unreference_unlocked(obj);
4183 return ret;
4184 }
4185
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004186 if (obj_priv->madv != I915_MADV_WILLNEED) {
4187 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004188 drm_gem_object_unreference(obj);
4189 mutex_unlock(&dev->struct_mutex);
4190 return -EINVAL;
4191 }
4192
Jesse Barnes79e53942008-11-07 14:24:08 -08004193 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4194 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4195 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004196 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004197 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004198 return -EINVAL;
4199 }
4200
4201 obj_priv->user_pin_count++;
4202 obj_priv->pin_filp = file_priv;
4203 if (obj_priv->user_pin_count == 1) {
4204 ret = i915_gem_object_pin(obj, args->alignment);
4205 if (ret != 0) {
4206 drm_gem_object_unreference(obj);
4207 mutex_unlock(&dev->struct_mutex);
4208 return ret;
4209 }
Eric Anholt673a3942008-07-30 12:06:12 -07004210 }
4211
4212 /* XXX - flush the CPU caches for pinned objects
4213 * as the X server doesn't manage domains yet
4214 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004215 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004216 args->offset = obj_priv->gtt_offset;
4217 drm_gem_object_unreference(obj);
4218 mutex_unlock(&dev->struct_mutex);
4219
4220 return 0;
4221}
4222
4223int
4224i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4225 struct drm_file *file_priv)
4226{
4227 struct drm_i915_gem_pin *args = data;
4228 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004229 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004230 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004231
4232 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4233 if (obj == NULL) {
4234 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4235 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004236 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004237 }
4238
Daniel Vetter23010e42010-03-08 13:35:02 +01004239 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004240
4241 ret = i915_mutex_lock_interruptible(dev);
4242 if (ret) {
4243 drm_gem_object_unreference_unlocked(obj);
4244 return ret;
4245 }
4246
Jesse Barnes79e53942008-11-07 14:24:08 -08004247 if (obj_priv->pin_filp != file_priv) {
4248 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4249 args->handle);
4250 drm_gem_object_unreference(obj);
4251 mutex_unlock(&dev->struct_mutex);
4252 return -EINVAL;
4253 }
4254 obj_priv->user_pin_count--;
4255 if (obj_priv->user_pin_count == 0) {
4256 obj_priv->pin_filp = NULL;
4257 i915_gem_object_unpin(obj);
4258 }
Eric Anholt673a3942008-07-30 12:06:12 -07004259
4260 drm_gem_object_unreference(obj);
4261 mutex_unlock(&dev->struct_mutex);
4262 return 0;
4263}
4264
4265int
4266i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4267 struct drm_file *file_priv)
4268{
4269 struct drm_i915_gem_busy *args = data;
4270 struct drm_gem_object *obj;
4271 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004272 int ret;
4273
Eric Anholt673a3942008-07-30 12:06:12 -07004274 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4275 if (obj == NULL) {
4276 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4277 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004278 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004279 }
4280
Chris Wilson76c1dec2010-09-25 11:22:51 +01004281 ret = i915_mutex_lock_interruptible(dev);
4282 if (ret) {
4283 drm_gem_object_unreference_unlocked(obj);
4284 return ret;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004285 }
4286
Chris Wilson0be555b2010-08-04 15:36:30 +01004287 /* Count all active objects as busy, even if they are currently not used
4288 * by the gpu. Users of this interface expect objects to eventually
4289 * become non-busy without any further actions, therefore emit any
4290 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004291 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004292 obj_priv = to_intel_bo(obj);
4293 args->busy = obj_priv->active;
4294 if (args->busy) {
4295 /* Unconditionally flush objects, even when the gpu still uses this
4296 * object. Userspace calling this function indicates that it wants to
4297 * use this buffer rather sooner than later, so issuing the required
4298 * flush earlier is beneficial.
4299 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004300 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4301 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004302 obj_priv->ring,
4303 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004304
4305 /* Update the active list for the hardware's current position.
4306 * Otherwise this only updates on a delayed timer or when irqs
4307 * are actually unmasked, and our working set ends up being
4308 * larger than required.
4309 */
4310 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4311
4312 args->busy = obj_priv->active;
4313 }
Eric Anholt673a3942008-07-30 12:06:12 -07004314
4315 drm_gem_object_unreference(obj);
4316 mutex_unlock(&dev->struct_mutex);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004317 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004318}
4319
4320int
4321i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4322 struct drm_file *file_priv)
4323{
4324 return i915_gem_ring_throttle(dev, file_priv);
4325}
4326
Chris Wilson3ef94da2009-09-14 16:50:29 +01004327int
4328i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4329 struct drm_file *file_priv)
4330{
4331 struct drm_i915_gem_madvise *args = data;
4332 struct drm_gem_object *obj;
4333 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004334 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004335
4336 switch (args->madv) {
4337 case I915_MADV_DONTNEED:
4338 case I915_MADV_WILLNEED:
4339 break;
4340 default:
4341 return -EINVAL;
4342 }
4343
4344 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4345 if (obj == NULL) {
4346 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4347 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004348 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004349 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004350 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004351
Chris Wilson76c1dec2010-09-25 11:22:51 +01004352 ret = i915_mutex_lock_interruptible(dev);
4353 if (ret) {
4354 drm_gem_object_unreference_unlocked(obj);
4355 return ret;
4356 }
4357
Chris Wilson3ef94da2009-09-14 16:50:29 +01004358 if (obj_priv->pin_count) {
4359 drm_gem_object_unreference(obj);
4360 mutex_unlock(&dev->struct_mutex);
4361
4362 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4363 return -EINVAL;
4364 }
4365
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004366 if (obj_priv->madv != __I915_MADV_PURGED)
4367 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004368
Chris Wilson2d7ef392009-09-20 23:13:10 +01004369 /* if the object is no longer bound, discard its backing storage */
4370 if (i915_gem_object_is_purgeable(obj_priv) &&
4371 obj_priv->gtt_space == NULL)
4372 i915_gem_object_truncate(obj);
4373
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004374 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4375
Chris Wilson3ef94da2009-09-14 16:50:29 +01004376 drm_gem_object_unreference(obj);
4377 mutex_unlock(&dev->struct_mutex);
4378
4379 return 0;
4380}
4381
Daniel Vetterac52bc52010-04-09 19:05:06 +00004382struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4383 size_t size)
4384{
Daniel Vetterc397b902010-04-09 19:05:07 +00004385 struct drm_i915_gem_object *obj;
4386
4387 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4388 if (obj == NULL)
4389 return NULL;
4390
4391 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4392 kfree(obj);
4393 return NULL;
4394 }
4395
4396 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4397 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4398
4399 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004400 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004401 obj->fence_reg = I915_FENCE_REG_NONE;
4402 INIT_LIST_HEAD(&obj->list);
4403 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004404 obj->madv = I915_MADV_WILLNEED;
4405
4406 trace_i915_gem_object_create(&obj->base);
4407
4408 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004409}
4410
Eric Anholt673a3942008-07-30 12:06:12 -07004411int i915_gem_init_object(struct drm_gem_object *obj)
4412{
Daniel Vetterc397b902010-04-09 19:05:07 +00004413 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004414
Eric Anholt673a3942008-07-30 12:06:12 -07004415 return 0;
4416}
4417
Chris Wilsonbe726152010-07-23 23:18:50 +01004418static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4419{
4420 struct drm_device *dev = obj->dev;
4421 drm_i915_private_t *dev_priv = dev->dev_private;
4422 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4423 int ret;
4424
4425 ret = i915_gem_object_unbind(obj);
4426 if (ret == -ERESTARTSYS) {
4427 list_move(&obj_priv->list,
4428 &dev_priv->mm.deferred_free_list);
4429 return;
4430 }
4431
4432 if (obj_priv->mmap_offset)
4433 i915_gem_free_mmap_offset(obj);
4434
4435 drm_gem_object_release(obj);
4436
4437 kfree(obj_priv->page_cpu_valid);
4438 kfree(obj_priv->bit_17);
4439 kfree(obj_priv);
4440}
4441
Eric Anholt673a3942008-07-30 12:06:12 -07004442void i915_gem_free_object(struct drm_gem_object *obj)
4443{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004444 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004445 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004446
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004447 trace_i915_gem_object_destroy(obj);
4448
Eric Anholt673a3942008-07-30 12:06:12 -07004449 while (obj_priv->pin_count > 0)
4450 i915_gem_object_unpin(obj);
4451
Dave Airlie71acb5e2008-12-30 20:31:46 +10004452 if (obj_priv->phys_obj)
4453 i915_gem_detach_phys_object(dev, obj);
4454
Chris Wilsonbe726152010-07-23 23:18:50 +01004455 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004456}
4457
Jesse Barnes5669fca2009-02-17 15:13:31 -08004458int
Eric Anholt673a3942008-07-30 12:06:12 -07004459i915_gem_idle(struct drm_device *dev)
4460{
4461 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004462 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004463
Keith Packard6dbe2772008-10-14 21:41:13 -07004464 mutex_lock(&dev->struct_mutex);
4465
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004466 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004467 (dev_priv->render_ring.gem_object == NULL) ||
4468 (HAS_BSD(dev) &&
4469 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004470 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004471 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004472 }
Eric Anholt673a3942008-07-30 12:06:12 -07004473
Chris Wilson29105cc2010-01-07 10:39:13 +00004474 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004475 if (ret) {
4476 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004477 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004478 }
Eric Anholt673a3942008-07-30 12:06:12 -07004479
Chris Wilson29105cc2010-01-07 10:39:13 +00004480 /* Under UMS, be paranoid and evict. */
4481 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004482 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004483 if (ret) {
4484 mutex_unlock(&dev->struct_mutex);
4485 return ret;
4486 }
4487 }
4488
4489 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4490 * We need to replace this with a semaphore, or something.
4491 * And not confound mm.suspended!
4492 */
4493 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004494 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004495
4496 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004497 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004498
Keith Packard6dbe2772008-10-14 21:41:13 -07004499 mutex_unlock(&dev->struct_mutex);
4500
Chris Wilson29105cc2010-01-07 10:39:13 +00004501 /* Cancel the retire work handler, which should be idle now. */
4502 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4503
Eric Anholt673a3942008-07-30 12:06:12 -07004504 return 0;
4505}
4506
Jesse Barnese552eb72010-04-21 11:39:23 -07004507/*
4508 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4509 * over cache flushing.
4510 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004511static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004512i915_gem_init_pipe_control(struct drm_device *dev)
4513{
4514 drm_i915_private_t *dev_priv = dev->dev_private;
4515 struct drm_gem_object *obj;
4516 struct drm_i915_gem_object *obj_priv;
4517 int ret;
4518
Eric Anholt34dc4d42010-05-07 14:30:03 -07004519 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004520 if (obj == NULL) {
4521 DRM_ERROR("Failed to allocate seqno page\n");
4522 ret = -ENOMEM;
4523 goto err;
4524 }
4525 obj_priv = to_intel_bo(obj);
4526 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4527
4528 ret = i915_gem_object_pin(obj, 4096);
4529 if (ret)
4530 goto err_unref;
4531
4532 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4533 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4534 if (dev_priv->seqno_page == NULL)
4535 goto err_unpin;
4536
4537 dev_priv->seqno_obj = obj;
4538 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4539
4540 return 0;
4541
4542err_unpin:
4543 i915_gem_object_unpin(obj);
4544err_unref:
4545 drm_gem_object_unreference(obj);
4546err:
4547 return ret;
4548}
4549
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004550
4551static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004552i915_gem_cleanup_pipe_control(struct drm_device *dev)
4553{
4554 drm_i915_private_t *dev_priv = dev->dev_private;
4555 struct drm_gem_object *obj;
4556 struct drm_i915_gem_object *obj_priv;
4557
4558 obj = dev_priv->seqno_obj;
4559 obj_priv = to_intel_bo(obj);
4560 kunmap(obj_priv->pages[0]);
4561 i915_gem_object_unpin(obj);
4562 drm_gem_object_unreference(obj);
4563 dev_priv->seqno_obj = NULL;
4564
4565 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004566}
4567
Eric Anholt673a3942008-07-30 12:06:12 -07004568int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004569i915_gem_init_ringbuffer(struct drm_device *dev)
4570{
4571 drm_i915_private_t *dev_priv = dev->dev_private;
4572 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004573
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004574 if (HAS_PIPE_CONTROL(dev)) {
4575 ret = i915_gem_init_pipe_control(dev);
4576 if (ret)
4577 return ret;
4578 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004579
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004580 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004581 if (ret)
4582 goto cleanup_pipe_control;
4583
4584 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004585 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004586 if (ret)
4587 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004588 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004589
Chris Wilson6f392d5482010-08-07 11:01:22 +01004590 dev_priv->next_seqno = 1;
4591
Chris Wilson68f95ba2010-05-27 13:18:22 +01004592 return 0;
4593
4594cleanup_render_ring:
4595 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4596cleanup_pipe_control:
4597 if (HAS_PIPE_CONTROL(dev))
4598 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004599 return ret;
4600}
4601
4602void
4603i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4604{
4605 drm_i915_private_t *dev_priv = dev->dev_private;
4606
4607 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004608 if (HAS_BSD(dev))
4609 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004610 if (HAS_PIPE_CONTROL(dev))
4611 i915_gem_cleanup_pipe_control(dev);
4612}
4613
4614int
Eric Anholt673a3942008-07-30 12:06:12 -07004615i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4616 struct drm_file *file_priv)
4617{
4618 drm_i915_private_t *dev_priv = dev->dev_private;
4619 int ret;
4620
Jesse Barnes79e53942008-11-07 14:24:08 -08004621 if (drm_core_check_feature(dev, DRIVER_MODESET))
4622 return 0;
4623
Ben Gamariba1234d2009-09-14 17:48:47 -04004624 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004625 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004626 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004627 }
4628
Eric Anholt673a3942008-07-30 12:06:12 -07004629 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004630 dev_priv->mm.suspended = 0;
4631
4632 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004633 if (ret != 0) {
4634 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004635 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004636 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004637
Zou Nan hai852835f2010-05-21 09:08:56 +08004638 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004639 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004640 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4641 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004642 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004643 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004644 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004645
Chris Wilson5f353082010-06-07 14:03:03 +01004646 ret = drm_irq_install(dev);
4647 if (ret)
4648 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004649
Eric Anholt673a3942008-07-30 12:06:12 -07004650 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004651
4652cleanup_ringbuffer:
4653 mutex_lock(&dev->struct_mutex);
4654 i915_gem_cleanup_ringbuffer(dev);
4655 dev_priv->mm.suspended = 1;
4656 mutex_unlock(&dev->struct_mutex);
4657
4658 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004659}
4660
4661int
4662i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4663 struct drm_file *file_priv)
4664{
Jesse Barnes79e53942008-11-07 14:24:08 -08004665 if (drm_core_check_feature(dev, DRIVER_MODESET))
4666 return 0;
4667
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004668 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004669 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004670}
4671
4672void
4673i915_gem_lastclose(struct drm_device *dev)
4674{
4675 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004676
Eric Anholte806b492009-01-22 09:56:58 -08004677 if (drm_core_check_feature(dev, DRIVER_MODESET))
4678 return;
4679
Keith Packard6dbe2772008-10-14 21:41:13 -07004680 ret = i915_gem_idle(dev);
4681 if (ret)
4682 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004683}
4684
4685void
4686i915_gem_load(struct drm_device *dev)
4687{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004688 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004689 drm_i915_private_t *dev_priv = dev->dev_private;
4690
Eric Anholt673a3942008-07-30 12:06:12 -07004691 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004692 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004693 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004694 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004695 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004696 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004697 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4698 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004699 if (HAS_BSD(dev)) {
4700 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4701 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4702 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004703 for (i = 0; i < 16; i++)
4704 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004705 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4706 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004707 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004708 spin_lock(&shrink_list_lock);
4709 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4710 spin_unlock(&shrink_list_lock);
4711
Dave Airlie94400122010-07-20 13:15:31 +10004712 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4713 if (IS_GEN3(dev)) {
4714 u32 tmp = I915_READ(MI_ARB_STATE);
4715 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4716 /* arb state is a masked write, so set bit + bit in mask */
4717 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4718 I915_WRITE(MI_ARB_STATE, tmp);
4719 }
4720 }
4721
Jesse Barnesde151cf2008-11-12 10:03:55 -08004722 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004723 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4724 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004725
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004726 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004727 dev_priv->num_fence_regs = 16;
4728 else
4729 dev_priv->num_fence_regs = 8;
4730
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004731 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004732 switch (INTEL_INFO(dev)->gen) {
4733 case 6:
4734 for (i = 0; i < 16; i++)
4735 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4736 break;
4737 case 5:
4738 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004739 for (i = 0; i < 16; i++)
4740 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004741 break;
4742 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004743 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4744 for (i = 0; i < 8; i++)
4745 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004746 case 2:
4747 for (i = 0; i < 8; i++)
4748 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4749 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004750 }
Eric Anholt673a3942008-07-30 12:06:12 -07004751 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004752 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004753}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004754
4755/*
4756 * Create a physically contiguous memory object for this object
4757 * e.g. for cursor + overlay regs
4758 */
Chris Wilson995b6762010-08-20 13:23:26 +01004759static int i915_gem_init_phys_object(struct drm_device *dev,
4760 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004761{
4762 drm_i915_private_t *dev_priv = dev->dev_private;
4763 struct drm_i915_gem_phys_object *phys_obj;
4764 int ret;
4765
4766 if (dev_priv->mm.phys_objs[id - 1] || !size)
4767 return 0;
4768
Eric Anholt9a298b22009-03-24 12:23:04 -07004769 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004770 if (!phys_obj)
4771 return -ENOMEM;
4772
4773 phys_obj->id = id;
4774
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004775 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004776 if (!phys_obj->handle) {
4777 ret = -ENOMEM;
4778 goto kfree_obj;
4779 }
4780#ifdef CONFIG_X86
4781 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4782#endif
4783
4784 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4785
4786 return 0;
4787kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004788 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004789 return ret;
4790}
4791
Chris Wilson995b6762010-08-20 13:23:26 +01004792static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004793{
4794 drm_i915_private_t *dev_priv = dev->dev_private;
4795 struct drm_i915_gem_phys_object *phys_obj;
4796
4797 if (!dev_priv->mm.phys_objs[id - 1])
4798 return;
4799
4800 phys_obj = dev_priv->mm.phys_objs[id - 1];
4801 if (phys_obj->cur_obj) {
4802 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4803 }
4804
4805#ifdef CONFIG_X86
4806 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4807#endif
4808 drm_pci_free(dev, phys_obj->handle);
4809 kfree(phys_obj);
4810 dev_priv->mm.phys_objs[id - 1] = NULL;
4811}
4812
4813void i915_gem_free_all_phys_object(struct drm_device *dev)
4814{
4815 int i;
4816
Dave Airlie260883c2009-01-22 17:58:49 +10004817 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004818 i915_gem_free_phys_object(dev, i);
4819}
4820
4821void i915_gem_detach_phys_object(struct drm_device *dev,
4822 struct drm_gem_object *obj)
4823{
4824 struct drm_i915_gem_object *obj_priv;
4825 int i;
4826 int ret;
4827 int page_count;
4828
Daniel Vetter23010e42010-03-08 13:35:02 +01004829 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004830 if (!obj_priv->phys_obj)
4831 return;
4832
Chris Wilson4bdadb92010-01-27 13:36:32 +00004833 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004834 if (ret)
4835 goto out;
4836
4837 page_count = obj->size / PAGE_SIZE;
4838
4839 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004840 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004841 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4842
4843 memcpy(dst, src, PAGE_SIZE);
4844 kunmap_atomic(dst, KM_USER0);
4845 }
Eric Anholt856fa192009-03-19 14:10:50 -07004846 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004847 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004848
4849 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004850out:
4851 obj_priv->phys_obj->cur_obj = NULL;
4852 obj_priv->phys_obj = NULL;
4853}
4854
4855int
4856i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004857 struct drm_gem_object *obj,
4858 int id,
4859 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004860{
4861 drm_i915_private_t *dev_priv = dev->dev_private;
4862 struct drm_i915_gem_object *obj_priv;
4863 int ret = 0;
4864 int page_count;
4865 int i;
4866
4867 if (id > I915_MAX_PHYS_OBJECT)
4868 return -EINVAL;
4869
Daniel Vetter23010e42010-03-08 13:35:02 +01004870 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004871
4872 if (obj_priv->phys_obj) {
4873 if (obj_priv->phys_obj->id == id)
4874 return 0;
4875 i915_gem_detach_phys_object(dev, obj);
4876 }
4877
Dave Airlie71acb5e2008-12-30 20:31:46 +10004878 /* create a new object */
4879 if (!dev_priv->mm.phys_objs[id - 1]) {
4880 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004881 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004882 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004883 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004884 goto out;
4885 }
4886 }
4887
4888 /* bind to the object */
4889 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4890 obj_priv->phys_obj->cur_obj = obj;
4891
Chris Wilson4bdadb92010-01-27 13:36:32 +00004892 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004893 if (ret) {
4894 DRM_ERROR("failed to get page list\n");
4895 goto out;
4896 }
4897
4898 page_count = obj->size / PAGE_SIZE;
4899
4900 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004901 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004902 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4903
4904 memcpy(dst, src, PAGE_SIZE);
4905 kunmap_atomic(src, KM_USER0);
4906 }
4907
Chris Wilsond78b47b2009-06-17 21:52:49 +01004908 i915_gem_object_put_pages(obj);
4909
Dave Airlie71acb5e2008-12-30 20:31:46 +10004910 return 0;
4911out:
4912 return ret;
4913}
4914
4915static int
4916i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4917 struct drm_i915_gem_pwrite *args,
4918 struct drm_file *file_priv)
4919{
Daniel Vetter23010e42010-03-08 13:35:02 +01004920 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004921 void *obj_addr;
4922 int ret;
4923 char __user *user_data;
4924
4925 user_data = (char __user *) (uintptr_t) args->data_ptr;
4926 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4927
Zhao Yakui44d98a62009-10-09 11:39:40 +08004928 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004929 ret = copy_from_user(obj_addr, user_data, args->size);
4930 if (ret)
4931 return -EFAULT;
4932
4933 drm_agp_chipset_flush(dev);
4934 return 0;
4935}
Eric Anholtb9624422009-06-03 07:27:35 +00004936
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004937void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004938{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004939 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004940
4941 /* Clean up our request list when the client is going away, so that
4942 * later retire_requests won't dereference our soon-to-be-gone
4943 * file_priv.
4944 */
Chris Wilson1c255952010-09-26 11:03:27 +01004945 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004946 while (!list_empty(&file_priv->mm.request_list)) {
4947 struct drm_i915_gem_request *request;
4948
4949 request = list_first_entry(&file_priv->mm.request_list,
4950 struct drm_i915_gem_request,
4951 client_list);
4952 list_del(&request->client_list);
4953 request->file_priv = NULL;
4954 }
Chris Wilson1c255952010-09-26 11:03:27 +01004955 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004956}
Chris Wilson31169712009-09-14 16:50:28 +01004957
Chris Wilson31169712009-09-14 16:50:28 +01004958static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004959i915_gpu_is_active(struct drm_device *dev)
4960{
4961 drm_i915_private_t *dev_priv = dev->dev_private;
4962 int lists_empty;
4963
Chris Wilson1637ef42010-04-20 17:10:35 +01004964 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004965 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004966 if (HAS_BSD(dev))
4967 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004968
4969 return !lists_empty;
4970}
4971
4972static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004973i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004974{
4975 drm_i915_private_t *dev_priv, *next_dev;
4976 struct drm_i915_gem_object *obj_priv, *next_obj;
4977 int cnt = 0;
4978 int would_deadlock = 1;
4979
4980 /* "fast-path" to count number of available objects */
4981 if (nr_to_scan == 0) {
4982 spin_lock(&shrink_list_lock);
4983 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4984 struct drm_device *dev = dev_priv->dev;
4985
4986 if (mutex_trylock(&dev->struct_mutex)) {
4987 list_for_each_entry(obj_priv,
4988 &dev_priv->mm.inactive_list,
4989 list)
4990 cnt++;
4991 mutex_unlock(&dev->struct_mutex);
4992 }
4993 }
4994 spin_unlock(&shrink_list_lock);
4995
4996 return (cnt / 100) * sysctl_vfs_cache_pressure;
4997 }
4998
4999 spin_lock(&shrink_list_lock);
5000
Chris Wilson1637ef42010-04-20 17:10:35 +01005001rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005002 /* first scan for clean buffers */
5003 list_for_each_entry_safe(dev_priv, next_dev,
5004 &shrink_list, mm.shrink_list) {
5005 struct drm_device *dev = dev_priv->dev;
5006
5007 if (! mutex_trylock(&dev->struct_mutex))
5008 continue;
5009
5010 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005011 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005012
Chris Wilson31169712009-09-14 16:50:28 +01005013 list_for_each_entry_safe(obj_priv, next_obj,
5014 &dev_priv->mm.inactive_list,
5015 list) {
5016 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005017 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005018 if (--nr_to_scan <= 0)
5019 break;
5020 }
5021 }
5022
5023 spin_lock(&shrink_list_lock);
5024 mutex_unlock(&dev->struct_mutex);
5025
Chris Wilson963b4832009-09-20 23:03:54 +01005026 would_deadlock = 0;
5027
Chris Wilson31169712009-09-14 16:50:28 +01005028 if (nr_to_scan <= 0)
5029 break;
5030 }
5031
5032 /* second pass, evict/count anything still on the inactive list */
5033 list_for_each_entry_safe(dev_priv, next_dev,
5034 &shrink_list, mm.shrink_list) {
5035 struct drm_device *dev = dev_priv->dev;
5036
5037 if (! mutex_trylock(&dev->struct_mutex))
5038 continue;
5039
5040 spin_unlock(&shrink_list_lock);
5041
5042 list_for_each_entry_safe(obj_priv, next_obj,
5043 &dev_priv->mm.inactive_list,
5044 list) {
5045 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005046 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005047 nr_to_scan--;
5048 } else
5049 cnt++;
5050 }
5051
5052 spin_lock(&shrink_list_lock);
5053 mutex_unlock(&dev->struct_mutex);
5054
5055 would_deadlock = 0;
5056 }
5057
Chris Wilson1637ef42010-04-20 17:10:35 +01005058 if (nr_to_scan) {
5059 int active = 0;
5060
5061 /*
5062 * We are desperate for pages, so as a last resort, wait
5063 * for the GPU to finish and discard whatever we can.
5064 * This has a dramatic impact to reduce the number of
5065 * OOM-killer events whilst running the GPU aggressively.
5066 */
5067 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5068 struct drm_device *dev = dev_priv->dev;
5069
5070 if (!mutex_trylock(&dev->struct_mutex))
5071 continue;
5072
5073 spin_unlock(&shrink_list_lock);
5074
5075 if (i915_gpu_is_active(dev)) {
5076 i915_gpu_idle(dev);
5077 active++;
5078 }
5079
5080 spin_lock(&shrink_list_lock);
5081 mutex_unlock(&dev->struct_mutex);
5082 }
5083
5084 if (active)
5085 goto rescan;
5086 }
5087
Chris Wilson31169712009-09-14 16:50:28 +01005088 spin_unlock(&shrink_list_lock);
5089
5090 if (would_deadlock)
5091 return -1;
5092 else if (cnt > 0)
5093 return (cnt / 100) * sysctl_vfs_cache_pressure;
5094 else
5095 return 0;
5096}
5097
5098static struct shrinker shrinker = {
5099 .shrink = i915_gem_shrink,
5100 .seeks = DEFAULT_SEEKS,
5101};
5102
5103__init void
5104i915_gem_shrinker_init(void)
5105{
5106 register_shrinker(&shrinker);
5107}
5108
5109__exit void
5110i915_gem_shrinker_exit(void)
5111{
5112 unregister_shrinker(&shrinker);
5113}