blob: 4c043cb02b389fda9e53baa95b46f7ca4d0937dd [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);
Chris Wilson2dafb1e2010-06-07 14:03:05 +010040static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080041static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
42static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080043static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
44 int write);
45static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
46 uint64_t offset,
47 uint64_t size);
48static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Daniel Vettere35a41d2010-02-11 22:13:59 +010049static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
50 bool interruptible);
Jesse Barnesde151cf2008-11-12 10:03:55 -080051static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
52 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080053static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100054static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
55 struct drm_i915_gem_pwrite *args,
56 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010057static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070058
Chris Wilson31169712009-09-14 16:50:28 +010059static LIST_HEAD(shrink_list);
60static DEFINE_SPINLOCK(shrink_list_lock);
61
Chris Wilson7d1c4802010-08-07 21:45:03 +010062static inline bool
63i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
64{
65 return obj_priv->gtt_space &&
66 !obj_priv->active &&
67 obj_priv->pin_count == 0;
68}
69
Jesse Barnes79e53942008-11-07 14:24:08 -080070int i915_gem_do_init(struct drm_device *dev, unsigned long start,
71 unsigned long end)
72{
73 drm_i915_private_t *dev_priv = dev->dev_private;
74
75 if (start >= end ||
76 (start & (PAGE_SIZE - 1)) != 0 ||
77 (end & (PAGE_SIZE - 1)) != 0) {
78 return -EINVAL;
79 }
80
81 drm_mm_init(&dev_priv->mm.gtt_space, start,
82 end - start);
83
84 dev->gtt_total = (uint32_t) (end - start);
85
86 return 0;
87}
Keith Packard6dbe2772008-10-14 21:41:13 -070088
Eric Anholt673a3942008-07-30 12:06:12 -070089int
90i915_gem_init_ioctl(struct drm_device *dev, void *data,
91 struct drm_file *file_priv)
92{
Eric Anholt673a3942008-07-30 12:06:12 -070093 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080094 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070095
96 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080097 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070098 mutex_unlock(&dev->struct_mutex);
99
Jesse Barnes79e53942008-11-07 14:24:08 -0800100 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700101}
102
Eric Anholt5a125c32008-10-22 21:40:13 -0700103int
104i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
105 struct drm_file *file_priv)
106{
Eric Anholt5a125c32008-10-22 21:40:13 -0700107 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700108
109 if (!(dev->driver->driver_features & DRIVER_GEM))
110 return -ENODEV;
111
112 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800113 args->aper_available_size = (args->aper_size -
114 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700115
116 return 0;
117}
118
Eric Anholt673a3942008-07-30 12:06:12 -0700119
120/**
121 * Creates a new mm object and returns a handle to it.
122 */
123int
124i915_gem_create_ioctl(struct drm_device *dev, void *data,
125 struct drm_file *file_priv)
126{
127 struct drm_i915_gem_create *args = data;
128 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300129 int ret;
130 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700131
132 args->size = roundup(args->size, PAGE_SIZE);
133
134 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000135 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700136 if (obj == NULL)
137 return -ENOMEM;
138
139 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100140 if (ret) {
141 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700142 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100143 }
144
145 /* Sink the floating reference from kref_init(handlecount) */
146 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700147
148 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700149 return 0;
150}
151
Eric Anholt40123c12009-03-09 13:42:30 -0700152static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700153fast_shmem_read(struct page **pages,
154 loff_t page_base, int page_offset,
155 char __user *data,
156 int length)
157{
158 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200159 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700160
161 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
162 if (vaddr == NULL)
163 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200164 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700165 kunmap_atomic(vaddr, KM_USER0);
166
Florian Mickler2bc43b52009-04-06 22:55:41 +0200167 if (unwritten)
168 return -EFAULT;
169
170 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700171}
172
Eric Anholt280b7132009-03-12 16:56:27 -0700173static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
174{
175 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100176 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700177
178 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
179 obj_priv->tiling_mode != I915_TILING_NONE;
180}
181
Chris Wilson99a03df2010-05-27 14:15:34 +0100182static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700183slow_shmem_copy(struct page *dst_page,
184 int dst_offset,
185 struct page *src_page,
186 int src_offset,
187 int length)
188{
189 char *dst_vaddr, *src_vaddr;
190
Chris Wilson99a03df2010-05-27 14:15:34 +0100191 dst_vaddr = kmap(dst_page);
192 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700193
194 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
195
Chris Wilson99a03df2010-05-27 14:15:34 +0100196 kunmap(src_page);
197 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700198}
199
Chris Wilson99a03df2010-05-27 14:15:34 +0100200static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700201slow_shmem_bit17_copy(struct page *gpu_page,
202 int gpu_offset,
203 struct page *cpu_page,
204 int cpu_offset,
205 int length,
206 int is_read)
207{
208 char *gpu_vaddr, *cpu_vaddr;
209
210 /* Use the unswizzled path if this page isn't affected. */
211 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
212 if (is_read)
213 return slow_shmem_copy(cpu_page, cpu_offset,
214 gpu_page, gpu_offset, length);
215 else
216 return slow_shmem_copy(gpu_page, gpu_offset,
217 cpu_page, cpu_offset, length);
218 }
219
Chris Wilson99a03df2010-05-27 14:15:34 +0100220 gpu_vaddr = kmap(gpu_page);
221 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700222
223 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
224 * XORing with the other bits (A9 for Y, A9 and A10 for X)
225 */
226 while (length > 0) {
227 int cacheline_end = ALIGN(gpu_offset + 1, 64);
228 int this_length = min(cacheline_end - gpu_offset, length);
229 int swizzled_gpu_offset = gpu_offset ^ 64;
230
231 if (is_read) {
232 memcpy(cpu_vaddr + cpu_offset,
233 gpu_vaddr + swizzled_gpu_offset,
234 this_length);
235 } else {
236 memcpy(gpu_vaddr + swizzled_gpu_offset,
237 cpu_vaddr + cpu_offset,
238 this_length);
239 }
240 cpu_offset += this_length;
241 gpu_offset += this_length;
242 length -= this_length;
243 }
244
Chris Wilson99a03df2010-05-27 14:15:34 +0100245 kunmap(cpu_page);
246 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700247}
248
Eric Anholt673a3942008-07-30 12:06:12 -0700249/**
Eric Anholteb014592009-03-10 11:44:52 -0700250 * This is the fast shmem pread path, which attempts to copy_from_user directly
251 * from the backing pages of the object to the user's address space. On a
252 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
253 */
254static int
255i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
256 struct drm_i915_gem_pread *args,
257 struct drm_file *file_priv)
258{
Daniel Vetter23010e42010-03-08 13:35:02 +0100259 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700260 ssize_t remain;
261 loff_t offset, page_base;
262 char __user *user_data;
263 int page_offset, page_length;
264 int ret;
265
266 user_data = (char __user *) (uintptr_t) args->data_ptr;
267 remain = args->size;
268
269 mutex_lock(&dev->struct_mutex);
270
Chris Wilson4bdadb92010-01-27 13:36:32 +0000271 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700272 if (ret != 0)
273 goto fail_unlock;
274
275 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
276 args->size);
277 if (ret != 0)
278 goto fail_put_pages;
279
Daniel Vetter23010e42010-03-08 13:35:02 +0100280 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700281 offset = args->offset;
282
283 while (remain > 0) {
284 /* Operation in this page
285 *
286 * page_base = page offset within aperture
287 * page_offset = offset within page
288 * page_length = bytes to copy for this page
289 */
290 page_base = (offset & ~(PAGE_SIZE-1));
291 page_offset = offset & (PAGE_SIZE-1);
292 page_length = remain;
293 if ((page_offset + remain) > PAGE_SIZE)
294 page_length = PAGE_SIZE - page_offset;
295
296 ret = fast_shmem_read(obj_priv->pages,
297 page_base, page_offset,
298 user_data, page_length);
299 if (ret)
300 goto fail_put_pages;
301
302 remain -= page_length;
303 user_data += page_length;
304 offset += page_length;
305 }
306
307fail_put_pages:
308 i915_gem_object_put_pages(obj);
309fail_unlock:
310 mutex_unlock(&dev->struct_mutex);
311
312 return ret;
313}
314
Chris Wilson07f73f62009-09-14 16:50:30 +0100315static int
316i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
317{
318 int ret;
319
Chris Wilson4bdadb92010-01-27 13:36:32 +0000320 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100321
322 /* If we've insufficient memory to map in the pages, attempt
323 * to make some space by throwing out some old buffers.
324 */
325 if (ret == -ENOMEM) {
326 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100327
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100328 ret = i915_gem_evict_something(dev, obj->size,
329 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100330 if (ret)
331 return ret;
332
Chris Wilson4bdadb92010-01-27 13:36:32 +0000333 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100334 }
335
336 return ret;
337}
338
Eric Anholteb014592009-03-10 11:44:52 -0700339/**
340 * This is the fallback shmem pread path, which allocates temporary storage
341 * in kernel space to copy_to_user into outside of the struct_mutex, so we
342 * can copy out of the object's backing pages while holding the struct mutex
343 * and not take page faults.
344 */
345static int
346i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
347 struct drm_i915_gem_pread *args,
348 struct drm_file *file_priv)
349{
Daniel Vetter23010e42010-03-08 13:35:02 +0100350 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700351 struct mm_struct *mm = current->mm;
352 struct page **user_pages;
353 ssize_t remain;
354 loff_t offset, pinned_pages, i;
355 loff_t first_data_page, last_data_page, num_pages;
356 int shmem_page_index, shmem_page_offset;
357 int data_page_index, data_page_offset;
358 int page_length;
359 int ret;
360 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700361 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700362
363 remain = args->size;
364
365 /* Pin the user pages containing the data. We can't fault while
366 * holding the struct mutex, yet we want to hold it while
367 * dereferencing the user data.
368 */
369 first_data_page = data_ptr / PAGE_SIZE;
370 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
371 num_pages = last_data_page - first_data_page + 1;
372
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700373 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700374 if (user_pages == NULL)
375 return -ENOMEM;
376
377 down_read(&mm->mmap_sem);
378 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700379 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700380 up_read(&mm->mmap_sem);
381 if (pinned_pages < num_pages) {
382 ret = -EFAULT;
383 goto fail_put_user_pages;
384 }
385
Eric Anholt280b7132009-03-12 16:56:27 -0700386 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
387
Eric Anholteb014592009-03-10 11:44:52 -0700388 mutex_lock(&dev->struct_mutex);
389
Chris Wilson07f73f62009-09-14 16:50:30 +0100390 ret = i915_gem_object_get_pages_or_evict(obj);
391 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700392 goto fail_unlock;
393
394 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
395 args->size);
396 if (ret != 0)
397 goto fail_put_pages;
398
Daniel Vetter23010e42010-03-08 13:35:02 +0100399 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700400 offset = args->offset;
401
402 while (remain > 0) {
403 /* Operation in this page
404 *
405 * shmem_page_index = page number within shmem file
406 * shmem_page_offset = offset within page in shmem file
407 * data_page_index = page number in get_user_pages return
408 * data_page_offset = offset with data_page_index page.
409 * page_length = bytes to copy for this page
410 */
411 shmem_page_index = offset / PAGE_SIZE;
412 shmem_page_offset = offset & ~PAGE_MASK;
413 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
414 data_page_offset = data_ptr & ~PAGE_MASK;
415
416 page_length = remain;
417 if ((shmem_page_offset + page_length) > PAGE_SIZE)
418 page_length = PAGE_SIZE - shmem_page_offset;
419 if ((data_page_offset + page_length) > PAGE_SIZE)
420 page_length = PAGE_SIZE - data_page_offset;
421
Eric Anholt280b7132009-03-12 16:56:27 -0700422 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100423 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700424 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100425 user_pages[data_page_index],
426 data_page_offset,
427 page_length,
428 1);
429 } else {
430 slow_shmem_copy(user_pages[data_page_index],
431 data_page_offset,
432 obj_priv->pages[shmem_page_index],
433 shmem_page_offset,
434 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700435 }
Eric Anholteb014592009-03-10 11:44:52 -0700436
437 remain -= page_length;
438 data_ptr += page_length;
439 offset += page_length;
440 }
441
442fail_put_pages:
443 i915_gem_object_put_pages(obj);
444fail_unlock:
445 mutex_unlock(&dev->struct_mutex);
446fail_put_user_pages:
447 for (i = 0; i < pinned_pages; i++) {
448 SetPageDirty(user_pages[i]);
449 page_cache_release(user_pages[i]);
450 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700451 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700452
453 return ret;
454}
455
Eric Anholt673a3942008-07-30 12:06:12 -0700456/**
457 * Reads data from the object referenced by handle.
458 *
459 * On error, the contents of *data are undefined.
460 */
461int
462i915_gem_pread_ioctl(struct drm_device *dev, void *data,
463 struct drm_file *file_priv)
464{
465 struct drm_i915_gem_pread *args = data;
466 struct drm_gem_object *obj;
467 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700468 int ret;
469
470 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
471 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100472 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100473 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700474
475 /* Bounds check source.
476 *
477 * XXX: This could use review for overflow issues...
478 */
479 if (args->offset > obj->size || args->size > obj->size ||
480 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000481 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700482 return -EINVAL;
483 }
484
Eric Anholt280b7132009-03-12 16:56:27 -0700485 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700486 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700487 } else {
488 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
489 if (ret != 0)
490 ret = i915_gem_shmem_pread_slow(dev, obj, args,
491 file_priv);
492 }
Eric Anholt673a3942008-07-30 12:06:12 -0700493
Luca Barbieribc9025b2010-02-09 05:49:12 +0000494 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700495
Eric Anholteb014592009-03-10 11:44:52 -0700496 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700497}
498
Keith Packard0839ccb2008-10-30 19:38:48 -0700499/* This is the fast write path which cannot handle
500 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700501 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700502
Keith Packard0839ccb2008-10-30 19:38:48 -0700503static inline int
504fast_user_write(struct io_mapping *mapping,
505 loff_t page_base, int page_offset,
506 char __user *user_data,
507 int length)
508{
509 char *vaddr_atomic;
510 unsigned long unwritten;
511
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100512 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700513 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
514 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100515 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700516 if (unwritten)
517 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700518 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700519}
520
521/* Here's the write path which can sleep for
522 * page faults
523 */
524
Chris Wilsonab34c222010-05-27 14:15:35 +0100525static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700526slow_kernel_write(struct io_mapping *mapping,
527 loff_t gtt_base, int gtt_offset,
528 struct page *user_page, int user_offset,
529 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700530{
Chris Wilsonab34c222010-05-27 14:15:35 +0100531 char __iomem *dst_vaddr;
532 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700533
Chris Wilsonab34c222010-05-27 14:15:35 +0100534 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
535 src_vaddr = kmap(user_page);
536
537 memcpy_toio(dst_vaddr + gtt_offset,
538 src_vaddr + user_offset,
539 length);
540
541 kunmap(user_page);
542 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700543}
544
Eric Anholt40123c12009-03-09 13:42:30 -0700545static inline int
546fast_shmem_write(struct page **pages,
547 loff_t page_base, int page_offset,
548 char __user *data,
549 int length)
550{
551 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400552 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700553
554 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
555 if (vaddr == NULL)
556 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400557 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700558 kunmap_atomic(vaddr, KM_USER0);
559
Dave Airlied0088772009-03-28 20:29:48 -0400560 if (unwritten)
561 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700562 return 0;
563}
564
Eric Anholt3de09aa2009-03-09 09:42:23 -0700565/**
566 * This is the fast pwrite path, where we copy the data directly from the
567 * user into the GTT, uncached.
568 */
Eric Anholt673a3942008-07-30 12:06:12 -0700569static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700570i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
571 struct drm_i915_gem_pwrite *args,
572 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700573{
Daniel Vetter23010e42010-03-08 13:35:02 +0100574 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700575 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700576 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700577 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700578 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700579 int page_offset, page_length;
580 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700581
582 user_data = (char __user *) (uintptr_t) args->data_ptr;
583 remain = args->size;
584 if (!access_ok(VERIFY_READ, user_data, remain))
585 return -EFAULT;
586
587
588 mutex_lock(&dev->struct_mutex);
589 ret = i915_gem_object_pin(obj, 0);
590 if (ret) {
591 mutex_unlock(&dev->struct_mutex);
592 return ret;
593 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800594 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700595 if (ret)
596 goto fail;
597
Daniel Vetter23010e42010-03-08 13:35:02 +0100598 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700599 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700600
601 while (remain > 0) {
602 /* Operation in this page
603 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700604 * page_base = page offset within aperture
605 * page_offset = offset within page
606 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700607 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700608 page_base = (offset & ~(PAGE_SIZE-1));
609 page_offset = offset & (PAGE_SIZE-1);
610 page_length = remain;
611 if ((page_offset + remain) > PAGE_SIZE)
612 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700613
Keith Packard0839ccb2008-10-30 19:38:48 -0700614 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
615 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700616
Keith Packard0839ccb2008-10-30 19:38:48 -0700617 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700618 * source page isn't available. Return the error and we'll
619 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700620 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700621 if (ret)
622 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700623
Keith Packard0839ccb2008-10-30 19:38:48 -0700624 remain -= page_length;
625 user_data += page_length;
626 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700627 }
Eric Anholt673a3942008-07-30 12:06:12 -0700628
629fail:
630 i915_gem_object_unpin(obj);
631 mutex_unlock(&dev->struct_mutex);
632
633 return ret;
634}
635
Eric Anholt3de09aa2009-03-09 09:42:23 -0700636/**
637 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
638 * the memory and maps it using kmap_atomic for copying.
639 *
640 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
641 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
642 */
Eric Anholt3043c602008-10-02 12:24:47 -0700643static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700644i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
645 struct drm_i915_gem_pwrite *args,
646 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700647{
Daniel Vetter23010e42010-03-08 13:35:02 +0100648 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700649 drm_i915_private_t *dev_priv = dev->dev_private;
650 ssize_t remain;
651 loff_t gtt_page_base, offset;
652 loff_t first_data_page, last_data_page, num_pages;
653 loff_t pinned_pages, i;
654 struct page **user_pages;
655 struct mm_struct *mm = current->mm;
656 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700657 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700658 uint64_t data_ptr = args->data_ptr;
659
660 remain = args->size;
661
662 /* Pin the user pages containing the data. We can't fault while
663 * holding the struct mutex, and all of the pwrite implementations
664 * want to hold it while dereferencing the user data.
665 */
666 first_data_page = data_ptr / PAGE_SIZE;
667 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
668 num_pages = last_data_page - first_data_page + 1;
669
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700670 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700671 if (user_pages == NULL)
672 return -ENOMEM;
673
674 down_read(&mm->mmap_sem);
675 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
676 num_pages, 0, 0, user_pages, NULL);
677 up_read(&mm->mmap_sem);
678 if (pinned_pages < num_pages) {
679 ret = -EFAULT;
680 goto out_unpin_pages;
681 }
682
683 mutex_lock(&dev->struct_mutex);
684 ret = i915_gem_object_pin(obj, 0);
685 if (ret)
686 goto out_unlock;
687
688 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
689 if (ret)
690 goto out_unpin_object;
691
Daniel Vetter23010e42010-03-08 13:35:02 +0100692 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700693 offset = obj_priv->gtt_offset + args->offset;
694
695 while (remain > 0) {
696 /* Operation in this page
697 *
698 * gtt_page_base = page offset within aperture
699 * gtt_page_offset = offset within page in aperture
700 * data_page_index = page number in get_user_pages return
701 * data_page_offset = offset with data_page_index page.
702 * page_length = bytes to copy for this page
703 */
704 gtt_page_base = offset & PAGE_MASK;
705 gtt_page_offset = offset & ~PAGE_MASK;
706 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
707 data_page_offset = data_ptr & ~PAGE_MASK;
708
709 page_length = remain;
710 if ((gtt_page_offset + page_length) > PAGE_SIZE)
711 page_length = PAGE_SIZE - gtt_page_offset;
712 if ((data_page_offset + page_length) > PAGE_SIZE)
713 page_length = PAGE_SIZE - data_page_offset;
714
Chris Wilsonab34c222010-05-27 14:15:35 +0100715 slow_kernel_write(dev_priv->mm.gtt_mapping,
716 gtt_page_base, gtt_page_offset,
717 user_pages[data_page_index],
718 data_page_offset,
719 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700720
721 remain -= page_length;
722 offset += page_length;
723 data_ptr += page_length;
724 }
725
726out_unpin_object:
727 i915_gem_object_unpin(obj);
728out_unlock:
729 mutex_unlock(&dev->struct_mutex);
730out_unpin_pages:
731 for (i = 0; i < pinned_pages; i++)
732 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700733 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700734
735 return ret;
736}
737
Eric Anholt40123c12009-03-09 13:42:30 -0700738/**
739 * This is the fast shmem pwrite path, which attempts to directly
740 * copy_from_user into the kmapped pages backing the object.
741 */
Eric Anholt673a3942008-07-30 12:06:12 -0700742static int
Eric Anholt40123c12009-03-09 13:42:30 -0700743i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
744 struct drm_i915_gem_pwrite *args,
745 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700746{
Daniel Vetter23010e42010-03-08 13:35:02 +0100747 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700748 ssize_t remain;
749 loff_t offset, page_base;
750 char __user *user_data;
751 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700752 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700753
754 user_data = (char __user *) (uintptr_t) args->data_ptr;
755 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700756
757 mutex_lock(&dev->struct_mutex);
758
Chris Wilson4bdadb92010-01-27 13:36:32 +0000759 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700760 if (ret != 0)
761 goto fail_unlock;
762
Eric Anholte47c68e2008-11-14 13:35:19 -0800763 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700764 if (ret != 0)
765 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700766
Daniel Vetter23010e42010-03-08 13:35:02 +0100767 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700768 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700769 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700770
Eric Anholt40123c12009-03-09 13:42:30 -0700771 while (remain > 0) {
772 /* Operation in this page
773 *
774 * page_base = page offset within aperture
775 * page_offset = offset within page
776 * page_length = bytes to copy for this page
777 */
778 page_base = (offset & ~(PAGE_SIZE-1));
779 page_offset = offset & (PAGE_SIZE-1);
780 page_length = remain;
781 if ((page_offset + remain) > PAGE_SIZE)
782 page_length = PAGE_SIZE - page_offset;
783
784 ret = fast_shmem_write(obj_priv->pages,
785 page_base, page_offset,
786 user_data, page_length);
787 if (ret)
788 goto fail_put_pages;
789
790 remain -= page_length;
791 user_data += page_length;
792 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700793 }
794
Eric Anholt40123c12009-03-09 13:42:30 -0700795fail_put_pages:
796 i915_gem_object_put_pages(obj);
797fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700798 mutex_unlock(&dev->struct_mutex);
799
Eric Anholt40123c12009-03-09 13:42:30 -0700800 return ret;
801}
802
803/**
804 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
805 * the memory and maps it using kmap_atomic for copying.
806 *
807 * This avoids taking mmap_sem for faulting on the user's address while the
808 * struct_mutex is held.
809 */
810static int
811i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
812 struct drm_i915_gem_pwrite *args,
813 struct drm_file *file_priv)
814{
Daniel Vetter23010e42010-03-08 13:35:02 +0100815 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700816 struct mm_struct *mm = current->mm;
817 struct page **user_pages;
818 ssize_t remain;
819 loff_t offset, pinned_pages, i;
820 loff_t first_data_page, last_data_page, num_pages;
821 int shmem_page_index, shmem_page_offset;
822 int data_page_index, data_page_offset;
823 int page_length;
824 int ret;
825 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700826 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700827
828 remain = args->size;
829
830 /* Pin the user pages containing the data. We can't fault while
831 * holding the struct mutex, and all of the pwrite implementations
832 * want to hold it while dereferencing the user data.
833 */
834 first_data_page = data_ptr / PAGE_SIZE;
835 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
836 num_pages = last_data_page - first_data_page + 1;
837
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700838 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700839 if (user_pages == NULL)
840 return -ENOMEM;
841
842 down_read(&mm->mmap_sem);
843 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
844 num_pages, 0, 0, user_pages, NULL);
845 up_read(&mm->mmap_sem);
846 if (pinned_pages < num_pages) {
847 ret = -EFAULT;
848 goto fail_put_user_pages;
849 }
850
Eric Anholt280b7132009-03-12 16:56:27 -0700851 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
852
Eric Anholt40123c12009-03-09 13:42:30 -0700853 mutex_lock(&dev->struct_mutex);
854
Chris Wilson07f73f62009-09-14 16:50:30 +0100855 ret = i915_gem_object_get_pages_or_evict(obj);
856 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700857 goto fail_unlock;
858
859 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
860 if (ret != 0)
861 goto fail_put_pages;
862
Daniel Vetter23010e42010-03-08 13:35:02 +0100863 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700864 offset = args->offset;
865 obj_priv->dirty = 1;
866
867 while (remain > 0) {
868 /* Operation in this page
869 *
870 * shmem_page_index = page number within shmem file
871 * shmem_page_offset = offset within page in shmem file
872 * data_page_index = page number in get_user_pages return
873 * data_page_offset = offset with data_page_index page.
874 * page_length = bytes to copy for this page
875 */
876 shmem_page_index = offset / PAGE_SIZE;
877 shmem_page_offset = offset & ~PAGE_MASK;
878 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
879 data_page_offset = data_ptr & ~PAGE_MASK;
880
881 page_length = remain;
882 if ((shmem_page_offset + page_length) > PAGE_SIZE)
883 page_length = PAGE_SIZE - shmem_page_offset;
884 if ((data_page_offset + page_length) > PAGE_SIZE)
885 page_length = PAGE_SIZE - data_page_offset;
886
Eric Anholt280b7132009-03-12 16:56:27 -0700887 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100888 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700889 shmem_page_offset,
890 user_pages[data_page_index],
891 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100892 page_length,
893 0);
894 } else {
895 slow_shmem_copy(obj_priv->pages[shmem_page_index],
896 shmem_page_offset,
897 user_pages[data_page_index],
898 data_page_offset,
899 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700900 }
Eric Anholt40123c12009-03-09 13:42:30 -0700901
902 remain -= page_length;
903 data_ptr += page_length;
904 offset += page_length;
905 }
906
907fail_put_pages:
908 i915_gem_object_put_pages(obj);
909fail_unlock:
910 mutex_unlock(&dev->struct_mutex);
911fail_put_user_pages:
912 for (i = 0; i < pinned_pages; i++)
913 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700914 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700915
916 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700917}
918
919/**
920 * Writes data to the object referenced by handle.
921 *
922 * On error, the contents of the buffer that were to be modified are undefined.
923 */
924int
925i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
926 struct drm_file *file_priv)
927{
928 struct drm_i915_gem_pwrite *args = data;
929 struct drm_gem_object *obj;
930 struct drm_i915_gem_object *obj_priv;
931 int ret = 0;
932
933 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
934 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100935 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100936 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700937
938 /* Bounds check destination.
939 *
940 * XXX: This could use review for overflow issues...
941 */
942 if (args->offset > obj->size || args->size > obj->size ||
943 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000944 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700945 return -EINVAL;
946 }
947
948 /* We can only do the GTT pwrite on untiled buffers, as otherwise
949 * it would end up going through the fenced access, and we'll get
950 * different detiling behavior between reading and writing.
951 * pread/pwrite currently are reading and writing from the CPU
952 * perspective, requiring manual detiling by the client.
953 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000954 if (obj_priv->phys_obj)
955 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
956 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100957 dev->gtt_total != 0 &&
958 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700959 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
960 if (ret == -EFAULT) {
961 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
962 file_priv);
963 }
Eric Anholt280b7132009-03-12 16:56:27 -0700964 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
965 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700966 } else {
967 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
968 if (ret == -EFAULT) {
969 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
970 file_priv);
971 }
972 }
Eric Anholt673a3942008-07-30 12:06:12 -0700973
974#if WATCH_PWRITE
975 if (ret)
976 DRM_INFO("pwrite failed %d\n", ret);
977#endif
978
Luca Barbieribc9025b2010-02-09 05:49:12 +0000979 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700980
981 return ret;
982}
983
984/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800985 * Called when user space prepares to use an object with the CPU, either
986 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700987 */
988int
989i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
990 struct drm_file *file_priv)
991{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700992 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700993 struct drm_i915_gem_set_domain *args = data;
994 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700995 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800996 uint32_t read_domains = args->read_domains;
997 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700998 int ret;
999
1000 if (!(dev->driver->driver_features & DRIVER_GEM))
1001 return -ENODEV;
1002
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001003 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001004 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001005 return -EINVAL;
1006
Chris Wilson21d509e2009-06-06 09:46:02 +01001007 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001008 return -EINVAL;
1009
1010 /* Having something in the write domain implies it's in the read
1011 * domain, and only that read domain. Enforce that in the request.
1012 */
1013 if (write_domain != 0 && read_domains != write_domain)
1014 return -EINVAL;
1015
Eric Anholt673a3942008-07-30 12:06:12 -07001016 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1017 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001018 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001019 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001020
1021 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001022
1023 intel_mark_busy(dev, obj);
1024
Eric Anholt673a3942008-07-30 12:06:12 -07001025#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001026 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001027 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001028#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001029 if (read_domains & I915_GEM_DOMAIN_GTT) {
1030 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001031
Eric Anholta09ba7f2009-08-29 12:49:51 -07001032 /* Update the LRU on the fence for the CPU access that's
1033 * about to occur.
1034 */
1035 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001036 struct drm_i915_fence_reg *reg =
1037 &dev_priv->fence_regs[obj_priv->fence_reg];
1038 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001039 &dev_priv->mm.fence_list);
1040 }
1041
Eric Anholt02354392008-11-26 13:58:13 -08001042 /* Silently promote "you're not bound, there was nothing to do"
1043 * to success, since the client was just asking us to
1044 * make sure everything was done.
1045 */
1046 if (ret == -EINVAL)
1047 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001048 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001049 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001050 }
1051
Chris Wilson7d1c4802010-08-07 21:45:03 +01001052
1053 /* Maintain LRU order of "inactive" objects */
1054 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1055 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1056
Eric Anholt673a3942008-07-30 12:06:12 -07001057 drm_gem_object_unreference(obj);
1058 mutex_unlock(&dev->struct_mutex);
1059 return ret;
1060}
1061
1062/**
1063 * Called when user space has done writes to this buffer
1064 */
1065int
1066i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1067 struct drm_file *file_priv)
1068{
1069 struct drm_i915_gem_sw_finish *args = data;
1070 struct drm_gem_object *obj;
1071 struct drm_i915_gem_object *obj_priv;
1072 int ret = 0;
1073
1074 if (!(dev->driver->driver_features & DRIVER_GEM))
1075 return -ENODEV;
1076
1077 mutex_lock(&dev->struct_mutex);
1078 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1079 if (obj == NULL) {
1080 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001081 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001082 }
1083
1084#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001085 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001086 __func__, args->handle, obj, obj->size);
1087#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001088 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001089
1090 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001091 if (obj_priv->pin_count)
1092 i915_gem_object_flush_cpu_write_domain(obj);
1093
Eric Anholt673a3942008-07-30 12:06:12 -07001094 drm_gem_object_unreference(obj);
1095 mutex_unlock(&dev->struct_mutex);
1096 return ret;
1097}
1098
1099/**
1100 * Maps the contents of an object, returning the address it is mapped
1101 * into.
1102 *
1103 * While the mapping holds a reference on the contents of the object, it doesn't
1104 * imply a ref on the object itself.
1105 */
1106int
1107i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1108 struct drm_file *file_priv)
1109{
1110 struct drm_i915_gem_mmap *args = data;
1111 struct drm_gem_object *obj;
1112 loff_t offset;
1113 unsigned long addr;
1114
1115 if (!(dev->driver->driver_features & DRIVER_GEM))
1116 return -ENODEV;
1117
1118 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1119 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001120 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001121
1122 offset = args->offset;
1123
1124 down_write(&current->mm->mmap_sem);
1125 addr = do_mmap(obj->filp, 0, args->size,
1126 PROT_READ | PROT_WRITE, MAP_SHARED,
1127 args->offset);
1128 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001129 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001130 if (IS_ERR((void *)addr))
1131 return addr;
1132
1133 args->addr_ptr = (uint64_t) addr;
1134
1135 return 0;
1136}
1137
Jesse Barnesde151cf2008-11-12 10:03:55 -08001138/**
1139 * i915_gem_fault - fault a page into the GTT
1140 * vma: VMA in question
1141 * vmf: fault info
1142 *
1143 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1144 * from userspace. The fault handler takes care of binding the object to
1145 * the GTT (if needed), allocating and programming a fence register (again,
1146 * only if needed based on whether the old reg is still valid or the object
1147 * is tiled) and inserting a new PTE into the faulting process.
1148 *
1149 * Note that the faulting process may involve evicting existing objects
1150 * from the GTT and/or fence registers to make room. So performance may
1151 * suffer if the GTT working set is large or there are few fence registers
1152 * left.
1153 */
1154int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1155{
1156 struct drm_gem_object *obj = vma->vm_private_data;
1157 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001158 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001159 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001160 pgoff_t page_offset;
1161 unsigned long pfn;
1162 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001163 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001164
1165 /* We don't use vmf->pgoff since that has the fake offset */
1166 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1167 PAGE_SHIFT;
1168
1169 /* Now bind it into the GTT if needed */
1170 mutex_lock(&dev->struct_mutex);
1171 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001172 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001173 if (ret)
1174 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001175
Jesse Barnesde151cf2008-11-12 10:03:55 -08001176 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001177 if (ret)
1178 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001179 }
1180
1181 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001182 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001183 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001184 if (ret)
1185 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001186 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001187
Chris Wilson7d1c4802010-08-07 21:45:03 +01001188 if (i915_gem_object_is_inactive(obj_priv))
1189 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1190
Jesse Barnesde151cf2008-11-12 10:03:55 -08001191 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1192 page_offset;
1193
1194 /* Finally, remap it using the new GTT offset */
1195 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001196unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001197 mutex_unlock(&dev->struct_mutex);
1198
1199 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001200 case 0:
1201 case -ERESTARTSYS:
1202 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001203 case -ENOMEM:
1204 case -EAGAIN:
1205 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001206 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001207 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001208 }
1209}
1210
1211/**
1212 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1213 * @obj: obj in question
1214 *
1215 * GEM memory mapping works by handing back to userspace a fake mmap offset
1216 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1217 * up the object based on the offset and sets up the various memory mapping
1218 * structures.
1219 *
1220 * This routine allocates and attaches a fake offset for @obj.
1221 */
1222static int
1223i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1224{
1225 struct drm_device *dev = obj->dev;
1226 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001227 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001228 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001229 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001230 int ret = 0;
1231
1232 /* Set the object up for mmap'ing */
1233 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001234 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001235 if (!list->map)
1236 return -ENOMEM;
1237
1238 map = list->map;
1239 map->type = _DRM_GEM;
1240 map->size = obj->size;
1241 map->handle = obj;
1242
1243 /* Get a DRM GEM mmap offset allocated... */
1244 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1245 obj->size / PAGE_SIZE, 0, 0);
1246 if (!list->file_offset_node) {
1247 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1248 ret = -ENOMEM;
1249 goto out_free_list;
1250 }
1251
1252 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1253 obj->size / PAGE_SIZE, 0);
1254 if (!list->file_offset_node) {
1255 ret = -ENOMEM;
1256 goto out_free_list;
1257 }
1258
1259 list->hash.key = list->file_offset_node->start;
1260 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1261 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001262 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263 goto out_free_mm;
1264 }
1265
1266 /* By now we should be all set, any drm_mmap request on the offset
1267 * below will get to our mmap & fault handler */
1268 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1269
1270 return 0;
1271
1272out_free_mm:
1273 drm_mm_put_block(list->file_offset_node);
1274out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001275 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001276
1277 return ret;
1278}
1279
Chris Wilson901782b2009-07-10 08:18:50 +01001280/**
1281 * i915_gem_release_mmap - remove physical page mappings
1282 * @obj: obj in question
1283 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001284 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001285 * relinquish ownership of the pages back to the system.
1286 *
1287 * It is vital that we remove the page mapping if we have mapped a tiled
1288 * object through the GTT and then lose the fence register due to
1289 * resource pressure. Similarly if the object has been moved out of the
1290 * aperture, than pages mapped into userspace must be revoked. Removing the
1291 * mapping will then trigger a page fault on the next user access, allowing
1292 * fixup by i915_gem_fault().
1293 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001294void
Chris Wilson901782b2009-07-10 08:18:50 +01001295i915_gem_release_mmap(struct drm_gem_object *obj)
1296{
1297 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001298 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001299
1300 if (dev->dev_mapping)
1301 unmap_mapping_range(dev->dev_mapping,
1302 obj_priv->mmap_offset, obj->size, 1);
1303}
1304
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001305static void
1306i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1307{
1308 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001309 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001310 struct drm_gem_mm *mm = dev->mm_private;
1311 struct drm_map_list *list;
1312
1313 list = &obj->map_list;
1314 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1315
1316 if (list->file_offset_node) {
1317 drm_mm_put_block(list->file_offset_node);
1318 list->file_offset_node = NULL;
1319 }
1320
1321 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001322 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001323 list->map = NULL;
1324 }
1325
1326 obj_priv->mmap_offset = 0;
1327}
1328
Jesse Barnesde151cf2008-11-12 10:03:55 -08001329/**
1330 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1331 * @obj: object to check
1332 *
1333 * Return the required GTT alignment for an object, taking into account
1334 * potential fence register mapping if needed.
1335 */
1336static uint32_t
1337i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1338{
1339 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001340 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001341 int start, i;
1342
1343 /*
1344 * Minimum alignment is 4k (GTT page size), but might be greater
1345 * if a fence register is needed for the object.
1346 */
1347 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1348 return 4096;
1349
1350 /*
1351 * Previous chips need to be aligned to the size of the smallest
1352 * fence register that can contain the object.
1353 */
1354 if (IS_I9XX(dev))
1355 start = 1024*1024;
1356 else
1357 start = 512*1024;
1358
1359 for (i = start; i < obj->size; i <<= 1)
1360 ;
1361
1362 return i;
1363}
1364
1365/**
1366 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1367 * @dev: DRM device
1368 * @data: GTT mapping ioctl data
1369 * @file_priv: GEM object info
1370 *
1371 * Simply returns the fake offset to userspace so it can mmap it.
1372 * The mmap call will end up in drm_gem_mmap(), which will set things
1373 * up so we can get faults in the handler above.
1374 *
1375 * The fault handler will take care of binding the object into the GTT
1376 * (since it may have been evicted to make room for something), allocating
1377 * a fence register, and mapping the appropriate aperture address into
1378 * userspace.
1379 */
1380int
1381i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1382 struct drm_file *file_priv)
1383{
1384 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001385 struct drm_gem_object *obj;
1386 struct drm_i915_gem_object *obj_priv;
1387 int ret;
1388
1389 if (!(dev->driver->driver_features & DRIVER_GEM))
1390 return -ENODEV;
1391
1392 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1393 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001394 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001395
1396 mutex_lock(&dev->struct_mutex);
1397
Daniel Vetter23010e42010-03-08 13:35:02 +01001398 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001399
Chris Wilsonab182822009-09-22 18:46:17 +01001400 if (obj_priv->madv != I915_MADV_WILLNEED) {
1401 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1402 drm_gem_object_unreference(obj);
1403 mutex_unlock(&dev->struct_mutex);
1404 return -EINVAL;
1405 }
1406
1407
Jesse Barnesde151cf2008-11-12 10:03:55 -08001408 if (!obj_priv->mmap_offset) {
1409 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001410 if (ret) {
1411 drm_gem_object_unreference(obj);
1412 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001413 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001414 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001415 }
1416
1417 args->offset = obj_priv->mmap_offset;
1418
Jesse Barnesde151cf2008-11-12 10:03:55 -08001419 /*
1420 * Pull it into the GTT so that we have a page list (makes the
1421 * initial fault faster and any subsequent flushing possible).
1422 */
1423 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001424 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001425 if (ret) {
1426 drm_gem_object_unreference(obj);
1427 mutex_unlock(&dev->struct_mutex);
1428 return ret;
1429 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001430 }
1431
1432 drm_gem_object_unreference(obj);
1433 mutex_unlock(&dev->struct_mutex);
1434
1435 return 0;
1436}
1437
Ben Gamari6911a9b2009-04-02 11:24:54 -07001438void
Eric Anholt856fa192009-03-19 14:10:50 -07001439i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001440{
Daniel Vetter23010e42010-03-08 13:35:02 +01001441 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001442 int page_count = obj->size / PAGE_SIZE;
1443 int i;
1444
Eric Anholt856fa192009-03-19 14:10:50 -07001445 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001446 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001447
1448 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001449 return;
1450
Eric Anholt280b7132009-03-12 16:56:27 -07001451 if (obj_priv->tiling_mode != I915_TILING_NONE)
1452 i915_gem_object_save_bit_17_swizzle(obj);
1453
Chris Wilson3ef94da2009-09-14 16:50:29 +01001454 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001455 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001456
1457 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001458 if (obj_priv->dirty)
1459 set_page_dirty(obj_priv->pages[i]);
1460
1461 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001462 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001463
1464 page_cache_release(obj_priv->pages[i]);
1465 }
Eric Anholt673a3942008-07-30 12:06:12 -07001466 obj_priv->dirty = 0;
1467
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001468 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001469 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001470}
1471
Daniel Vettere35a41d2010-02-11 22:13:59 +01001472static uint32_t
Daniel Vettera6910432010-02-02 17:08:37 +01001473i915_gem_next_request_seqno(struct drm_device *dev,
1474 struct intel_ring_buffer *ring)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001475{
1476 drm_i915_private_t *dev_priv = dev->dev_private;
1477
Daniel Vettera6910432010-02-02 17:08:37 +01001478 ring->outstanding_lazy_request = true;
1479
Daniel Vettere35a41d2010-02-11 22:13:59 +01001480 return dev_priv->next_seqno;
1481}
1482
Eric Anholt673a3942008-07-30 12:06:12 -07001483static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001484i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001485 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001486{
1487 struct drm_device *dev = obj->dev;
1488 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001489 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001490 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
1491
Zou Nan hai852835f2010-05-21 09:08:56 +08001492 BUG_ON(ring == NULL);
1493 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001494
1495 /* Add a reference if we're newly entering the active list. */
1496 if (!obj_priv->active) {
1497 drm_gem_object_reference(obj);
1498 obj_priv->active = 1;
1499 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001500
Eric Anholt673a3942008-07-30 12:06:12 -07001501 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001502 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001503 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001504 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001505 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001506}
1507
Eric Anholtce44b0e2008-11-06 16:00:31 -08001508static void
1509i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1510{
1511 struct drm_device *dev = obj->dev;
1512 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001513 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001514
1515 BUG_ON(!obj_priv->active);
1516 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1517 obj_priv->last_rendering_seqno = 0;
1518}
Eric Anholt673a3942008-07-30 12:06:12 -07001519
Chris Wilson963b4832009-09-20 23:03:54 +01001520/* Immediately discard the backing storage */
1521static void
1522i915_gem_object_truncate(struct drm_gem_object *obj)
1523{
Daniel Vetter23010e42010-03-08 13:35:02 +01001524 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001525 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001526
Chris Wilsonae9fed62010-08-07 11:01:30 +01001527 /* Our goal here is to return as much of the memory as
1528 * is possible back to the system as we are called from OOM.
1529 * To do this we must instruct the shmfs to drop all of its
1530 * backing pages, *now*. Here we mirror the actions taken
1531 * when by shmem_delete_inode() to release the backing store.
1532 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001533 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001534 truncate_inode_pages(inode->i_mapping, 0);
1535 if (inode->i_op->truncate_range)
1536 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001537
1538 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001539}
1540
1541static inline int
1542i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1543{
1544 return obj_priv->madv == I915_MADV_DONTNEED;
1545}
1546
Eric Anholt673a3942008-07-30 12:06:12 -07001547static void
1548i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1549{
1550 struct drm_device *dev = obj->dev;
1551 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001552 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001553
1554 i915_verify_inactive(dev, __FILE__, __LINE__);
1555 if (obj_priv->pin_count != 0)
1556 list_del_init(&obj_priv->list);
1557 else
1558 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1559
Daniel Vetter99fcb762010-02-07 16:20:18 +01001560 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1561
Eric Anholtce44b0e2008-11-06 16:00:31 -08001562 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001563 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001564 if (obj_priv->active) {
1565 obj_priv->active = 0;
1566 drm_gem_object_unreference(obj);
1567 }
1568 i915_verify_inactive(dev, __FILE__, __LINE__);
1569}
1570
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001571void
Daniel Vetter63560392010-02-19 11:51:59 +01001572i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001573 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001574 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001575{
1576 drm_i915_private_t *dev_priv = dev->dev_private;
1577 struct drm_i915_gem_object *obj_priv, *next;
1578
1579 list_for_each_entry_safe(obj_priv, next,
1580 &dev_priv->mm.gpu_write_list,
1581 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001582 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001583
1584 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001585 obj->write_domain &&
1586 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001587 uint32_t old_write_domain = obj->write_domain;
1588
1589 obj->write_domain = 0;
1590 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001591 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001592
1593 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001594 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1595 struct drm_i915_fence_reg *reg =
1596 &dev_priv->fence_regs[obj_priv->fence_reg];
1597 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001598 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001599 }
Daniel Vetter63560392010-02-19 11:51:59 +01001600
1601 trace_i915_gem_object_change_domain(obj,
1602 obj->read_domains,
1603 old_write_domain);
1604 }
1605 }
1606}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001607
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001608uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001609i915_add_request(struct drm_device *dev,
1610 struct drm_file *file_priv,
1611 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001612{
1613 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001614 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001615 struct drm_i915_gem_request *request;
1616 uint32_t seqno;
1617 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001618
Eric Anholtb9624422009-06-03 07:27:35 +00001619 if (file_priv != NULL)
1620 i915_file_priv = file_priv->driver_priv;
1621
Eric Anholt9a298b22009-03-24 12:23:04 -07001622 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001623 if (request == NULL)
1624 return 0;
1625
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001626 seqno = ring->add_request(dev, ring, file_priv, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001627
1628 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001629 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001630 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001631 was_empty = list_empty(&ring->request_list);
1632 list_add_tail(&request->list, &ring->request_list);
1633
Eric Anholtb9624422009-06-03 07:27:35 +00001634 if (i915_file_priv) {
1635 list_add_tail(&request->client_list,
1636 &i915_file_priv->mm.request_list);
1637 } else {
1638 INIT_LIST_HEAD(&request->client_list);
1639 }
Eric Anholt673a3942008-07-30 12:06:12 -07001640
Ben Gamarif65d9422009-09-14 17:48:44 -04001641 if (!dev_priv->mm.suspended) {
1642 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1643 if (was_empty)
1644 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1645 }
Eric Anholt673a3942008-07-30 12:06:12 -07001646 return seqno;
1647}
1648
1649/**
1650 * Command execution barrier
1651 *
1652 * Ensures that all commands in the ring are finished
1653 * before signalling the CPU
1654 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001655static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001656i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001657{
Eric Anholt673a3942008-07-30 12:06:12 -07001658 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001659
1660 /* The sampler always gets flushed on i965 (sigh) */
1661 if (IS_I965G(dev))
1662 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001663
1664 ring->flush(dev, ring,
1665 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001666}
1667
1668/**
1669 * Moves buffers associated only with the given active seqno from the active
1670 * to inactive list, potentially freeing them.
1671 */
1672static void
1673i915_gem_retire_request(struct drm_device *dev,
1674 struct drm_i915_gem_request *request)
1675{
1676 drm_i915_private_t *dev_priv = dev->dev_private;
1677
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001678 trace_i915_gem_request_retire(dev, request->seqno);
1679
Eric Anholt673a3942008-07-30 12:06:12 -07001680 /* Move any buffers on the active list that are no longer referenced
1681 * by the ringbuffer to the flushing/inactive lists as appropriate.
1682 */
Carl Worth5e118f42009-03-20 11:54:25 -07001683 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001684 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001685 struct drm_gem_object *obj;
1686 struct drm_i915_gem_object *obj_priv;
1687
Zou Nan hai852835f2010-05-21 09:08:56 +08001688 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001689 struct drm_i915_gem_object,
1690 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001691 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001692
1693 /* If the seqno being retired doesn't match the oldest in the
1694 * list, then the oldest in the list must still be newer than
1695 * this seqno.
1696 */
1697 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001698 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001699
Eric Anholt673a3942008-07-30 12:06:12 -07001700#if WATCH_LRU
1701 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1702 __func__, request->seqno, obj);
1703#endif
1704
Eric Anholtce44b0e2008-11-06 16:00:31 -08001705 if (obj->write_domain != 0)
1706 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001707 else {
1708 /* Take a reference on the object so it won't be
1709 * freed while the spinlock is held. The list
1710 * protection for this spinlock is safe when breaking
1711 * the lock like this since the next thing we do
1712 * is just get the head of the list again.
1713 */
1714 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001715 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001716 spin_unlock(&dev_priv->mm.active_list_lock);
1717 drm_gem_object_unreference(obj);
1718 spin_lock(&dev_priv->mm.active_list_lock);
1719 }
Eric Anholt673a3942008-07-30 12:06:12 -07001720 }
Carl Worth5e118f42009-03-20 11:54:25 -07001721out:
1722 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001723}
1724
1725/**
1726 * Returns true if seq1 is later than seq2.
1727 */
Ben Gamari22be1722009-09-14 17:48:43 -04001728bool
Eric Anholt673a3942008-07-30 12:06:12 -07001729i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1730{
1731 return (int32_t)(seq1 - seq2) >= 0;
1732}
1733
1734uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001735i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001736 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001737{
Zou Nan hai852835f2010-05-21 09:08:56 +08001738 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001739}
1740
1741/**
1742 * This function clears the request list as sequence numbers are passed.
1743 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001744static void
1745i915_gem_retire_requests_ring(struct drm_device *dev,
1746 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001747{
1748 drm_i915_private_t *dev_priv = dev->dev_private;
1749 uint32_t seqno;
1750
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001751 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001752 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001753 return;
1754
Zou Nan hai852835f2010-05-21 09:08:56 +08001755 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001756
Zou Nan hai852835f2010-05-21 09:08:56 +08001757 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001758 struct drm_i915_gem_request *request;
1759 uint32_t retiring_seqno;
1760
Zou Nan hai852835f2010-05-21 09:08:56 +08001761 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001762 struct drm_i915_gem_request,
1763 list);
1764 retiring_seqno = request->seqno;
1765
1766 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001767 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001768 i915_gem_retire_request(dev, request);
1769
1770 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001771 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001772 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001773 } else
1774 break;
1775 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001776
1777 if (unlikely (dev_priv->trace_irq_seqno &&
1778 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001779
1780 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001781 dev_priv->trace_irq_seqno = 0;
1782 }
Eric Anholt673a3942008-07-30 12:06:12 -07001783}
1784
1785void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001786i915_gem_retire_requests(struct drm_device *dev)
1787{
1788 drm_i915_private_t *dev_priv = dev->dev_private;
1789
Chris Wilsonbe726152010-07-23 23:18:50 +01001790 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1791 struct drm_i915_gem_object *obj_priv, *tmp;
1792
1793 /* We must be careful that during unbind() we do not
1794 * accidentally infinitely recurse into retire requests.
1795 * Currently:
1796 * retire -> free -> unbind -> wait -> retire_ring
1797 */
1798 list_for_each_entry_safe(obj_priv, tmp,
1799 &dev_priv->mm.deferred_free_list,
1800 list)
1801 i915_gem_free_object_tail(&obj_priv->base);
1802 }
1803
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001804 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1805 if (HAS_BSD(dev))
1806 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1807}
1808
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001809static void
Eric Anholt673a3942008-07-30 12:06:12 -07001810i915_gem_retire_work_handler(struct work_struct *work)
1811{
1812 drm_i915_private_t *dev_priv;
1813 struct drm_device *dev;
1814
1815 dev_priv = container_of(work, drm_i915_private_t,
1816 mm.retire_work.work);
1817 dev = dev_priv->dev;
1818
1819 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001820 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001821
Keith Packard6dbe2772008-10-14 21:41:13 -07001822 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001823 (!list_empty(&dev_priv->render_ring.request_list) ||
1824 (HAS_BSD(dev) &&
1825 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001826 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001827 mutex_unlock(&dev->struct_mutex);
1828}
1829
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001830int
Zou Nan hai852835f2010-05-21 09:08:56 +08001831i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001832 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001833{
1834 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001835 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001836 int ret = 0;
1837
1838 BUG_ON(seqno == 0);
1839
Daniel Vettere35a41d2010-02-11 22:13:59 +01001840 if (seqno == dev_priv->next_seqno) {
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001841 seqno = i915_add_request(dev, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001842 if (seqno == 0)
1843 return -ENOMEM;
1844 }
1845
Ben Gamariba1234d2009-09-14 17:48:47 -04001846 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001847 return -EIO;
1848
Zou Nan hai852835f2010-05-21 09:08:56 +08001849 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001850 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001851 ier = I915_READ(DEIER) | I915_READ(GTIER);
1852 else
1853 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001854 if (!ier) {
1855 DRM_ERROR("something (likely vbetool) disabled "
1856 "interrupts, re-enabling\n");
1857 i915_driver_irq_preinstall(dev);
1858 i915_driver_irq_postinstall(dev);
1859 }
1860
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001861 trace_i915_gem_request_wait_begin(dev, seqno);
1862
Zou Nan hai852835f2010-05-21 09:08:56 +08001863 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001864 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001865 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001866 ret = wait_event_interruptible(ring->irq_queue,
1867 i915_seqno_passed(
1868 ring->get_gem_seqno(dev, ring), seqno)
1869 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001870 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001871 wait_event(ring->irq_queue,
1872 i915_seqno_passed(
1873 ring->get_gem_seqno(dev, ring), seqno)
1874 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001875
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001876 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001877 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001878
1879 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001880 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001881 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001882 ret = -EIO;
1883
1884 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01001885 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
1886 __func__, ret, seqno, ring->get_gem_seqno(dev, ring),
1887 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001888
1889 /* Directly dispatch request retiring. While we have the work queue
1890 * to handle this, the waiter on a request often wants an associated
1891 * buffer to have made it to the inactive list, and we would need
1892 * a separate wait queue to handle that.
1893 */
1894 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001895 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001896
1897 return ret;
1898}
1899
Daniel Vetter48764bf2009-09-15 22:57:32 +02001900/**
1901 * Waits for a sequence number to be signaled, and cleans up the
1902 * request and object lists appropriately for that event.
1903 */
1904static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001905i915_wait_request(struct drm_device *dev, uint32_t seqno,
1906 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001907{
Zou Nan hai852835f2010-05-21 09:08:56 +08001908 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001909}
1910
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001911static void
1912i915_gem_flush(struct drm_device *dev,
1913 uint32_t invalidate_domains,
1914 uint32_t flush_domains)
1915{
1916 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01001917
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001918 if (flush_domains & I915_GEM_DOMAIN_CPU)
1919 drm_agp_chipset_flush(dev);
Daniel Vetter8bff9172010-02-11 22:19:40 +01001920
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001921 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1922 invalidate_domains,
1923 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001924
1925 if (HAS_BSD(dev))
1926 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1927 invalidate_domains,
1928 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001929}
1930
Eric Anholt673a3942008-07-30 12:06:12 -07001931/**
1932 * Ensures that all rendering to the object has completed and the object is
1933 * safe to unbind from the GTT or access from the CPU.
1934 */
1935static int
Daniel Vettere35a41d2010-02-11 22:13:59 +01001936i915_gem_object_wait_rendering(struct drm_gem_object *obj,
1937 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07001938{
1939 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001940 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001941 int ret;
1942
Eric Anholte47c68e2008-11-14 13:35:19 -08001943 /* This function only exists to support waiting for existing rendering,
1944 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001945 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001946 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001947
1948 /* If there is rendering queued on the buffer being evicted, wait for
1949 * it.
1950 */
1951 if (obj_priv->active) {
1952#if WATCH_BUF
1953 DRM_INFO("%s: object %p wait for seqno %08x\n",
1954 __func__, obj, obj_priv->last_rendering_seqno);
1955#endif
Daniel Vettere35a41d2010-02-11 22:13:59 +01001956 ret = i915_do_wait_request(dev,
1957 obj_priv->last_rendering_seqno,
1958 interruptible,
1959 obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001960 if (ret != 0)
1961 return ret;
1962 }
1963
1964 return 0;
1965}
1966
1967/**
1968 * Unbinds an object from the GTT aperture.
1969 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001970int
Eric Anholt673a3942008-07-30 12:06:12 -07001971i915_gem_object_unbind(struct drm_gem_object *obj)
1972{
1973 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001974 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001975 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001976 int ret = 0;
1977
1978#if WATCH_BUF
1979 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1980 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1981#endif
1982 if (obj_priv->gtt_space == NULL)
1983 return 0;
1984
1985 if (obj_priv->pin_count != 0) {
1986 DRM_ERROR("Attempting to unbind pinned buffer\n");
1987 return -EINVAL;
1988 }
1989
Eric Anholt5323fd02009-09-09 11:50:45 -07001990 /* blow away mappings if mapped through GTT */
1991 i915_gem_release_mmap(obj);
1992
Eric Anholt673a3942008-07-30 12:06:12 -07001993 /* Move the object to the CPU domain to ensure that
1994 * any possible CPU writes while it's not in the GTT
1995 * are flushed when we go to remap it. This will
1996 * also ensure that all pending GPU writes are finished
1997 * before we unbind.
1998 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001999 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002000 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002001 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002002 /* Continue on if we fail due to EIO, the GPU is hung so we
2003 * should be safe and we need to cleanup or else we might
2004 * cause memory corruption through use-after-free.
2005 */
Eric Anholt673a3942008-07-30 12:06:12 -07002006
Daniel Vetter96b47b62009-12-15 17:50:00 +01002007 /* release the fence reg _after_ flushing */
2008 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2009 i915_gem_clear_fence_reg(obj);
2010
Eric Anholt673a3942008-07-30 12:06:12 -07002011 if (obj_priv->agp_mem != NULL) {
2012 drm_unbind_agp(obj_priv->agp_mem);
2013 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2014 obj_priv->agp_mem = NULL;
2015 }
2016
Eric Anholt856fa192009-03-19 14:10:50 -07002017 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002018 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002019
2020 if (obj_priv->gtt_space) {
2021 atomic_dec(&dev->gtt_count);
2022 atomic_sub(obj->size, &dev->gtt_memory);
2023
2024 drm_mm_put_block(obj_priv->gtt_space);
2025 obj_priv->gtt_space = NULL;
2026 }
2027
2028 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002029 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002030 if (!list_empty(&obj_priv->list))
2031 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002032 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002033
Chris Wilson963b4832009-09-20 23:03:54 +01002034 if (i915_gem_object_is_purgeable(obj_priv))
2035 i915_gem_object_truncate(obj);
2036
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002037 trace_i915_gem_object_unbind(obj);
2038
Chris Wilson8dc17752010-07-23 23:18:51 +01002039 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002040}
2041
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002042int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002043i915_gpu_idle(struct drm_device *dev)
2044{
2045 drm_i915_private_t *dev_priv = dev->dev_private;
2046 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002047 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002048 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002049
2050 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002051 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2052 list_empty(&dev_priv->render_ring.active_list) &&
2053 (!HAS_BSD(dev) ||
2054 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002055 spin_unlock(&dev_priv->mm.active_list_lock);
2056
2057 if (lists_empty)
2058 return 0;
2059
2060 /* Flush everything onto the inactive list. */
2061 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002062 seqno1 = i915_add_request(dev, NULL, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002063 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002064 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002065 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002066 if (ret)
2067 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002068
2069 if (HAS_BSD(dev)) {
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002070 seqno2 = i915_add_request(dev, NULL, &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002071 if (seqno2 == 0)
2072 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002073 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2074 if (ret)
2075 return ret;
2076 }
2077
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002078 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002079}
2080
Ben Gamari6911a9b2009-04-02 11:24:54 -07002081int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002082i915_gem_object_get_pages(struct drm_gem_object *obj,
2083 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002084{
Daniel Vetter23010e42010-03-08 13:35:02 +01002085 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002086 int page_count, i;
2087 struct address_space *mapping;
2088 struct inode *inode;
2089 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002090
Daniel Vetter778c3542010-05-13 11:49:44 +02002091 BUG_ON(obj_priv->pages_refcount
2092 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2093
Eric Anholt856fa192009-03-19 14:10:50 -07002094 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002095 return 0;
2096
2097 /* Get the list of pages out of our struct file. They'll be pinned
2098 * at this point until we release them.
2099 */
2100 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002101 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002102 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002103 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002104 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002105 return -ENOMEM;
2106 }
2107
2108 inode = obj->filp->f_path.dentry->d_inode;
2109 mapping = inode->i_mapping;
2110 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002111 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002112 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002113 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002114 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002115 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002116 if (IS_ERR(page))
2117 goto err_pages;
2118
Eric Anholt856fa192009-03-19 14:10:50 -07002119 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002120 }
Eric Anholt280b7132009-03-12 16:56:27 -07002121
2122 if (obj_priv->tiling_mode != I915_TILING_NONE)
2123 i915_gem_object_do_bit_17_swizzle(obj);
2124
Eric Anholt673a3942008-07-30 12:06:12 -07002125 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002126
2127err_pages:
2128 while (i--)
2129 page_cache_release(obj_priv->pages[i]);
2130
2131 drm_free_large(obj_priv->pages);
2132 obj_priv->pages = NULL;
2133 obj_priv->pages_refcount--;
2134 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002135}
2136
Eric Anholt4e901fd2009-10-26 16:44:17 -07002137static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2138{
2139 struct drm_gem_object *obj = reg->obj;
2140 struct drm_device *dev = obj->dev;
2141 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002142 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002143 int regnum = obj_priv->fence_reg;
2144 uint64_t val;
2145
2146 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2147 0xfffff000) << 32;
2148 val |= obj_priv->gtt_offset & 0xfffff000;
2149 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2150 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2151
2152 if (obj_priv->tiling_mode == I915_TILING_Y)
2153 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2154 val |= I965_FENCE_REG_VALID;
2155
2156 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2157}
2158
Jesse Barnesde151cf2008-11-12 10:03:55 -08002159static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2160{
2161 struct drm_gem_object *obj = reg->obj;
2162 struct drm_device *dev = obj->dev;
2163 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002164 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002165 int regnum = obj_priv->fence_reg;
2166 uint64_t val;
2167
2168 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2169 0xfffff000) << 32;
2170 val |= obj_priv->gtt_offset & 0xfffff000;
2171 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2172 if (obj_priv->tiling_mode == I915_TILING_Y)
2173 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2174 val |= I965_FENCE_REG_VALID;
2175
2176 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2177}
2178
2179static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2180{
2181 struct drm_gem_object *obj = reg->obj;
2182 struct drm_device *dev = obj->dev;
2183 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002184 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002185 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002186 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002187 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002188 uint32_t pitch_val;
2189
2190 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2191 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002192 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002193 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002194 return;
2195 }
2196
Jesse Barnes0f973f22009-01-26 17:10:45 -08002197 if (obj_priv->tiling_mode == I915_TILING_Y &&
2198 HAS_128_BYTE_Y_TILING(dev))
2199 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002200 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002201 tile_width = 512;
2202
2203 /* Note: pitch better be a power of two tile widths */
2204 pitch_val = obj_priv->stride / tile_width;
2205 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002206
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002207 if (obj_priv->tiling_mode == I915_TILING_Y &&
2208 HAS_128_BYTE_Y_TILING(dev))
2209 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2210 else
2211 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2212
Jesse Barnesde151cf2008-11-12 10:03:55 -08002213 val = obj_priv->gtt_offset;
2214 if (obj_priv->tiling_mode == I915_TILING_Y)
2215 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2216 val |= I915_FENCE_SIZE_BITS(obj->size);
2217 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2218 val |= I830_FENCE_REG_VALID;
2219
Eric Anholtdc529a42009-03-10 22:34:49 -07002220 if (regnum < 8)
2221 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2222 else
2223 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2224 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002225}
2226
2227static void i830_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;
2234 uint32_t val;
2235 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002236 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002237
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002238 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002239 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002240 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002241 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002242 return;
2243 }
2244
Eric Anholte76a16d2009-05-26 17:44:56 -07002245 pitch_val = obj_priv->stride / 128;
2246 pitch_val = ffs(pitch_val) - 1;
2247 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2248
Jesse Barnesde151cf2008-11-12 10:03:55 -08002249 val = obj_priv->gtt_offset;
2250 if (obj_priv->tiling_mode == I915_TILING_Y)
2251 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002252 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2253 WARN_ON(fence_size_bits & ~0x00000f00);
2254 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002255 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2256 val |= I830_FENCE_REG_VALID;
2257
2258 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002259}
2260
Daniel Vetterae3db242010-02-19 11:51:58 +01002261static int i915_find_fence_reg(struct drm_device *dev)
2262{
2263 struct drm_i915_fence_reg *reg = NULL;
2264 struct drm_i915_gem_object *obj_priv = NULL;
2265 struct drm_i915_private *dev_priv = dev->dev_private;
2266 struct drm_gem_object *obj = NULL;
2267 int i, avail, ret;
2268
2269 /* First try to find a free reg */
2270 avail = 0;
2271 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2272 reg = &dev_priv->fence_regs[i];
2273 if (!reg->obj)
2274 return i;
2275
Daniel Vetter23010e42010-03-08 13:35:02 +01002276 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002277 if (!obj_priv->pin_count)
2278 avail++;
2279 }
2280
2281 if (avail == 0)
2282 return -ENOSPC;
2283
2284 /* None available, try to steal one or wait for a user to finish */
2285 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002286 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2287 lru_list) {
2288 obj = reg->obj;
2289 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002290
2291 if (obj_priv->pin_count)
2292 continue;
2293
2294 /* found one! */
2295 i = obj_priv->fence_reg;
2296 break;
2297 }
2298
2299 BUG_ON(i == I915_FENCE_REG_NONE);
2300
2301 /* We only have a reference on obj from the active list. put_fence_reg
2302 * might drop that one, causing a use-after-free in it. So hold a
2303 * private reference to obj like the other callers of put_fence_reg
2304 * (set_tiling ioctl) do. */
2305 drm_gem_object_reference(obj);
2306 ret = i915_gem_object_put_fence_reg(obj);
2307 drm_gem_object_unreference(obj);
2308 if (ret != 0)
2309 return ret;
2310
2311 return i;
2312}
2313
Jesse Barnesde151cf2008-11-12 10:03:55 -08002314/**
2315 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2316 * @obj: object to map through a fence reg
2317 *
2318 * When mapping objects through the GTT, userspace wants to be able to write
2319 * to them without having to worry about swizzling if the object is tiled.
2320 *
2321 * This function walks the fence regs looking for a free one for @obj,
2322 * stealing one if it can't find any.
2323 *
2324 * It then sets up the reg based on the object's properties: address, pitch
2325 * and tiling format.
2326 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002327int
2328i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002329{
2330 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002331 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002332 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002333 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002334 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002335
Eric Anholta09ba7f2009-08-29 12:49:51 -07002336 /* Just update our place in the LRU if our fence is getting used. */
2337 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002338 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2339 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002340 return 0;
2341 }
2342
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343 switch (obj_priv->tiling_mode) {
2344 case I915_TILING_NONE:
2345 WARN(1, "allocating a fence for non-tiled object?\n");
2346 break;
2347 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002348 if (!obj_priv->stride)
2349 return -EINVAL;
2350 WARN((obj_priv->stride & (512 - 1)),
2351 "object 0x%08x is X tiled but has non-512B pitch\n",
2352 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002353 break;
2354 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002355 if (!obj_priv->stride)
2356 return -EINVAL;
2357 WARN((obj_priv->stride & (128 - 1)),
2358 "object 0x%08x is Y tiled but has non-128B pitch\n",
2359 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002360 break;
2361 }
2362
Daniel Vetterae3db242010-02-19 11:51:58 +01002363 ret = i915_find_fence_reg(dev);
2364 if (ret < 0)
2365 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002366
Daniel Vetterae3db242010-02-19 11:51:58 +01002367 obj_priv->fence_reg = ret;
2368 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002369 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002370
Jesse Barnesde151cf2008-11-12 10:03:55 -08002371 reg->obj = obj;
2372
Eric Anholt4e901fd2009-10-26 16:44:17 -07002373 if (IS_GEN6(dev))
2374 sandybridge_write_fence_reg(reg);
2375 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002376 i965_write_fence_reg(reg);
2377 else if (IS_I9XX(dev))
2378 i915_write_fence_reg(reg);
2379 else
2380 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002381
Daniel Vetterae3db242010-02-19 11:51:58 +01002382 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2383 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002384
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002385 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002386}
2387
2388/**
2389 * i915_gem_clear_fence_reg - clear out fence register info
2390 * @obj: object to clear
2391 *
2392 * Zeroes out the fence register itself and clears out the associated
2393 * data structures in dev_priv and obj_priv.
2394 */
2395static void
2396i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2397{
2398 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002399 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002400 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002401 struct drm_i915_fence_reg *reg =
2402 &dev_priv->fence_regs[obj_priv->fence_reg];
Jesse Barnesde151cf2008-11-12 10:03:55 -08002403
Eric Anholt4e901fd2009-10-26 16:44:17 -07002404 if (IS_GEN6(dev)) {
2405 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2406 (obj_priv->fence_reg * 8), 0);
2407 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002408 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002409 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002410 uint32_t fence_reg;
2411
2412 if (obj_priv->fence_reg < 8)
2413 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2414 else
2415 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2416 8) * 4;
2417
2418 I915_WRITE(fence_reg, 0);
2419 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002420
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002421 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002422 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002423 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002424}
2425
Eric Anholt673a3942008-07-30 12:06:12 -07002426/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002427 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2428 * to the buffer to finish, and then resets the fence register.
2429 * @obj: tiled object holding a fence register.
2430 *
2431 * Zeroes out the fence register itself and clears out the associated
2432 * data structures in dev_priv and obj_priv.
2433 */
2434int
2435i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2436{
2437 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002438 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002439
2440 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2441 return 0;
2442
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002443 /* If we've changed tiling, GTT-mappings of the object
2444 * need to re-fault to ensure that the correct fence register
2445 * setup is in place.
2446 */
2447 i915_gem_release_mmap(obj);
2448
Chris Wilson52dc7d32009-06-06 09:46:01 +01002449 /* On the i915, GPU access to tiled buffers is via a fence,
2450 * therefore we must wait for any outstanding access to complete
2451 * before clearing the fence.
2452 */
2453 if (!IS_I965G(dev)) {
2454 int ret;
2455
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002456 ret = i915_gem_object_flush_gpu_write_domain(obj);
2457 if (ret != 0)
2458 return ret;
2459
Daniel Vettere35a41d2010-02-11 22:13:59 +01002460 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002461 if (ret != 0)
2462 return ret;
2463 }
2464
Daniel Vetter4a726612010-02-01 13:59:16 +01002465 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002466 i915_gem_clear_fence_reg (obj);
2467
2468 return 0;
2469}
2470
2471/**
Eric Anholt673a3942008-07-30 12:06:12 -07002472 * Finds free space in the GTT aperture and binds the object there.
2473 */
2474static int
2475i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2476{
2477 struct drm_device *dev = obj->dev;
2478 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002479 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002480 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002481 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002482 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002483
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002484 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002485 DRM_ERROR("Attempting to bind a purgeable object\n");
2486 return -EINVAL;
2487 }
2488
Eric Anholt673a3942008-07-30 12:06:12 -07002489 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002490 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002491 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002492 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2493 return -EINVAL;
2494 }
2495
Chris Wilson654fc602010-05-27 13:18:21 +01002496 /* If the object is bigger than the entire aperture, reject it early
2497 * before evicting everything in a vain attempt to find space.
2498 */
2499 if (obj->size > dev->gtt_total) {
2500 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2501 return -E2BIG;
2502 }
2503
Eric Anholt673a3942008-07-30 12:06:12 -07002504 search_free:
2505 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2506 obj->size, alignment, 0);
2507 if (free_space != NULL) {
2508 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2509 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002510 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002511 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002512 }
2513 if (obj_priv->gtt_space == NULL) {
2514 /* If the gtt is empty and we're still having trouble
2515 * fitting our object in, we're out of memory.
2516 */
2517#if WATCH_LRU
2518 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2519#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002520 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002521 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002522 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002523
Eric Anholt673a3942008-07-30 12:06:12 -07002524 goto search_free;
2525 }
2526
2527#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002528 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002529 obj->size, obj_priv->gtt_offset);
2530#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002531 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002532 if (ret) {
2533 drm_mm_put_block(obj_priv->gtt_space);
2534 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002535
2536 if (ret == -ENOMEM) {
2537 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002538 ret = i915_gem_evict_something(dev, obj->size,
2539 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002540 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002541 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002542 if (gfpmask) {
2543 gfpmask = 0;
2544 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002545 }
2546
2547 return ret;
2548 }
2549
2550 goto search_free;
2551 }
2552
Eric Anholt673a3942008-07-30 12:06:12 -07002553 return ret;
2554 }
2555
Eric Anholt673a3942008-07-30 12:06:12 -07002556 /* Create an AGP memory structure pointing at our pages, and bind it
2557 * into the GTT.
2558 */
2559 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002560 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002561 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002562 obj_priv->gtt_offset,
2563 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002564 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002565 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002566 drm_mm_put_block(obj_priv->gtt_space);
2567 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002568
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002569 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002570 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002571 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002572
2573 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002574 }
2575 atomic_inc(&dev->gtt_count);
2576 atomic_add(obj->size, &dev->gtt_memory);
2577
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002578 /* keep track of bounds object by adding it to the inactive list */
2579 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2580
Eric Anholt673a3942008-07-30 12:06:12 -07002581 /* Assert that the object is not currently in any GPU domain. As it
2582 * wasn't in the GTT, there shouldn't be any way it could have been in
2583 * a GPU cache
2584 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002585 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2586 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002587
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002588 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2589
Eric Anholt673a3942008-07-30 12:06:12 -07002590 return 0;
2591}
2592
2593void
2594i915_gem_clflush_object(struct drm_gem_object *obj)
2595{
Daniel Vetter23010e42010-03-08 13:35:02 +01002596 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002597
2598 /* If we don't have a page list set up, then we're not pinned
2599 * to GPU, and we can ignore the cache flush because it'll happen
2600 * again at bind time.
2601 */
Eric Anholt856fa192009-03-19 14:10:50 -07002602 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002603 return;
2604
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002605 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002606
Eric Anholt856fa192009-03-19 14:10:50 -07002607 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002608}
2609
Eric Anholte47c68e2008-11-14 13:35:19 -08002610/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002611static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002612i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2613{
2614 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002615 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002616 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002617
2618 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002619 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002620
2621 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002622 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002623 i915_gem_flush(dev, 0, obj->write_domain);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002624 if (i915_add_request(dev, NULL, obj_priv->ring) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002625 return -ENOMEM;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002626
2627 trace_i915_gem_object_change_domain(obj,
2628 obj->read_domains,
2629 old_write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002630 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002631}
2632
2633/** Flushes the GTT write domain for the object if it's dirty. */
2634static void
2635i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2636{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002637 uint32_t old_write_domain;
2638
Eric Anholte47c68e2008-11-14 13:35:19 -08002639 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2640 return;
2641
2642 /* No actual flushing is required for the GTT write domain. Writes
2643 * to it immediately go to main memory as far as we know, so there's
2644 * no chipset flush. It also doesn't land in render cache.
2645 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002646 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002647 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002648
2649 trace_i915_gem_object_change_domain(obj,
2650 obj->read_domains,
2651 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002652}
2653
2654/** Flushes the CPU write domain for the object if it's dirty. */
2655static void
2656i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2657{
2658 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002659 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002660
2661 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2662 return;
2663
2664 i915_gem_clflush_object(obj);
2665 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002666 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002667 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002668
2669 trace_i915_gem_object_change_domain(obj,
2670 obj->read_domains,
2671 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002672}
2673
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002674int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002675i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2676{
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002677 int ret = 0;
2678
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002679 switch (obj->write_domain) {
2680 case I915_GEM_DOMAIN_GTT:
2681 i915_gem_object_flush_gtt_write_domain(obj);
2682 break;
2683 case I915_GEM_DOMAIN_CPU:
2684 i915_gem_object_flush_cpu_write_domain(obj);
2685 break;
2686 default:
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002687 ret = i915_gem_object_flush_gpu_write_domain(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002688 break;
2689 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002690
2691 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002692}
2693
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002694/**
2695 * Moves a single object to the GTT read, and possibly write domain.
2696 *
2697 * This function returns when the move is complete, including waiting on
2698 * flushes to occur.
2699 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002700int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002701i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2702{
Daniel Vetter23010e42010-03-08 13:35:02 +01002703 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002704 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002705 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002706
Eric Anholt02354392008-11-26 13:58:13 -08002707 /* Not valid to be called on unbound objects. */
2708 if (obj_priv->gtt_space == NULL)
2709 return -EINVAL;
2710
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002711 ret = i915_gem_object_flush_gpu_write_domain(obj);
2712 if (ret != 0)
2713 return ret;
2714
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002715 /* Wait on any GPU rendering and flushing to occur. */
Daniel Vettere35a41d2010-02-11 22:13:59 +01002716 ret = i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002717 if (ret != 0)
2718 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002719
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002720 old_write_domain = obj->write_domain;
2721 old_read_domains = obj->read_domains;
2722
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002723 /* If we're writing through the GTT domain, then CPU and GPU caches
2724 * will need to be invalidated at next use.
2725 */
2726 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002727 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002728
Eric Anholte47c68e2008-11-14 13:35:19 -08002729 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002730
2731 /* It should now be out of any other write domains, and we can update
2732 * the domain values for our changes.
2733 */
2734 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2735 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002736 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002737 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002738 obj_priv->dirty = 1;
2739 }
2740
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002741 trace_i915_gem_object_change_domain(obj,
2742 old_read_domains,
2743 old_write_domain);
2744
Eric Anholte47c68e2008-11-14 13:35:19 -08002745 return 0;
2746}
2747
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002748/*
2749 * Prepare buffer for display plane. Use uninterruptible for possible flush
2750 * wait, as in modesetting process we're not supposed to be interrupted.
2751 */
2752int
2753i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2754{
Daniel Vetter23010e42010-03-08 13:35:02 +01002755 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002756 uint32_t old_write_domain, old_read_domains;
2757 int ret;
2758
2759 /* Not valid to be called on unbound objects. */
2760 if (obj_priv->gtt_space == NULL)
2761 return -EINVAL;
2762
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002763 ret = i915_gem_object_flush_gpu_write_domain(obj);
2764 if (ret)
2765 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002766
2767 /* Wait on any GPU rendering and flushing to occur. */
Daniel Vettere35a41d2010-02-11 22:13:59 +01002768 ret = i915_gem_object_wait_rendering(obj, false);
2769 if (ret != 0)
2770 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002771
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002772 i915_gem_object_flush_cpu_write_domain(obj);
2773
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002774 old_write_domain = obj->write_domain;
2775 old_read_domains = obj->read_domains;
2776
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002777 /* It should now be out of any other write domains, and we can update
2778 * the domain values for our changes.
2779 */
2780 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002781 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002782 obj->write_domain = I915_GEM_DOMAIN_GTT;
2783 obj_priv->dirty = 1;
2784
2785 trace_i915_gem_object_change_domain(obj,
2786 old_read_domains,
2787 old_write_domain);
2788
2789 return 0;
2790}
2791
Eric Anholte47c68e2008-11-14 13:35:19 -08002792/**
2793 * Moves a single object to the CPU read, and possibly write domain.
2794 *
2795 * This function returns when the move is complete, including waiting on
2796 * flushes to occur.
2797 */
2798static int
2799i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2800{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002801 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002802 int ret;
2803
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002804 ret = i915_gem_object_flush_gpu_write_domain(obj);
2805 if (ret)
2806 return ret;
2807
Eric Anholte47c68e2008-11-14 13:35:19 -08002808 /* Wait on any GPU rendering and flushing to occur. */
Daniel Vettere35a41d2010-02-11 22:13:59 +01002809 ret = i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002810 if (ret != 0)
2811 return ret;
2812
2813 i915_gem_object_flush_gtt_write_domain(obj);
2814
2815 /* If we have a partially-valid cache of the object in the CPU,
2816 * finish invalidating it and free the per-page flags.
2817 */
2818 i915_gem_object_set_to_full_cpu_read_domain(obj);
2819
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002820 old_write_domain = obj->write_domain;
2821 old_read_domains = obj->read_domains;
2822
Eric Anholte47c68e2008-11-14 13:35:19 -08002823 /* Flush the CPU cache if it's still invalid. */
2824 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2825 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002826
2827 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2828 }
2829
2830 /* It should now be out of any other write domains, and we can update
2831 * the domain values for our changes.
2832 */
2833 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2834
2835 /* If we're writing through the CPU, then the GPU read domains will
2836 * need to be invalidated at next use.
2837 */
2838 if (write) {
2839 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2840 obj->write_domain = I915_GEM_DOMAIN_CPU;
2841 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002842
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002843 trace_i915_gem_object_change_domain(obj,
2844 old_read_domains,
2845 old_write_domain);
2846
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002847 return 0;
2848}
2849
Eric Anholt673a3942008-07-30 12:06:12 -07002850/*
2851 * Set the next domain for the specified object. This
2852 * may not actually perform the necessary flushing/invaliding though,
2853 * as that may want to be batched with other set_domain operations
2854 *
2855 * This is (we hope) the only really tricky part of gem. The goal
2856 * is fairly simple -- track which caches hold bits of the object
2857 * and make sure they remain coherent. A few concrete examples may
2858 * help to explain how it works. For shorthand, we use the notation
2859 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2860 * a pair of read and write domain masks.
2861 *
2862 * Case 1: the batch buffer
2863 *
2864 * 1. Allocated
2865 * 2. Written by CPU
2866 * 3. Mapped to GTT
2867 * 4. Read by GPU
2868 * 5. Unmapped from GTT
2869 * 6. Freed
2870 *
2871 * Let's take these a step at a time
2872 *
2873 * 1. Allocated
2874 * Pages allocated from the kernel may still have
2875 * cache contents, so we set them to (CPU, CPU) always.
2876 * 2. Written by CPU (using pwrite)
2877 * The pwrite function calls set_domain (CPU, CPU) and
2878 * this function does nothing (as nothing changes)
2879 * 3. Mapped by GTT
2880 * This function asserts that the object is not
2881 * currently in any GPU-based read or write domains
2882 * 4. Read by GPU
2883 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2884 * As write_domain is zero, this function adds in the
2885 * current read domains (CPU+COMMAND, 0).
2886 * flush_domains is set to CPU.
2887 * invalidate_domains is set to COMMAND
2888 * clflush is run to get data out of the CPU caches
2889 * then i915_dev_set_domain calls i915_gem_flush to
2890 * emit an MI_FLUSH and drm_agp_chipset_flush
2891 * 5. Unmapped from GTT
2892 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2893 * flush_domains and invalidate_domains end up both zero
2894 * so no flushing/invalidating happens
2895 * 6. Freed
2896 * yay, done
2897 *
2898 * Case 2: The shared render buffer
2899 *
2900 * 1. Allocated
2901 * 2. Mapped to GTT
2902 * 3. Read/written by GPU
2903 * 4. set_domain to (CPU,CPU)
2904 * 5. Read/written by CPU
2905 * 6. Read/written by GPU
2906 *
2907 * 1. Allocated
2908 * Same as last example, (CPU, CPU)
2909 * 2. Mapped to GTT
2910 * Nothing changes (assertions find that it is not in the GPU)
2911 * 3. Read/written by GPU
2912 * execbuffer calls set_domain (RENDER, RENDER)
2913 * flush_domains gets CPU
2914 * invalidate_domains gets GPU
2915 * clflush (obj)
2916 * MI_FLUSH and drm_agp_chipset_flush
2917 * 4. set_domain (CPU, CPU)
2918 * flush_domains gets GPU
2919 * invalidate_domains gets CPU
2920 * wait_rendering (obj) to make sure all drawing is complete.
2921 * This will include an MI_FLUSH to get the data from GPU
2922 * to memory
2923 * clflush (obj) to invalidate the CPU cache
2924 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2925 * 5. Read/written by CPU
2926 * cache lines are loaded and dirtied
2927 * 6. Read written by GPU
2928 * Same as last GPU access
2929 *
2930 * Case 3: The constant buffer
2931 *
2932 * 1. Allocated
2933 * 2. Written by CPU
2934 * 3. Read by GPU
2935 * 4. Updated (written) by CPU again
2936 * 5. Read by GPU
2937 *
2938 * 1. Allocated
2939 * (CPU, CPU)
2940 * 2. Written by CPU
2941 * (CPU, CPU)
2942 * 3. Read by GPU
2943 * (CPU+RENDER, 0)
2944 * flush_domains = CPU
2945 * invalidate_domains = RENDER
2946 * clflush (obj)
2947 * MI_FLUSH
2948 * drm_agp_chipset_flush
2949 * 4. Updated (written) by CPU again
2950 * (CPU, CPU)
2951 * flush_domains = 0 (no previous write domain)
2952 * invalidate_domains = 0 (no new read domains)
2953 * 5. Read by GPU
2954 * (CPU+RENDER, 0)
2955 * flush_domains = CPU
2956 * invalidate_domains = RENDER
2957 * clflush (obj)
2958 * MI_FLUSH
2959 * drm_agp_chipset_flush
2960 */
Keith Packardc0d90822008-11-20 23:11:08 -08002961static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002962i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002963{
2964 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002965 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002966 uint32_t invalidate_domains = 0;
2967 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002968 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002969
Eric Anholt8b0e3782009-02-19 14:40:50 -08002970 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2971 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002972
Jesse Barnes652c3932009-08-17 13:31:43 -07002973 intel_mark_busy(dev, obj);
2974
Eric Anholt673a3942008-07-30 12:06:12 -07002975#if WATCH_BUF
2976 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2977 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002978 obj->read_domains, obj->pending_read_domains,
2979 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002980#endif
2981 /*
2982 * If the object isn't moving to a new write domain,
2983 * let the object stay in multiple read domains
2984 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002985 if (obj->pending_write_domain == 0)
2986 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002987 else
2988 obj_priv->dirty = 1;
2989
2990 /*
2991 * Flush the current write domain if
2992 * the new read domains don't match. Invalidate
2993 * any read domains which differ from the old
2994 * write domain
2995 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002996 if (obj->write_domain &&
2997 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002998 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002999 invalidate_domains |=
3000 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003001 }
3002 /*
3003 * Invalidate any read caches which may have
3004 * stale data. That is, any new read domains.
3005 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003006 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003007 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3008#if WATCH_BUF
3009 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3010 __func__, flush_domains, invalidate_domains);
3011#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003012 i915_gem_clflush_object(obj);
3013 }
3014
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003015 old_read_domains = obj->read_domains;
3016
Eric Anholtefbeed92009-02-19 14:54:51 -08003017 /* The actual obj->write_domain will be updated with
3018 * pending_write_domain after we emit the accumulated flush for all
3019 * of our domain changes in execbuffers (which clears objects'
3020 * write_domains). So if we have a current write domain that we
3021 * aren't changing, set pending_write_domain to that.
3022 */
3023 if (flush_domains == 0 && obj->pending_write_domain == 0)
3024 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003025 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003026
3027 dev->invalidate_domains |= invalidate_domains;
3028 dev->flush_domains |= flush_domains;
3029#if WATCH_BUF
3030 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3031 __func__,
3032 obj->read_domains, obj->write_domain,
3033 dev->invalidate_domains, dev->flush_domains);
3034#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003035
3036 trace_i915_gem_object_change_domain(obj,
3037 old_read_domains,
3038 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003039}
3040
3041/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003042 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003043 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003044 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3045 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3046 */
3047static void
3048i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3049{
Daniel Vetter23010e42010-03-08 13:35:02 +01003050 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003051
3052 if (!obj_priv->page_cpu_valid)
3053 return;
3054
3055 /* If we're partially in the CPU read domain, finish moving it in.
3056 */
3057 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3058 int i;
3059
3060 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3061 if (obj_priv->page_cpu_valid[i])
3062 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003063 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003064 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003065 }
3066
3067 /* Free the page_cpu_valid mappings which are now stale, whether
3068 * or not we've got I915_GEM_DOMAIN_CPU.
3069 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003070 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003071 obj_priv->page_cpu_valid = NULL;
3072}
3073
3074/**
3075 * Set the CPU read domain on a range of the object.
3076 *
3077 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3078 * not entirely valid. The page_cpu_valid member of the object flags which
3079 * pages have been flushed, and will be respected by
3080 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3081 * of the whole object.
3082 *
3083 * This function returns when the move is complete, including waiting on
3084 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003085 */
3086static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003087i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3088 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003089{
Daniel Vetter23010e42010-03-08 13:35:02 +01003090 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003091 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003092 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003093
Eric Anholte47c68e2008-11-14 13:35:19 -08003094 if (offset == 0 && size == obj->size)
3095 return i915_gem_object_set_to_cpu_domain(obj, 0);
3096
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003097 ret = i915_gem_object_flush_gpu_write_domain(obj);
3098 if (ret)
3099 return ret;
3100
Eric Anholte47c68e2008-11-14 13:35:19 -08003101 /* Wait on any GPU rendering and flushing to occur. */
Daniel Vettere35a41d2010-02-11 22:13:59 +01003102 ret = i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08003103 if (ret != 0)
3104 return ret;
3105 i915_gem_object_flush_gtt_write_domain(obj);
3106
3107 /* If we're already fully in the CPU read domain, we're done. */
3108 if (obj_priv->page_cpu_valid == NULL &&
3109 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003110 return 0;
3111
Eric Anholte47c68e2008-11-14 13:35:19 -08003112 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3113 * newly adding I915_GEM_DOMAIN_CPU
3114 */
Eric Anholt673a3942008-07-30 12:06:12 -07003115 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003116 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3117 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003118 if (obj_priv->page_cpu_valid == NULL)
3119 return -ENOMEM;
3120 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3121 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003122
3123 /* Flush the cache on any pages that are still invalid from the CPU's
3124 * perspective.
3125 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003126 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3127 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003128 if (obj_priv->page_cpu_valid[i])
3129 continue;
3130
Eric Anholt856fa192009-03-19 14:10:50 -07003131 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003132
3133 obj_priv->page_cpu_valid[i] = 1;
3134 }
3135
Eric Anholte47c68e2008-11-14 13:35:19 -08003136 /* It should now be out of any other write domains, and we can update
3137 * the domain values for our changes.
3138 */
3139 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3140
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003141 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003142 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3143
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003144 trace_i915_gem_object_change_domain(obj,
3145 old_read_domains,
3146 obj->write_domain);
3147
Eric Anholt673a3942008-07-30 12:06:12 -07003148 return 0;
3149}
3150
3151/**
Eric Anholt673a3942008-07-30 12:06:12 -07003152 * Pin an object to the GTT and evaluate the relocations landing in it.
3153 */
3154static int
3155i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3156 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003157 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003158 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003159{
3160 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003161 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003162 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003163 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003164 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003165 bool need_fence;
3166
3167 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3168 obj_priv->tiling_mode != I915_TILING_NONE;
3169
3170 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003171 if (need_fence &&
3172 !i915_gem_object_fence_offset_ok(obj,
3173 obj_priv->tiling_mode)) {
3174 ret = i915_gem_object_unbind(obj);
3175 if (ret)
3176 return ret;
3177 }
Eric Anholt673a3942008-07-30 12:06:12 -07003178
3179 /* Choose the GTT offset for our buffer and put it there. */
3180 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3181 if (ret)
3182 return ret;
3183
Jesse Barnes76446ca2009-12-17 22:05:42 -05003184 /*
3185 * Pre-965 chips need a fence register set up in order to
3186 * properly handle blits to/from tiled surfaces.
3187 */
3188 if (need_fence) {
3189 ret = i915_gem_object_get_fence_reg(obj);
3190 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003191 i915_gem_object_unpin(obj);
3192 return ret;
3193 }
3194 }
3195
Eric Anholt673a3942008-07-30 12:06:12 -07003196 entry->offset = obj_priv->gtt_offset;
3197
Eric Anholt673a3942008-07-30 12:06:12 -07003198 /* Apply the relocations, using the GTT aperture to avoid cache
3199 * flushing requirements.
3200 */
3201 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003202 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003203 struct drm_gem_object *target_obj;
3204 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003205 uint32_t reloc_val, reloc_offset;
3206 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003207
Eric Anholt673a3942008-07-30 12:06:12 -07003208 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003209 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003210 if (target_obj == NULL) {
3211 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003212 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003213 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003214 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003215
Chris Wilson8542a0b2009-09-09 21:15:15 +01003216#if WATCH_RELOC
3217 DRM_INFO("%s: obj %p offset %08x target %d "
3218 "read %08x write %08x gtt %08x "
3219 "presumed %08x delta %08x\n",
3220 __func__,
3221 obj,
3222 (int) reloc->offset,
3223 (int) reloc->target_handle,
3224 (int) reloc->read_domains,
3225 (int) reloc->write_domain,
3226 (int) target_obj_priv->gtt_offset,
3227 (int) reloc->presumed_offset,
3228 reloc->delta);
3229#endif
3230
Eric Anholt673a3942008-07-30 12:06:12 -07003231 /* The target buffer should have appeared before us in the
3232 * exec_object list, so it should have a GTT space bound by now.
3233 */
3234 if (target_obj_priv->gtt_space == NULL) {
3235 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003236 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003237 drm_gem_object_unreference(target_obj);
3238 i915_gem_object_unpin(obj);
3239 return -EINVAL;
3240 }
3241
Chris Wilson8542a0b2009-09-09 21:15:15 +01003242 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003243 if (reloc->write_domain & (reloc->write_domain - 1)) {
3244 DRM_ERROR("reloc with multiple write domains: "
3245 "obj %p target %d offset %d "
3246 "read %08x write %08x",
3247 obj, reloc->target_handle,
3248 (int) reloc->offset,
3249 reloc->read_domains,
3250 reloc->write_domain);
3251 return -EINVAL;
3252 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003253 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3254 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3255 DRM_ERROR("reloc with read/write CPU domains: "
3256 "obj %p target %d offset %d "
3257 "read %08x write %08x",
3258 obj, reloc->target_handle,
3259 (int) reloc->offset,
3260 reloc->read_domains,
3261 reloc->write_domain);
3262 drm_gem_object_unreference(target_obj);
3263 i915_gem_object_unpin(obj);
3264 return -EINVAL;
3265 }
3266 if (reloc->write_domain && target_obj->pending_write_domain &&
3267 reloc->write_domain != target_obj->pending_write_domain) {
3268 DRM_ERROR("Write domain conflict: "
3269 "obj %p target %d offset %d "
3270 "new %08x old %08x\n",
3271 obj, reloc->target_handle,
3272 (int) reloc->offset,
3273 reloc->write_domain,
3274 target_obj->pending_write_domain);
3275 drm_gem_object_unreference(target_obj);
3276 i915_gem_object_unpin(obj);
3277 return -EINVAL;
3278 }
3279
3280 target_obj->pending_read_domains |= reloc->read_domains;
3281 target_obj->pending_write_domain |= reloc->write_domain;
3282
3283 /* If the relocation already has the right value in it, no
3284 * more work needs to be done.
3285 */
3286 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3287 drm_gem_object_unreference(target_obj);
3288 continue;
3289 }
3290
3291 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003292 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003293 DRM_ERROR("Relocation beyond object bounds: "
3294 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003295 obj, reloc->target_handle,
3296 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003297 drm_gem_object_unreference(target_obj);
3298 i915_gem_object_unpin(obj);
3299 return -EINVAL;
3300 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003301 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003302 DRM_ERROR("Relocation not 4-byte aligned: "
3303 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003304 obj, reloc->target_handle,
3305 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003306 drm_gem_object_unreference(target_obj);
3307 i915_gem_object_unpin(obj);
3308 return -EINVAL;
3309 }
3310
Chris Wilson8542a0b2009-09-09 21:15:15 +01003311 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003312 if (reloc->delta >= target_obj->size) {
3313 DRM_ERROR("Relocation beyond target object bounds: "
3314 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003315 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003316 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003317 drm_gem_object_unreference(target_obj);
3318 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003319 return -EINVAL;
3320 }
3321
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003322 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3323 if (ret != 0) {
3324 drm_gem_object_unreference(target_obj);
3325 i915_gem_object_unpin(obj);
3326 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003327 }
3328
3329 /* Map the page containing the relocation we're going to
3330 * perform.
3331 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003332 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003333 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3334 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003335 ~(PAGE_SIZE - 1)),
3336 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003337 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003338 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003339 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003340
3341#if WATCH_BUF
3342 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003343 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003344 readl(reloc_entry), reloc_val);
3345#endif
3346 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003347 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003348
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003349 /* The updated presumed offset for this entry will be
3350 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003351 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003352 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003353
3354 drm_gem_object_unreference(target_obj);
3355 }
3356
Eric Anholt673a3942008-07-30 12:06:12 -07003357#if WATCH_BUF
3358 if (0)
3359 i915_gem_dump_object(obj, 128, __func__, ~0);
3360#endif
3361 return 0;
3362}
3363
Eric Anholt673a3942008-07-30 12:06:12 -07003364/* Throttle our rendering by waiting until the ring has completed our requests
3365 * emitted over 20 msec ago.
3366 *
Eric Anholtb9624422009-06-03 07:27:35 +00003367 * Note that if we were to use the current jiffies each time around the loop,
3368 * we wouldn't escape the function with any frames outstanding if the time to
3369 * render a frame was over 20ms.
3370 *
Eric Anholt673a3942008-07-30 12:06:12 -07003371 * This should get us reasonable parallelism between CPU and GPU but also
3372 * relatively low latency when blocking on a particular request to finish.
3373 */
3374static int
3375i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3376{
3377 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3378 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003379 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003380
3381 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003382 while (!list_empty(&i915_file_priv->mm.request_list)) {
3383 struct drm_i915_gem_request *request;
3384
3385 request = list_first_entry(&i915_file_priv->mm.request_list,
3386 struct drm_i915_gem_request,
3387 client_list);
3388
3389 if (time_after_eq(request->emitted_jiffies, recent_enough))
3390 break;
3391
Zou Nan hai852835f2010-05-21 09:08:56 +08003392 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003393 if (ret != 0)
3394 break;
3395 }
Eric Anholt673a3942008-07-30 12:06:12 -07003396 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003397
Eric Anholt673a3942008-07-30 12:06:12 -07003398 return ret;
3399}
3400
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003401static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003402i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003403 uint32_t buffer_count,
3404 struct drm_i915_gem_relocation_entry **relocs)
3405{
3406 uint32_t reloc_count = 0, reloc_index = 0, i;
3407 int ret;
3408
3409 *relocs = NULL;
3410 for (i = 0; i < buffer_count; i++) {
3411 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3412 return -EINVAL;
3413 reloc_count += exec_list[i].relocation_count;
3414 }
3415
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003416 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003417 if (*relocs == NULL) {
3418 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003419 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003420 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003421
3422 for (i = 0; i < buffer_count; i++) {
3423 struct drm_i915_gem_relocation_entry __user *user_relocs;
3424
3425 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3426
3427 ret = copy_from_user(&(*relocs)[reloc_index],
3428 user_relocs,
3429 exec_list[i].relocation_count *
3430 sizeof(**relocs));
3431 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003432 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003433 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003434 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003435 }
3436
3437 reloc_index += exec_list[i].relocation_count;
3438 }
3439
Florian Mickler2bc43b52009-04-06 22:55:41 +02003440 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003441}
3442
3443static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003444i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003445 uint32_t buffer_count,
3446 struct drm_i915_gem_relocation_entry *relocs)
3447{
3448 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003449 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003450
Chris Wilson93533c22010-01-31 10:40:48 +00003451 if (relocs == NULL)
3452 return 0;
3453
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003454 for (i = 0; i < buffer_count; i++) {
3455 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003456 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003457
3458 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3459
Florian Mickler2bc43b52009-04-06 22:55:41 +02003460 unwritten = copy_to_user(user_relocs,
3461 &relocs[reloc_count],
3462 exec_list[i].relocation_count *
3463 sizeof(*relocs));
3464
3465 if (unwritten) {
3466 ret = -EFAULT;
3467 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003468 }
3469
3470 reloc_count += exec_list[i].relocation_count;
3471 }
3472
Florian Mickler2bc43b52009-04-06 22:55:41 +02003473err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003474 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003475
3476 return ret;
3477}
3478
Chris Wilson83d60792009-06-06 09:45:57 +01003479static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003480i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003481 uint64_t exec_offset)
3482{
3483 uint32_t exec_start, exec_len;
3484
3485 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3486 exec_len = (uint32_t) exec->batch_len;
3487
3488 if ((exec_start | exec_len) & 0x7)
3489 return -EINVAL;
3490
3491 if (!exec_start)
3492 return -EINVAL;
3493
3494 return 0;
3495}
3496
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003497static int
3498i915_gem_wait_for_pending_flip(struct drm_device *dev,
3499 struct drm_gem_object **object_list,
3500 int count)
3501{
3502 drm_i915_private_t *dev_priv = dev->dev_private;
3503 struct drm_i915_gem_object *obj_priv;
3504 DEFINE_WAIT(wait);
3505 int i, ret = 0;
3506
3507 for (;;) {
3508 prepare_to_wait(&dev_priv->pending_flip_queue,
3509 &wait, TASK_INTERRUPTIBLE);
3510 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003511 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003512 if (atomic_read(&obj_priv->pending_flip) > 0)
3513 break;
3514 }
3515 if (i == count)
3516 break;
3517
3518 if (!signal_pending(current)) {
3519 mutex_unlock(&dev->struct_mutex);
3520 schedule();
3521 mutex_lock(&dev->struct_mutex);
3522 continue;
3523 }
3524 ret = -ERESTARTSYS;
3525 break;
3526 }
3527 finish_wait(&dev_priv->pending_flip_queue, &wait);
3528
3529 return ret;
3530}
3531
Chris Wilson43b27f42010-07-02 08:57:15 +01003532
Eric Anholt673a3942008-07-30 12:06:12 -07003533int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003534i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3535 struct drm_file *file_priv,
3536 struct drm_i915_gem_execbuffer2 *args,
3537 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003538{
3539 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003540 struct drm_gem_object **object_list = NULL;
3541 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003542 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003543 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003544 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003545 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003546 uint64_t exec_offset;
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003547 uint32_t seqno, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003548 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003549
Zou Nan hai852835f2010-05-21 09:08:56 +08003550 struct intel_ring_buffer *ring = NULL;
3551
Eric Anholt673a3942008-07-30 12:06:12 -07003552#if WATCH_EXEC
3553 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3554 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3555#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003556 if (args->flags & I915_EXEC_BSD) {
3557 if (!HAS_BSD(dev)) {
3558 DRM_ERROR("execbuf with wrong flag\n");
3559 return -EINVAL;
3560 }
3561 ring = &dev_priv->bsd_ring;
3562 } else {
3563 ring = &dev_priv->render_ring;
3564 }
3565
Eric Anholt4f481ed2008-09-10 14:22:49 -07003566 if (args->buffer_count < 1) {
3567 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3568 return -EINVAL;
3569 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003570 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003571 if (object_list == NULL) {
3572 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003573 args->buffer_count);
3574 ret = -ENOMEM;
3575 goto pre_mutex_err;
3576 }
Eric Anholt673a3942008-07-30 12:06:12 -07003577
Eric Anholt201361a2009-03-11 12:30:04 -07003578 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003579 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3580 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003581 if (cliprects == NULL) {
3582 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003583 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003584 }
Eric Anholt201361a2009-03-11 12:30:04 -07003585
3586 ret = copy_from_user(cliprects,
3587 (struct drm_clip_rect __user *)
3588 (uintptr_t) args->cliprects_ptr,
3589 sizeof(*cliprects) * args->num_cliprects);
3590 if (ret != 0) {
3591 DRM_ERROR("copy %d cliprects failed: %d\n",
3592 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003593 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003594 goto pre_mutex_err;
3595 }
3596 }
3597
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003598 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3599 &relocs);
3600 if (ret != 0)
3601 goto pre_mutex_err;
3602
Eric Anholt673a3942008-07-30 12:06:12 -07003603 mutex_lock(&dev->struct_mutex);
3604
3605 i915_verify_inactive(dev, __FILE__, __LINE__);
3606
Ben Gamariba1234d2009-09-14 17:48:47 -04003607 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003608 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003609 ret = -EIO;
3610 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003611 }
3612
3613 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003614 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003615 ret = -EBUSY;
3616 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003617 }
3618
Keith Packardac94a962008-11-20 23:30:27 -08003619 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003620 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003621 for (i = 0; i < args->buffer_count; i++) {
3622 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3623 exec_list[i].handle);
3624 if (object_list[i] == NULL) {
3625 DRM_ERROR("Invalid object handle %d at index %d\n",
3626 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003627 /* prevent error path from reading uninitialized data */
3628 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003629 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003630 goto err;
3631 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003632
Daniel Vetter23010e42010-03-08 13:35:02 +01003633 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003634 if (obj_priv->in_execbuffer) {
3635 DRM_ERROR("Object %p appears more than once in object list\n",
3636 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003637 /* prevent error path from reading uninitialized data */
3638 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003639 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003640 goto err;
3641 }
3642 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003643 flips += atomic_read(&obj_priv->pending_flip);
3644 }
3645
3646 if (flips > 0) {
3647 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3648 args->buffer_count);
3649 if (ret)
3650 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003651 }
Eric Anholt673a3942008-07-30 12:06:12 -07003652
Keith Packardac94a962008-11-20 23:30:27 -08003653 /* Pin and relocate */
3654 for (pin_tries = 0; ; pin_tries++) {
3655 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003656 reloc_index = 0;
3657
Keith Packardac94a962008-11-20 23:30:27 -08003658 for (i = 0; i < args->buffer_count; i++) {
3659 object_list[i]->pending_read_domains = 0;
3660 object_list[i]->pending_write_domain = 0;
3661 ret = i915_gem_object_pin_and_relocate(object_list[i],
3662 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003663 &exec_list[i],
3664 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003665 if (ret)
3666 break;
3667 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003668 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003669 }
3670 /* success */
3671 if (ret == 0)
3672 break;
3673
3674 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003675 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003676 if (ret != -ERESTARTSYS) {
3677 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003678 int num_fences = 0;
3679 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003680 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003681
Chris Wilson07f73f62009-09-14 16:50:30 +01003682 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003683 num_fences +=
3684 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3685 obj_priv->tiling_mode != I915_TILING_NONE;
3686 }
3687 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003688 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003689 total_size, num_fences,
3690 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003691 DRM_ERROR("%d objects [%d pinned], "
3692 "%d object bytes [%d pinned], "
3693 "%d/%d gtt bytes\n",
3694 atomic_read(&dev->object_count),
3695 atomic_read(&dev->pin_count),
3696 atomic_read(&dev->object_memory),
3697 atomic_read(&dev->pin_memory),
3698 atomic_read(&dev->gtt_memory),
3699 dev->gtt_total);
3700 }
Eric Anholt673a3942008-07-30 12:06:12 -07003701 goto err;
3702 }
Keith Packardac94a962008-11-20 23:30:27 -08003703
3704 /* unpin all of our buffers */
3705 for (i = 0; i < pinned; i++)
3706 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003707 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003708
3709 /* evict everyone we can from the aperture */
3710 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003711 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003712 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003713 }
3714
3715 /* Set the pending read domains for the batch buffer to COMMAND */
3716 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003717 if (batch_obj->pending_write_domain) {
3718 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3719 ret = -EINVAL;
3720 goto err;
3721 }
3722 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003723
Chris Wilson83d60792009-06-06 09:45:57 +01003724 /* Sanity check the batch buffer, prior to moving objects */
3725 exec_offset = exec_list[args->buffer_count - 1].offset;
3726 ret = i915_gem_check_execbuffer (args, exec_offset);
3727 if (ret != 0) {
3728 DRM_ERROR("execbuf with invalid offset/length\n");
3729 goto err;
3730 }
3731
Eric Anholt673a3942008-07-30 12:06:12 -07003732 i915_verify_inactive(dev, __FILE__, __LINE__);
3733
Keith Packard646f0f62008-11-20 23:23:03 -08003734 /* Zero the global flush/invalidate flags. These
3735 * will be modified as new domains are computed
3736 * for each object
3737 */
3738 dev->invalidate_domains = 0;
3739 dev->flush_domains = 0;
3740
Eric Anholt673a3942008-07-30 12:06:12 -07003741 for (i = 0; i < args->buffer_count; i++) {
3742 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003743
Keith Packard646f0f62008-11-20 23:23:03 -08003744 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003745 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003746 }
3747
3748 i915_verify_inactive(dev, __FILE__, __LINE__);
3749
Keith Packard646f0f62008-11-20 23:23:03 -08003750 if (dev->invalidate_domains | dev->flush_domains) {
3751#if WATCH_EXEC
3752 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3753 __func__,
3754 dev->invalidate_domains,
3755 dev->flush_domains);
3756#endif
3757 i915_gem_flush(dev,
3758 dev->invalidate_domains,
3759 dev->flush_domains);
Daniel Vettera6910432010-02-02 17:08:37 +01003760 }
3761
3762 if (dev_priv->render_ring.outstanding_lazy_request) {
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003763 (void)i915_add_request(dev, file_priv, &dev_priv->render_ring);
Daniel Vettera6910432010-02-02 17:08:37 +01003764 dev_priv->render_ring.outstanding_lazy_request = false;
3765 }
3766 if (dev_priv->bsd_ring.outstanding_lazy_request) {
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003767 (void)i915_add_request(dev, file_priv, &dev_priv->bsd_ring);
Daniel Vettera6910432010-02-02 17:08:37 +01003768 dev_priv->bsd_ring.outstanding_lazy_request = false;
Keith Packard646f0f62008-11-20 23:23:03 -08003769 }
Eric Anholt673a3942008-07-30 12:06:12 -07003770
Eric Anholtefbeed92009-02-19 14:54:51 -08003771 for (i = 0; i < args->buffer_count; i++) {
3772 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003773 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003774 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003775
3776 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003777 if (obj->write_domain)
3778 list_move_tail(&obj_priv->gpu_write_list,
3779 &dev_priv->mm.gpu_write_list);
3780 else
3781 list_del_init(&obj_priv->gpu_write_list);
3782
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003783 trace_i915_gem_object_change_domain(obj,
3784 obj->read_domains,
3785 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003786 }
3787
Eric Anholt673a3942008-07-30 12:06:12 -07003788 i915_verify_inactive(dev, __FILE__, __LINE__);
3789
3790#if WATCH_COHERENCY
3791 for (i = 0; i < args->buffer_count; i++) {
3792 i915_gem_object_check_coherency(object_list[i],
3793 exec_list[i].handle);
3794 }
3795#endif
3796
Eric Anholt673a3942008-07-30 12:06:12 -07003797#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003798 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003799 args->batch_len,
3800 __func__,
3801 ~0);
3802#endif
3803
Eric Anholt673a3942008-07-30 12:06:12 -07003804 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003805 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3806 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003807 if (ret) {
3808 DRM_ERROR("dispatch failed %d\n", ret);
3809 goto err;
3810 }
3811
3812 /*
3813 * Ensure that the commands in the batch buffer are
3814 * finished before the interrupt fires
3815 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003816 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003817
3818 i915_verify_inactive(dev, __FILE__, __LINE__);
3819
Daniel Vetter617dbe22010-02-11 22:16:02 +01003820 for (i = 0; i < args->buffer_count; i++) {
3821 struct drm_gem_object *obj = object_list[i];
3822 obj_priv = to_intel_bo(obj);
3823
3824 i915_gem_object_move_to_active(obj, ring);
3825#if WATCH_LRU
3826 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3827#endif
3828 }
3829
Eric Anholt673a3942008-07-30 12:06:12 -07003830 /*
3831 * Get a seqno representing the execution of the current buffer,
3832 * which we can wait on. We would like to mitigate these interrupts,
3833 * likely by only creating seqnos occasionally (so that we have
3834 * *some* interrupts representing completion of buffers that we can
3835 * wait on when trying to clear up gtt space).
3836 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003837 seqno = i915_add_request(dev, file_priv, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003838
Eric Anholt673a3942008-07-30 12:06:12 -07003839#if WATCH_LRU
3840 i915_dump_lru(dev, __func__);
3841#endif
3842
3843 i915_verify_inactive(dev, __FILE__, __LINE__);
3844
Eric Anholt673a3942008-07-30 12:06:12 -07003845err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003846 for (i = 0; i < pinned; i++)
3847 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003848
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003849 for (i = 0; i < args->buffer_count; i++) {
3850 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003851 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003852 obj_priv->in_execbuffer = false;
3853 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003854 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003855 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003856
Eric Anholt673a3942008-07-30 12:06:12 -07003857 mutex_unlock(&dev->struct_mutex);
3858
Chris Wilson93533c22010-01-31 10:40:48 +00003859pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003860 /* Copy the updated relocations out regardless of current error
3861 * state. Failure to update the relocs would mean that the next
3862 * time userland calls execbuf, it would do so with presumed offset
3863 * state that didn't match the actual object state.
3864 */
3865 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3866 relocs);
3867 if (ret2 != 0) {
3868 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3869
3870 if (ret == 0)
3871 ret = ret2;
3872 }
3873
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003874 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003875 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003876
3877 return ret;
3878}
3879
Jesse Barnes76446ca2009-12-17 22:05:42 -05003880/*
3881 * Legacy execbuffer just creates an exec2 list from the original exec object
3882 * list array and passes it to the real function.
3883 */
3884int
3885i915_gem_execbuffer(struct drm_device *dev, void *data,
3886 struct drm_file *file_priv)
3887{
3888 struct drm_i915_gem_execbuffer *args = data;
3889 struct drm_i915_gem_execbuffer2 exec2;
3890 struct drm_i915_gem_exec_object *exec_list = NULL;
3891 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3892 int ret, i;
3893
3894#if WATCH_EXEC
3895 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3896 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3897#endif
3898
3899 if (args->buffer_count < 1) {
3900 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3901 return -EINVAL;
3902 }
3903
3904 /* Copy in the exec list from userland */
3905 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3906 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3907 if (exec_list == NULL || exec2_list == NULL) {
3908 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3909 args->buffer_count);
3910 drm_free_large(exec_list);
3911 drm_free_large(exec2_list);
3912 return -ENOMEM;
3913 }
3914 ret = copy_from_user(exec_list,
3915 (struct drm_i915_relocation_entry __user *)
3916 (uintptr_t) args->buffers_ptr,
3917 sizeof(*exec_list) * args->buffer_count);
3918 if (ret != 0) {
3919 DRM_ERROR("copy %d exec entries failed %d\n",
3920 args->buffer_count, ret);
3921 drm_free_large(exec_list);
3922 drm_free_large(exec2_list);
3923 return -EFAULT;
3924 }
3925
3926 for (i = 0; i < args->buffer_count; i++) {
3927 exec2_list[i].handle = exec_list[i].handle;
3928 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3929 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3930 exec2_list[i].alignment = exec_list[i].alignment;
3931 exec2_list[i].offset = exec_list[i].offset;
3932 if (!IS_I965G(dev))
3933 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3934 else
3935 exec2_list[i].flags = 0;
3936 }
3937
3938 exec2.buffers_ptr = args->buffers_ptr;
3939 exec2.buffer_count = args->buffer_count;
3940 exec2.batch_start_offset = args->batch_start_offset;
3941 exec2.batch_len = args->batch_len;
3942 exec2.DR1 = args->DR1;
3943 exec2.DR4 = args->DR4;
3944 exec2.num_cliprects = args->num_cliprects;
3945 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003946 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003947
3948 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3949 if (!ret) {
3950 /* Copy the new buffer offsets back to the user's exec list. */
3951 for (i = 0; i < args->buffer_count; i++)
3952 exec_list[i].offset = exec2_list[i].offset;
3953 /* ... and back out to userspace */
3954 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3955 (uintptr_t) args->buffers_ptr,
3956 exec_list,
3957 sizeof(*exec_list) * args->buffer_count);
3958 if (ret) {
3959 ret = -EFAULT;
3960 DRM_ERROR("failed to copy %d exec entries "
3961 "back to user (%d)\n",
3962 args->buffer_count, ret);
3963 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003964 }
3965
3966 drm_free_large(exec_list);
3967 drm_free_large(exec2_list);
3968 return ret;
3969}
3970
3971int
3972i915_gem_execbuffer2(struct drm_device *dev, void *data,
3973 struct drm_file *file_priv)
3974{
3975 struct drm_i915_gem_execbuffer2 *args = data;
3976 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3977 int ret;
3978
3979#if WATCH_EXEC
3980 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3981 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3982#endif
3983
3984 if (args->buffer_count < 1) {
3985 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
3986 return -EINVAL;
3987 }
3988
3989 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3990 if (exec2_list == NULL) {
3991 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3992 args->buffer_count);
3993 return -ENOMEM;
3994 }
3995 ret = copy_from_user(exec2_list,
3996 (struct drm_i915_relocation_entry __user *)
3997 (uintptr_t) args->buffers_ptr,
3998 sizeof(*exec2_list) * args->buffer_count);
3999 if (ret != 0) {
4000 DRM_ERROR("copy %d exec entries failed %d\n",
4001 args->buffer_count, ret);
4002 drm_free_large(exec2_list);
4003 return -EFAULT;
4004 }
4005
4006 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4007 if (!ret) {
4008 /* Copy the new buffer offsets back to the user's exec list. */
4009 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4010 (uintptr_t) args->buffers_ptr,
4011 exec2_list,
4012 sizeof(*exec2_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 }
4019 }
4020
4021 drm_free_large(exec2_list);
4022 return ret;
4023}
4024
Eric Anholt673a3942008-07-30 12:06:12 -07004025int
4026i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4027{
4028 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004029 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004030 int ret;
4031
Daniel Vetter778c3542010-05-13 11:49:44 +02004032 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4033
Eric Anholt673a3942008-07-30 12:06:12 -07004034 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004035
4036 if (obj_priv->gtt_space != NULL) {
4037 if (alignment == 0)
4038 alignment = i915_gem_get_gtt_alignment(obj);
4039 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004040 WARN(obj_priv->pin_count,
4041 "bo is already pinned with incorrect alignment:"
4042 " offset=%x, req.alignment=%x\n",
4043 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004044 ret = i915_gem_object_unbind(obj);
4045 if (ret)
4046 return ret;
4047 }
4048 }
4049
Eric Anholt673a3942008-07-30 12:06:12 -07004050 if (obj_priv->gtt_space == NULL) {
4051 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004052 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004053 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004054 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004055
Eric Anholt673a3942008-07-30 12:06:12 -07004056 obj_priv->pin_count++;
4057
4058 /* If the object is not active and not pending a flush,
4059 * remove it from the inactive list
4060 */
4061 if (obj_priv->pin_count == 1) {
4062 atomic_inc(&dev->pin_count);
4063 atomic_add(obj->size, &dev->pin_memory);
4064 if (!obj_priv->active &&
Chris Wilsonbf1a1092010-08-07 11:01:20 +01004065 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004066 list_del_init(&obj_priv->list);
4067 }
4068 i915_verify_inactive(dev, __FILE__, __LINE__);
4069
4070 return 0;
4071}
4072
4073void
4074i915_gem_object_unpin(struct drm_gem_object *obj)
4075{
4076 struct drm_device *dev = obj->dev;
4077 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004078 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004079
4080 i915_verify_inactive(dev, __FILE__, __LINE__);
4081 obj_priv->pin_count--;
4082 BUG_ON(obj_priv->pin_count < 0);
4083 BUG_ON(obj_priv->gtt_space == NULL);
4084
4085 /* If the object is no longer pinned, and is
4086 * neither active nor being flushed, then stick it on
4087 * the inactive list
4088 */
4089 if (obj_priv->pin_count == 0) {
4090 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004091 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004092 list_move_tail(&obj_priv->list,
4093 &dev_priv->mm.inactive_list);
4094 atomic_dec(&dev->pin_count);
4095 atomic_sub(obj->size, &dev->pin_memory);
4096 }
4097 i915_verify_inactive(dev, __FILE__, __LINE__);
4098}
4099
4100int
4101i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4102 struct drm_file *file_priv)
4103{
4104 struct drm_i915_gem_pin *args = data;
4105 struct drm_gem_object *obj;
4106 struct drm_i915_gem_object *obj_priv;
4107 int ret;
4108
4109 mutex_lock(&dev->struct_mutex);
4110
4111 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4112 if (obj == NULL) {
4113 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4114 args->handle);
4115 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004116 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004117 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004118 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004119
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004120 if (obj_priv->madv != I915_MADV_WILLNEED) {
4121 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004122 drm_gem_object_unreference(obj);
4123 mutex_unlock(&dev->struct_mutex);
4124 return -EINVAL;
4125 }
4126
Jesse Barnes79e53942008-11-07 14:24:08 -08004127 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4128 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4129 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004130 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004131 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004132 return -EINVAL;
4133 }
4134
4135 obj_priv->user_pin_count++;
4136 obj_priv->pin_filp = file_priv;
4137 if (obj_priv->user_pin_count == 1) {
4138 ret = i915_gem_object_pin(obj, args->alignment);
4139 if (ret != 0) {
4140 drm_gem_object_unreference(obj);
4141 mutex_unlock(&dev->struct_mutex);
4142 return ret;
4143 }
Eric Anholt673a3942008-07-30 12:06:12 -07004144 }
4145
4146 /* XXX - flush the CPU caches for pinned objects
4147 * as the X server doesn't manage domains yet
4148 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004149 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004150 args->offset = obj_priv->gtt_offset;
4151 drm_gem_object_unreference(obj);
4152 mutex_unlock(&dev->struct_mutex);
4153
4154 return 0;
4155}
4156
4157int
4158i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4159 struct drm_file *file_priv)
4160{
4161 struct drm_i915_gem_pin *args = data;
4162 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004163 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004164
4165 mutex_lock(&dev->struct_mutex);
4166
4167 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4168 if (obj == NULL) {
4169 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4170 args->handle);
4171 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004172 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004173 }
4174
Daniel Vetter23010e42010-03-08 13:35:02 +01004175 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004176 if (obj_priv->pin_filp != file_priv) {
4177 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4178 args->handle);
4179 drm_gem_object_unreference(obj);
4180 mutex_unlock(&dev->struct_mutex);
4181 return -EINVAL;
4182 }
4183 obj_priv->user_pin_count--;
4184 if (obj_priv->user_pin_count == 0) {
4185 obj_priv->pin_filp = NULL;
4186 i915_gem_object_unpin(obj);
4187 }
Eric Anholt673a3942008-07-30 12:06:12 -07004188
4189 drm_gem_object_unreference(obj);
4190 mutex_unlock(&dev->struct_mutex);
4191 return 0;
4192}
4193
4194int
4195i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4196 struct drm_file *file_priv)
4197{
4198 struct drm_i915_gem_busy *args = data;
4199 struct drm_gem_object *obj;
4200 struct drm_i915_gem_object *obj_priv;
4201
Eric Anholt673a3942008-07-30 12:06:12 -07004202 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4203 if (obj == NULL) {
4204 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4205 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004206 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004207 }
4208
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004209 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004210
Chris Wilson0be555b2010-08-04 15:36:30 +01004211 /* Count all active objects as busy, even if they are currently not used
4212 * by the gpu. Users of this interface expect objects to eventually
4213 * become non-busy without any further actions, therefore emit any
4214 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004215 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004216 obj_priv = to_intel_bo(obj);
4217 args->busy = obj_priv->active;
4218 if (args->busy) {
4219 /* Unconditionally flush objects, even when the gpu still uses this
4220 * object. Userspace calling this function indicates that it wants to
4221 * use this buffer rather sooner than later, so issuing the required
4222 * flush earlier is beneficial.
4223 */
4224 if (obj->write_domain) {
4225 i915_gem_flush(dev, 0, obj->write_domain);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01004226 (void)i915_add_request(dev, file_priv, obj_priv->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01004227 }
4228
4229 /* Update the active list for the hardware's current position.
4230 * Otherwise this only updates on a delayed timer or when irqs
4231 * are actually unmasked, and our working set ends up being
4232 * larger than required.
4233 */
4234 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4235
4236 args->busy = obj_priv->active;
4237 }
Eric Anholt673a3942008-07-30 12:06:12 -07004238
4239 drm_gem_object_unreference(obj);
4240 mutex_unlock(&dev->struct_mutex);
4241 return 0;
4242}
4243
4244int
4245i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4246 struct drm_file *file_priv)
4247{
4248 return i915_gem_ring_throttle(dev, file_priv);
4249}
4250
Chris Wilson3ef94da2009-09-14 16:50:29 +01004251int
4252i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4253 struct drm_file *file_priv)
4254{
4255 struct drm_i915_gem_madvise *args = data;
4256 struct drm_gem_object *obj;
4257 struct drm_i915_gem_object *obj_priv;
4258
4259 switch (args->madv) {
4260 case I915_MADV_DONTNEED:
4261 case I915_MADV_WILLNEED:
4262 break;
4263 default:
4264 return -EINVAL;
4265 }
4266
4267 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4268 if (obj == NULL) {
4269 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4270 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004271 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004272 }
4273
4274 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004275 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004276
4277 if (obj_priv->pin_count) {
4278 drm_gem_object_unreference(obj);
4279 mutex_unlock(&dev->struct_mutex);
4280
4281 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4282 return -EINVAL;
4283 }
4284
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004285 if (obj_priv->madv != __I915_MADV_PURGED)
4286 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004287
Chris Wilson2d7ef392009-09-20 23:13:10 +01004288 /* if the object is no longer bound, discard its backing storage */
4289 if (i915_gem_object_is_purgeable(obj_priv) &&
4290 obj_priv->gtt_space == NULL)
4291 i915_gem_object_truncate(obj);
4292
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004293 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4294
Chris Wilson3ef94da2009-09-14 16:50:29 +01004295 drm_gem_object_unreference(obj);
4296 mutex_unlock(&dev->struct_mutex);
4297
4298 return 0;
4299}
4300
Daniel Vetterac52bc52010-04-09 19:05:06 +00004301struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4302 size_t size)
4303{
Daniel Vetterc397b902010-04-09 19:05:07 +00004304 struct drm_i915_gem_object *obj;
4305
4306 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4307 if (obj == NULL)
4308 return NULL;
4309
4310 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4311 kfree(obj);
4312 return NULL;
4313 }
4314
4315 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4316 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4317
4318 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004319 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004320 obj->fence_reg = I915_FENCE_REG_NONE;
4321 INIT_LIST_HEAD(&obj->list);
4322 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004323 obj->madv = I915_MADV_WILLNEED;
4324
4325 trace_i915_gem_object_create(&obj->base);
4326
4327 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004328}
4329
Eric Anholt673a3942008-07-30 12:06:12 -07004330int i915_gem_init_object(struct drm_gem_object *obj)
4331{
Daniel Vetterc397b902010-04-09 19:05:07 +00004332 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004333
Eric Anholt673a3942008-07-30 12:06:12 -07004334 return 0;
4335}
4336
Chris Wilsonbe726152010-07-23 23:18:50 +01004337static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4338{
4339 struct drm_device *dev = obj->dev;
4340 drm_i915_private_t *dev_priv = dev->dev_private;
4341 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4342 int ret;
4343
4344 ret = i915_gem_object_unbind(obj);
4345 if (ret == -ERESTARTSYS) {
4346 list_move(&obj_priv->list,
4347 &dev_priv->mm.deferred_free_list);
4348 return;
4349 }
4350
4351 if (obj_priv->mmap_offset)
4352 i915_gem_free_mmap_offset(obj);
4353
4354 drm_gem_object_release(obj);
4355
4356 kfree(obj_priv->page_cpu_valid);
4357 kfree(obj_priv->bit_17);
4358 kfree(obj_priv);
4359}
4360
Eric Anholt673a3942008-07-30 12:06:12 -07004361void i915_gem_free_object(struct drm_gem_object *obj)
4362{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004363 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004364 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004365
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004366 trace_i915_gem_object_destroy(obj);
4367
Eric Anholt673a3942008-07-30 12:06:12 -07004368 while (obj_priv->pin_count > 0)
4369 i915_gem_object_unpin(obj);
4370
Dave Airlie71acb5e2008-12-30 20:31:46 +10004371 if (obj_priv->phys_obj)
4372 i915_gem_detach_phys_object(dev, obj);
4373
Chris Wilsonbe726152010-07-23 23:18:50 +01004374 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004375}
4376
Jesse Barnes5669fca2009-02-17 15:13:31 -08004377int
Eric Anholt673a3942008-07-30 12:06:12 -07004378i915_gem_idle(struct drm_device *dev)
4379{
4380 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004381 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004382
Keith Packard6dbe2772008-10-14 21:41:13 -07004383 mutex_lock(&dev->struct_mutex);
4384
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004385 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004386 (dev_priv->render_ring.gem_object == NULL) ||
4387 (HAS_BSD(dev) &&
4388 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004389 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004390 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004391 }
Eric Anholt673a3942008-07-30 12:06:12 -07004392
Chris Wilson29105cc2010-01-07 10:39:13 +00004393 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004394 if (ret) {
4395 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004396 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004397 }
Eric Anholt673a3942008-07-30 12:06:12 -07004398
Chris Wilson29105cc2010-01-07 10:39:13 +00004399 /* Under UMS, be paranoid and evict. */
4400 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004401 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004402 if (ret) {
4403 mutex_unlock(&dev->struct_mutex);
4404 return ret;
4405 }
4406 }
4407
4408 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4409 * We need to replace this with a semaphore, or something.
4410 * And not confound mm.suspended!
4411 */
4412 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004413 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004414
4415 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004416 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004417
Keith Packard6dbe2772008-10-14 21:41:13 -07004418 mutex_unlock(&dev->struct_mutex);
4419
Chris Wilson29105cc2010-01-07 10:39:13 +00004420 /* Cancel the retire work handler, which should be idle now. */
4421 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4422
Eric Anholt673a3942008-07-30 12:06:12 -07004423 return 0;
4424}
4425
Jesse Barnese552eb72010-04-21 11:39:23 -07004426/*
4427 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4428 * over cache flushing.
4429 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004430static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004431i915_gem_init_pipe_control(struct drm_device *dev)
4432{
4433 drm_i915_private_t *dev_priv = dev->dev_private;
4434 struct drm_gem_object *obj;
4435 struct drm_i915_gem_object *obj_priv;
4436 int ret;
4437
Eric Anholt34dc4d42010-05-07 14:30:03 -07004438 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004439 if (obj == NULL) {
4440 DRM_ERROR("Failed to allocate seqno page\n");
4441 ret = -ENOMEM;
4442 goto err;
4443 }
4444 obj_priv = to_intel_bo(obj);
4445 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4446
4447 ret = i915_gem_object_pin(obj, 4096);
4448 if (ret)
4449 goto err_unref;
4450
4451 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4452 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4453 if (dev_priv->seqno_page == NULL)
4454 goto err_unpin;
4455
4456 dev_priv->seqno_obj = obj;
4457 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4458
4459 return 0;
4460
4461err_unpin:
4462 i915_gem_object_unpin(obj);
4463err_unref:
4464 drm_gem_object_unreference(obj);
4465err:
4466 return ret;
4467}
4468
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004469
4470static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004471i915_gem_cleanup_pipe_control(struct drm_device *dev)
4472{
4473 drm_i915_private_t *dev_priv = dev->dev_private;
4474 struct drm_gem_object *obj;
4475 struct drm_i915_gem_object *obj_priv;
4476
4477 obj = dev_priv->seqno_obj;
4478 obj_priv = to_intel_bo(obj);
4479 kunmap(obj_priv->pages[0]);
4480 i915_gem_object_unpin(obj);
4481 drm_gem_object_unreference(obj);
4482 dev_priv->seqno_obj = NULL;
4483
4484 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004485}
4486
Eric Anholt673a3942008-07-30 12:06:12 -07004487int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004488i915_gem_init_ringbuffer(struct drm_device *dev)
4489{
4490 drm_i915_private_t *dev_priv = dev->dev_private;
4491 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004492
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004493 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004494
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004495 if (!I915_NEED_GFX_HWS(dev)) {
4496 dev_priv->render_ring.status_page.page_addr
4497 = dev_priv->status_page_dmah->vaddr;
4498 memset(dev_priv->render_ring.status_page.page_addr,
4499 0, PAGE_SIZE);
4500 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004501
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004502 if (HAS_PIPE_CONTROL(dev)) {
4503 ret = i915_gem_init_pipe_control(dev);
4504 if (ret)
4505 return ret;
4506 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004507
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004508 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004509 if (ret)
4510 goto cleanup_pipe_control;
4511
4512 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004513 dev_priv->bsd_ring = bsd_ring;
4514 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004515 if (ret)
4516 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004517 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004518
Chris Wilson6f392d5482010-08-07 11:01:22 +01004519 dev_priv->next_seqno = 1;
4520
Chris Wilson68f95ba2010-05-27 13:18:22 +01004521 return 0;
4522
4523cleanup_render_ring:
4524 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4525cleanup_pipe_control:
4526 if (HAS_PIPE_CONTROL(dev))
4527 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004528 return ret;
4529}
4530
4531void
4532i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4533{
4534 drm_i915_private_t *dev_priv = dev->dev_private;
4535
4536 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004537 if (HAS_BSD(dev))
4538 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004539 if (HAS_PIPE_CONTROL(dev))
4540 i915_gem_cleanup_pipe_control(dev);
4541}
4542
4543int
Eric Anholt673a3942008-07-30 12:06:12 -07004544i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4545 struct drm_file *file_priv)
4546{
4547 drm_i915_private_t *dev_priv = dev->dev_private;
4548 int ret;
4549
Jesse Barnes79e53942008-11-07 14:24:08 -08004550 if (drm_core_check_feature(dev, DRIVER_MODESET))
4551 return 0;
4552
Ben Gamariba1234d2009-09-14 17:48:47 -04004553 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004554 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004555 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004556 }
4557
Eric Anholt673a3942008-07-30 12:06:12 -07004558 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004559 dev_priv->mm.suspended = 0;
4560
4561 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004562 if (ret != 0) {
4563 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004564 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004565 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004566
Carl Worth5e118f42009-03-20 11:54:25 -07004567 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004568 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004569 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004570 spin_unlock(&dev_priv->mm.active_list_lock);
4571
Eric Anholt673a3942008-07-30 12:06:12 -07004572 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4573 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004574 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004575 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004576 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004577
Chris Wilson5f353082010-06-07 14:03:03 +01004578 ret = drm_irq_install(dev);
4579 if (ret)
4580 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004581
Eric Anholt673a3942008-07-30 12:06:12 -07004582 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004583
4584cleanup_ringbuffer:
4585 mutex_lock(&dev->struct_mutex);
4586 i915_gem_cleanup_ringbuffer(dev);
4587 dev_priv->mm.suspended = 1;
4588 mutex_unlock(&dev->struct_mutex);
4589
4590 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004591}
4592
4593int
4594i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4595 struct drm_file *file_priv)
4596{
Jesse Barnes79e53942008-11-07 14:24:08 -08004597 if (drm_core_check_feature(dev, DRIVER_MODESET))
4598 return 0;
4599
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004600 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004601 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004602}
4603
4604void
4605i915_gem_lastclose(struct drm_device *dev)
4606{
4607 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004608
Eric Anholte806b492009-01-22 09:56:58 -08004609 if (drm_core_check_feature(dev, DRIVER_MODESET))
4610 return;
4611
Keith Packard6dbe2772008-10-14 21:41:13 -07004612 ret = i915_gem_idle(dev);
4613 if (ret)
4614 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004615}
4616
4617void
4618i915_gem_load(struct drm_device *dev)
4619{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004620 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004621 drm_i915_private_t *dev_priv = dev->dev_private;
4622
Carl Worth5e118f42009-03-20 11:54:25 -07004623 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004624 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004625 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004626 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004627 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004628 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004629 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4630 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004631 if (HAS_BSD(dev)) {
4632 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4633 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4634 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004635 for (i = 0; i < 16; i++)
4636 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004637 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4638 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004639 spin_lock(&shrink_list_lock);
4640 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4641 spin_unlock(&shrink_list_lock);
4642
Dave Airlie94400122010-07-20 13:15:31 +10004643 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4644 if (IS_GEN3(dev)) {
4645 u32 tmp = I915_READ(MI_ARB_STATE);
4646 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4647 /* arb state is a masked write, so set bit + bit in mask */
4648 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4649 I915_WRITE(MI_ARB_STATE, tmp);
4650 }
4651 }
4652
Jesse Barnesde151cf2008-11-12 10:03:55 -08004653 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004654 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4655 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004656
Jesse Barnes0f973f22009-01-26 17:10:45 -08004657 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004658 dev_priv->num_fence_regs = 16;
4659 else
4660 dev_priv->num_fence_regs = 8;
4661
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004662 /* Initialize fence registers to zero */
4663 if (IS_I965G(dev)) {
4664 for (i = 0; i < 16; i++)
4665 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4666 } else {
4667 for (i = 0; i < 8; i++)
4668 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4669 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4670 for (i = 0; i < 8; i++)
4671 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4672 }
Eric Anholt673a3942008-07-30 12:06:12 -07004673 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004674 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004675}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004676
4677/*
4678 * Create a physically contiguous memory object for this object
4679 * e.g. for cursor + overlay regs
4680 */
4681int i915_gem_init_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004682 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004683{
4684 drm_i915_private_t *dev_priv = dev->dev_private;
4685 struct drm_i915_gem_phys_object *phys_obj;
4686 int ret;
4687
4688 if (dev_priv->mm.phys_objs[id - 1] || !size)
4689 return 0;
4690
Eric Anholt9a298b22009-03-24 12:23:04 -07004691 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004692 if (!phys_obj)
4693 return -ENOMEM;
4694
4695 phys_obj->id = id;
4696
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004697 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004698 if (!phys_obj->handle) {
4699 ret = -ENOMEM;
4700 goto kfree_obj;
4701 }
4702#ifdef CONFIG_X86
4703 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4704#endif
4705
4706 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4707
4708 return 0;
4709kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004710 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004711 return ret;
4712}
4713
4714void i915_gem_free_phys_object(struct drm_device *dev, int id)
4715{
4716 drm_i915_private_t *dev_priv = dev->dev_private;
4717 struct drm_i915_gem_phys_object *phys_obj;
4718
4719 if (!dev_priv->mm.phys_objs[id - 1])
4720 return;
4721
4722 phys_obj = dev_priv->mm.phys_objs[id - 1];
4723 if (phys_obj->cur_obj) {
4724 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4725 }
4726
4727#ifdef CONFIG_X86
4728 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4729#endif
4730 drm_pci_free(dev, phys_obj->handle);
4731 kfree(phys_obj);
4732 dev_priv->mm.phys_objs[id - 1] = NULL;
4733}
4734
4735void i915_gem_free_all_phys_object(struct drm_device *dev)
4736{
4737 int i;
4738
Dave Airlie260883c2009-01-22 17:58:49 +10004739 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004740 i915_gem_free_phys_object(dev, i);
4741}
4742
4743void i915_gem_detach_phys_object(struct drm_device *dev,
4744 struct drm_gem_object *obj)
4745{
4746 struct drm_i915_gem_object *obj_priv;
4747 int i;
4748 int ret;
4749 int page_count;
4750
Daniel Vetter23010e42010-03-08 13:35:02 +01004751 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004752 if (!obj_priv->phys_obj)
4753 return;
4754
Chris Wilson4bdadb92010-01-27 13:36:32 +00004755 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004756 if (ret)
4757 goto out;
4758
4759 page_count = obj->size / PAGE_SIZE;
4760
4761 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004762 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004763 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4764
4765 memcpy(dst, src, PAGE_SIZE);
4766 kunmap_atomic(dst, KM_USER0);
4767 }
Eric Anholt856fa192009-03-19 14:10:50 -07004768 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004769 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004770
4771 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004772out:
4773 obj_priv->phys_obj->cur_obj = NULL;
4774 obj_priv->phys_obj = NULL;
4775}
4776
4777int
4778i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004779 struct drm_gem_object *obj,
4780 int id,
4781 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004782{
4783 drm_i915_private_t *dev_priv = dev->dev_private;
4784 struct drm_i915_gem_object *obj_priv;
4785 int ret = 0;
4786 int page_count;
4787 int i;
4788
4789 if (id > I915_MAX_PHYS_OBJECT)
4790 return -EINVAL;
4791
Daniel Vetter23010e42010-03-08 13:35:02 +01004792 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004793
4794 if (obj_priv->phys_obj) {
4795 if (obj_priv->phys_obj->id == id)
4796 return 0;
4797 i915_gem_detach_phys_object(dev, obj);
4798 }
4799
Dave Airlie71acb5e2008-12-30 20:31:46 +10004800 /* create a new object */
4801 if (!dev_priv->mm.phys_objs[id - 1]) {
4802 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004803 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004804 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004805 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004806 goto out;
4807 }
4808 }
4809
4810 /* bind to the object */
4811 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4812 obj_priv->phys_obj->cur_obj = obj;
4813
Chris Wilson4bdadb92010-01-27 13:36:32 +00004814 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004815 if (ret) {
4816 DRM_ERROR("failed to get page list\n");
4817 goto out;
4818 }
4819
4820 page_count = obj->size / PAGE_SIZE;
4821
4822 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004823 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004824 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4825
4826 memcpy(dst, src, PAGE_SIZE);
4827 kunmap_atomic(src, KM_USER0);
4828 }
4829
Chris Wilsond78b47b2009-06-17 21:52:49 +01004830 i915_gem_object_put_pages(obj);
4831
Dave Airlie71acb5e2008-12-30 20:31:46 +10004832 return 0;
4833out:
4834 return ret;
4835}
4836
4837static int
4838i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4839 struct drm_i915_gem_pwrite *args,
4840 struct drm_file *file_priv)
4841{
Daniel Vetter23010e42010-03-08 13:35:02 +01004842 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004843 void *obj_addr;
4844 int ret;
4845 char __user *user_data;
4846
4847 user_data = (char __user *) (uintptr_t) args->data_ptr;
4848 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4849
Zhao Yakui44d98a62009-10-09 11:39:40 +08004850 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004851 ret = copy_from_user(obj_addr, user_data, args->size);
4852 if (ret)
4853 return -EFAULT;
4854
4855 drm_agp_chipset_flush(dev);
4856 return 0;
4857}
Eric Anholtb9624422009-06-03 07:27:35 +00004858
4859void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4860{
4861 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4862
4863 /* Clean up our request list when the client is going away, so that
4864 * later retire_requests won't dereference our soon-to-be-gone
4865 * file_priv.
4866 */
4867 mutex_lock(&dev->struct_mutex);
4868 while (!list_empty(&i915_file_priv->mm.request_list))
4869 list_del_init(i915_file_priv->mm.request_list.next);
4870 mutex_unlock(&dev->struct_mutex);
4871}
Chris Wilson31169712009-09-14 16:50:28 +01004872
Chris Wilson31169712009-09-14 16:50:28 +01004873static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004874i915_gpu_is_active(struct drm_device *dev)
4875{
4876 drm_i915_private_t *dev_priv = dev->dev_private;
4877 int lists_empty;
4878
4879 spin_lock(&dev_priv->mm.active_list_lock);
4880 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004881 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004882 if (HAS_BSD(dev))
4883 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004884 spin_unlock(&dev_priv->mm.active_list_lock);
4885
4886 return !lists_empty;
4887}
4888
4889static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004890i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004891{
4892 drm_i915_private_t *dev_priv, *next_dev;
4893 struct drm_i915_gem_object *obj_priv, *next_obj;
4894 int cnt = 0;
4895 int would_deadlock = 1;
4896
4897 /* "fast-path" to count number of available objects */
4898 if (nr_to_scan == 0) {
4899 spin_lock(&shrink_list_lock);
4900 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4901 struct drm_device *dev = dev_priv->dev;
4902
4903 if (mutex_trylock(&dev->struct_mutex)) {
4904 list_for_each_entry(obj_priv,
4905 &dev_priv->mm.inactive_list,
4906 list)
4907 cnt++;
4908 mutex_unlock(&dev->struct_mutex);
4909 }
4910 }
4911 spin_unlock(&shrink_list_lock);
4912
4913 return (cnt / 100) * sysctl_vfs_cache_pressure;
4914 }
4915
4916 spin_lock(&shrink_list_lock);
4917
Chris Wilson1637ef42010-04-20 17:10:35 +01004918rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004919 /* first scan for clean buffers */
4920 list_for_each_entry_safe(dev_priv, next_dev,
4921 &shrink_list, mm.shrink_list) {
4922 struct drm_device *dev = dev_priv->dev;
4923
4924 if (! mutex_trylock(&dev->struct_mutex))
4925 continue;
4926
4927 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004928 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004929
Chris Wilson31169712009-09-14 16:50:28 +01004930 list_for_each_entry_safe(obj_priv, next_obj,
4931 &dev_priv->mm.inactive_list,
4932 list) {
4933 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004934 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004935 if (--nr_to_scan <= 0)
4936 break;
4937 }
4938 }
4939
4940 spin_lock(&shrink_list_lock);
4941 mutex_unlock(&dev->struct_mutex);
4942
Chris Wilson963b4832009-09-20 23:03:54 +01004943 would_deadlock = 0;
4944
Chris Wilson31169712009-09-14 16:50:28 +01004945 if (nr_to_scan <= 0)
4946 break;
4947 }
4948
4949 /* second pass, evict/count anything still on the inactive list */
4950 list_for_each_entry_safe(dev_priv, next_dev,
4951 &shrink_list, mm.shrink_list) {
4952 struct drm_device *dev = dev_priv->dev;
4953
4954 if (! mutex_trylock(&dev->struct_mutex))
4955 continue;
4956
4957 spin_unlock(&shrink_list_lock);
4958
4959 list_for_each_entry_safe(obj_priv, next_obj,
4960 &dev_priv->mm.inactive_list,
4961 list) {
4962 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004963 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004964 nr_to_scan--;
4965 } else
4966 cnt++;
4967 }
4968
4969 spin_lock(&shrink_list_lock);
4970 mutex_unlock(&dev->struct_mutex);
4971
4972 would_deadlock = 0;
4973 }
4974
Chris Wilson1637ef42010-04-20 17:10:35 +01004975 if (nr_to_scan) {
4976 int active = 0;
4977
4978 /*
4979 * We are desperate for pages, so as a last resort, wait
4980 * for the GPU to finish and discard whatever we can.
4981 * This has a dramatic impact to reduce the number of
4982 * OOM-killer events whilst running the GPU aggressively.
4983 */
4984 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4985 struct drm_device *dev = dev_priv->dev;
4986
4987 if (!mutex_trylock(&dev->struct_mutex))
4988 continue;
4989
4990 spin_unlock(&shrink_list_lock);
4991
4992 if (i915_gpu_is_active(dev)) {
4993 i915_gpu_idle(dev);
4994 active++;
4995 }
4996
4997 spin_lock(&shrink_list_lock);
4998 mutex_unlock(&dev->struct_mutex);
4999 }
5000
5001 if (active)
5002 goto rescan;
5003 }
5004
Chris Wilson31169712009-09-14 16:50:28 +01005005 spin_unlock(&shrink_list_lock);
5006
5007 if (would_deadlock)
5008 return -1;
5009 else if (cnt > 0)
5010 return (cnt / 100) * sysctl_vfs_cache_pressure;
5011 else
5012 return 0;
5013}
5014
5015static struct shrinker shrinker = {
5016 .shrink = i915_gem_shrink,
5017 .seeks = DEFAULT_SEEKS,
5018};
5019
5020__init void
5021i915_gem_shrinker_init(void)
5022{
5023 register_shrinker(&shrinker);
5024}
5025
5026__exit void
5027i915_gem_shrinker_exit(void)
5028{
5029 unregister_shrinker(&shrinker);
5030}