blob: a7283092c233beced1c8b3c30d4eadb5c6235759 [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
94
Chris Wilson7d1c4802010-08-07 21:45:03 +010095static inline bool
96i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
97{
98 return obj_priv->gtt_space &&
99 !obj_priv->active &&
100 obj_priv->pin_count == 0;
101}
102
Jesse Barnes79e53942008-11-07 14:24:08 -0800103int i915_gem_do_init(struct drm_device *dev, unsigned long start,
104 unsigned long end)
105{
106 drm_i915_private_t *dev_priv = dev->dev_private;
107
108 if (start >= end ||
109 (start & (PAGE_SIZE - 1)) != 0 ||
110 (end & (PAGE_SIZE - 1)) != 0) {
111 return -EINVAL;
112 }
113
114 drm_mm_init(&dev_priv->mm.gtt_space, start,
115 end - start);
116
117 dev->gtt_total = (uint32_t) (end - start);
118
119 return 0;
120}
Keith Packard6dbe2772008-10-14 21:41:13 -0700121
Eric Anholt673a3942008-07-30 12:06:12 -0700122int
123i915_gem_init_ioctl(struct drm_device *dev, void *data,
124 struct drm_file *file_priv)
125{
Eric Anholt673a3942008-07-30 12:06:12 -0700126 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800127 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700128
129 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -0800130 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700131 mutex_unlock(&dev->struct_mutex);
132
Jesse Barnes79e53942008-11-07 14:24:08 -0800133 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700134}
135
Eric Anholt5a125c32008-10-22 21:40:13 -0700136int
137i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
138 struct drm_file *file_priv)
139{
Eric Anholt5a125c32008-10-22 21:40:13 -0700140 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700141
142 if (!(dev->driver->driver_features & DRIVER_GEM))
143 return -ENODEV;
144
145 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800146 args->aper_available_size = (args->aper_size -
147 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700148
149 return 0;
150}
151
Eric Anholt673a3942008-07-30 12:06:12 -0700152
153/**
154 * Creates a new mm object and returns a handle to it.
155 */
156int
157i915_gem_create_ioctl(struct drm_device *dev, void *data,
158 struct drm_file *file_priv)
159{
160 struct drm_i915_gem_create *args = data;
161 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300162 int ret;
163 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700164
165 args->size = roundup(args->size, PAGE_SIZE);
166
167 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000168 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700169 if (obj == NULL)
170 return -ENOMEM;
171
172 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100173 if (ret) {
174 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700175 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100176 }
177
178 /* Sink the floating reference from kref_init(handlecount) */
179 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700180
181 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700182 return 0;
183}
184
Eric Anholt40123c12009-03-09 13:42:30 -0700185static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700186fast_shmem_read(struct page **pages,
187 loff_t page_base, int page_offset,
188 char __user *data,
189 int length)
190{
191 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200192 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700193
194 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
195 if (vaddr == NULL)
196 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200197 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700198 kunmap_atomic(vaddr, KM_USER0);
199
Florian Mickler2bc43b52009-04-06 22:55:41 +0200200 if (unwritten)
201 return -EFAULT;
202
203 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700204}
205
Eric Anholt280b7132009-03-12 16:56:27 -0700206static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
207{
208 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100209 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700210
211 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
212 obj_priv->tiling_mode != I915_TILING_NONE;
213}
214
Chris Wilson99a03df2010-05-27 14:15:34 +0100215static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700216slow_shmem_copy(struct page *dst_page,
217 int dst_offset,
218 struct page *src_page,
219 int src_offset,
220 int length)
221{
222 char *dst_vaddr, *src_vaddr;
223
Chris Wilson99a03df2010-05-27 14:15:34 +0100224 dst_vaddr = kmap(dst_page);
225 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700226
227 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
228
Chris Wilson99a03df2010-05-27 14:15:34 +0100229 kunmap(src_page);
230 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700231}
232
Chris Wilson99a03df2010-05-27 14:15:34 +0100233static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700234slow_shmem_bit17_copy(struct page *gpu_page,
235 int gpu_offset,
236 struct page *cpu_page,
237 int cpu_offset,
238 int length,
239 int is_read)
240{
241 char *gpu_vaddr, *cpu_vaddr;
242
243 /* Use the unswizzled path if this page isn't affected. */
244 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
245 if (is_read)
246 return slow_shmem_copy(cpu_page, cpu_offset,
247 gpu_page, gpu_offset, length);
248 else
249 return slow_shmem_copy(gpu_page, gpu_offset,
250 cpu_page, cpu_offset, length);
251 }
252
Chris Wilson99a03df2010-05-27 14:15:34 +0100253 gpu_vaddr = kmap(gpu_page);
254 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700255
256 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
257 * XORing with the other bits (A9 for Y, A9 and A10 for X)
258 */
259 while (length > 0) {
260 int cacheline_end = ALIGN(gpu_offset + 1, 64);
261 int this_length = min(cacheline_end - gpu_offset, length);
262 int swizzled_gpu_offset = gpu_offset ^ 64;
263
264 if (is_read) {
265 memcpy(cpu_vaddr + cpu_offset,
266 gpu_vaddr + swizzled_gpu_offset,
267 this_length);
268 } else {
269 memcpy(gpu_vaddr + swizzled_gpu_offset,
270 cpu_vaddr + cpu_offset,
271 this_length);
272 }
273 cpu_offset += this_length;
274 gpu_offset += this_length;
275 length -= this_length;
276 }
277
Chris Wilson99a03df2010-05-27 14:15:34 +0100278 kunmap(cpu_page);
279 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700280}
281
Eric Anholt673a3942008-07-30 12:06:12 -0700282/**
Eric Anholteb014592009-03-10 11:44:52 -0700283 * This is the fast shmem pread path, which attempts to copy_from_user directly
284 * from the backing pages of the object to the user's address space. On a
285 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
286 */
287static int
288i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
289 struct drm_i915_gem_pread *args,
290 struct drm_file *file_priv)
291{
Daniel Vetter23010e42010-03-08 13:35:02 +0100292 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700293 ssize_t remain;
294 loff_t offset, page_base;
295 char __user *user_data;
296 int page_offset, page_length;
297 int ret;
298
299 user_data = (char __user *) (uintptr_t) args->data_ptr;
300 remain = args->size;
301
302 mutex_lock(&dev->struct_mutex);
303
Chris Wilson4bdadb92010-01-27 13:36:32 +0000304 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700305 if (ret != 0)
306 goto fail_unlock;
307
308 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
309 args->size);
310 if (ret != 0)
311 goto fail_put_pages;
312
Daniel Vetter23010e42010-03-08 13:35:02 +0100313 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700314 offset = args->offset;
315
316 while (remain > 0) {
317 /* Operation in this page
318 *
319 * page_base = page offset within aperture
320 * page_offset = offset within page
321 * page_length = bytes to copy for this page
322 */
323 page_base = (offset & ~(PAGE_SIZE-1));
324 page_offset = offset & (PAGE_SIZE-1);
325 page_length = remain;
326 if ((page_offset + remain) > PAGE_SIZE)
327 page_length = PAGE_SIZE - page_offset;
328
329 ret = fast_shmem_read(obj_priv->pages,
330 page_base, page_offset,
331 user_data, page_length);
332 if (ret)
333 goto fail_put_pages;
334
335 remain -= page_length;
336 user_data += page_length;
337 offset += page_length;
338 }
339
340fail_put_pages:
341 i915_gem_object_put_pages(obj);
342fail_unlock:
343 mutex_unlock(&dev->struct_mutex);
344
345 return ret;
346}
347
Chris Wilson07f73f62009-09-14 16:50:30 +0100348static int
349i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
350{
351 int ret;
352
Chris Wilson4bdadb92010-01-27 13:36:32 +0000353 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100354
355 /* If we've insufficient memory to map in the pages, attempt
356 * to make some space by throwing out some old buffers.
357 */
358 if (ret == -ENOMEM) {
359 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100360
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100361 ret = i915_gem_evict_something(dev, obj->size,
362 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100363 if (ret)
364 return ret;
365
Chris Wilson4bdadb92010-01-27 13:36:32 +0000366 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100367 }
368
369 return ret;
370}
371
Eric Anholteb014592009-03-10 11:44:52 -0700372/**
373 * This is the fallback shmem pread path, which allocates temporary storage
374 * in kernel space to copy_to_user into outside of the struct_mutex, so we
375 * can copy out of the object's backing pages while holding the struct mutex
376 * and not take page faults.
377 */
378static int
379i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
380 struct drm_i915_gem_pread *args,
381 struct drm_file *file_priv)
382{
Daniel Vetter23010e42010-03-08 13:35:02 +0100383 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700384 struct mm_struct *mm = current->mm;
385 struct page **user_pages;
386 ssize_t remain;
387 loff_t offset, pinned_pages, i;
388 loff_t first_data_page, last_data_page, num_pages;
389 int shmem_page_index, shmem_page_offset;
390 int data_page_index, data_page_offset;
391 int page_length;
392 int ret;
393 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700394 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700395
396 remain = args->size;
397
398 /* Pin the user pages containing the data. We can't fault while
399 * holding the struct mutex, yet we want to hold it while
400 * dereferencing the user data.
401 */
402 first_data_page = data_ptr / PAGE_SIZE;
403 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
404 num_pages = last_data_page - first_data_page + 1;
405
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700406 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700407 if (user_pages == NULL)
408 return -ENOMEM;
409
410 down_read(&mm->mmap_sem);
411 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700412 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700413 up_read(&mm->mmap_sem);
414 if (pinned_pages < num_pages) {
415 ret = -EFAULT;
416 goto fail_put_user_pages;
417 }
418
Eric Anholt280b7132009-03-12 16:56:27 -0700419 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
420
Eric Anholteb014592009-03-10 11:44:52 -0700421 mutex_lock(&dev->struct_mutex);
422
Chris Wilson07f73f62009-09-14 16:50:30 +0100423 ret = i915_gem_object_get_pages_or_evict(obj);
424 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700425 goto fail_unlock;
426
427 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
428 args->size);
429 if (ret != 0)
430 goto fail_put_pages;
431
Daniel Vetter23010e42010-03-08 13:35:02 +0100432 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700433 offset = args->offset;
434
435 while (remain > 0) {
436 /* Operation in this page
437 *
438 * shmem_page_index = page number within shmem file
439 * shmem_page_offset = offset within page in shmem file
440 * data_page_index = page number in get_user_pages return
441 * data_page_offset = offset with data_page_index page.
442 * page_length = bytes to copy for this page
443 */
444 shmem_page_index = offset / PAGE_SIZE;
445 shmem_page_offset = offset & ~PAGE_MASK;
446 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
447 data_page_offset = data_ptr & ~PAGE_MASK;
448
449 page_length = remain;
450 if ((shmem_page_offset + page_length) > PAGE_SIZE)
451 page_length = PAGE_SIZE - shmem_page_offset;
452 if ((data_page_offset + page_length) > PAGE_SIZE)
453 page_length = PAGE_SIZE - data_page_offset;
454
Eric Anholt280b7132009-03-12 16:56:27 -0700455 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100456 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700457 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100458 user_pages[data_page_index],
459 data_page_offset,
460 page_length,
461 1);
462 } else {
463 slow_shmem_copy(user_pages[data_page_index],
464 data_page_offset,
465 obj_priv->pages[shmem_page_index],
466 shmem_page_offset,
467 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700468 }
Eric Anholteb014592009-03-10 11:44:52 -0700469
470 remain -= page_length;
471 data_ptr += page_length;
472 offset += page_length;
473 }
474
475fail_put_pages:
476 i915_gem_object_put_pages(obj);
477fail_unlock:
478 mutex_unlock(&dev->struct_mutex);
479fail_put_user_pages:
480 for (i = 0; i < pinned_pages; i++) {
481 SetPageDirty(user_pages[i]);
482 page_cache_release(user_pages[i]);
483 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700484 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700485
486 return ret;
487}
488
Eric Anholt673a3942008-07-30 12:06:12 -0700489/**
490 * Reads data from the object referenced by handle.
491 *
492 * On error, the contents of *data are undefined.
493 */
494int
495i915_gem_pread_ioctl(struct drm_device *dev, void *data,
496 struct drm_file *file_priv)
497{
498 struct drm_i915_gem_pread *args = data;
499 struct drm_gem_object *obj;
500 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700501 int ret;
502
503 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
504 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100505 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100506 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700507
508 /* Bounds check source.
509 *
510 * XXX: This could use review for overflow issues...
511 */
512 if (args->offset > obj->size || args->size > obj->size ||
513 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000514 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700515 return -EINVAL;
516 }
517
Eric Anholt280b7132009-03-12 16:56:27 -0700518 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700519 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700520 } else {
521 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
522 if (ret != 0)
523 ret = i915_gem_shmem_pread_slow(dev, obj, args,
524 file_priv);
525 }
Eric Anholt673a3942008-07-30 12:06:12 -0700526
Luca Barbieribc9025b2010-02-09 05:49:12 +0000527 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700528
Eric Anholteb014592009-03-10 11:44:52 -0700529 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700530}
531
Keith Packard0839ccb2008-10-30 19:38:48 -0700532/* This is the fast write path which cannot handle
533 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700534 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700535
Keith Packard0839ccb2008-10-30 19:38:48 -0700536static inline int
537fast_user_write(struct io_mapping *mapping,
538 loff_t page_base, int page_offset,
539 char __user *user_data,
540 int length)
541{
542 char *vaddr_atomic;
543 unsigned long unwritten;
544
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100545 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700546 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
547 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100548 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700549 if (unwritten)
550 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700551 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700552}
553
554/* Here's the write path which can sleep for
555 * page faults
556 */
557
Chris Wilsonab34c222010-05-27 14:15:35 +0100558static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700559slow_kernel_write(struct io_mapping *mapping,
560 loff_t gtt_base, int gtt_offset,
561 struct page *user_page, int user_offset,
562 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700563{
Chris Wilsonab34c222010-05-27 14:15:35 +0100564 char __iomem *dst_vaddr;
565 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700566
Chris Wilsonab34c222010-05-27 14:15:35 +0100567 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
568 src_vaddr = kmap(user_page);
569
570 memcpy_toio(dst_vaddr + gtt_offset,
571 src_vaddr + user_offset,
572 length);
573
574 kunmap(user_page);
575 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700576}
577
Eric Anholt40123c12009-03-09 13:42:30 -0700578static inline int
579fast_shmem_write(struct page **pages,
580 loff_t page_base, int page_offset,
581 char __user *data,
582 int length)
583{
584 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400585 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700586
587 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
588 if (vaddr == NULL)
589 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400590 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700591 kunmap_atomic(vaddr, KM_USER0);
592
Dave Airlied0088772009-03-28 20:29:48 -0400593 if (unwritten)
594 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700595 return 0;
596}
597
Eric Anholt3de09aa2009-03-09 09:42:23 -0700598/**
599 * This is the fast pwrite path, where we copy the data directly from the
600 * user into the GTT, uncached.
601 */
Eric Anholt673a3942008-07-30 12:06:12 -0700602static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700603i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
604 struct drm_i915_gem_pwrite *args,
605 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700606{
Daniel Vetter23010e42010-03-08 13:35:02 +0100607 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700608 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700609 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700610 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700611 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700612 int page_offset, page_length;
613 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700614
615 user_data = (char __user *) (uintptr_t) args->data_ptr;
616 remain = args->size;
617 if (!access_ok(VERIFY_READ, user_data, remain))
618 return -EFAULT;
619
620
621 mutex_lock(&dev->struct_mutex);
622 ret = i915_gem_object_pin(obj, 0);
623 if (ret) {
624 mutex_unlock(&dev->struct_mutex);
625 return ret;
626 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800627 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700628 if (ret)
629 goto fail;
630
Daniel Vetter23010e42010-03-08 13:35:02 +0100631 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700632 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700633
634 while (remain > 0) {
635 /* Operation in this page
636 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700637 * page_base = page offset within aperture
638 * page_offset = offset within page
639 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700640 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700641 page_base = (offset & ~(PAGE_SIZE-1));
642 page_offset = offset & (PAGE_SIZE-1);
643 page_length = remain;
644 if ((page_offset + remain) > PAGE_SIZE)
645 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700646
Keith Packard0839ccb2008-10-30 19:38:48 -0700647 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
648 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700649
Keith Packard0839ccb2008-10-30 19:38:48 -0700650 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700651 * source page isn't available. Return the error and we'll
652 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700653 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700654 if (ret)
655 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700656
Keith Packard0839ccb2008-10-30 19:38:48 -0700657 remain -= page_length;
658 user_data += page_length;
659 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700660 }
Eric Anholt673a3942008-07-30 12:06:12 -0700661
662fail:
663 i915_gem_object_unpin(obj);
664 mutex_unlock(&dev->struct_mutex);
665
666 return ret;
667}
668
Eric Anholt3de09aa2009-03-09 09:42:23 -0700669/**
670 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
671 * the memory and maps it using kmap_atomic for copying.
672 *
673 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
674 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
675 */
Eric Anholt3043c602008-10-02 12:24:47 -0700676static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700677i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
678 struct drm_i915_gem_pwrite *args,
679 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700680{
Daniel Vetter23010e42010-03-08 13:35:02 +0100681 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700682 drm_i915_private_t *dev_priv = dev->dev_private;
683 ssize_t remain;
684 loff_t gtt_page_base, offset;
685 loff_t first_data_page, last_data_page, num_pages;
686 loff_t pinned_pages, i;
687 struct page **user_pages;
688 struct mm_struct *mm = current->mm;
689 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700690 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700691 uint64_t data_ptr = args->data_ptr;
692
693 remain = args->size;
694
695 /* Pin the user pages containing the data. We can't fault while
696 * holding the struct mutex, and all of the pwrite implementations
697 * want to hold it while dereferencing the user data.
698 */
699 first_data_page = data_ptr / PAGE_SIZE;
700 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
701 num_pages = last_data_page - first_data_page + 1;
702
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700703 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700704 if (user_pages == NULL)
705 return -ENOMEM;
706
707 down_read(&mm->mmap_sem);
708 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
709 num_pages, 0, 0, user_pages, NULL);
710 up_read(&mm->mmap_sem);
711 if (pinned_pages < num_pages) {
712 ret = -EFAULT;
713 goto out_unpin_pages;
714 }
715
716 mutex_lock(&dev->struct_mutex);
717 ret = i915_gem_object_pin(obj, 0);
718 if (ret)
719 goto out_unlock;
720
721 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
722 if (ret)
723 goto out_unpin_object;
724
Daniel Vetter23010e42010-03-08 13:35:02 +0100725 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700726 offset = obj_priv->gtt_offset + args->offset;
727
728 while (remain > 0) {
729 /* Operation in this page
730 *
731 * gtt_page_base = page offset within aperture
732 * gtt_page_offset = offset within page in aperture
733 * data_page_index = page number in get_user_pages return
734 * data_page_offset = offset with data_page_index page.
735 * page_length = bytes to copy for this page
736 */
737 gtt_page_base = offset & PAGE_MASK;
738 gtt_page_offset = offset & ~PAGE_MASK;
739 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
740 data_page_offset = data_ptr & ~PAGE_MASK;
741
742 page_length = remain;
743 if ((gtt_page_offset + page_length) > PAGE_SIZE)
744 page_length = PAGE_SIZE - gtt_page_offset;
745 if ((data_page_offset + page_length) > PAGE_SIZE)
746 page_length = PAGE_SIZE - data_page_offset;
747
Chris Wilsonab34c222010-05-27 14:15:35 +0100748 slow_kernel_write(dev_priv->mm.gtt_mapping,
749 gtt_page_base, gtt_page_offset,
750 user_pages[data_page_index],
751 data_page_offset,
752 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700753
754 remain -= page_length;
755 offset += page_length;
756 data_ptr += page_length;
757 }
758
759out_unpin_object:
760 i915_gem_object_unpin(obj);
761out_unlock:
762 mutex_unlock(&dev->struct_mutex);
763out_unpin_pages:
764 for (i = 0; i < pinned_pages; i++)
765 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700766 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700767
768 return ret;
769}
770
Eric Anholt40123c12009-03-09 13:42:30 -0700771/**
772 * This is the fast shmem pwrite path, which attempts to directly
773 * copy_from_user into the kmapped pages backing the object.
774 */
Eric Anholt673a3942008-07-30 12:06:12 -0700775static int
Eric Anholt40123c12009-03-09 13:42:30 -0700776i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
777 struct drm_i915_gem_pwrite *args,
778 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700779{
Daniel Vetter23010e42010-03-08 13:35:02 +0100780 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700781 ssize_t remain;
782 loff_t offset, page_base;
783 char __user *user_data;
784 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700785 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700786
787 user_data = (char __user *) (uintptr_t) args->data_ptr;
788 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700789
790 mutex_lock(&dev->struct_mutex);
791
Chris Wilson4bdadb92010-01-27 13:36:32 +0000792 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700793 if (ret != 0)
794 goto fail_unlock;
795
Eric Anholte47c68e2008-11-14 13:35:19 -0800796 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700797 if (ret != 0)
798 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700799
Daniel Vetter23010e42010-03-08 13:35:02 +0100800 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700801 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700802 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700803
Eric Anholt40123c12009-03-09 13:42:30 -0700804 while (remain > 0) {
805 /* Operation in this page
806 *
807 * page_base = page offset within aperture
808 * page_offset = offset within page
809 * page_length = bytes to copy for this page
810 */
811 page_base = (offset & ~(PAGE_SIZE-1));
812 page_offset = offset & (PAGE_SIZE-1);
813 page_length = remain;
814 if ((page_offset + remain) > PAGE_SIZE)
815 page_length = PAGE_SIZE - page_offset;
816
817 ret = fast_shmem_write(obj_priv->pages,
818 page_base, page_offset,
819 user_data, page_length);
820 if (ret)
821 goto fail_put_pages;
822
823 remain -= page_length;
824 user_data += page_length;
825 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700826 }
827
Eric Anholt40123c12009-03-09 13:42:30 -0700828fail_put_pages:
829 i915_gem_object_put_pages(obj);
830fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700831 mutex_unlock(&dev->struct_mutex);
832
Eric Anholt40123c12009-03-09 13:42:30 -0700833 return ret;
834}
835
836/**
837 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
838 * the memory and maps it using kmap_atomic for copying.
839 *
840 * This avoids taking mmap_sem for faulting on the user's address while the
841 * struct_mutex is held.
842 */
843static int
844i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
845 struct drm_i915_gem_pwrite *args,
846 struct drm_file *file_priv)
847{
Daniel Vetter23010e42010-03-08 13:35:02 +0100848 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700849 struct mm_struct *mm = current->mm;
850 struct page **user_pages;
851 ssize_t remain;
852 loff_t offset, pinned_pages, i;
853 loff_t first_data_page, last_data_page, num_pages;
854 int shmem_page_index, shmem_page_offset;
855 int data_page_index, data_page_offset;
856 int page_length;
857 int ret;
858 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700859 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700860
861 remain = args->size;
862
863 /* Pin the user pages containing the data. We can't fault while
864 * holding the struct mutex, and all of the pwrite implementations
865 * want to hold it while dereferencing the user data.
866 */
867 first_data_page = data_ptr / PAGE_SIZE;
868 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
869 num_pages = last_data_page - first_data_page + 1;
870
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700871 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700872 if (user_pages == NULL)
873 return -ENOMEM;
874
875 down_read(&mm->mmap_sem);
876 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
877 num_pages, 0, 0, user_pages, NULL);
878 up_read(&mm->mmap_sem);
879 if (pinned_pages < num_pages) {
880 ret = -EFAULT;
881 goto fail_put_user_pages;
882 }
883
Eric Anholt280b7132009-03-12 16:56:27 -0700884 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
885
Eric Anholt40123c12009-03-09 13:42:30 -0700886 mutex_lock(&dev->struct_mutex);
887
Chris Wilson07f73f62009-09-14 16:50:30 +0100888 ret = i915_gem_object_get_pages_or_evict(obj);
889 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700890 goto fail_unlock;
891
892 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
893 if (ret != 0)
894 goto fail_put_pages;
895
Daniel Vetter23010e42010-03-08 13:35:02 +0100896 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700897 offset = args->offset;
898 obj_priv->dirty = 1;
899
900 while (remain > 0) {
901 /* Operation in this page
902 *
903 * shmem_page_index = page number within shmem file
904 * shmem_page_offset = offset within page in shmem file
905 * data_page_index = page number in get_user_pages return
906 * data_page_offset = offset with data_page_index page.
907 * page_length = bytes to copy for this page
908 */
909 shmem_page_index = offset / PAGE_SIZE;
910 shmem_page_offset = offset & ~PAGE_MASK;
911 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
912 data_page_offset = data_ptr & ~PAGE_MASK;
913
914 page_length = remain;
915 if ((shmem_page_offset + page_length) > PAGE_SIZE)
916 page_length = PAGE_SIZE - shmem_page_offset;
917 if ((data_page_offset + page_length) > PAGE_SIZE)
918 page_length = PAGE_SIZE - data_page_offset;
919
Eric Anholt280b7132009-03-12 16:56:27 -0700920 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100921 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700922 shmem_page_offset,
923 user_pages[data_page_index],
924 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100925 page_length,
926 0);
927 } else {
928 slow_shmem_copy(obj_priv->pages[shmem_page_index],
929 shmem_page_offset,
930 user_pages[data_page_index],
931 data_page_offset,
932 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700933 }
Eric Anholt40123c12009-03-09 13:42:30 -0700934
935 remain -= page_length;
936 data_ptr += page_length;
937 offset += page_length;
938 }
939
940fail_put_pages:
941 i915_gem_object_put_pages(obj);
942fail_unlock:
943 mutex_unlock(&dev->struct_mutex);
944fail_put_user_pages:
945 for (i = 0; i < pinned_pages; i++)
946 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700947 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700948
949 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700950}
951
952/**
953 * Writes data to the object referenced by handle.
954 *
955 * On error, the contents of the buffer that were to be modified are undefined.
956 */
957int
958i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
959 struct drm_file *file_priv)
960{
961 struct drm_i915_gem_pwrite *args = data;
962 struct drm_gem_object *obj;
963 struct drm_i915_gem_object *obj_priv;
964 int ret = 0;
965
966 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
967 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100968 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100969 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700970
971 /* Bounds check destination.
972 *
973 * XXX: This could use review for overflow issues...
974 */
975 if (args->offset > obj->size || args->size > obj->size ||
976 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000977 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700978 return -EINVAL;
979 }
980
981 /* We can only do the GTT pwrite on untiled buffers, as otherwise
982 * it would end up going through the fenced access, and we'll get
983 * different detiling behavior between reading and writing.
984 * pread/pwrite currently are reading and writing from the CPU
985 * perspective, requiring manual detiling by the client.
986 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000987 if (obj_priv->phys_obj)
988 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
989 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100990 dev->gtt_total != 0 &&
991 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700992 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
993 if (ret == -EFAULT) {
994 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
995 file_priv);
996 }
Eric Anholt280b7132009-03-12 16:56:27 -0700997 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
998 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700999 } else {
1000 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
1001 if (ret == -EFAULT) {
1002 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
1003 file_priv);
1004 }
1005 }
Eric Anholt673a3942008-07-30 12:06:12 -07001006
1007#if WATCH_PWRITE
1008 if (ret)
1009 DRM_INFO("pwrite failed %d\n", ret);
1010#endif
1011
Luca Barbieribc9025b2010-02-09 05:49:12 +00001012 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001013
1014 return ret;
1015}
1016
1017/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001018 * Called when user space prepares to use an object with the CPU, either
1019 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001020 */
1021int
1022i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1023 struct drm_file *file_priv)
1024{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001025 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001026 struct drm_i915_gem_set_domain *args = data;
1027 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001028 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001029 uint32_t read_domains = args->read_domains;
1030 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001031 int ret;
1032
1033 if (!(dev->driver->driver_features & DRIVER_GEM))
1034 return -ENODEV;
1035
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001036 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001037 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001038 return -EINVAL;
1039
Chris Wilson21d509e2009-06-06 09:46:02 +01001040 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001041 return -EINVAL;
1042
1043 /* Having something in the write domain implies it's in the read
1044 * domain, and only that read domain. Enforce that in the request.
1045 */
1046 if (write_domain != 0 && read_domains != write_domain)
1047 return -EINVAL;
1048
Eric Anholt673a3942008-07-30 12:06:12 -07001049 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1050 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001051 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001052 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001053
1054 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001055
1056 intel_mark_busy(dev, obj);
1057
Eric Anholt673a3942008-07-30 12:06:12 -07001058#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001059 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001060 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001061#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001062 if (read_domains & I915_GEM_DOMAIN_GTT) {
1063 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001064
Eric Anholta09ba7f2009-08-29 12:49:51 -07001065 /* Update the LRU on the fence for the CPU access that's
1066 * about to occur.
1067 */
1068 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001069 struct drm_i915_fence_reg *reg =
1070 &dev_priv->fence_regs[obj_priv->fence_reg];
1071 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001072 &dev_priv->mm.fence_list);
1073 }
1074
Eric Anholt02354392008-11-26 13:58:13 -08001075 /* Silently promote "you're not bound, there was nothing to do"
1076 * to success, since the client was just asking us to
1077 * make sure everything was done.
1078 */
1079 if (ret == -EINVAL)
1080 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001081 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001082 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001083 }
1084
Chris Wilson7d1c4802010-08-07 21:45:03 +01001085 /* Maintain LRU order of "inactive" objects */
1086 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1087 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1088
Eric Anholt673a3942008-07-30 12:06:12 -07001089 drm_gem_object_unreference(obj);
1090 mutex_unlock(&dev->struct_mutex);
1091 return ret;
1092}
1093
1094/**
1095 * Called when user space has done writes to this buffer
1096 */
1097int
1098i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1099 struct drm_file *file_priv)
1100{
1101 struct drm_i915_gem_sw_finish *args = data;
1102 struct drm_gem_object *obj;
1103 struct drm_i915_gem_object *obj_priv;
1104 int ret = 0;
1105
1106 if (!(dev->driver->driver_features & DRIVER_GEM))
1107 return -ENODEV;
1108
1109 mutex_lock(&dev->struct_mutex);
1110 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1111 if (obj == NULL) {
1112 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001113 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001114 }
1115
1116#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001117 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001118 __func__, args->handle, obj, obj->size);
1119#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001120 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001121
1122 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001123 if (obj_priv->pin_count)
1124 i915_gem_object_flush_cpu_write_domain(obj);
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 * Maps the contents of an object, returning the address it is mapped
1133 * into.
1134 *
1135 * While the mapping holds a reference on the contents of the object, it doesn't
1136 * imply a ref on the object itself.
1137 */
1138int
1139i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1140 struct drm_file *file_priv)
1141{
1142 struct drm_i915_gem_mmap *args = data;
1143 struct drm_gem_object *obj;
1144 loff_t offset;
1145 unsigned long addr;
1146
1147 if (!(dev->driver->driver_features & DRIVER_GEM))
1148 return -ENODEV;
1149
1150 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1151 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001152 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001153
1154 offset = args->offset;
1155
1156 down_write(&current->mm->mmap_sem);
1157 addr = do_mmap(obj->filp, 0, args->size,
1158 PROT_READ | PROT_WRITE, MAP_SHARED,
1159 args->offset);
1160 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001161 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001162 if (IS_ERR((void *)addr))
1163 return addr;
1164
1165 args->addr_ptr = (uint64_t) addr;
1166
1167 return 0;
1168}
1169
Jesse Barnesde151cf2008-11-12 10:03:55 -08001170/**
1171 * i915_gem_fault - fault a page into the GTT
1172 * vma: VMA in question
1173 * vmf: fault info
1174 *
1175 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1176 * from userspace. The fault handler takes care of binding the object to
1177 * the GTT (if needed), allocating and programming a fence register (again,
1178 * only if needed based on whether the old reg is still valid or the object
1179 * is tiled) and inserting a new PTE into the faulting process.
1180 *
1181 * Note that the faulting process may involve evicting existing objects
1182 * from the GTT and/or fence registers to make room. So performance may
1183 * suffer if the GTT working set is large or there are few fence registers
1184 * left.
1185 */
1186int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1187{
1188 struct drm_gem_object *obj = vma->vm_private_data;
1189 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001190 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001191 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001192 pgoff_t page_offset;
1193 unsigned long pfn;
1194 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001195 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001196
1197 /* We don't use vmf->pgoff since that has the fake offset */
1198 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1199 PAGE_SHIFT;
1200
1201 /* Now bind it into the GTT if needed */
1202 mutex_lock(&dev->struct_mutex);
1203 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001204 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001205 if (ret)
1206 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001207
Jesse Barnesde151cf2008-11-12 10:03:55 -08001208 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001209 if (ret)
1210 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001211 }
1212
1213 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001214 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001215 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001216 if (ret)
1217 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001218 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001219
Chris Wilson7d1c4802010-08-07 21:45:03 +01001220 if (i915_gem_object_is_inactive(obj_priv))
1221 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1222
Jesse Barnesde151cf2008-11-12 10:03:55 -08001223 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1224 page_offset;
1225
1226 /* Finally, remap it using the new GTT offset */
1227 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001228unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001229 mutex_unlock(&dev->struct_mutex);
1230
1231 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001232 case 0:
1233 case -ERESTARTSYS:
1234 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001235 case -ENOMEM:
1236 case -EAGAIN:
1237 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001238 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001239 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001240 }
1241}
1242
1243/**
1244 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1245 * @obj: obj in question
1246 *
1247 * GEM memory mapping works by handing back to userspace a fake mmap offset
1248 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1249 * up the object based on the offset and sets up the various memory mapping
1250 * structures.
1251 *
1252 * This routine allocates and attaches a fake offset for @obj.
1253 */
1254static int
1255i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1256{
1257 struct drm_device *dev = obj->dev;
1258 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001259 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001260 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001261 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001262 int ret = 0;
1263
1264 /* Set the object up for mmap'ing */
1265 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001266 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001267 if (!list->map)
1268 return -ENOMEM;
1269
1270 map = list->map;
1271 map->type = _DRM_GEM;
1272 map->size = obj->size;
1273 map->handle = obj;
1274
1275 /* Get a DRM GEM mmap offset allocated... */
1276 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1277 obj->size / PAGE_SIZE, 0, 0);
1278 if (!list->file_offset_node) {
1279 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae532010-09-21 15:05:24 +01001280 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001281 goto out_free_list;
1282 }
1283
1284 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1285 obj->size / PAGE_SIZE, 0);
1286 if (!list->file_offset_node) {
1287 ret = -ENOMEM;
1288 goto out_free_list;
1289 }
1290
1291 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae532010-09-21 15:05:24 +01001292 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1293 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001294 DRM_ERROR("failed to add to map hash\n");
1295 goto out_free_mm;
1296 }
1297
1298 /* By now we should be all set, any drm_mmap request on the offset
1299 * below will get to our mmap & fault handler */
1300 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1301
1302 return 0;
1303
1304out_free_mm:
1305 drm_mm_put_block(list->file_offset_node);
1306out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001307 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001308
1309 return ret;
1310}
1311
Chris Wilson901782b2009-07-10 08:18:50 +01001312/**
1313 * i915_gem_release_mmap - remove physical page mappings
1314 * @obj: obj in question
1315 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001316 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001317 * relinquish ownership of the pages back to the system.
1318 *
1319 * It is vital that we remove the page mapping if we have mapped a tiled
1320 * object through the GTT and then lose the fence register due to
1321 * resource pressure. Similarly if the object has been moved out of the
1322 * aperture, than pages mapped into userspace must be revoked. Removing the
1323 * mapping will then trigger a page fault on the next user access, allowing
1324 * fixup by i915_gem_fault().
1325 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001326void
Chris Wilson901782b2009-07-10 08:18:50 +01001327i915_gem_release_mmap(struct drm_gem_object *obj)
1328{
1329 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001330 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001331
1332 if (dev->dev_mapping)
1333 unmap_mapping_range(dev->dev_mapping,
1334 obj_priv->mmap_offset, obj->size, 1);
1335}
1336
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001337static void
1338i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1339{
1340 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001341 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001342 struct drm_gem_mm *mm = dev->mm_private;
1343 struct drm_map_list *list;
1344
1345 list = &obj->map_list;
1346 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1347
1348 if (list->file_offset_node) {
1349 drm_mm_put_block(list->file_offset_node);
1350 list->file_offset_node = NULL;
1351 }
1352
1353 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001354 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001355 list->map = NULL;
1356 }
1357
1358 obj_priv->mmap_offset = 0;
1359}
1360
Jesse Barnesde151cf2008-11-12 10:03:55 -08001361/**
1362 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1363 * @obj: object to check
1364 *
1365 * Return the required GTT alignment for an object, taking into account
1366 * potential fence register mapping if needed.
1367 */
1368static uint32_t
1369i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1370{
1371 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001372 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001373 int start, i;
1374
1375 /*
1376 * Minimum alignment is 4k (GTT page size), but might be greater
1377 * if a fence register is needed for the object.
1378 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001379 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001380 return 4096;
1381
1382 /*
1383 * Previous chips need to be aligned to the size of the smallest
1384 * fence register that can contain the object.
1385 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001386 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001387 start = 1024*1024;
1388 else
1389 start = 512*1024;
1390
1391 for (i = start; i < obj->size; i <<= 1)
1392 ;
1393
1394 return i;
1395}
1396
1397/**
1398 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1399 * @dev: DRM device
1400 * @data: GTT mapping ioctl data
1401 * @file_priv: GEM object info
1402 *
1403 * Simply returns the fake offset to userspace so it can mmap it.
1404 * The mmap call will end up in drm_gem_mmap(), which will set things
1405 * up so we can get faults in the handler above.
1406 *
1407 * The fault handler will take care of binding the object into the GTT
1408 * (since it may have been evicted to make room for something), allocating
1409 * a fence register, and mapping the appropriate aperture address into
1410 * userspace.
1411 */
1412int
1413i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1414 struct drm_file *file_priv)
1415{
1416 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001417 struct drm_gem_object *obj;
1418 struct drm_i915_gem_object *obj_priv;
1419 int ret;
1420
1421 if (!(dev->driver->driver_features & DRIVER_GEM))
1422 return -ENODEV;
1423
1424 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1425 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001426 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001427
1428 mutex_lock(&dev->struct_mutex);
1429
Daniel Vetter23010e42010-03-08 13:35:02 +01001430 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001431
Chris Wilsonab182822009-09-22 18:46:17 +01001432 if (obj_priv->madv != I915_MADV_WILLNEED) {
1433 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1434 drm_gem_object_unreference(obj);
1435 mutex_unlock(&dev->struct_mutex);
1436 return -EINVAL;
1437 }
1438
1439
Jesse Barnesde151cf2008-11-12 10:03:55 -08001440 if (!obj_priv->mmap_offset) {
1441 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001442 if (ret) {
1443 drm_gem_object_unreference(obj);
1444 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001445 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001446 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001447 }
1448
1449 args->offset = obj_priv->mmap_offset;
1450
Jesse Barnesde151cf2008-11-12 10:03:55 -08001451 /*
1452 * Pull it into the GTT so that we have a page list (makes the
1453 * initial fault faster and any subsequent flushing possible).
1454 */
1455 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001456 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001457 if (ret) {
1458 drm_gem_object_unreference(obj);
1459 mutex_unlock(&dev->struct_mutex);
1460 return ret;
1461 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001462 }
1463
1464 drm_gem_object_unreference(obj);
1465 mutex_unlock(&dev->struct_mutex);
1466
1467 return 0;
1468}
1469
Ben Gamari6911a9b2009-04-02 11:24:54 -07001470void
Eric Anholt856fa192009-03-19 14:10:50 -07001471i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001472{
Daniel Vetter23010e42010-03-08 13:35:02 +01001473 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001474 int page_count = obj->size / PAGE_SIZE;
1475 int i;
1476
Eric Anholt856fa192009-03-19 14:10:50 -07001477 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001478 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001479
1480 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001481 return;
1482
Eric Anholt280b7132009-03-12 16:56:27 -07001483 if (obj_priv->tiling_mode != I915_TILING_NONE)
1484 i915_gem_object_save_bit_17_swizzle(obj);
1485
Chris Wilson3ef94da2009-09-14 16:50:29 +01001486 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001487 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001488
1489 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001490 if (obj_priv->dirty)
1491 set_page_dirty(obj_priv->pages[i]);
1492
1493 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001494 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001495
1496 page_cache_release(obj_priv->pages[i]);
1497 }
Eric Anholt673a3942008-07-30 12:06:12 -07001498 obj_priv->dirty = 0;
1499
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001500 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001501 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001502}
1503
1504static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001505i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001506 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001507{
Chris Wilson5c12a072010-09-22 11:22:30 +01001508 struct drm_i915_private *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001509 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001510
Zou Nan hai852835f2010-05-21 09:08:56 +08001511 BUG_ON(ring == NULL);
1512 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001513
1514 /* Add a reference if we're newly entering the active list. */
1515 if (!obj_priv->active) {
1516 drm_gem_object_reference(obj);
1517 obj_priv->active = 1;
1518 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001519
Eric Anholt673a3942008-07-30 12:06:12 -07001520 /* Move from whatever list we were on to the tail of execution. */
Zou Nan hai852835f2010-05-21 09:08:56 +08001521 list_move_tail(&obj_priv->list, &ring->active_list);
Chris Wilson5c12a072010-09-22 11:22:30 +01001522 obj_priv->last_rendering_seqno = dev_priv->next_seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001523}
1524
Eric Anholtce44b0e2008-11-06 16:00:31 -08001525static void
1526i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1527{
1528 struct drm_device *dev = obj->dev;
1529 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001530 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001531
1532 BUG_ON(!obj_priv->active);
1533 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1534 obj_priv->last_rendering_seqno = 0;
1535}
Eric Anholt673a3942008-07-30 12:06:12 -07001536
Chris Wilson963b4832009-09-20 23:03:54 +01001537/* Immediately discard the backing storage */
1538static void
1539i915_gem_object_truncate(struct drm_gem_object *obj)
1540{
Daniel Vetter23010e42010-03-08 13:35:02 +01001541 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001542 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001543
Chris Wilsonae9fed62010-08-07 11:01:30 +01001544 /* Our goal here is to return as much of the memory as
1545 * is possible back to the system as we are called from OOM.
1546 * To do this we must instruct the shmfs to drop all of its
1547 * backing pages, *now*. Here we mirror the actions taken
1548 * when by shmem_delete_inode() to release the backing store.
1549 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001550 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001551 truncate_inode_pages(inode->i_mapping, 0);
1552 if (inode->i_op->truncate_range)
1553 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001554
1555 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001556}
1557
1558static inline int
1559i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1560{
1561 return obj_priv->madv == I915_MADV_DONTNEED;
1562}
1563
Eric Anholt673a3942008-07-30 12:06:12 -07001564static void
1565i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1566{
1567 struct drm_device *dev = obj->dev;
1568 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001569 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001570
1571 i915_verify_inactive(dev, __FILE__, __LINE__);
1572 if (obj_priv->pin_count != 0)
Chris Wilsonf13d3f72010-09-20 17:36:15 +01001573 list_move_tail(&obj_priv->list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001574 else
1575 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1576
Daniel Vetter99fcb762010-02-07 16:20:18 +01001577 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1578
Eric Anholtce44b0e2008-11-06 16:00:31 -08001579 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001580 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001581 if (obj_priv->active) {
1582 obj_priv->active = 0;
1583 drm_gem_object_unreference(obj);
1584 }
1585 i915_verify_inactive(dev, __FILE__, __LINE__);
1586}
1587
Chris Wilson92204342010-09-18 11:02:01 +01001588static void
Daniel Vetter63560392010-02-19 11:51:59 +01001589i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001590 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001591 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001592{
1593 drm_i915_private_t *dev_priv = dev->dev_private;
1594 struct drm_i915_gem_object *obj_priv, *next;
1595
1596 list_for_each_entry_safe(obj_priv, next,
1597 &dev_priv->mm.gpu_write_list,
1598 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001599 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001600
Chris Wilson2b6efaa2010-09-14 17:04:02 +01001601 if (obj->write_domain & flush_domains &&
1602 obj_priv->ring == ring) {
Daniel Vetter63560392010-02-19 11:51:59 +01001603 uint32_t old_write_domain = obj->write_domain;
1604
1605 obj->write_domain = 0;
1606 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001607 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001608
1609 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001610 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1611 struct drm_i915_fence_reg *reg =
1612 &dev_priv->fence_regs[obj_priv->fence_reg];
1613 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001614 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001615 }
Daniel Vetter63560392010-02-19 11:51:59 +01001616
1617 trace_i915_gem_object_change_domain(obj,
1618 obj->read_domains,
1619 old_write_domain);
1620 }
1621 }
1622}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001623
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001624uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001625i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001626 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001627 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001628 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001629{
1630 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001631 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001632 uint32_t seqno;
1633 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001634
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001635 if (file != NULL)
1636 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001637
Chris Wilson8dc5d142010-08-12 12:36:12 +01001638 if (request == NULL) {
1639 request = kzalloc(sizeof(*request), GFP_KERNEL);
1640 if (request == NULL)
1641 return 0;
1642 }
Eric Anholt673a3942008-07-30 12:06:12 -07001643
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001644 seqno = ring->add_request(dev, ring, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001645
1646 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001647 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001648 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001649 was_empty = list_empty(&ring->request_list);
1650 list_add_tail(&request->list, &ring->request_list);
1651
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001652 if (file_priv) {
1653 mutex_lock(&file_priv->mutex);
1654 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001655 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001656 &file_priv->mm.request_list);
1657 mutex_unlock(&file_priv->mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00001658 }
Eric Anholt673a3942008-07-30 12:06:12 -07001659
Ben Gamarif65d9422009-09-14 17:48:44 -04001660 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001661 mod_timer(&dev_priv->hangcheck_timer,
1662 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001663 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001664 queue_delayed_work(dev_priv->wq,
1665 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001666 }
Eric Anholt673a3942008-07-30 12:06:12 -07001667 return seqno;
1668}
1669
1670/**
1671 * Command execution barrier
1672 *
1673 * Ensures that all commands in the ring are finished
1674 * before signalling the CPU
1675 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001676static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001677i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001678{
Eric Anholt673a3942008-07-30 12:06:12 -07001679 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001680
1681 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001682 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001683 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001684
1685 ring->flush(dev, ring,
1686 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001687}
1688
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001689static inline void
1690i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001691{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001692 if (request->file_priv) {
1693 mutex_lock(&request->file_priv->mutex);
1694 list_del(&request->client_list);
1695 mutex_unlock(&request->file_priv->mutex);
1696 }
Eric Anholt673a3942008-07-30 12:06:12 -07001697}
1698
Chris Wilsondfaae392010-09-22 10:31:52 +01001699static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1700 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001701{
Chris Wilsondfaae392010-09-22 10:31:52 +01001702 while (!list_empty(&ring->request_list)) {
1703 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001704
Chris Wilsondfaae392010-09-22 10:31:52 +01001705 request = list_first_entry(&ring->request_list,
1706 struct drm_i915_gem_request,
1707 list);
1708
1709 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001710 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001711 kfree(request);
1712 }
1713
1714 while (!list_empty(&ring->active_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001715 struct drm_i915_gem_object *obj_priv;
1716
Chris Wilsondfaae392010-09-22 10:31:52 +01001717 obj_priv = list_first_entry(&ring->active_list,
1718 struct drm_i915_gem_object,
1719 list);
1720
1721 obj_priv->base.write_domain = 0;
1722 list_del_init(&obj_priv->gpu_write_list);
1723 i915_gem_object_move_to_inactive(&obj_priv->base);
1724 }
1725}
1726
1727void i915_gem_reset_lists(struct drm_device *dev)
1728{
1729 struct drm_i915_private *dev_priv = dev->dev_private;
1730 struct drm_i915_gem_object *obj_priv;
1731
1732 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
1733 if (HAS_BSD(dev))
1734 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
1735
1736 /* Remove anything from the flushing lists. The GPU cache is likely
1737 * to be lost on reset along with the data, so simply move the
1738 * lost bo to the inactive list.
1739 */
1740 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001741 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1742 struct drm_i915_gem_object,
1743 list);
1744
1745 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001746 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001747 i915_gem_object_move_to_inactive(&obj_priv->base);
1748 }
Chris Wilson9375e442010-09-19 12:21:28 +01001749
Chris Wilsondfaae392010-09-22 10:31:52 +01001750 /* Move everything out of the GPU domains to ensure we do any
1751 * necessary invalidation upon reuse.
1752 */
Chris Wilson77f01232010-09-19 12:31:36 +01001753 list_for_each_entry(obj_priv,
1754 &dev_priv->mm.inactive_list,
1755 list)
1756 {
1757 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1758 }
1759}
1760
Eric Anholt673a3942008-07-30 12:06:12 -07001761/**
1762 * This function clears the request list as sequence numbers are passed.
1763 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001764static void
1765i915_gem_retire_requests_ring(struct drm_device *dev,
1766 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001767{
1768 drm_i915_private_t *dev_priv = dev->dev_private;
1769 uint32_t seqno;
1770
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001771 if (!ring->status_page.page_addr ||
1772 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001773 return;
1774
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001775 seqno = ring->get_seqno(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001776 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001777 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001778
Zou Nan hai852835f2010-05-21 09:08:56 +08001779 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001780 struct drm_i915_gem_request,
1781 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001782
Chris Wilsondfaae392010-09-22 10:31:52 +01001783 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001784 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001785
1786 trace_i915_gem_request_retire(dev, request->seqno);
1787
1788 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001789 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001790 kfree(request);
1791 }
1792
1793 /* Move any buffers on the active list that are no longer referenced
1794 * by the ringbuffer to the flushing/inactive lists as appropriate.
1795 */
1796 while (!list_empty(&ring->active_list)) {
1797 struct drm_gem_object *obj;
1798 struct drm_i915_gem_object *obj_priv;
1799
1800 obj_priv = list_first_entry(&ring->active_list,
1801 struct drm_i915_gem_object,
1802 list);
1803
Chris Wilsondfaae392010-09-22 10:31:52 +01001804 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001805 break;
1806
1807 obj = &obj_priv->base;
1808
1809#if WATCH_LRU
1810 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1811 __func__, request->seqno, obj);
1812#endif
1813
1814 if (obj->write_domain != 0)
1815 i915_gem_object_move_to_flushing(obj);
1816 else
1817 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001818 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001819
1820 if (unlikely (dev_priv->trace_irq_seqno &&
1821 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001822 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001823 dev_priv->trace_irq_seqno = 0;
1824 }
Eric Anholt673a3942008-07-30 12:06:12 -07001825}
1826
1827void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001828i915_gem_retire_requests(struct drm_device *dev)
1829{
1830 drm_i915_private_t *dev_priv = dev->dev_private;
1831
Chris Wilsonbe726152010-07-23 23:18:50 +01001832 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1833 struct drm_i915_gem_object *obj_priv, *tmp;
1834
1835 /* We must be careful that during unbind() we do not
1836 * accidentally infinitely recurse into retire requests.
1837 * Currently:
1838 * retire -> free -> unbind -> wait -> retire_ring
1839 */
1840 list_for_each_entry_safe(obj_priv, tmp,
1841 &dev_priv->mm.deferred_free_list,
1842 list)
1843 i915_gem_free_object_tail(&obj_priv->base);
1844 }
1845
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001846 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1847 if (HAS_BSD(dev))
1848 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1849}
1850
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001851static void
Eric Anholt673a3942008-07-30 12:06:12 -07001852i915_gem_retire_work_handler(struct work_struct *work)
1853{
1854 drm_i915_private_t *dev_priv;
1855 struct drm_device *dev;
1856
1857 dev_priv = container_of(work, drm_i915_private_t,
1858 mm.retire_work.work);
1859 dev = dev_priv->dev;
1860
1861 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001862 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001863
Keith Packard6dbe2772008-10-14 21:41:13 -07001864 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001865 (!list_empty(&dev_priv->render_ring.request_list) ||
1866 (HAS_BSD(dev) &&
1867 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001868 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001869 mutex_unlock(&dev->struct_mutex);
1870}
1871
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001872int
Zou Nan hai852835f2010-05-21 09:08:56 +08001873i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001874 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001875{
1876 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001877 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001878 int ret = 0;
1879
1880 BUG_ON(seqno == 0);
1881
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001882 if (atomic_read(&dev_priv->mm.wedged))
1883 return -EAGAIN;
1884
Daniel Vettere35a41d2010-02-11 22:13:59 +01001885 if (seqno == dev_priv->next_seqno) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01001886 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001887 if (seqno == 0)
1888 return -ENOMEM;
1889 }
1890
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001891 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001892 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001893 ier = I915_READ(DEIER) | I915_READ(GTIER);
1894 else
1895 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001896 if (!ier) {
1897 DRM_ERROR("something (likely vbetool) disabled "
1898 "interrupts, re-enabling\n");
1899 i915_driver_irq_preinstall(dev);
1900 i915_driver_irq_postinstall(dev);
1901 }
1902
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001903 trace_i915_gem_request_wait_begin(dev, seqno);
1904
Zou Nan hai852835f2010-05-21 09:08:56 +08001905 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001906 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001907 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001908 ret = wait_event_interruptible(ring->irq_queue,
1909 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001910 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001911 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001912 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001913 wait_event(ring->irq_queue,
1914 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001915 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001916 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001917
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001918 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001919 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001920
1921 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001922 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001923 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001924 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07001925
1926 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01001927 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001928 __func__, ret, seqno, ring->get_seqno(dev, ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01001929 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001930
1931 /* Directly dispatch request retiring. While we have the work queue
1932 * to handle this, the waiter on a request often wants an associated
1933 * buffer to have made it to the inactive list, and we would need
1934 * a separate wait queue to handle that.
1935 */
1936 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001937 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001938
1939 return ret;
1940}
1941
Daniel Vetter48764bf2009-09-15 22:57:32 +02001942/**
1943 * Waits for a sequence number to be signaled, and cleans up the
1944 * request and object lists appropriately for that event.
1945 */
1946static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001947i915_wait_request(struct drm_device *dev, uint32_t seqno,
1948 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001949{
Zou Nan hai852835f2010-05-21 09:08:56 +08001950 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001951}
1952
Chris Wilson20f0cd52010-09-23 11:00:38 +01001953static void
Chris Wilson92204342010-09-18 11:02:01 +01001954i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01001955 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01001956 struct intel_ring_buffer *ring,
1957 uint32_t invalidate_domains,
1958 uint32_t flush_domains)
1959{
1960 ring->flush(dev, ring, invalidate_domains, flush_domains);
1961 i915_gem_process_flushing_list(dev, flush_domains, ring);
1962}
1963
1964static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001965i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01001966 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001967 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01001968 uint32_t flush_domains,
1969 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001970{
1971 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01001972
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001973 if (flush_domains & I915_GEM_DOMAIN_CPU)
1974 drm_agp_chipset_flush(dev);
Daniel Vetter8bff9172010-02-11 22:19:40 +01001975
Chris Wilson92204342010-09-18 11:02:01 +01001976 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
1977 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01001978 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01001979 &dev_priv->render_ring,
1980 invalidate_domains, flush_domains);
1981 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01001982 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01001983 &dev_priv->bsd_ring,
1984 invalidate_domains, flush_domains);
1985 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001986}
1987
Eric Anholt673a3942008-07-30 12:06:12 -07001988/**
1989 * Ensures that all rendering to the object has completed and the object is
1990 * safe to unbind from the GTT or access from the CPU.
1991 */
1992static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01001993i915_gem_object_wait_rendering(struct drm_gem_object *obj,
1994 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07001995{
1996 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001997 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001998 int ret;
1999
Eric Anholte47c68e2008-11-14 13:35:19 -08002000 /* This function only exists to support waiting for existing rendering,
2001 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002002 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002003 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002004
2005 /* If there is rendering queued on the buffer being evicted, wait for
2006 * it.
2007 */
2008 if (obj_priv->active) {
2009#if WATCH_BUF
2010 DRM_INFO("%s: object %p wait for seqno %08x\n",
2011 __func__, obj, obj_priv->last_rendering_seqno);
2012#endif
Chris Wilson2cf34d72010-09-14 13:03:28 +01002013 ret = i915_do_wait_request(dev,
2014 obj_priv->last_rendering_seqno,
2015 interruptible,
2016 obj_priv->ring);
2017 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002018 return ret;
2019 }
2020
2021 return 0;
2022}
2023
2024/**
2025 * Unbinds an object from the GTT aperture.
2026 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002027int
Eric Anholt673a3942008-07-30 12:06:12 -07002028i915_gem_object_unbind(struct drm_gem_object *obj)
2029{
2030 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002031 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002032 int ret = 0;
2033
2034#if WATCH_BUF
2035 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
2036 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
2037#endif
2038 if (obj_priv->gtt_space == NULL)
2039 return 0;
2040
2041 if (obj_priv->pin_count != 0) {
2042 DRM_ERROR("Attempting to unbind pinned buffer\n");
2043 return -EINVAL;
2044 }
2045
Eric Anholt5323fd02009-09-09 11:50:45 -07002046 /* blow away mappings if mapped through GTT */
2047 i915_gem_release_mmap(obj);
2048
Eric Anholt673a3942008-07-30 12:06:12 -07002049 /* Move the object to the CPU domain to ensure that
2050 * any possible CPU writes while it's not in the GTT
2051 * are flushed when we go to remap it. This will
2052 * also ensure that all pending GPU writes are finished
2053 * before we unbind.
2054 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002055 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002056 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002057 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002058 /* Continue on if we fail due to EIO, the GPU is hung so we
2059 * should be safe and we need to cleanup or else we might
2060 * cause memory corruption through use-after-free.
2061 */
Eric Anholt673a3942008-07-30 12:06:12 -07002062
Daniel Vetter96b47b62009-12-15 17:50:00 +01002063 /* release the fence reg _after_ flushing */
2064 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2065 i915_gem_clear_fence_reg(obj);
2066
Eric Anholt673a3942008-07-30 12:06:12 -07002067 if (obj_priv->agp_mem != NULL) {
2068 drm_unbind_agp(obj_priv->agp_mem);
2069 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2070 obj_priv->agp_mem = NULL;
2071 }
2072
Eric Anholt856fa192009-03-19 14:10:50 -07002073 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002074 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002075
2076 if (obj_priv->gtt_space) {
2077 atomic_dec(&dev->gtt_count);
2078 atomic_sub(obj->size, &dev->gtt_memory);
2079
2080 drm_mm_put_block(obj_priv->gtt_space);
2081 obj_priv->gtt_space = NULL;
2082 }
2083
Chris Wilsonf13d3f72010-09-20 17:36:15 +01002084 list_del_init(&obj_priv->list);
Eric Anholt673a3942008-07-30 12:06:12 -07002085
Chris Wilson963b4832009-09-20 23:03:54 +01002086 if (i915_gem_object_is_purgeable(obj_priv))
2087 i915_gem_object_truncate(obj);
2088
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002089 trace_i915_gem_object_unbind(obj);
2090
Chris Wilson8dc17752010-07-23 23:18:51 +01002091 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002092}
2093
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002094int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002095i915_gpu_idle(struct drm_device *dev)
2096{
2097 drm_i915_private_t *dev_priv = dev->dev_private;
2098 bool lists_empty;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002099 u32 seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08002100 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002101
Zou Nan haid1b851f2010-05-21 09:08:57 +08002102 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2103 list_empty(&dev_priv->render_ring.active_list) &&
2104 (!HAS_BSD(dev) ||
2105 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002106 if (lists_empty)
2107 return 0;
2108
2109 /* Flush everything onto the inactive list. */
Chris Wilson5c12a072010-09-22 11:22:30 +01002110 seqno = dev_priv->next_seqno;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002111 i915_gem_flush_ring(dev, NULL, &dev_priv->render_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002112 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilsonc78ec302010-09-20 12:50:23 +01002113 ret = i915_wait_request(dev, seqno, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002114 if (ret)
2115 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002116
2117 if (HAS_BSD(dev)) {
Chris Wilson5c12a072010-09-22 11:22:30 +01002118 seqno = dev_priv->next_seqno;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002119 i915_gem_flush_ring(dev, NULL, &dev_priv->bsd_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002120 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilsonc78ec302010-09-20 12:50:23 +01002121 ret = i915_wait_request(dev, seqno, &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002122 if (ret)
2123 return ret;
2124 }
2125
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002126 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002127}
2128
Ben Gamari6911a9b2009-04-02 11:24:54 -07002129int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002130i915_gem_object_get_pages(struct drm_gem_object *obj,
2131 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002132{
Daniel Vetter23010e42010-03-08 13:35:02 +01002133 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002134 int page_count, i;
2135 struct address_space *mapping;
2136 struct inode *inode;
2137 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002138
Daniel Vetter778c3542010-05-13 11:49:44 +02002139 BUG_ON(obj_priv->pages_refcount
2140 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2141
Eric Anholt856fa192009-03-19 14:10:50 -07002142 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002143 return 0;
2144
2145 /* Get the list of pages out of our struct file. They'll be pinned
2146 * at this point until we release them.
2147 */
2148 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002149 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002150 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002151 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002152 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002153 return -ENOMEM;
2154 }
2155
2156 inode = obj->filp->f_path.dentry->d_inode;
2157 mapping = inode->i_mapping;
2158 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002159 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002160 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002161 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002162 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002163 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002164 if (IS_ERR(page))
2165 goto err_pages;
2166
Eric Anholt856fa192009-03-19 14:10:50 -07002167 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002168 }
Eric Anholt280b7132009-03-12 16:56:27 -07002169
2170 if (obj_priv->tiling_mode != I915_TILING_NONE)
2171 i915_gem_object_do_bit_17_swizzle(obj);
2172
Eric Anholt673a3942008-07-30 12:06:12 -07002173 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002174
2175err_pages:
2176 while (i--)
2177 page_cache_release(obj_priv->pages[i]);
2178
2179 drm_free_large(obj_priv->pages);
2180 obj_priv->pages = NULL;
2181 obj_priv->pages_refcount--;
2182 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002183}
2184
Eric Anholt4e901fd2009-10-26 16:44:17 -07002185static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2186{
2187 struct drm_gem_object *obj = reg->obj;
2188 struct drm_device *dev = obj->dev;
2189 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002190 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002191 int regnum = obj_priv->fence_reg;
2192 uint64_t val;
2193
2194 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2195 0xfffff000) << 32;
2196 val |= obj_priv->gtt_offset & 0xfffff000;
2197 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2198 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2199
2200 if (obj_priv->tiling_mode == I915_TILING_Y)
2201 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2202 val |= I965_FENCE_REG_VALID;
2203
2204 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2205}
2206
Jesse Barnesde151cf2008-11-12 10:03:55 -08002207static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2208{
2209 struct drm_gem_object *obj = reg->obj;
2210 struct drm_device *dev = obj->dev;
2211 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002212 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002213 int regnum = obj_priv->fence_reg;
2214 uint64_t val;
2215
2216 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2217 0xfffff000) << 32;
2218 val |= obj_priv->gtt_offset & 0xfffff000;
2219 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2220 if (obj_priv->tiling_mode == I915_TILING_Y)
2221 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2222 val |= I965_FENCE_REG_VALID;
2223
2224 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2225}
2226
2227static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2228{
2229 struct drm_gem_object *obj = reg->obj;
2230 struct drm_device *dev = obj->dev;
2231 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002232 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002233 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002234 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002235 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002236 uint32_t pitch_val;
2237
2238 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2239 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002240 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002241 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002242 return;
2243 }
2244
Jesse Barnes0f973f22009-01-26 17:10:45 -08002245 if (obj_priv->tiling_mode == I915_TILING_Y &&
2246 HAS_128_BYTE_Y_TILING(dev))
2247 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002248 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002249 tile_width = 512;
2250
2251 /* Note: pitch better be a power of two tile widths */
2252 pitch_val = obj_priv->stride / tile_width;
2253 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002254
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002255 if (obj_priv->tiling_mode == I915_TILING_Y &&
2256 HAS_128_BYTE_Y_TILING(dev))
2257 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2258 else
2259 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2260
Jesse Barnesde151cf2008-11-12 10:03:55 -08002261 val = obj_priv->gtt_offset;
2262 if (obj_priv->tiling_mode == I915_TILING_Y)
2263 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2264 val |= I915_FENCE_SIZE_BITS(obj->size);
2265 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2266 val |= I830_FENCE_REG_VALID;
2267
Eric Anholtdc529a42009-03-10 22:34:49 -07002268 if (regnum < 8)
2269 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2270 else
2271 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2272 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002273}
2274
2275static void i830_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;
2282 uint32_t val;
2283 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002284 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002285
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002286 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002287 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002288 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002289 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002290 return;
2291 }
2292
Eric Anholte76a16d2009-05-26 17:44:56 -07002293 pitch_val = obj_priv->stride / 128;
2294 pitch_val = ffs(pitch_val) - 1;
2295 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2296
Jesse Barnesde151cf2008-11-12 10:03:55 -08002297 val = obj_priv->gtt_offset;
2298 if (obj_priv->tiling_mode == I915_TILING_Y)
2299 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002300 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2301 WARN_ON(fence_size_bits & ~0x00000f00);
2302 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002303 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2304 val |= I830_FENCE_REG_VALID;
2305
2306 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002307}
2308
Chris Wilson2cf34d72010-09-14 13:03:28 +01002309static int i915_find_fence_reg(struct drm_device *dev,
2310 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002311{
2312 struct drm_i915_fence_reg *reg = NULL;
2313 struct drm_i915_gem_object *obj_priv = NULL;
2314 struct drm_i915_private *dev_priv = dev->dev_private;
2315 struct drm_gem_object *obj = NULL;
2316 int i, avail, ret;
2317
2318 /* First try to find a free reg */
2319 avail = 0;
2320 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2321 reg = &dev_priv->fence_regs[i];
2322 if (!reg->obj)
2323 return i;
2324
Daniel Vetter23010e42010-03-08 13:35:02 +01002325 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002326 if (!obj_priv->pin_count)
2327 avail++;
2328 }
2329
2330 if (avail == 0)
2331 return -ENOSPC;
2332
2333 /* None available, try to steal one or wait for a user to finish */
2334 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002335 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2336 lru_list) {
2337 obj = reg->obj;
2338 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002339
2340 if (obj_priv->pin_count)
2341 continue;
2342
2343 /* found one! */
2344 i = obj_priv->fence_reg;
2345 break;
2346 }
2347
2348 BUG_ON(i == I915_FENCE_REG_NONE);
2349
2350 /* We only have a reference on obj from the active list. put_fence_reg
2351 * might drop that one, causing a use-after-free in it. So hold a
2352 * private reference to obj like the other callers of put_fence_reg
2353 * (set_tiling ioctl) do. */
2354 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002355 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002356 drm_gem_object_unreference(obj);
2357 if (ret != 0)
2358 return ret;
2359
2360 return i;
2361}
2362
Jesse Barnesde151cf2008-11-12 10:03:55 -08002363/**
2364 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2365 * @obj: object to map through a fence reg
2366 *
2367 * When mapping objects through the GTT, userspace wants to be able to write
2368 * to them without having to worry about swizzling if the object is tiled.
2369 *
2370 * This function walks the fence regs looking for a free one for @obj,
2371 * stealing one if it can't find any.
2372 *
2373 * It then sets up the reg based on the object's properties: address, pitch
2374 * and tiling format.
2375 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002376int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002377i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2378 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002379{
2380 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002381 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002382 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002383 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002384 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002385
Eric Anholta09ba7f2009-08-29 12:49:51 -07002386 /* Just update our place in the LRU if our fence is getting used. */
2387 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002388 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2389 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002390 return 0;
2391 }
2392
Jesse Barnesde151cf2008-11-12 10:03:55 -08002393 switch (obj_priv->tiling_mode) {
2394 case I915_TILING_NONE:
2395 WARN(1, "allocating a fence for non-tiled object?\n");
2396 break;
2397 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002398 if (!obj_priv->stride)
2399 return -EINVAL;
2400 WARN((obj_priv->stride & (512 - 1)),
2401 "object 0x%08x is X tiled but has non-512B pitch\n",
2402 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002403 break;
2404 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002405 if (!obj_priv->stride)
2406 return -EINVAL;
2407 WARN((obj_priv->stride & (128 - 1)),
2408 "object 0x%08x is Y tiled but has non-128B pitch\n",
2409 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002410 break;
2411 }
2412
Chris Wilson2cf34d72010-09-14 13:03:28 +01002413 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002414 if (ret < 0)
2415 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002416
Daniel Vetterae3db242010-02-19 11:51:58 +01002417 obj_priv->fence_reg = ret;
2418 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002419 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002420
Jesse Barnesde151cf2008-11-12 10:03:55 -08002421 reg->obj = obj;
2422
Chris Wilsone259bef2010-09-17 00:32:02 +01002423 switch (INTEL_INFO(dev)->gen) {
2424 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002425 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002426 break;
2427 case 5:
2428 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002429 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002430 break;
2431 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002432 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002433 break;
2434 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002435 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002436 break;
2437 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002438
Daniel Vetterae3db242010-02-19 11:51:58 +01002439 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2440 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002441
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002442 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002443}
2444
2445/**
2446 * i915_gem_clear_fence_reg - clear out fence register info
2447 * @obj: object to clear
2448 *
2449 * Zeroes out the fence register itself and clears out the associated
2450 * data structures in dev_priv and obj_priv.
2451 */
2452static void
2453i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2454{
2455 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002456 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002457 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002458 struct drm_i915_fence_reg *reg =
2459 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002460 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002461
Chris Wilsone259bef2010-09-17 00:32:02 +01002462 switch (INTEL_INFO(dev)->gen) {
2463 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002464 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2465 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002466 break;
2467 case 5:
2468 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002469 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002470 break;
2471 case 3:
2472 if (obj_priv->fence_reg > 8)
2473 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002474 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002475 case 2:
2476 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002477
2478 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002479 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002480 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002481
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002482 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002483 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002484 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002485}
2486
Eric Anholt673a3942008-07-30 12:06:12 -07002487/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002488 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2489 * to the buffer to finish, and then resets the fence register.
2490 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002491 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002492 *
2493 * Zeroes out the fence register itself and clears out the associated
2494 * data structures in dev_priv and obj_priv.
2495 */
2496int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002497i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2498 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002499{
2500 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002501 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002502 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002503 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002504
2505 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2506 return 0;
2507
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002508 /* If we've changed tiling, GTT-mappings of the object
2509 * need to re-fault to ensure that the correct fence register
2510 * setup is in place.
2511 */
2512 i915_gem_release_mmap(obj);
2513
Chris Wilson52dc7d32009-06-06 09:46:01 +01002514 /* On the i915, GPU access to tiled buffers is via a fence,
2515 * therefore we must wait for any outstanding access to complete
2516 * before clearing the fence.
2517 */
Chris Wilson53640e12010-09-20 11:40:50 +01002518 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2519 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002520 int ret;
2521
Chris Wilson2cf34d72010-09-14 13:03:28 +01002522 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002523 if (ret)
2524 return ret;
2525
Chris Wilson2cf34d72010-09-14 13:03:28 +01002526 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002527 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002528 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002529
2530 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002531 }
2532
Daniel Vetter4a726612010-02-01 13:59:16 +01002533 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002534 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002535
2536 return 0;
2537}
2538
2539/**
Eric Anholt673a3942008-07-30 12:06:12 -07002540 * Finds free space in the GTT aperture and binds the object there.
2541 */
2542static int
2543i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2544{
2545 struct drm_device *dev = obj->dev;
2546 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002547 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002548 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002549 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002550 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002551
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002552 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002553 DRM_ERROR("Attempting to bind a purgeable object\n");
2554 return -EINVAL;
2555 }
2556
Eric Anholt673a3942008-07-30 12:06:12 -07002557 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002558 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002559 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002560 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2561 return -EINVAL;
2562 }
2563
Chris Wilson654fc602010-05-27 13:18:21 +01002564 /* If the object is bigger than the entire aperture, reject it early
2565 * before evicting everything in a vain attempt to find space.
2566 */
2567 if (obj->size > dev->gtt_total) {
2568 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2569 return -E2BIG;
2570 }
2571
Eric Anholt673a3942008-07-30 12:06:12 -07002572 search_free:
2573 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2574 obj->size, alignment, 0);
2575 if (free_space != NULL) {
2576 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2577 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002578 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002579 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002580 }
2581 if (obj_priv->gtt_space == NULL) {
2582 /* If the gtt is empty and we're still having trouble
2583 * fitting our object in, we're out of memory.
2584 */
2585#if WATCH_LRU
2586 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2587#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002588 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002589 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002590 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002591
Eric Anholt673a3942008-07-30 12:06:12 -07002592 goto search_free;
2593 }
2594
2595#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002596 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002597 obj->size, obj_priv->gtt_offset);
2598#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002599 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002600 if (ret) {
2601 drm_mm_put_block(obj_priv->gtt_space);
2602 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002603
2604 if (ret == -ENOMEM) {
2605 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002606 ret = i915_gem_evict_something(dev, obj->size,
2607 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002608 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002609 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002610 if (gfpmask) {
2611 gfpmask = 0;
2612 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002613 }
2614
2615 return ret;
2616 }
2617
2618 goto search_free;
2619 }
2620
Eric Anholt673a3942008-07-30 12:06:12 -07002621 return ret;
2622 }
2623
Eric Anholt673a3942008-07-30 12:06:12 -07002624 /* Create an AGP memory structure pointing at our pages, and bind it
2625 * into the GTT.
2626 */
2627 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002628 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002629 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002630 obj_priv->gtt_offset,
2631 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002632 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002633 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002634 drm_mm_put_block(obj_priv->gtt_space);
2635 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002636
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002637 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002638 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002639 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002640
2641 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002642 }
2643 atomic_inc(&dev->gtt_count);
2644 atomic_add(obj->size, &dev->gtt_memory);
2645
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002646 /* keep track of bounds object by adding it to the inactive list */
2647 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2648
Eric Anholt673a3942008-07-30 12:06:12 -07002649 /* Assert that the object is not currently in any GPU domain. As it
2650 * wasn't in the GTT, there shouldn't be any way it could have been in
2651 * a GPU cache
2652 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002653 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2654 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002655
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002656 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2657
Eric Anholt673a3942008-07-30 12:06:12 -07002658 return 0;
2659}
2660
2661void
2662i915_gem_clflush_object(struct drm_gem_object *obj)
2663{
Daniel Vetter23010e42010-03-08 13:35:02 +01002664 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002665
2666 /* If we don't have a page list set up, then we're not pinned
2667 * to GPU, and we can ignore the cache flush because it'll happen
2668 * again at bind time.
2669 */
Eric Anholt856fa192009-03-19 14:10:50 -07002670 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002671 return;
2672
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002673 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002674
Eric Anholt856fa192009-03-19 14:10:50 -07002675 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002676}
2677
Eric Anholte47c68e2008-11-14 13:35:19 -08002678/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002679static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002680i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2681 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002682{
2683 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002684 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002685
2686 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002687 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002688
2689 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002690 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002691 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002692 to_intel_bo(obj)->ring,
2693 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002694 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002695
2696 trace_i915_gem_object_change_domain(obj,
2697 obj->read_domains,
2698 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002699
2700 if (pipelined)
2701 return 0;
2702
Chris Wilson2cf34d72010-09-14 13:03:28 +01002703 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002704}
2705
2706/** Flushes the GTT write domain for the object if it's dirty. */
2707static void
2708i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2709{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002710 uint32_t old_write_domain;
2711
Eric Anholte47c68e2008-11-14 13:35:19 -08002712 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2713 return;
2714
2715 /* No actual flushing is required for the GTT write domain. Writes
2716 * to it immediately go to main memory as far as we know, so there's
2717 * no chipset flush. It also doesn't land in render cache.
2718 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002719 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002720 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002721
2722 trace_i915_gem_object_change_domain(obj,
2723 obj->read_domains,
2724 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002725}
2726
2727/** Flushes the CPU write domain for the object if it's dirty. */
2728static void
2729i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2730{
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_DOMAIN_CPU)
2735 return;
2736
2737 i915_gem_clflush_object(obj);
2738 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002739 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002740 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002741
2742 trace_i915_gem_object_change_domain(obj,
2743 obj->read_domains,
2744 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002745}
2746
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002747/**
2748 * Moves a single object to the GTT read, and possibly write domain.
2749 *
2750 * This function returns when the move is complete, including waiting on
2751 * flushes to occur.
2752 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002753int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002754i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2755{
Daniel Vetter23010e42010-03-08 13:35:02 +01002756 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002757 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002758 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002759
Eric Anholt02354392008-11-26 13:58:13 -08002760 /* Not valid to be called on unbound objects. */
2761 if (obj_priv->gtt_space == NULL)
2762 return -EINVAL;
2763
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002764 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002765 if (ret != 0)
2766 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002767
Chris Wilson72133422010-09-13 23:56:38 +01002768 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002769
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002770 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002771 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002772 if (ret)
2773 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002774 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002775
Chris Wilson72133422010-09-13 23:56:38 +01002776 old_write_domain = obj->write_domain;
2777 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002778
2779 /* It should now be out of any other write domains, and we can update
2780 * the domain values for our changes.
2781 */
2782 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2783 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002784 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002785 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002786 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002787 obj_priv->dirty = 1;
2788 }
2789
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002790 trace_i915_gem_object_change_domain(obj,
2791 old_read_domains,
2792 old_write_domain);
2793
Eric Anholte47c68e2008-11-14 13:35:19 -08002794 return 0;
2795}
2796
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002797/*
2798 * Prepare buffer for display plane. Use uninterruptible for possible flush
2799 * wait, as in modesetting process we're not supposed to be interrupted.
2800 */
2801int
Chris Wilson48b956c2010-09-14 12:50:34 +01002802i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2803 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002804{
Daniel Vetter23010e42010-03-08 13:35:02 +01002805 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002806 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002807 int ret;
2808
2809 /* Not valid to be called on unbound objects. */
2810 if (obj_priv->gtt_space == NULL)
2811 return -EINVAL;
2812
Chris Wilson48b956c2010-09-14 12:50:34 +01002813 ret = i915_gem_object_flush_gpu_write_domain(obj, pipelined);
2814 if (ret)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002815 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002816
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002817 i915_gem_object_flush_cpu_write_domain(obj);
2818
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002819 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002820 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002821
2822 trace_i915_gem_object_change_domain(obj,
2823 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002824 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002825
2826 return 0;
2827}
2828
Eric Anholte47c68e2008-11-14 13:35:19 -08002829/**
2830 * Moves a single object to the CPU read, and possibly write domain.
2831 *
2832 * This function returns when the move is complete, including waiting on
2833 * flushes to occur.
2834 */
2835static int
2836i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2837{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002838 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002839 int ret;
2840
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002841 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002842 if (ret != 0)
2843 return ret;
2844
2845 i915_gem_object_flush_gtt_write_domain(obj);
2846
2847 /* If we have a partially-valid cache of the object in the CPU,
2848 * finish invalidating it and free the per-page flags.
2849 */
2850 i915_gem_object_set_to_full_cpu_read_domain(obj);
2851
Chris Wilson72133422010-09-13 23:56:38 +01002852 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002853 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002854 if (ret)
2855 return ret;
2856 }
2857
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002858 old_write_domain = obj->write_domain;
2859 old_read_domains = obj->read_domains;
2860
Eric Anholte47c68e2008-11-14 13:35:19 -08002861 /* Flush the CPU cache if it's still invalid. */
2862 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2863 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002864
2865 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2866 }
2867
2868 /* It should now be out of any other write domains, and we can update
2869 * the domain values for our changes.
2870 */
2871 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2872
2873 /* If we're writing through the CPU, then the GPU read domains will
2874 * need to be invalidated at next use.
2875 */
2876 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002877 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002878 obj->write_domain = I915_GEM_DOMAIN_CPU;
2879 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002880
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002881 trace_i915_gem_object_change_domain(obj,
2882 old_read_domains,
2883 old_write_domain);
2884
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002885 return 0;
2886}
2887
Eric Anholt673a3942008-07-30 12:06:12 -07002888/*
2889 * Set the next domain for the specified object. This
2890 * may not actually perform the necessary flushing/invaliding though,
2891 * as that may want to be batched with other set_domain operations
2892 *
2893 * This is (we hope) the only really tricky part of gem. The goal
2894 * is fairly simple -- track which caches hold bits of the object
2895 * and make sure they remain coherent. A few concrete examples may
2896 * help to explain how it works. For shorthand, we use the notation
2897 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2898 * a pair of read and write domain masks.
2899 *
2900 * Case 1: the batch buffer
2901 *
2902 * 1. Allocated
2903 * 2. Written by CPU
2904 * 3. Mapped to GTT
2905 * 4. Read by GPU
2906 * 5. Unmapped from GTT
2907 * 6. Freed
2908 *
2909 * Let's take these a step at a time
2910 *
2911 * 1. Allocated
2912 * Pages allocated from the kernel may still have
2913 * cache contents, so we set them to (CPU, CPU) always.
2914 * 2. Written by CPU (using pwrite)
2915 * The pwrite function calls set_domain (CPU, CPU) and
2916 * this function does nothing (as nothing changes)
2917 * 3. Mapped by GTT
2918 * This function asserts that the object is not
2919 * currently in any GPU-based read or write domains
2920 * 4. Read by GPU
2921 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2922 * As write_domain is zero, this function adds in the
2923 * current read domains (CPU+COMMAND, 0).
2924 * flush_domains is set to CPU.
2925 * invalidate_domains is set to COMMAND
2926 * clflush is run to get data out of the CPU caches
2927 * then i915_dev_set_domain calls i915_gem_flush to
2928 * emit an MI_FLUSH and drm_agp_chipset_flush
2929 * 5. Unmapped from GTT
2930 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2931 * flush_domains and invalidate_domains end up both zero
2932 * so no flushing/invalidating happens
2933 * 6. Freed
2934 * yay, done
2935 *
2936 * Case 2: The shared render buffer
2937 *
2938 * 1. Allocated
2939 * 2. Mapped to GTT
2940 * 3. Read/written by GPU
2941 * 4. set_domain to (CPU,CPU)
2942 * 5. Read/written by CPU
2943 * 6. Read/written by GPU
2944 *
2945 * 1. Allocated
2946 * Same as last example, (CPU, CPU)
2947 * 2. Mapped to GTT
2948 * Nothing changes (assertions find that it is not in the GPU)
2949 * 3. Read/written by GPU
2950 * execbuffer calls set_domain (RENDER, RENDER)
2951 * flush_domains gets CPU
2952 * invalidate_domains gets GPU
2953 * clflush (obj)
2954 * MI_FLUSH and drm_agp_chipset_flush
2955 * 4. set_domain (CPU, CPU)
2956 * flush_domains gets GPU
2957 * invalidate_domains gets CPU
2958 * wait_rendering (obj) to make sure all drawing is complete.
2959 * This will include an MI_FLUSH to get the data from GPU
2960 * to memory
2961 * clflush (obj) to invalidate the CPU cache
2962 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2963 * 5. Read/written by CPU
2964 * cache lines are loaded and dirtied
2965 * 6. Read written by GPU
2966 * Same as last GPU access
2967 *
2968 * Case 3: The constant buffer
2969 *
2970 * 1. Allocated
2971 * 2. Written by CPU
2972 * 3. Read by GPU
2973 * 4. Updated (written) by CPU again
2974 * 5. Read by GPU
2975 *
2976 * 1. Allocated
2977 * (CPU, CPU)
2978 * 2. Written by CPU
2979 * (CPU, CPU)
2980 * 3. Read by GPU
2981 * (CPU+RENDER, 0)
2982 * flush_domains = CPU
2983 * invalidate_domains = RENDER
2984 * clflush (obj)
2985 * MI_FLUSH
2986 * drm_agp_chipset_flush
2987 * 4. Updated (written) by CPU again
2988 * (CPU, CPU)
2989 * flush_domains = 0 (no previous write domain)
2990 * invalidate_domains = 0 (no new read domains)
2991 * 5. Read by GPU
2992 * (CPU+RENDER, 0)
2993 * flush_domains = CPU
2994 * invalidate_domains = RENDER
2995 * clflush (obj)
2996 * MI_FLUSH
2997 * drm_agp_chipset_flush
2998 */
Keith Packardc0d90822008-11-20 23:11:08 -08002999static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003000i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003001{
3002 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003003 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003004 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003005 uint32_t invalidate_domains = 0;
3006 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003007 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003008
Eric Anholt8b0e3782009-02-19 14:40:50 -08003009 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3010 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003011
Jesse Barnes652c3932009-08-17 13:31:43 -07003012 intel_mark_busy(dev, obj);
3013
Eric Anholt673a3942008-07-30 12:06:12 -07003014#if WATCH_BUF
3015 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3016 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08003017 obj->read_domains, obj->pending_read_domains,
3018 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003019#endif
3020 /*
3021 * If the object isn't moving to a new write domain,
3022 * let the object stay in multiple read domains
3023 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003024 if (obj->pending_write_domain == 0)
3025 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003026 else
3027 obj_priv->dirty = 1;
3028
3029 /*
3030 * Flush the current write domain if
3031 * the new read domains don't match. Invalidate
3032 * any read domains which differ from the old
3033 * write domain
3034 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003035 if (obj->write_domain &&
3036 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003037 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003038 invalidate_domains |=
3039 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003040 }
3041 /*
3042 * Invalidate any read caches which may have
3043 * stale data. That is, any new read domains.
3044 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003045 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003046 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3047#if WATCH_BUF
3048 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3049 __func__, flush_domains, invalidate_domains);
3050#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003051 i915_gem_clflush_object(obj);
3052 }
3053
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003054 old_read_domains = obj->read_domains;
3055
Eric Anholtefbeed92009-02-19 14:54:51 -08003056 /* The actual obj->write_domain will be updated with
3057 * pending_write_domain after we emit the accumulated flush for all
3058 * of our domain changes in execbuffers (which clears objects'
3059 * write_domains). So if we have a current write domain that we
3060 * aren't changing, set pending_write_domain to that.
3061 */
3062 if (flush_domains == 0 && obj->pending_write_domain == 0)
3063 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003064 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003065
3066 dev->invalidate_domains |= invalidate_domains;
3067 dev->flush_domains |= flush_domains;
Chris Wilson92204342010-09-18 11:02:01 +01003068 if (obj_priv->ring)
3069 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003070#if WATCH_BUF
3071 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3072 __func__,
3073 obj->read_domains, obj->write_domain,
3074 dev->invalidate_domains, dev->flush_domains);
3075#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003076
3077 trace_i915_gem_object_change_domain(obj,
3078 old_read_domains,
3079 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003080}
3081
3082/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003083 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003084 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003085 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3086 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3087 */
3088static void
3089i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3090{
Daniel Vetter23010e42010-03-08 13:35:02 +01003091 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003092
3093 if (!obj_priv->page_cpu_valid)
3094 return;
3095
3096 /* If we're partially in the CPU read domain, finish moving it in.
3097 */
3098 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3099 int i;
3100
3101 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3102 if (obj_priv->page_cpu_valid[i])
3103 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003104 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003105 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003106 }
3107
3108 /* Free the page_cpu_valid mappings which are now stale, whether
3109 * or not we've got I915_GEM_DOMAIN_CPU.
3110 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003111 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003112 obj_priv->page_cpu_valid = NULL;
3113}
3114
3115/**
3116 * Set the CPU read domain on a range of the object.
3117 *
3118 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3119 * not entirely valid. The page_cpu_valid member of the object flags which
3120 * pages have been flushed, and will be respected by
3121 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3122 * of the whole object.
3123 *
3124 * This function returns when the move is complete, including waiting on
3125 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003126 */
3127static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003128i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3129 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003130{
Daniel Vetter23010e42010-03-08 13:35:02 +01003131 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003132 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003133 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003134
Eric Anholte47c68e2008-11-14 13:35:19 -08003135 if (offset == 0 && size == obj->size)
3136 return i915_gem_object_set_to_cpu_domain(obj, 0);
3137
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003138 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003139 if (ret != 0)
3140 return ret;
3141 i915_gem_object_flush_gtt_write_domain(obj);
3142
3143 /* If we're already fully in the CPU read domain, we're done. */
3144 if (obj_priv->page_cpu_valid == NULL &&
3145 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003146 return 0;
3147
Eric Anholte47c68e2008-11-14 13:35:19 -08003148 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3149 * newly adding I915_GEM_DOMAIN_CPU
3150 */
Eric Anholt673a3942008-07-30 12:06:12 -07003151 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003152 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3153 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003154 if (obj_priv->page_cpu_valid == NULL)
3155 return -ENOMEM;
3156 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3157 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003158
3159 /* Flush the cache on any pages that are still invalid from the CPU's
3160 * perspective.
3161 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003162 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3163 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003164 if (obj_priv->page_cpu_valid[i])
3165 continue;
3166
Eric Anholt856fa192009-03-19 14:10:50 -07003167 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003168
3169 obj_priv->page_cpu_valid[i] = 1;
3170 }
3171
Eric Anholte47c68e2008-11-14 13:35:19 -08003172 /* It should now be out of any other write domains, and we can update
3173 * the domain values for our changes.
3174 */
3175 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3176
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003177 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003178 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3179
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003180 trace_i915_gem_object_change_domain(obj,
3181 old_read_domains,
3182 obj->write_domain);
3183
Eric Anholt673a3942008-07-30 12:06:12 -07003184 return 0;
3185}
3186
3187/**
Eric Anholt673a3942008-07-30 12:06:12 -07003188 * Pin an object to the GTT and evaluate the relocations landing in it.
3189 */
3190static int
3191i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3192 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003193 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003194 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003195{
3196 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003197 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003198 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003199 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003200 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003201 bool need_fence;
3202
3203 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3204 obj_priv->tiling_mode != I915_TILING_NONE;
3205
3206 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d2010-05-27 13:18:15 +01003207 if (need_fence &&
3208 !i915_gem_object_fence_offset_ok(obj,
3209 obj_priv->tiling_mode)) {
3210 ret = i915_gem_object_unbind(obj);
3211 if (ret)
3212 return ret;
3213 }
Eric Anholt673a3942008-07-30 12:06:12 -07003214
3215 /* Choose the GTT offset for our buffer and put it there. */
3216 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3217 if (ret)
3218 return ret;
3219
Jesse Barnes76446ca2009-12-17 22:05:42 -05003220 /*
3221 * Pre-965 chips need a fence register set up in order to
3222 * properly handle blits to/from tiled surfaces.
3223 */
3224 if (need_fence) {
Chris Wilson53640e12010-09-20 11:40:50 +01003225 ret = i915_gem_object_get_fence_reg(obj, true);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003226 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003227 i915_gem_object_unpin(obj);
3228 return ret;
3229 }
Chris Wilson53640e12010-09-20 11:40:50 +01003230
3231 dev_priv->fence_regs[obj_priv->fence_reg].gpu = true;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003232 }
3233
Eric Anholt673a3942008-07-30 12:06:12 -07003234 entry->offset = obj_priv->gtt_offset;
3235
Eric Anholt673a3942008-07-30 12:06:12 -07003236 /* Apply the relocations, using the GTT aperture to avoid cache
3237 * flushing requirements.
3238 */
3239 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003240 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003241 struct drm_gem_object *target_obj;
3242 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003243 uint32_t reloc_val, reloc_offset;
3244 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003245
Eric Anholt673a3942008-07-30 12:06:12 -07003246 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003247 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003248 if (target_obj == NULL) {
3249 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003250 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003251 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003252 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003253
Chris Wilson8542a0b2009-09-09 21:15:15 +01003254#if WATCH_RELOC
3255 DRM_INFO("%s: obj %p offset %08x target %d "
3256 "read %08x write %08x gtt %08x "
3257 "presumed %08x delta %08x\n",
3258 __func__,
3259 obj,
3260 (int) reloc->offset,
3261 (int) reloc->target_handle,
3262 (int) reloc->read_domains,
3263 (int) reloc->write_domain,
3264 (int) target_obj_priv->gtt_offset,
3265 (int) reloc->presumed_offset,
3266 reloc->delta);
3267#endif
3268
Eric Anholt673a3942008-07-30 12:06:12 -07003269 /* The target buffer should have appeared before us in the
3270 * exec_object list, so it should have a GTT space bound by now.
3271 */
3272 if (target_obj_priv->gtt_space == NULL) {
3273 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003274 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003275 drm_gem_object_unreference(target_obj);
3276 i915_gem_object_unpin(obj);
3277 return -EINVAL;
3278 }
3279
Chris Wilson8542a0b2009-09-09 21:15:15 +01003280 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003281 if (reloc->write_domain & (reloc->write_domain - 1)) {
3282 DRM_ERROR("reloc with multiple write domains: "
3283 "obj %p target %d offset %d "
3284 "read %08x write %08x",
3285 obj, reloc->target_handle,
3286 (int) reloc->offset,
3287 reloc->read_domains,
3288 reloc->write_domain);
3289 return -EINVAL;
3290 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003291 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3292 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3293 DRM_ERROR("reloc with read/write CPU domains: "
3294 "obj %p target %d offset %d "
3295 "read %08x write %08x",
3296 obj, reloc->target_handle,
3297 (int) reloc->offset,
3298 reloc->read_domains,
3299 reloc->write_domain);
3300 drm_gem_object_unreference(target_obj);
3301 i915_gem_object_unpin(obj);
3302 return -EINVAL;
3303 }
3304 if (reloc->write_domain && target_obj->pending_write_domain &&
3305 reloc->write_domain != target_obj->pending_write_domain) {
3306 DRM_ERROR("Write domain conflict: "
3307 "obj %p target %d offset %d "
3308 "new %08x old %08x\n",
3309 obj, reloc->target_handle,
3310 (int) reloc->offset,
3311 reloc->write_domain,
3312 target_obj->pending_write_domain);
3313 drm_gem_object_unreference(target_obj);
3314 i915_gem_object_unpin(obj);
3315 return -EINVAL;
3316 }
3317
3318 target_obj->pending_read_domains |= reloc->read_domains;
3319 target_obj->pending_write_domain |= reloc->write_domain;
3320
3321 /* If the relocation already has the right value in it, no
3322 * more work needs to be done.
3323 */
3324 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3325 drm_gem_object_unreference(target_obj);
3326 continue;
3327 }
3328
3329 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003330 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003331 DRM_ERROR("Relocation beyond object bounds: "
3332 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003333 obj, reloc->target_handle,
3334 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003335 drm_gem_object_unreference(target_obj);
3336 i915_gem_object_unpin(obj);
3337 return -EINVAL;
3338 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003339 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003340 DRM_ERROR("Relocation not 4-byte aligned: "
3341 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003342 obj, reloc->target_handle,
3343 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003344 drm_gem_object_unreference(target_obj);
3345 i915_gem_object_unpin(obj);
3346 return -EINVAL;
3347 }
3348
Chris Wilson8542a0b2009-09-09 21:15:15 +01003349 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003350 if (reloc->delta >= target_obj->size) {
3351 DRM_ERROR("Relocation beyond target object bounds: "
3352 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003353 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003354 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003355 drm_gem_object_unreference(target_obj);
3356 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003357 return -EINVAL;
3358 }
3359
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003360 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3361 if (ret != 0) {
3362 drm_gem_object_unreference(target_obj);
3363 i915_gem_object_unpin(obj);
3364 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003365 }
3366
3367 /* Map the page containing the relocation we're going to
3368 * perform.
3369 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003370 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003371 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3372 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003373 ~(PAGE_SIZE - 1)),
3374 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003375 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003376 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003377 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003378
3379#if WATCH_BUF
3380 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003381 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003382 readl(reloc_entry), reloc_val);
3383#endif
3384 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003385 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003386
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003387 /* The updated presumed offset for this entry will be
3388 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003389 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003390 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003391
3392 drm_gem_object_unreference(target_obj);
3393 }
3394
Eric Anholt673a3942008-07-30 12:06:12 -07003395#if WATCH_BUF
3396 if (0)
3397 i915_gem_dump_object(obj, 128, __func__, ~0);
3398#endif
3399 return 0;
3400}
3401
Eric Anholt673a3942008-07-30 12:06:12 -07003402/* Throttle our rendering by waiting until the ring has completed our requests
3403 * emitted over 20 msec ago.
3404 *
Eric Anholtb9624422009-06-03 07:27:35 +00003405 * Note that if we were to use the current jiffies each time around the loop,
3406 * we wouldn't escape the function with any frames outstanding if the time to
3407 * render a frame was over 20ms.
3408 *
Eric Anholt673a3942008-07-30 12:06:12 -07003409 * This should get us reasonable parallelism between CPU and GPU but also
3410 * relatively low latency when blocking on a particular request to finish.
3411 */
3412static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003413i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003414{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003415 struct drm_i915_private *dev_priv = dev->dev_private;
3416 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003417 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003418 struct drm_i915_gem_request *request;
3419 struct intel_ring_buffer *ring = NULL;
3420 u32 seqno = 0;
3421 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003422
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003423 mutex_lock(&file_priv->mutex);
3424 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003425 if (time_after_eq(request->emitted_jiffies, recent_enough))
3426 break;
3427
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003428 ring = request->ring;
3429 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003430 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003431 mutex_unlock(&file_priv->mutex);
3432
3433 if (seqno == 0)
3434 return 0;
3435
3436 ret = 0;
3437 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3438 /* And wait for the seqno passing without holding any locks and
3439 * causing extra latency for others. This is safe as the irq
3440 * generation is designed to be run atomically and so is
3441 * lockless.
3442 */
3443 ring->user_irq_get(dev, ring);
3444 ret = wait_event_interruptible(ring->irq_queue,
3445 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3446 || atomic_read(&dev_priv->mm.wedged));
3447 ring->user_irq_put(dev, ring);
3448
3449 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3450 ret = -EIO;
3451 }
3452
3453 if (ret == 0)
3454 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003455
Eric Anholt673a3942008-07-30 12:06:12 -07003456 return ret;
3457}
3458
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003459static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003460i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003461 uint32_t buffer_count,
3462 struct drm_i915_gem_relocation_entry **relocs)
3463{
3464 uint32_t reloc_count = 0, reloc_index = 0, i;
3465 int ret;
3466
3467 *relocs = NULL;
3468 for (i = 0; i < buffer_count; i++) {
3469 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3470 return -EINVAL;
3471 reloc_count += exec_list[i].relocation_count;
3472 }
3473
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003474 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003475 if (*relocs == NULL) {
3476 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003477 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003478 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003479
3480 for (i = 0; i < buffer_count; i++) {
3481 struct drm_i915_gem_relocation_entry __user *user_relocs;
3482
3483 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3484
3485 ret = copy_from_user(&(*relocs)[reloc_index],
3486 user_relocs,
3487 exec_list[i].relocation_count *
3488 sizeof(**relocs));
3489 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003490 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003491 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003492 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003493 }
3494
3495 reloc_index += exec_list[i].relocation_count;
3496 }
3497
Florian Mickler2bc43b52009-04-06 22:55:41 +02003498 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003499}
3500
3501static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003502i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003503 uint32_t buffer_count,
3504 struct drm_i915_gem_relocation_entry *relocs)
3505{
3506 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003507 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003508
Chris Wilson93533c22010-01-31 10:40:48 +00003509 if (relocs == NULL)
3510 return 0;
3511
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003512 for (i = 0; i < buffer_count; i++) {
3513 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003514 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003515
3516 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3517
Florian Mickler2bc43b52009-04-06 22:55:41 +02003518 unwritten = copy_to_user(user_relocs,
3519 &relocs[reloc_count],
3520 exec_list[i].relocation_count *
3521 sizeof(*relocs));
3522
3523 if (unwritten) {
3524 ret = -EFAULT;
3525 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003526 }
3527
3528 reloc_count += exec_list[i].relocation_count;
3529 }
3530
Florian Mickler2bc43b52009-04-06 22:55:41 +02003531err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003532 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003533
3534 return ret;
3535}
3536
Chris Wilson83d60792009-06-06 09:45:57 +01003537static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003538i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003539 uint64_t exec_offset)
3540{
3541 uint32_t exec_start, exec_len;
3542
3543 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3544 exec_len = (uint32_t) exec->batch_len;
3545
3546 if ((exec_start | exec_len) & 0x7)
3547 return -EINVAL;
3548
3549 if (!exec_start)
3550 return -EINVAL;
3551
3552 return 0;
3553}
3554
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003555static int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003556i915_gem_wait_for_pending_flip(struct drm_device *dev,
3557 struct drm_gem_object **object_list,
3558 int count)
3559{
3560 drm_i915_private_t *dev_priv = dev->dev_private;
3561 struct drm_i915_gem_object *obj_priv;
3562 DEFINE_WAIT(wait);
3563 int i, ret = 0;
3564
3565 for (;;) {
3566 prepare_to_wait(&dev_priv->pending_flip_queue,
3567 &wait, TASK_INTERRUPTIBLE);
3568 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003569 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003570 if (atomic_read(&obj_priv->pending_flip) > 0)
3571 break;
3572 }
3573 if (i == count)
3574 break;
3575
3576 if (!signal_pending(current)) {
3577 mutex_unlock(&dev->struct_mutex);
3578 schedule();
3579 mutex_lock(&dev->struct_mutex);
3580 continue;
3581 }
3582 ret = -ERESTARTSYS;
3583 break;
3584 }
3585 finish_wait(&dev_priv->pending_flip_queue, &wait);
3586
3587 return ret;
3588}
3589
Chris Wilson8dc5d142010-08-12 12:36:12 +01003590static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003591i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3592 struct drm_file *file_priv,
3593 struct drm_i915_gem_execbuffer2 *args,
3594 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003595{
3596 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003597 struct drm_gem_object **object_list = NULL;
3598 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003599 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003600 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003601 struct drm_i915_gem_relocation_entry *relocs = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003602 struct drm_i915_gem_request *request = NULL;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003603 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003604 uint64_t exec_offset;
Chris Wilson5c12a072010-09-22 11:22:30 +01003605 uint32_t reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003606 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003607
Zou Nan hai852835f2010-05-21 09:08:56 +08003608 struct intel_ring_buffer *ring = NULL;
3609
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003610 ret = i915_gem_check_is_wedged(dev);
3611 if (ret)
3612 return ret;
3613
Eric Anholt673a3942008-07-30 12:06:12 -07003614#if WATCH_EXEC
3615 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3616 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3617#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003618 if (args->flags & I915_EXEC_BSD) {
3619 if (!HAS_BSD(dev)) {
3620 DRM_ERROR("execbuf with wrong flag\n");
3621 return -EINVAL;
3622 }
3623 ring = &dev_priv->bsd_ring;
3624 } else {
3625 ring = &dev_priv->render_ring;
3626 }
3627
Eric Anholt4f481ed2008-09-10 14:22:49 -07003628 if (args->buffer_count < 1) {
3629 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3630 return -EINVAL;
3631 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003632 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003633 if (object_list == NULL) {
3634 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003635 args->buffer_count);
3636 ret = -ENOMEM;
3637 goto pre_mutex_err;
3638 }
Eric Anholt673a3942008-07-30 12:06:12 -07003639
Eric Anholt201361a2009-03-11 12:30:04 -07003640 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003641 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3642 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003643 if (cliprects == NULL) {
3644 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003645 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003646 }
Eric Anholt201361a2009-03-11 12:30:04 -07003647
3648 ret = copy_from_user(cliprects,
3649 (struct drm_clip_rect __user *)
3650 (uintptr_t) args->cliprects_ptr,
3651 sizeof(*cliprects) * args->num_cliprects);
3652 if (ret != 0) {
3653 DRM_ERROR("copy %d cliprects failed: %d\n",
3654 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003655 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003656 goto pre_mutex_err;
3657 }
3658 }
3659
Chris Wilson8dc5d142010-08-12 12:36:12 +01003660 request = kzalloc(sizeof(*request), GFP_KERNEL);
3661 if (request == NULL) {
3662 ret = -ENOMEM;
3663 goto pre_mutex_err;
3664 }
3665
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003666 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3667 &relocs);
3668 if (ret != 0)
3669 goto pre_mutex_err;
3670
Eric Anholt673a3942008-07-30 12:06:12 -07003671 mutex_lock(&dev->struct_mutex);
3672
3673 i915_verify_inactive(dev, __FILE__, __LINE__);
3674
Ben Gamariba1234d2009-09-14 17:48:47 -04003675 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003676 mutex_unlock(&dev->struct_mutex);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003677 ret = -EAGAIN;
Chris Wilsona198bc82009-02-06 16:55:20 +00003678 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003679 }
3680
3681 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003682 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003683 ret = -EBUSY;
3684 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003685 }
3686
Keith Packardac94a962008-11-20 23:30:27 -08003687 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003688 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003689 for (i = 0; i < args->buffer_count; i++) {
3690 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3691 exec_list[i].handle);
3692 if (object_list[i] == NULL) {
3693 DRM_ERROR("Invalid object handle %d at index %d\n",
3694 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003695 /* prevent error path from reading uninitialized data */
3696 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003697 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003698 goto err;
3699 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003700
Daniel Vetter23010e42010-03-08 13:35:02 +01003701 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003702 if (obj_priv->in_execbuffer) {
3703 DRM_ERROR("Object %p appears more than once in object list\n",
3704 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003705 /* prevent error path from reading uninitialized data */
3706 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003707 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003708 goto err;
3709 }
3710 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003711 flips += atomic_read(&obj_priv->pending_flip);
3712 }
3713
3714 if (flips > 0) {
3715 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3716 args->buffer_count);
3717 if (ret)
3718 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003719 }
Eric Anholt673a3942008-07-30 12:06:12 -07003720
Keith Packardac94a962008-11-20 23:30:27 -08003721 /* Pin and relocate */
3722 for (pin_tries = 0; ; pin_tries++) {
3723 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003724 reloc_index = 0;
3725
Keith Packardac94a962008-11-20 23:30:27 -08003726 for (i = 0; i < args->buffer_count; i++) {
3727 object_list[i]->pending_read_domains = 0;
3728 object_list[i]->pending_write_domain = 0;
3729 ret = i915_gem_object_pin_and_relocate(object_list[i],
3730 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003731 &exec_list[i],
3732 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003733 if (ret)
3734 break;
3735 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003736 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003737 }
3738 /* success */
3739 if (ret == 0)
3740 break;
3741
3742 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003743 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003744 if (ret != -ERESTARTSYS) {
3745 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003746 int num_fences = 0;
3747 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003748 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003749
Chris Wilson07f73f62009-09-14 16:50:30 +01003750 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003751 num_fences +=
3752 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3753 obj_priv->tiling_mode != I915_TILING_NONE;
3754 }
3755 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003756 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003757 total_size, num_fences,
3758 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003759 DRM_ERROR("%d objects [%d pinned], "
3760 "%d object bytes [%d pinned], "
3761 "%d/%d gtt bytes\n",
3762 atomic_read(&dev->object_count),
3763 atomic_read(&dev->pin_count),
3764 atomic_read(&dev->object_memory),
3765 atomic_read(&dev->pin_memory),
3766 atomic_read(&dev->gtt_memory),
3767 dev->gtt_total);
3768 }
Eric Anholt673a3942008-07-30 12:06:12 -07003769 goto err;
3770 }
Keith Packardac94a962008-11-20 23:30:27 -08003771
3772 /* unpin all of our buffers */
3773 for (i = 0; i < pinned; i++)
3774 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003775 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003776
3777 /* evict everyone we can from the aperture */
3778 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003779 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003780 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003781 }
3782
3783 /* Set the pending read domains for the batch buffer to COMMAND */
3784 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003785 if (batch_obj->pending_write_domain) {
3786 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3787 ret = -EINVAL;
3788 goto err;
3789 }
3790 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003791
Chris Wilson83d60792009-06-06 09:45:57 +01003792 /* Sanity check the batch buffer, prior to moving objects */
3793 exec_offset = exec_list[args->buffer_count - 1].offset;
3794 ret = i915_gem_check_execbuffer (args, exec_offset);
3795 if (ret != 0) {
3796 DRM_ERROR("execbuf with invalid offset/length\n");
3797 goto err;
3798 }
3799
Eric Anholt673a3942008-07-30 12:06:12 -07003800 i915_verify_inactive(dev, __FILE__, __LINE__);
3801
Keith Packard646f0f62008-11-20 23:23:03 -08003802 /* Zero the global flush/invalidate flags. These
3803 * will be modified as new domains are computed
3804 * for each object
3805 */
3806 dev->invalidate_domains = 0;
3807 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003808 dev_priv->mm.flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003809
Eric Anholt673a3942008-07-30 12:06:12 -07003810 for (i = 0; i < args->buffer_count; i++) {
3811 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003812
Keith Packard646f0f62008-11-20 23:23:03 -08003813 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003814 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003815 }
3816
3817 i915_verify_inactive(dev, __FILE__, __LINE__);
3818
Keith Packard646f0f62008-11-20 23:23:03 -08003819 if (dev->invalidate_domains | dev->flush_domains) {
3820#if WATCH_EXEC
3821 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3822 __func__,
3823 dev->invalidate_domains,
3824 dev->flush_domains);
3825#endif
Chris Wilsonc78ec302010-09-20 12:50:23 +01003826 i915_gem_flush(dev, file_priv,
Keith Packard646f0f62008-11-20 23:23:03 -08003827 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003828 dev->flush_domains,
3829 dev_priv->mm.flush_rings);
Daniel Vettera6910432010-02-02 17:08:37 +01003830 }
3831
Eric Anholtefbeed92009-02-19 14:54:51 -08003832 for (i = 0; i < args->buffer_count; i++) {
3833 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003834 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003835 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003836
3837 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003838 if (obj->write_domain)
3839 list_move_tail(&obj_priv->gpu_write_list,
3840 &dev_priv->mm.gpu_write_list);
3841 else
3842 list_del_init(&obj_priv->gpu_write_list);
3843
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003844 trace_i915_gem_object_change_domain(obj,
3845 obj->read_domains,
3846 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003847 }
3848
Eric Anholt673a3942008-07-30 12:06:12 -07003849 i915_verify_inactive(dev, __FILE__, __LINE__);
3850
3851#if WATCH_COHERENCY
3852 for (i = 0; i < args->buffer_count; i++) {
3853 i915_gem_object_check_coherency(object_list[i],
3854 exec_list[i].handle);
3855 }
3856#endif
3857
Eric Anholt673a3942008-07-30 12:06:12 -07003858#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003859 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003860 args->batch_len,
3861 __func__,
3862 ~0);
3863#endif
3864
Eric Anholt673a3942008-07-30 12:06:12 -07003865 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003866 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3867 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003868 if (ret) {
3869 DRM_ERROR("dispatch failed %d\n", ret);
3870 goto err;
3871 }
3872
3873 /*
3874 * Ensure that the commands in the batch buffer are
3875 * finished before the interrupt fires
3876 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003877 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003878
3879 i915_verify_inactive(dev, __FILE__, __LINE__);
3880
Daniel Vetter617dbe22010-02-11 22:16:02 +01003881 for (i = 0; i < args->buffer_count; i++) {
3882 struct drm_gem_object *obj = object_list[i];
3883 obj_priv = to_intel_bo(obj);
3884
3885 i915_gem_object_move_to_active(obj, ring);
3886#if WATCH_LRU
3887 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3888#endif
3889 }
Chris Wilson5c12a072010-09-22 11:22:30 +01003890 i915_add_request(dev, file_priv, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003891 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003892
Eric Anholt673a3942008-07-30 12:06:12 -07003893#if WATCH_LRU
3894 i915_dump_lru(dev, __func__);
3895#endif
3896
3897 i915_verify_inactive(dev, __FILE__, __LINE__);
3898
Eric Anholt673a3942008-07-30 12:06:12 -07003899err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003900 for (i = 0; i < pinned; i++)
3901 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003902
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003903 for (i = 0; i < args->buffer_count; i++) {
3904 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003905 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003906 obj_priv->in_execbuffer = false;
3907 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003908 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003909 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003910
Eric Anholt673a3942008-07-30 12:06:12 -07003911 mutex_unlock(&dev->struct_mutex);
3912
Chris Wilson93533c22010-01-31 10:40:48 +00003913pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003914 /* Copy the updated relocations out regardless of current error
3915 * state. Failure to update the relocs would mean that the next
3916 * time userland calls execbuf, it would do so with presumed offset
3917 * state that didn't match the actual object state.
3918 */
3919 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3920 relocs);
3921 if (ret2 != 0) {
3922 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3923
3924 if (ret == 0)
3925 ret = ret2;
3926 }
3927
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003928 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003929 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003930 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003931
3932 return ret;
3933}
3934
Jesse Barnes76446ca2009-12-17 22:05:42 -05003935/*
3936 * Legacy execbuffer just creates an exec2 list from the original exec object
3937 * list array and passes it to the real function.
3938 */
3939int
3940i915_gem_execbuffer(struct drm_device *dev, void *data,
3941 struct drm_file *file_priv)
3942{
3943 struct drm_i915_gem_execbuffer *args = data;
3944 struct drm_i915_gem_execbuffer2 exec2;
3945 struct drm_i915_gem_exec_object *exec_list = NULL;
3946 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3947 int ret, i;
3948
3949#if WATCH_EXEC
3950 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3951 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3952#endif
3953
3954 if (args->buffer_count < 1) {
3955 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3956 return -EINVAL;
3957 }
3958
3959 /* Copy in the exec list from userland */
3960 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3961 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3962 if (exec_list == NULL || exec2_list == NULL) {
3963 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3964 args->buffer_count);
3965 drm_free_large(exec_list);
3966 drm_free_large(exec2_list);
3967 return -ENOMEM;
3968 }
3969 ret = copy_from_user(exec_list,
3970 (struct drm_i915_relocation_entry __user *)
3971 (uintptr_t) args->buffers_ptr,
3972 sizeof(*exec_list) * args->buffer_count);
3973 if (ret != 0) {
3974 DRM_ERROR("copy %d exec entries failed %d\n",
3975 args->buffer_count, ret);
3976 drm_free_large(exec_list);
3977 drm_free_large(exec2_list);
3978 return -EFAULT;
3979 }
3980
3981 for (i = 0; i < args->buffer_count; i++) {
3982 exec2_list[i].handle = exec_list[i].handle;
3983 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3984 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3985 exec2_list[i].alignment = exec_list[i].alignment;
3986 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003987 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05003988 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3989 else
3990 exec2_list[i].flags = 0;
3991 }
3992
3993 exec2.buffers_ptr = args->buffers_ptr;
3994 exec2.buffer_count = args->buffer_count;
3995 exec2.batch_start_offset = args->batch_start_offset;
3996 exec2.batch_len = args->batch_len;
3997 exec2.DR1 = args->DR1;
3998 exec2.DR4 = args->DR4;
3999 exec2.num_cliprects = args->num_cliprects;
4000 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004001 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004002
4003 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4004 if (!ret) {
4005 /* Copy the new buffer offsets back to the user's exec list. */
4006 for (i = 0; i < args->buffer_count; i++)
4007 exec_list[i].offset = exec2_list[i].offset;
4008 /* ... and back out to userspace */
4009 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4010 (uintptr_t) args->buffers_ptr,
4011 exec_list,
4012 sizeof(*exec_list) * args->buffer_count);
4013 if (ret) {
4014 ret = -EFAULT;
4015 DRM_ERROR("failed to copy %d exec entries "
4016 "back to user (%d)\n",
4017 args->buffer_count, ret);
4018 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004019 }
4020
4021 drm_free_large(exec_list);
4022 drm_free_large(exec2_list);
4023 return ret;
4024}
4025
4026int
4027i915_gem_execbuffer2(struct drm_device *dev, void *data,
4028 struct drm_file *file_priv)
4029{
4030 struct drm_i915_gem_execbuffer2 *args = data;
4031 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4032 int ret;
4033
4034#if WATCH_EXEC
4035 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4036 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4037#endif
4038
4039 if (args->buffer_count < 1) {
4040 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4041 return -EINVAL;
4042 }
4043
4044 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4045 if (exec2_list == NULL) {
4046 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4047 args->buffer_count);
4048 return -ENOMEM;
4049 }
4050 ret = copy_from_user(exec2_list,
4051 (struct drm_i915_relocation_entry __user *)
4052 (uintptr_t) args->buffers_ptr,
4053 sizeof(*exec2_list) * args->buffer_count);
4054 if (ret != 0) {
4055 DRM_ERROR("copy %d exec entries failed %d\n",
4056 args->buffer_count, ret);
4057 drm_free_large(exec2_list);
4058 return -EFAULT;
4059 }
4060
4061 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4062 if (!ret) {
4063 /* Copy the new buffer offsets back to the user's exec list. */
4064 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4065 (uintptr_t) args->buffers_ptr,
4066 exec2_list,
4067 sizeof(*exec2_list) * args->buffer_count);
4068 if (ret) {
4069 ret = -EFAULT;
4070 DRM_ERROR("failed to copy %d exec entries "
4071 "back to user (%d)\n",
4072 args->buffer_count, ret);
4073 }
4074 }
4075
4076 drm_free_large(exec2_list);
4077 return ret;
4078}
4079
Eric Anholt673a3942008-07-30 12:06:12 -07004080int
4081i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4082{
4083 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004084 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004085 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004086 int ret;
4087
Daniel Vetter778c3542010-05-13 11:49:44 +02004088 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4089
Eric Anholt673a3942008-07-30 12:06:12 -07004090 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004091
4092 if (obj_priv->gtt_space != NULL) {
4093 if (alignment == 0)
4094 alignment = i915_gem_get_gtt_alignment(obj);
4095 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004096 WARN(obj_priv->pin_count,
4097 "bo is already pinned with incorrect alignment:"
4098 " offset=%x, req.alignment=%x\n",
4099 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004100 ret = i915_gem_object_unbind(obj);
4101 if (ret)
4102 return ret;
4103 }
4104 }
4105
Eric Anholt673a3942008-07-30 12:06:12 -07004106 if (obj_priv->gtt_space == NULL) {
4107 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004108 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004109 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004110 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004111
Eric Anholt673a3942008-07-30 12:06:12 -07004112 obj_priv->pin_count++;
4113
4114 /* If the object is not active and not pending a flush,
4115 * remove it from the inactive list
4116 */
4117 if (obj_priv->pin_count == 1) {
4118 atomic_inc(&dev->pin_count);
4119 atomic_add(obj->size, &dev->pin_memory);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004120 if (!obj_priv->active)
4121 list_move_tail(&obj_priv->list,
4122 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004123 }
4124 i915_verify_inactive(dev, __FILE__, __LINE__);
4125
4126 return 0;
4127}
4128
4129void
4130i915_gem_object_unpin(struct drm_gem_object *obj)
4131{
4132 struct drm_device *dev = obj->dev;
4133 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004134 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004135
4136 i915_verify_inactive(dev, __FILE__, __LINE__);
4137 obj_priv->pin_count--;
4138 BUG_ON(obj_priv->pin_count < 0);
4139 BUG_ON(obj_priv->gtt_space == NULL);
4140
4141 /* If the object is no longer pinned, and is
4142 * neither active nor being flushed, then stick it on
4143 * the inactive list
4144 */
4145 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004146 if (!obj_priv->active)
Eric Anholt673a3942008-07-30 12:06:12 -07004147 list_move_tail(&obj_priv->list,
4148 &dev_priv->mm.inactive_list);
4149 atomic_dec(&dev->pin_count);
4150 atomic_sub(obj->size, &dev->pin_memory);
4151 }
4152 i915_verify_inactive(dev, __FILE__, __LINE__);
4153}
4154
4155int
4156i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4157 struct drm_file *file_priv)
4158{
4159 struct drm_i915_gem_pin *args = data;
4160 struct drm_gem_object *obj;
4161 struct drm_i915_gem_object *obj_priv;
4162 int ret;
4163
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004164 ret = i915_gem_check_is_wedged(dev);
4165 if (ret)
4166 return ret;
4167
Eric Anholt673a3942008-07-30 12:06:12 -07004168 mutex_lock(&dev->struct_mutex);
4169
4170 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4171 if (obj == NULL) {
4172 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4173 args->handle);
4174 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004175 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004176 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004177 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004178
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004179 if (obj_priv->madv != I915_MADV_WILLNEED) {
4180 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004181 drm_gem_object_unreference(obj);
4182 mutex_unlock(&dev->struct_mutex);
4183 return -EINVAL;
4184 }
4185
Jesse Barnes79e53942008-11-07 14:24:08 -08004186 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4187 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4188 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004189 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004190 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004191 return -EINVAL;
4192 }
4193
4194 obj_priv->user_pin_count++;
4195 obj_priv->pin_filp = file_priv;
4196 if (obj_priv->user_pin_count == 1) {
4197 ret = i915_gem_object_pin(obj, args->alignment);
4198 if (ret != 0) {
4199 drm_gem_object_unreference(obj);
4200 mutex_unlock(&dev->struct_mutex);
4201 return ret;
4202 }
Eric Anholt673a3942008-07-30 12:06:12 -07004203 }
4204
4205 /* XXX - flush the CPU caches for pinned objects
4206 * as the X server doesn't manage domains yet
4207 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004208 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004209 args->offset = obj_priv->gtt_offset;
4210 drm_gem_object_unreference(obj);
4211 mutex_unlock(&dev->struct_mutex);
4212
4213 return 0;
4214}
4215
4216int
4217i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4218 struct drm_file *file_priv)
4219{
4220 struct drm_i915_gem_pin *args = data;
4221 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004222 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004223
4224 mutex_lock(&dev->struct_mutex);
4225
4226 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4227 if (obj == NULL) {
4228 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4229 args->handle);
4230 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004231 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004232 }
4233
Daniel Vetter23010e42010-03-08 13:35:02 +01004234 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004235 if (obj_priv->pin_filp != file_priv) {
4236 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4237 args->handle);
4238 drm_gem_object_unreference(obj);
4239 mutex_unlock(&dev->struct_mutex);
4240 return -EINVAL;
4241 }
4242 obj_priv->user_pin_count--;
4243 if (obj_priv->user_pin_count == 0) {
4244 obj_priv->pin_filp = NULL;
4245 i915_gem_object_unpin(obj);
4246 }
Eric Anholt673a3942008-07-30 12:06:12 -07004247
4248 drm_gem_object_unreference(obj);
4249 mutex_unlock(&dev->struct_mutex);
4250 return 0;
4251}
4252
4253int
4254i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4255 struct drm_file *file_priv)
4256{
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004257 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004258 struct drm_i915_gem_busy *args = data;
4259 struct drm_gem_object *obj;
4260 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004261 int ret;
4262
4263 ret = i915_gem_check_is_wedged(dev);
4264 if (ret)
4265 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004266
Eric Anholt673a3942008-07-30 12:06:12 -07004267 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4268 if (obj == NULL) {
4269 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4270 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004271 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004272 }
4273
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004274 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004275
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004276 if (atomic_read(&dev_priv->mm.wedged)) {
4277 ret = -EAGAIN;
4278 goto unlock;
4279 }
4280
Chris Wilson0be555b2010-08-04 15:36:30 +01004281 /* Count all active objects as busy, even if they are currently not used
4282 * by the gpu. Users of this interface expect objects to eventually
4283 * become non-busy without any further actions, therefore emit any
4284 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004285 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004286 obj_priv = to_intel_bo(obj);
4287 args->busy = obj_priv->active;
4288 if (args->busy) {
4289 /* Unconditionally flush objects, even when the gpu still uses this
4290 * object. Userspace calling this function indicates that it wants to
4291 * use this buffer rather sooner than later, so issuing the required
4292 * flush earlier is beneficial.
4293 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004294 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4295 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004296 obj_priv->ring,
4297 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004298
4299 /* Update the active list for the hardware's current position.
4300 * Otherwise this only updates on a delayed timer or when irqs
4301 * are actually unmasked, and our working set ends up being
4302 * larger than required.
4303 */
4304 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4305
4306 args->busy = obj_priv->active;
4307 }
Eric Anholt673a3942008-07-30 12:06:12 -07004308
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004309unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004310 drm_gem_object_unreference(obj);
4311 mutex_unlock(&dev->struct_mutex);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004312 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004313}
4314
4315int
4316i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4317 struct drm_file *file_priv)
4318{
4319 return i915_gem_ring_throttle(dev, file_priv);
4320}
4321
Chris Wilson3ef94da2009-09-14 16:50:29 +01004322int
4323i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4324 struct drm_file *file_priv)
4325{
4326 struct drm_i915_gem_madvise *args = data;
4327 struct drm_gem_object *obj;
4328 struct drm_i915_gem_object *obj_priv;
4329
4330 switch (args->madv) {
4331 case I915_MADV_DONTNEED:
4332 case I915_MADV_WILLNEED:
4333 break;
4334 default:
4335 return -EINVAL;
4336 }
4337
4338 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4339 if (obj == NULL) {
4340 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4341 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004342 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004343 }
4344
4345 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004346 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004347
4348 if (obj_priv->pin_count) {
4349 drm_gem_object_unreference(obj);
4350 mutex_unlock(&dev->struct_mutex);
4351
4352 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4353 return -EINVAL;
4354 }
4355
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004356 if (obj_priv->madv != __I915_MADV_PURGED)
4357 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004358
Chris Wilson2d7ef392009-09-20 23:13:10 +01004359 /* if the object is no longer bound, discard its backing storage */
4360 if (i915_gem_object_is_purgeable(obj_priv) &&
4361 obj_priv->gtt_space == NULL)
4362 i915_gem_object_truncate(obj);
4363
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004364 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4365
Chris Wilson3ef94da2009-09-14 16:50:29 +01004366 drm_gem_object_unreference(obj);
4367 mutex_unlock(&dev->struct_mutex);
4368
4369 return 0;
4370}
4371
Daniel Vetterac52bc52010-04-09 19:05:06 +00004372struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4373 size_t size)
4374{
Daniel Vetterc397b902010-04-09 19:05:07 +00004375 struct drm_i915_gem_object *obj;
4376
4377 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4378 if (obj == NULL)
4379 return NULL;
4380
4381 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4382 kfree(obj);
4383 return NULL;
4384 }
4385
4386 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4387 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4388
4389 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004390 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004391 obj->fence_reg = I915_FENCE_REG_NONE;
4392 INIT_LIST_HEAD(&obj->list);
4393 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004394 obj->madv = I915_MADV_WILLNEED;
4395
4396 trace_i915_gem_object_create(&obj->base);
4397
4398 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004399}
4400
Eric Anholt673a3942008-07-30 12:06:12 -07004401int i915_gem_init_object(struct drm_gem_object *obj)
4402{
Daniel Vetterc397b902010-04-09 19:05:07 +00004403 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004404
Eric Anholt673a3942008-07-30 12:06:12 -07004405 return 0;
4406}
4407
Chris Wilsonbe726152010-07-23 23:18:50 +01004408static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4409{
4410 struct drm_device *dev = obj->dev;
4411 drm_i915_private_t *dev_priv = dev->dev_private;
4412 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4413 int ret;
4414
4415 ret = i915_gem_object_unbind(obj);
4416 if (ret == -ERESTARTSYS) {
4417 list_move(&obj_priv->list,
4418 &dev_priv->mm.deferred_free_list);
4419 return;
4420 }
4421
4422 if (obj_priv->mmap_offset)
4423 i915_gem_free_mmap_offset(obj);
4424
4425 drm_gem_object_release(obj);
4426
4427 kfree(obj_priv->page_cpu_valid);
4428 kfree(obj_priv->bit_17);
4429 kfree(obj_priv);
4430}
4431
Eric Anholt673a3942008-07-30 12:06:12 -07004432void i915_gem_free_object(struct drm_gem_object *obj)
4433{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004434 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004435 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004436
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004437 trace_i915_gem_object_destroy(obj);
4438
Eric Anholt673a3942008-07-30 12:06:12 -07004439 while (obj_priv->pin_count > 0)
4440 i915_gem_object_unpin(obj);
4441
Dave Airlie71acb5e2008-12-30 20:31:46 +10004442 if (obj_priv->phys_obj)
4443 i915_gem_detach_phys_object(dev, obj);
4444
Chris Wilsonbe726152010-07-23 23:18:50 +01004445 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004446}
4447
Jesse Barnes5669fca2009-02-17 15:13:31 -08004448int
Eric Anholt673a3942008-07-30 12:06:12 -07004449i915_gem_idle(struct drm_device *dev)
4450{
4451 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004452 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004453
Keith Packard6dbe2772008-10-14 21:41:13 -07004454 mutex_lock(&dev->struct_mutex);
4455
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004456 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004457 (dev_priv->render_ring.gem_object == NULL) ||
4458 (HAS_BSD(dev) &&
4459 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004460 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004461 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004462 }
Eric Anholt673a3942008-07-30 12:06:12 -07004463
Chris Wilson29105cc2010-01-07 10:39:13 +00004464 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004465 if (ret) {
4466 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004467 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004468 }
Eric Anholt673a3942008-07-30 12:06:12 -07004469
Chris Wilson29105cc2010-01-07 10:39:13 +00004470 /* Under UMS, be paranoid and evict. */
4471 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004472 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004473 if (ret) {
4474 mutex_unlock(&dev->struct_mutex);
4475 return ret;
4476 }
4477 }
4478
4479 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4480 * We need to replace this with a semaphore, or something.
4481 * And not confound mm.suspended!
4482 */
4483 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004484 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004485
4486 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004487 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004488
Keith Packard6dbe2772008-10-14 21:41:13 -07004489 mutex_unlock(&dev->struct_mutex);
4490
Chris Wilson29105cc2010-01-07 10:39:13 +00004491 /* Cancel the retire work handler, which should be idle now. */
4492 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4493
Eric Anholt673a3942008-07-30 12:06:12 -07004494 return 0;
4495}
4496
Jesse Barnese552eb72010-04-21 11:39:23 -07004497/*
4498 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4499 * over cache flushing.
4500 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004501static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004502i915_gem_init_pipe_control(struct drm_device *dev)
4503{
4504 drm_i915_private_t *dev_priv = dev->dev_private;
4505 struct drm_gem_object *obj;
4506 struct drm_i915_gem_object *obj_priv;
4507 int ret;
4508
Eric Anholt34dc4d42010-05-07 14:30:03 -07004509 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004510 if (obj == NULL) {
4511 DRM_ERROR("Failed to allocate seqno page\n");
4512 ret = -ENOMEM;
4513 goto err;
4514 }
4515 obj_priv = to_intel_bo(obj);
4516 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4517
4518 ret = i915_gem_object_pin(obj, 4096);
4519 if (ret)
4520 goto err_unref;
4521
4522 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4523 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4524 if (dev_priv->seqno_page == NULL)
4525 goto err_unpin;
4526
4527 dev_priv->seqno_obj = obj;
4528 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4529
4530 return 0;
4531
4532err_unpin:
4533 i915_gem_object_unpin(obj);
4534err_unref:
4535 drm_gem_object_unreference(obj);
4536err:
4537 return ret;
4538}
4539
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004540
4541static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004542i915_gem_cleanup_pipe_control(struct drm_device *dev)
4543{
4544 drm_i915_private_t *dev_priv = dev->dev_private;
4545 struct drm_gem_object *obj;
4546 struct drm_i915_gem_object *obj_priv;
4547
4548 obj = dev_priv->seqno_obj;
4549 obj_priv = to_intel_bo(obj);
4550 kunmap(obj_priv->pages[0]);
4551 i915_gem_object_unpin(obj);
4552 drm_gem_object_unreference(obj);
4553 dev_priv->seqno_obj = NULL;
4554
4555 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004556}
4557
Eric Anholt673a3942008-07-30 12:06:12 -07004558int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004559i915_gem_init_ringbuffer(struct drm_device *dev)
4560{
4561 drm_i915_private_t *dev_priv = dev->dev_private;
4562 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004563
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004564 if (HAS_PIPE_CONTROL(dev)) {
4565 ret = i915_gem_init_pipe_control(dev);
4566 if (ret)
4567 return ret;
4568 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004569
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004570 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004571 if (ret)
4572 goto cleanup_pipe_control;
4573
4574 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004575 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004576 if (ret)
4577 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004578 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004579
Chris Wilson6f392d52010-08-07 11:01:22 +01004580 dev_priv->next_seqno = 1;
4581
Chris Wilson68f95ba2010-05-27 13:18:22 +01004582 return 0;
4583
4584cleanup_render_ring:
4585 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4586cleanup_pipe_control:
4587 if (HAS_PIPE_CONTROL(dev))
4588 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004589 return ret;
4590}
4591
4592void
4593i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4594{
4595 drm_i915_private_t *dev_priv = dev->dev_private;
4596
4597 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004598 if (HAS_BSD(dev))
4599 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004600 if (HAS_PIPE_CONTROL(dev))
4601 i915_gem_cleanup_pipe_control(dev);
4602}
4603
4604int
Eric Anholt673a3942008-07-30 12:06:12 -07004605i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4606 struct drm_file *file_priv)
4607{
4608 drm_i915_private_t *dev_priv = dev->dev_private;
4609 int ret;
4610
Jesse Barnes79e53942008-11-07 14:24:08 -08004611 if (drm_core_check_feature(dev, DRIVER_MODESET))
4612 return 0;
4613
Ben Gamariba1234d2009-09-14 17:48:47 -04004614 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004615 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004616 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004617 }
4618
Eric Anholt673a3942008-07-30 12:06:12 -07004619 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004620 dev_priv->mm.suspended = 0;
4621
4622 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004623 if (ret != 0) {
4624 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004625 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004626 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004627
Zou Nan hai852835f2010-05-21 09:08:56 +08004628 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004629 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004630 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4631 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004632 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004633 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004634 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004635
Chris Wilson5f353082010-06-07 14:03:03 +01004636 ret = drm_irq_install(dev);
4637 if (ret)
4638 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004639
Eric Anholt673a3942008-07-30 12:06:12 -07004640 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004641
4642cleanup_ringbuffer:
4643 mutex_lock(&dev->struct_mutex);
4644 i915_gem_cleanup_ringbuffer(dev);
4645 dev_priv->mm.suspended = 1;
4646 mutex_unlock(&dev->struct_mutex);
4647
4648 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004649}
4650
4651int
4652i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4653 struct drm_file *file_priv)
4654{
Jesse Barnes79e53942008-11-07 14:24:08 -08004655 if (drm_core_check_feature(dev, DRIVER_MODESET))
4656 return 0;
4657
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004658 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004659 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004660}
4661
4662void
4663i915_gem_lastclose(struct drm_device *dev)
4664{
4665 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004666
Eric Anholte806b492009-01-22 09:56:58 -08004667 if (drm_core_check_feature(dev, DRIVER_MODESET))
4668 return;
4669
Keith Packard6dbe2772008-10-14 21:41:13 -07004670 ret = i915_gem_idle(dev);
4671 if (ret)
4672 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004673}
4674
4675void
4676i915_gem_load(struct drm_device *dev)
4677{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004678 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004679 drm_i915_private_t *dev_priv = dev->dev_private;
4680
Eric Anholt673a3942008-07-30 12:06:12 -07004681 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004682 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004683 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004684 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004685 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004686 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004687 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4688 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004689 if (HAS_BSD(dev)) {
4690 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4691 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4692 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004693 for (i = 0; i < 16; i++)
4694 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004695 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4696 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004697 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004698 spin_lock(&shrink_list_lock);
4699 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4700 spin_unlock(&shrink_list_lock);
4701
Dave Airlie94400122010-07-20 13:15:31 +10004702 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4703 if (IS_GEN3(dev)) {
4704 u32 tmp = I915_READ(MI_ARB_STATE);
4705 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4706 /* arb state is a masked write, so set bit + bit in mask */
4707 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4708 I915_WRITE(MI_ARB_STATE, tmp);
4709 }
4710 }
4711
Jesse Barnesde151cf2008-11-12 10:03:55 -08004712 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004713 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4714 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004715
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004716 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004717 dev_priv->num_fence_regs = 16;
4718 else
4719 dev_priv->num_fence_regs = 8;
4720
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004721 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004722 switch (INTEL_INFO(dev)->gen) {
4723 case 6:
4724 for (i = 0; i < 16; i++)
4725 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4726 break;
4727 case 5:
4728 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004729 for (i = 0; i < 16; i++)
4730 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004731 break;
4732 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004733 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4734 for (i = 0; i < 8; i++)
4735 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004736 case 2:
4737 for (i = 0; i < 8; i++)
4738 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4739 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004740 }
Eric Anholt673a3942008-07-30 12:06:12 -07004741 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004742 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004743}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004744
4745/*
4746 * Create a physically contiguous memory object for this object
4747 * e.g. for cursor + overlay regs
4748 */
Chris Wilson995b6762010-08-20 13:23:26 +01004749static int i915_gem_init_phys_object(struct drm_device *dev,
4750 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004751{
4752 drm_i915_private_t *dev_priv = dev->dev_private;
4753 struct drm_i915_gem_phys_object *phys_obj;
4754 int ret;
4755
4756 if (dev_priv->mm.phys_objs[id - 1] || !size)
4757 return 0;
4758
Eric Anholt9a298b22009-03-24 12:23:04 -07004759 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004760 if (!phys_obj)
4761 return -ENOMEM;
4762
4763 phys_obj->id = id;
4764
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004765 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004766 if (!phys_obj->handle) {
4767 ret = -ENOMEM;
4768 goto kfree_obj;
4769 }
4770#ifdef CONFIG_X86
4771 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4772#endif
4773
4774 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4775
4776 return 0;
4777kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004778 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004779 return ret;
4780}
4781
Chris Wilson995b6762010-08-20 13:23:26 +01004782static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004783{
4784 drm_i915_private_t *dev_priv = dev->dev_private;
4785 struct drm_i915_gem_phys_object *phys_obj;
4786
4787 if (!dev_priv->mm.phys_objs[id - 1])
4788 return;
4789
4790 phys_obj = dev_priv->mm.phys_objs[id - 1];
4791 if (phys_obj->cur_obj) {
4792 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4793 }
4794
4795#ifdef CONFIG_X86
4796 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4797#endif
4798 drm_pci_free(dev, phys_obj->handle);
4799 kfree(phys_obj);
4800 dev_priv->mm.phys_objs[id - 1] = NULL;
4801}
4802
4803void i915_gem_free_all_phys_object(struct drm_device *dev)
4804{
4805 int i;
4806
Dave Airlie260883c2009-01-22 17:58:49 +10004807 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004808 i915_gem_free_phys_object(dev, i);
4809}
4810
4811void i915_gem_detach_phys_object(struct drm_device *dev,
4812 struct drm_gem_object *obj)
4813{
4814 struct drm_i915_gem_object *obj_priv;
4815 int i;
4816 int ret;
4817 int page_count;
4818
Daniel Vetter23010e42010-03-08 13:35:02 +01004819 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004820 if (!obj_priv->phys_obj)
4821 return;
4822
Chris Wilson4bdadb92010-01-27 13:36:32 +00004823 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004824 if (ret)
4825 goto out;
4826
4827 page_count = obj->size / PAGE_SIZE;
4828
4829 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004830 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004831 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4832
4833 memcpy(dst, src, PAGE_SIZE);
4834 kunmap_atomic(dst, KM_USER0);
4835 }
Eric Anholt856fa192009-03-19 14:10:50 -07004836 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004837 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004838
4839 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004840out:
4841 obj_priv->phys_obj->cur_obj = NULL;
4842 obj_priv->phys_obj = NULL;
4843}
4844
4845int
4846i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004847 struct drm_gem_object *obj,
4848 int id,
4849 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004850{
4851 drm_i915_private_t *dev_priv = dev->dev_private;
4852 struct drm_i915_gem_object *obj_priv;
4853 int ret = 0;
4854 int page_count;
4855 int i;
4856
4857 if (id > I915_MAX_PHYS_OBJECT)
4858 return -EINVAL;
4859
Daniel Vetter23010e42010-03-08 13:35:02 +01004860 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004861
4862 if (obj_priv->phys_obj) {
4863 if (obj_priv->phys_obj->id == id)
4864 return 0;
4865 i915_gem_detach_phys_object(dev, obj);
4866 }
4867
Dave Airlie71acb5e2008-12-30 20:31:46 +10004868 /* create a new object */
4869 if (!dev_priv->mm.phys_objs[id - 1]) {
4870 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004871 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004872 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004873 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004874 goto out;
4875 }
4876 }
4877
4878 /* bind to the object */
4879 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4880 obj_priv->phys_obj->cur_obj = obj;
4881
Chris Wilson4bdadb92010-01-27 13:36:32 +00004882 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004883 if (ret) {
4884 DRM_ERROR("failed to get page list\n");
4885 goto out;
4886 }
4887
4888 page_count = obj->size / PAGE_SIZE;
4889
4890 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004891 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004892 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4893
4894 memcpy(dst, src, PAGE_SIZE);
4895 kunmap_atomic(src, KM_USER0);
4896 }
4897
Chris Wilsond78b47b2009-06-17 21:52:49 +01004898 i915_gem_object_put_pages(obj);
4899
Dave Airlie71acb5e2008-12-30 20:31:46 +10004900 return 0;
4901out:
4902 return ret;
4903}
4904
4905static int
4906i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4907 struct drm_i915_gem_pwrite *args,
4908 struct drm_file *file_priv)
4909{
Daniel Vetter23010e42010-03-08 13:35:02 +01004910 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004911 void *obj_addr;
4912 int ret;
4913 char __user *user_data;
4914
4915 user_data = (char __user *) (uintptr_t) args->data_ptr;
4916 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4917
Zhao Yakui44d98a62009-10-09 11:39:40 +08004918 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004919 ret = copy_from_user(obj_addr, user_data, args->size);
4920 if (ret)
4921 return -EFAULT;
4922
4923 drm_agp_chipset_flush(dev);
4924 return 0;
4925}
Eric Anholtb9624422009-06-03 07:27:35 +00004926
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004927void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004928{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004929 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004930
4931 /* Clean up our request list when the client is going away, so that
4932 * later retire_requests won't dereference our soon-to-be-gone
4933 * file_priv.
4934 */
4935 mutex_lock(&dev->struct_mutex);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004936 mutex_lock(&file_priv->mutex);
4937 while (!list_empty(&file_priv->mm.request_list)) {
4938 struct drm_i915_gem_request *request;
4939
4940 request = list_first_entry(&file_priv->mm.request_list,
4941 struct drm_i915_gem_request,
4942 client_list);
4943 list_del(&request->client_list);
4944 request->file_priv = NULL;
4945 }
4946 mutex_unlock(&file_priv->mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00004947 mutex_unlock(&dev->struct_mutex);
4948}
Chris Wilson31169712009-09-14 16:50:28 +01004949
Chris Wilson31169712009-09-14 16:50:28 +01004950static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004951i915_gpu_is_active(struct drm_device *dev)
4952{
4953 drm_i915_private_t *dev_priv = dev->dev_private;
4954 int lists_empty;
4955
Chris Wilson1637ef42010-04-20 17:10:35 +01004956 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004957 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004958 if (HAS_BSD(dev))
4959 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004960
4961 return !lists_empty;
4962}
4963
4964static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004965i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004966{
4967 drm_i915_private_t *dev_priv, *next_dev;
4968 struct drm_i915_gem_object *obj_priv, *next_obj;
4969 int cnt = 0;
4970 int would_deadlock = 1;
4971
4972 /* "fast-path" to count number of available objects */
4973 if (nr_to_scan == 0) {
4974 spin_lock(&shrink_list_lock);
4975 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4976 struct drm_device *dev = dev_priv->dev;
4977
4978 if (mutex_trylock(&dev->struct_mutex)) {
4979 list_for_each_entry(obj_priv,
4980 &dev_priv->mm.inactive_list,
4981 list)
4982 cnt++;
4983 mutex_unlock(&dev->struct_mutex);
4984 }
4985 }
4986 spin_unlock(&shrink_list_lock);
4987
4988 return (cnt / 100) * sysctl_vfs_cache_pressure;
4989 }
4990
4991 spin_lock(&shrink_list_lock);
4992
Chris Wilson1637ef42010-04-20 17:10:35 +01004993rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004994 /* first scan for clean buffers */
4995 list_for_each_entry_safe(dev_priv, next_dev,
4996 &shrink_list, mm.shrink_list) {
4997 struct drm_device *dev = dev_priv->dev;
4998
4999 if (! mutex_trylock(&dev->struct_mutex))
5000 continue;
5001
5002 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005003 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005004
Chris Wilson31169712009-09-14 16:50:28 +01005005 list_for_each_entry_safe(obj_priv, next_obj,
5006 &dev_priv->mm.inactive_list,
5007 list) {
5008 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005009 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005010 if (--nr_to_scan <= 0)
5011 break;
5012 }
5013 }
5014
5015 spin_lock(&shrink_list_lock);
5016 mutex_unlock(&dev->struct_mutex);
5017
Chris Wilson963b4832009-09-20 23:03:54 +01005018 would_deadlock = 0;
5019
Chris Wilson31169712009-09-14 16:50:28 +01005020 if (nr_to_scan <= 0)
5021 break;
5022 }
5023
5024 /* second pass, evict/count anything still on the inactive list */
5025 list_for_each_entry_safe(dev_priv, next_dev,
5026 &shrink_list, mm.shrink_list) {
5027 struct drm_device *dev = dev_priv->dev;
5028
5029 if (! mutex_trylock(&dev->struct_mutex))
5030 continue;
5031
5032 spin_unlock(&shrink_list_lock);
5033
5034 list_for_each_entry_safe(obj_priv, next_obj,
5035 &dev_priv->mm.inactive_list,
5036 list) {
5037 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005038 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005039 nr_to_scan--;
5040 } else
5041 cnt++;
5042 }
5043
5044 spin_lock(&shrink_list_lock);
5045 mutex_unlock(&dev->struct_mutex);
5046
5047 would_deadlock = 0;
5048 }
5049
Chris Wilson1637ef42010-04-20 17:10:35 +01005050 if (nr_to_scan) {
5051 int active = 0;
5052
5053 /*
5054 * We are desperate for pages, so as a last resort, wait
5055 * for the GPU to finish and discard whatever we can.
5056 * This has a dramatic impact to reduce the number of
5057 * OOM-killer events whilst running the GPU aggressively.
5058 */
5059 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5060 struct drm_device *dev = dev_priv->dev;
5061
5062 if (!mutex_trylock(&dev->struct_mutex))
5063 continue;
5064
5065 spin_unlock(&shrink_list_lock);
5066
5067 if (i915_gpu_is_active(dev)) {
5068 i915_gpu_idle(dev);
5069 active++;
5070 }
5071
5072 spin_lock(&shrink_list_lock);
5073 mutex_unlock(&dev->struct_mutex);
5074 }
5075
5076 if (active)
5077 goto rescan;
5078 }
5079
Chris Wilson31169712009-09-14 16:50:28 +01005080 spin_unlock(&shrink_list_lock);
5081
5082 if (would_deadlock)
5083 return -1;
5084 else if (cnt > 0)
5085 return (cnt / 100) * sysctl_vfs_cache_pressure;
5086 else
5087 return 0;
5088}
5089
5090static struct shrinker shrinker = {
5091 .shrink = i915_gem_shrink,
5092 .seeks = DEFAULT_SEEKS,
5093};
5094
5095__init void
5096i915_gem_shrinker_init(void)
5097{
5098 register_shrinker(&shrinker);
5099}
5100
5101__exit void
5102i915_gem_shrinker_exit(void)
5103{
5104 unregister_shrinker(&shrinker);
5105}