blob: c60ca32f65d254d0788a6972fcd1aa1459c617eb [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"
Jesse Barnes652c3932009-08-17 13:31:43 -070032#include "intel_drv.h"
Eric Anholt673a3942008-07-30 12:06:12 -070033#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080034#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035
Eric Anholt28dfe522008-11-13 15:00:55 -080036#define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
37
Eric Anholte47c68e2008-11-14 13:35:19 -080038static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
39static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
40static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080041static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
42 int write);
43static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
44 uint64_t offset,
45 uint64_t size);
46static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070047static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080048static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
49 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080050static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Chris Wilson07f73f62009-09-14 16:50:30 +010051static int i915_gem_evict_something(struct drm_device *dev, int min_size);
52static int i915_gem_evict_from_list(struct drm_device *dev,
53 struct list_head *head);
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);
Eric Anholt673a3942008-07-30 12:06:12 -070057
Chris Wilson31169712009-09-14 16:50:28 +010058static LIST_HEAD(shrink_list);
59static DEFINE_SPINLOCK(shrink_list_lock);
60
Jesse Barnes79e53942008-11-07 14:24:08 -080061int i915_gem_do_init(struct drm_device *dev, unsigned long start,
62 unsigned long end)
63{
64 drm_i915_private_t *dev_priv = dev->dev_private;
65
66 if (start >= end ||
67 (start & (PAGE_SIZE - 1)) != 0 ||
68 (end & (PAGE_SIZE - 1)) != 0) {
69 return -EINVAL;
70 }
71
72 drm_mm_init(&dev_priv->mm.gtt_space, start,
73 end - start);
74
75 dev->gtt_total = (uint32_t) (end - start);
76
77 return 0;
78}
Keith Packard6dbe2772008-10-14 21:41:13 -070079
Eric Anholt673a3942008-07-30 12:06:12 -070080int
81i915_gem_init_ioctl(struct drm_device *dev, void *data,
82 struct drm_file *file_priv)
83{
Eric Anholt673a3942008-07-30 12:06:12 -070084 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080085 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070086
87 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080088 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070089 mutex_unlock(&dev->struct_mutex);
90
Jesse Barnes79e53942008-11-07 14:24:08 -080091 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070092}
93
Eric Anholt5a125c32008-10-22 21:40:13 -070094int
95i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
96 struct drm_file *file_priv)
97{
Eric Anholt5a125c32008-10-22 21:40:13 -070098 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -070099
100 if (!(dev->driver->driver_features & DRIVER_GEM))
101 return -ENODEV;
102
103 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800104 args->aper_available_size = (args->aper_size -
105 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700106
107 return 0;
108}
109
Eric Anholt673a3942008-07-30 12:06:12 -0700110
111/**
112 * Creates a new mm object and returns a handle to it.
113 */
114int
115i915_gem_create_ioctl(struct drm_device *dev, void *data,
116 struct drm_file *file_priv)
117{
118 struct drm_i915_gem_create *args = data;
119 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300120 int ret;
121 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700122
123 args->size = roundup(args->size, PAGE_SIZE);
124
125 /* Allocate the new object */
126 obj = drm_gem_object_alloc(dev, args->size);
127 if (obj == NULL)
128 return -ENOMEM;
129
130 ret = drm_gem_handle_create(file_priv, obj, &handle);
131 mutex_lock(&dev->struct_mutex);
132 drm_gem_object_handle_unreference(obj);
133 mutex_unlock(&dev->struct_mutex);
134
135 if (ret)
136 return ret;
137
138 args->handle = handle;
139
140 return 0;
141}
142
Eric Anholt40123c12009-03-09 13:42:30 -0700143static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700144fast_shmem_read(struct page **pages,
145 loff_t page_base, int page_offset,
146 char __user *data,
147 int length)
148{
149 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200150 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700151
152 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
153 if (vaddr == NULL)
154 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200155 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700156 kunmap_atomic(vaddr, KM_USER0);
157
Florian Mickler2bc43b52009-04-06 22:55:41 +0200158 if (unwritten)
159 return -EFAULT;
160
161 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700162}
163
Eric Anholt280b7132009-03-12 16:56:27 -0700164static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
165{
166 drm_i915_private_t *dev_priv = obj->dev->dev_private;
167 struct drm_i915_gem_object *obj_priv = obj->driver_private;
168
169 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
170 obj_priv->tiling_mode != I915_TILING_NONE;
171}
172
Eric Anholteb014592009-03-10 11:44:52 -0700173static inline int
Eric Anholt40123c12009-03-09 13:42:30 -0700174slow_shmem_copy(struct page *dst_page,
175 int dst_offset,
176 struct page *src_page,
177 int src_offset,
178 int length)
179{
180 char *dst_vaddr, *src_vaddr;
181
182 dst_vaddr = kmap_atomic(dst_page, KM_USER0);
183 if (dst_vaddr == NULL)
184 return -ENOMEM;
185
186 src_vaddr = kmap_atomic(src_page, KM_USER1);
187 if (src_vaddr == NULL) {
188 kunmap_atomic(dst_vaddr, KM_USER0);
189 return -ENOMEM;
190 }
191
192 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
193
194 kunmap_atomic(src_vaddr, KM_USER1);
195 kunmap_atomic(dst_vaddr, KM_USER0);
196
197 return 0;
198}
199
Eric Anholt280b7132009-03-12 16:56:27 -0700200static inline int
201slow_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
220 gpu_vaddr = kmap_atomic(gpu_page, KM_USER0);
221 if (gpu_vaddr == NULL)
222 return -ENOMEM;
223
224 cpu_vaddr = kmap_atomic(cpu_page, KM_USER1);
225 if (cpu_vaddr == NULL) {
226 kunmap_atomic(gpu_vaddr, KM_USER0);
227 return -ENOMEM;
228 }
229
230 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
231 * XORing with the other bits (A9 for Y, A9 and A10 for X)
232 */
233 while (length > 0) {
234 int cacheline_end = ALIGN(gpu_offset + 1, 64);
235 int this_length = min(cacheline_end - gpu_offset, length);
236 int swizzled_gpu_offset = gpu_offset ^ 64;
237
238 if (is_read) {
239 memcpy(cpu_vaddr + cpu_offset,
240 gpu_vaddr + swizzled_gpu_offset,
241 this_length);
242 } else {
243 memcpy(gpu_vaddr + swizzled_gpu_offset,
244 cpu_vaddr + cpu_offset,
245 this_length);
246 }
247 cpu_offset += this_length;
248 gpu_offset += this_length;
249 length -= this_length;
250 }
251
252 kunmap_atomic(cpu_vaddr, KM_USER1);
253 kunmap_atomic(gpu_vaddr, KM_USER0);
254
255 return 0;
256}
257
Eric Anholt673a3942008-07-30 12:06:12 -0700258/**
Eric Anholteb014592009-03-10 11:44:52 -0700259 * This is the fast shmem pread path, which attempts to copy_from_user directly
260 * from the backing pages of the object to the user's address space. On a
261 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
262 */
263static int
264i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
265 struct drm_i915_gem_pread *args,
266 struct drm_file *file_priv)
267{
268 struct drm_i915_gem_object *obj_priv = obj->driver_private;
269 ssize_t remain;
270 loff_t offset, page_base;
271 char __user *user_data;
272 int page_offset, page_length;
273 int ret;
274
275 user_data = (char __user *) (uintptr_t) args->data_ptr;
276 remain = args->size;
277
278 mutex_lock(&dev->struct_mutex);
279
280 ret = i915_gem_object_get_pages(obj);
281 if (ret != 0)
282 goto fail_unlock;
283
284 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
285 args->size);
286 if (ret != 0)
287 goto fail_put_pages;
288
289 obj_priv = obj->driver_private;
290 offset = args->offset;
291
292 while (remain > 0) {
293 /* Operation in this page
294 *
295 * page_base = page offset within aperture
296 * page_offset = offset within page
297 * page_length = bytes to copy for this page
298 */
299 page_base = (offset & ~(PAGE_SIZE-1));
300 page_offset = offset & (PAGE_SIZE-1);
301 page_length = remain;
302 if ((page_offset + remain) > PAGE_SIZE)
303 page_length = PAGE_SIZE - page_offset;
304
305 ret = fast_shmem_read(obj_priv->pages,
306 page_base, page_offset,
307 user_data, page_length);
308 if (ret)
309 goto fail_put_pages;
310
311 remain -= page_length;
312 user_data += page_length;
313 offset += page_length;
314 }
315
316fail_put_pages:
317 i915_gem_object_put_pages(obj);
318fail_unlock:
319 mutex_unlock(&dev->struct_mutex);
320
321 return ret;
322}
323
Chris Wilson07f73f62009-09-14 16:50:30 +0100324static inline gfp_t
325i915_gem_object_get_page_gfp_mask (struct drm_gem_object *obj)
326{
327 return mapping_gfp_mask(obj->filp->f_path.dentry->d_inode->i_mapping);
328}
329
330static inline void
331i915_gem_object_set_page_gfp_mask (struct drm_gem_object *obj, gfp_t gfp)
332{
333 mapping_set_gfp_mask(obj->filp->f_path.dentry->d_inode->i_mapping, gfp);
334}
335
336static int
337i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
338{
339 int ret;
340
341 ret = i915_gem_object_get_pages(obj);
342
343 /* If we've insufficient memory to map in the pages, attempt
344 * to make some space by throwing out some old buffers.
345 */
346 if (ret == -ENOMEM) {
347 struct drm_device *dev = obj->dev;
348 gfp_t gfp;
349
350 ret = i915_gem_evict_something(dev, obj->size);
351 if (ret)
352 return ret;
353
354 gfp = i915_gem_object_get_page_gfp_mask(obj);
355 i915_gem_object_set_page_gfp_mask(obj, gfp & ~__GFP_NORETRY);
356 ret = i915_gem_object_get_pages(obj);
357 i915_gem_object_set_page_gfp_mask (obj, gfp);
358 }
359
360 return ret;
361}
362
Eric Anholteb014592009-03-10 11:44:52 -0700363/**
364 * This is the fallback shmem pread path, which allocates temporary storage
365 * in kernel space to copy_to_user into outside of the struct_mutex, so we
366 * can copy out of the object's backing pages while holding the struct mutex
367 * and not take page faults.
368 */
369static int
370i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
371 struct drm_i915_gem_pread *args,
372 struct drm_file *file_priv)
373{
374 struct drm_i915_gem_object *obj_priv = obj->driver_private;
375 struct mm_struct *mm = current->mm;
376 struct page **user_pages;
377 ssize_t remain;
378 loff_t offset, pinned_pages, i;
379 loff_t first_data_page, last_data_page, num_pages;
380 int shmem_page_index, shmem_page_offset;
381 int data_page_index, data_page_offset;
382 int page_length;
383 int ret;
384 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700385 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700386
387 remain = args->size;
388
389 /* Pin the user pages containing the data. We can't fault while
390 * holding the struct mutex, yet we want to hold it while
391 * dereferencing the user data.
392 */
393 first_data_page = data_ptr / PAGE_SIZE;
394 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
395 num_pages = last_data_page - first_data_page + 1;
396
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700397 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700398 if (user_pages == NULL)
399 return -ENOMEM;
400
401 down_read(&mm->mmap_sem);
402 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700403 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700404 up_read(&mm->mmap_sem);
405 if (pinned_pages < num_pages) {
406 ret = -EFAULT;
407 goto fail_put_user_pages;
408 }
409
Eric Anholt280b7132009-03-12 16:56:27 -0700410 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
411
Eric Anholteb014592009-03-10 11:44:52 -0700412 mutex_lock(&dev->struct_mutex);
413
Chris Wilson07f73f62009-09-14 16:50:30 +0100414 ret = i915_gem_object_get_pages_or_evict(obj);
415 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700416 goto fail_unlock;
417
418 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
419 args->size);
420 if (ret != 0)
421 goto fail_put_pages;
422
423 obj_priv = obj->driver_private;
424 offset = args->offset;
425
426 while (remain > 0) {
427 /* Operation in this page
428 *
429 * shmem_page_index = page number within shmem file
430 * shmem_page_offset = offset within page in shmem file
431 * data_page_index = page number in get_user_pages return
432 * data_page_offset = offset with data_page_index page.
433 * page_length = bytes to copy for this page
434 */
435 shmem_page_index = offset / PAGE_SIZE;
436 shmem_page_offset = offset & ~PAGE_MASK;
437 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
438 data_page_offset = data_ptr & ~PAGE_MASK;
439
440 page_length = remain;
441 if ((shmem_page_offset + page_length) > PAGE_SIZE)
442 page_length = PAGE_SIZE - shmem_page_offset;
443 if ((data_page_offset + page_length) > PAGE_SIZE)
444 page_length = PAGE_SIZE - data_page_offset;
445
Eric Anholt280b7132009-03-12 16:56:27 -0700446 if (do_bit17_swizzling) {
447 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
448 shmem_page_offset,
449 user_pages[data_page_index],
450 data_page_offset,
451 page_length,
452 1);
453 } else {
454 ret = slow_shmem_copy(user_pages[data_page_index],
455 data_page_offset,
456 obj_priv->pages[shmem_page_index],
457 shmem_page_offset,
458 page_length);
459 }
Eric Anholteb014592009-03-10 11:44:52 -0700460 if (ret)
461 goto fail_put_pages;
462
463 remain -= page_length;
464 data_ptr += page_length;
465 offset += page_length;
466 }
467
468fail_put_pages:
469 i915_gem_object_put_pages(obj);
470fail_unlock:
471 mutex_unlock(&dev->struct_mutex);
472fail_put_user_pages:
473 for (i = 0; i < pinned_pages; i++) {
474 SetPageDirty(user_pages[i]);
475 page_cache_release(user_pages[i]);
476 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700477 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700478
479 return ret;
480}
481
Eric Anholt673a3942008-07-30 12:06:12 -0700482/**
483 * Reads data from the object referenced by handle.
484 *
485 * On error, the contents of *data are undefined.
486 */
487int
488i915_gem_pread_ioctl(struct drm_device *dev, void *data,
489 struct drm_file *file_priv)
490{
491 struct drm_i915_gem_pread *args = data;
492 struct drm_gem_object *obj;
493 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700494 int ret;
495
496 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
497 if (obj == NULL)
498 return -EBADF;
499 obj_priv = obj->driver_private;
500
501 /* Bounds check source.
502 *
503 * XXX: This could use review for overflow issues...
504 */
505 if (args->offset > obj->size || args->size > obj->size ||
506 args->offset + args->size > obj->size) {
507 drm_gem_object_unreference(obj);
508 return -EINVAL;
509 }
510
Eric Anholt280b7132009-03-12 16:56:27 -0700511 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700512 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700513 } else {
514 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
515 if (ret != 0)
516 ret = i915_gem_shmem_pread_slow(dev, obj, args,
517 file_priv);
518 }
Eric Anholt673a3942008-07-30 12:06:12 -0700519
520 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700521
Eric Anholteb014592009-03-10 11:44:52 -0700522 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700523}
524
Keith Packard0839ccb2008-10-30 19:38:48 -0700525/* This is the fast write path which cannot handle
526 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700527 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700528
Keith Packard0839ccb2008-10-30 19:38:48 -0700529static inline int
530fast_user_write(struct io_mapping *mapping,
531 loff_t page_base, int page_offset,
532 char __user *user_data,
533 int length)
534{
535 char *vaddr_atomic;
536 unsigned long unwritten;
537
538 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
539 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
540 user_data, length);
541 io_mapping_unmap_atomic(vaddr_atomic);
542 if (unwritten)
543 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700544 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700545}
546
547/* Here's the write path which can sleep for
548 * page faults
549 */
550
551static inline int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700552slow_kernel_write(struct io_mapping *mapping,
553 loff_t gtt_base, int gtt_offset,
554 struct page *user_page, int user_offset,
555 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700556{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700557 char *src_vaddr, *dst_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700558 unsigned long unwritten;
559
Eric Anholt3de09aa2009-03-09 09:42:23 -0700560 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
561 src_vaddr = kmap_atomic(user_page, KM_USER1);
562 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
563 src_vaddr + user_offset,
564 length);
565 kunmap_atomic(src_vaddr, KM_USER1);
566 io_mapping_unmap_atomic(dst_vaddr);
Keith Packard0839ccb2008-10-30 19:38:48 -0700567 if (unwritten)
568 return -EFAULT;
569 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700570}
571
Eric Anholt40123c12009-03-09 13:42:30 -0700572static inline int
573fast_shmem_write(struct page **pages,
574 loff_t page_base, int page_offset,
575 char __user *data,
576 int length)
577{
578 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400579 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700580
581 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
582 if (vaddr == NULL)
583 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400584 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700585 kunmap_atomic(vaddr, KM_USER0);
586
Dave Airlied0088772009-03-28 20:29:48 -0400587 if (unwritten)
588 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700589 return 0;
590}
591
Eric Anholt3de09aa2009-03-09 09:42:23 -0700592/**
593 * This is the fast pwrite path, where we copy the data directly from the
594 * user into the GTT, uncached.
595 */
Eric Anholt673a3942008-07-30 12:06:12 -0700596static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700597i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
598 struct drm_i915_gem_pwrite *args,
599 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700600{
601 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Keith Packard0839ccb2008-10-30 19:38:48 -0700602 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700603 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700604 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700605 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700606 int page_offset, page_length;
607 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700608
609 user_data = (char __user *) (uintptr_t) args->data_ptr;
610 remain = args->size;
611 if (!access_ok(VERIFY_READ, user_data, remain))
612 return -EFAULT;
613
614
615 mutex_lock(&dev->struct_mutex);
616 ret = i915_gem_object_pin(obj, 0);
617 if (ret) {
618 mutex_unlock(&dev->struct_mutex);
619 return ret;
620 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800621 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700622 if (ret)
623 goto fail;
624
625 obj_priv = obj->driver_private;
626 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700627
628 while (remain > 0) {
629 /* Operation in this page
630 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700631 * page_base = page offset within aperture
632 * page_offset = offset within page
633 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700634 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700635 page_base = (offset & ~(PAGE_SIZE-1));
636 page_offset = offset & (PAGE_SIZE-1);
637 page_length = remain;
638 if ((page_offset + remain) > PAGE_SIZE)
639 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700640
Keith Packard0839ccb2008-10-30 19:38:48 -0700641 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
642 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700643
Keith Packard0839ccb2008-10-30 19:38:48 -0700644 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700645 * source page isn't available. Return the error and we'll
646 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700647 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700648 if (ret)
649 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700650
Keith Packard0839ccb2008-10-30 19:38:48 -0700651 remain -= page_length;
652 user_data += page_length;
653 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700654 }
Eric Anholt673a3942008-07-30 12:06:12 -0700655
656fail:
657 i915_gem_object_unpin(obj);
658 mutex_unlock(&dev->struct_mutex);
659
660 return ret;
661}
662
Eric Anholt3de09aa2009-03-09 09:42:23 -0700663/**
664 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
665 * the memory and maps it using kmap_atomic for copying.
666 *
667 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
668 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
669 */
Eric Anholt3043c602008-10-02 12:24:47 -0700670static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700671i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
672 struct drm_i915_gem_pwrite *args,
673 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700674{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700675 struct drm_i915_gem_object *obj_priv = obj->driver_private;
676 drm_i915_private_t *dev_priv = dev->dev_private;
677 ssize_t remain;
678 loff_t gtt_page_base, offset;
679 loff_t first_data_page, last_data_page, num_pages;
680 loff_t pinned_pages, i;
681 struct page **user_pages;
682 struct mm_struct *mm = current->mm;
683 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700684 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700685 uint64_t data_ptr = args->data_ptr;
686
687 remain = args->size;
688
689 /* Pin the user pages containing the data. We can't fault while
690 * holding the struct mutex, and all of the pwrite implementations
691 * want to hold it while dereferencing the user data.
692 */
693 first_data_page = data_ptr / PAGE_SIZE;
694 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
695 num_pages = last_data_page - first_data_page + 1;
696
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700697 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700698 if (user_pages == NULL)
699 return -ENOMEM;
700
701 down_read(&mm->mmap_sem);
702 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
703 num_pages, 0, 0, user_pages, NULL);
704 up_read(&mm->mmap_sem);
705 if (pinned_pages < num_pages) {
706 ret = -EFAULT;
707 goto out_unpin_pages;
708 }
709
710 mutex_lock(&dev->struct_mutex);
711 ret = i915_gem_object_pin(obj, 0);
712 if (ret)
713 goto out_unlock;
714
715 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
716 if (ret)
717 goto out_unpin_object;
718
719 obj_priv = obj->driver_private;
720 offset = obj_priv->gtt_offset + args->offset;
721
722 while (remain > 0) {
723 /* Operation in this page
724 *
725 * gtt_page_base = page offset within aperture
726 * gtt_page_offset = offset within page in aperture
727 * data_page_index = page number in get_user_pages return
728 * data_page_offset = offset with data_page_index page.
729 * page_length = bytes to copy for this page
730 */
731 gtt_page_base = offset & PAGE_MASK;
732 gtt_page_offset = offset & ~PAGE_MASK;
733 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
734 data_page_offset = data_ptr & ~PAGE_MASK;
735
736 page_length = remain;
737 if ((gtt_page_offset + page_length) > PAGE_SIZE)
738 page_length = PAGE_SIZE - gtt_page_offset;
739 if ((data_page_offset + page_length) > PAGE_SIZE)
740 page_length = PAGE_SIZE - data_page_offset;
741
742 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
743 gtt_page_base, gtt_page_offset,
744 user_pages[data_page_index],
745 data_page_offset,
746 page_length);
747
748 /* If we get a fault while copying data, then (presumably) our
749 * source page isn't available. Return the error and we'll
750 * retry in the slow path.
751 */
752 if (ret)
753 goto out_unpin_object;
754
755 remain -= page_length;
756 offset += page_length;
757 data_ptr += page_length;
758 }
759
760out_unpin_object:
761 i915_gem_object_unpin(obj);
762out_unlock:
763 mutex_unlock(&dev->struct_mutex);
764out_unpin_pages:
765 for (i = 0; i < pinned_pages; i++)
766 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700767 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700768
769 return ret;
770}
771
Eric Anholt40123c12009-03-09 13:42:30 -0700772/**
773 * This is the fast shmem pwrite path, which attempts to directly
774 * copy_from_user into the kmapped pages backing the object.
775 */
Eric Anholt673a3942008-07-30 12:06:12 -0700776static int
Eric Anholt40123c12009-03-09 13:42:30 -0700777i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
778 struct drm_i915_gem_pwrite *args,
779 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700780{
Eric Anholt40123c12009-03-09 13:42:30 -0700781 struct drm_i915_gem_object *obj_priv = obj->driver_private;
782 ssize_t remain;
783 loff_t offset, page_base;
784 char __user *user_data;
785 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700786 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700787
788 user_data = (char __user *) (uintptr_t) args->data_ptr;
789 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700790
791 mutex_lock(&dev->struct_mutex);
792
Eric Anholt40123c12009-03-09 13:42:30 -0700793 ret = i915_gem_object_get_pages(obj);
794 if (ret != 0)
795 goto fail_unlock;
796
Eric Anholte47c68e2008-11-14 13:35:19 -0800797 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700798 if (ret != 0)
799 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700800
Eric Anholt40123c12009-03-09 13:42:30 -0700801 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700802 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700803 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700804
Eric Anholt40123c12009-03-09 13:42:30 -0700805 while (remain > 0) {
806 /* Operation in this page
807 *
808 * page_base = page offset within aperture
809 * page_offset = offset within page
810 * page_length = bytes to copy for this page
811 */
812 page_base = (offset & ~(PAGE_SIZE-1));
813 page_offset = offset & (PAGE_SIZE-1);
814 page_length = remain;
815 if ((page_offset + remain) > PAGE_SIZE)
816 page_length = PAGE_SIZE - page_offset;
817
818 ret = fast_shmem_write(obj_priv->pages,
819 page_base, page_offset,
820 user_data, page_length);
821 if (ret)
822 goto fail_put_pages;
823
824 remain -= page_length;
825 user_data += page_length;
826 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700827 }
828
Eric Anholt40123c12009-03-09 13:42:30 -0700829fail_put_pages:
830 i915_gem_object_put_pages(obj);
831fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700832 mutex_unlock(&dev->struct_mutex);
833
Eric Anholt40123c12009-03-09 13:42:30 -0700834 return ret;
835}
836
837/**
838 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
839 * the memory and maps it using kmap_atomic for copying.
840 *
841 * This avoids taking mmap_sem for faulting on the user's address while the
842 * struct_mutex is held.
843 */
844static int
845i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
846 struct drm_i915_gem_pwrite *args,
847 struct drm_file *file_priv)
848{
849 struct drm_i915_gem_object *obj_priv = obj->driver_private;
850 struct mm_struct *mm = current->mm;
851 struct page **user_pages;
852 ssize_t remain;
853 loff_t offset, pinned_pages, i;
854 loff_t first_data_page, last_data_page, num_pages;
855 int shmem_page_index, shmem_page_offset;
856 int data_page_index, data_page_offset;
857 int page_length;
858 int ret;
859 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700860 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700861
862 remain = args->size;
863
864 /* Pin the user pages containing the data. We can't fault while
865 * holding the struct mutex, and all of the pwrite implementations
866 * want to hold it while dereferencing the user data.
867 */
868 first_data_page = data_ptr / PAGE_SIZE;
869 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
870 num_pages = last_data_page - first_data_page + 1;
871
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700872 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700873 if (user_pages == NULL)
874 return -ENOMEM;
875
876 down_read(&mm->mmap_sem);
877 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
878 num_pages, 0, 0, user_pages, NULL);
879 up_read(&mm->mmap_sem);
880 if (pinned_pages < num_pages) {
881 ret = -EFAULT;
882 goto fail_put_user_pages;
883 }
884
Eric Anholt280b7132009-03-12 16:56:27 -0700885 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
886
Eric Anholt40123c12009-03-09 13:42:30 -0700887 mutex_lock(&dev->struct_mutex);
888
Chris Wilson07f73f62009-09-14 16:50:30 +0100889 ret = i915_gem_object_get_pages_or_evict(obj);
890 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700891 goto fail_unlock;
892
893 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
894 if (ret != 0)
895 goto fail_put_pages;
896
897 obj_priv = obj->driver_private;
898 offset = args->offset;
899 obj_priv->dirty = 1;
900
901 while (remain > 0) {
902 /* Operation in this page
903 *
904 * shmem_page_index = page number within shmem file
905 * shmem_page_offset = offset within page in shmem file
906 * data_page_index = page number in get_user_pages return
907 * data_page_offset = offset with data_page_index page.
908 * page_length = bytes to copy for this page
909 */
910 shmem_page_index = offset / PAGE_SIZE;
911 shmem_page_offset = offset & ~PAGE_MASK;
912 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
913 data_page_offset = data_ptr & ~PAGE_MASK;
914
915 page_length = remain;
916 if ((shmem_page_offset + page_length) > PAGE_SIZE)
917 page_length = PAGE_SIZE - shmem_page_offset;
918 if ((data_page_offset + page_length) > PAGE_SIZE)
919 page_length = PAGE_SIZE - data_page_offset;
920
Eric Anholt280b7132009-03-12 16:56:27 -0700921 if (do_bit17_swizzling) {
922 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
923 shmem_page_offset,
924 user_pages[data_page_index],
925 data_page_offset,
926 page_length,
927 0);
928 } else {
929 ret = slow_shmem_copy(obj_priv->pages[shmem_page_index],
930 shmem_page_offset,
931 user_pages[data_page_index],
932 data_page_offset,
933 page_length);
934 }
Eric Anholt40123c12009-03-09 13:42:30 -0700935 if (ret)
936 goto fail_put_pages;
937
938 remain -= page_length;
939 data_ptr += page_length;
940 offset += page_length;
941 }
942
943fail_put_pages:
944 i915_gem_object_put_pages(obj);
945fail_unlock:
946 mutex_unlock(&dev->struct_mutex);
947fail_put_user_pages:
948 for (i = 0; i < pinned_pages; i++)
949 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700950 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700951
952 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700953}
954
955/**
956 * Writes data to the object referenced by handle.
957 *
958 * On error, the contents of the buffer that were to be modified are undefined.
959 */
960int
961i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
962 struct drm_file *file_priv)
963{
964 struct drm_i915_gem_pwrite *args = data;
965 struct drm_gem_object *obj;
966 struct drm_i915_gem_object *obj_priv;
967 int ret = 0;
968
969 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
970 if (obj == NULL)
971 return -EBADF;
972 obj_priv = obj->driver_private;
973
974 /* Bounds check destination.
975 *
976 * XXX: This could use review for overflow issues...
977 */
978 if (args->offset > obj->size || args->size > obj->size ||
979 args->offset + args->size > obj->size) {
980 drm_gem_object_unreference(obj);
981 return -EINVAL;
982 }
983
984 /* We can only do the GTT pwrite on untiled buffers, as otherwise
985 * it would end up going through the fenced access, and we'll get
986 * different detiling behavior between reading and writing.
987 * pread/pwrite currently are reading and writing from the CPU
988 * perspective, requiring manual detiling by the client.
989 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000990 if (obj_priv->phys_obj)
991 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
992 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Eric Anholt3de09aa2009-03-09 09:42:23 -0700993 dev->gtt_total != 0) {
994 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
995 if (ret == -EFAULT) {
996 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
997 file_priv);
998 }
Eric Anholt280b7132009-03-12 16:56:27 -0700999 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
1000 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -07001001 } else {
1002 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
1003 if (ret == -EFAULT) {
1004 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
1005 file_priv);
1006 }
1007 }
Eric Anholt673a3942008-07-30 12:06:12 -07001008
1009#if WATCH_PWRITE
1010 if (ret)
1011 DRM_INFO("pwrite failed %d\n", ret);
1012#endif
1013
1014 drm_gem_object_unreference(obj);
1015
1016 return ret;
1017}
1018
1019/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001020 * Called when user space prepares to use an object with the CPU, either
1021 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001022 */
1023int
1024i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1025 struct drm_file *file_priv)
1026{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001027 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001028 struct drm_i915_gem_set_domain *args = data;
1029 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001030 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001031 uint32_t read_domains = args->read_domains;
1032 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001033 int ret;
1034
1035 if (!(dev->driver->driver_features & DRIVER_GEM))
1036 return -ENODEV;
1037
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001038 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001039 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001040 return -EINVAL;
1041
Chris Wilson21d509e2009-06-06 09:46:02 +01001042 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001043 return -EINVAL;
1044
1045 /* Having something in the write domain implies it's in the read
1046 * domain, and only that read domain. Enforce that in the request.
1047 */
1048 if (write_domain != 0 && read_domains != write_domain)
1049 return -EINVAL;
1050
Eric Anholt673a3942008-07-30 12:06:12 -07001051 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1052 if (obj == NULL)
1053 return -EBADF;
Jesse Barnes652c3932009-08-17 13:31:43 -07001054 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001055
1056 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001057
1058 intel_mark_busy(dev, obj);
1059
Eric Anholt673a3942008-07-30 12:06:12 -07001060#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001061 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001062 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001063#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001064 if (read_domains & I915_GEM_DOMAIN_GTT) {
1065 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001066
Eric Anholta09ba7f2009-08-29 12:49:51 -07001067 /* Update the LRU on the fence for the CPU access that's
1068 * about to occur.
1069 */
1070 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1071 list_move_tail(&obj_priv->fence_list,
1072 &dev_priv->mm.fence_list);
1073 }
1074
Eric Anholt02354392008-11-26 13:58:13 -08001075 /* Silently promote "you're not bound, there was nothing to do"
1076 * to success, since the client was just asking us to
1077 * make sure everything was done.
1078 */
1079 if (ret == -EINVAL)
1080 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001081 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001082 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001083 }
1084
Eric Anholt673a3942008-07-30 12:06:12 -07001085 drm_gem_object_unreference(obj);
1086 mutex_unlock(&dev->struct_mutex);
1087 return ret;
1088}
1089
1090/**
1091 * Called when user space has done writes to this buffer
1092 */
1093int
1094i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1095 struct drm_file *file_priv)
1096{
1097 struct drm_i915_gem_sw_finish *args = data;
1098 struct drm_gem_object *obj;
1099 struct drm_i915_gem_object *obj_priv;
1100 int ret = 0;
1101
1102 if (!(dev->driver->driver_features & DRIVER_GEM))
1103 return -ENODEV;
1104
1105 mutex_lock(&dev->struct_mutex);
1106 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1107 if (obj == NULL) {
1108 mutex_unlock(&dev->struct_mutex);
1109 return -EBADF;
1110 }
1111
1112#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001113 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001114 __func__, args->handle, obj, obj->size);
1115#endif
1116 obj_priv = obj->driver_private;
1117
1118 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001119 if (obj_priv->pin_count)
1120 i915_gem_object_flush_cpu_write_domain(obj);
1121
Eric Anholt673a3942008-07-30 12:06:12 -07001122 drm_gem_object_unreference(obj);
1123 mutex_unlock(&dev->struct_mutex);
1124 return ret;
1125}
1126
1127/**
1128 * Maps the contents of an object, returning the address it is mapped
1129 * into.
1130 *
1131 * While the mapping holds a reference on the contents of the object, it doesn't
1132 * imply a ref on the object itself.
1133 */
1134int
1135i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1136 struct drm_file *file_priv)
1137{
1138 struct drm_i915_gem_mmap *args = data;
1139 struct drm_gem_object *obj;
1140 loff_t offset;
1141 unsigned long addr;
1142
1143 if (!(dev->driver->driver_features & DRIVER_GEM))
1144 return -ENODEV;
1145
1146 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1147 if (obj == NULL)
1148 return -EBADF;
1149
1150 offset = args->offset;
1151
1152 down_write(&current->mm->mmap_sem);
1153 addr = do_mmap(obj->filp, 0, args->size,
1154 PROT_READ | PROT_WRITE, MAP_SHARED,
1155 args->offset);
1156 up_write(&current->mm->mmap_sem);
1157 mutex_lock(&dev->struct_mutex);
1158 drm_gem_object_unreference(obj);
1159 mutex_unlock(&dev->struct_mutex);
1160 if (IS_ERR((void *)addr))
1161 return addr;
1162
1163 args->addr_ptr = (uint64_t) addr;
1164
1165 return 0;
1166}
1167
Jesse Barnesde151cf2008-11-12 10:03:55 -08001168/**
1169 * i915_gem_fault - fault a page into the GTT
1170 * vma: VMA in question
1171 * vmf: fault info
1172 *
1173 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1174 * from userspace. The fault handler takes care of binding the object to
1175 * the GTT (if needed), allocating and programming a fence register (again,
1176 * only if needed based on whether the old reg is still valid or the object
1177 * is tiled) and inserting a new PTE into the faulting process.
1178 *
1179 * Note that the faulting process may involve evicting existing objects
1180 * from the GTT and/or fence registers to make room. So performance may
1181 * suffer if the GTT working set is large or there are few fence registers
1182 * left.
1183 */
1184int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1185{
1186 struct drm_gem_object *obj = vma->vm_private_data;
1187 struct drm_device *dev = obj->dev;
1188 struct drm_i915_private *dev_priv = dev->dev_private;
1189 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1190 pgoff_t page_offset;
1191 unsigned long pfn;
1192 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001193 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001194
1195 /* We don't use vmf->pgoff since that has the fake offset */
1196 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1197 PAGE_SHIFT;
1198
1199 /* Now bind it into the GTT if needed */
1200 mutex_lock(&dev->struct_mutex);
1201 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001202 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001203 if (ret) {
1204 mutex_unlock(&dev->struct_mutex);
1205 return VM_FAULT_SIGBUS;
1206 }
Chris Wilson4960aac2009-09-14 16:50:25 +01001207 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001208
1209 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1210 if (ret) {
1211 mutex_unlock(&dev->struct_mutex);
1212 return VM_FAULT_SIGBUS;
1213 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001214 }
1215
1216 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001217 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001218 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001219 if (ret) {
1220 mutex_unlock(&dev->struct_mutex);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001221 return VM_FAULT_SIGBUS;
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001222 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001223 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001224
1225 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1226 page_offset;
1227
1228 /* Finally, remap it using the new GTT offset */
1229 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1230
1231 mutex_unlock(&dev->struct_mutex);
1232
1233 switch (ret) {
1234 case -ENOMEM:
1235 case -EAGAIN:
1236 return VM_FAULT_OOM;
1237 case -EFAULT:
Jesse Barnes959b8872009-03-20 14:16:33 -07001238 case -EINVAL:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001239 return VM_FAULT_SIGBUS;
1240 default:
1241 return VM_FAULT_NOPAGE;
1242 }
1243}
1244
1245/**
1246 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1247 * @obj: obj in question
1248 *
1249 * GEM memory mapping works by handing back to userspace a fake mmap offset
1250 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1251 * up the object based on the offset and sets up the various memory mapping
1252 * structures.
1253 *
1254 * This routine allocates and attaches a fake offset for @obj.
1255 */
1256static int
1257i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1258{
1259 struct drm_device *dev = obj->dev;
1260 struct drm_gem_mm *mm = dev->mm_private;
1261 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1262 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001263 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001264 int ret = 0;
1265
1266 /* Set the object up for mmap'ing */
1267 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001268 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001269 if (!list->map)
1270 return -ENOMEM;
1271
1272 map = list->map;
1273 map->type = _DRM_GEM;
1274 map->size = obj->size;
1275 map->handle = obj;
1276
1277 /* Get a DRM GEM mmap offset allocated... */
1278 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1279 obj->size / PAGE_SIZE, 0, 0);
1280 if (!list->file_offset_node) {
1281 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1282 ret = -ENOMEM;
1283 goto out_free_list;
1284 }
1285
1286 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1287 obj->size / PAGE_SIZE, 0);
1288 if (!list->file_offset_node) {
1289 ret = -ENOMEM;
1290 goto out_free_list;
1291 }
1292
1293 list->hash.key = list->file_offset_node->start;
1294 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1295 DRM_ERROR("failed to add to map hash\n");
1296 goto out_free_mm;
1297 }
1298
1299 /* By now we should be all set, any drm_mmap request on the offset
1300 * below will get to our mmap & fault handler */
1301 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1302
1303 return 0;
1304
1305out_free_mm:
1306 drm_mm_put_block(list->file_offset_node);
1307out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001308 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001309
1310 return ret;
1311}
1312
Chris Wilson901782b2009-07-10 08:18:50 +01001313/**
1314 * i915_gem_release_mmap - remove physical page mappings
1315 * @obj: obj in question
1316 *
1317 * Preserve the reservation of the mmaping with the DRM core code, but
1318 * relinquish ownership of the pages back to the system.
1319 *
1320 * It is vital that we remove the page mapping if we have mapped a tiled
1321 * object through the GTT and then lose the fence register due to
1322 * resource pressure. Similarly if the object has been moved out of the
1323 * aperture, than pages mapped into userspace must be revoked. Removing the
1324 * mapping will then trigger a page fault on the next user access, allowing
1325 * fixup by i915_gem_fault().
1326 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001327void
Chris Wilson901782b2009-07-10 08:18:50 +01001328i915_gem_release_mmap(struct drm_gem_object *obj)
1329{
1330 struct drm_device *dev = obj->dev;
1331 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1332
1333 if (dev->dev_mapping)
1334 unmap_mapping_range(dev->dev_mapping,
1335 obj_priv->mmap_offset, obj->size, 1);
1336}
1337
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001338static void
1339i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1340{
1341 struct drm_device *dev = obj->dev;
1342 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1343 struct drm_gem_mm *mm = dev->mm_private;
1344 struct drm_map_list *list;
1345
1346 list = &obj->map_list;
1347 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1348
1349 if (list->file_offset_node) {
1350 drm_mm_put_block(list->file_offset_node);
1351 list->file_offset_node = NULL;
1352 }
1353
1354 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001355 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001356 list->map = NULL;
1357 }
1358
1359 obj_priv->mmap_offset = 0;
1360}
1361
Jesse Barnesde151cf2008-11-12 10:03:55 -08001362/**
1363 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1364 * @obj: object to check
1365 *
1366 * Return the required GTT alignment for an object, taking into account
1367 * potential fence register mapping if needed.
1368 */
1369static uint32_t
1370i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1371{
1372 struct drm_device *dev = obj->dev;
1373 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1374 int start, i;
1375
1376 /*
1377 * Minimum alignment is 4k (GTT page size), but might be greater
1378 * if a fence register is needed for the object.
1379 */
1380 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1381 return 4096;
1382
1383 /*
1384 * Previous chips need to be aligned to the size of the smallest
1385 * fence register that can contain the object.
1386 */
1387 if (IS_I9XX(dev))
1388 start = 1024*1024;
1389 else
1390 start = 512*1024;
1391
1392 for (i = start; i < obj->size; i <<= 1)
1393 ;
1394
1395 return i;
1396}
1397
1398/**
1399 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1400 * @dev: DRM device
1401 * @data: GTT mapping ioctl data
1402 * @file_priv: GEM object info
1403 *
1404 * Simply returns the fake offset to userspace so it can mmap it.
1405 * The mmap call will end up in drm_gem_mmap(), which will set things
1406 * up so we can get faults in the handler above.
1407 *
1408 * The fault handler will take care of binding the object into the GTT
1409 * (since it may have been evicted to make room for something), allocating
1410 * a fence register, and mapping the appropriate aperture address into
1411 * userspace.
1412 */
1413int
1414i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1415 struct drm_file *file_priv)
1416{
1417 struct drm_i915_gem_mmap_gtt *args = data;
1418 struct drm_i915_private *dev_priv = dev->dev_private;
1419 struct drm_gem_object *obj;
1420 struct drm_i915_gem_object *obj_priv;
1421 int ret;
1422
1423 if (!(dev->driver->driver_features & DRIVER_GEM))
1424 return -ENODEV;
1425
1426 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1427 if (obj == NULL)
1428 return -EBADF;
1429
1430 mutex_lock(&dev->struct_mutex);
1431
1432 obj_priv = obj->driver_private;
1433
1434 if (!obj_priv->mmap_offset) {
1435 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001436 if (ret) {
1437 drm_gem_object_unreference(obj);
1438 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001439 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001440 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001441 }
1442
1443 args->offset = obj_priv->mmap_offset;
1444
Jesse Barnesde151cf2008-11-12 10:03:55 -08001445 /*
1446 * Pull it into the GTT so that we have a page list (makes the
1447 * initial fault faster and any subsequent flushing possible).
1448 */
1449 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001450 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001451 if (ret) {
1452 drm_gem_object_unreference(obj);
1453 mutex_unlock(&dev->struct_mutex);
1454 return ret;
1455 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001456 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001457 }
1458
1459 drm_gem_object_unreference(obj);
1460 mutex_unlock(&dev->struct_mutex);
1461
1462 return 0;
1463}
1464
Ben Gamari6911a9b2009-04-02 11:24:54 -07001465void
Eric Anholt856fa192009-03-19 14:10:50 -07001466i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001467{
1468 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1469 int page_count = obj->size / PAGE_SIZE;
1470 int i;
1471
Eric Anholt856fa192009-03-19 14:10:50 -07001472 BUG_ON(obj_priv->pages_refcount == 0);
1473
1474 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001475 return;
1476
Eric Anholt280b7132009-03-12 16:56:27 -07001477 if (obj_priv->tiling_mode != I915_TILING_NONE)
1478 i915_gem_object_save_bit_17_swizzle(obj);
1479
Chris Wilson3ef94da2009-09-14 16:50:29 +01001480 if (obj_priv->madv == I915_MADV_DONTNEED)
1481 obj_priv->dirty = 0;
1482
1483 for (i = 0; i < page_count; i++) {
1484 if (obj_priv->pages[i] == NULL)
1485 break;
1486
1487 if (obj_priv->dirty)
1488 set_page_dirty(obj_priv->pages[i]);
1489
1490 if (obj_priv->madv == I915_MADV_WILLNEED)
1491 mark_page_accessed(obj_priv->pages[i]);
1492
1493 page_cache_release(obj_priv->pages[i]);
1494 }
Eric Anholt673a3942008-07-30 12:06:12 -07001495 obj_priv->dirty = 0;
1496
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001497 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001498 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001499}
1500
1501static void
Eric Anholtce44b0e2008-11-06 16:00:31 -08001502i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001503{
1504 struct drm_device *dev = obj->dev;
1505 drm_i915_private_t *dev_priv = dev->dev_private;
1506 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1507
1508 /* Add a reference if we're newly entering the active list. */
1509 if (!obj_priv->active) {
1510 drm_gem_object_reference(obj);
1511 obj_priv->active = 1;
1512 }
1513 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001514 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001515 list_move_tail(&obj_priv->list,
1516 &dev_priv->mm.active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001517 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001518 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001519}
1520
Eric Anholtce44b0e2008-11-06 16:00:31 -08001521static void
1522i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1523{
1524 struct drm_device *dev = obj->dev;
1525 drm_i915_private_t *dev_priv = dev->dev_private;
1526 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1527
1528 BUG_ON(!obj_priv->active);
1529 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1530 obj_priv->last_rendering_seqno = 0;
1531}
Eric Anholt673a3942008-07-30 12:06:12 -07001532
1533static void
1534i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1535{
1536 struct drm_device *dev = obj->dev;
1537 drm_i915_private_t *dev_priv = dev->dev_private;
1538 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1539
1540 i915_verify_inactive(dev, __FILE__, __LINE__);
1541 if (obj_priv->pin_count != 0)
1542 list_del_init(&obj_priv->list);
1543 else
1544 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1545
Eric Anholtce44b0e2008-11-06 16:00:31 -08001546 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001547 if (obj_priv->active) {
1548 obj_priv->active = 0;
1549 drm_gem_object_unreference(obj);
1550 }
1551 i915_verify_inactive(dev, __FILE__, __LINE__);
1552}
1553
1554/**
1555 * Creates a new sequence number, emitting a write of it to the status page
1556 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1557 *
1558 * Must be called with struct_lock held.
1559 *
1560 * Returned sequence numbers are nonzero on success.
1561 */
1562static uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001563i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
1564 uint32_t flush_domains)
Eric Anholt673a3942008-07-30 12:06:12 -07001565{
1566 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001567 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001568 struct drm_i915_gem_request *request;
1569 uint32_t seqno;
1570 int was_empty;
1571 RING_LOCALS;
1572
Eric Anholtb9624422009-06-03 07:27:35 +00001573 if (file_priv != NULL)
1574 i915_file_priv = file_priv->driver_priv;
1575
Eric Anholt9a298b22009-03-24 12:23:04 -07001576 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001577 if (request == NULL)
1578 return 0;
1579
1580 /* Grab the seqno we're going to make this request be, and bump the
1581 * next (skipping 0 so it can be the reserved no-seqno value).
1582 */
1583 seqno = dev_priv->mm.next_gem_seqno;
1584 dev_priv->mm.next_gem_seqno++;
1585 if (dev_priv->mm.next_gem_seqno == 0)
1586 dev_priv->mm.next_gem_seqno++;
1587
1588 BEGIN_LP_RING(4);
1589 OUT_RING(MI_STORE_DWORD_INDEX);
1590 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1591 OUT_RING(seqno);
1592
1593 OUT_RING(MI_USER_INTERRUPT);
1594 ADVANCE_LP_RING();
1595
1596 DRM_DEBUG("%d\n", seqno);
1597
1598 request->seqno = seqno;
1599 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -07001600 was_empty = list_empty(&dev_priv->mm.request_list);
1601 list_add_tail(&request->list, &dev_priv->mm.request_list);
Eric Anholtb9624422009-06-03 07:27:35 +00001602 if (i915_file_priv) {
1603 list_add_tail(&request->client_list,
1604 &i915_file_priv->mm.request_list);
1605 } else {
1606 INIT_LIST_HEAD(&request->client_list);
1607 }
Eric Anholt673a3942008-07-30 12:06:12 -07001608
Eric Anholtce44b0e2008-11-06 16:00:31 -08001609 /* Associate any objects on the flushing list matching the write
1610 * domain we're flushing with our flush.
1611 */
1612 if (flush_domains != 0) {
1613 struct drm_i915_gem_object *obj_priv, *next;
1614
1615 list_for_each_entry_safe(obj_priv, next,
1616 &dev_priv->mm.flushing_list, list) {
1617 struct drm_gem_object *obj = obj_priv->obj;
1618
1619 if ((obj->write_domain & flush_domains) ==
1620 obj->write_domain) {
1621 obj->write_domain = 0;
1622 i915_gem_object_move_to_active(obj, seqno);
1623 }
1624 }
1625
1626 }
1627
Ben Gamarif65d9422009-09-14 17:48:44 -04001628 if (!dev_priv->mm.suspended) {
1629 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1630 if (was_empty)
1631 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1632 }
Eric Anholt673a3942008-07-30 12:06:12 -07001633 return seqno;
1634}
1635
1636/**
1637 * Command execution barrier
1638 *
1639 * Ensures that all commands in the ring are finished
1640 * before signalling the CPU
1641 */
Eric Anholt3043c602008-10-02 12:24:47 -07001642static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -07001643i915_retire_commands(struct drm_device *dev)
1644{
1645 drm_i915_private_t *dev_priv = dev->dev_private;
1646 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1647 uint32_t flush_domains = 0;
1648 RING_LOCALS;
1649
1650 /* The sampler always gets flushed on i965 (sigh) */
1651 if (IS_I965G(dev))
1652 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1653 BEGIN_LP_RING(2);
1654 OUT_RING(cmd);
1655 OUT_RING(0); /* noop */
1656 ADVANCE_LP_RING();
1657 return flush_domains;
1658}
1659
1660/**
1661 * Moves buffers associated only with the given active seqno from the active
1662 * to inactive list, potentially freeing them.
1663 */
1664static void
1665i915_gem_retire_request(struct drm_device *dev,
1666 struct drm_i915_gem_request *request)
1667{
1668 drm_i915_private_t *dev_priv = dev->dev_private;
1669
1670 /* Move any buffers on the active list that are no longer referenced
1671 * by the ringbuffer to the flushing/inactive lists as appropriate.
1672 */
Carl Worth5e118f42009-03-20 11:54:25 -07001673 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001674 while (!list_empty(&dev_priv->mm.active_list)) {
1675 struct drm_gem_object *obj;
1676 struct drm_i915_gem_object *obj_priv;
1677
1678 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1679 struct drm_i915_gem_object,
1680 list);
1681 obj = obj_priv->obj;
1682
1683 /* If the seqno being retired doesn't match the oldest in the
1684 * list, then the oldest in the list must still be newer than
1685 * this seqno.
1686 */
1687 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001688 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001689
Eric Anholt673a3942008-07-30 12:06:12 -07001690#if WATCH_LRU
1691 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1692 __func__, request->seqno, obj);
1693#endif
1694
Eric Anholtce44b0e2008-11-06 16:00:31 -08001695 if (obj->write_domain != 0)
1696 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001697 else {
1698 /* Take a reference on the object so it won't be
1699 * freed while the spinlock is held. The list
1700 * protection for this spinlock is safe when breaking
1701 * the lock like this since the next thing we do
1702 * is just get the head of the list again.
1703 */
1704 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001705 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001706 spin_unlock(&dev_priv->mm.active_list_lock);
1707 drm_gem_object_unreference(obj);
1708 spin_lock(&dev_priv->mm.active_list_lock);
1709 }
Eric Anholt673a3942008-07-30 12:06:12 -07001710 }
Carl Worth5e118f42009-03-20 11:54:25 -07001711out:
1712 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001713}
1714
1715/**
1716 * Returns true if seq1 is later than seq2.
1717 */
Ben Gamari22be1722009-09-14 17:48:43 -04001718bool
Eric Anholt673a3942008-07-30 12:06:12 -07001719i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1720{
1721 return (int32_t)(seq1 - seq2) >= 0;
1722}
1723
1724uint32_t
1725i915_get_gem_seqno(struct drm_device *dev)
1726{
1727 drm_i915_private_t *dev_priv = dev->dev_private;
1728
1729 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1730}
1731
1732/**
1733 * This function clears the request list as sequence numbers are passed.
1734 */
1735void
1736i915_gem_retire_requests(struct drm_device *dev)
1737{
1738 drm_i915_private_t *dev_priv = dev->dev_private;
1739 uint32_t seqno;
1740
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001741 if (!dev_priv->hw_status_page)
1742 return;
1743
Eric Anholt673a3942008-07-30 12:06:12 -07001744 seqno = i915_get_gem_seqno(dev);
1745
1746 while (!list_empty(&dev_priv->mm.request_list)) {
1747 struct drm_i915_gem_request *request;
1748 uint32_t retiring_seqno;
1749
1750 request = list_first_entry(&dev_priv->mm.request_list,
1751 struct drm_i915_gem_request,
1752 list);
1753 retiring_seqno = request->seqno;
1754
1755 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001756 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001757 i915_gem_retire_request(dev, request);
1758
1759 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001760 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001761 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001762 } else
1763 break;
1764 }
1765}
1766
1767void
1768i915_gem_retire_work_handler(struct work_struct *work)
1769{
1770 drm_i915_private_t *dev_priv;
1771 struct drm_device *dev;
1772
1773 dev_priv = container_of(work, drm_i915_private_t,
1774 mm.retire_work.work);
1775 dev = dev_priv->dev;
1776
1777 mutex_lock(&dev->struct_mutex);
1778 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001779 if (!dev_priv->mm.suspended &&
1780 !list_empty(&dev_priv->mm.request_list))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001781 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001782 mutex_unlock(&dev->struct_mutex);
1783}
1784
1785/**
1786 * Waits for a sequence number to be signaled, and cleans up the
1787 * request and object lists appropriately for that event.
1788 */
Eric Anholt3043c602008-10-02 12:24:47 -07001789static int
Eric Anholt673a3942008-07-30 12:06:12 -07001790i915_wait_request(struct drm_device *dev, uint32_t seqno)
1791{
1792 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001793 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001794 int ret = 0;
1795
1796 BUG_ON(seqno == 0);
1797
Ben Gamariba1234d2009-09-14 17:48:47 -04001798 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001799 return -EIO;
1800
Eric Anholt673a3942008-07-30 12:06:12 -07001801 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001802 if (IS_IGDNG(dev))
1803 ier = I915_READ(DEIER) | I915_READ(GTIER);
1804 else
1805 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001806 if (!ier) {
1807 DRM_ERROR("something (likely vbetool) disabled "
1808 "interrupts, re-enabling\n");
1809 i915_driver_irq_preinstall(dev);
1810 i915_driver_irq_postinstall(dev);
1811 }
1812
Eric Anholt673a3942008-07-30 12:06:12 -07001813 dev_priv->mm.waiting_gem_seqno = seqno;
1814 i915_user_irq_get(dev);
1815 ret = wait_event_interruptible(dev_priv->irq_queue,
1816 i915_seqno_passed(i915_get_gem_seqno(dev),
1817 seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001818 atomic_read(&dev_priv->mm.wedged));
Eric Anholt673a3942008-07-30 12:06:12 -07001819 i915_user_irq_put(dev);
1820 dev_priv->mm.waiting_gem_seqno = 0;
1821 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001822 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001823 ret = -EIO;
1824
1825 if (ret && ret != -ERESTARTSYS)
1826 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1827 __func__, ret, seqno, i915_get_gem_seqno(dev));
1828
1829 /* Directly dispatch request retiring. While we have the work queue
1830 * to handle this, the waiter on a request often wants an associated
1831 * buffer to have made it to the inactive list, and we would need
1832 * a separate wait queue to handle that.
1833 */
1834 if (ret == 0)
1835 i915_gem_retire_requests(dev);
1836
1837 return ret;
1838}
1839
1840static void
1841i915_gem_flush(struct drm_device *dev,
1842 uint32_t invalidate_domains,
1843 uint32_t flush_domains)
1844{
1845 drm_i915_private_t *dev_priv = dev->dev_private;
1846 uint32_t cmd;
1847 RING_LOCALS;
1848
1849#if WATCH_EXEC
1850 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1851 invalidate_domains, flush_domains);
1852#endif
1853
1854 if (flush_domains & I915_GEM_DOMAIN_CPU)
1855 drm_agp_chipset_flush(dev);
1856
Chris Wilson21d509e2009-06-06 09:46:02 +01001857 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
Eric Anholt673a3942008-07-30 12:06:12 -07001858 /*
1859 * read/write caches:
1860 *
1861 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1862 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1863 * also flushed at 2d versus 3d pipeline switches.
1864 *
1865 * read-only caches:
1866 *
1867 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1868 * MI_READ_FLUSH is set, and is always flushed on 965.
1869 *
1870 * I915_GEM_DOMAIN_COMMAND may not exist?
1871 *
1872 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1873 * invalidated when MI_EXE_FLUSH is set.
1874 *
1875 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1876 * invalidated with every MI_FLUSH.
1877 *
1878 * TLBs:
1879 *
1880 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1881 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1882 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1883 * are flushed at any MI_FLUSH.
1884 */
1885
1886 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1887 if ((invalidate_domains|flush_domains) &
1888 I915_GEM_DOMAIN_RENDER)
1889 cmd &= ~MI_NO_WRITE_FLUSH;
1890 if (!IS_I965G(dev)) {
1891 /*
1892 * On the 965, the sampler cache always gets flushed
1893 * and this bit is reserved.
1894 */
1895 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1896 cmd |= MI_READ_FLUSH;
1897 }
1898 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1899 cmd |= MI_EXE_FLUSH;
1900
1901#if WATCH_EXEC
1902 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1903#endif
1904 BEGIN_LP_RING(2);
1905 OUT_RING(cmd);
1906 OUT_RING(0); /* noop */
1907 ADVANCE_LP_RING();
1908 }
1909}
1910
1911/**
1912 * Ensures that all rendering to the object has completed and the object is
1913 * safe to unbind from the GTT or access from the CPU.
1914 */
1915static int
1916i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1917{
1918 struct drm_device *dev = obj->dev;
1919 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1920 int ret;
1921
Eric Anholte47c68e2008-11-14 13:35:19 -08001922 /* This function only exists to support waiting for existing rendering,
1923 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001924 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001925 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001926
1927 /* If there is rendering queued on the buffer being evicted, wait for
1928 * it.
1929 */
1930 if (obj_priv->active) {
1931#if WATCH_BUF
1932 DRM_INFO("%s: object %p wait for seqno %08x\n",
1933 __func__, obj, obj_priv->last_rendering_seqno);
1934#endif
1935 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1936 if (ret != 0)
1937 return ret;
1938 }
1939
1940 return 0;
1941}
1942
1943/**
1944 * Unbinds an object from the GTT aperture.
1945 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001946int
Eric Anholt673a3942008-07-30 12:06:12 -07001947i915_gem_object_unbind(struct drm_gem_object *obj)
1948{
1949 struct drm_device *dev = obj->dev;
1950 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1951 int ret = 0;
1952
1953#if WATCH_BUF
1954 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1955 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1956#endif
1957 if (obj_priv->gtt_space == NULL)
1958 return 0;
1959
1960 if (obj_priv->pin_count != 0) {
1961 DRM_ERROR("Attempting to unbind pinned buffer\n");
1962 return -EINVAL;
1963 }
1964
Eric Anholt5323fd02009-09-09 11:50:45 -07001965 /* blow away mappings if mapped through GTT */
1966 i915_gem_release_mmap(obj);
1967
1968 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1969 i915_gem_clear_fence_reg(obj);
1970
Eric Anholt673a3942008-07-30 12:06:12 -07001971 /* Move the object to the CPU domain to ensure that
1972 * any possible CPU writes while it's not in the GTT
1973 * are flushed when we go to remap it. This will
1974 * also ensure that all pending GPU writes are finished
1975 * before we unbind.
1976 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001977 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001978 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001979 if (ret != -ERESTARTSYS)
1980 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001981 return ret;
1982 }
1983
Eric Anholt5323fd02009-09-09 11:50:45 -07001984 BUG_ON(obj_priv->active);
1985
Eric Anholt673a3942008-07-30 12:06:12 -07001986 if (obj_priv->agp_mem != NULL) {
1987 drm_unbind_agp(obj_priv->agp_mem);
1988 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1989 obj_priv->agp_mem = NULL;
1990 }
1991
Eric Anholt856fa192009-03-19 14:10:50 -07001992 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001993
1994 if (obj_priv->gtt_space) {
1995 atomic_dec(&dev->gtt_count);
1996 atomic_sub(obj->size, &dev->gtt_memory);
1997
1998 drm_mm_put_block(obj_priv->gtt_space);
1999 obj_priv->gtt_space = NULL;
2000 }
2001
2002 /* Remove ourselves from the LRU list if present. */
2003 if (!list_empty(&obj_priv->list))
2004 list_del_init(&obj_priv->list);
2005
2006 return 0;
2007}
2008
Chris Wilson07f73f62009-09-14 16:50:30 +01002009static inline int
2010i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
2011{
2012 return !obj_priv->dirty || obj_priv->madv == I915_MADV_DONTNEED;
2013}
2014
2015static struct drm_gem_object *
2016i915_gem_find_inactive_object(struct drm_device *dev, int min_size)
2017{
2018 drm_i915_private_t *dev_priv = dev->dev_private;
2019 struct drm_i915_gem_object *obj_priv;
2020 struct drm_gem_object *best = NULL;
2021 struct drm_gem_object *first = NULL;
2022
2023 /* Try to find the smallest clean object */
2024 list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
2025 struct drm_gem_object *obj = obj_priv->obj;
2026 if (obj->size >= min_size) {
2027 if (i915_gem_object_is_purgeable(obj_priv) &&
2028 (!best || obj->size < best->size)) {
2029 best = obj;
2030 if (best->size == min_size)
2031 return best;
2032 }
2033 if (!first)
2034 first = obj;
2035 }
2036 }
2037
2038 return best ? best : first;
2039}
2040
Eric Anholt673a3942008-07-30 12:06:12 -07002041static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002042i915_gem_evict_everything(struct drm_device *dev)
2043{
2044 drm_i915_private_t *dev_priv = dev->dev_private;
2045 uint32_t seqno;
2046 int ret;
2047 bool lists_empty;
2048
2049 DRM_INFO("GTT full, evicting everything: "
2050 "%d objects [%d pinned], "
2051 "%d object bytes [%d pinned], "
2052 "%d/%d gtt bytes\n",
2053 atomic_read(&dev->object_count),
2054 atomic_read(&dev->pin_count),
2055 atomic_read(&dev->object_memory),
2056 atomic_read(&dev->pin_memory),
2057 atomic_read(&dev->gtt_memory),
2058 dev->gtt_total);
2059
2060 spin_lock(&dev_priv->mm.active_list_lock);
2061 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2062 list_empty(&dev_priv->mm.flushing_list) &&
2063 list_empty(&dev_priv->mm.active_list));
2064 spin_unlock(&dev_priv->mm.active_list_lock);
2065
2066 if (lists_empty) {
2067 DRM_ERROR("GTT full, but lists empty!\n");
2068 return -ENOSPC;
2069 }
2070
2071 /* Flush everything (on to the inactive lists) and evict */
2072 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2073 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
2074 if (seqno == 0)
2075 return -ENOMEM;
2076
2077 ret = i915_wait_request(dev, seqno);
2078 if (ret)
2079 return ret;
2080
2081 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
2082 if (ret)
2083 return ret;
2084
2085 spin_lock(&dev_priv->mm.active_list_lock);
2086 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2087 list_empty(&dev_priv->mm.flushing_list) &&
2088 list_empty(&dev_priv->mm.active_list));
2089 spin_unlock(&dev_priv->mm.active_list_lock);
2090 BUG_ON(!lists_empty);
2091
2092 return 0;
2093}
2094
2095static int
2096i915_gem_evict_something(struct drm_device *dev, int min_size)
Eric Anholt673a3942008-07-30 12:06:12 -07002097{
2098 drm_i915_private_t *dev_priv = dev->dev_private;
2099 struct drm_gem_object *obj;
Chris Wilson07f73f62009-09-14 16:50:30 +01002100 int have_waited = 0;
2101 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002102
2103 for (;;) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002104 i915_gem_retire_requests(dev);
2105
Eric Anholt673a3942008-07-30 12:06:12 -07002106 /* If there's an inactive buffer available now, grab it
2107 * and be done.
2108 */
Chris Wilson07f73f62009-09-14 16:50:30 +01002109 obj = i915_gem_find_inactive_object(dev, min_size);
2110 if (obj) {
2111 struct drm_i915_gem_object *obj_priv;
2112
Eric Anholt673a3942008-07-30 12:06:12 -07002113#if WATCH_LRU
2114 DRM_INFO("%s: evicting %p\n", __func__, obj);
2115#endif
Chris Wilson07f73f62009-09-14 16:50:30 +01002116 obj_priv = obj->driver_private;
2117 BUG_ON(obj_priv->pin_count != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002118 BUG_ON(obj_priv->active);
2119
2120 /* Wait on the rendering and unbind the buffer. */
Chris Wilson07f73f62009-09-14 16:50:30 +01002121 return i915_gem_object_unbind(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002122 }
2123
Chris Wilson07f73f62009-09-14 16:50:30 +01002124 if (have_waited)
2125 return 0;
2126
Eric Anholt673a3942008-07-30 12:06:12 -07002127 /* If we didn't get anything, but the ring is still processing
Chris Wilson07f73f62009-09-14 16:50:30 +01002128 * things, wait for the next to finish and hopefully leave us
2129 * a buffer to evict.
Eric Anholt673a3942008-07-30 12:06:12 -07002130 */
2131 if (!list_empty(&dev_priv->mm.request_list)) {
2132 struct drm_i915_gem_request *request;
2133
2134 request = list_first_entry(&dev_priv->mm.request_list,
2135 struct drm_i915_gem_request,
2136 list);
2137
2138 ret = i915_wait_request(dev, request->seqno);
2139 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002140 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002141
Chris Wilson07f73f62009-09-14 16:50:30 +01002142 have_waited = 1;
2143 continue;
Eric Anholt673a3942008-07-30 12:06:12 -07002144 }
2145
2146 /* If we didn't have anything on the request list but there
2147 * are buffers awaiting a flush, emit one and try again.
2148 * When we wait on it, those buffers waiting for that flush
2149 * will get moved to inactive.
2150 */
2151 if (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002152 struct drm_i915_gem_object *obj_priv;
2153 uint32_t seqno;
2154
Eric Anholt673a3942008-07-30 12:06:12 -07002155 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
2156 struct drm_i915_gem_object,
2157 list);
2158 obj = obj_priv->obj;
2159
2160 i915_gem_flush(dev,
2161 obj->write_domain,
2162 obj->write_domain);
Chris Wilson07f73f62009-09-14 16:50:30 +01002163 seqno = i915_add_request(dev, NULL, obj->write_domain);
2164 if (seqno == 0)
2165 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002166
Chris Wilson07f73f62009-09-14 16:50:30 +01002167 ret = i915_wait_request(dev, seqno);
2168 if (ret)
2169 return ret;
2170
2171 have_waited = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07002172 continue;
2173 }
2174
Chris Wilson07f73f62009-09-14 16:50:30 +01002175 /* If we didn't do any of the above, there's no single buffer
2176 * large enough to swap out for the new one, so just evict
2177 * everything and start again. (This should be rare.)
Eric Anholt673a3942008-07-30 12:06:12 -07002178 */
Chris Wilson07f73f62009-09-14 16:50:30 +01002179 if (!list_empty (&dev_priv->mm.inactive_list)) {
2180 DRM_INFO("GTT full, evicting inactive buffers\n");
2181 return i915_gem_evict_from_list(dev,
2182 &dev_priv->mm.inactive_list);
2183 } else
2184 return i915_gem_evict_everything(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002185 }
Keith Packardac94a962008-11-20 23:30:27 -08002186}
2187
Ben Gamari6911a9b2009-04-02 11:24:54 -07002188int
Eric Anholt856fa192009-03-19 14:10:50 -07002189i915_gem_object_get_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002190{
2191 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2192 int page_count, i;
2193 struct address_space *mapping;
2194 struct inode *inode;
2195 struct page *page;
2196 int ret;
2197
Eric Anholt856fa192009-03-19 14:10:50 -07002198 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002199 return 0;
2200
2201 /* Get the list of pages out of our struct file. They'll be pinned
2202 * at this point until we release them.
2203 */
2204 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002205 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002206 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002207 if (obj_priv->pages == NULL) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002208 DRM_ERROR("Failed to allocate page list\n");
Eric Anholt856fa192009-03-19 14:10:50 -07002209 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002210 return -ENOMEM;
2211 }
2212
2213 inode = obj->filp->f_path.dentry->d_inode;
2214 mapping = inode->i_mapping;
2215 for (i = 0; i < page_count; i++) {
2216 page = read_mapping_page(mapping, i, NULL);
2217 if (IS_ERR(page)) {
2218 ret = PTR_ERR(page);
Eric Anholt856fa192009-03-19 14:10:50 -07002219 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002220 return ret;
2221 }
Eric Anholt856fa192009-03-19 14:10:50 -07002222 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002223 }
Eric Anholt280b7132009-03-12 16:56:27 -07002224
2225 if (obj_priv->tiling_mode != I915_TILING_NONE)
2226 i915_gem_object_do_bit_17_swizzle(obj);
2227
Eric Anholt673a3942008-07-30 12:06:12 -07002228 return 0;
2229}
2230
Jesse Barnesde151cf2008-11-12 10:03:55 -08002231static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2232{
2233 struct drm_gem_object *obj = reg->obj;
2234 struct drm_device *dev = obj->dev;
2235 drm_i915_private_t *dev_priv = dev->dev_private;
2236 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2237 int regnum = obj_priv->fence_reg;
2238 uint64_t val;
2239
2240 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2241 0xfffff000) << 32;
2242 val |= obj_priv->gtt_offset & 0xfffff000;
2243 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2244 if (obj_priv->tiling_mode == I915_TILING_Y)
2245 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2246 val |= I965_FENCE_REG_VALID;
2247
2248 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2249}
2250
2251static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2252{
2253 struct drm_gem_object *obj = reg->obj;
2254 struct drm_device *dev = obj->dev;
2255 drm_i915_private_t *dev_priv = dev->dev_private;
2256 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2257 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002258 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002259 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002260 uint32_t pitch_val;
2261
2262 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2263 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002264 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002265 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002266 return;
2267 }
2268
Jesse Barnes0f973f22009-01-26 17:10:45 -08002269 if (obj_priv->tiling_mode == I915_TILING_Y &&
2270 HAS_128_BYTE_Y_TILING(dev))
2271 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002272 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002273 tile_width = 512;
2274
2275 /* Note: pitch better be a power of two tile widths */
2276 pitch_val = obj_priv->stride / tile_width;
2277 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002278
2279 val = obj_priv->gtt_offset;
2280 if (obj_priv->tiling_mode == I915_TILING_Y)
2281 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2282 val |= I915_FENCE_SIZE_BITS(obj->size);
2283 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2284 val |= I830_FENCE_REG_VALID;
2285
Eric Anholtdc529a42009-03-10 22:34:49 -07002286 if (regnum < 8)
2287 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2288 else
2289 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2290 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002291}
2292
2293static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2294{
2295 struct drm_gem_object *obj = reg->obj;
2296 struct drm_device *dev = obj->dev;
2297 drm_i915_private_t *dev_priv = dev->dev_private;
2298 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2299 int regnum = obj_priv->fence_reg;
2300 uint32_t val;
2301 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002302 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002303
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002304 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002305 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002306 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002307 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308 return;
2309 }
2310
Eric Anholte76a16d2009-05-26 17:44:56 -07002311 pitch_val = obj_priv->stride / 128;
2312 pitch_val = ffs(pitch_val) - 1;
2313 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2314
Jesse Barnesde151cf2008-11-12 10:03:55 -08002315 val = obj_priv->gtt_offset;
2316 if (obj_priv->tiling_mode == I915_TILING_Y)
2317 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002318 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2319 WARN_ON(fence_size_bits & ~0x00000f00);
2320 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002321 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2322 val |= I830_FENCE_REG_VALID;
2323
2324 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002325}
2326
2327/**
2328 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2329 * @obj: object to map through a fence reg
2330 *
2331 * When mapping objects through the GTT, userspace wants to be able to write
2332 * to them without having to worry about swizzling if the object is tiled.
2333 *
2334 * This function walks the fence regs looking for a free one for @obj,
2335 * stealing one if it can't find any.
2336 *
2337 * It then sets up the reg based on the object's properties: address, pitch
2338 * and tiling format.
2339 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002340int
2341i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002342{
2343 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002344 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002345 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2346 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002347 struct drm_i915_gem_object *old_obj_priv = NULL;
2348 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002349
Eric Anholta09ba7f2009-08-29 12:49:51 -07002350 /* Just update our place in the LRU if our fence is getting used. */
2351 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
2352 list_move_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2353 return 0;
2354 }
2355
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356 switch (obj_priv->tiling_mode) {
2357 case I915_TILING_NONE:
2358 WARN(1, "allocating a fence for non-tiled object?\n");
2359 break;
2360 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002361 if (!obj_priv->stride)
2362 return -EINVAL;
2363 WARN((obj_priv->stride & (512 - 1)),
2364 "object 0x%08x is X tiled but has non-512B pitch\n",
2365 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002366 break;
2367 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002368 if (!obj_priv->stride)
2369 return -EINVAL;
2370 WARN((obj_priv->stride & (128 - 1)),
2371 "object 0x%08x is Y tiled but has non-128B pitch\n",
2372 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002373 break;
2374 }
2375
2376 /* First try to find a free reg */
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002377 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002378 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2379 reg = &dev_priv->fence_regs[i];
2380 if (!reg->obj)
2381 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002382
2383 old_obj_priv = reg->obj->driver_private;
2384 if (!old_obj_priv->pin_count)
2385 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002386 }
2387
2388 /* None available, try to steal one or wait for a user to finish */
2389 if (i == dev_priv->num_fence_regs) {
Eric Anholta09ba7f2009-08-29 12:49:51 -07002390 struct drm_gem_object *old_obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002391
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002392 if (avail == 0)
Chris Wilson2939e1f2009-06-06 09:46:03 +01002393 return -ENOSPC;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002394
Eric Anholta09ba7f2009-08-29 12:49:51 -07002395 list_for_each_entry(old_obj_priv, &dev_priv->mm.fence_list,
2396 fence_list) {
2397 old_obj = old_obj_priv->obj;
Chris Wilsond7619c42009-02-11 14:26:47 +00002398
2399 if (old_obj_priv->pin_count)
2400 continue;
2401
Eric Anholta09ba7f2009-08-29 12:49:51 -07002402 /* Take a reference, as otherwise the wait_rendering
2403 * below may cause the object to get freed out from
2404 * under us.
2405 */
2406 drm_gem_object_reference(old_obj);
2407
Chris Wilsond7619c42009-02-11 14:26:47 +00002408 /* i915 uses fences for GPU access to tiled buffers */
2409 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002410 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002411
Eric Anholta09ba7f2009-08-29 12:49:51 -07002412 /* This brings the object to the head of the LRU if it
2413 * had been written to. The only way this should
2414 * result in us waiting longer than the expected
2415 * optimal amount of time is if there was a
2416 * fence-using buffer later that was read-only.
2417 */
2418 i915_gem_object_flush_gpu_write_domain(old_obj);
2419 ret = i915_gem_object_wait_rendering(old_obj);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002420 if (ret != 0) {
2421 drm_gem_object_unreference(old_obj);
Chris Wilsond7619c42009-02-11 14:26:47 +00002422 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002423 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002424
Eric Anholta09ba7f2009-08-29 12:49:51 -07002425 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002426 }
2427
2428 /*
2429 * Zap this virtual mapping so we can set up a fence again
2430 * for this object next time we need it.
2431 */
Chris Wilson58c2fb62009-09-01 12:02:39 +01002432 i915_gem_release_mmap(old_obj);
2433
Eric Anholta09ba7f2009-08-29 12:49:51 -07002434 i = old_obj_priv->fence_reg;
Chris Wilson58c2fb62009-09-01 12:02:39 +01002435 reg = &dev_priv->fence_regs[i];
2436
Jesse Barnesde151cf2008-11-12 10:03:55 -08002437 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002438 list_del_init(&old_obj_priv->fence_list);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002439
Eric Anholta09ba7f2009-08-29 12:49:51 -07002440 drm_gem_object_unreference(old_obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002441 }
2442
2443 obj_priv->fence_reg = i;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002444 list_add_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2445
Jesse Barnesde151cf2008-11-12 10:03:55 -08002446 reg->obj = obj;
2447
2448 if (IS_I965G(dev))
2449 i965_write_fence_reg(reg);
2450 else if (IS_I9XX(dev))
2451 i915_write_fence_reg(reg);
2452 else
2453 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002454
2455 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002456}
2457
2458/**
2459 * i915_gem_clear_fence_reg - clear out fence register info
2460 * @obj: object to clear
2461 *
2462 * Zeroes out the fence register itself and clears out the associated
2463 * data structures in dev_priv and obj_priv.
2464 */
2465static void
2466i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2467{
2468 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002469 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002470 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2471
2472 if (IS_I965G(dev))
2473 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002474 else {
2475 uint32_t fence_reg;
2476
2477 if (obj_priv->fence_reg < 8)
2478 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2479 else
2480 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2481 8) * 4;
2482
2483 I915_WRITE(fence_reg, 0);
2484 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002485
2486 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2487 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002488 list_del_init(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002489}
2490
Eric Anholt673a3942008-07-30 12:06:12 -07002491/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002492 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2493 * to the buffer to finish, and then resets the fence register.
2494 * @obj: tiled object holding a fence register.
2495 *
2496 * Zeroes out the fence register itself and clears out the associated
2497 * data structures in dev_priv and obj_priv.
2498 */
2499int
2500i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2501{
2502 struct drm_device *dev = obj->dev;
2503 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2504
2505 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2506 return 0;
2507
2508 /* On the i915, GPU access to tiled buffers is via a fence,
2509 * therefore we must wait for any outstanding access to complete
2510 * before clearing the fence.
2511 */
2512 if (!IS_I965G(dev)) {
2513 int ret;
2514
2515 i915_gem_object_flush_gpu_write_domain(obj);
2516 i915_gem_object_flush_gtt_write_domain(obj);
2517 ret = i915_gem_object_wait_rendering(obj);
2518 if (ret != 0)
2519 return ret;
2520 }
2521
2522 i915_gem_clear_fence_reg (obj);
2523
2524 return 0;
2525}
2526
2527/**
Eric Anholt673a3942008-07-30 12:06:12 -07002528 * Finds free space in the GTT aperture and binds the object there.
2529 */
2530static int
2531i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2532{
2533 struct drm_device *dev = obj->dev;
2534 drm_i915_private_t *dev_priv = dev->dev_private;
2535 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2536 struct drm_mm_node *free_space;
Chris Wilson07f73f62009-09-14 16:50:30 +01002537 bool retry_alloc = false;
2538 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002539
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002540 if (dev_priv->mm.suspended)
2541 return -EBUSY;
Chris Wilson3ef94da2009-09-14 16:50:29 +01002542
2543 if (obj_priv->madv == I915_MADV_DONTNEED) {
2544 DRM_ERROR("Attempting to bind a purgeable object\n");
2545 return -EINVAL;
2546 }
2547
Eric Anholt673a3942008-07-30 12:06:12 -07002548 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002549 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002550 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002551 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2552 return -EINVAL;
2553 }
2554
2555 search_free:
2556 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2557 obj->size, alignment, 0);
2558 if (free_space != NULL) {
2559 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2560 alignment);
2561 if (obj_priv->gtt_space != NULL) {
2562 obj_priv->gtt_space->private = obj;
2563 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2564 }
2565 }
2566 if (obj_priv->gtt_space == NULL) {
2567 /* If the gtt is empty and we're still having trouble
2568 * fitting our object in, we're out of memory.
2569 */
2570#if WATCH_LRU
2571 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2572#endif
Chris Wilson07f73f62009-09-14 16:50:30 +01002573 ret = i915_gem_evict_something(dev, obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07002574 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08002575 if (ret != -ERESTARTSYS)
2576 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002577 return ret;
2578 }
2579 goto search_free;
2580 }
2581
2582#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002583 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002584 obj->size, obj_priv->gtt_offset);
2585#endif
Chris Wilson07f73f62009-09-14 16:50:30 +01002586 if (retry_alloc) {
2587 i915_gem_object_set_page_gfp_mask (obj,
2588 i915_gem_object_get_page_gfp_mask (obj) & ~__GFP_NORETRY);
2589 }
Eric Anholt856fa192009-03-19 14:10:50 -07002590 ret = i915_gem_object_get_pages(obj);
Chris Wilson07f73f62009-09-14 16:50:30 +01002591 if (retry_alloc) {
2592 i915_gem_object_set_page_gfp_mask (obj,
2593 i915_gem_object_get_page_gfp_mask (obj) | __GFP_NORETRY);
2594 }
Eric Anholt673a3942008-07-30 12:06:12 -07002595 if (ret) {
2596 drm_mm_put_block(obj_priv->gtt_space);
2597 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002598
2599 if (ret == -ENOMEM) {
2600 /* first try to clear up some space from the GTT */
2601 ret = i915_gem_evict_something(dev, obj->size);
2602 if (ret) {
2603 if (ret != -ERESTARTSYS)
2604 DRM_ERROR("Failed to allocate space for backing pages %d\n", ret);
2605
2606 /* now try to shrink everyone else */
2607 if (! retry_alloc) {
2608 retry_alloc = true;
2609 goto search_free;
2610 }
2611
2612 return ret;
2613 }
2614
2615 goto search_free;
2616 }
2617
Eric Anholt673a3942008-07-30 12:06:12 -07002618 return ret;
2619 }
2620
Eric Anholt673a3942008-07-30 12:06:12 -07002621 /* Create an AGP memory structure pointing at our pages, and bind it
2622 * into the GTT.
2623 */
2624 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002625 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002626 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002627 obj_priv->gtt_offset,
2628 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002629 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002630 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002631 drm_mm_put_block(obj_priv->gtt_space);
2632 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002633
2634 ret = i915_gem_evict_something(dev, obj->size);
2635 if (ret) {
2636 if (ret != -ERESTARTSYS)
2637 DRM_ERROR("Failed to allocate space to bind AGP: %d\n", ret);
2638 return ret;
2639 }
2640
2641 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002642 }
2643 atomic_inc(&dev->gtt_count);
2644 atomic_add(obj->size, &dev->gtt_memory);
2645
2646 /* Assert that the object is not currently in any GPU domain. As it
2647 * wasn't in the GTT, there shouldn't be any way it could have been in
2648 * a GPU cache
2649 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002650 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2651 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002652
2653 return 0;
2654}
2655
2656void
2657i915_gem_clflush_object(struct drm_gem_object *obj)
2658{
2659 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2660
2661 /* If we don't have a page list set up, then we're not pinned
2662 * to GPU, and we can ignore the cache flush because it'll happen
2663 * again at bind time.
2664 */
Eric Anholt856fa192009-03-19 14:10:50 -07002665 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002666 return;
2667
Eric Anholt856fa192009-03-19 14:10:50 -07002668 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002669}
2670
Eric Anholte47c68e2008-11-14 13:35:19 -08002671/** Flushes any GPU write domain for the object if it's dirty. */
2672static void
2673i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2674{
2675 struct drm_device *dev = obj->dev;
2676 uint32_t seqno;
2677
2678 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2679 return;
2680
2681 /* Queue the GPU write cache flushing we need. */
2682 i915_gem_flush(dev, 0, obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002683 seqno = i915_add_request(dev, NULL, obj->write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002684 obj->write_domain = 0;
2685 i915_gem_object_move_to_active(obj, seqno);
2686}
2687
2688/** Flushes the GTT write domain for the object if it's dirty. */
2689static void
2690i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2691{
2692 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2693 return;
2694
2695 /* No actual flushing is required for the GTT write domain. Writes
2696 * to it immediately go to main memory as far as we know, so there's
2697 * no chipset flush. It also doesn't land in render cache.
2698 */
2699 obj->write_domain = 0;
2700}
2701
2702/** Flushes the CPU write domain for the object if it's dirty. */
2703static void
2704i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2705{
2706 struct drm_device *dev = obj->dev;
2707
2708 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2709 return;
2710
2711 i915_gem_clflush_object(obj);
2712 drm_agp_chipset_flush(dev);
2713 obj->write_domain = 0;
2714}
2715
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002716/**
2717 * Moves a single object to the GTT read, and possibly write domain.
2718 *
2719 * This function returns when the move is complete, including waiting on
2720 * flushes to occur.
2721 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002722int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002723i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2724{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002725 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002726 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002727
Eric Anholt02354392008-11-26 13:58:13 -08002728 /* Not valid to be called on unbound objects. */
2729 if (obj_priv->gtt_space == NULL)
2730 return -EINVAL;
2731
Eric Anholte47c68e2008-11-14 13:35:19 -08002732 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002733 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002734 ret = i915_gem_object_wait_rendering(obj);
2735 if (ret != 0)
2736 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002737
2738 /* If we're writing through the GTT domain, then CPU and GPU caches
2739 * will need to be invalidated at next use.
2740 */
2741 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002742 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002743
Eric Anholte47c68e2008-11-14 13:35:19 -08002744 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002745
2746 /* It should now be out of any other write domains, and we can update
2747 * the domain values for our changes.
2748 */
2749 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2750 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002751 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002752 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002753 obj_priv->dirty = 1;
2754 }
2755
2756 return 0;
2757}
2758
2759/**
2760 * Moves a single object to the CPU read, and possibly write domain.
2761 *
2762 * This function returns when the move is complete, including waiting on
2763 * flushes to occur.
2764 */
2765static int
2766i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2767{
Eric Anholte47c68e2008-11-14 13:35:19 -08002768 int ret;
2769
2770 i915_gem_object_flush_gpu_write_domain(obj);
2771 /* Wait on any GPU rendering and flushing to occur. */
2772 ret = i915_gem_object_wait_rendering(obj);
2773 if (ret != 0)
2774 return ret;
2775
2776 i915_gem_object_flush_gtt_write_domain(obj);
2777
2778 /* If we have a partially-valid cache of the object in the CPU,
2779 * finish invalidating it and free the per-page flags.
2780 */
2781 i915_gem_object_set_to_full_cpu_read_domain(obj);
2782
2783 /* Flush the CPU cache if it's still invalid. */
2784 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2785 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002786
2787 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2788 }
2789
2790 /* It should now be out of any other write domains, and we can update
2791 * the domain values for our changes.
2792 */
2793 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2794
2795 /* If we're writing through the CPU, then the GPU read domains will
2796 * need to be invalidated at next use.
2797 */
2798 if (write) {
2799 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2800 obj->write_domain = I915_GEM_DOMAIN_CPU;
2801 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002802
2803 return 0;
2804}
2805
Eric Anholt673a3942008-07-30 12:06:12 -07002806/*
2807 * Set the next domain for the specified object. This
2808 * may not actually perform the necessary flushing/invaliding though,
2809 * as that may want to be batched with other set_domain operations
2810 *
2811 * This is (we hope) the only really tricky part of gem. The goal
2812 * is fairly simple -- track which caches hold bits of the object
2813 * and make sure they remain coherent. A few concrete examples may
2814 * help to explain how it works. For shorthand, we use the notation
2815 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2816 * a pair of read and write domain masks.
2817 *
2818 * Case 1: the batch buffer
2819 *
2820 * 1. Allocated
2821 * 2. Written by CPU
2822 * 3. Mapped to GTT
2823 * 4. Read by GPU
2824 * 5. Unmapped from GTT
2825 * 6. Freed
2826 *
2827 * Let's take these a step at a time
2828 *
2829 * 1. Allocated
2830 * Pages allocated from the kernel may still have
2831 * cache contents, so we set them to (CPU, CPU) always.
2832 * 2. Written by CPU (using pwrite)
2833 * The pwrite function calls set_domain (CPU, CPU) and
2834 * this function does nothing (as nothing changes)
2835 * 3. Mapped by GTT
2836 * This function asserts that the object is not
2837 * currently in any GPU-based read or write domains
2838 * 4. Read by GPU
2839 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2840 * As write_domain is zero, this function adds in the
2841 * current read domains (CPU+COMMAND, 0).
2842 * flush_domains is set to CPU.
2843 * invalidate_domains is set to COMMAND
2844 * clflush is run to get data out of the CPU caches
2845 * then i915_dev_set_domain calls i915_gem_flush to
2846 * emit an MI_FLUSH and drm_agp_chipset_flush
2847 * 5. Unmapped from GTT
2848 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2849 * flush_domains and invalidate_domains end up both zero
2850 * so no flushing/invalidating happens
2851 * 6. Freed
2852 * yay, done
2853 *
2854 * Case 2: The shared render buffer
2855 *
2856 * 1. Allocated
2857 * 2. Mapped to GTT
2858 * 3. Read/written by GPU
2859 * 4. set_domain to (CPU,CPU)
2860 * 5. Read/written by CPU
2861 * 6. Read/written by GPU
2862 *
2863 * 1. Allocated
2864 * Same as last example, (CPU, CPU)
2865 * 2. Mapped to GTT
2866 * Nothing changes (assertions find that it is not in the GPU)
2867 * 3. Read/written by GPU
2868 * execbuffer calls set_domain (RENDER, RENDER)
2869 * flush_domains gets CPU
2870 * invalidate_domains gets GPU
2871 * clflush (obj)
2872 * MI_FLUSH and drm_agp_chipset_flush
2873 * 4. set_domain (CPU, CPU)
2874 * flush_domains gets GPU
2875 * invalidate_domains gets CPU
2876 * wait_rendering (obj) to make sure all drawing is complete.
2877 * This will include an MI_FLUSH to get the data from GPU
2878 * to memory
2879 * clflush (obj) to invalidate the CPU cache
2880 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2881 * 5. Read/written by CPU
2882 * cache lines are loaded and dirtied
2883 * 6. Read written by GPU
2884 * Same as last GPU access
2885 *
2886 * Case 3: The constant buffer
2887 *
2888 * 1. Allocated
2889 * 2. Written by CPU
2890 * 3. Read by GPU
2891 * 4. Updated (written) by CPU again
2892 * 5. Read by GPU
2893 *
2894 * 1. Allocated
2895 * (CPU, CPU)
2896 * 2. Written by CPU
2897 * (CPU, CPU)
2898 * 3. Read by GPU
2899 * (CPU+RENDER, 0)
2900 * flush_domains = CPU
2901 * invalidate_domains = RENDER
2902 * clflush (obj)
2903 * MI_FLUSH
2904 * drm_agp_chipset_flush
2905 * 4. Updated (written) by CPU again
2906 * (CPU, CPU)
2907 * flush_domains = 0 (no previous write domain)
2908 * invalidate_domains = 0 (no new read domains)
2909 * 5. Read by GPU
2910 * (CPU+RENDER, 0)
2911 * flush_domains = CPU
2912 * invalidate_domains = RENDER
2913 * clflush (obj)
2914 * MI_FLUSH
2915 * drm_agp_chipset_flush
2916 */
Keith Packardc0d90822008-11-20 23:11:08 -08002917static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002918i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002919{
2920 struct drm_device *dev = obj->dev;
2921 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2922 uint32_t invalidate_domains = 0;
2923 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002924
Eric Anholt8b0e3782009-02-19 14:40:50 -08002925 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2926 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002927
Jesse Barnes652c3932009-08-17 13:31:43 -07002928 intel_mark_busy(dev, obj);
2929
Eric Anholt673a3942008-07-30 12:06:12 -07002930#if WATCH_BUF
2931 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2932 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002933 obj->read_domains, obj->pending_read_domains,
2934 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002935#endif
2936 /*
2937 * If the object isn't moving to a new write domain,
2938 * let the object stay in multiple read domains
2939 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002940 if (obj->pending_write_domain == 0)
2941 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002942 else
2943 obj_priv->dirty = 1;
2944
2945 /*
2946 * Flush the current write domain if
2947 * the new read domains don't match. Invalidate
2948 * any read domains which differ from the old
2949 * write domain
2950 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002951 if (obj->write_domain &&
2952 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002953 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002954 invalidate_domains |=
2955 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002956 }
2957 /*
2958 * Invalidate any read caches which may have
2959 * stale data. That is, any new read domains.
2960 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002961 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002962 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2963#if WATCH_BUF
2964 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2965 __func__, flush_domains, invalidate_domains);
2966#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002967 i915_gem_clflush_object(obj);
2968 }
2969
Eric Anholtefbeed92009-02-19 14:54:51 -08002970 /* The actual obj->write_domain will be updated with
2971 * pending_write_domain after we emit the accumulated flush for all
2972 * of our domain changes in execbuffers (which clears objects'
2973 * write_domains). So if we have a current write domain that we
2974 * aren't changing, set pending_write_domain to that.
2975 */
2976 if (flush_domains == 0 && obj->pending_write_domain == 0)
2977 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002978 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002979
2980 dev->invalidate_domains |= invalidate_domains;
2981 dev->flush_domains |= flush_domains;
2982#if WATCH_BUF
2983 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2984 __func__,
2985 obj->read_domains, obj->write_domain,
2986 dev->invalidate_domains, dev->flush_domains);
2987#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002988}
2989
2990/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002991 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002992 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002993 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2994 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2995 */
2996static void
2997i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2998{
Eric Anholte47c68e2008-11-14 13:35:19 -08002999 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3000
3001 if (!obj_priv->page_cpu_valid)
3002 return;
3003
3004 /* If we're partially in the CPU read domain, finish moving it in.
3005 */
3006 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3007 int i;
3008
3009 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3010 if (obj_priv->page_cpu_valid[i])
3011 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003012 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003013 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003014 }
3015
3016 /* Free the page_cpu_valid mappings which are now stale, whether
3017 * or not we've got I915_GEM_DOMAIN_CPU.
3018 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003019 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003020 obj_priv->page_cpu_valid = NULL;
3021}
3022
3023/**
3024 * Set the CPU read domain on a range of the object.
3025 *
3026 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3027 * not entirely valid. The page_cpu_valid member of the object flags which
3028 * pages have been flushed, and will be respected by
3029 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3030 * of the whole object.
3031 *
3032 * This function returns when the move is complete, including waiting on
3033 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003034 */
3035static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003036i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3037 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003038{
3039 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08003040 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003041
Eric Anholte47c68e2008-11-14 13:35:19 -08003042 if (offset == 0 && size == obj->size)
3043 return i915_gem_object_set_to_cpu_domain(obj, 0);
3044
3045 i915_gem_object_flush_gpu_write_domain(obj);
3046 /* Wait on any GPU rendering and flushing to occur. */
3047 ret = i915_gem_object_wait_rendering(obj);
3048 if (ret != 0)
3049 return ret;
3050 i915_gem_object_flush_gtt_write_domain(obj);
3051
3052 /* If we're already fully in the CPU read domain, we're done. */
3053 if (obj_priv->page_cpu_valid == NULL &&
3054 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003055 return 0;
3056
Eric Anholte47c68e2008-11-14 13:35:19 -08003057 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3058 * newly adding I915_GEM_DOMAIN_CPU
3059 */
Eric Anholt673a3942008-07-30 12:06:12 -07003060 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003061 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3062 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003063 if (obj_priv->page_cpu_valid == NULL)
3064 return -ENOMEM;
3065 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3066 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003067
3068 /* Flush the cache on any pages that are still invalid from the CPU's
3069 * perspective.
3070 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003071 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3072 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003073 if (obj_priv->page_cpu_valid[i])
3074 continue;
3075
Eric Anholt856fa192009-03-19 14:10:50 -07003076 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003077
3078 obj_priv->page_cpu_valid[i] = 1;
3079 }
3080
Eric Anholte47c68e2008-11-14 13:35:19 -08003081 /* It should now be out of any other write domains, and we can update
3082 * the domain values for our changes.
3083 */
3084 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3085
3086 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3087
Eric Anholt673a3942008-07-30 12:06:12 -07003088 return 0;
3089}
3090
3091/**
Eric Anholt673a3942008-07-30 12:06:12 -07003092 * Pin an object to the GTT and evaluate the relocations landing in it.
3093 */
3094static int
3095i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3096 struct drm_file *file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003097 struct drm_i915_gem_exec_object *entry,
3098 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003099{
3100 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003101 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003102 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3103 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003104 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07003105
3106 /* Choose the GTT offset for our buffer and put it there. */
3107 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3108 if (ret)
3109 return ret;
3110
3111 entry->offset = obj_priv->gtt_offset;
3112
Eric Anholt673a3942008-07-30 12:06:12 -07003113 /* Apply the relocations, using the GTT aperture to avoid cache
3114 * flushing requirements.
3115 */
3116 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003117 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003118 struct drm_gem_object *target_obj;
3119 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003120 uint32_t reloc_val, reloc_offset;
3121 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003122
Eric Anholt673a3942008-07-30 12:06:12 -07003123 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003124 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003125 if (target_obj == NULL) {
3126 i915_gem_object_unpin(obj);
3127 return -EBADF;
3128 }
3129 target_obj_priv = target_obj->driver_private;
3130
3131 /* The target buffer should have appeared before us in the
3132 * exec_object list, so it should have a GTT space bound by now.
3133 */
3134 if (target_obj_priv->gtt_space == NULL) {
3135 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003136 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003137 drm_gem_object_unreference(target_obj);
3138 i915_gem_object_unpin(obj);
3139 return -EINVAL;
3140 }
3141
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003142 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003143 DRM_ERROR("Relocation beyond object bounds: "
3144 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003145 obj, reloc->target_handle,
3146 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003147 drm_gem_object_unreference(target_obj);
3148 i915_gem_object_unpin(obj);
3149 return -EINVAL;
3150 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003151 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003152 DRM_ERROR("Relocation not 4-byte aligned: "
3153 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003154 obj, reloc->target_handle,
3155 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003156 drm_gem_object_unreference(target_obj);
3157 i915_gem_object_unpin(obj);
3158 return -EINVAL;
3159 }
3160
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003161 if (reloc->delta >= target_obj->size) {
3162 DRM_ERROR("Relocation beyond target object bounds: "
3163 "obj %p target %d delta %d size %d.\n",
3164 obj, reloc->target_handle,
3165 (int) reloc->delta, (int) target_obj->size);
3166 drm_gem_object_unreference(target_obj);
3167 i915_gem_object_unpin(obj);
3168 return -EINVAL;
3169 }
3170
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003171 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3172 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003173 DRM_ERROR("reloc with read/write CPU domains: "
3174 "obj %p target %d offset %d "
3175 "read %08x write %08x",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003176 obj, reloc->target_handle,
3177 (int) reloc->offset,
3178 reloc->read_domains,
3179 reloc->write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00003180 drm_gem_object_unreference(target_obj);
3181 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003182 return -EINVAL;
3183 }
3184
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003185 if (reloc->write_domain && target_obj->pending_write_domain &&
3186 reloc->write_domain != target_obj->pending_write_domain) {
Eric Anholt673a3942008-07-30 12:06:12 -07003187 DRM_ERROR("Write domain conflict: "
3188 "obj %p target %d offset %d "
3189 "new %08x old %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003190 obj, reloc->target_handle,
3191 (int) reloc->offset,
3192 reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003193 target_obj->pending_write_domain);
3194 drm_gem_object_unreference(target_obj);
3195 i915_gem_object_unpin(obj);
3196 return -EINVAL;
3197 }
3198
3199#if WATCH_RELOC
3200 DRM_INFO("%s: obj %p offset %08x target %d "
3201 "read %08x write %08x gtt %08x "
3202 "presumed %08x delta %08x\n",
3203 __func__,
3204 obj,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003205 (int) reloc->offset,
3206 (int) reloc->target_handle,
3207 (int) reloc->read_domains,
3208 (int) reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003209 (int) target_obj_priv->gtt_offset,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003210 (int) reloc->presumed_offset,
3211 reloc->delta);
Eric Anholt673a3942008-07-30 12:06:12 -07003212#endif
3213
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003214 target_obj->pending_read_domains |= reloc->read_domains;
3215 target_obj->pending_write_domain |= reloc->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003216
3217 /* If the relocation already has the right value in it, no
3218 * more work needs to be done.
3219 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003220 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
Eric Anholt673a3942008-07-30 12:06:12 -07003221 drm_gem_object_unreference(target_obj);
3222 continue;
3223 }
3224
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003225 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3226 if (ret != 0) {
3227 drm_gem_object_unreference(target_obj);
3228 i915_gem_object_unpin(obj);
3229 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003230 }
3231
3232 /* Map the page containing the relocation we're going to
3233 * perform.
3234 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003235 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003236 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3237 (reloc_offset &
3238 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003239 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003240 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003241 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003242
3243#if WATCH_BUF
3244 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003245 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003246 readl(reloc_entry), reloc_val);
3247#endif
3248 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003249 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003250
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003251 /* The updated presumed offset for this entry will be
3252 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003253 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003254 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003255
3256 drm_gem_object_unreference(target_obj);
3257 }
3258
Eric Anholt673a3942008-07-30 12:06:12 -07003259#if WATCH_BUF
3260 if (0)
3261 i915_gem_dump_object(obj, 128, __func__, ~0);
3262#endif
3263 return 0;
3264}
3265
3266/** Dispatch a batchbuffer to the ring
3267 */
3268static int
3269i915_dispatch_gem_execbuffer(struct drm_device *dev,
3270 struct drm_i915_gem_execbuffer *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003271 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003272 uint64_t exec_offset)
3273{
3274 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003275 int nbox = exec->num_cliprects;
3276 int i = 0, count;
Chris Wilson83d60792009-06-06 09:45:57 +01003277 uint32_t exec_start, exec_len;
Eric Anholt673a3942008-07-30 12:06:12 -07003278 RING_LOCALS;
3279
3280 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3281 exec_len = (uint32_t) exec->batch_len;
3282
Eric Anholt673a3942008-07-30 12:06:12 -07003283 count = nbox ? nbox : 1;
3284
3285 for (i = 0; i < count; i++) {
3286 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003287 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003288 exec->DR1, exec->DR4);
3289 if (ret)
3290 return ret;
3291 }
3292
3293 if (IS_I830(dev) || IS_845G(dev)) {
3294 BEGIN_LP_RING(4);
3295 OUT_RING(MI_BATCH_BUFFER);
3296 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3297 OUT_RING(exec_start + exec_len - 4);
3298 OUT_RING(0);
3299 ADVANCE_LP_RING();
3300 } else {
3301 BEGIN_LP_RING(2);
3302 if (IS_I965G(dev)) {
3303 OUT_RING(MI_BATCH_BUFFER_START |
3304 (2 << 6) |
3305 MI_BATCH_NON_SECURE_I965);
3306 OUT_RING(exec_start);
3307 } else {
3308 OUT_RING(MI_BATCH_BUFFER_START |
3309 (2 << 6));
3310 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3311 }
3312 ADVANCE_LP_RING();
3313 }
3314 }
3315
3316 /* XXX breadcrumb */
3317 return 0;
3318}
3319
3320/* Throttle our rendering by waiting until the ring has completed our requests
3321 * emitted over 20 msec ago.
3322 *
Eric Anholtb9624422009-06-03 07:27:35 +00003323 * Note that if we were to use the current jiffies each time around the loop,
3324 * we wouldn't escape the function with any frames outstanding if the time to
3325 * render a frame was over 20ms.
3326 *
Eric Anholt673a3942008-07-30 12:06:12 -07003327 * This should get us reasonable parallelism between CPU and GPU but also
3328 * relatively low latency when blocking on a particular request to finish.
3329 */
3330static int
3331i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3332{
3333 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3334 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003335 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003336
3337 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003338 while (!list_empty(&i915_file_priv->mm.request_list)) {
3339 struct drm_i915_gem_request *request;
3340
3341 request = list_first_entry(&i915_file_priv->mm.request_list,
3342 struct drm_i915_gem_request,
3343 client_list);
3344
3345 if (time_after_eq(request->emitted_jiffies, recent_enough))
3346 break;
3347
3348 ret = i915_wait_request(dev, request->seqno);
3349 if (ret != 0)
3350 break;
3351 }
Eric Anholt673a3942008-07-30 12:06:12 -07003352 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003353
Eric Anholt673a3942008-07-30 12:06:12 -07003354 return ret;
3355}
3356
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003357static int
3358i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3359 uint32_t buffer_count,
3360 struct drm_i915_gem_relocation_entry **relocs)
3361{
3362 uint32_t reloc_count = 0, reloc_index = 0, i;
3363 int ret;
3364
3365 *relocs = NULL;
3366 for (i = 0; i < buffer_count; i++) {
3367 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3368 return -EINVAL;
3369 reloc_count += exec_list[i].relocation_count;
3370 }
3371
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003372 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003373 if (*relocs == NULL)
3374 return -ENOMEM;
3375
3376 for (i = 0; i < buffer_count; i++) {
3377 struct drm_i915_gem_relocation_entry __user *user_relocs;
3378
3379 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3380
3381 ret = copy_from_user(&(*relocs)[reloc_index],
3382 user_relocs,
3383 exec_list[i].relocation_count *
3384 sizeof(**relocs));
3385 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003386 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003387 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003388 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003389 }
3390
3391 reloc_index += exec_list[i].relocation_count;
3392 }
3393
Florian Mickler2bc43b52009-04-06 22:55:41 +02003394 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003395}
3396
3397static int
3398i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3399 uint32_t buffer_count,
3400 struct drm_i915_gem_relocation_entry *relocs)
3401{
3402 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003403 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003404
3405 for (i = 0; i < buffer_count; i++) {
3406 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003407 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003408
3409 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3410
Florian Mickler2bc43b52009-04-06 22:55:41 +02003411 unwritten = copy_to_user(user_relocs,
3412 &relocs[reloc_count],
3413 exec_list[i].relocation_count *
3414 sizeof(*relocs));
3415
3416 if (unwritten) {
3417 ret = -EFAULT;
3418 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003419 }
3420
3421 reloc_count += exec_list[i].relocation_count;
3422 }
3423
Florian Mickler2bc43b52009-04-06 22:55:41 +02003424err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003425 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003426
3427 return ret;
3428}
3429
Chris Wilson83d60792009-06-06 09:45:57 +01003430static int
3431i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer *exec,
3432 uint64_t exec_offset)
3433{
3434 uint32_t exec_start, exec_len;
3435
3436 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3437 exec_len = (uint32_t) exec->batch_len;
3438
3439 if ((exec_start | exec_len) & 0x7)
3440 return -EINVAL;
3441
3442 if (!exec_start)
3443 return -EINVAL;
3444
3445 return 0;
3446}
3447
Eric Anholt673a3942008-07-30 12:06:12 -07003448int
3449i915_gem_execbuffer(struct drm_device *dev, void *data,
3450 struct drm_file *file_priv)
3451{
3452 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003453 struct drm_i915_gem_execbuffer *args = data;
3454 struct drm_i915_gem_exec_object *exec_list = NULL;
3455 struct drm_gem_object **object_list = NULL;
3456 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003457 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003458 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003459 struct drm_i915_gem_relocation_entry *relocs;
3460 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003461 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003462 uint32_t seqno, flush_domains, reloc_index;
Keith Packardac94a962008-11-20 23:30:27 -08003463 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07003464
3465#if WATCH_EXEC
3466 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3467 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3468#endif
3469
Eric Anholt4f481ed2008-09-10 14:22:49 -07003470 if (args->buffer_count < 1) {
3471 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3472 return -EINVAL;
3473 }
Eric Anholt673a3942008-07-30 12:06:12 -07003474 /* Copy in the exec list from userland */
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003475 exec_list = drm_calloc_large(sizeof(*exec_list), args->buffer_count);
3476 object_list = drm_calloc_large(sizeof(*object_list), args->buffer_count);
Eric Anholt673a3942008-07-30 12:06:12 -07003477 if (exec_list == NULL || object_list == NULL) {
3478 DRM_ERROR("Failed to allocate exec or object list "
3479 "for %d buffers\n",
3480 args->buffer_count);
3481 ret = -ENOMEM;
3482 goto pre_mutex_err;
3483 }
3484 ret = copy_from_user(exec_list,
3485 (struct drm_i915_relocation_entry __user *)
3486 (uintptr_t) args->buffers_ptr,
3487 sizeof(*exec_list) * args->buffer_count);
3488 if (ret != 0) {
3489 DRM_ERROR("copy %d exec entries failed %d\n",
3490 args->buffer_count, ret);
3491 goto pre_mutex_err;
3492 }
3493
Eric Anholt201361a2009-03-11 12:30:04 -07003494 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003495 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3496 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -07003497 if (cliprects == NULL)
3498 goto pre_mutex_err;
3499
3500 ret = copy_from_user(cliprects,
3501 (struct drm_clip_rect __user *)
3502 (uintptr_t) args->cliprects_ptr,
3503 sizeof(*cliprects) * args->num_cliprects);
3504 if (ret != 0) {
3505 DRM_ERROR("copy %d cliprects failed: %d\n",
3506 args->num_cliprects, ret);
3507 goto pre_mutex_err;
3508 }
3509 }
3510
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003511 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3512 &relocs);
3513 if (ret != 0)
3514 goto pre_mutex_err;
3515
Eric Anholt673a3942008-07-30 12:06:12 -07003516 mutex_lock(&dev->struct_mutex);
3517
3518 i915_verify_inactive(dev, __FILE__, __LINE__);
3519
Ben Gamariba1234d2009-09-14 17:48:47 -04003520 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003521 DRM_ERROR("Execbuf while wedged\n");
3522 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003523 ret = -EIO;
3524 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003525 }
3526
3527 if (dev_priv->mm.suspended) {
3528 DRM_ERROR("Execbuf while VT-switched.\n");
3529 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003530 ret = -EBUSY;
3531 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003532 }
3533
Keith Packardac94a962008-11-20 23:30:27 -08003534 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003535 for (i = 0; i < args->buffer_count; i++) {
3536 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3537 exec_list[i].handle);
3538 if (object_list[i] == NULL) {
3539 DRM_ERROR("Invalid object handle %d at index %d\n",
3540 exec_list[i].handle, i);
3541 ret = -EBADF;
3542 goto err;
3543 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003544
3545 obj_priv = object_list[i]->driver_private;
3546 if (obj_priv->in_execbuffer) {
3547 DRM_ERROR("Object %p appears more than once in object list\n",
3548 object_list[i]);
3549 ret = -EBADF;
3550 goto err;
3551 }
3552 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003553 }
Eric Anholt673a3942008-07-30 12:06:12 -07003554
Keith Packardac94a962008-11-20 23:30:27 -08003555 /* Pin and relocate */
3556 for (pin_tries = 0; ; pin_tries++) {
3557 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003558 reloc_index = 0;
3559
Keith Packardac94a962008-11-20 23:30:27 -08003560 for (i = 0; i < args->buffer_count; i++) {
3561 object_list[i]->pending_read_domains = 0;
3562 object_list[i]->pending_write_domain = 0;
3563 ret = i915_gem_object_pin_and_relocate(object_list[i],
3564 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003565 &exec_list[i],
3566 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003567 if (ret)
3568 break;
3569 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003570 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003571 }
3572 /* success */
3573 if (ret == 0)
3574 break;
3575
3576 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003577 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003578 if (ret != -ERESTARTSYS) {
3579 unsigned long long total_size = 0;
3580 for (i = 0; i < args->buffer_count; i++)
3581 total_size += object_list[i]->size;
3582 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes: %d\n",
3583 pinned+1, args->buffer_count,
3584 total_size, ret);
3585 DRM_ERROR("%d objects [%d pinned], "
3586 "%d object bytes [%d pinned], "
3587 "%d/%d gtt bytes\n",
3588 atomic_read(&dev->object_count),
3589 atomic_read(&dev->pin_count),
3590 atomic_read(&dev->object_memory),
3591 atomic_read(&dev->pin_memory),
3592 atomic_read(&dev->gtt_memory),
3593 dev->gtt_total);
3594 }
Eric Anholt673a3942008-07-30 12:06:12 -07003595 goto err;
3596 }
Keith Packardac94a962008-11-20 23:30:27 -08003597
3598 /* unpin all of our buffers */
3599 for (i = 0; i < pinned; i++)
3600 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003601 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003602
3603 /* evict everyone we can from the aperture */
3604 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003605 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003606 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003607 }
3608
3609 /* Set the pending read domains for the batch buffer to COMMAND */
3610 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003611 if (batch_obj->pending_write_domain) {
3612 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3613 ret = -EINVAL;
3614 goto err;
3615 }
3616 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003617
Chris Wilson83d60792009-06-06 09:45:57 +01003618 /* Sanity check the batch buffer, prior to moving objects */
3619 exec_offset = exec_list[args->buffer_count - 1].offset;
3620 ret = i915_gem_check_execbuffer (args, exec_offset);
3621 if (ret != 0) {
3622 DRM_ERROR("execbuf with invalid offset/length\n");
3623 goto err;
3624 }
3625
Eric Anholt673a3942008-07-30 12:06:12 -07003626 i915_verify_inactive(dev, __FILE__, __LINE__);
3627
Keith Packard646f0f62008-11-20 23:23:03 -08003628 /* Zero the global flush/invalidate flags. These
3629 * will be modified as new domains are computed
3630 * for each object
3631 */
3632 dev->invalidate_domains = 0;
3633 dev->flush_domains = 0;
3634
Eric Anholt673a3942008-07-30 12:06:12 -07003635 for (i = 0; i < args->buffer_count; i++) {
3636 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003637
Keith Packard646f0f62008-11-20 23:23:03 -08003638 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003639 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003640 }
3641
3642 i915_verify_inactive(dev, __FILE__, __LINE__);
3643
Keith Packard646f0f62008-11-20 23:23:03 -08003644 if (dev->invalidate_domains | dev->flush_domains) {
3645#if WATCH_EXEC
3646 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3647 __func__,
3648 dev->invalidate_domains,
3649 dev->flush_domains);
3650#endif
3651 i915_gem_flush(dev,
3652 dev->invalidate_domains,
3653 dev->flush_domains);
3654 if (dev->flush_domains)
Eric Anholtb9624422009-06-03 07:27:35 +00003655 (void)i915_add_request(dev, file_priv,
3656 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003657 }
Eric Anholt673a3942008-07-30 12:06:12 -07003658
Eric Anholtefbeed92009-02-19 14:54:51 -08003659 for (i = 0; i < args->buffer_count; i++) {
3660 struct drm_gem_object *obj = object_list[i];
3661
3662 obj->write_domain = obj->pending_write_domain;
3663 }
3664
Eric Anholt673a3942008-07-30 12:06:12 -07003665 i915_verify_inactive(dev, __FILE__, __LINE__);
3666
3667#if WATCH_COHERENCY
3668 for (i = 0; i < args->buffer_count; i++) {
3669 i915_gem_object_check_coherency(object_list[i],
3670 exec_list[i].handle);
3671 }
3672#endif
3673
Eric Anholt673a3942008-07-30 12:06:12 -07003674#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003675 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003676 args->batch_len,
3677 __func__,
3678 ~0);
3679#endif
3680
Eric Anholt673a3942008-07-30 12:06:12 -07003681 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003682 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003683 if (ret) {
3684 DRM_ERROR("dispatch failed %d\n", ret);
3685 goto err;
3686 }
3687
3688 /*
3689 * Ensure that the commands in the batch buffer are
3690 * finished before the interrupt fires
3691 */
3692 flush_domains = i915_retire_commands(dev);
3693
3694 i915_verify_inactive(dev, __FILE__, __LINE__);
3695
3696 /*
3697 * Get a seqno representing the execution of the current buffer,
3698 * which we can wait on. We would like to mitigate these interrupts,
3699 * likely by only creating seqnos occasionally (so that we have
3700 * *some* interrupts representing completion of buffers that we can
3701 * wait on when trying to clear up gtt space).
3702 */
Eric Anholtb9624422009-06-03 07:27:35 +00003703 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003704 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003705 for (i = 0; i < args->buffer_count; i++) {
3706 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003707
Eric Anholtce44b0e2008-11-06 16:00:31 -08003708 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003709#if WATCH_LRU
3710 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3711#endif
3712 }
3713#if WATCH_LRU
3714 i915_dump_lru(dev, __func__);
3715#endif
3716
3717 i915_verify_inactive(dev, __FILE__, __LINE__);
3718
Eric Anholt673a3942008-07-30 12:06:12 -07003719err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003720 for (i = 0; i < pinned; i++)
3721 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003722
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003723 for (i = 0; i < args->buffer_count; i++) {
3724 if (object_list[i]) {
3725 obj_priv = object_list[i]->driver_private;
3726 obj_priv->in_execbuffer = false;
3727 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003728 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003729 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003730
Eric Anholt673a3942008-07-30 12:06:12 -07003731 mutex_unlock(&dev->struct_mutex);
3732
Roland Dreiera35f2e22009-02-06 17:48:09 -08003733 if (!ret) {
3734 /* Copy the new buffer offsets back to the user's exec list. */
3735 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3736 (uintptr_t) args->buffers_ptr,
3737 exec_list,
3738 sizeof(*exec_list) * args->buffer_count);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003739 if (ret) {
3740 ret = -EFAULT;
Roland Dreiera35f2e22009-02-06 17:48:09 -08003741 DRM_ERROR("failed to copy %d exec entries "
3742 "back to user (%d)\n",
3743 args->buffer_count, ret);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003744 }
Roland Dreiera35f2e22009-02-06 17:48:09 -08003745 }
3746
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003747 /* Copy the updated relocations out regardless of current error
3748 * state. Failure to update the relocs would mean that the next
3749 * time userland calls execbuf, it would do so with presumed offset
3750 * state that didn't match the actual object state.
3751 */
3752 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3753 relocs);
3754 if (ret2 != 0) {
3755 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3756
3757 if (ret == 0)
3758 ret = ret2;
3759 }
3760
Eric Anholt673a3942008-07-30 12:06:12 -07003761pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003762 drm_free_large(object_list);
3763 drm_free_large(exec_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003764 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003765
3766 return ret;
3767}
3768
3769int
3770i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3771{
3772 struct drm_device *dev = obj->dev;
3773 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3774 int ret;
3775
3776 i915_verify_inactive(dev, __FILE__, __LINE__);
3777 if (obj_priv->gtt_space == NULL) {
3778 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3779 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003780 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05003781 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003782 return ret;
3783 }
Chris Wilson22c344e2009-02-11 14:26:45 +00003784 }
3785 /*
3786 * Pre-965 chips need a fence register set up in order to
3787 * properly handle tiled surfaces.
3788 */
Eric Anholta09ba7f2009-08-29 12:49:51 -07003789 if (!IS_I965G(dev) && obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003790 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson22c344e2009-02-11 14:26:45 +00003791 if (ret != 0) {
3792 if (ret != -EBUSY && ret != -ERESTARTSYS)
3793 DRM_ERROR("Failure to install fence: %d\n",
3794 ret);
3795 return ret;
3796 }
Eric Anholt673a3942008-07-30 12:06:12 -07003797 }
3798 obj_priv->pin_count++;
3799
3800 /* If the object is not active and not pending a flush,
3801 * remove it from the inactive list
3802 */
3803 if (obj_priv->pin_count == 1) {
3804 atomic_inc(&dev->pin_count);
3805 atomic_add(obj->size, &dev->pin_memory);
3806 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003807 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07003808 !list_empty(&obj_priv->list))
3809 list_del_init(&obj_priv->list);
3810 }
3811 i915_verify_inactive(dev, __FILE__, __LINE__);
3812
3813 return 0;
3814}
3815
3816void
3817i915_gem_object_unpin(struct drm_gem_object *obj)
3818{
3819 struct drm_device *dev = obj->dev;
3820 drm_i915_private_t *dev_priv = dev->dev_private;
3821 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3822
3823 i915_verify_inactive(dev, __FILE__, __LINE__);
3824 obj_priv->pin_count--;
3825 BUG_ON(obj_priv->pin_count < 0);
3826 BUG_ON(obj_priv->gtt_space == NULL);
3827
3828 /* If the object is no longer pinned, and is
3829 * neither active nor being flushed, then stick it on
3830 * the inactive list
3831 */
3832 if (obj_priv->pin_count == 0) {
3833 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003834 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003835 list_move_tail(&obj_priv->list,
3836 &dev_priv->mm.inactive_list);
3837 atomic_dec(&dev->pin_count);
3838 atomic_sub(obj->size, &dev->pin_memory);
3839 }
3840 i915_verify_inactive(dev, __FILE__, __LINE__);
3841}
3842
3843int
3844i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3845 struct drm_file *file_priv)
3846{
3847 struct drm_i915_gem_pin *args = data;
3848 struct drm_gem_object *obj;
3849 struct drm_i915_gem_object *obj_priv;
3850 int ret;
3851
3852 mutex_lock(&dev->struct_mutex);
3853
3854 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3855 if (obj == NULL) {
3856 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3857 args->handle);
3858 mutex_unlock(&dev->struct_mutex);
3859 return -EBADF;
3860 }
3861 obj_priv = obj->driver_private;
3862
Chris Wilson3ef94da2009-09-14 16:50:29 +01003863 if (obj_priv->madv == I915_MADV_DONTNEED) {
3864 DRM_ERROR("Attempting to pin a I915_MADV_DONTNEED buffer\n");
3865 drm_gem_object_unreference(obj);
3866 mutex_unlock(&dev->struct_mutex);
3867 return -EINVAL;
3868 }
3869
Jesse Barnes79e53942008-11-07 14:24:08 -08003870 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3871 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3872 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00003873 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003874 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003875 return -EINVAL;
3876 }
3877
3878 obj_priv->user_pin_count++;
3879 obj_priv->pin_filp = file_priv;
3880 if (obj_priv->user_pin_count == 1) {
3881 ret = i915_gem_object_pin(obj, args->alignment);
3882 if (ret != 0) {
3883 drm_gem_object_unreference(obj);
3884 mutex_unlock(&dev->struct_mutex);
3885 return ret;
3886 }
Eric Anholt673a3942008-07-30 12:06:12 -07003887 }
3888
3889 /* XXX - flush the CPU caches for pinned objects
3890 * as the X server doesn't manage domains yet
3891 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003892 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003893 args->offset = obj_priv->gtt_offset;
3894 drm_gem_object_unreference(obj);
3895 mutex_unlock(&dev->struct_mutex);
3896
3897 return 0;
3898}
3899
3900int
3901i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3902 struct drm_file *file_priv)
3903{
3904 struct drm_i915_gem_pin *args = data;
3905 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08003906 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003907
3908 mutex_lock(&dev->struct_mutex);
3909
3910 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3911 if (obj == NULL) {
3912 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3913 args->handle);
3914 mutex_unlock(&dev->struct_mutex);
3915 return -EBADF;
3916 }
3917
Jesse Barnes79e53942008-11-07 14:24:08 -08003918 obj_priv = obj->driver_private;
3919 if (obj_priv->pin_filp != file_priv) {
3920 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3921 args->handle);
3922 drm_gem_object_unreference(obj);
3923 mutex_unlock(&dev->struct_mutex);
3924 return -EINVAL;
3925 }
3926 obj_priv->user_pin_count--;
3927 if (obj_priv->user_pin_count == 0) {
3928 obj_priv->pin_filp = NULL;
3929 i915_gem_object_unpin(obj);
3930 }
Eric Anholt673a3942008-07-30 12:06:12 -07003931
3932 drm_gem_object_unreference(obj);
3933 mutex_unlock(&dev->struct_mutex);
3934 return 0;
3935}
3936
3937int
3938i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3939 struct drm_file *file_priv)
3940{
3941 struct drm_i915_gem_busy *args = data;
3942 struct drm_gem_object *obj;
3943 struct drm_i915_gem_object *obj_priv;
3944
Eric Anholt673a3942008-07-30 12:06:12 -07003945 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3946 if (obj == NULL) {
3947 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3948 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003949 return -EBADF;
3950 }
3951
Chris Wilsonb1ce7862009-06-06 09:46:00 +01003952 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08003953 /* Update the active list for the hardware's current position.
3954 * Otherwise this only updates on a delayed timer or when irqs are
3955 * actually unmasked, and our working set ends up being larger than
3956 * required.
3957 */
3958 i915_gem_retire_requests(dev);
3959
Eric Anholt673a3942008-07-30 12:06:12 -07003960 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08003961 /* Don't count being on the flushing list against the object being
3962 * done. Otherwise, a buffer left on the flushing list but not getting
3963 * flushed (because nobody's flushing that domain) won't ever return
3964 * unbusy and get reused by libdrm's bo cache. The other expected
3965 * consumer of this interface, OpenGL's occlusion queries, also specs
3966 * that the objects get unbusy "eventually" without any interference.
3967 */
3968 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003969
3970 drm_gem_object_unreference(obj);
3971 mutex_unlock(&dev->struct_mutex);
3972 return 0;
3973}
3974
3975int
3976i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3977 struct drm_file *file_priv)
3978{
3979 return i915_gem_ring_throttle(dev, file_priv);
3980}
3981
Chris Wilson3ef94da2009-09-14 16:50:29 +01003982int
3983i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3984 struct drm_file *file_priv)
3985{
3986 struct drm_i915_gem_madvise *args = data;
3987 struct drm_gem_object *obj;
3988 struct drm_i915_gem_object *obj_priv;
3989
3990 switch (args->madv) {
3991 case I915_MADV_DONTNEED:
3992 case I915_MADV_WILLNEED:
3993 break;
3994 default:
3995 return -EINVAL;
3996 }
3997
3998 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3999 if (obj == NULL) {
4000 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4001 args->handle);
4002 return -EBADF;
4003 }
4004
4005 mutex_lock(&dev->struct_mutex);
4006 obj_priv = obj->driver_private;
4007
4008 if (obj_priv->pin_count) {
4009 drm_gem_object_unreference(obj);
4010 mutex_unlock(&dev->struct_mutex);
4011
4012 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4013 return -EINVAL;
4014 }
4015
4016 obj_priv->madv = args->madv;
4017 args->retained = obj_priv->gtt_space != NULL;
4018
4019 drm_gem_object_unreference(obj);
4020 mutex_unlock(&dev->struct_mutex);
4021
4022 return 0;
4023}
4024
Eric Anholt673a3942008-07-30 12:06:12 -07004025int i915_gem_init_object(struct drm_gem_object *obj)
4026{
4027 struct drm_i915_gem_object *obj_priv;
4028
Eric Anholt9a298b22009-03-24 12:23:04 -07004029 obj_priv = kzalloc(sizeof(*obj_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07004030 if (obj_priv == NULL)
4031 return -ENOMEM;
4032
4033 /*
4034 * We've just allocated pages from the kernel,
4035 * so they've just been written by the CPU with
4036 * zeros. They'll need to be clflushed before we
4037 * use them with the GPU.
4038 */
4039 obj->write_domain = I915_GEM_DOMAIN_CPU;
4040 obj->read_domains = I915_GEM_DOMAIN_CPU;
4041
Keith Packardba1eb1d2008-10-14 19:55:10 -07004042 obj_priv->agp_type = AGP_USER_MEMORY;
4043
Eric Anholt673a3942008-07-30 12:06:12 -07004044 obj->driver_private = obj_priv;
4045 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004046 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07004047 INIT_LIST_HEAD(&obj_priv->list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004048 INIT_LIST_HEAD(&obj_priv->fence_list);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004049 obj_priv->madv = I915_MADV_WILLNEED;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004050
Eric Anholt673a3942008-07-30 12:06:12 -07004051 return 0;
4052}
4053
4054void i915_gem_free_object(struct drm_gem_object *obj)
4055{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004056 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07004057 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4058
4059 while (obj_priv->pin_count > 0)
4060 i915_gem_object_unpin(obj);
4061
Dave Airlie71acb5e2008-12-30 20:31:46 +10004062 if (obj_priv->phys_obj)
4063 i915_gem_detach_phys_object(dev, obj);
4064
Eric Anholt673a3942008-07-30 12:06:12 -07004065 i915_gem_object_unbind(obj);
4066
Chris Wilson7e616152009-09-10 08:53:04 +01004067 if (obj_priv->mmap_offset)
4068 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08004069
Eric Anholt9a298b22009-03-24 12:23:04 -07004070 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07004071 kfree(obj_priv->bit_17);
Eric Anholt9a298b22009-03-24 12:23:04 -07004072 kfree(obj->driver_private);
Eric Anholt673a3942008-07-30 12:06:12 -07004073}
4074
Eric Anholt673a3942008-07-30 12:06:12 -07004075/** Unbinds all objects that are on the given buffer list. */
4076static int
4077i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
4078{
4079 struct drm_gem_object *obj;
4080 struct drm_i915_gem_object *obj_priv;
4081 int ret;
4082
4083 while (!list_empty(head)) {
4084 obj_priv = list_first_entry(head,
4085 struct drm_i915_gem_object,
4086 list);
4087 obj = obj_priv->obj;
4088
4089 if (obj_priv->pin_count != 0) {
4090 DRM_ERROR("Pinned object in unbind list\n");
4091 mutex_unlock(&dev->struct_mutex);
4092 return -EINVAL;
4093 }
4094
4095 ret = i915_gem_object_unbind(obj);
4096 if (ret != 0) {
4097 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
4098 ret);
4099 mutex_unlock(&dev->struct_mutex);
4100 return ret;
4101 }
4102 }
4103
4104
4105 return 0;
4106}
4107
Jesse Barnes5669fca2009-02-17 15:13:31 -08004108int
Eric Anholt673a3942008-07-30 12:06:12 -07004109i915_gem_idle(struct drm_device *dev)
4110{
4111 drm_i915_private_t *dev_priv = dev->dev_private;
4112 uint32_t seqno, cur_seqno, last_seqno;
4113 int stuck, ret;
4114
Keith Packard6dbe2772008-10-14 21:41:13 -07004115 mutex_lock(&dev->struct_mutex);
4116
4117 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
4118 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004119 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004120 }
Eric Anholt673a3942008-07-30 12:06:12 -07004121
4122 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4123 * We need to replace this with a semaphore, or something.
4124 */
4125 dev_priv->mm.suspended = 1;
Ben Gamarif65d9422009-09-14 17:48:44 -04004126 del_timer(&dev_priv->hangcheck_timer);
Eric Anholt673a3942008-07-30 12:06:12 -07004127
Keith Packard6dbe2772008-10-14 21:41:13 -07004128 /* Cancel the retire work handler, wait for it to finish if running
4129 */
4130 mutex_unlock(&dev->struct_mutex);
4131 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4132 mutex_lock(&dev->struct_mutex);
4133
Eric Anholt673a3942008-07-30 12:06:12 -07004134 i915_kernel_lost_context(dev);
4135
4136 /* Flush the GPU along with all non-CPU write domains
4137 */
Chris Wilson21d509e2009-06-06 09:46:02 +01004138 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
4139 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07004140
4141 if (seqno == 0) {
4142 mutex_unlock(&dev->struct_mutex);
4143 return -ENOMEM;
4144 }
4145
4146 dev_priv->mm.waiting_gem_seqno = seqno;
4147 last_seqno = 0;
4148 stuck = 0;
4149 for (;;) {
4150 cur_seqno = i915_get_gem_seqno(dev);
4151 if (i915_seqno_passed(cur_seqno, seqno))
4152 break;
4153 if (last_seqno == cur_seqno) {
4154 if (stuck++ > 100) {
4155 DRM_ERROR("hardware wedged\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004156 atomic_set(&dev_priv->mm.wedged, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07004157 DRM_WAKEUP(&dev_priv->irq_queue);
4158 break;
4159 }
4160 }
4161 msleep(10);
4162 last_seqno = cur_seqno;
4163 }
4164 dev_priv->mm.waiting_gem_seqno = 0;
4165
4166 i915_gem_retire_requests(dev);
4167
Carl Worth5e118f42009-03-20 11:54:25 -07004168 spin_lock(&dev_priv->mm.active_list_lock);
Ben Gamariba1234d2009-09-14 17:48:47 -04004169 if (!atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt28dfe522008-11-13 15:00:55 -08004170 /* Active and flushing should now be empty as we've
4171 * waited for a sequence higher than any pending execbuffer
4172 */
4173 WARN_ON(!list_empty(&dev_priv->mm.active_list));
4174 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
4175 /* Request should now be empty as we've also waited
4176 * for the last request in the list
4177 */
4178 WARN_ON(!list_empty(&dev_priv->mm.request_list));
4179 }
Eric Anholt673a3942008-07-30 12:06:12 -07004180
Eric Anholt28dfe522008-11-13 15:00:55 -08004181 /* Empty the active and flushing lists to inactive. If there's
4182 * anything left at this point, it means that we're wedged and
4183 * nothing good's going to happen by leaving them there. So strip
4184 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07004185 */
Eric Anholt28dfe522008-11-13 15:00:55 -08004186 while (!list_empty(&dev_priv->mm.active_list)) {
4187 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004188
Eric Anholt28dfe522008-11-13 15:00:55 -08004189 obj_priv = list_first_entry(&dev_priv->mm.active_list,
4190 struct drm_i915_gem_object,
4191 list);
4192 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
4193 i915_gem_object_move_to_inactive(obj_priv->obj);
4194 }
Carl Worth5e118f42009-03-20 11:54:25 -07004195 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08004196
4197 while (!list_empty(&dev_priv->mm.flushing_list)) {
4198 struct drm_i915_gem_object *obj_priv;
4199
Eric Anholt151903d2008-12-01 10:23:21 +10004200 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08004201 struct drm_i915_gem_object,
4202 list);
4203 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
4204 i915_gem_object_move_to_inactive(obj_priv->obj);
4205 }
4206
4207
4208 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07004209 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08004210 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07004211 if (ret) {
4212 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004213 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004214 }
Eric Anholt673a3942008-07-30 12:06:12 -07004215
Keith Packard6dbe2772008-10-14 21:41:13 -07004216 i915_gem_cleanup_ringbuffer(dev);
4217 mutex_unlock(&dev->struct_mutex);
4218
Eric Anholt673a3942008-07-30 12:06:12 -07004219 return 0;
4220}
4221
4222static int
4223i915_gem_init_hws(struct drm_device *dev)
4224{
4225 drm_i915_private_t *dev_priv = dev->dev_private;
4226 struct drm_gem_object *obj;
4227 struct drm_i915_gem_object *obj_priv;
4228 int ret;
4229
4230 /* If we need a physical address for the status page, it's already
4231 * initialized at driver load time.
4232 */
4233 if (!I915_NEED_GFX_HWS(dev))
4234 return 0;
4235
4236 obj = drm_gem_object_alloc(dev, 4096);
4237 if (obj == NULL) {
4238 DRM_ERROR("Failed to allocate status page\n");
4239 return -ENOMEM;
4240 }
4241 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07004242 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07004243
4244 ret = i915_gem_object_pin(obj, 4096);
4245 if (ret != 0) {
4246 drm_gem_object_unreference(obj);
4247 return ret;
4248 }
4249
4250 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07004251
Eric Anholt856fa192009-03-19 14:10:50 -07004252 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004253 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004254 DRM_ERROR("Failed to map status page.\n");
4255 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00004256 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004257 drm_gem_object_unreference(obj);
4258 return -EINVAL;
4259 }
4260 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004261 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
4262 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004263 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07004264 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
4265
4266 return 0;
4267}
4268
Chris Wilson85a7bb92009-02-11 14:52:44 +00004269static void
4270i915_gem_cleanup_hws(struct drm_device *dev)
4271{
4272 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004273 struct drm_gem_object *obj;
4274 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00004275
4276 if (dev_priv->hws_obj == NULL)
4277 return;
4278
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004279 obj = dev_priv->hws_obj;
4280 obj_priv = obj->driver_private;
4281
Eric Anholt856fa192009-03-19 14:10:50 -07004282 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004283 i915_gem_object_unpin(obj);
4284 drm_gem_object_unreference(obj);
4285 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004286
Chris Wilson85a7bb92009-02-11 14:52:44 +00004287 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4288 dev_priv->hw_status_page = NULL;
4289
4290 /* Write high address into HWS_PGA when disabling. */
4291 I915_WRITE(HWS_PGA, 0x1ffff000);
4292}
4293
Jesse Barnes79e53942008-11-07 14:24:08 -08004294int
Eric Anholt673a3942008-07-30 12:06:12 -07004295i915_gem_init_ringbuffer(struct drm_device *dev)
4296{
4297 drm_i915_private_t *dev_priv = dev->dev_private;
4298 struct drm_gem_object *obj;
4299 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08004300 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07004301 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07004302 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07004303
4304 ret = i915_gem_init_hws(dev);
4305 if (ret != 0)
4306 return ret;
4307
4308 obj = drm_gem_object_alloc(dev, 128 * 1024);
4309 if (obj == NULL) {
4310 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004311 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004312 return -ENOMEM;
4313 }
4314 obj_priv = obj->driver_private;
4315
4316 ret = i915_gem_object_pin(obj, 4096);
4317 if (ret != 0) {
4318 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004319 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004320 return ret;
4321 }
4322
4323 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004324 ring->Size = obj->size;
Eric Anholt673a3942008-07-30 12:06:12 -07004325
Jesse Barnes79e53942008-11-07 14:24:08 -08004326 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4327 ring->map.size = obj->size;
4328 ring->map.type = 0;
4329 ring->map.flags = 0;
4330 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004331
Jesse Barnes79e53942008-11-07 14:24:08 -08004332 drm_core_ioremap_wc(&ring->map, dev);
4333 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004334 DRM_ERROR("Failed to map ringbuffer.\n");
4335 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004336 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004337 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004338 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004339 return -EINVAL;
4340 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004341 ring->ring_obj = obj;
4342 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004343
4344 /* Stop the ring if it's running. */
4345 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004346 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07004347 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004348
4349 /* Initialize the ring. */
4350 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07004351 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4352
4353 /* G45 ring initialization fails to reset head to zero */
4354 if (head != 0) {
4355 DRM_ERROR("Ring head not reset to zero "
4356 "ctl %08x head %08x tail %08x start %08x\n",
4357 I915_READ(PRB0_CTL),
4358 I915_READ(PRB0_HEAD),
4359 I915_READ(PRB0_TAIL),
4360 I915_READ(PRB0_START));
4361 I915_WRITE(PRB0_HEAD, 0);
4362
4363 DRM_ERROR("Ring head forced to zero "
4364 "ctl %08x head %08x tail %08x start %08x\n",
4365 I915_READ(PRB0_CTL),
4366 I915_READ(PRB0_HEAD),
4367 I915_READ(PRB0_TAIL),
4368 I915_READ(PRB0_START));
4369 }
4370
Eric Anholt673a3942008-07-30 12:06:12 -07004371 I915_WRITE(PRB0_CTL,
4372 ((obj->size - 4096) & RING_NR_PAGES) |
4373 RING_NO_REPORT |
4374 RING_VALID);
4375
Keith Packard50aa253d2008-10-14 17:20:35 -07004376 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4377
4378 /* If the head is still not zero, the ring is dead */
4379 if (head != 0) {
4380 DRM_ERROR("Ring initialization failed "
4381 "ctl %08x head %08x tail %08x start %08x\n",
4382 I915_READ(PRB0_CTL),
4383 I915_READ(PRB0_HEAD),
4384 I915_READ(PRB0_TAIL),
4385 I915_READ(PRB0_START));
4386 return -EIO;
4387 }
4388
Eric Anholt673a3942008-07-30 12:06:12 -07004389 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004390 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4391 i915_kernel_lost_context(dev);
4392 else {
4393 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4394 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4395 ring->space = ring->head - (ring->tail + 8);
4396 if (ring->space < 0)
4397 ring->space += ring->Size;
4398 }
Eric Anholt673a3942008-07-30 12:06:12 -07004399
4400 return 0;
4401}
4402
Jesse Barnes79e53942008-11-07 14:24:08 -08004403void
Eric Anholt673a3942008-07-30 12:06:12 -07004404i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4405{
4406 drm_i915_private_t *dev_priv = dev->dev_private;
4407
4408 if (dev_priv->ring.ring_obj == NULL)
4409 return;
4410
4411 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4412
4413 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4414 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4415 dev_priv->ring.ring_obj = NULL;
4416 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4417
Chris Wilson85a7bb92009-02-11 14:52:44 +00004418 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004419}
4420
4421int
4422i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4423 struct drm_file *file_priv)
4424{
4425 drm_i915_private_t *dev_priv = dev->dev_private;
4426 int ret;
4427
Jesse Barnes79e53942008-11-07 14:24:08 -08004428 if (drm_core_check_feature(dev, DRIVER_MODESET))
4429 return 0;
4430
Ben Gamariba1234d2009-09-14 17:48:47 -04004431 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004432 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004433 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004434 }
4435
Eric Anholt673a3942008-07-30 12:06:12 -07004436 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004437 dev_priv->mm.suspended = 0;
4438
4439 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004440 if (ret != 0) {
4441 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004442 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004443 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004444
Carl Worth5e118f42009-03-20 11:54:25 -07004445 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004446 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004447 spin_unlock(&dev_priv->mm.active_list_lock);
4448
Eric Anholt673a3942008-07-30 12:06:12 -07004449 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4450 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4451 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004452 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004453
4454 drm_irq_install(dev);
4455
Eric Anholt673a3942008-07-30 12:06:12 -07004456 return 0;
4457}
4458
4459int
4460i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4461 struct drm_file *file_priv)
4462{
4463 int ret;
4464
Jesse Barnes79e53942008-11-07 14:24:08 -08004465 if (drm_core_check_feature(dev, DRIVER_MODESET))
4466 return 0;
4467
Eric Anholt673a3942008-07-30 12:06:12 -07004468 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004469 drm_irq_uninstall(dev);
4470
Keith Packard6dbe2772008-10-14 21:41:13 -07004471 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004472}
4473
4474void
4475i915_gem_lastclose(struct drm_device *dev)
4476{
4477 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004478
Eric Anholte806b492009-01-22 09:56:58 -08004479 if (drm_core_check_feature(dev, DRIVER_MODESET))
4480 return;
4481
Keith Packard6dbe2772008-10-14 21:41:13 -07004482 ret = i915_gem_idle(dev);
4483 if (ret)
4484 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004485}
4486
4487void
4488i915_gem_load(struct drm_device *dev)
4489{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004490 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004491 drm_i915_private_t *dev_priv = dev->dev_private;
4492
Carl Worth5e118f42009-03-20 11:54:25 -07004493 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004494 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4495 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4496 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4497 INIT_LIST_HEAD(&dev_priv->mm.request_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004498 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004499 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4500 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004501 dev_priv->mm.next_gem_seqno = 1;
4502
Chris Wilson31169712009-09-14 16:50:28 +01004503 spin_lock(&shrink_list_lock);
4504 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4505 spin_unlock(&shrink_list_lock);
4506
Jesse Barnesde151cf2008-11-12 10:03:55 -08004507 /* Old X drivers will take 0-2 for front, back, depth buffers */
4508 dev_priv->fence_reg_start = 3;
4509
Jesse Barnes0f973f22009-01-26 17:10:45 -08004510 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004511 dev_priv->num_fence_regs = 16;
4512 else
4513 dev_priv->num_fence_regs = 8;
4514
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004515 /* Initialize fence registers to zero */
4516 if (IS_I965G(dev)) {
4517 for (i = 0; i < 16; i++)
4518 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4519 } else {
4520 for (i = 0; i < 8; i++)
4521 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4522 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4523 for (i = 0; i < 8; i++)
4524 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4525 }
4526
Eric Anholt673a3942008-07-30 12:06:12 -07004527 i915_gem_detect_bit_6_swizzle(dev);
4528}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004529
4530/*
4531 * Create a physically contiguous memory object for this object
4532 * e.g. for cursor + overlay regs
4533 */
4534int i915_gem_init_phys_object(struct drm_device *dev,
4535 int id, int size)
4536{
4537 drm_i915_private_t *dev_priv = dev->dev_private;
4538 struct drm_i915_gem_phys_object *phys_obj;
4539 int ret;
4540
4541 if (dev_priv->mm.phys_objs[id - 1] || !size)
4542 return 0;
4543
Eric Anholt9a298b22009-03-24 12:23:04 -07004544 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004545 if (!phys_obj)
4546 return -ENOMEM;
4547
4548 phys_obj->id = id;
4549
4550 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4551 if (!phys_obj->handle) {
4552 ret = -ENOMEM;
4553 goto kfree_obj;
4554 }
4555#ifdef CONFIG_X86
4556 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4557#endif
4558
4559 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4560
4561 return 0;
4562kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004563 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004564 return ret;
4565}
4566
4567void i915_gem_free_phys_object(struct drm_device *dev, int id)
4568{
4569 drm_i915_private_t *dev_priv = dev->dev_private;
4570 struct drm_i915_gem_phys_object *phys_obj;
4571
4572 if (!dev_priv->mm.phys_objs[id - 1])
4573 return;
4574
4575 phys_obj = dev_priv->mm.phys_objs[id - 1];
4576 if (phys_obj->cur_obj) {
4577 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4578 }
4579
4580#ifdef CONFIG_X86
4581 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4582#endif
4583 drm_pci_free(dev, phys_obj->handle);
4584 kfree(phys_obj);
4585 dev_priv->mm.phys_objs[id - 1] = NULL;
4586}
4587
4588void i915_gem_free_all_phys_object(struct drm_device *dev)
4589{
4590 int i;
4591
Dave Airlie260883c2009-01-22 17:58:49 +10004592 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004593 i915_gem_free_phys_object(dev, i);
4594}
4595
4596void i915_gem_detach_phys_object(struct drm_device *dev,
4597 struct drm_gem_object *obj)
4598{
4599 struct drm_i915_gem_object *obj_priv;
4600 int i;
4601 int ret;
4602 int page_count;
4603
4604 obj_priv = obj->driver_private;
4605 if (!obj_priv->phys_obj)
4606 return;
4607
Eric Anholt856fa192009-03-19 14:10:50 -07004608 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004609 if (ret)
4610 goto out;
4611
4612 page_count = obj->size / PAGE_SIZE;
4613
4614 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004615 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004616 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4617
4618 memcpy(dst, src, PAGE_SIZE);
4619 kunmap_atomic(dst, KM_USER0);
4620 }
Eric Anholt856fa192009-03-19 14:10:50 -07004621 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004622 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004623
4624 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004625out:
4626 obj_priv->phys_obj->cur_obj = NULL;
4627 obj_priv->phys_obj = NULL;
4628}
4629
4630int
4631i915_gem_attach_phys_object(struct drm_device *dev,
4632 struct drm_gem_object *obj, int id)
4633{
4634 drm_i915_private_t *dev_priv = dev->dev_private;
4635 struct drm_i915_gem_object *obj_priv;
4636 int ret = 0;
4637 int page_count;
4638 int i;
4639
4640 if (id > I915_MAX_PHYS_OBJECT)
4641 return -EINVAL;
4642
4643 obj_priv = obj->driver_private;
4644
4645 if (obj_priv->phys_obj) {
4646 if (obj_priv->phys_obj->id == id)
4647 return 0;
4648 i915_gem_detach_phys_object(dev, obj);
4649 }
4650
4651
4652 /* create a new object */
4653 if (!dev_priv->mm.phys_objs[id - 1]) {
4654 ret = i915_gem_init_phys_object(dev, id,
4655 obj->size);
4656 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004657 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004658 goto out;
4659 }
4660 }
4661
4662 /* bind to the object */
4663 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4664 obj_priv->phys_obj->cur_obj = obj;
4665
Eric Anholt856fa192009-03-19 14:10:50 -07004666 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004667 if (ret) {
4668 DRM_ERROR("failed to get page list\n");
4669 goto out;
4670 }
4671
4672 page_count = obj->size / PAGE_SIZE;
4673
4674 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004675 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004676 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4677
4678 memcpy(dst, src, PAGE_SIZE);
4679 kunmap_atomic(src, KM_USER0);
4680 }
4681
Chris Wilsond78b47b2009-06-17 21:52:49 +01004682 i915_gem_object_put_pages(obj);
4683
Dave Airlie71acb5e2008-12-30 20:31:46 +10004684 return 0;
4685out:
4686 return ret;
4687}
4688
4689static int
4690i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4691 struct drm_i915_gem_pwrite *args,
4692 struct drm_file *file_priv)
4693{
4694 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4695 void *obj_addr;
4696 int ret;
4697 char __user *user_data;
4698
4699 user_data = (char __user *) (uintptr_t) args->data_ptr;
4700 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4701
Dave Airliee08fb4f2009-02-25 14:52:30 +10004702 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004703 ret = copy_from_user(obj_addr, user_data, args->size);
4704 if (ret)
4705 return -EFAULT;
4706
4707 drm_agp_chipset_flush(dev);
4708 return 0;
4709}
Eric Anholtb9624422009-06-03 07:27:35 +00004710
4711void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4712{
4713 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4714
4715 /* Clean up our request list when the client is going away, so that
4716 * later retire_requests won't dereference our soon-to-be-gone
4717 * file_priv.
4718 */
4719 mutex_lock(&dev->struct_mutex);
4720 while (!list_empty(&i915_file_priv->mm.request_list))
4721 list_del_init(i915_file_priv->mm.request_list.next);
4722 mutex_unlock(&dev->struct_mutex);
4723}
Chris Wilson31169712009-09-14 16:50:28 +01004724
4725/* Immediately discard the backing storage */
4726static void
4727i915_gem_object_truncate(struct drm_gem_object *obj)
4728{
4729 struct inode *inode;
4730
4731 inode = obj->filp->f_path.dentry->d_inode;
4732
4733 mutex_lock(&inode->i_mutex);
4734 truncate_inode_pages(inode->i_mapping, 0);
4735 mutex_unlock(&inode->i_mutex);
4736}
4737
Chris Wilson31169712009-09-14 16:50:28 +01004738static int
4739i915_gem_shrink(int nr_to_scan, gfp_t gfp_mask)
4740{
4741 drm_i915_private_t *dev_priv, *next_dev;
4742 struct drm_i915_gem_object *obj_priv, *next_obj;
4743 int cnt = 0;
4744 int would_deadlock = 1;
4745
4746 /* "fast-path" to count number of available objects */
4747 if (nr_to_scan == 0) {
4748 spin_lock(&shrink_list_lock);
4749 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4750 struct drm_device *dev = dev_priv->dev;
4751
4752 if (mutex_trylock(&dev->struct_mutex)) {
4753 list_for_each_entry(obj_priv,
4754 &dev_priv->mm.inactive_list,
4755 list)
4756 cnt++;
4757 mutex_unlock(&dev->struct_mutex);
4758 }
4759 }
4760 spin_unlock(&shrink_list_lock);
4761
4762 return (cnt / 100) * sysctl_vfs_cache_pressure;
4763 }
4764
4765 spin_lock(&shrink_list_lock);
4766
4767 /* first scan for clean buffers */
4768 list_for_each_entry_safe(dev_priv, next_dev,
4769 &shrink_list, mm.shrink_list) {
4770 struct drm_device *dev = dev_priv->dev;
4771
4772 if (! mutex_trylock(&dev->struct_mutex))
4773 continue;
4774
4775 spin_unlock(&shrink_list_lock);
4776
4777 i915_gem_retire_requests(dev);
4778
4779 list_for_each_entry_safe(obj_priv, next_obj,
4780 &dev_priv->mm.inactive_list,
4781 list) {
4782 if (i915_gem_object_is_purgeable(obj_priv)) {
4783 struct drm_gem_object *obj = obj_priv->obj;
4784 i915_gem_object_unbind(obj);
4785 i915_gem_object_truncate(obj);
4786
4787 if (--nr_to_scan <= 0)
4788 break;
4789 }
4790 }
4791
4792 spin_lock(&shrink_list_lock);
4793 mutex_unlock(&dev->struct_mutex);
4794
4795 if (nr_to_scan <= 0)
4796 break;
4797 }
4798
4799 /* second pass, evict/count anything still on the inactive list */
4800 list_for_each_entry_safe(dev_priv, next_dev,
4801 &shrink_list, mm.shrink_list) {
4802 struct drm_device *dev = dev_priv->dev;
4803
4804 if (! mutex_trylock(&dev->struct_mutex))
4805 continue;
4806
4807 spin_unlock(&shrink_list_lock);
4808
4809 list_for_each_entry_safe(obj_priv, next_obj,
4810 &dev_priv->mm.inactive_list,
4811 list) {
4812 if (nr_to_scan > 0) {
4813 struct drm_gem_object *obj = obj_priv->obj;
4814 i915_gem_object_unbind(obj);
4815 if (i915_gem_object_is_purgeable(obj_priv))
4816 i915_gem_object_truncate(obj);
4817
4818 nr_to_scan--;
4819 } else
4820 cnt++;
4821 }
4822
4823 spin_lock(&shrink_list_lock);
4824 mutex_unlock(&dev->struct_mutex);
4825
4826 would_deadlock = 0;
4827 }
4828
4829 spin_unlock(&shrink_list_lock);
4830
4831 if (would_deadlock)
4832 return -1;
4833 else if (cnt > 0)
4834 return (cnt / 100) * sysctl_vfs_cache_pressure;
4835 else
4836 return 0;
4837}
4838
4839static struct shrinker shrinker = {
4840 .shrink = i915_gem_shrink,
4841 .seeks = DEFAULT_SEEKS,
4842};
4843
4844__init void
4845i915_gem_shrinker_init(void)
4846{
4847 register_shrinker(&shrinker);
4848}
4849
4850__exit void
4851i915_gem_shrinker_exit(void)
4852{
4853 unregister_shrinker(&shrinker);
4854}