blob: 78282edc02cafb15c70b593667d89ab35916f222 [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 Anholt673a3942008-07-30 12:06:12 -07001095#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001096 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001097 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001098#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001099 if (read_domains & I915_GEM_DOMAIN_GTT) {
1100 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001101
Eric Anholta09ba7f2009-08-29 12:49:51 -07001102 /* Update the LRU on the fence for the CPU access that's
1103 * about to occur.
1104 */
1105 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001106 struct drm_i915_fence_reg *reg =
1107 &dev_priv->fence_regs[obj_priv->fence_reg];
1108 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001109 &dev_priv->mm.fence_list);
1110 }
1111
Eric Anholt02354392008-11-26 13:58:13 -08001112 /* Silently promote "you're not bound, there was nothing to do"
1113 * to success, since the client was just asking us to
1114 * make sure everything was done.
1115 */
1116 if (ret == -EINVAL)
1117 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001118 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001119 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001120 }
1121
Chris Wilson7d1c4802010-08-07 21:45:03 +01001122 /* Maintain LRU order of "inactive" objects */
1123 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1124 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1125
Eric Anholt673a3942008-07-30 12:06:12 -07001126 drm_gem_object_unreference(obj);
1127 mutex_unlock(&dev->struct_mutex);
1128 return ret;
1129}
1130
1131/**
1132 * Called when user space has done writes to this buffer
1133 */
1134int
1135i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1136 struct drm_file *file_priv)
1137{
1138 struct drm_i915_gem_sw_finish *args = data;
1139 struct drm_gem_object *obj;
1140 struct drm_i915_gem_object *obj_priv;
1141 int ret = 0;
1142
1143 if (!(dev->driver->driver_features & DRIVER_GEM))
1144 return -ENODEV;
1145
Eric Anholt673a3942008-07-30 12:06:12 -07001146 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson76c1dec2010-09-25 11:22:51 +01001147 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001148 return -ENOENT;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001149
1150 ret = i915_mutex_lock_interruptible(dev);
1151 if (ret) {
1152 drm_gem_object_unreference_unlocked(obj);
1153 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001154 }
1155
1156#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001157 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001158 __func__, args->handle, obj, obj->size);
1159#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001160 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001161
1162 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001163 if (obj_priv->pin_count)
1164 i915_gem_object_flush_cpu_write_domain(obj);
1165
Eric Anholt673a3942008-07-30 12:06:12 -07001166 drm_gem_object_unreference(obj);
1167 mutex_unlock(&dev->struct_mutex);
1168 return ret;
1169}
1170
1171/**
1172 * Maps the contents of an object, returning the address it is mapped
1173 * into.
1174 *
1175 * While the mapping holds a reference on the contents of the object, it doesn't
1176 * imply a ref on the object itself.
1177 */
1178int
1179i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1180 struct drm_file *file_priv)
1181{
1182 struct drm_i915_gem_mmap *args = data;
1183 struct drm_gem_object *obj;
1184 loff_t offset;
1185 unsigned long addr;
1186
1187 if (!(dev->driver->driver_features & DRIVER_GEM))
1188 return -ENODEV;
1189
1190 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1191 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001192 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001193
1194 offset = args->offset;
1195
1196 down_write(&current->mm->mmap_sem);
1197 addr = do_mmap(obj->filp, 0, args->size,
1198 PROT_READ | PROT_WRITE, MAP_SHARED,
1199 args->offset);
1200 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001201 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001202 if (IS_ERR((void *)addr))
1203 return addr;
1204
1205 args->addr_ptr = (uint64_t) addr;
1206
1207 return 0;
1208}
1209
Jesse Barnesde151cf2008-11-12 10:03:55 -08001210/**
1211 * i915_gem_fault - fault a page into the GTT
1212 * vma: VMA in question
1213 * vmf: fault info
1214 *
1215 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1216 * from userspace. The fault handler takes care of binding the object to
1217 * the GTT (if needed), allocating and programming a fence register (again,
1218 * only if needed based on whether the old reg is still valid or the object
1219 * is tiled) and inserting a new PTE into the faulting process.
1220 *
1221 * Note that the faulting process may involve evicting existing objects
1222 * from the GTT and/or fence registers to make room. So performance may
1223 * suffer if the GTT working set is large or there are few fence registers
1224 * left.
1225 */
1226int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1227{
1228 struct drm_gem_object *obj = vma->vm_private_data;
1229 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001230 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001231 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001232 pgoff_t page_offset;
1233 unsigned long pfn;
1234 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001235 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001236
1237 /* We don't use vmf->pgoff since that has the fake offset */
1238 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1239 PAGE_SHIFT;
1240
1241 /* Now bind it into the GTT if needed */
1242 mutex_lock(&dev->struct_mutex);
1243 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001244 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001245 if (ret)
1246 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001247
Jesse Barnesde151cf2008-11-12 10:03:55 -08001248 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001249 if (ret)
1250 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001251 }
1252
1253 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001254 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001255 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001256 if (ret)
1257 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001258 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001259
Chris Wilson7d1c4802010-08-07 21:45:03 +01001260 if (i915_gem_object_is_inactive(obj_priv))
1261 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1262
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1264 page_offset;
1265
1266 /* Finally, remap it using the new GTT offset */
1267 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001268unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001269 mutex_unlock(&dev->struct_mutex);
1270
1271 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001272 case 0:
1273 case -ERESTARTSYS:
1274 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001275 case -ENOMEM:
1276 case -EAGAIN:
1277 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001278 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001279 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001280 }
1281}
1282
1283/**
1284 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1285 * @obj: obj in question
1286 *
1287 * GEM memory mapping works by handing back to userspace a fake mmap offset
1288 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1289 * up the object based on the offset and sets up the various memory mapping
1290 * structures.
1291 *
1292 * This routine allocates and attaches a fake offset for @obj.
1293 */
1294static int
1295i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1296{
1297 struct drm_device *dev = obj->dev;
1298 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001299 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001300 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001301 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001302 int ret = 0;
1303
1304 /* Set the object up for mmap'ing */
1305 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001306 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001307 if (!list->map)
1308 return -ENOMEM;
1309
1310 map = list->map;
1311 map->type = _DRM_GEM;
1312 map->size = obj->size;
1313 map->handle = obj;
1314
1315 /* Get a DRM GEM mmap offset allocated... */
1316 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1317 obj->size / PAGE_SIZE, 0, 0);
1318 if (!list->file_offset_node) {
1319 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001320 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321 goto out_free_list;
1322 }
1323
1324 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1325 obj->size / PAGE_SIZE, 0);
1326 if (!list->file_offset_node) {
1327 ret = -ENOMEM;
1328 goto out_free_list;
1329 }
1330
1331 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001332 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1333 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001334 DRM_ERROR("failed to add to map hash\n");
1335 goto out_free_mm;
1336 }
1337
1338 /* By now we should be all set, any drm_mmap request on the offset
1339 * below will get to our mmap & fault handler */
1340 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1341
1342 return 0;
1343
1344out_free_mm:
1345 drm_mm_put_block(list->file_offset_node);
1346out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001347 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001348
1349 return ret;
1350}
1351
Chris Wilson901782b2009-07-10 08:18:50 +01001352/**
1353 * i915_gem_release_mmap - remove physical page mappings
1354 * @obj: obj in question
1355 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001356 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001357 * relinquish ownership of the pages back to the system.
1358 *
1359 * It is vital that we remove the page mapping if we have mapped a tiled
1360 * object through the GTT and then lose the fence register due to
1361 * resource pressure. Similarly if the object has been moved out of the
1362 * aperture, than pages mapped into userspace must be revoked. Removing the
1363 * mapping will then trigger a page fault on the next user access, allowing
1364 * fixup by i915_gem_fault().
1365 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001366void
Chris Wilson901782b2009-07-10 08:18:50 +01001367i915_gem_release_mmap(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);
Chris Wilson901782b2009-07-10 08:18:50 +01001371
1372 if (dev->dev_mapping)
1373 unmap_mapping_range(dev->dev_mapping,
1374 obj_priv->mmap_offset, obj->size, 1);
1375}
1376
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001377static void
1378i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1379{
1380 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001381 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001382 struct drm_gem_mm *mm = dev->mm_private;
1383 struct drm_map_list *list;
1384
1385 list = &obj->map_list;
1386 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1387
1388 if (list->file_offset_node) {
1389 drm_mm_put_block(list->file_offset_node);
1390 list->file_offset_node = NULL;
1391 }
1392
1393 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001394 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001395 list->map = NULL;
1396 }
1397
1398 obj_priv->mmap_offset = 0;
1399}
1400
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401/**
1402 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1403 * @obj: object to check
1404 *
1405 * Return the required GTT alignment for an object, taking into account
1406 * potential fence register mapping if needed.
1407 */
1408static uint32_t
1409i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1410{
1411 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001412 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001413 int start, i;
1414
1415 /*
1416 * Minimum alignment is 4k (GTT page size), but might be greater
1417 * if a fence register is needed for the object.
1418 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001419 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001420 return 4096;
1421
1422 /*
1423 * Previous chips need to be aligned to the size of the smallest
1424 * fence register that can contain the object.
1425 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001426 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001427 start = 1024*1024;
1428 else
1429 start = 512*1024;
1430
1431 for (i = start; i < obj->size; i <<= 1)
1432 ;
1433
1434 return i;
1435}
1436
1437/**
1438 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1439 * @dev: DRM device
1440 * @data: GTT mapping ioctl data
1441 * @file_priv: GEM object info
1442 *
1443 * Simply returns the fake offset to userspace so it can mmap it.
1444 * The mmap call will end up in drm_gem_mmap(), which will set things
1445 * up so we can get faults in the handler above.
1446 *
1447 * The fault handler will take care of binding the object into the GTT
1448 * (since it may have been evicted to make room for something), allocating
1449 * a fence register, and mapping the appropriate aperture address into
1450 * userspace.
1451 */
1452int
1453i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1454 struct drm_file *file_priv)
1455{
1456 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001457 struct drm_gem_object *obj;
1458 struct drm_i915_gem_object *obj_priv;
1459 int ret;
1460
1461 if (!(dev->driver->driver_features & DRIVER_GEM))
1462 return -ENODEV;
1463
1464 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1465 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001466 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001467
Chris Wilson76c1dec2010-09-25 11:22:51 +01001468 ret = i915_mutex_lock_interruptible(dev);
1469 if (ret) {
1470 drm_gem_object_unreference_unlocked(obj);
1471 return ret;
1472 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001473
Daniel Vetter23010e42010-03-08 13:35:02 +01001474 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001475
Chris Wilsonab182822009-09-22 18:46:17 +01001476 if (obj_priv->madv != I915_MADV_WILLNEED) {
1477 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1478 drm_gem_object_unreference(obj);
1479 mutex_unlock(&dev->struct_mutex);
1480 return -EINVAL;
1481 }
1482
1483
Jesse Barnesde151cf2008-11-12 10:03:55 -08001484 if (!obj_priv->mmap_offset) {
1485 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001486 if (ret) {
1487 drm_gem_object_unreference(obj);
1488 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001489 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001490 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001491 }
1492
1493 args->offset = obj_priv->mmap_offset;
1494
Jesse Barnesde151cf2008-11-12 10:03:55 -08001495 /*
1496 * Pull it into the GTT so that we have a page list (makes the
1497 * initial fault faster and any subsequent flushing possible).
1498 */
1499 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001500 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001501 if (ret) {
1502 drm_gem_object_unreference(obj);
1503 mutex_unlock(&dev->struct_mutex);
1504 return ret;
1505 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001506 }
1507
1508 drm_gem_object_unreference(obj);
1509 mutex_unlock(&dev->struct_mutex);
1510
1511 return 0;
1512}
1513
Ben Gamari6911a9b2009-04-02 11:24:54 -07001514void
Eric Anholt856fa192009-03-19 14:10:50 -07001515i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001516{
Daniel Vetter23010e42010-03-08 13:35:02 +01001517 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001518 int page_count = obj->size / PAGE_SIZE;
1519 int i;
1520
Eric Anholt856fa192009-03-19 14:10:50 -07001521 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001522 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001523
1524 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001525 return;
1526
Eric Anholt280b7132009-03-12 16:56:27 -07001527 if (obj_priv->tiling_mode != I915_TILING_NONE)
1528 i915_gem_object_save_bit_17_swizzle(obj);
1529
Chris Wilson3ef94da2009-09-14 16:50:29 +01001530 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001531 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001532
1533 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001534 if (obj_priv->dirty)
1535 set_page_dirty(obj_priv->pages[i]);
1536
1537 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001538 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001539
1540 page_cache_release(obj_priv->pages[i]);
1541 }
Eric Anholt673a3942008-07-30 12:06:12 -07001542 obj_priv->dirty = 0;
1543
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001544 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001545 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001546}
1547
1548static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001549i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001550 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001551{
Chris Wilson5c12a07e2010-09-22 11:22:30 +01001552 struct drm_i915_private *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001553 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
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 Wilson5c12a07e2010-09-22 11:22:30 +01001566 obj_priv->last_rendering_seqno = dev_priv->next_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);
Eric Anholt673a3942008-07-30 12:06:12 -07001689
1690 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001691 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001692 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001693 was_empty = list_empty(&ring->request_list);
1694 list_add_tail(&request->list, &ring->request_list);
1695
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001696 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001697 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001698 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001699 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001700 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001701 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001702 }
Eric Anholt673a3942008-07-30 12:06:12 -07001703
Ben Gamarif65d9422009-09-14 17:48:44 -04001704 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001705 mod_timer(&dev_priv->hangcheck_timer,
1706 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001707 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001708 queue_delayed_work(dev_priv->wq,
1709 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001710 }
Eric Anholt673a3942008-07-30 12:06:12 -07001711 return seqno;
1712}
1713
1714/**
1715 * Command execution barrier
1716 *
1717 * Ensures that all commands in the ring are finished
1718 * before signalling the CPU
1719 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001720static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001721i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001722{
Eric Anholt673a3942008-07-30 12:06:12 -07001723 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001724
1725 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001726 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001727 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001728
1729 ring->flush(dev, ring,
1730 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001731}
1732
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001733static inline void
1734i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001735{
Chris Wilson1c255952010-09-26 11:03:27 +01001736 struct drm_i915_file_private *file_priv = request->file_priv;
1737
1738 if (!file_priv)
1739 return;
1740
1741 spin_lock(&file_priv->mm.lock);
1742 list_del(&request->client_list);
1743 request->file_priv = NULL;
1744 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001745}
1746
Chris Wilsondfaae392010-09-22 10:31:52 +01001747static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1748 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001749{
Chris Wilsondfaae392010-09-22 10:31:52 +01001750 while (!list_empty(&ring->request_list)) {
1751 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001752
Chris Wilsondfaae392010-09-22 10:31:52 +01001753 request = list_first_entry(&ring->request_list,
1754 struct drm_i915_gem_request,
1755 list);
1756
1757 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001758 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001759 kfree(request);
1760 }
1761
1762 while (!list_empty(&ring->active_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001763 struct drm_i915_gem_object *obj_priv;
1764
Chris Wilsondfaae392010-09-22 10:31:52 +01001765 obj_priv = list_first_entry(&ring->active_list,
1766 struct drm_i915_gem_object,
1767 list);
1768
1769 obj_priv->base.write_domain = 0;
1770 list_del_init(&obj_priv->gpu_write_list);
1771 i915_gem_object_move_to_inactive(&obj_priv->base);
1772 }
1773}
1774
1775void i915_gem_reset_lists(struct drm_device *dev)
1776{
1777 struct drm_i915_private *dev_priv = dev->dev_private;
1778 struct drm_i915_gem_object *obj_priv;
1779
1780 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
1781 if (HAS_BSD(dev))
1782 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
1783
1784 /* Remove anything from the flushing lists. The GPU cache is likely
1785 * to be lost on reset along with the data, so simply move the
1786 * lost bo to the inactive list.
1787 */
1788 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001789 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1790 struct drm_i915_gem_object,
1791 list);
1792
1793 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001794 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001795 i915_gem_object_move_to_inactive(&obj_priv->base);
1796 }
Chris Wilson9375e442010-09-19 12:21:28 +01001797
Chris Wilsondfaae392010-09-22 10:31:52 +01001798 /* Move everything out of the GPU domains to ensure we do any
1799 * necessary invalidation upon reuse.
1800 */
Chris Wilson77f01232010-09-19 12:31:36 +01001801 list_for_each_entry(obj_priv,
1802 &dev_priv->mm.inactive_list,
1803 list)
1804 {
1805 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1806 }
1807}
1808
Eric Anholt673a3942008-07-30 12:06:12 -07001809/**
1810 * This function clears the request list as sequence numbers are passed.
1811 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001812static void
1813i915_gem_retire_requests_ring(struct drm_device *dev,
1814 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001815{
1816 drm_i915_private_t *dev_priv = dev->dev_private;
1817 uint32_t seqno;
1818
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001819 if (!ring->status_page.page_addr ||
1820 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001821 return;
1822
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001823 seqno = ring->get_seqno(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001824 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001825 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001826
Zou Nan hai852835f2010-05-21 09:08:56 +08001827 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001828 struct drm_i915_gem_request,
1829 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001830
Chris Wilsondfaae392010-09-22 10:31:52 +01001831 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001832 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001833
1834 trace_i915_gem_request_retire(dev, request->seqno);
1835
1836 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001837 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001838 kfree(request);
1839 }
1840
1841 /* Move any buffers on the active list that are no longer referenced
1842 * by the ringbuffer to the flushing/inactive lists as appropriate.
1843 */
1844 while (!list_empty(&ring->active_list)) {
1845 struct drm_gem_object *obj;
1846 struct drm_i915_gem_object *obj_priv;
1847
1848 obj_priv = list_first_entry(&ring->active_list,
1849 struct drm_i915_gem_object,
1850 list);
1851
Chris Wilsondfaae392010-09-22 10:31:52 +01001852 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001853 break;
1854
1855 obj = &obj_priv->base;
1856
1857#if WATCH_LRU
1858 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1859 __func__, request->seqno, obj);
1860#endif
1861
1862 if (obj->write_domain != 0)
1863 i915_gem_object_move_to_flushing(obj);
1864 else
1865 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001866 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001867
1868 if (unlikely (dev_priv->trace_irq_seqno &&
1869 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001870 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001871 dev_priv->trace_irq_seqno = 0;
1872 }
Eric Anholt673a3942008-07-30 12:06:12 -07001873}
1874
1875void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001876i915_gem_retire_requests(struct drm_device *dev)
1877{
1878 drm_i915_private_t *dev_priv = dev->dev_private;
1879
Chris Wilsonbe726152010-07-23 23:18:50 +01001880 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1881 struct drm_i915_gem_object *obj_priv, *tmp;
1882
1883 /* We must be careful that during unbind() we do not
1884 * accidentally infinitely recurse into retire requests.
1885 * Currently:
1886 * retire -> free -> unbind -> wait -> retire_ring
1887 */
1888 list_for_each_entry_safe(obj_priv, tmp,
1889 &dev_priv->mm.deferred_free_list,
1890 list)
1891 i915_gem_free_object_tail(&obj_priv->base);
1892 }
1893
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001894 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1895 if (HAS_BSD(dev))
1896 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1897}
1898
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001899static void
Eric Anholt673a3942008-07-30 12:06:12 -07001900i915_gem_retire_work_handler(struct work_struct *work)
1901{
1902 drm_i915_private_t *dev_priv;
1903 struct drm_device *dev;
1904
1905 dev_priv = container_of(work, drm_i915_private_t,
1906 mm.retire_work.work);
1907 dev = dev_priv->dev;
1908
1909 mutex_lock(&dev->struct_mutex);
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
Daniel Vettere35a41d2010-02-11 22:13:59 +01001933 if (seqno == dev_priv->next_seqno) {
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 }
1938
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001939 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001940 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001941 ier = I915_READ(DEIER) | I915_READ(GTIER);
1942 else
1943 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001944 if (!ier) {
1945 DRM_ERROR("something (likely vbetool) disabled "
1946 "interrupts, re-enabling\n");
1947 i915_driver_irq_preinstall(dev);
1948 i915_driver_irq_postinstall(dev);
1949 }
1950
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001951 trace_i915_gem_request_wait_begin(dev, seqno);
1952
Zou Nan hai852835f2010-05-21 09:08:56 +08001953 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001954 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001955 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001956 ret = wait_event_interruptible(ring->irq_queue,
1957 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001958 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001959 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001960 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001961 wait_event(ring->irq_queue,
1962 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001963 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001964 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001965
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001966 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001967 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001968
1969 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001970 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001971 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001972 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07001973
1974 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01001975 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001976 __func__, ret, seqno, ring->get_seqno(dev, ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01001977 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001978
1979 /* Directly dispatch request retiring. While we have the work queue
1980 * to handle this, the waiter on a request often wants an associated
1981 * buffer to have made it to the inactive list, and we would need
1982 * a separate wait queue to handle that.
1983 */
1984 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001985 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001986
1987 return ret;
1988}
1989
Daniel Vetter48764bf2009-09-15 22:57:32 +02001990/**
1991 * Waits for a sequence number to be signaled, and cleans up the
1992 * request and object lists appropriately for that event.
1993 */
1994static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001995i915_wait_request(struct drm_device *dev, uint32_t seqno,
1996 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001997{
Zou Nan hai852835f2010-05-21 09:08:56 +08001998 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001999}
2000
Chris Wilson20f0cd52010-09-23 11:00:38 +01002001static void
Chris Wilson92204342010-09-18 11:02:01 +01002002i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002003 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002004 struct intel_ring_buffer *ring,
2005 uint32_t invalidate_domains,
2006 uint32_t flush_domains)
2007{
2008 ring->flush(dev, ring, invalidate_domains, flush_domains);
2009 i915_gem_process_flushing_list(dev, flush_domains, ring);
2010}
2011
2012static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002013i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002014 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002015 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002016 uint32_t flush_domains,
2017 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002018{
2019 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002020
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002021 if (flush_domains & I915_GEM_DOMAIN_CPU)
2022 drm_agp_chipset_flush(dev);
Daniel Vetter8bff9172010-02-11 22:19:40 +01002023
Chris Wilson92204342010-09-18 11:02:01 +01002024 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2025 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002026 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002027 &dev_priv->render_ring,
2028 invalidate_domains, flush_domains);
2029 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002030 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002031 &dev_priv->bsd_ring,
2032 invalidate_domains, flush_domains);
2033 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002034}
2035
Eric Anholt673a3942008-07-30 12:06:12 -07002036/**
2037 * Ensures that all rendering to the object has completed and the object is
2038 * safe to unbind from the GTT or access from the CPU.
2039 */
2040static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002041i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2042 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002043{
2044 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002045 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002046 int ret;
2047
Eric Anholte47c68e2008-11-14 13:35:19 -08002048 /* This function only exists to support waiting for existing rendering,
2049 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002050 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002051 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002052
2053 /* If there is rendering queued on the buffer being evicted, wait for
2054 * it.
2055 */
2056 if (obj_priv->active) {
2057#if WATCH_BUF
2058 DRM_INFO("%s: object %p wait for seqno %08x\n",
2059 __func__, obj, obj_priv->last_rendering_seqno);
2060#endif
Chris Wilson2cf34d72010-09-14 13:03:28 +01002061 ret = i915_do_wait_request(dev,
2062 obj_priv->last_rendering_seqno,
2063 interruptible,
2064 obj_priv->ring);
2065 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002066 return ret;
2067 }
2068
2069 return 0;
2070}
2071
2072/**
2073 * Unbinds an object from the GTT aperture.
2074 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002075int
Eric Anholt673a3942008-07-30 12:06:12 -07002076i915_gem_object_unbind(struct drm_gem_object *obj)
2077{
2078 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002079 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002080 int ret = 0;
2081
2082#if WATCH_BUF
2083 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
2084 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
2085#endif
2086 if (obj_priv->gtt_space == NULL)
2087 return 0;
2088
2089 if (obj_priv->pin_count != 0) {
2090 DRM_ERROR("Attempting to unbind pinned buffer\n");
2091 return -EINVAL;
2092 }
2093
Eric Anholt5323fd02009-09-09 11:50:45 -07002094 /* blow away mappings if mapped through GTT */
2095 i915_gem_release_mmap(obj);
2096
Eric Anholt673a3942008-07-30 12:06:12 -07002097 /* Move the object to the CPU domain to ensure that
2098 * any possible CPU writes while it's not in the GTT
2099 * are flushed when we go to remap it. This will
2100 * also ensure that all pending GPU writes are finished
2101 * before we unbind.
2102 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002103 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002104 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002105 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002106 /* Continue on if we fail due to EIO, the GPU is hung so we
2107 * should be safe and we need to cleanup or else we might
2108 * cause memory corruption through use-after-free.
2109 */
Eric Anholt673a3942008-07-30 12:06:12 -07002110
Daniel Vetter96b47b62009-12-15 17:50:00 +01002111 /* release the fence reg _after_ flushing */
2112 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2113 i915_gem_clear_fence_reg(obj);
2114
Eric Anholt673a3942008-07-30 12:06:12 -07002115 if (obj_priv->agp_mem != NULL) {
2116 drm_unbind_agp(obj_priv->agp_mem);
2117 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2118 obj_priv->agp_mem = NULL;
2119 }
2120
Eric Anholt856fa192009-03-19 14:10:50 -07002121 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002122 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002123
2124 if (obj_priv->gtt_space) {
2125 atomic_dec(&dev->gtt_count);
2126 atomic_sub(obj->size, &dev->gtt_memory);
2127
2128 drm_mm_put_block(obj_priv->gtt_space);
2129 obj_priv->gtt_space = NULL;
2130 }
2131
Chris Wilsonf13d3f72010-09-20 17:36:15 +01002132 list_del_init(&obj_priv->list);
Eric Anholt673a3942008-07-30 12:06:12 -07002133
Chris Wilson963b4832009-09-20 23:03:54 +01002134 if (i915_gem_object_is_purgeable(obj_priv))
2135 i915_gem_object_truncate(obj);
2136
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002137 trace_i915_gem_object_unbind(obj);
2138
Chris Wilson8dc17752010-07-23 23:18:51 +01002139 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002140}
2141
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002142int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002143i915_gpu_idle(struct drm_device *dev)
2144{
2145 drm_i915_private_t *dev_priv = dev->dev_private;
2146 bool lists_empty;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002147 u32 seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08002148 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002149
Zou Nan haid1b851f2010-05-21 09:08:57 +08002150 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2151 list_empty(&dev_priv->render_ring.active_list) &&
2152 (!HAS_BSD(dev) ||
2153 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002154 if (lists_empty)
2155 return 0;
2156
2157 /* Flush everything onto the inactive list. */
Chris Wilson5c12a07e2010-09-22 11:22:30 +01002158 seqno = dev_priv->next_seqno;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002159 i915_gem_flush_ring(dev, NULL, &dev_priv->render_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002160 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilsonc78ec302010-09-20 12:50:23 +01002161 ret = i915_wait_request(dev, seqno, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002162 if (ret)
2163 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002164
2165 if (HAS_BSD(dev)) {
Chris Wilson5c12a07e2010-09-22 11:22:30 +01002166 seqno = dev_priv->next_seqno;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002167 i915_gem_flush_ring(dev, NULL, &dev_priv->bsd_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002168 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilsonc78ec302010-09-20 12:50:23 +01002169 ret = i915_wait_request(dev, seqno, &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002170 if (ret)
2171 return ret;
2172 }
2173
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002174 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002175}
2176
Ben Gamari6911a9b2009-04-02 11:24:54 -07002177int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002178i915_gem_object_get_pages(struct drm_gem_object *obj,
2179 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002180{
Daniel Vetter23010e42010-03-08 13:35:02 +01002181 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002182 int page_count, i;
2183 struct address_space *mapping;
2184 struct inode *inode;
2185 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002186
Daniel Vetter778c3542010-05-13 11:49:44 +02002187 BUG_ON(obj_priv->pages_refcount
2188 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2189
Eric Anholt856fa192009-03-19 14:10:50 -07002190 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002191 return 0;
2192
2193 /* Get the list of pages out of our struct file. They'll be pinned
2194 * at this point until we release them.
2195 */
2196 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002197 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002198 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002199 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002200 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002201 return -ENOMEM;
2202 }
2203
2204 inode = obj->filp->f_path.dentry->d_inode;
2205 mapping = inode->i_mapping;
2206 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002207 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002208 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002209 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002210 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002211 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002212 if (IS_ERR(page))
2213 goto err_pages;
2214
Eric Anholt856fa192009-03-19 14:10:50 -07002215 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002216 }
Eric Anholt280b7132009-03-12 16:56:27 -07002217
2218 if (obj_priv->tiling_mode != I915_TILING_NONE)
2219 i915_gem_object_do_bit_17_swizzle(obj);
2220
Eric Anholt673a3942008-07-30 12:06:12 -07002221 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002222
2223err_pages:
2224 while (i--)
2225 page_cache_release(obj_priv->pages[i]);
2226
2227 drm_free_large(obj_priv->pages);
2228 obj_priv->pages = NULL;
2229 obj_priv->pages_refcount--;
2230 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002231}
2232
Eric Anholt4e901fd2009-10-26 16:44:17 -07002233static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2234{
2235 struct drm_gem_object *obj = reg->obj;
2236 struct drm_device *dev = obj->dev;
2237 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002238 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002239 int regnum = obj_priv->fence_reg;
2240 uint64_t val;
2241
2242 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2243 0xfffff000) << 32;
2244 val |= obj_priv->gtt_offset & 0xfffff000;
2245 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2246 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2247
2248 if (obj_priv->tiling_mode == I915_TILING_Y)
2249 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2250 val |= I965_FENCE_REG_VALID;
2251
2252 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2253}
2254
Jesse Barnesde151cf2008-11-12 10:03:55 -08002255static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2256{
2257 struct drm_gem_object *obj = reg->obj;
2258 struct drm_device *dev = obj->dev;
2259 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002260 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002261 int regnum = obj_priv->fence_reg;
2262 uint64_t val;
2263
2264 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2265 0xfffff000) << 32;
2266 val |= obj_priv->gtt_offset & 0xfffff000;
2267 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2268 if (obj_priv->tiling_mode == I915_TILING_Y)
2269 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2270 val |= I965_FENCE_REG_VALID;
2271
2272 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2273}
2274
2275static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2276{
2277 struct drm_gem_object *obj = reg->obj;
2278 struct drm_device *dev = obj->dev;
2279 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002280 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002281 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002282 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002283 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002284 uint32_t pitch_val;
2285
2286 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2287 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002288 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002289 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002290 return;
2291 }
2292
Jesse Barnes0f973f22009-01-26 17:10:45 -08002293 if (obj_priv->tiling_mode == I915_TILING_Y &&
2294 HAS_128_BYTE_Y_TILING(dev))
2295 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002296 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002297 tile_width = 512;
2298
2299 /* Note: pitch better be a power of two tile widths */
2300 pitch_val = obj_priv->stride / tile_width;
2301 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002302
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002303 if (obj_priv->tiling_mode == I915_TILING_Y &&
2304 HAS_128_BYTE_Y_TILING(dev))
2305 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2306 else
2307 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2308
Jesse Barnesde151cf2008-11-12 10:03:55 -08002309 val = obj_priv->gtt_offset;
2310 if (obj_priv->tiling_mode == I915_TILING_Y)
2311 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2312 val |= I915_FENCE_SIZE_BITS(obj->size);
2313 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2314 val |= I830_FENCE_REG_VALID;
2315
Eric Anholtdc529a42009-03-10 22:34:49 -07002316 if (regnum < 8)
2317 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2318 else
2319 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2320 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002321}
2322
2323static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2324{
2325 struct drm_gem_object *obj = reg->obj;
2326 struct drm_device *dev = obj->dev;
2327 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002328 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002329 int regnum = obj_priv->fence_reg;
2330 uint32_t val;
2331 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002332 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002333
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002334 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002335 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002336 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002337 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002338 return;
2339 }
2340
Eric Anholte76a16d2009-05-26 17:44:56 -07002341 pitch_val = obj_priv->stride / 128;
2342 pitch_val = ffs(pitch_val) - 1;
2343 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2344
Jesse Barnesde151cf2008-11-12 10:03:55 -08002345 val = obj_priv->gtt_offset;
2346 if (obj_priv->tiling_mode == I915_TILING_Y)
2347 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002348 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2349 WARN_ON(fence_size_bits & ~0x00000f00);
2350 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002351 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2352 val |= I830_FENCE_REG_VALID;
2353
2354 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002355}
2356
Chris Wilson2cf34d72010-09-14 13:03:28 +01002357static int i915_find_fence_reg(struct drm_device *dev,
2358 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002359{
2360 struct drm_i915_fence_reg *reg = NULL;
2361 struct drm_i915_gem_object *obj_priv = NULL;
2362 struct drm_i915_private *dev_priv = dev->dev_private;
2363 struct drm_gem_object *obj = NULL;
2364 int i, avail, ret;
2365
2366 /* First try to find a free reg */
2367 avail = 0;
2368 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2369 reg = &dev_priv->fence_regs[i];
2370 if (!reg->obj)
2371 return i;
2372
Daniel Vetter23010e42010-03-08 13:35:02 +01002373 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002374 if (!obj_priv->pin_count)
2375 avail++;
2376 }
2377
2378 if (avail == 0)
2379 return -ENOSPC;
2380
2381 /* None available, try to steal one or wait for a user to finish */
2382 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002383 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2384 lru_list) {
2385 obj = reg->obj;
2386 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002387
2388 if (obj_priv->pin_count)
2389 continue;
2390
2391 /* found one! */
2392 i = obj_priv->fence_reg;
2393 break;
2394 }
2395
2396 BUG_ON(i == I915_FENCE_REG_NONE);
2397
2398 /* We only have a reference on obj from the active list. put_fence_reg
2399 * might drop that one, causing a use-after-free in it. So hold a
2400 * private reference to obj like the other callers of put_fence_reg
2401 * (set_tiling ioctl) do. */
2402 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002403 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002404 drm_gem_object_unreference(obj);
2405 if (ret != 0)
2406 return ret;
2407
2408 return i;
2409}
2410
Jesse Barnesde151cf2008-11-12 10:03:55 -08002411/**
2412 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2413 * @obj: object to map through a fence reg
2414 *
2415 * When mapping objects through the GTT, userspace wants to be able to write
2416 * to them without having to worry about swizzling if the object is tiled.
2417 *
2418 * This function walks the fence regs looking for a free one for @obj,
2419 * stealing one if it can't find any.
2420 *
2421 * It then sets up the reg based on the object's properties: address, pitch
2422 * and tiling format.
2423 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002424int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002425i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2426 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002427{
2428 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002429 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002430 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002431 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002432 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002433
Eric Anholta09ba7f2009-08-29 12:49:51 -07002434 /* Just update our place in the LRU if our fence is getting used. */
2435 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002436 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2437 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002438 return 0;
2439 }
2440
Jesse Barnesde151cf2008-11-12 10:03:55 -08002441 switch (obj_priv->tiling_mode) {
2442 case I915_TILING_NONE:
2443 WARN(1, "allocating a fence for non-tiled object?\n");
2444 break;
2445 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002446 if (!obj_priv->stride)
2447 return -EINVAL;
2448 WARN((obj_priv->stride & (512 - 1)),
2449 "object 0x%08x is X tiled but has non-512B pitch\n",
2450 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002451 break;
2452 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002453 if (!obj_priv->stride)
2454 return -EINVAL;
2455 WARN((obj_priv->stride & (128 - 1)),
2456 "object 0x%08x is Y tiled but has non-128B pitch\n",
2457 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002458 break;
2459 }
2460
Chris Wilson2cf34d72010-09-14 13:03:28 +01002461 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002462 if (ret < 0)
2463 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002464
Daniel Vetterae3db242010-02-19 11:51:58 +01002465 obj_priv->fence_reg = ret;
2466 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002467 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002468
Jesse Barnesde151cf2008-11-12 10:03:55 -08002469 reg->obj = obj;
2470
Chris Wilsone259bef2010-09-17 00:32:02 +01002471 switch (INTEL_INFO(dev)->gen) {
2472 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002473 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002474 break;
2475 case 5:
2476 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002477 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002478 break;
2479 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002480 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002481 break;
2482 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002483 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002484 break;
2485 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002486
Daniel Vetterae3db242010-02-19 11:51:58 +01002487 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2488 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002489
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002490 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002491}
2492
2493/**
2494 * i915_gem_clear_fence_reg - clear out fence register info
2495 * @obj: object to clear
2496 *
2497 * Zeroes out the fence register itself and clears out the associated
2498 * data structures in dev_priv and obj_priv.
2499 */
2500static void
2501i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2502{
2503 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002504 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002505 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002506 struct drm_i915_fence_reg *reg =
2507 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002508 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002509
Chris Wilsone259bef2010-09-17 00:32:02 +01002510 switch (INTEL_INFO(dev)->gen) {
2511 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002512 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2513 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002514 break;
2515 case 5:
2516 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002517 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002518 break;
2519 case 3:
2520 if (obj_priv->fence_reg > 8)
2521 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002522 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002523 case 2:
2524 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002525
2526 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002527 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002528 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002529
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002530 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002531 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002532 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002533}
2534
Eric Anholt673a3942008-07-30 12:06:12 -07002535/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002536 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2537 * to the buffer to finish, and then resets the fence register.
2538 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002539 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002540 *
2541 * Zeroes out the fence register itself and clears out the associated
2542 * data structures in dev_priv and obj_priv.
2543 */
2544int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002545i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2546 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002547{
2548 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002549 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002550 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002551 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002552
2553 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2554 return 0;
2555
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002556 /* If we've changed tiling, GTT-mappings of the object
2557 * need to re-fault to ensure that the correct fence register
2558 * setup is in place.
2559 */
2560 i915_gem_release_mmap(obj);
2561
Chris Wilson52dc7d32009-06-06 09:46:01 +01002562 /* On the i915, GPU access to tiled buffers is via a fence,
2563 * therefore we must wait for any outstanding access to complete
2564 * before clearing the fence.
2565 */
Chris Wilson53640e12010-09-20 11:40:50 +01002566 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2567 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002568 int ret;
2569
Chris Wilson2cf34d72010-09-14 13:03:28 +01002570 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002571 if (ret)
2572 return ret;
2573
Chris Wilson2cf34d72010-09-14 13:03:28 +01002574 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002575 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002576 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002577
2578 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002579 }
2580
Daniel Vetter4a726612010-02-01 13:59:16 +01002581 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002582 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002583
2584 return 0;
2585}
2586
2587/**
Eric Anholt673a3942008-07-30 12:06:12 -07002588 * Finds free space in the GTT aperture and binds the object there.
2589 */
2590static int
2591i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2592{
2593 struct drm_device *dev = obj->dev;
2594 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002595 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002596 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002597 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002598 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002599
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002600 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002601 DRM_ERROR("Attempting to bind a purgeable object\n");
2602 return -EINVAL;
2603 }
2604
Eric Anholt673a3942008-07-30 12:06:12 -07002605 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002606 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002607 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002608 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2609 return -EINVAL;
2610 }
2611
Chris Wilson654fc602010-05-27 13:18:21 +01002612 /* If the object is bigger than the entire aperture, reject it early
2613 * before evicting everything in a vain attempt to find space.
2614 */
2615 if (obj->size > dev->gtt_total) {
2616 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2617 return -E2BIG;
2618 }
2619
Eric Anholt673a3942008-07-30 12:06:12 -07002620 search_free:
2621 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2622 obj->size, alignment, 0);
2623 if (free_space != NULL) {
2624 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2625 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002626 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002627 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002628 }
2629 if (obj_priv->gtt_space == NULL) {
2630 /* If the gtt is empty and we're still having trouble
2631 * fitting our object in, we're out of memory.
2632 */
2633#if WATCH_LRU
2634 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2635#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002636 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002637 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002638 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002639
Eric Anholt673a3942008-07-30 12:06:12 -07002640 goto search_free;
2641 }
2642
2643#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002644 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002645 obj->size, obj_priv->gtt_offset);
2646#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002647 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002648 if (ret) {
2649 drm_mm_put_block(obj_priv->gtt_space);
2650 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002651
2652 if (ret == -ENOMEM) {
2653 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002654 ret = i915_gem_evict_something(dev, obj->size,
2655 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002656 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002657 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002658 if (gfpmask) {
2659 gfpmask = 0;
2660 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002661 }
2662
2663 return ret;
2664 }
2665
2666 goto search_free;
2667 }
2668
Eric Anholt673a3942008-07-30 12:06:12 -07002669 return ret;
2670 }
2671
Eric Anholt673a3942008-07-30 12:06:12 -07002672 /* Create an AGP memory structure pointing at our pages, and bind it
2673 * into the GTT.
2674 */
2675 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002676 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002677 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002678 obj_priv->gtt_offset,
2679 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002680 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002681 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002682 drm_mm_put_block(obj_priv->gtt_space);
2683 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002684
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002685 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002686 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002687 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002688
2689 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002690 }
2691 atomic_inc(&dev->gtt_count);
2692 atomic_add(obj->size, &dev->gtt_memory);
2693
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002694 /* keep track of bounds object by adding it to the inactive list */
2695 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2696
Eric Anholt673a3942008-07-30 12:06:12 -07002697 /* Assert that the object is not currently in any GPU domain. As it
2698 * wasn't in the GTT, there shouldn't be any way it could have been in
2699 * a GPU cache
2700 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002701 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2702 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002703
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002704 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2705
Eric Anholt673a3942008-07-30 12:06:12 -07002706 return 0;
2707}
2708
2709void
2710i915_gem_clflush_object(struct drm_gem_object *obj)
2711{
Daniel Vetter23010e42010-03-08 13:35:02 +01002712 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002713
2714 /* If we don't have a page list set up, then we're not pinned
2715 * to GPU, and we can ignore the cache flush because it'll happen
2716 * again at bind time.
2717 */
Eric Anholt856fa192009-03-19 14:10:50 -07002718 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002719 return;
2720
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002721 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002722
Eric Anholt856fa192009-03-19 14:10:50 -07002723 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002724}
2725
Eric Anholte47c68e2008-11-14 13:35:19 -08002726/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002727static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002728i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2729 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002730{
2731 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002732 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002733
2734 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002735 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002736
2737 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002738 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002739 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002740 to_intel_bo(obj)->ring,
2741 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002742 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002743
2744 trace_i915_gem_object_change_domain(obj,
2745 obj->read_domains,
2746 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002747
2748 if (pipelined)
2749 return 0;
2750
Chris Wilson2cf34d72010-09-14 13:03:28 +01002751 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002752}
2753
2754/** Flushes the GTT write domain for the object if it's dirty. */
2755static void
2756i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2757{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002758 uint32_t old_write_domain;
2759
Eric Anholte47c68e2008-11-14 13:35:19 -08002760 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2761 return;
2762
2763 /* No actual flushing is required for the GTT write domain. Writes
2764 * to it immediately go to main memory as far as we know, so there's
2765 * no chipset flush. It also doesn't land in render cache.
2766 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002767 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002768 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002769
2770 trace_i915_gem_object_change_domain(obj,
2771 obj->read_domains,
2772 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002773}
2774
2775/** Flushes the CPU write domain for the object if it's dirty. */
2776static void
2777i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2778{
2779 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002780 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002781
2782 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2783 return;
2784
2785 i915_gem_clflush_object(obj);
2786 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002787 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002788 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002789
2790 trace_i915_gem_object_change_domain(obj,
2791 obj->read_domains,
2792 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002793}
2794
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002795/**
2796 * Moves a single object to the GTT read, and possibly write domain.
2797 *
2798 * This function returns when the move is complete, including waiting on
2799 * flushes to occur.
2800 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002801int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002802i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2803{
Daniel Vetter23010e42010-03-08 13:35:02 +01002804 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002805 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002806 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002807
Eric Anholt02354392008-11-26 13:58:13 -08002808 /* Not valid to be called on unbound objects. */
2809 if (obj_priv->gtt_space == NULL)
2810 return -EINVAL;
2811
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002812 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002813 if (ret != 0)
2814 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002815
Chris Wilson72133422010-09-13 23:56:38 +01002816 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002817
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002818 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002819 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002820 if (ret)
2821 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002822 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002823
Chris Wilson72133422010-09-13 23:56:38 +01002824 old_write_domain = obj->write_domain;
2825 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002826
2827 /* It should now be out of any other write domains, and we can update
2828 * the domain values for our changes.
2829 */
2830 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2831 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002832 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002833 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002834 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002835 obj_priv->dirty = 1;
2836 }
2837
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002838 trace_i915_gem_object_change_domain(obj,
2839 old_read_domains,
2840 old_write_domain);
2841
Eric Anholte47c68e2008-11-14 13:35:19 -08002842 return 0;
2843}
2844
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002845/*
2846 * Prepare buffer for display plane. Use uninterruptible for possible flush
2847 * wait, as in modesetting process we're not supposed to be interrupted.
2848 */
2849int
Chris Wilson48b956c2010-09-14 12:50:34 +01002850i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2851 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002852{
Daniel Vetter23010e42010-03-08 13:35:02 +01002853 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002854 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002855 int ret;
2856
2857 /* Not valid to be called on unbound objects. */
2858 if (obj_priv->gtt_space == NULL)
2859 return -EINVAL;
2860
Chris Wilson48b956c2010-09-14 12:50:34 +01002861 ret = i915_gem_object_flush_gpu_write_domain(obj, pipelined);
2862 if (ret)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002863 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002864
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002865 i915_gem_object_flush_cpu_write_domain(obj);
2866
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002867 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002868 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002869
2870 trace_i915_gem_object_change_domain(obj,
2871 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002872 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002873
2874 return 0;
2875}
2876
Eric Anholte47c68e2008-11-14 13:35:19 -08002877/**
2878 * Moves a single object to the CPU read, and possibly write domain.
2879 *
2880 * This function returns when the move is complete, including waiting on
2881 * flushes to occur.
2882 */
2883static int
2884i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2885{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002886 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002887 int ret;
2888
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002889 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002890 if (ret != 0)
2891 return ret;
2892
2893 i915_gem_object_flush_gtt_write_domain(obj);
2894
2895 /* If we have a partially-valid cache of the object in the CPU,
2896 * finish invalidating it and free the per-page flags.
2897 */
2898 i915_gem_object_set_to_full_cpu_read_domain(obj);
2899
Chris Wilson72133422010-09-13 23:56:38 +01002900 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002901 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002902 if (ret)
2903 return ret;
2904 }
2905
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002906 old_write_domain = obj->write_domain;
2907 old_read_domains = obj->read_domains;
2908
Eric Anholte47c68e2008-11-14 13:35:19 -08002909 /* Flush the CPU cache if it's still invalid. */
2910 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2911 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002912
2913 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2914 }
2915
2916 /* It should now be out of any other write domains, and we can update
2917 * the domain values for our changes.
2918 */
2919 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2920
2921 /* If we're writing through the CPU, then the GPU read domains will
2922 * need to be invalidated at next use.
2923 */
2924 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002925 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002926 obj->write_domain = I915_GEM_DOMAIN_CPU;
2927 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002928
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002929 trace_i915_gem_object_change_domain(obj,
2930 old_read_domains,
2931 old_write_domain);
2932
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002933 return 0;
2934}
2935
Eric Anholt673a3942008-07-30 12:06:12 -07002936/*
2937 * Set the next domain for the specified object. This
2938 * may not actually perform the necessary flushing/invaliding though,
2939 * as that may want to be batched with other set_domain operations
2940 *
2941 * This is (we hope) the only really tricky part of gem. The goal
2942 * is fairly simple -- track which caches hold bits of the object
2943 * and make sure they remain coherent. A few concrete examples may
2944 * help to explain how it works. For shorthand, we use the notation
2945 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2946 * a pair of read and write domain masks.
2947 *
2948 * Case 1: the batch buffer
2949 *
2950 * 1. Allocated
2951 * 2. Written by CPU
2952 * 3. Mapped to GTT
2953 * 4. Read by GPU
2954 * 5. Unmapped from GTT
2955 * 6. Freed
2956 *
2957 * Let's take these a step at a time
2958 *
2959 * 1. Allocated
2960 * Pages allocated from the kernel may still have
2961 * cache contents, so we set them to (CPU, CPU) always.
2962 * 2. Written by CPU (using pwrite)
2963 * The pwrite function calls set_domain (CPU, CPU) and
2964 * this function does nothing (as nothing changes)
2965 * 3. Mapped by GTT
2966 * This function asserts that the object is not
2967 * currently in any GPU-based read or write domains
2968 * 4. Read by GPU
2969 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2970 * As write_domain is zero, this function adds in the
2971 * current read domains (CPU+COMMAND, 0).
2972 * flush_domains is set to CPU.
2973 * invalidate_domains is set to COMMAND
2974 * clflush is run to get data out of the CPU caches
2975 * then i915_dev_set_domain calls i915_gem_flush to
2976 * emit an MI_FLUSH and drm_agp_chipset_flush
2977 * 5. Unmapped from GTT
2978 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2979 * flush_domains and invalidate_domains end up both zero
2980 * so no flushing/invalidating happens
2981 * 6. Freed
2982 * yay, done
2983 *
2984 * Case 2: The shared render buffer
2985 *
2986 * 1. Allocated
2987 * 2. Mapped to GTT
2988 * 3. Read/written by GPU
2989 * 4. set_domain to (CPU,CPU)
2990 * 5. Read/written by CPU
2991 * 6. Read/written by GPU
2992 *
2993 * 1. Allocated
2994 * Same as last example, (CPU, CPU)
2995 * 2. Mapped to GTT
2996 * Nothing changes (assertions find that it is not in the GPU)
2997 * 3. Read/written by GPU
2998 * execbuffer calls set_domain (RENDER, RENDER)
2999 * flush_domains gets CPU
3000 * invalidate_domains gets GPU
3001 * clflush (obj)
3002 * MI_FLUSH and drm_agp_chipset_flush
3003 * 4. set_domain (CPU, CPU)
3004 * flush_domains gets GPU
3005 * invalidate_domains gets CPU
3006 * wait_rendering (obj) to make sure all drawing is complete.
3007 * This will include an MI_FLUSH to get the data from GPU
3008 * to memory
3009 * clflush (obj) to invalidate the CPU cache
3010 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3011 * 5. Read/written by CPU
3012 * cache lines are loaded and dirtied
3013 * 6. Read written by GPU
3014 * Same as last GPU access
3015 *
3016 * Case 3: The constant buffer
3017 *
3018 * 1. Allocated
3019 * 2. Written by CPU
3020 * 3. Read by GPU
3021 * 4. Updated (written) by CPU again
3022 * 5. Read by GPU
3023 *
3024 * 1. Allocated
3025 * (CPU, CPU)
3026 * 2. Written by CPU
3027 * (CPU, CPU)
3028 * 3. Read by GPU
3029 * (CPU+RENDER, 0)
3030 * flush_domains = CPU
3031 * invalidate_domains = RENDER
3032 * clflush (obj)
3033 * MI_FLUSH
3034 * drm_agp_chipset_flush
3035 * 4. Updated (written) by CPU again
3036 * (CPU, CPU)
3037 * flush_domains = 0 (no previous write domain)
3038 * invalidate_domains = 0 (no new read domains)
3039 * 5. Read by GPU
3040 * (CPU+RENDER, 0)
3041 * flush_domains = CPU
3042 * invalidate_domains = RENDER
3043 * clflush (obj)
3044 * MI_FLUSH
3045 * drm_agp_chipset_flush
3046 */
Keith Packardc0d90822008-11-20 23:11:08 -08003047static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003048i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003049{
3050 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003051 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003052 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003053 uint32_t invalidate_domains = 0;
3054 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003055 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003056
Eric Anholt8b0e3782009-02-19 14:40:50 -08003057 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3058 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003059
Jesse Barnes652c3932009-08-17 13:31:43 -07003060 intel_mark_busy(dev, obj);
3061
Eric Anholt673a3942008-07-30 12:06:12 -07003062#if WATCH_BUF
3063 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3064 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08003065 obj->read_domains, obj->pending_read_domains,
3066 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003067#endif
3068 /*
3069 * If the object isn't moving to a new write domain,
3070 * let the object stay in multiple read domains
3071 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003072 if (obj->pending_write_domain == 0)
3073 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003074 else
3075 obj_priv->dirty = 1;
3076
3077 /*
3078 * Flush the current write domain if
3079 * the new read domains don't match. Invalidate
3080 * any read domains which differ from the old
3081 * write domain
3082 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003083 if (obj->write_domain &&
3084 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003085 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003086 invalidate_domains |=
3087 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003088 }
3089 /*
3090 * Invalidate any read caches which may have
3091 * stale data. That is, any new read domains.
3092 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003093 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003094 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3095#if WATCH_BUF
3096 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3097 __func__, flush_domains, invalidate_domains);
3098#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003099 i915_gem_clflush_object(obj);
3100 }
3101
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003102 old_read_domains = obj->read_domains;
3103
Eric Anholtefbeed92009-02-19 14:54:51 -08003104 /* The actual obj->write_domain will be updated with
3105 * pending_write_domain after we emit the accumulated flush for all
3106 * of our domain changes in execbuffers (which clears objects'
3107 * write_domains). So if we have a current write domain that we
3108 * aren't changing, set pending_write_domain to that.
3109 */
3110 if (flush_domains == 0 && obj->pending_write_domain == 0)
3111 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003112 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003113
3114 dev->invalidate_domains |= invalidate_domains;
3115 dev->flush_domains |= flush_domains;
Chris Wilson92204342010-09-18 11:02:01 +01003116 if (obj_priv->ring)
3117 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003118#if WATCH_BUF
3119 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3120 __func__,
3121 obj->read_domains, obj->write_domain,
3122 dev->invalidate_domains, dev->flush_domains);
3123#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003124
3125 trace_i915_gem_object_change_domain(obj,
3126 old_read_domains,
3127 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003128}
3129
3130/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003131 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003132 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003133 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3134 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3135 */
3136static void
3137i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3138{
Daniel Vetter23010e42010-03-08 13:35:02 +01003139 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003140
3141 if (!obj_priv->page_cpu_valid)
3142 return;
3143
3144 /* If we're partially in the CPU read domain, finish moving it in.
3145 */
3146 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3147 int i;
3148
3149 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3150 if (obj_priv->page_cpu_valid[i])
3151 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003152 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003153 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003154 }
3155
3156 /* Free the page_cpu_valid mappings which are now stale, whether
3157 * or not we've got I915_GEM_DOMAIN_CPU.
3158 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003159 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003160 obj_priv->page_cpu_valid = NULL;
3161}
3162
3163/**
3164 * Set the CPU read domain on a range of the object.
3165 *
3166 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3167 * not entirely valid. The page_cpu_valid member of the object flags which
3168 * pages have been flushed, and will be respected by
3169 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3170 * of the whole object.
3171 *
3172 * This function returns when the move is complete, including waiting on
3173 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003174 */
3175static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003176i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3177 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003178{
Daniel Vetter23010e42010-03-08 13:35:02 +01003179 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003180 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003181 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003182
Eric Anholte47c68e2008-11-14 13:35:19 -08003183 if (offset == 0 && size == obj->size)
3184 return i915_gem_object_set_to_cpu_domain(obj, 0);
3185
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003186 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003187 if (ret != 0)
3188 return ret;
3189 i915_gem_object_flush_gtt_write_domain(obj);
3190
3191 /* If we're already fully in the CPU read domain, we're done. */
3192 if (obj_priv->page_cpu_valid == NULL &&
3193 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003194 return 0;
3195
Eric Anholte47c68e2008-11-14 13:35:19 -08003196 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3197 * newly adding I915_GEM_DOMAIN_CPU
3198 */
Eric Anholt673a3942008-07-30 12:06:12 -07003199 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003200 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3201 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003202 if (obj_priv->page_cpu_valid == NULL)
3203 return -ENOMEM;
3204 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3205 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003206
3207 /* Flush the cache on any pages that are still invalid from the CPU's
3208 * perspective.
3209 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003210 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3211 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003212 if (obj_priv->page_cpu_valid[i])
3213 continue;
3214
Eric Anholt856fa192009-03-19 14:10:50 -07003215 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003216
3217 obj_priv->page_cpu_valid[i] = 1;
3218 }
3219
Eric Anholte47c68e2008-11-14 13:35:19 -08003220 /* It should now be out of any other write domains, and we can update
3221 * the domain values for our changes.
3222 */
3223 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3224
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003225 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003226 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3227
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003228 trace_i915_gem_object_change_domain(obj,
3229 old_read_domains,
3230 obj->write_domain);
3231
Eric Anholt673a3942008-07-30 12:06:12 -07003232 return 0;
3233}
3234
3235/**
Eric Anholt673a3942008-07-30 12:06:12 -07003236 * Pin an object to the GTT and evaluate the relocations landing in it.
3237 */
3238static int
3239i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3240 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003241 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003242 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003243{
3244 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003245 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003246 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003247 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003248 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003249 bool need_fence;
3250
3251 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3252 obj_priv->tiling_mode != I915_TILING_NONE;
3253
3254 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003255 if (need_fence &&
3256 !i915_gem_object_fence_offset_ok(obj,
3257 obj_priv->tiling_mode)) {
3258 ret = i915_gem_object_unbind(obj);
3259 if (ret)
3260 return ret;
3261 }
Eric Anholt673a3942008-07-30 12:06:12 -07003262
3263 /* Choose the GTT offset for our buffer and put it there. */
3264 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3265 if (ret)
3266 return ret;
3267
Jesse Barnes76446ca2009-12-17 22:05:42 -05003268 /*
3269 * Pre-965 chips need a fence register set up in order to
3270 * properly handle blits to/from tiled surfaces.
3271 */
3272 if (need_fence) {
Chris Wilson53640e12010-09-20 11:40:50 +01003273 ret = i915_gem_object_get_fence_reg(obj, true);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003274 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003275 i915_gem_object_unpin(obj);
3276 return ret;
3277 }
Chris Wilson53640e12010-09-20 11:40:50 +01003278
3279 dev_priv->fence_regs[obj_priv->fence_reg].gpu = true;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003280 }
3281
Eric Anholt673a3942008-07-30 12:06:12 -07003282 entry->offset = obj_priv->gtt_offset;
3283
Eric Anholt673a3942008-07-30 12:06:12 -07003284 /* Apply the relocations, using the GTT aperture to avoid cache
3285 * flushing requirements.
3286 */
3287 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003288 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003289 struct drm_gem_object *target_obj;
3290 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003291 uint32_t reloc_val, reloc_offset;
3292 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003293
Eric Anholt673a3942008-07-30 12:06:12 -07003294 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003295 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003296 if (target_obj == NULL) {
3297 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003298 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003299 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003300 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003301
Chris Wilson8542a0b2009-09-09 21:15:15 +01003302#if WATCH_RELOC
3303 DRM_INFO("%s: obj %p offset %08x target %d "
3304 "read %08x write %08x gtt %08x "
3305 "presumed %08x delta %08x\n",
3306 __func__,
3307 obj,
3308 (int) reloc->offset,
3309 (int) reloc->target_handle,
3310 (int) reloc->read_domains,
3311 (int) reloc->write_domain,
3312 (int) target_obj_priv->gtt_offset,
3313 (int) reloc->presumed_offset,
3314 reloc->delta);
3315#endif
3316
Eric Anholt673a3942008-07-30 12:06:12 -07003317 /* The target buffer should have appeared before us in the
3318 * exec_object list, so it should have a GTT space bound by now.
3319 */
3320 if (target_obj_priv->gtt_space == NULL) {
3321 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003322 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003323 drm_gem_object_unreference(target_obj);
3324 i915_gem_object_unpin(obj);
3325 return -EINVAL;
3326 }
3327
Chris Wilson8542a0b2009-09-09 21:15:15 +01003328 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003329 if (reloc->write_domain & (reloc->write_domain - 1)) {
3330 DRM_ERROR("reloc with multiple write domains: "
3331 "obj %p target %d offset %d "
3332 "read %08x write %08x",
3333 obj, reloc->target_handle,
3334 (int) reloc->offset,
3335 reloc->read_domains,
3336 reloc->write_domain);
3337 return -EINVAL;
3338 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003339 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3340 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3341 DRM_ERROR("reloc with read/write CPU domains: "
3342 "obj %p target %d offset %d "
3343 "read %08x write %08x",
3344 obj, reloc->target_handle,
3345 (int) reloc->offset,
3346 reloc->read_domains,
3347 reloc->write_domain);
3348 drm_gem_object_unreference(target_obj);
3349 i915_gem_object_unpin(obj);
3350 return -EINVAL;
3351 }
3352 if (reloc->write_domain && target_obj->pending_write_domain &&
3353 reloc->write_domain != target_obj->pending_write_domain) {
3354 DRM_ERROR("Write domain conflict: "
3355 "obj %p target %d offset %d "
3356 "new %08x old %08x\n",
3357 obj, reloc->target_handle,
3358 (int) reloc->offset,
3359 reloc->write_domain,
3360 target_obj->pending_write_domain);
3361 drm_gem_object_unreference(target_obj);
3362 i915_gem_object_unpin(obj);
3363 return -EINVAL;
3364 }
3365
3366 target_obj->pending_read_domains |= reloc->read_domains;
3367 target_obj->pending_write_domain |= reloc->write_domain;
3368
3369 /* If the relocation already has the right value in it, no
3370 * more work needs to be done.
3371 */
3372 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3373 drm_gem_object_unreference(target_obj);
3374 continue;
3375 }
3376
3377 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003378 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003379 DRM_ERROR("Relocation beyond object bounds: "
3380 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003381 obj, reloc->target_handle,
3382 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003383 drm_gem_object_unreference(target_obj);
3384 i915_gem_object_unpin(obj);
3385 return -EINVAL;
3386 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003387 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003388 DRM_ERROR("Relocation not 4-byte aligned: "
3389 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003390 obj, reloc->target_handle,
3391 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003392 drm_gem_object_unreference(target_obj);
3393 i915_gem_object_unpin(obj);
3394 return -EINVAL;
3395 }
3396
Chris Wilson8542a0b2009-09-09 21:15:15 +01003397 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003398 if (reloc->delta >= target_obj->size) {
3399 DRM_ERROR("Relocation beyond target object bounds: "
3400 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003401 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003402 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003403 drm_gem_object_unreference(target_obj);
3404 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003405 return -EINVAL;
3406 }
3407
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003408 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3409 if (ret != 0) {
3410 drm_gem_object_unreference(target_obj);
3411 i915_gem_object_unpin(obj);
3412 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003413 }
3414
3415 /* Map the page containing the relocation we're going to
3416 * perform.
3417 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003418 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003419 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3420 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003421 ~(PAGE_SIZE - 1)),
3422 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003423 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003424 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003425 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003426
3427#if WATCH_BUF
3428 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003429 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003430 readl(reloc_entry), reloc_val);
3431#endif
3432 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003433 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003434
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003435 /* The updated presumed offset for this entry will be
3436 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003437 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003438 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003439
3440 drm_gem_object_unreference(target_obj);
3441 }
3442
Eric Anholt673a3942008-07-30 12:06:12 -07003443#if WATCH_BUF
3444 if (0)
3445 i915_gem_dump_object(obj, 128, __func__, ~0);
3446#endif
3447 return 0;
3448}
3449
Eric Anholt673a3942008-07-30 12:06:12 -07003450/* Throttle our rendering by waiting until the ring has completed our requests
3451 * emitted over 20 msec ago.
3452 *
Eric Anholtb9624422009-06-03 07:27:35 +00003453 * Note that if we were to use the current jiffies each time around the loop,
3454 * we wouldn't escape the function with any frames outstanding if the time to
3455 * render a frame was over 20ms.
3456 *
Eric Anholt673a3942008-07-30 12:06:12 -07003457 * This should get us reasonable parallelism between CPU and GPU but also
3458 * relatively low latency when blocking on a particular request to finish.
3459 */
3460static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003461i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003462{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003463 struct drm_i915_private *dev_priv = dev->dev_private;
3464 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003465 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003466 struct drm_i915_gem_request *request;
3467 struct intel_ring_buffer *ring = NULL;
3468 u32 seqno = 0;
3469 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003470
Chris Wilson1c255952010-09-26 11:03:27 +01003471 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003472 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003473 if (time_after_eq(request->emitted_jiffies, recent_enough))
3474 break;
3475
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003476 ring = request->ring;
3477 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003478 }
Chris Wilson1c255952010-09-26 11:03:27 +01003479 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003480
3481 if (seqno == 0)
3482 return 0;
3483
3484 ret = 0;
3485 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3486 /* And wait for the seqno passing without holding any locks and
3487 * causing extra latency for others. This is safe as the irq
3488 * generation is designed to be run atomically and so is
3489 * lockless.
3490 */
3491 ring->user_irq_get(dev, ring);
3492 ret = wait_event_interruptible(ring->irq_queue,
3493 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3494 || atomic_read(&dev_priv->mm.wedged));
3495 ring->user_irq_put(dev, ring);
3496
3497 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3498 ret = -EIO;
3499 }
3500
3501 if (ret == 0)
3502 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003503
Eric Anholt673a3942008-07-30 12:06:12 -07003504 return ret;
3505}
3506
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003507static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003508i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003509 uint32_t buffer_count,
3510 struct drm_i915_gem_relocation_entry **relocs)
3511{
3512 uint32_t reloc_count = 0, reloc_index = 0, i;
3513 int ret;
3514
3515 *relocs = NULL;
3516 for (i = 0; i < buffer_count; i++) {
3517 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3518 return -EINVAL;
3519 reloc_count += exec_list[i].relocation_count;
3520 }
3521
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003522 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003523 if (*relocs == NULL) {
3524 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003525 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003526 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003527
3528 for (i = 0; i < buffer_count; i++) {
3529 struct drm_i915_gem_relocation_entry __user *user_relocs;
3530
3531 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3532
3533 ret = copy_from_user(&(*relocs)[reloc_index],
3534 user_relocs,
3535 exec_list[i].relocation_count *
3536 sizeof(**relocs));
3537 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003538 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003539 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003540 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003541 }
3542
3543 reloc_index += exec_list[i].relocation_count;
3544 }
3545
Florian Mickler2bc43b52009-04-06 22:55:41 +02003546 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003547}
3548
3549static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003550i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003551 uint32_t buffer_count,
3552 struct drm_i915_gem_relocation_entry *relocs)
3553{
3554 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003555 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003556
Chris Wilson93533c22010-01-31 10:40:48 +00003557 if (relocs == NULL)
3558 return 0;
3559
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003560 for (i = 0; i < buffer_count; i++) {
3561 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003562 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003563
3564 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3565
Florian Mickler2bc43b52009-04-06 22:55:41 +02003566 unwritten = copy_to_user(user_relocs,
3567 &relocs[reloc_count],
3568 exec_list[i].relocation_count *
3569 sizeof(*relocs));
3570
3571 if (unwritten) {
3572 ret = -EFAULT;
3573 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003574 }
3575
3576 reloc_count += exec_list[i].relocation_count;
3577 }
3578
Florian Mickler2bc43b52009-04-06 22:55:41 +02003579err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003580 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003581
3582 return ret;
3583}
3584
Chris Wilson83d60792009-06-06 09:45:57 +01003585static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003586i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003587 uint64_t exec_offset)
3588{
3589 uint32_t exec_start, exec_len;
3590
3591 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3592 exec_len = (uint32_t) exec->batch_len;
3593
3594 if ((exec_start | exec_len) & 0x7)
3595 return -EINVAL;
3596
3597 if (!exec_start)
3598 return -EINVAL;
3599
3600 return 0;
3601}
3602
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003603static int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003604i915_gem_wait_for_pending_flip(struct drm_device *dev,
3605 struct drm_gem_object **object_list,
3606 int count)
3607{
3608 drm_i915_private_t *dev_priv = dev->dev_private;
3609 struct drm_i915_gem_object *obj_priv;
3610 DEFINE_WAIT(wait);
3611 int i, ret = 0;
3612
3613 for (;;) {
3614 prepare_to_wait(&dev_priv->pending_flip_queue,
3615 &wait, TASK_INTERRUPTIBLE);
3616 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003617 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003618 if (atomic_read(&obj_priv->pending_flip) > 0)
3619 break;
3620 }
3621 if (i == count)
3622 break;
3623
3624 if (!signal_pending(current)) {
3625 mutex_unlock(&dev->struct_mutex);
3626 schedule();
3627 mutex_lock(&dev->struct_mutex);
3628 continue;
3629 }
3630 ret = -ERESTARTSYS;
3631 break;
3632 }
3633 finish_wait(&dev_priv->pending_flip_queue, &wait);
3634
3635 return ret;
3636}
3637
Chris Wilson8dc5d142010-08-12 12:36:12 +01003638static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003639i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3640 struct drm_file *file_priv,
3641 struct drm_i915_gem_execbuffer2 *args,
3642 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003643{
3644 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003645 struct drm_gem_object **object_list = NULL;
3646 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003647 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003648 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003649 struct drm_i915_gem_relocation_entry *relocs = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003650 struct drm_i915_gem_request *request = NULL;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003651 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003652 uint64_t exec_offset;
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003653 uint32_t reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003654 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003655
Zou Nan hai852835f2010-05-21 09:08:56 +08003656 struct intel_ring_buffer *ring = NULL;
3657
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003658 ret = i915_gem_check_is_wedged(dev);
3659 if (ret)
3660 return ret;
3661
Eric Anholt673a3942008-07-30 12:06:12 -07003662#if WATCH_EXEC
3663 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3664 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3665#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003666 if (args->flags & I915_EXEC_BSD) {
3667 if (!HAS_BSD(dev)) {
3668 DRM_ERROR("execbuf with wrong flag\n");
3669 return -EINVAL;
3670 }
3671 ring = &dev_priv->bsd_ring;
3672 } else {
3673 ring = &dev_priv->render_ring;
3674 }
3675
Eric Anholt4f481ed2008-09-10 14:22:49 -07003676 if (args->buffer_count < 1) {
3677 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3678 return -EINVAL;
3679 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003680 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003681 if (object_list == NULL) {
3682 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003683 args->buffer_count);
3684 ret = -ENOMEM;
3685 goto pre_mutex_err;
3686 }
Eric Anholt673a3942008-07-30 12:06:12 -07003687
Eric Anholt201361a2009-03-11 12:30:04 -07003688 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003689 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3690 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003691 if (cliprects == NULL) {
3692 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003693 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003694 }
Eric Anholt201361a2009-03-11 12:30:04 -07003695
3696 ret = copy_from_user(cliprects,
3697 (struct drm_clip_rect __user *)
3698 (uintptr_t) args->cliprects_ptr,
3699 sizeof(*cliprects) * args->num_cliprects);
3700 if (ret != 0) {
3701 DRM_ERROR("copy %d cliprects failed: %d\n",
3702 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003703 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003704 goto pre_mutex_err;
3705 }
3706 }
3707
Chris Wilson8dc5d142010-08-12 12:36:12 +01003708 request = kzalloc(sizeof(*request), GFP_KERNEL);
3709 if (request == NULL) {
3710 ret = -ENOMEM;
3711 goto pre_mutex_err;
3712 }
3713
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003714 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3715 &relocs);
3716 if (ret != 0)
3717 goto pre_mutex_err;
3718
Chris Wilson76c1dec2010-09-25 11:22:51 +01003719 ret = i915_mutex_lock_interruptible(dev);
3720 if (ret)
3721 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003722
3723 i915_verify_inactive(dev, __FILE__, __LINE__);
3724
Eric Anholt673a3942008-07-30 12:06:12 -07003725 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003726 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003727 ret = -EBUSY;
3728 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003729 }
3730
Keith Packardac94a962008-11-20 23:30:27 -08003731 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003732 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003733 for (i = 0; i < args->buffer_count; i++) {
3734 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3735 exec_list[i].handle);
3736 if (object_list[i] == NULL) {
3737 DRM_ERROR("Invalid object handle %d at index %d\n",
3738 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003739 /* prevent error path from reading uninitialized data */
3740 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003741 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003742 goto err;
3743 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003744
Daniel Vetter23010e42010-03-08 13:35:02 +01003745 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003746 if (obj_priv->in_execbuffer) {
3747 DRM_ERROR("Object %p appears more than once in object list\n",
3748 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003749 /* prevent error path from reading uninitialized data */
3750 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003751 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003752 goto err;
3753 }
3754 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003755 flips += atomic_read(&obj_priv->pending_flip);
3756 }
3757
3758 if (flips > 0) {
3759 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3760 args->buffer_count);
3761 if (ret)
3762 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003763 }
Eric Anholt673a3942008-07-30 12:06:12 -07003764
Keith Packardac94a962008-11-20 23:30:27 -08003765 /* Pin and relocate */
3766 for (pin_tries = 0; ; pin_tries++) {
3767 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003768 reloc_index = 0;
3769
Keith Packardac94a962008-11-20 23:30:27 -08003770 for (i = 0; i < args->buffer_count; i++) {
3771 object_list[i]->pending_read_domains = 0;
3772 object_list[i]->pending_write_domain = 0;
3773 ret = i915_gem_object_pin_and_relocate(object_list[i],
3774 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003775 &exec_list[i],
3776 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003777 if (ret)
3778 break;
3779 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003780 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003781 }
3782 /* success */
3783 if (ret == 0)
3784 break;
3785
3786 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003787 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003788 if (ret != -ERESTARTSYS) {
3789 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003790 int num_fences = 0;
3791 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003792 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003793
Chris Wilson07f73f62009-09-14 16:50:30 +01003794 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003795 num_fences +=
3796 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3797 obj_priv->tiling_mode != I915_TILING_NONE;
3798 }
3799 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003800 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003801 total_size, num_fences,
3802 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003803 DRM_ERROR("%d objects [%d pinned], "
3804 "%d object bytes [%d pinned], "
3805 "%d/%d gtt bytes\n",
3806 atomic_read(&dev->object_count),
3807 atomic_read(&dev->pin_count),
3808 atomic_read(&dev->object_memory),
3809 atomic_read(&dev->pin_memory),
3810 atomic_read(&dev->gtt_memory),
3811 dev->gtt_total);
3812 }
Eric Anholt673a3942008-07-30 12:06:12 -07003813 goto err;
3814 }
Keith Packardac94a962008-11-20 23:30:27 -08003815
3816 /* unpin all of our buffers */
3817 for (i = 0; i < pinned; i++)
3818 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003819 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003820
3821 /* evict everyone we can from the aperture */
3822 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003823 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003824 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003825 }
3826
3827 /* Set the pending read domains for the batch buffer to COMMAND */
3828 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003829 if (batch_obj->pending_write_domain) {
3830 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3831 ret = -EINVAL;
3832 goto err;
3833 }
3834 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003835
Chris Wilson83d60792009-06-06 09:45:57 +01003836 /* Sanity check the batch buffer, prior to moving objects */
3837 exec_offset = exec_list[args->buffer_count - 1].offset;
3838 ret = i915_gem_check_execbuffer (args, exec_offset);
3839 if (ret != 0) {
3840 DRM_ERROR("execbuf with invalid offset/length\n");
3841 goto err;
3842 }
3843
Eric Anholt673a3942008-07-30 12:06:12 -07003844 i915_verify_inactive(dev, __FILE__, __LINE__);
3845
Keith Packard646f0f62008-11-20 23:23:03 -08003846 /* Zero the global flush/invalidate flags. These
3847 * will be modified as new domains are computed
3848 * for each object
3849 */
3850 dev->invalidate_domains = 0;
3851 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003852 dev_priv->mm.flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003853
Eric Anholt673a3942008-07-30 12:06:12 -07003854 for (i = 0; i < args->buffer_count; i++) {
3855 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003856
Keith Packard646f0f62008-11-20 23:23:03 -08003857 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003858 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003859 }
3860
3861 i915_verify_inactive(dev, __FILE__, __LINE__);
3862
Keith Packard646f0f62008-11-20 23:23:03 -08003863 if (dev->invalidate_domains | dev->flush_domains) {
3864#if WATCH_EXEC
3865 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3866 __func__,
3867 dev->invalidate_domains,
3868 dev->flush_domains);
3869#endif
Chris Wilsonc78ec302010-09-20 12:50:23 +01003870 i915_gem_flush(dev, file_priv,
Keith Packard646f0f62008-11-20 23:23:03 -08003871 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003872 dev->flush_domains,
3873 dev_priv->mm.flush_rings);
Daniel Vettera6910432010-02-02 17:08:37 +01003874 }
3875
Eric Anholtefbeed92009-02-19 14:54:51 -08003876 for (i = 0; i < args->buffer_count; i++) {
3877 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003878 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003879 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003880
3881 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003882 if (obj->write_domain)
3883 list_move_tail(&obj_priv->gpu_write_list,
3884 &dev_priv->mm.gpu_write_list);
3885 else
3886 list_del_init(&obj_priv->gpu_write_list);
3887
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003888 trace_i915_gem_object_change_domain(obj,
3889 obj->read_domains,
3890 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003891 }
3892
Eric Anholt673a3942008-07-30 12:06:12 -07003893 i915_verify_inactive(dev, __FILE__, __LINE__);
3894
3895#if WATCH_COHERENCY
3896 for (i = 0; i < args->buffer_count; i++) {
3897 i915_gem_object_check_coherency(object_list[i],
3898 exec_list[i].handle);
3899 }
3900#endif
3901
Eric Anholt673a3942008-07-30 12:06:12 -07003902#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003903 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003904 args->batch_len,
3905 __func__,
3906 ~0);
3907#endif
3908
Eric Anholt673a3942008-07-30 12:06:12 -07003909 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003910 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3911 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003912 if (ret) {
3913 DRM_ERROR("dispatch failed %d\n", ret);
3914 goto err;
3915 }
3916
3917 /*
3918 * Ensure that the commands in the batch buffer are
3919 * finished before the interrupt fires
3920 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003921 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003922
3923 i915_verify_inactive(dev, __FILE__, __LINE__);
3924
Daniel Vetter617dbe22010-02-11 22:16:02 +01003925 for (i = 0; i < args->buffer_count; i++) {
3926 struct drm_gem_object *obj = object_list[i];
3927 obj_priv = to_intel_bo(obj);
3928
3929 i915_gem_object_move_to_active(obj, ring);
3930#if WATCH_LRU
3931 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3932#endif
3933 }
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003934 i915_add_request(dev, file_priv, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003935 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003936
Eric Anholt673a3942008-07-30 12:06:12 -07003937#if WATCH_LRU
3938 i915_dump_lru(dev, __func__);
3939#endif
3940
3941 i915_verify_inactive(dev, __FILE__, __LINE__);
3942
Eric Anholt673a3942008-07-30 12:06:12 -07003943err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003944 for (i = 0; i < pinned; i++)
3945 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003946
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003947 for (i = 0; i < args->buffer_count; i++) {
3948 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003949 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003950 obj_priv->in_execbuffer = false;
3951 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003952 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003953 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003954
Eric Anholt673a3942008-07-30 12:06:12 -07003955 mutex_unlock(&dev->struct_mutex);
3956
Chris Wilson93533c22010-01-31 10:40:48 +00003957pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003958 /* Copy the updated relocations out regardless of current error
3959 * state. Failure to update the relocs would mean that the next
3960 * time userland calls execbuf, it would do so with presumed offset
3961 * state that didn't match the actual object state.
3962 */
3963 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3964 relocs);
3965 if (ret2 != 0) {
3966 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3967
3968 if (ret == 0)
3969 ret = ret2;
3970 }
3971
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003972 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003973 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003974 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003975
3976 return ret;
3977}
3978
Jesse Barnes76446ca2009-12-17 22:05:42 -05003979/*
3980 * Legacy execbuffer just creates an exec2 list from the original exec object
3981 * list array and passes it to the real function.
3982 */
3983int
3984i915_gem_execbuffer(struct drm_device *dev, void *data,
3985 struct drm_file *file_priv)
3986{
3987 struct drm_i915_gem_execbuffer *args = data;
3988 struct drm_i915_gem_execbuffer2 exec2;
3989 struct drm_i915_gem_exec_object *exec_list = NULL;
3990 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3991 int ret, i;
3992
3993#if WATCH_EXEC
3994 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3995 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3996#endif
3997
3998 if (args->buffer_count < 1) {
3999 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4000 return -EINVAL;
4001 }
4002
4003 /* Copy in the exec list from userland */
4004 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4005 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4006 if (exec_list == NULL || exec2_list == NULL) {
4007 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4008 args->buffer_count);
4009 drm_free_large(exec_list);
4010 drm_free_large(exec2_list);
4011 return -ENOMEM;
4012 }
4013 ret = copy_from_user(exec_list,
4014 (struct drm_i915_relocation_entry __user *)
4015 (uintptr_t) args->buffers_ptr,
4016 sizeof(*exec_list) * args->buffer_count);
4017 if (ret != 0) {
4018 DRM_ERROR("copy %d exec entries failed %d\n",
4019 args->buffer_count, ret);
4020 drm_free_large(exec_list);
4021 drm_free_large(exec2_list);
4022 return -EFAULT;
4023 }
4024
4025 for (i = 0; i < args->buffer_count; i++) {
4026 exec2_list[i].handle = exec_list[i].handle;
4027 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4028 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4029 exec2_list[i].alignment = exec_list[i].alignment;
4030 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004031 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004032 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4033 else
4034 exec2_list[i].flags = 0;
4035 }
4036
4037 exec2.buffers_ptr = args->buffers_ptr;
4038 exec2.buffer_count = args->buffer_count;
4039 exec2.batch_start_offset = args->batch_start_offset;
4040 exec2.batch_len = args->batch_len;
4041 exec2.DR1 = args->DR1;
4042 exec2.DR4 = args->DR4;
4043 exec2.num_cliprects = args->num_cliprects;
4044 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004045 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004046
4047 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4048 if (!ret) {
4049 /* Copy the new buffer offsets back to the user's exec list. */
4050 for (i = 0; i < args->buffer_count; i++)
4051 exec_list[i].offset = exec2_list[i].offset;
4052 /* ... and back out to userspace */
4053 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4054 (uintptr_t) args->buffers_ptr,
4055 exec_list,
4056 sizeof(*exec_list) * args->buffer_count);
4057 if (ret) {
4058 ret = -EFAULT;
4059 DRM_ERROR("failed to copy %d exec entries "
4060 "back to user (%d)\n",
4061 args->buffer_count, ret);
4062 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004063 }
4064
4065 drm_free_large(exec_list);
4066 drm_free_large(exec2_list);
4067 return ret;
4068}
4069
4070int
4071i915_gem_execbuffer2(struct drm_device *dev, void *data,
4072 struct drm_file *file_priv)
4073{
4074 struct drm_i915_gem_execbuffer2 *args = data;
4075 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4076 int ret;
4077
4078#if WATCH_EXEC
4079 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4080 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4081#endif
4082
4083 if (args->buffer_count < 1) {
4084 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4085 return -EINVAL;
4086 }
4087
4088 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4089 if (exec2_list == NULL) {
4090 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4091 args->buffer_count);
4092 return -ENOMEM;
4093 }
4094 ret = copy_from_user(exec2_list,
4095 (struct drm_i915_relocation_entry __user *)
4096 (uintptr_t) args->buffers_ptr,
4097 sizeof(*exec2_list) * args->buffer_count);
4098 if (ret != 0) {
4099 DRM_ERROR("copy %d exec entries failed %d\n",
4100 args->buffer_count, ret);
4101 drm_free_large(exec2_list);
4102 return -EFAULT;
4103 }
4104
4105 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4106 if (!ret) {
4107 /* Copy the new buffer offsets back to the user's exec list. */
4108 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4109 (uintptr_t) args->buffers_ptr,
4110 exec2_list,
4111 sizeof(*exec2_list) * args->buffer_count);
4112 if (ret) {
4113 ret = -EFAULT;
4114 DRM_ERROR("failed to copy %d exec entries "
4115 "back to user (%d)\n",
4116 args->buffer_count, ret);
4117 }
4118 }
4119
4120 drm_free_large(exec2_list);
4121 return ret;
4122}
4123
Eric Anholt673a3942008-07-30 12:06:12 -07004124int
4125i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4126{
4127 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004128 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004129 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004130 int ret;
4131
Daniel Vetter778c3542010-05-13 11:49:44 +02004132 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4133
Eric Anholt673a3942008-07-30 12:06:12 -07004134 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004135
4136 if (obj_priv->gtt_space != NULL) {
4137 if (alignment == 0)
4138 alignment = i915_gem_get_gtt_alignment(obj);
4139 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004140 WARN(obj_priv->pin_count,
4141 "bo is already pinned with incorrect alignment:"
4142 " offset=%x, req.alignment=%x\n",
4143 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004144 ret = i915_gem_object_unbind(obj);
4145 if (ret)
4146 return ret;
4147 }
4148 }
4149
Eric Anholt673a3942008-07-30 12:06:12 -07004150 if (obj_priv->gtt_space == NULL) {
4151 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004152 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004153 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004154 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004155
Eric Anholt673a3942008-07-30 12:06:12 -07004156 obj_priv->pin_count++;
4157
4158 /* If the object is not active and not pending a flush,
4159 * remove it from the inactive list
4160 */
4161 if (obj_priv->pin_count == 1) {
4162 atomic_inc(&dev->pin_count);
4163 atomic_add(obj->size, &dev->pin_memory);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004164 if (!obj_priv->active)
4165 list_move_tail(&obj_priv->list,
4166 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004167 }
4168 i915_verify_inactive(dev, __FILE__, __LINE__);
4169
4170 return 0;
4171}
4172
4173void
4174i915_gem_object_unpin(struct drm_gem_object *obj)
4175{
4176 struct drm_device *dev = obj->dev;
4177 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004178 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004179
4180 i915_verify_inactive(dev, __FILE__, __LINE__);
4181 obj_priv->pin_count--;
4182 BUG_ON(obj_priv->pin_count < 0);
4183 BUG_ON(obj_priv->gtt_space == NULL);
4184
4185 /* If the object is no longer pinned, and is
4186 * neither active nor being flushed, then stick it on
4187 * the inactive list
4188 */
4189 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004190 if (!obj_priv->active)
Eric Anholt673a3942008-07-30 12:06:12 -07004191 list_move_tail(&obj_priv->list,
4192 &dev_priv->mm.inactive_list);
4193 atomic_dec(&dev->pin_count);
4194 atomic_sub(obj->size, &dev->pin_memory);
4195 }
4196 i915_verify_inactive(dev, __FILE__, __LINE__);
4197}
4198
4199int
4200i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4201 struct drm_file *file_priv)
4202{
4203 struct drm_i915_gem_pin *args = data;
4204 struct drm_gem_object *obj;
4205 struct drm_i915_gem_object *obj_priv;
4206 int ret;
4207
Eric Anholt673a3942008-07-30 12:06:12 -07004208 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4209 if (obj == NULL) {
4210 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4211 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004212 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004213 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004214 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004215
Chris Wilson76c1dec2010-09-25 11:22:51 +01004216 ret = i915_mutex_lock_interruptible(dev);
4217 if (ret) {
4218 drm_gem_object_unreference_unlocked(obj);
4219 return ret;
4220 }
4221
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004222 if (obj_priv->madv != I915_MADV_WILLNEED) {
4223 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004224 drm_gem_object_unreference(obj);
4225 mutex_unlock(&dev->struct_mutex);
4226 return -EINVAL;
4227 }
4228
Jesse Barnes79e53942008-11-07 14:24:08 -08004229 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4230 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4231 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004232 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004233 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004234 return -EINVAL;
4235 }
4236
4237 obj_priv->user_pin_count++;
4238 obj_priv->pin_filp = file_priv;
4239 if (obj_priv->user_pin_count == 1) {
4240 ret = i915_gem_object_pin(obj, args->alignment);
4241 if (ret != 0) {
4242 drm_gem_object_unreference(obj);
4243 mutex_unlock(&dev->struct_mutex);
4244 return ret;
4245 }
Eric Anholt673a3942008-07-30 12:06:12 -07004246 }
4247
4248 /* XXX - flush the CPU caches for pinned objects
4249 * as the X server doesn't manage domains yet
4250 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004251 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004252 args->offset = obj_priv->gtt_offset;
4253 drm_gem_object_unreference(obj);
4254 mutex_unlock(&dev->struct_mutex);
4255
4256 return 0;
4257}
4258
4259int
4260i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4261 struct drm_file *file_priv)
4262{
4263 struct drm_i915_gem_pin *args = data;
4264 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004265 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004266 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004267
4268 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4269 if (obj == NULL) {
4270 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4271 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004272 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004273 }
4274
Daniel Vetter23010e42010-03-08 13:35:02 +01004275 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004276
4277 ret = i915_mutex_lock_interruptible(dev);
4278 if (ret) {
4279 drm_gem_object_unreference_unlocked(obj);
4280 return ret;
4281 }
4282
Jesse Barnes79e53942008-11-07 14:24:08 -08004283 if (obj_priv->pin_filp != file_priv) {
4284 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4285 args->handle);
4286 drm_gem_object_unreference(obj);
4287 mutex_unlock(&dev->struct_mutex);
4288 return -EINVAL;
4289 }
4290 obj_priv->user_pin_count--;
4291 if (obj_priv->user_pin_count == 0) {
4292 obj_priv->pin_filp = NULL;
4293 i915_gem_object_unpin(obj);
4294 }
Eric Anholt673a3942008-07-30 12:06:12 -07004295
4296 drm_gem_object_unreference(obj);
4297 mutex_unlock(&dev->struct_mutex);
4298 return 0;
4299}
4300
4301int
4302i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4303 struct drm_file *file_priv)
4304{
4305 struct drm_i915_gem_busy *args = data;
4306 struct drm_gem_object *obj;
4307 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004308 int ret;
4309
Eric Anholt673a3942008-07-30 12:06:12 -07004310 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4311 if (obj == NULL) {
4312 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4313 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004314 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004315 }
4316
Chris Wilson76c1dec2010-09-25 11:22:51 +01004317 ret = i915_mutex_lock_interruptible(dev);
4318 if (ret) {
4319 drm_gem_object_unreference_unlocked(obj);
4320 return ret;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004321 }
4322
Chris Wilson0be555b2010-08-04 15:36:30 +01004323 /* Count all active objects as busy, even if they are currently not used
4324 * by the gpu. Users of this interface expect objects to eventually
4325 * become non-busy without any further actions, therefore emit any
4326 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004327 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004328 obj_priv = to_intel_bo(obj);
4329 args->busy = obj_priv->active;
4330 if (args->busy) {
4331 /* Unconditionally flush objects, even when the gpu still uses this
4332 * object. Userspace calling this function indicates that it wants to
4333 * use this buffer rather sooner than later, so issuing the required
4334 * flush earlier is beneficial.
4335 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004336 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4337 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004338 obj_priv->ring,
4339 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004340
4341 /* Update the active list for the hardware's current position.
4342 * Otherwise this only updates on a delayed timer or when irqs
4343 * are actually unmasked, and our working set ends up being
4344 * larger than required.
4345 */
4346 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4347
4348 args->busy = obj_priv->active;
4349 }
Eric Anholt673a3942008-07-30 12:06:12 -07004350
4351 drm_gem_object_unreference(obj);
4352 mutex_unlock(&dev->struct_mutex);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004353 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004354}
4355
4356int
4357i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4358 struct drm_file *file_priv)
4359{
4360 return i915_gem_ring_throttle(dev, file_priv);
4361}
4362
Chris Wilson3ef94da2009-09-14 16:50:29 +01004363int
4364i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4365 struct drm_file *file_priv)
4366{
4367 struct drm_i915_gem_madvise *args = data;
4368 struct drm_gem_object *obj;
4369 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004370 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004371
4372 switch (args->madv) {
4373 case I915_MADV_DONTNEED:
4374 case I915_MADV_WILLNEED:
4375 break;
4376 default:
4377 return -EINVAL;
4378 }
4379
4380 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4381 if (obj == NULL) {
4382 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4383 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004384 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004385 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004386 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004387
Chris Wilson76c1dec2010-09-25 11:22:51 +01004388 ret = i915_mutex_lock_interruptible(dev);
4389 if (ret) {
4390 drm_gem_object_unreference_unlocked(obj);
4391 return ret;
4392 }
4393
Chris Wilson3ef94da2009-09-14 16:50:29 +01004394 if (obj_priv->pin_count) {
4395 drm_gem_object_unreference(obj);
4396 mutex_unlock(&dev->struct_mutex);
4397
4398 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4399 return -EINVAL;
4400 }
4401
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004402 if (obj_priv->madv != __I915_MADV_PURGED)
4403 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004404
Chris Wilson2d7ef392009-09-20 23:13:10 +01004405 /* if the object is no longer bound, discard its backing storage */
4406 if (i915_gem_object_is_purgeable(obj_priv) &&
4407 obj_priv->gtt_space == NULL)
4408 i915_gem_object_truncate(obj);
4409
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004410 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4411
Chris Wilson3ef94da2009-09-14 16:50:29 +01004412 drm_gem_object_unreference(obj);
4413 mutex_unlock(&dev->struct_mutex);
4414
4415 return 0;
4416}
4417
Daniel Vetterac52bc52010-04-09 19:05:06 +00004418struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4419 size_t size)
4420{
Daniel Vetterc397b902010-04-09 19:05:07 +00004421 struct drm_i915_gem_object *obj;
4422
4423 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4424 if (obj == NULL)
4425 return NULL;
4426
4427 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4428 kfree(obj);
4429 return NULL;
4430 }
4431
4432 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4433 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4434
4435 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004436 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004437 obj->fence_reg = I915_FENCE_REG_NONE;
4438 INIT_LIST_HEAD(&obj->list);
4439 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004440 obj->madv = I915_MADV_WILLNEED;
4441
4442 trace_i915_gem_object_create(&obj->base);
4443
4444 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004445}
4446
Eric Anholt673a3942008-07-30 12:06:12 -07004447int i915_gem_init_object(struct drm_gem_object *obj)
4448{
Daniel Vetterc397b902010-04-09 19:05:07 +00004449 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004450
Eric Anholt673a3942008-07-30 12:06:12 -07004451 return 0;
4452}
4453
Chris Wilsonbe726152010-07-23 23:18:50 +01004454static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4455{
4456 struct drm_device *dev = obj->dev;
4457 drm_i915_private_t *dev_priv = dev->dev_private;
4458 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4459 int ret;
4460
4461 ret = i915_gem_object_unbind(obj);
4462 if (ret == -ERESTARTSYS) {
4463 list_move(&obj_priv->list,
4464 &dev_priv->mm.deferred_free_list);
4465 return;
4466 }
4467
4468 if (obj_priv->mmap_offset)
4469 i915_gem_free_mmap_offset(obj);
4470
4471 drm_gem_object_release(obj);
4472
4473 kfree(obj_priv->page_cpu_valid);
4474 kfree(obj_priv->bit_17);
4475 kfree(obj_priv);
4476}
4477
Eric Anholt673a3942008-07-30 12:06:12 -07004478void i915_gem_free_object(struct drm_gem_object *obj)
4479{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004480 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004481 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004482
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004483 trace_i915_gem_object_destroy(obj);
4484
Eric Anholt673a3942008-07-30 12:06:12 -07004485 while (obj_priv->pin_count > 0)
4486 i915_gem_object_unpin(obj);
4487
Dave Airlie71acb5e2008-12-30 20:31:46 +10004488 if (obj_priv->phys_obj)
4489 i915_gem_detach_phys_object(dev, obj);
4490
Chris Wilsonbe726152010-07-23 23:18:50 +01004491 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004492}
4493
Jesse Barnes5669fca2009-02-17 15:13:31 -08004494int
Eric Anholt673a3942008-07-30 12:06:12 -07004495i915_gem_idle(struct drm_device *dev)
4496{
4497 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004498 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004499
Keith Packard6dbe2772008-10-14 21:41:13 -07004500 mutex_lock(&dev->struct_mutex);
4501
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004502 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004503 (dev_priv->render_ring.gem_object == NULL) ||
4504 (HAS_BSD(dev) &&
4505 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004506 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004507 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004508 }
Eric Anholt673a3942008-07-30 12:06:12 -07004509
Chris Wilson29105cc2010-01-07 10:39:13 +00004510 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004511 if (ret) {
4512 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004513 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004514 }
Eric Anholt673a3942008-07-30 12:06:12 -07004515
Chris Wilson29105cc2010-01-07 10:39:13 +00004516 /* Under UMS, be paranoid and evict. */
4517 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004518 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004519 if (ret) {
4520 mutex_unlock(&dev->struct_mutex);
4521 return ret;
4522 }
4523 }
4524
4525 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4526 * We need to replace this with a semaphore, or something.
4527 * And not confound mm.suspended!
4528 */
4529 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004530 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004531
4532 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004533 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004534
Keith Packard6dbe2772008-10-14 21:41:13 -07004535 mutex_unlock(&dev->struct_mutex);
4536
Chris Wilson29105cc2010-01-07 10:39:13 +00004537 /* Cancel the retire work handler, which should be idle now. */
4538 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4539
Eric Anholt673a3942008-07-30 12:06:12 -07004540 return 0;
4541}
4542
Jesse Barnese552eb72010-04-21 11:39:23 -07004543/*
4544 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4545 * over cache flushing.
4546 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004547static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004548i915_gem_init_pipe_control(struct drm_device *dev)
4549{
4550 drm_i915_private_t *dev_priv = dev->dev_private;
4551 struct drm_gem_object *obj;
4552 struct drm_i915_gem_object *obj_priv;
4553 int ret;
4554
Eric Anholt34dc4d42010-05-07 14:30:03 -07004555 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004556 if (obj == NULL) {
4557 DRM_ERROR("Failed to allocate seqno page\n");
4558 ret = -ENOMEM;
4559 goto err;
4560 }
4561 obj_priv = to_intel_bo(obj);
4562 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4563
4564 ret = i915_gem_object_pin(obj, 4096);
4565 if (ret)
4566 goto err_unref;
4567
4568 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4569 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4570 if (dev_priv->seqno_page == NULL)
4571 goto err_unpin;
4572
4573 dev_priv->seqno_obj = obj;
4574 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4575
4576 return 0;
4577
4578err_unpin:
4579 i915_gem_object_unpin(obj);
4580err_unref:
4581 drm_gem_object_unreference(obj);
4582err:
4583 return ret;
4584}
4585
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004586
4587static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004588i915_gem_cleanup_pipe_control(struct drm_device *dev)
4589{
4590 drm_i915_private_t *dev_priv = dev->dev_private;
4591 struct drm_gem_object *obj;
4592 struct drm_i915_gem_object *obj_priv;
4593
4594 obj = dev_priv->seqno_obj;
4595 obj_priv = to_intel_bo(obj);
4596 kunmap(obj_priv->pages[0]);
4597 i915_gem_object_unpin(obj);
4598 drm_gem_object_unreference(obj);
4599 dev_priv->seqno_obj = NULL;
4600
4601 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004602}
4603
Eric Anholt673a3942008-07-30 12:06:12 -07004604int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004605i915_gem_init_ringbuffer(struct drm_device *dev)
4606{
4607 drm_i915_private_t *dev_priv = dev->dev_private;
4608 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004609
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004610 if (HAS_PIPE_CONTROL(dev)) {
4611 ret = i915_gem_init_pipe_control(dev);
4612 if (ret)
4613 return ret;
4614 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004615
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004616 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004617 if (ret)
4618 goto cleanup_pipe_control;
4619
4620 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004621 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004622 if (ret)
4623 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004624 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004625
Chris Wilson6f392d52010-08-07 11:01:22 +01004626 dev_priv->next_seqno = 1;
4627
Chris Wilson68f95ba2010-05-27 13:18:22 +01004628 return 0;
4629
4630cleanup_render_ring:
4631 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4632cleanup_pipe_control:
4633 if (HAS_PIPE_CONTROL(dev))
4634 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004635 return ret;
4636}
4637
4638void
4639i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4640{
4641 drm_i915_private_t *dev_priv = dev->dev_private;
4642
4643 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004644 if (HAS_BSD(dev))
4645 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004646 if (HAS_PIPE_CONTROL(dev))
4647 i915_gem_cleanup_pipe_control(dev);
4648}
4649
4650int
Eric Anholt673a3942008-07-30 12:06:12 -07004651i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4652 struct drm_file *file_priv)
4653{
4654 drm_i915_private_t *dev_priv = dev->dev_private;
4655 int ret;
4656
Jesse Barnes79e53942008-11-07 14:24:08 -08004657 if (drm_core_check_feature(dev, DRIVER_MODESET))
4658 return 0;
4659
Ben Gamariba1234d2009-09-14 17:48:47 -04004660 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004661 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004662 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004663 }
4664
Eric Anholt673a3942008-07-30 12:06:12 -07004665 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004666 dev_priv->mm.suspended = 0;
4667
4668 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004669 if (ret != 0) {
4670 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004671 return ret;
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004672 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004673
Zou Nan hai852835f2010-05-21 09:08:56 +08004674 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004675 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004676 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4677 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004678 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004679 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004680 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004681
Chris Wilson5f353082010-06-07 14:03:03 +01004682 ret = drm_irq_install(dev);
4683 if (ret)
4684 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004685
Eric Anholt673a3942008-07-30 12:06:12 -07004686 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004687
4688cleanup_ringbuffer:
4689 mutex_lock(&dev->struct_mutex);
4690 i915_gem_cleanup_ringbuffer(dev);
4691 dev_priv->mm.suspended = 1;
4692 mutex_unlock(&dev->struct_mutex);
4693
4694 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004695}
4696
4697int
4698i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4699 struct drm_file *file_priv)
4700{
Jesse Barnes79e53942008-11-07 14:24:08 -08004701 if (drm_core_check_feature(dev, DRIVER_MODESET))
4702 return 0;
4703
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004704 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004705 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004706}
4707
4708void
4709i915_gem_lastclose(struct drm_device *dev)
4710{
4711 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004712
Eric Anholte806b492009-01-22 09:56:58 -08004713 if (drm_core_check_feature(dev, DRIVER_MODESET))
4714 return;
4715
Keith Packard6dbe2772008-10-14 21:41:13 -07004716 ret = i915_gem_idle(dev);
4717 if (ret)
4718 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004719}
4720
4721void
4722i915_gem_load(struct drm_device *dev)
4723{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004724 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004725 drm_i915_private_t *dev_priv = dev->dev_private;
4726
Eric Anholt673a3942008-07-30 12:06:12 -07004727 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004728 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004729 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004730 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004731 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004732 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004733 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4734 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004735 if (HAS_BSD(dev)) {
4736 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4737 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4738 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004739 for (i = 0; i < 16; i++)
4740 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004741 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4742 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004743 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004744 spin_lock(&shrink_list_lock);
4745 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4746 spin_unlock(&shrink_list_lock);
4747
Dave Airlie94400122010-07-20 13:15:31 +10004748 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4749 if (IS_GEN3(dev)) {
4750 u32 tmp = I915_READ(MI_ARB_STATE);
4751 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4752 /* arb state is a masked write, so set bit + bit in mask */
4753 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4754 I915_WRITE(MI_ARB_STATE, tmp);
4755 }
4756 }
4757
Jesse Barnesde151cf2008-11-12 10:03:55 -08004758 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004759 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4760 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004761
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004762 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004763 dev_priv->num_fence_regs = 16;
4764 else
4765 dev_priv->num_fence_regs = 8;
4766
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004767 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004768 switch (INTEL_INFO(dev)->gen) {
4769 case 6:
4770 for (i = 0; i < 16; i++)
4771 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4772 break;
4773 case 5:
4774 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004775 for (i = 0; i < 16; i++)
4776 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004777 break;
4778 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004779 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4780 for (i = 0; i < 8; i++)
4781 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004782 case 2:
4783 for (i = 0; i < 8; i++)
4784 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4785 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004786 }
Eric Anholt673a3942008-07-30 12:06:12 -07004787 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004788 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004789}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004790
4791/*
4792 * Create a physically contiguous memory object for this object
4793 * e.g. for cursor + overlay regs
4794 */
Chris Wilson995b67622010-08-20 13:23:26 +01004795static int i915_gem_init_phys_object(struct drm_device *dev,
4796 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004797{
4798 drm_i915_private_t *dev_priv = dev->dev_private;
4799 struct drm_i915_gem_phys_object *phys_obj;
4800 int ret;
4801
4802 if (dev_priv->mm.phys_objs[id - 1] || !size)
4803 return 0;
4804
Eric Anholt9a298b22009-03-24 12:23:04 -07004805 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004806 if (!phys_obj)
4807 return -ENOMEM;
4808
4809 phys_obj->id = id;
4810
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004811 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004812 if (!phys_obj->handle) {
4813 ret = -ENOMEM;
4814 goto kfree_obj;
4815 }
4816#ifdef CONFIG_X86
4817 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4818#endif
4819
4820 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4821
4822 return 0;
4823kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004824 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004825 return ret;
4826}
4827
Chris Wilson995b67622010-08-20 13:23:26 +01004828static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004829{
4830 drm_i915_private_t *dev_priv = dev->dev_private;
4831 struct drm_i915_gem_phys_object *phys_obj;
4832
4833 if (!dev_priv->mm.phys_objs[id - 1])
4834 return;
4835
4836 phys_obj = dev_priv->mm.phys_objs[id - 1];
4837 if (phys_obj->cur_obj) {
4838 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4839 }
4840
4841#ifdef CONFIG_X86
4842 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4843#endif
4844 drm_pci_free(dev, phys_obj->handle);
4845 kfree(phys_obj);
4846 dev_priv->mm.phys_objs[id - 1] = NULL;
4847}
4848
4849void i915_gem_free_all_phys_object(struct drm_device *dev)
4850{
4851 int i;
4852
Dave Airlie260883c2009-01-22 17:58:49 +10004853 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004854 i915_gem_free_phys_object(dev, i);
4855}
4856
4857void i915_gem_detach_phys_object(struct drm_device *dev,
4858 struct drm_gem_object *obj)
4859{
4860 struct drm_i915_gem_object *obj_priv;
4861 int i;
4862 int ret;
4863 int page_count;
4864
Daniel Vetter23010e42010-03-08 13:35:02 +01004865 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004866 if (!obj_priv->phys_obj)
4867 return;
4868
Chris Wilson4bdadb92010-01-27 13:36:32 +00004869 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004870 if (ret)
4871 goto out;
4872
4873 page_count = obj->size / PAGE_SIZE;
4874
4875 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004876 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004877 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4878
4879 memcpy(dst, src, PAGE_SIZE);
4880 kunmap_atomic(dst, KM_USER0);
4881 }
Eric Anholt856fa192009-03-19 14:10:50 -07004882 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004883 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004884
4885 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004886out:
4887 obj_priv->phys_obj->cur_obj = NULL;
4888 obj_priv->phys_obj = NULL;
4889}
4890
4891int
4892i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004893 struct drm_gem_object *obj,
4894 int id,
4895 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004896{
4897 drm_i915_private_t *dev_priv = dev->dev_private;
4898 struct drm_i915_gem_object *obj_priv;
4899 int ret = 0;
4900 int page_count;
4901 int i;
4902
4903 if (id > I915_MAX_PHYS_OBJECT)
4904 return -EINVAL;
4905
Daniel Vetter23010e42010-03-08 13:35:02 +01004906 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004907
4908 if (obj_priv->phys_obj) {
4909 if (obj_priv->phys_obj->id == id)
4910 return 0;
4911 i915_gem_detach_phys_object(dev, obj);
4912 }
4913
Dave Airlie71acb5e2008-12-30 20:31:46 +10004914 /* create a new object */
4915 if (!dev_priv->mm.phys_objs[id - 1]) {
4916 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004917 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004918 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004919 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004920 goto out;
4921 }
4922 }
4923
4924 /* bind to the object */
4925 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4926 obj_priv->phys_obj->cur_obj = obj;
4927
Chris Wilson4bdadb92010-01-27 13:36:32 +00004928 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004929 if (ret) {
4930 DRM_ERROR("failed to get page list\n");
4931 goto out;
4932 }
4933
4934 page_count = obj->size / PAGE_SIZE;
4935
4936 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004937 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004938 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4939
4940 memcpy(dst, src, PAGE_SIZE);
4941 kunmap_atomic(src, KM_USER0);
4942 }
4943
Chris Wilsond78b47b2009-06-17 21:52:49 +01004944 i915_gem_object_put_pages(obj);
4945
Dave Airlie71acb5e2008-12-30 20:31:46 +10004946 return 0;
4947out:
4948 return ret;
4949}
4950
4951static int
4952i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4953 struct drm_i915_gem_pwrite *args,
4954 struct drm_file *file_priv)
4955{
Daniel Vetter23010e42010-03-08 13:35:02 +01004956 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004957 void *obj_addr;
4958 int ret;
4959 char __user *user_data;
4960
4961 user_data = (char __user *) (uintptr_t) args->data_ptr;
4962 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4963
Zhao Yakui44d98a62009-10-09 11:39:40 +08004964 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004965 ret = copy_from_user(obj_addr, user_data, args->size);
4966 if (ret)
4967 return -EFAULT;
4968
4969 drm_agp_chipset_flush(dev);
4970 return 0;
4971}
Eric Anholtb9624422009-06-03 07:27:35 +00004972
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004973void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004974{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004975 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004976
4977 /* Clean up our request list when the client is going away, so that
4978 * later retire_requests won't dereference our soon-to-be-gone
4979 * file_priv.
4980 */
Chris Wilson1c255952010-09-26 11:03:27 +01004981 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004982 while (!list_empty(&file_priv->mm.request_list)) {
4983 struct drm_i915_gem_request *request;
4984
4985 request = list_first_entry(&file_priv->mm.request_list,
4986 struct drm_i915_gem_request,
4987 client_list);
4988 list_del(&request->client_list);
4989 request->file_priv = NULL;
4990 }
Chris Wilson1c255952010-09-26 11:03:27 +01004991 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004992}
Chris Wilson31169712009-09-14 16:50:28 +01004993
Chris Wilson31169712009-09-14 16:50:28 +01004994static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004995i915_gpu_is_active(struct drm_device *dev)
4996{
4997 drm_i915_private_t *dev_priv = dev->dev_private;
4998 int lists_empty;
4999
Chris Wilson1637ef42010-04-20 17:10:35 +01005000 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08005001 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005002 if (HAS_BSD(dev))
5003 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005004
5005 return !lists_empty;
5006}
5007
5008static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10005009i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005010{
5011 drm_i915_private_t *dev_priv, *next_dev;
5012 struct drm_i915_gem_object *obj_priv, *next_obj;
5013 int cnt = 0;
5014 int would_deadlock = 1;
5015
5016 /* "fast-path" to count number of available objects */
5017 if (nr_to_scan == 0) {
5018 spin_lock(&shrink_list_lock);
5019 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5020 struct drm_device *dev = dev_priv->dev;
5021
5022 if (mutex_trylock(&dev->struct_mutex)) {
5023 list_for_each_entry(obj_priv,
5024 &dev_priv->mm.inactive_list,
5025 list)
5026 cnt++;
5027 mutex_unlock(&dev->struct_mutex);
5028 }
5029 }
5030 spin_unlock(&shrink_list_lock);
5031
5032 return (cnt / 100) * sysctl_vfs_cache_pressure;
5033 }
5034
5035 spin_lock(&shrink_list_lock);
5036
Chris Wilson1637ef42010-04-20 17:10:35 +01005037rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005038 /* first scan for clean buffers */
5039 list_for_each_entry_safe(dev_priv, next_dev,
5040 &shrink_list, mm.shrink_list) {
5041 struct drm_device *dev = dev_priv->dev;
5042
5043 if (! mutex_trylock(&dev->struct_mutex))
5044 continue;
5045
5046 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005047 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005048
Chris Wilson31169712009-09-14 16:50:28 +01005049 list_for_each_entry_safe(obj_priv, next_obj,
5050 &dev_priv->mm.inactive_list,
5051 list) {
5052 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005053 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005054 if (--nr_to_scan <= 0)
5055 break;
5056 }
5057 }
5058
5059 spin_lock(&shrink_list_lock);
5060 mutex_unlock(&dev->struct_mutex);
5061
Chris Wilson963b4832009-09-20 23:03:54 +01005062 would_deadlock = 0;
5063
Chris Wilson31169712009-09-14 16:50:28 +01005064 if (nr_to_scan <= 0)
5065 break;
5066 }
5067
5068 /* second pass, evict/count anything still on the inactive list */
5069 list_for_each_entry_safe(dev_priv, next_dev,
5070 &shrink_list, mm.shrink_list) {
5071 struct drm_device *dev = dev_priv->dev;
5072
5073 if (! mutex_trylock(&dev->struct_mutex))
5074 continue;
5075
5076 spin_unlock(&shrink_list_lock);
5077
5078 list_for_each_entry_safe(obj_priv, next_obj,
5079 &dev_priv->mm.inactive_list,
5080 list) {
5081 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005082 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005083 nr_to_scan--;
5084 } else
5085 cnt++;
5086 }
5087
5088 spin_lock(&shrink_list_lock);
5089 mutex_unlock(&dev->struct_mutex);
5090
5091 would_deadlock = 0;
5092 }
5093
Chris Wilson1637ef42010-04-20 17:10:35 +01005094 if (nr_to_scan) {
5095 int active = 0;
5096
5097 /*
5098 * We are desperate for pages, so as a last resort, wait
5099 * for the GPU to finish and discard whatever we can.
5100 * This has a dramatic impact to reduce the number of
5101 * OOM-killer events whilst running the GPU aggressively.
5102 */
5103 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5104 struct drm_device *dev = dev_priv->dev;
5105
5106 if (!mutex_trylock(&dev->struct_mutex))
5107 continue;
5108
5109 spin_unlock(&shrink_list_lock);
5110
5111 if (i915_gpu_is_active(dev)) {
5112 i915_gpu_idle(dev);
5113 active++;
5114 }
5115
5116 spin_lock(&shrink_list_lock);
5117 mutex_unlock(&dev->struct_mutex);
5118 }
5119
5120 if (active)
5121 goto rescan;
5122 }
5123
Chris Wilson31169712009-09-14 16:50:28 +01005124 spin_unlock(&shrink_list_lock);
5125
5126 if (would_deadlock)
5127 return -1;
5128 else if (cnt > 0)
5129 return (cnt / 100) * sysctl_vfs_cache_pressure;
5130 else
5131 return 0;
5132}
5133
5134static struct shrinker shrinker = {
5135 .shrink = i915_gem_shrink,
5136 .seeks = DEFAULT_SEEKS,
5137};
5138
5139__init void
5140i915_gem_shrinker_init(void)
5141{
5142 register_shrinker(&shrinker);
5143}
5144
5145__exit void
5146i915_gem_shrinker_exit(void)
5147{
5148 unregister_shrinker(&shrinker);
5149}