blob: dda787aafcc626ce064c3d59daf183c588fb5b69 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Eric Anholt673a3942008-07-30 12:06:12 -070034#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070036
Eric Anholt28dfe522008-11-13 15:00:55 -080037#define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
38
Eric Anholte47c68e2008-11-14 13:35:19 -080039static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
40static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
41static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080042static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
43 int write);
44static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
45 uint64_t offset,
46 uint64_t size);
47static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070048static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080049static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
50 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080051static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Chris Wilson07f73f62009-09-14 16:50:30 +010052static int i915_gem_evict_something(struct drm_device *dev, int min_size);
Chris Wilsonab5ee572009-09-20 19:25:47 +010053static int i915_gem_evict_from_inactive_list(struct drm_device *dev);
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
Chris Wilson4bdadb92010-01-27 13:36:32 +0000280 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700281 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 int
325i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
326{
327 int ret;
328
Chris Wilson4bdadb92010-01-27 13:36:32 +0000329 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100330
331 /* If we've insufficient memory to map in the pages, attempt
332 * to make some space by throwing out some old buffers.
333 */
334 if (ret == -ENOMEM) {
335 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100336
337 ret = i915_gem_evict_something(dev, obj->size);
338 if (ret)
339 return ret;
340
Chris Wilson4bdadb92010-01-27 13:36:32 +0000341 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100342 }
343
344 return ret;
345}
346
Eric Anholteb014592009-03-10 11:44:52 -0700347/**
348 * This is the fallback shmem pread path, which allocates temporary storage
349 * in kernel space to copy_to_user into outside of the struct_mutex, so we
350 * can copy out of the object's backing pages while holding the struct mutex
351 * and not take page faults.
352 */
353static int
354i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
355 struct drm_i915_gem_pread *args,
356 struct drm_file *file_priv)
357{
358 struct drm_i915_gem_object *obj_priv = obj->driver_private;
359 struct mm_struct *mm = current->mm;
360 struct page **user_pages;
361 ssize_t remain;
362 loff_t offset, pinned_pages, i;
363 loff_t first_data_page, last_data_page, num_pages;
364 int shmem_page_index, shmem_page_offset;
365 int data_page_index, data_page_offset;
366 int page_length;
367 int ret;
368 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700369 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700370
371 remain = args->size;
372
373 /* Pin the user pages containing the data. We can't fault while
374 * holding the struct mutex, yet we want to hold it while
375 * dereferencing the user data.
376 */
377 first_data_page = data_ptr / PAGE_SIZE;
378 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
379 num_pages = last_data_page - first_data_page + 1;
380
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700381 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700382 if (user_pages == NULL)
383 return -ENOMEM;
384
385 down_read(&mm->mmap_sem);
386 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700387 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700388 up_read(&mm->mmap_sem);
389 if (pinned_pages < num_pages) {
390 ret = -EFAULT;
391 goto fail_put_user_pages;
392 }
393
Eric Anholt280b7132009-03-12 16:56:27 -0700394 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
395
Eric Anholteb014592009-03-10 11:44:52 -0700396 mutex_lock(&dev->struct_mutex);
397
Chris Wilson07f73f62009-09-14 16:50:30 +0100398 ret = i915_gem_object_get_pages_or_evict(obj);
399 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700400 goto fail_unlock;
401
402 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
403 args->size);
404 if (ret != 0)
405 goto fail_put_pages;
406
407 obj_priv = obj->driver_private;
408 offset = args->offset;
409
410 while (remain > 0) {
411 /* Operation in this page
412 *
413 * shmem_page_index = page number within shmem file
414 * shmem_page_offset = offset within page in shmem file
415 * data_page_index = page number in get_user_pages return
416 * data_page_offset = offset with data_page_index page.
417 * page_length = bytes to copy for this page
418 */
419 shmem_page_index = offset / PAGE_SIZE;
420 shmem_page_offset = offset & ~PAGE_MASK;
421 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
422 data_page_offset = data_ptr & ~PAGE_MASK;
423
424 page_length = remain;
425 if ((shmem_page_offset + page_length) > PAGE_SIZE)
426 page_length = PAGE_SIZE - shmem_page_offset;
427 if ((data_page_offset + page_length) > PAGE_SIZE)
428 page_length = PAGE_SIZE - data_page_offset;
429
Eric Anholt280b7132009-03-12 16:56:27 -0700430 if (do_bit17_swizzling) {
431 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
432 shmem_page_offset,
433 user_pages[data_page_index],
434 data_page_offset,
435 page_length,
436 1);
437 } else {
438 ret = slow_shmem_copy(user_pages[data_page_index],
439 data_page_offset,
440 obj_priv->pages[shmem_page_index],
441 shmem_page_offset,
442 page_length);
443 }
Eric Anholteb014592009-03-10 11:44:52 -0700444 if (ret)
445 goto fail_put_pages;
446
447 remain -= page_length;
448 data_ptr += page_length;
449 offset += page_length;
450 }
451
452fail_put_pages:
453 i915_gem_object_put_pages(obj);
454fail_unlock:
455 mutex_unlock(&dev->struct_mutex);
456fail_put_user_pages:
457 for (i = 0; i < pinned_pages; i++) {
458 SetPageDirty(user_pages[i]);
459 page_cache_release(user_pages[i]);
460 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700461 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700462
463 return ret;
464}
465
Eric Anholt673a3942008-07-30 12:06:12 -0700466/**
467 * Reads data from the object referenced by handle.
468 *
469 * On error, the contents of *data are undefined.
470 */
471int
472i915_gem_pread_ioctl(struct drm_device *dev, void *data,
473 struct drm_file *file_priv)
474{
475 struct drm_i915_gem_pread *args = data;
476 struct drm_gem_object *obj;
477 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700478 int ret;
479
480 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
481 if (obj == NULL)
482 return -EBADF;
483 obj_priv = obj->driver_private;
484
485 /* Bounds check source.
486 *
487 * XXX: This could use review for overflow issues...
488 */
489 if (args->offset > obj->size || args->size > obj->size ||
490 args->offset + args->size > obj->size) {
491 drm_gem_object_unreference(obj);
492 return -EINVAL;
493 }
494
Eric Anholt280b7132009-03-12 16:56:27 -0700495 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700496 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700497 } else {
498 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
499 if (ret != 0)
500 ret = i915_gem_shmem_pread_slow(dev, obj, args,
501 file_priv);
502 }
Eric Anholt673a3942008-07-30 12:06:12 -0700503
504 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700505
Eric Anholteb014592009-03-10 11:44:52 -0700506 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700507}
508
Keith Packard0839ccb2008-10-30 19:38:48 -0700509/* This is the fast write path which cannot handle
510 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700511 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700512
Keith Packard0839ccb2008-10-30 19:38:48 -0700513static inline int
514fast_user_write(struct io_mapping *mapping,
515 loff_t page_base, int page_offset,
516 char __user *user_data,
517 int length)
518{
519 char *vaddr_atomic;
520 unsigned long unwritten;
521
522 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
523 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
524 user_data, length);
525 io_mapping_unmap_atomic(vaddr_atomic);
526 if (unwritten)
527 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700528 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700529}
530
531/* Here's the write path which can sleep for
532 * page faults
533 */
534
535static inline int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700536slow_kernel_write(struct io_mapping *mapping,
537 loff_t gtt_base, int gtt_offset,
538 struct page *user_page, int user_offset,
539 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700540{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700541 char *src_vaddr, *dst_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700542 unsigned long unwritten;
543
Eric Anholt3de09aa2009-03-09 09:42:23 -0700544 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
545 src_vaddr = kmap_atomic(user_page, KM_USER1);
546 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
547 src_vaddr + user_offset,
548 length);
549 kunmap_atomic(src_vaddr, KM_USER1);
550 io_mapping_unmap_atomic(dst_vaddr);
Keith Packard0839ccb2008-10-30 19:38:48 -0700551 if (unwritten)
552 return -EFAULT;
553 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700554}
555
Eric Anholt40123c12009-03-09 13:42:30 -0700556static inline int
557fast_shmem_write(struct page **pages,
558 loff_t page_base, int page_offset,
559 char __user *data,
560 int length)
561{
562 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400563 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700564
565 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
566 if (vaddr == NULL)
567 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400568 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700569 kunmap_atomic(vaddr, KM_USER0);
570
Dave Airlied0088772009-03-28 20:29:48 -0400571 if (unwritten)
572 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700573 return 0;
574}
575
Eric Anholt3de09aa2009-03-09 09:42:23 -0700576/**
577 * This is the fast pwrite path, where we copy the data directly from the
578 * user into the GTT, uncached.
579 */
Eric Anholt673a3942008-07-30 12:06:12 -0700580static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700581i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
582 struct drm_i915_gem_pwrite *args,
583 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700584{
585 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Keith Packard0839ccb2008-10-30 19:38:48 -0700586 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700587 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700588 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700589 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700590 int page_offset, page_length;
591 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700592
593 user_data = (char __user *) (uintptr_t) args->data_ptr;
594 remain = args->size;
595 if (!access_ok(VERIFY_READ, user_data, remain))
596 return -EFAULT;
597
598
599 mutex_lock(&dev->struct_mutex);
600 ret = i915_gem_object_pin(obj, 0);
601 if (ret) {
602 mutex_unlock(&dev->struct_mutex);
603 return ret;
604 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800605 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700606 if (ret)
607 goto fail;
608
609 obj_priv = obj->driver_private;
610 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700611
612 while (remain > 0) {
613 /* Operation in this page
614 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700615 * page_base = page offset within aperture
616 * page_offset = offset within page
617 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700618 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700619 page_base = (offset & ~(PAGE_SIZE-1));
620 page_offset = offset & (PAGE_SIZE-1);
621 page_length = remain;
622 if ((page_offset + remain) > PAGE_SIZE)
623 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700624
Keith Packard0839ccb2008-10-30 19:38:48 -0700625 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
626 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700627
Keith Packard0839ccb2008-10-30 19:38:48 -0700628 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700629 * source page isn't available. Return the error and we'll
630 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700631 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700632 if (ret)
633 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700634
Keith Packard0839ccb2008-10-30 19:38:48 -0700635 remain -= page_length;
636 user_data += page_length;
637 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700638 }
Eric Anholt673a3942008-07-30 12:06:12 -0700639
640fail:
641 i915_gem_object_unpin(obj);
642 mutex_unlock(&dev->struct_mutex);
643
644 return ret;
645}
646
Eric Anholt3de09aa2009-03-09 09:42:23 -0700647/**
648 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
649 * the memory and maps it using kmap_atomic for copying.
650 *
651 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
652 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
653 */
Eric Anholt3043c602008-10-02 12:24:47 -0700654static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700655i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
656 struct drm_i915_gem_pwrite *args,
657 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700658{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700659 struct drm_i915_gem_object *obj_priv = obj->driver_private;
660 drm_i915_private_t *dev_priv = dev->dev_private;
661 ssize_t remain;
662 loff_t gtt_page_base, offset;
663 loff_t first_data_page, last_data_page, num_pages;
664 loff_t pinned_pages, i;
665 struct page **user_pages;
666 struct mm_struct *mm = current->mm;
667 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700668 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700669 uint64_t data_ptr = args->data_ptr;
670
671 remain = args->size;
672
673 /* Pin the user pages containing the data. We can't fault while
674 * holding the struct mutex, and all of the pwrite implementations
675 * want to hold it while dereferencing the user data.
676 */
677 first_data_page = data_ptr / PAGE_SIZE;
678 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
679 num_pages = last_data_page - first_data_page + 1;
680
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700681 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700682 if (user_pages == NULL)
683 return -ENOMEM;
684
685 down_read(&mm->mmap_sem);
686 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
687 num_pages, 0, 0, user_pages, NULL);
688 up_read(&mm->mmap_sem);
689 if (pinned_pages < num_pages) {
690 ret = -EFAULT;
691 goto out_unpin_pages;
692 }
693
694 mutex_lock(&dev->struct_mutex);
695 ret = i915_gem_object_pin(obj, 0);
696 if (ret)
697 goto out_unlock;
698
699 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
700 if (ret)
701 goto out_unpin_object;
702
703 obj_priv = obj->driver_private;
704 offset = obj_priv->gtt_offset + args->offset;
705
706 while (remain > 0) {
707 /* Operation in this page
708 *
709 * gtt_page_base = page offset within aperture
710 * gtt_page_offset = offset within page in aperture
711 * data_page_index = page number in get_user_pages return
712 * data_page_offset = offset with data_page_index page.
713 * page_length = bytes to copy for this page
714 */
715 gtt_page_base = offset & PAGE_MASK;
716 gtt_page_offset = offset & ~PAGE_MASK;
717 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
718 data_page_offset = data_ptr & ~PAGE_MASK;
719
720 page_length = remain;
721 if ((gtt_page_offset + page_length) > PAGE_SIZE)
722 page_length = PAGE_SIZE - gtt_page_offset;
723 if ((data_page_offset + page_length) > PAGE_SIZE)
724 page_length = PAGE_SIZE - data_page_offset;
725
726 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
727 gtt_page_base, gtt_page_offset,
728 user_pages[data_page_index],
729 data_page_offset,
730 page_length);
731
732 /* If we get a fault while copying data, then (presumably) our
733 * source page isn't available. Return the error and we'll
734 * retry in the slow path.
735 */
736 if (ret)
737 goto out_unpin_object;
738
739 remain -= page_length;
740 offset += page_length;
741 data_ptr += page_length;
742 }
743
744out_unpin_object:
745 i915_gem_object_unpin(obj);
746out_unlock:
747 mutex_unlock(&dev->struct_mutex);
748out_unpin_pages:
749 for (i = 0; i < pinned_pages; i++)
750 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700751 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700752
753 return ret;
754}
755
Eric Anholt40123c12009-03-09 13:42:30 -0700756/**
757 * This is the fast shmem pwrite path, which attempts to directly
758 * copy_from_user into the kmapped pages backing the object.
759 */
Eric Anholt673a3942008-07-30 12:06:12 -0700760static int
Eric Anholt40123c12009-03-09 13:42:30 -0700761i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
762 struct drm_i915_gem_pwrite *args,
763 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700764{
Eric Anholt40123c12009-03-09 13:42:30 -0700765 struct drm_i915_gem_object *obj_priv = obj->driver_private;
766 ssize_t remain;
767 loff_t offset, page_base;
768 char __user *user_data;
769 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700770 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700771
772 user_data = (char __user *) (uintptr_t) args->data_ptr;
773 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700774
775 mutex_lock(&dev->struct_mutex);
776
Chris Wilson4bdadb92010-01-27 13:36:32 +0000777 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700778 if (ret != 0)
779 goto fail_unlock;
780
Eric Anholte47c68e2008-11-14 13:35:19 -0800781 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700782 if (ret != 0)
783 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700784
Eric Anholt40123c12009-03-09 13:42:30 -0700785 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700786 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700787 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700788
Eric Anholt40123c12009-03-09 13:42:30 -0700789 while (remain > 0) {
790 /* Operation in this page
791 *
792 * page_base = page offset within aperture
793 * page_offset = offset within page
794 * page_length = bytes to copy for this page
795 */
796 page_base = (offset & ~(PAGE_SIZE-1));
797 page_offset = offset & (PAGE_SIZE-1);
798 page_length = remain;
799 if ((page_offset + remain) > PAGE_SIZE)
800 page_length = PAGE_SIZE - page_offset;
801
802 ret = fast_shmem_write(obj_priv->pages,
803 page_base, page_offset,
804 user_data, page_length);
805 if (ret)
806 goto fail_put_pages;
807
808 remain -= page_length;
809 user_data += page_length;
810 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700811 }
812
Eric Anholt40123c12009-03-09 13:42:30 -0700813fail_put_pages:
814 i915_gem_object_put_pages(obj);
815fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700816 mutex_unlock(&dev->struct_mutex);
817
Eric Anholt40123c12009-03-09 13:42:30 -0700818 return ret;
819}
820
821/**
822 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
823 * the memory and maps it using kmap_atomic for copying.
824 *
825 * This avoids taking mmap_sem for faulting on the user's address while the
826 * struct_mutex is held.
827 */
828static int
829i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
830 struct drm_i915_gem_pwrite *args,
831 struct drm_file *file_priv)
832{
833 struct drm_i915_gem_object *obj_priv = obj->driver_private;
834 struct mm_struct *mm = current->mm;
835 struct page **user_pages;
836 ssize_t remain;
837 loff_t offset, pinned_pages, i;
838 loff_t first_data_page, last_data_page, num_pages;
839 int shmem_page_index, shmem_page_offset;
840 int data_page_index, data_page_offset;
841 int page_length;
842 int ret;
843 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700844 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700845
846 remain = args->size;
847
848 /* Pin the user pages containing the data. We can't fault while
849 * holding the struct mutex, and all of the pwrite implementations
850 * want to hold it while dereferencing the user data.
851 */
852 first_data_page = data_ptr / PAGE_SIZE;
853 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
854 num_pages = last_data_page - first_data_page + 1;
855
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700856 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700857 if (user_pages == NULL)
858 return -ENOMEM;
859
860 down_read(&mm->mmap_sem);
861 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
862 num_pages, 0, 0, user_pages, NULL);
863 up_read(&mm->mmap_sem);
864 if (pinned_pages < num_pages) {
865 ret = -EFAULT;
866 goto fail_put_user_pages;
867 }
868
Eric Anholt280b7132009-03-12 16:56:27 -0700869 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
870
Eric Anholt40123c12009-03-09 13:42:30 -0700871 mutex_lock(&dev->struct_mutex);
872
Chris Wilson07f73f62009-09-14 16:50:30 +0100873 ret = i915_gem_object_get_pages_or_evict(obj);
874 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700875 goto fail_unlock;
876
877 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
878 if (ret != 0)
879 goto fail_put_pages;
880
881 obj_priv = obj->driver_private;
882 offset = args->offset;
883 obj_priv->dirty = 1;
884
885 while (remain > 0) {
886 /* Operation in this page
887 *
888 * shmem_page_index = page number within shmem file
889 * shmem_page_offset = offset within page in shmem file
890 * data_page_index = page number in get_user_pages return
891 * data_page_offset = offset with data_page_index page.
892 * page_length = bytes to copy for this page
893 */
894 shmem_page_index = offset / PAGE_SIZE;
895 shmem_page_offset = offset & ~PAGE_MASK;
896 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
897 data_page_offset = data_ptr & ~PAGE_MASK;
898
899 page_length = remain;
900 if ((shmem_page_offset + page_length) > PAGE_SIZE)
901 page_length = PAGE_SIZE - shmem_page_offset;
902 if ((data_page_offset + page_length) > PAGE_SIZE)
903 page_length = PAGE_SIZE - data_page_offset;
904
Eric Anholt280b7132009-03-12 16:56:27 -0700905 if (do_bit17_swizzling) {
906 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
907 shmem_page_offset,
908 user_pages[data_page_index],
909 data_page_offset,
910 page_length,
911 0);
912 } else {
913 ret = slow_shmem_copy(obj_priv->pages[shmem_page_index],
914 shmem_page_offset,
915 user_pages[data_page_index],
916 data_page_offset,
917 page_length);
918 }
Eric Anholt40123c12009-03-09 13:42:30 -0700919 if (ret)
920 goto fail_put_pages;
921
922 remain -= page_length;
923 data_ptr += page_length;
924 offset += page_length;
925 }
926
927fail_put_pages:
928 i915_gem_object_put_pages(obj);
929fail_unlock:
930 mutex_unlock(&dev->struct_mutex);
931fail_put_user_pages:
932 for (i = 0; i < pinned_pages; i++)
933 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700934 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700935
936 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700937}
938
939/**
940 * Writes data to the object referenced by handle.
941 *
942 * On error, the contents of the buffer that were to be modified are undefined.
943 */
944int
945i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
946 struct drm_file *file_priv)
947{
948 struct drm_i915_gem_pwrite *args = data;
949 struct drm_gem_object *obj;
950 struct drm_i915_gem_object *obj_priv;
951 int ret = 0;
952
953 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
954 if (obj == NULL)
955 return -EBADF;
956 obj_priv = obj->driver_private;
957
958 /* Bounds check destination.
959 *
960 * XXX: This could use review for overflow issues...
961 */
962 if (args->offset > obj->size || args->size > obj->size ||
963 args->offset + args->size > obj->size) {
964 drm_gem_object_unreference(obj);
965 return -EINVAL;
966 }
967
968 /* We can only do the GTT pwrite on untiled buffers, as otherwise
969 * it would end up going through the fenced access, and we'll get
970 * different detiling behavior between reading and writing.
971 * pread/pwrite currently are reading and writing from the CPU
972 * perspective, requiring manual detiling by the client.
973 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000974 if (obj_priv->phys_obj)
975 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
976 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Eric Anholt3de09aa2009-03-09 09:42:23 -0700977 dev->gtt_total != 0) {
978 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
979 if (ret == -EFAULT) {
980 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
981 file_priv);
982 }
Eric Anholt280b7132009-03-12 16:56:27 -0700983 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
984 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700985 } else {
986 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
987 if (ret == -EFAULT) {
988 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
989 file_priv);
990 }
991 }
Eric Anholt673a3942008-07-30 12:06:12 -0700992
993#if WATCH_PWRITE
994 if (ret)
995 DRM_INFO("pwrite failed %d\n", ret);
996#endif
997
998 drm_gem_object_unreference(obj);
999
1000 return ret;
1001}
1002
1003/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001004 * Called when user space prepares to use an object with the CPU, either
1005 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001006 */
1007int
1008i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1009 struct drm_file *file_priv)
1010{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001011 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001012 struct drm_i915_gem_set_domain *args = data;
1013 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001014 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001015 uint32_t read_domains = args->read_domains;
1016 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001017 int ret;
1018
1019 if (!(dev->driver->driver_features & DRIVER_GEM))
1020 return -ENODEV;
1021
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001022 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001023 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001024 return -EINVAL;
1025
Chris Wilson21d509e2009-06-06 09:46:02 +01001026 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001027 return -EINVAL;
1028
1029 /* Having something in the write domain implies it's in the read
1030 * domain, and only that read domain. Enforce that in the request.
1031 */
1032 if (write_domain != 0 && read_domains != write_domain)
1033 return -EINVAL;
1034
Eric Anholt673a3942008-07-30 12:06:12 -07001035 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1036 if (obj == NULL)
1037 return -EBADF;
Jesse Barnes652c3932009-08-17 13:31:43 -07001038 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001039
1040 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001041
1042 intel_mark_busy(dev, obj);
1043
Eric Anholt673a3942008-07-30 12:06:12 -07001044#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001045 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001046 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001047#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001048 if (read_domains & I915_GEM_DOMAIN_GTT) {
1049 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001050
Eric Anholta09ba7f2009-08-29 12:49:51 -07001051 /* Update the LRU on the fence for the CPU access that's
1052 * about to occur.
1053 */
1054 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1055 list_move_tail(&obj_priv->fence_list,
1056 &dev_priv->mm.fence_list);
1057 }
1058
Eric Anholt02354392008-11-26 13:58:13 -08001059 /* Silently promote "you're not bound, there was nothing to do"
1060 * to success, since the client was just asking us to
1061 * make sure everything was done.
1062 */
1063 if (ret == -EINVAL)
1064 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001065 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001066 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001067 }
1068
Eric Anholt673a3942008-07-30 12:06:12 -07001069 drm_gem_object_unreference(obj);
1070 mutex_unlock(&dev->struct_mutex);
1071 return ret;
1072}
1073
1074/**
1075 * Called when user space has done writes to this buffer
1076 */
1077int
1078i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1079 struct drm_file *file_priv)
1080{
1081 struct drm_i915_gem_sw_finish *args = data;
1082 struct drm_gem_object *obj;
1083 struct drm_i915_gem_object *obj_priv;
1084 int ret = 0;
1085
1086 if (!(dev->driver->driver_features & DRIVER_GEM))
1087 return -ENODEV;
1088
1089 mutex_lock(&dev->struct_mutex);
1090 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1091 if (obj == NULL) {
1092 mutex_unlock(&dev->struct_mutex);
1093 return -EBADF;
1094 }
1095
1096#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001097 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001098 __func__, args->handle, obj, obj->size);
1099#endif
1100 obj_priv = obj->driver_private;
1101
1102 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001103 if (obj_priv->pin_count)
1104 i915_gem_object_flush_cpu_write_domain(obj);
1105
Eric Anholt673a3942008-07-30 12:06:12 -07001106 drm_gem_object_unreference(obj);
1107 mutex_unlock(&dev->struct_mutex);
1108 return ret;
1109}
1110
1111/**
1112 * Maps the contents of an object, returning the address it is mapped
1113 * into.
1114 *
1115 * While the mapping holds a reference on the contents of the object, it doesn't
1116 * imply a ref on the object itself.
1117 */
1118int
1119i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1120 struct drm_file *file_priv)
1121{
1122 struct drm_i915_gem_mmap *args = data;
1123 struct drm_gem_object *obj;
1124 loff_t offset;
1125 unsigned long addr;
1126
1127 if (!(dev->driver->driver_features & DRIVER_GEM))
1128 return -ENODEV;
1129
1130 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1131 if (obj == NULL)
1132 return -EBADF;
1133
1134 offset = args->offset;
1135
1136 down_write(&current->mm->mmap_sem);
1137 addr = do_mmap(obj->filp, 0, args->size,
1138 PROT_READ | PROT_WRITE, MAP_SHARED,
1139 args->offset);
1140 up_write(&current->mm->mmap_sem);
1141 mutex_lock(&dev->struct_mutex);
1142 drm_gem_object_unreference(obj);
1143 mutex_unlock(&dev->struct_mutex);
1144 if (IS_ERR((void *)addr))
1145 return addr;
1146
1147 args->addr_ptr = (uint64_t) addr;
1148
1149 return 0;
1150}
1151
Jesse Barnesde151cf2008-11-12 10:03:55 -08001152/**
1153 * i915_gem_fault - fault a page into the GTT
1154 * vma: VMA in question
1155 * vmf: fault info
1156 *
1157 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1158 * from userspace. The fault handler takes care of binding the object to
1159 * the GTT (if needed), allocating and programming a fence register (again,
1160 * only if needed based on whether the old reg is still valid or the object
1161 * is tiled) and inserting a new PTE into the faulting process.
1162 *
1163 * Note that the faulting process may involve evicting existing objects
1164 * from the GTT and/or fence registers to make room. So performance may
1165 * suffer if the GTT working set is large or there are few fence registers
1166 * left.
1167 */
1168int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1169{
1170 struct drm_gem_object *obj = vma->vm_private_data;
1171 struct drm_device *dev = obj->dev;
1172 struct drm_i915_private *dev_priv = dev->dev_private;
1173 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1174 pgoff_t page_offset;
1175 unsigned long pfn;
1176 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001177 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001178
1179 /* We don't use vmf->pgoff since that has the fake offset */
1180 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1181 PAGE_SHIFT;
1182
1183 /* Now bind it into the GTT if needed */
1184 mutex_lock(&dev->struct_mutex);
1185 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001186 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001187 if (ret)
1188 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001189
Jesse Barnes14b60392009-05-20 16:47:08 -04001190 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001191
1192 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001193 if (ret)
1194 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001195 }
1196
1197 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001198 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001199 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001200 if (ret)
1201 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001202 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001203
1204 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1205 page_offset;
1206
1207 /* Finally, remap it using the new GTT offset */
1208 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001209unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001210 mutex_unlock(&dev->struct_mutex);
1211
1212 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001213 case 0:
1214 case -ERESTARTSYS:
1215 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001216 case -ENOMEM:
1217 case -EAGAIN:
1218 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001219 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001220 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001221 }
1222}
1223
1224/**
1225 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1226 * @obj: obj in question
1227 *
1228 * GEM memory mapping works by handing back to userspace a fake mmap offset
1229 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1230 * up the object based on the offset and sets up the various memory mapping
1231 * structures.
1232 *
1233 * This routine allocates and attaches a fake offset for @obj.
1234 */
1235static int
1236i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1237{
1238 struct drm_device *dev = obj->dev;
1239 struct drm_gem_mm *mm = dev->mm_private;
1240 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1241 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001242 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001243 int ret = 0;
1244
1245 /* Set the object up for mmap'ing */
1246 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001247 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001248 if (!list->map)
1249 return -ENOMEM;
1250
1251 map = list->map;
1252 map->type = _DRM_GEM;
1253 map->size = obj->size;
1254 map->handle = obj;
1255
1256 /* Get a DRM GEM mmap offset allocated... */
1257 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1258 obj->size / PAGE_SIZE, 0, 0);
1259 if (!list->file_offset_node) {
1260 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1261 ret = -ENOMEM;
1262 goto out_free_list;
1263 }
1264
1265 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1266 obj->size / PAGE_SIZE, 0);
1267 if (!list->file_offset_node) {
1268 ret = -ENOMEM;
1269 goto out_free_list;
1270 }
1271
1272 list->hash.key = list->file_offset_node->start;
1273 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1274 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001275 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001276 goto out_free_mm;
1277 }
1278
1279 /* By now we should be all set, any drm_mmap request on the offset
1280 * below will get to our mmap & fault handler */
1281 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1282
1283 return 0;
1284
1285out_free_mm:
1286 drm_mm_put_block(list->file_offset_node);
1287out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001288 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001289
1290 return ret;
1291}
1292
Chris Wilson901782b2009-07-10 08:18:50 +01001293/**
1294 * i915_gem_release_mmap - remove physical page mappings
1295 * @obj: obj in question
1296 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001297 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001298 * relinquish ownership of the pages back to the system.
1299 *
1300 * It is vital that we remove the page mapping if we have mapped a tiled
1301 * object through the GTT and then lose the fence register due to
1302 * resource pressure. Similarly if the object has been moved out of the
1303 * aperture, than pages mapped into userspace must be revoked. Removing the
1304 * mapping will then trigger a page fault on the next user access, allowing
1305 * fixup by i915_gem_fault().
1306 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001307void
Chris Wilson901782b2009-07-10 08:18:50 +01001308i915_gem_release_mmap(struct drm_gem_object *obj)
1309{
1310 struct drm_device *dev = obj->dev;
1311 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1312
1313 if (dev->dev_mapping)
1314 unmap_mapping_range(dev->dev_mapping,
1315 obj_priv->mmap_offset, obj->size, 1);
1316}
1317
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001318static void
1319i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1320{
1321 struct drm_device *dev = obj->dev;
1322 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1323 struct drm_gem_mm *mm = dev->mm_private;
1324 struct drm_map_list *list;
1325
1326 list = &obj->map_list;
1327 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1328
1329 if (list->file_offset_node) {
1330 drm_mm_put_block(list->file_offset_node);
1331 list->file_offset_node = NULL;
1332 }
1333
1334 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001335 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001336 list->map = NULL;
1337 }
1338
1339 obj_priv->mmap_offset = 0;
1340}
1341
Jesse Barnesde151cf2008-11-12 10:03:55 -08001342/**
1343 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1344 * @obj: object to check
1345 *
1346 * Return the required GTT alignment for an object, taking into account
1347 * potential fence register mapping if needed.
1348 */
1349static uint32_t
1350i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1351{
1352 struct drm_device *dev = obj->dev;
1353 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1354 int start, i;
1355
1356 /*
1357 * Minimum alignment is 4k (GTT page size), but might be greater
1358 * if a fence register is needed for the object.
1359 */
1360 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1361 return 4096;
1362
1363 /*
1364 * Previous chips need to be aligned to the size of the smallest
1365 * fence register that can contain the object.
1366 */
1367 if (IS_I9XX(dev))
1368 start = 1024*1024;
1369 else
1370 start = 512*1024;
1371
1372 for (i = start; i < obj->size; i <<= 1)
1373 ;
1374
1375 return i;
1376}
1377
1378/**
1379 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1380 * @dev: DRM device
1381 * @data: GTT mapping ioctl data
1382 * @file_priv: GEM object info
1383 *
1384 * Simply returns the fake offset to userspace so it can mmap it.
1385 * The mmap call will end up in drm_gem_mmap(), which will set things
1386 * up so we can get faults in the handler above.
1387 *
1388 * The fault handler will take care of binding the object into the GTT
1389 * (since it may have been evicted to make room for something), allocating
1390 * a fence register, and mapping the appropriate aperture address into
1391 * userspace.
1392 */
1393int
1394i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1395 struct drm_file *file_priv)
1396{
1397 struct drm_i915_gem_mmap_gtt *args = data;
1398 struct drm_i915_private *dev_priv = dev->dev_private;
1399 struct drm_gem_object *obj;
1400 struct drm_i915_gem_object *obj_priv;
1401 int ret;
1402
1403 if (!(dev->driver->driver_features & DRIVER_GEM))
1404 return -ENODEV;
1405
1406 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1407 if (obj == NULL)
1408 return -EBADF;
1409
1410 mutex_lock(&dev->struct_mutex);
1411
1412 obj_priv = obj->driver_private;
1413
Chris Wilsonab182822009-09-22 18:46:17 +01001414 if (obj_priv->madv != I915_MADV_WILLNEED) {
1415 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1416 drm_gem_object_unreference(obj);
1417 mutex_unlock(&dev->struct_mutex);
1418 return -EINVAL;
1419 }
1420
1421
Jesse Barnesde151cf2008-11-12 10:03:55 -08001422 if (!obj_priv->mmap_offset) {
1423 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001424 if (ret) {
1425 drm_gem_object_unreference(obj);
1426 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001427 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001428 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001429 }
1430
1431 args->offset = obj_priv->mmap_offset;
1432
Jesse Barnesde151cf2008-11-12 10:03:55 -08001433 /*
1434 * Pull it into the GTT so that we have a page list (makes the
1435 * initial fault faster and any subsequent flushing possible).
1436 */
1437 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001438 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001439 if (ret) {
1440 drm_gem_object_unreference(obj);
1441 mutex_unlock(&dev->struct_mutex);
1442 return ret;
1443 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001444 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001445 }
1446
1447 drm_gem_object_unreference(obj);
1448 mutex_unlock(&dev->struct_mutex);
1449
1450 return 0;
1451}
1452
Ben Gamari6911a9b2009-04-02 11:24:54 -07001453void
Eric Anholt856fa192009-03-19 14:10:50 -07001454i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001455{
1456 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1457 int page_count = obj->size / PAGE_SIZE;
1458 int i;
1459
Eric Anholt856fa192009-03-19 14:10:50 -07001460 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001461 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001462
1463 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001464 return;
1465
Eric Anholt280b7132009-03-12 16:56:27 -07001466 if (obj_priv->tiling_mode != I915_TILING_NONE)
1467 i915_gem_object_save_bit_17_swizzle(obj);
1468
Chris Wilson3ef94da2009-09-14 16:50:29 +01001469 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001470 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001471
1472 for (i = 0; i < page_count; i++) {
1473 if (obj_priv->pages[i] == NULL)
1474 break;
1475
1476 if (obj_priv->dirty)
1477 set_page_dirty(obj_priv->pages[i]);
1478
1479 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001480 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001481
1482 page_cache_release(obj_priv->pages[i]);
1483 }
Eric Anholt673a3942008-07-30 12:06:12 -07001484 obj_priv->dirty = 0;
1485
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001486 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001487 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001488}
1489
1490static void
Eric Anholtce44b0e2008-11-06 16:00:31 -08001491i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001492{
1493 struct drm_device *dev = obj->dev;
1494 drm_i915_private_t *dev_priv = dev->dev_private;
1495 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1496
1497 /* Add a reference if we're newly entering the active list. */
1498 if (!obj_priv->active) {
1499 drm_gem_object_reference(obj);
1500 obj_priv->active = 1;
1501 }
1502 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001503 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001504 list_move_tail(&obj_priv->list,
1505 &dev_priv->mm.active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001506 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001507 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001508}
1509
Eric Anholtce44b0e2008-11-06 16:00:31 -08001510static void
1511i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1512{
1513 struct drm_device *dev = obj->dev;
1514 drm_i915_private_t *dev_priv = dev->dev_private;
1515 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1516
1517 BUG_ON(!obj_priv->active);
1518 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1519 obj_priv->last_rendering_seqno = 0;
1520}
Eric Anholt673a3942008-07-30 12:06:12 -07001521
Chris Wilson963b4832009-09-20 23:03:54 +01001522/* Immediately discard the backing storage */
1523static void
1524i915_gem_object_truncate(struct drm_gem_object *obj)
1525{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001526 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1527 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001528
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001529 inode = obj->filp->f_path.dentry->d_inode;
1530 if (inode->i_op->truncate)
1531 inode->i_op->truncate (inode);
1532
1533 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001534}
1535
1536static inline int
1537i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1538{
1539 return obj_priv->madv == I915_MADV_DONTNEED;
1540}
1541
Eric Anholt673a3942008-07-30 12:06:12 -07001542static void
1543i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1544{
1545 struct drm_device *dev = obj->dev;
1546 drm_i915_private_t *dev_priv = dev->dev_private;
1547 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1548
1549 i915_verify_inactive(dev, __FILE__, __LINE__);
1550 if (obj_priv->pin_count != 0)
1551 list_del_init(&obj_priv->list);
1552 else
1553 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1554
Eric Anholtce44b0e2008-11-06 16:00:31 -08001555 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001556 if (obj_priv->active) {
1557 obj_priv->active = 0;
1558 drm_gem_object_unreference(obj);
1559 }
1560 i915_verify_inactive(dev, __FILE__, __LINE__);
1561}
1562
1563/**
1564 * Creates a new sequence number, emitting a write of it to the status page
1565 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1566 *
1567 * Must be called with struct_lock held.
1568 *
1569 * Returned sequence numbers are nonzero on success.
1570 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001571uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001572i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
1573 uint32_t flush_domains)
Eric Anholt673a3942008-07-30 12:06:12 -07001574{
1575 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001576 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001577 struct drm_i915_gem_request *request;
1578 uint32_t seqno;
1579 int was_empty;
1580 RING_LOCALS;
1581
Eric Anholtb9624422009-06-03 07:27:35 +00001582 if (file_priv != NULL)
1583 i915_file_priv = file_priv->driver_priv;
1584
Eric Anholt9a298b22009-03-24 12:23:04 -07001585 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001586 if (request == NULL)
1587 return 0;
1588
1589 /* Grab the seqno we're going to make this request be, and bump the
1590 * next (skipping 0 so it can be the reserved no-seqno value).
1591 */
1592 seqno = dev_priv->mm.next_gem_seqno;
1593 dev_priv->mm.next_gem_seqno++;
1594 if (dev_priv->mm.next_gem_seqno == 0)
1595 dev_priv->mm.next_gem_seqno++;
1596
1597 BEGIN_LP_RING(4);
1598 OUT_RING(MI_STORE_DWORD_INDEX);
1599 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1600 OUT_RING(seqno);
1601
1602 OUT_RING(MI_USER_INTERRUPT);
1603 ADVANCE_LP_RING();
1604
Zhao Yakui44d98a62009-10-09 11:39:40 +08001605 DRM_DEBUG_DRIVER("%d\n", seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001606
1607 request->seqno = seqno;
1608 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -07001609 was_empty = list_empty(&dev_priv->mm.request_list);
1610 list_add_tail(&request->list, &dev_priv->mm.request_list);
Eric Anholtb9624422009-06-03 07:27:35 +00001611 if (i915_file_priv) {
1612 list_add_tail(&request->client_list,
1613 &i915_file_priv->mm.request_list);
1614 } else {
1615 INIT_LIST_HEAD(&request->client_list);
1616 }
Eric Anholt673a3942008-07-30 12:06:12 -07001617
Eric Anholtce44b0e2008-11-06 16:00:31 -08001618 /* Associate any objects on the flushing list matching the write
1619 * domain we're flushing with our flush.
1620 */
1621 if (flush_domains != 0) {
1622 struct drm_i915_gem_object *obj_priv, *next;
1623
1624 list_for_each_entry_safe(obj_priv, next,
1625 &dev_priv->mm.flushing_list, list) {
1626 struct drm_gem_object *obj = obj_priv->obj;
1627
1628 if ((obj->write_domain & flush_domains) ==
1629 obj->write_domain) {
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001630 uint32_t old_write_domain = obj->write_domain;
1631
Eric Anholtce44b0e2008-11-06 16:00:31 -08001632 obj->write_domain = 0;
1633 i915_gem_object_move_to_active(obj, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001634
1635 trace_i915_gem_object_change_domain(obj,
1636 obj->read_domains,
1637 old_write_domain);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001638 }
1639 }
1640
1641 }
1642
Ben Gamarif65d9422009-09-14 17:48:44 -04001643 if (!dev_priv->mm.suspended) {
1644 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1645 if (was_empty)
1646 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1647 }
Eric Anholt673a3942008-07-30 12:06:12 -07001648 return seqno;
1649}
1650
1651/**
1652 * Command execution barrier
1653 *
1654 * Ensures that all commands in the ring are finished
1655 * before signalling the CPU
1656 */
Eric Anholt3043c602008-10-02 12:24:47 -07001657static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -07001658i915_retire_commands(struct drm_device *dev)
1659{
1660 drm_i915_private_t *dev_priv = dev->dev_private;
1661 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1662 uint32_t flush_domains = 0;
1663 RING_LOCALS;
1664
1665 /* The sampler always gets flushed on i965 (sigh) */
1666 if (IS_I965G(dev))
1667 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1668 BEGIN_LP_RING(2);
1669 OUT_RING(cmd);
1670 OUT_RING(0); /* noop */
1671 ADVANCE_LP_RING();
1672 return flush_domains;
1673}
1674
1675/**
1676 * Moves buffers associated only with the given active seqno from the active
1677 * to inactive list, potentially freeing them.
1678 */
1679static void
1680i915_gem_retire_request(struct drm_device *dev,
1681 struct drm_i915_gem_request *request)
1682{
1683 drm_i915_private_t *dev_priv = dev->dev_private;
1684
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001685 trace_i915_gem_request_retire(dev, request->seqno);
1686
Eric Anholt673a3942008-07-30 12:06:12 -07001687 /* Move any buffers on the active list that are no longer referenced
1688 * by the ringbuffer to the flushing/inactive lists as appropriate.
1689 */
Carl Worth5e118f42009-03-20 11:54:25 -07001690 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001691 while (!list_empty(&dev_priv->mm.active_list)) {
1692 struct drm_gem_object *obj;
1693 struct drm_i915_gem_object *obj_priv;
1694
1695 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1696 struct drm_i915_gem_object,
1697 list);
1698 obj = obj_priv->obj;
1699
1700 /* If the seqno being retired doesn't match the oldest in the
1701 * list, then the oldest in the list must still be newer than
1702 * this seqno.
1703 */
1704 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001705 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001706
Eric Anholt673a3942008-07-30 12:06:12 -07001707#if WATCH_LRU
1708 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1709 __func__, request->seqno, obj);
1710#endif
1711
Eric Anholtce44b0e2008-11-06 16:00:31 -08001712 if (obj->write_domain != 0)
1713 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001714 else {
1715 /* Take a reference on the object so it won't be
1716 * freed while the spinlock is held. The list
1717 * protection for this spinlock is safe when breaking
1718 * the lock like this since the next thing we do
1719 * is just get the head of the list again.
1720 */
1721 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001722 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001723 spin_unlock(&dev_priv->mm.active_list_lock);
1724 drm_gem_object_unreference(obj);
1725 spin_lock(&dev_priv->mm.active_list_lock);
1726 }
Eric Anholt673a3942008-07-30 12:06:12 -07001727 }
Carl Worth5e118f42009-03-20 11:54:25 -07001728out:
1729 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001730}
1731
1732/**
1733 * Returns true if seq1 is later than seq2.
1734 */
Ben Gamari22be1722009-09-14 17:48:43 -04001735bool
Eric Anholt673a3942008-07-30 12:06:12 -07001736i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1737{
1738 return (int32_t)(seq1 - seq2) >= 0;
1739}
1740
1741uint32_t
1742i915_get_gem_seqno(struct drm_device *dev)
1743{
1744 drm_i915_private_t *dev_priv = dev->dev_private;
1745
1746 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1747}
1748
1749/**
1750 * This function clears the request list as sequence numbers are passed.
1751 */
1752void
1753i915_gem_retire_requests(struct drm_device *dev)
1754{
1755 drm_i915_private_t *dev_priv = dev->dev_private;
1756 uint32_t seqno;
1757
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001758 if (!dev_priv->hw_status_page || list_empty(&dev_priv->mm.request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001759 return;
1760
Eric Anholt673a3942008-07-30 12:06:12 -07001761 seqno = i915_get_gem_seqno(dev);
1762
1763 while (!list_empty(&dev_priv->mm.request_list)) {
1764 struct drm_i915_gem_request *request;
1765 uint32_t retiring_seqno;
1766
1767 request = list_first_entry(&dev_priv->mm.request_list,
1768 struct drm_i915_gem_request,
1769 list);
1770 retiring_seqno = request->seqno;
1771
1772 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001773 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001774 i915_gem_retire_request(dev, request);
1775
1776 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001777 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001778 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001779 } else
1780 break;
1781 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001782
1783 if (unlikely (dev_priv->trace_irq_seqno &&
1784 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
1785 i915_user_irq_put(dev);
1786 dev_priv->trace_irq_seqno = 0;
1787 }
Eric Anholt673a3942008-07-30 12:06:12 -07001788}
1789
1790void
1791i915_gem_retire_work_handler(struct work_struct *work)
1792{
1793 drm_i915_private_t *dev_priv;
1794 struct drm_device *dev;
1795
1796 dev_priv = container_of(work, drm_i915_private_t,
1797 mm.retire_work.work);
1798 dev = dev_priv->dev;
1799
1800 mutex_lock(&dev->struct_mutex);
1801 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001802 if (!dev_priv->mm.suspended &&
1803 !list_empty(&dev_priv->mm.request_list))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001804 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001805 mutex_unlock(&dev->struct_mutex);
1806}
1807
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001808int
Daniel Vetter48764bf2009-09-15 22:57:32 +02001809i915_do_wait_request(struct drm_device *dev, uint32_t seqno, int interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07001810{
1811 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001812 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001813 int ret = 0;
1814
1815 BUG_ON(seqno == 0);
1816
Ben Gamariba1234d2009-09-14 17:48:47 -04001817 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001818 return -EIO;
1819
Eric Anholt673a3942008-07-30 12:06:12 -07001820 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001821 if (IS_IRONLAKE(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001822 ier = I915_READ(DEIER) | I915_READ(GTIER);
1823 else
1824 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001825 if (!ier) {
1826 DRM_ERROR("something (likely vbetool) disabled "
1827 "interrupts, re-enabling\n");
1828 i915_driver_irq_preinstall(dev);
1829 i915_driver_irq_postinstall(dev);
1830 }
1831
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001832 trace_i915_gem_request_wait_begin(dev, seqno);
1833
Eric Anholt673a3942008-07-30 12:06:12 -07001834 dev_priv->mm.waiting_gem_seqno = seqno;
1835 i915_user_irq_get(dev);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001836 if (interruptible)
1837 ret = wait_event_interruptible(dev_priv->irq_queue,
1838 i915_seqno_passed(i915_get_gem_seqno(dev), seqno) ||
1839 atomic_read(&dev_priv->mm.wedged));
1840 else
1841 wait_event(dev_priv->irq_queue,
1842 i915_seqno_passed(i915_get_gem_seqno(dev), seqno) ||
1843 atomic_read(&dev_priv->mm.wedged));
1844
Eric Anholt673a3942008-07-30 12:06:12 -07001845 i915_user_irq_put(dev);
1846 dev_priv->mm.waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001847
1848 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001849 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001850 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001851 ret = -EIO;
1852
1853 if (ret && ret != -ERESTARTSYS)
1854 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1855 __func__, ret, seqno, i915_get_gem_seqno(dev));
1856
1857 /* Directly dispatch request retiring. While we have the work queue
1858 * to handle this, the waiter on a request often wants an associated
1859 * buffer to have made it to the inactive list, and we would need
1860 * a separate wait queue to handle that.
1861 */
1862 if (ret == 0)
1863 i915_gem_retire_requests(dev);
1864
1865 return ret;
1866}
1867
Daniel Vetter48764bf2009-09-15 22:57:32 +02001868/**
1869 * Waits for a sequence number to be signaled, and cleans up the
1870 * request and object lists appropriately for that event.
1871 */
1872static int
1873i915_wait_request(struct drm_device *dev, uint32_t seqno)
1874{
1875 return i915_do_wait_request(dev, seqno, 1);
1876}
1877
Eric Anholt673a3942008-07-30 12:06:12 -07001878static void
1879i915_gem_flush(struct drm_device *dev,
1880 uint32_t invalidate_domains,
1881 uint32_t flush_domains)
1882{
1883 drm_i915_private_t *dev_priv = dev->dev_private;
1884 uint32_t cmd;
1885 RING_LOCALS;
1886
1887#if WATCH_EXEC
1888 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1889 invalidate_domains, flush_domains);
1890#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001891 trace_i915_gem_request_flush(dev, dev_priv->mm.next_gem_seqno,
1892 invalidate_domains, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001893
1894 if (flush_domains & I915_GEM_DOMAIN_CPU)
1895 drm_agp_chipset_flush(dev);
1896
Chris Wilson21d509e2009-06-06 09:46:02 +01001897 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
Eric Anholt673a3942008-07-30 12:06:12 -07001898 /*
1899 * read/write caches:
1900 *
1901 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1902 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1903 * also flushed at 2d versus 3d pipeline switches.
1904 *
1905 * read-only caches:
1906 *
1907 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1908 * MI_READ_FLUSH is set, and is always flushed on 965.
1909 *
1910 * I915_GEM_DOMAIN_COMMAND may not exist?
1911 *
1912 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1913 * invalidated when MI_EXE_FLUSH is set.
1914 *
1915 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1916 * invalidated with every MI_FLUSH.
1917 *
1918 * TLBs:
1919 *
1920 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1921 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1922 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1923 * are flushed at any MI_FLUSH.
1924 */
1925
1926 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1927 if ((invalidate_domains|flush_domains) &
1928 I915_GEM_DOMAIN_RENDER)
1929 cmd &= ~MI_NO_WRITE_FLUSH;
1930 if (!IS_I965G(dev)) {
1931 /*
1932 * On the 965, the sampler cache always gets flushed
1933 * and this bit is reserved.
1934 */
1935 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1936 cmd |= MI_READ_FLUSH;
1937 }
1938 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1939 cmd |= MI_EXE_FLUSH;
1940
1941#if WATCH_EXEC
1942 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1943#endif
1944 BEGIN_LP_RING(2);
1945 OUT_RING(cmd);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001946 OUT_RING(MI_NOOP);
Eric Anholt673a3942008-07-30 12:06:12 -07001947 ADVANCE_LP_RING();
1948 }
1949}
1950
1951/**
1952 * Ensures that all rendering to the object has completed and the object is
1953 * safe to unbind from the GTT or access from the CPU.
1954 */
1955static int
1956i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1957{
1958 struct drm_device *dev = obj->dev;
1959 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1960 int ret;
1961
Eric Anholte47c68e2008-11-14 13:35:19 -08001962 /* This function only exists to support waiting for existing rendering,
1963 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001964 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001965 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001966
1967 /* If there is rendering queued on the buffer being evicted, wait for
1968 * it.
1969 */
1970 if (obj_priv->active) {
1971#if WATCH_BUF
1972 DRM_INFO("%s: object %p wait for seqno %08x\n",
1973 __func__, obj, obj_priv->last_rendering_seqno);
1974#endif
1975 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1976 if (ret != 0)
1977 return ret;
1978 }
1979
1980 return 0;
1981}
1982
1983/**
1984 * Unbinds an object from the GTT aperture.
1985 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001986int
Eric Anholt673a3942008-07-30 12:06:12 -07001987i915_gem_object_unbind(struct drm_gem_object *obj)
1988{
1989 struct drm_device *dev = obj->dev;
1990 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1991 int ret = 0;
1992
1993#if WATCH_BUF
1994 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1995 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1996#endif
1997 if (obj_priv->gtt_space == NULL)
1998 return 0;
1999
2000 if (obj_priv->pin_count != 0) {
2001 DRM_ERROR("Attempting to unbind pinned buffer\n");
2002 return -EINVAL;
2003 }
2004
Eric Anholt5323fd02009-09-09 11:50:45 -07002005 /* blow away mappings if mapped through GTT */
2006 i915_gem_release_mmap(obj);
2007
Eric Anholt673a3942008-07-30 12:06:12 -07002008 /* Move the object to the CPU domain to ensure that
2009 * any possible CPU writes while it's not in the GTT
2010 * are flushed when we go to remap it. This will
2011 * also ensure that all pending GPU writes are finished
2012 * before we unbind.
2013 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002014 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002015 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08002016 if (ret != -ERESTARTSYS)
2017 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002018 return ret;
2019 }
2020
Eric Anholt5323fd02009-09-09 11:50:45 -07002021 BUG_ON(obj_priv->active);
2022
Daniel Vetter96b47b62009-12-15 17:50:00 +01002023 /* release the fence reg _after_ flushing */
2024 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2025 i915_gem_clear_fence_reg(obj);
2026
Eric Anholt673a3942008-07-30 12:06:12 -07002027 if (obj_priv->agp_mem != NULL) {
2028 drm_unbind_agp(obj_priv->agp_mem);
2029 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2030 obj_priv->agp_mem = NULL;
2031 }
2032
Eric Anholt856fa192009-03-19 14:10:50 -07002033 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002034 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002035
2036 if (obj_priv->gtt_space) {
2037 atomic_dec(&dev->gtt_count);
2038 atomic_sub(obj->size, &dev->gtt_memory);
2039
2040 drm_mm_put_block(obj_priv->gtt_space);
2041 obj_priv->gtt_space = NULL;
2042 }
2043
2044 /* Remove ourselves from the LRU list if present. */
2045 if (!list_empty(&obj_priv->list))
2046 list_del_init(&obj_priv->list);
2047
Chris Wilson963b4832009-09-20 23:03:54 +01002048 if (i915_gem_object_is_purgeable(obj_priv))
2049 i915_gem_object_truncate(obj);
2050
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002051 trace_i915_gem_object_unbind(obj);
2052
Eric Anholt673a3942008-07-30 12:06:12 -07002053 return 0;
2054}
2055
Chris Wilson07f73f62009-09-14 16:50:30 +01002056static struct drm_gem_object *
2057i915_gem_find_inactive_object(struct drm_device *dev, int min_size)
2058{
2059 drm_i915_private_t *dev_priv = dev->dev_private;
2060 struct drm_i915_gem_object *obj_priv;
2061 struct drm_gem_object *best = NULL;
2062 struct drm_gem_object *first = NULL;
2063
2064 /* Try to find the smallest clean object */
2065 list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
2066 struct drm_gem_object *obj = obj_priv->obj;
2067 if (obj->size >= min_size) {
Chris Wilson963b4832009-09-20 23:03:54 +01002068 if ((!obj_priv->dirty ||
2069 i915_gem_object_is_purgeable(obj_priv)) &&
Chris Wilson07f73f62009-09-14 16:50:30 +01002070 (!best || obj->size < best->size)) {
2071 best = obj;
2072 if (best->size == min_size)
2073 return best;
2074 }
2075 if (!first)
2076 first = obj;
2077 }
2078 }
2079
2080 return best ? best : first;
2081}
2082
Eric Anholt673a3942008-07-30 12:06:12 -07002083static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002084i915_gem_evict_everything(struct drm_device *dev)
2085{
2086 drm_i915_private_t *dev_priv = dev->dev_private;
2087 uint32_t seqno;
2088 int ret;
2089 bool lists_empty;
2090
Chris Wilson07f73f62009-09-14 16:50:30 +01002091 spin_lock(&dev_priv->mm.active_list_lock);
2092 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2093 list_empty(&dev_priv->mm.flushing_list) &&
2094 list_empty(&dev_priv->mm.active_list));
2095 spin_unlock(&dev_priv->mm.active_list_lock);
2096
Chris Wilson97311292009-09-21 00:22:34 +01002097 if (lists_empty)
Chris Wilson07f73f62009-09-14 16:50:30 +01002098 return -ENOSPC;
Chris Wilson07f73f62009-09-14 16:50:30 +01002099
2100 /* Flush everything (on to the inactive lists) and evict */
2101 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2102 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
2103 if (seqno == 0)
2104 return -ENOMEM;
2105
2106 ret = i915_wait_request(dev, seqno);
2107 if (ret)
2108 return ret;
2109
Chris Wilsonab5ee572009-09-20 19:25:47 +01002110 ret = i915_gem_evict_from_inactive_list(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002111 if (ret)
2112 return ret;
2113
2114 spin_lock(&dev_priv->mm.active_list_lock);
2115 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2116 list_empty(&dev_priv->mm.flushing_list) &&
2117 list_empty(&dev_priv->mm.active_list));
2118 spin_unlock(&dev_priv->mm.active_list_lock);
2119 BUG_ON(!lists_empty);
2120
Eric Anholt673a3942008-07-30 12:06:12 -07002121 return 0;
2122}
2123
2124static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002125i915_gem_evict_something(struct drm_device *dev, int min_size)
Eric Anholt673a3942008-07-30 12:06:12 -07002126{
2127 drm_i915_private_t *dev_priv = dev->dev_private;
2128 struct drm_gem_object *obj;
Chris Wilson07f73f62009-09-14 16:50:30 +01002129 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002130
2131 for (;;) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002132 i915_gem_retire_requests(dev);
2133
Eric Anholt673a3942008-07-30 12:06:12 -07002134 /* If there's an inactive buffer available now, grab it
2135 * and be done.
2136 */
Chris Wilson07f73f62009-09-14 16:50:30 +01002137 obj = i915_gem_find_inactive_object(dev, min_size);
2138 if (obj) {
2139 struct drm_i915_gem_object *obj_priv;
2140
Eric Anholt673a3942008-07-30 12:06:12 -07002141#if WATCH_LRU
2142 DRM_INFO("%s: evicting %p\n", __func__, obj);
2143#endif
Chris Wilson07f73f62009-09-14 16:50:30 +01002144 obj_priv = obj->driver_private;
2145 BUG_ON(obj_priv->pin_count != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002146 BUG_ON(obj_priv->active);
2147
2148 /* Wait on the rendering and unbind the buffer. */
Chris Wilson07f73f62009-09-14 16:50:30 +01002149 return i915_gem_object_unbind(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002150 }
2151
2152 /* If we didn't get anything, but the ring is still processing
Chris Wilson07f73f62009-09-14 16:50:30 +01002153 * things, wait for the next to finish and hopefully leave us
2154 * a buffer to evict.
Eric Anholt673a3942008-07-30 12:06:12 -07002155 */
2156 if (!list_empty(&dev_priv->mm.request_list)) {
2157 struct drm_i915_gem_request *request;
2158
2159 request = list_first_entry(&dev_priv->mm.request_list,
2160 struct drm_i915_gem_request,
2161 list);
2162
2163 ret = i915_wait_request(dev, request->seqno);
2164 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002165 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002166
Chris Wilson07f73f62009-09-14 16:50:30 +01002167 continue;
Eric Anholt673a3942008-07-30 12:06:12 -07002168 }
2169
2170 /* If we didn't have anything on the request list but there
2171 * are buffers awaiting a flush, emit one and try again.
2172 * When we wait on it, those buffers waiting for that flush
2173 * will get moved to inactive.
2174 */
2175 if (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002176 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002177
Chris Wilson9a1e2582009-09-20 20:16:50 +01002178 /* Find an object that we can immediately reuse */
2179 list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
2180 obj = obj_priv->obj;
2181 if (obj->size >= min_size)
2182 break;
Eric Anholt673a3942008-07-30 12:06:12 -07002183
Chris Wilson9a1e2582009-09-20 20:16:50 +01002184 obj = NULL;
2185 }
Eric Anholt673a3942008-07-30 12:06:12 -07002186
Chris Wilson9a1e2582009-09-20 20:16:50 +01002187 if (obj != NULL) {
2188 uint32_t seqno;
Chris Wilson07f73f62009-09-14 16:50:30 +01002189
Chris Wilson9a1e2582009-09-20 20:16:50 +01002190 i915_gem_flush(dev,
2191 obj->write_domain,
2192 obj->write_domain);
2193 seqno = i915_add_request(dev, NULL, obj->write_domain);
2194 if (seqno == 0)
2195 return -ENOMEM;
2196
2197 ret = i915_wait_request(dev, seqno);
2198 if (ret)
2199 return ret;
2200
2201 continue;
2202 }
Eric Anholt673a3942008-07-30 12:06:12 -07002203 }
2204
Chris Wilson07f73f62009-09-14 16:50:30 +01002205 /* If we didn't do any of the above, there's no single buffer
2206 * large enough to swap out for the new one, so just evict
2207 * everything and start again. (This should be rare.)
Eric Anholt673a3942008-07-30 12:06:12 -07002208 */
Chris Wilson97311292009-09-21 00:22:34 +01002209 if (!list_empty (&dev_priv->mm.inactive_list))
Chris Wilsonab5ee572009-09-20 19:25:47 +01002210 return i915_gem_evict_from_inactive_list(dev);
Chris Wilson97311292009-09-21 00:22:34 +01002211 else
Chris Wilson07f73f62009-09-14 16:50:30 +01002212 return i915_gem_evict_everything(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002213 }
Keith Packardac94a962008-11-20 23:30:27 -08002214}
2215
Ben Gamari6911a9b2009-04-02 11:24:54 -07002216int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002217i915_gem_object_get_pages(struct drm_gem_object *obj,
2218 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002219{
2220 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2221 int page_count, i;
2222 struct address_space *mapping;
2223 struct inode *inode;
2224 struct page *page;
2225 int ret;
2226
Eric Anholt856fa192009-03-19 14:10:50 -07002227 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002228 return 0;
2229
2230 /* Get the list of pages out of our struct file. They'll be pinned
2231 * at this point until we release them.
2232 */
2233 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002234 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002235 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002236 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002237 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002238 return -ENOMEM;
2239 }
2240
2241 inode = obj->filp->f_path.dentry->d_inode;
2242 mapping = inode->i_mapping;
2243 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002244 page = read_cache_page_gfp(mapping, i,
2245 mapping_gfp_mask (mapping) |
2246 __GFP_COLD |
2247 gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002248 if (IS_ERR(page)) {
2249 ret = PTR_ERR(page);
Eric Anholt856fa192009-03-19 14:10:50 -07002250 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002251 return ret;
2252 }
Eric Anholt856fa192009-03-19 14:10:50 -07002253 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002254 }
Eric Anholt280b7132009-03-12 16:56:27 -07002255
2256 if (obj_priv->tiling_mode != I915_TILING_NONE)
2257 i915_gem_object_do_bit_17_swizzle(obj);
2258
Eric Anholt673a3942008-07-30 12:06:12 -07002259 return 0;
2260}
2261
Jesse Barnesde151cf2008-11-12 10:03:55 -08002262static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2263{
2264 struct drm_gem_object *obj = reg->obj;
2265 struct drm_device *dev = obj->dev;
2266 drm_i915_private_t *dev_priv = dev->dev_private;
2267 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2268 int regnum = obj_priv->fence_reg;
2269 uint64_t val;
2270
2271 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2272 0xfffff000) << 32;
2273 val |= obj_priv->gtt_offset & 0xfffff000;
2274 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2275 if (obj_priv->tiling_mode == I915_TILING_Y)
2276 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2277 val |= I965_FENCE_REG_VALID;
2278
2279 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2280}
2281
2282static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2283{
2284 struct drm_gem_object *obj = reg->obj;
2285 struct drm_device *dev = obj->dev;
2286 drm_i915_private_t *dev_priv = dev->dev_private;
2287 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2288 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002289 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002290 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002291 uint32_t pitch_val;
2292
2293 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2294 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002295 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002296 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002297 return;
2298 }
2299
Jesse Barnes0f973f22009-01-26 17:10:45 -08002300 if (obj_priv->tiling_mode == I915_TILING_Y &&
2301 HAS_128_BYTE_Y_TILING(dev))
2302 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002303 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002304 tile_width = 512;
2305
2306 /* Note: pitch better be a power of two tile widths */
2307 pitch_val = obj_priv->stride / tile_width;
2308 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002309
2310 val = obj_priv->gtt_offset;
2311 if (obj_priv->tiling_mode == I915_TILING_Y)
2312 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2313 val |= I915_FENCE_SIZE_BITS(obj->size);
2314 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2315 val |= I830_FENCE_REG_VALID;
2316
Eric Anholtdc529a42009-03-10 22:34:49 -07002317 if (regnum < 8)
2318 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2319 else
2320 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2321 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002322}
2323
2324static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2325{
2326 struct drm_gem_object *obj = reg->obj;
2327 struct drm_device *dev = obj->dev;
2328 drm_i915_private_t *dev_priv = dev->dev_private;
2329 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2330 int regnum = obj_priv->fence_reg;
2331 uint32_t val;
2332 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002333 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002334
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002335 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002336 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002337 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002338 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002339 return;
2340 }
2341
Eric Anholte76a16d2009-05-26 17:44:56 -07002342 pitch_val = obj_priv->stride / 128;
2343 pitch_val = ffs(pitch_val) - 1;
2344 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2345
Jesse Barnesde151cf2008-11-12 10:03:55 -08002346 val = obj_priv->gtt_offset;
2347 if (obj_priv->tiling_mode == I915_TILING_Y)
2348 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002349 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2350 WARN_ON(fence_size_bits & ~0x00000f00);
2351 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2353 val |= I830_FENCE_REG_VALID;
2354
2355 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356}
2357
2358/**
2359 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2360 * @obj: object to map through a fence reg
2361 *
2362 * When mapping objects through the GTT, userspace wants to be able to write
2363 * to them without having to worry about swizzling if the object is tiled.
2364 *
2365 * This function walks the fence regs looking for a free one for @obj,
2366 * stealing one if it can't find any.
2367 *
2368 * It then sets up the reg based on the object's properties: address, pitch
2369 * and tiling format.
2370 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002371int
2372i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002373{
2374 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002375 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002376 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2377 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002378 struct drm_i915_gem_object *old_obj_priv = NULL;
2379 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002380
Eric Anholta09ba7f2009-08-29 12:49:51 -07002381 /* Just update our place in the LRU if our fence is getting used. */
2382 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
2383 list_move_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2384 return 0;
2385 }
2386
Jesse Barnesde151cf2008-11-12 10:03:55 -08002387 switch (obj_priv->tiling_mode) {
2388 case I915_TILING_NONE:
2389 WARN(1, "allocating a fence for non-tiled object?\n");
2390 break;
2391 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002392 if (!obj_priv->stride)
2393 return -EINVAL;
2394 WARN((obj_priv->stride & (512 - 1)),
2395 "object 0x%08x is X tiled but has non-512B pitch\n",
2396 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002397 break;
2398 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002399 if (!obj_priv->stride)
2400 return -EINVAL;
2401 WARN((obj_priv->stride & (128 - 1)),
2402 "object 0x%08x is Y tiled but has non-128B pitch\n",
2403 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002404 break;
2405 }
2406
2407 /* First try to find a free reg */
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002408 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002409 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2410 reg = &dev_priv->fence_regs[i];
2411 if (!reg->obj)
2412 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002413
2414 old_obj_priv = reg->obj->driver_private;
2415 if (!old_obj_priv->pin_count)
2416 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002417 }
2418
2419 /* None available, try to steal one or wait for a user to finish */
2420 if (i == dev_priv->num_fence_regs) {
Eric Anholta09ba7f2009-08-29 12:49:51 -07002421 struct drm_gem_object *old_obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002422
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002423 if (avail == 0)
Chris Wilson2939e1f2009-06-06 09:46:03 +01002424 return -ENOSPC;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002425
Eric Anholta09ba7f2009-08-29 12:49:51 -07002426 list_for_each_entry(old_obj_priv, &dev_priv->mm.fence_list,
2427 fence_list) {
2428 old_obj = old_obj_priv->obj;
Chris Wilsond7619c42009-02-11 14:26:47 +00002429
Chris Wilsond7619c42009-02-11 14:26:47 +00002430 if (old_obj_priv->pin_count)
2431 continue;
2432
Eric Anholta09ba7f2009-08-29 12:49:51 -07002433 /* Take a reference, as otherwise the wait_rendering
2434 * below may cause the object to get freed out from
2435 * under us.
2436 */
2437 drm_gem_object_reference(old_obj);
2438
Chris Wilsond7619c42009-02-11 14:26:47 +00002439 /* i915 uses fences for GPU access to tiled buffers */
2440 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002441 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002442
Eric Anholta09ba7f2009-08-29 12:49:51 -07002443 /* This brings the object to the head of the LRU if it
2444 * had been written to. The only way this should
2445 * result in us waiting longer than the expected
2446 * optimal amount of time is if there was a
2447 * fence-using buffer later that was read-only.
2448 */
2449 i915_gem_object_flush_gpu_write_domain(old_obj);
2450 ret = i915_gem_object_wait_rendering(old_obj);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002451 if (ret != 0) {
2452 drm_gem_object_unreference(old_obj);
Chris Wilsond7619c42009-02-11 14:26:47 +00002453 return ret;
Chris Wilson58c2fb62009-09-01 12:02:39 +01002454 }
2455
Eric Anholta09ba7f2009-08-29 12:49:51 -07002456 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002457 }
2458
2459 /*
2460 * Zap this virtual mapping so we can set up a fence again
2461 * for this object next time we need it.
2462 */
Chris Wilson58c2fb62009-09-01 12:02:39 +01002463 i915_gem_release_mmap(old_obj);
2464
Eric Anholta09ba7f2009-08-29 12:49:51 -07002465 i = old_obj_priv->fence_reg;
Chris Wilson58c2fb62009-09-01 12:02:39 +01002466 reg = &dev_priv->fence_regs[i];
2467
Jesse Barnesde151cf2008-11-12 10:03:55 -08002468 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002469 list_del_init(&old_obj_priv->fence_list);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002470
Eric Anholta09ba7f2009-08-29 12:49:51 -07002471 drm_gem_object_unreference(old_obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002472 }
2473
2474 obj_priv->fence_reg = i;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002475 list_add_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2476
Jesse Barnesde151cf2008-11-12 10:03:55 -08002477 reg->obj = obj;
2478
2479 if (IS_I965G(dev))
2480 i965_write_fence_reg(reg);
2481 else if (IS_I9XX(dev))
2482 i915_write_fence_reg(reg);
2483 else
2484 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002485
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002486 trace_i915_gem_object_get_fence(obj, i, obj_priv->tiling_mode);
2487
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002488 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002489}
2490
2491/**
2492 * i915_gem_clear_fence_reg - clear out fence register info
2493 * @obj: object to clear
2494 *
2495 * Zeroes out the fence register itself and clears out the associated
2496 * data structures in dev_priv and obj_priv.
2497 */
2498static void
2499i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2500{
2501 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002502 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002503 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2504
2505 if (IS_I965G(dev))
2506 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002507 else {
2508 uint32_t fence_reg;
2509
2510 if (obj_priv->fence_reg < 8)
2511 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2512 else
2513 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2514 8) * 4;
2515
2516 I915_WRITE(fence_reg, 0);
2517 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002518
2519 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2520 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002521 list_del_init(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002522}
2523
Eric Anholt673a3942008-07-30 12:06:12 -07002524/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002525 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2526 * to the buffer to finish, and then resets the fence register.
2527 * @obj: tiled object holding a fence register.
2528 *
2529 * Zeroes out the fence register itself and clears out the associated
2530 * data structures in dev_priv and obj_priv.
2531 */
2532int
2533i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2534{
2535 struct drm_device *dev = obj->dev;
2536 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2537
2538 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2539 return 0;
2540
2541 /* On the i915, GPU access to tiled buffers is via a fence,
2542 * therefore we must wait for any outstanding access to complete
2543 * before clearing the fence.
2544 */
2545 if (!IS_I965G(dev)) {
2546 int ret;
2547
2548 i915_gem_object_flush_gpu_write_domain(obj);
2549 i915_gem_object_flush_gtt_write_domain(obj);
2550 ret = i915_gem_object_wait_rendering(obj);
2551 if (ret != 0)
2552 return ret;
2553 }
2554
2555 i915_gem_clear_fence_reg (obj);
2556
2557 return 0;
2558}
2559
2560/**
Eric Anholt673a3942008-07-30 12:06:12 -07002561 * Finds free space in the GTT aperture and binds the object there.
2562 */
2563static int
2564i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2565{
2566 struct drm_device *dev = obj->dev;
2567 drm_i915_private_t *dev_priv = dev->dev_private;
2568 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2569 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002570 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002571 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002572
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002573 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002574 DRM_ERROR("Attempting to bind a purgeable object\n");
2575 return -EINVAL;
2576 }
2577
Eric Anholt673a3942008-07-30 12:06:12 -07002578 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002579 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002580 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002581 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2582 return -EINVAL;
2583 }
2584
2585 search_free:
2586 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2587 obj->size, alignment, 0);
2588 if (free_space != NULL) {
2589 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2590 alignment);
2591 if (obj_priv->gtt_space != NULL) {
2592 obj_priv->gtt_space->private = obj;
2593 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2594 }
2595 }
2596 if (obj_priv->gtt_space == NULL) {
2597 /* If the gtt is empty and we're still having trouble
2598 * fitting our object in, we're out of memory.
2599 */
2600#if WATCH_LRU
2601 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2602#endif
Chris Wilson07f73f62009-09-14 16:50:30 +01002603 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002604 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002605 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002606
Eric Anholt673a3942008-07-30 12:06:12 -07002607 goto search_free;
2608 }
2609
2610#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002611 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002612 obj->size, obj_priv->gtt_offset);
2613#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002614 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002615 if (ret) {
2616 drm_mm_put_block(obj_priv->gtt_space);
2617 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002618
2619 if (ret == -ENOMEM) {
2620 /* first try to clear up some space from the GTT */
2621 ret = i915_gem_evict_something(dev, obj->size);
2622 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002623 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002624 if (gfpmask) {
2625 gfpmask = 0;
2626 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002627 }
2628
2629 return ret;
2630 }
2631
2632 goto search_free;
2633 }
2634
Eric Anholt673a3942008-07-30 12:06:12 -07002635 return ret;
2636 }
2637
Eric Anholt673a3942008-07-30 12:06:12 -07002638 /* Create an AGP memory structure pointing at our pages, and bind it
2639 * into the GTT.
2640 */
2641 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002642 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002643 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002644 obj_priv->gtt_offset,
2645 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002646 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002647 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002648 drm_mm_put_block(obj_priv->gtt_space);
2649 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002650
2651 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002652 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002653 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002654
2655 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002656 }
2657 atomic_inc(&dev->gtt_count);
2658 atomic_add(obj->size, &dev->gtt_memory);
2659
2660 /* Assert that the object is not currently in any GPU domain. As it
2661 * wasn't in the GTT, there shouldn't be any way it could have been in
2662 * a GPU cache
2663 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002664 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2665 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002666
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002667 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2668
Eric Anholt673a3942008-07-30 12:06:12 -07002669 return 0;
2670}
2671
2672void
2673i915_gem_clflush_object(struct drm_gem_object *obj)
2674{
2675 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2676
2677 /* If we don't have a page list set up, then we're not pinned
2678 * to GPU, and we can ignore the cache flush because it'll happen
2679 * again at bind time.
2680 */
Eric Anholt856fa192009-03-19 14:10:50 -07002681 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002682 return;
2683
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002684 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002685
Eric Anholt856fa192009-03-19 14:10:50 -07002686 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002687}
2688
Eric Anholte47c68e2008-11-14 13:35:19 -08002689/** Flushes any GPU write domain for the object if it's dirty. */
2690static void
2691i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2692{
2693 struct drm_device *dev = obj->dev;
2694 uint32_t seqno;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002695 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002696
2697 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2698 return;
2699
2700 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002701 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002702 i915_gem_flush(dev, 0, obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002703 seqno = i915_add_request(dev, NULL, obj->write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002704 obj->write_domain = 0;
2705 i915_gem_object_move_to_active(obj, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002706
2707 trace_i915_gem_object_change_domain(obj,
2708 obj->read_domains,
2709 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002710}
2711
2712/** Flushes the GTT write domain for the object if it's dirty. */
2713static void
2714i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2715{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002716 uint32_t old_write_domain;
2717
Eric Anholte47c68e2008-11-14 13:35:19 -08002718 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2719 return;
2720
2721 /* No actual flushing is required for the GTT write domain. Writes
2722 * to it immediately go to main memory as far as we know, so there's
2723 * no chipset flush. It also doesn't land in render cache.
2724 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002725 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002726 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002727
2728 trace_i915_gem_object_change_domain(obj,
2729 obj->read_domains,
2730 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002731}
2732
2733/** Flushes the CPU write domain for the object if it's dirty. */
2734static void
2735i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2736{
2737 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002738 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002739
2740 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2741 return;
2742
2743 i915_gem_clflush_object(obj);
2744 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002745 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002746 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002747
2748 trace_i915_gem_object_change_domain(obj,
2749 obj->read_domains,
2750 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002751}
2752
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002753void
2754i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2755{
2756 switch (obj->write_domain) {
2757 case I915_GEM_DOMAIN_GTT:
2758 i915_gem_object_flush_gtt_write_domain(obj);
2759 break;
2760 case I915_GEM_DOMAIN_CPU:
2761 i915_gem_object_flush_cpu_write_domain(obj);
2762 break;
2763 default:
2764 i915_gem_object_flush_gpu_write_domain(obj);
2765 break;
2766 }
2767}
2768
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002769/**
2770 * Moves a single object to the GTT read, and possibly write domain.
2771 *
2772 * This function returns when the move is complete, including waiting on
2773 * flushes to occur.
2774 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002775int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002776i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2777{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002778 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002779 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002780 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002781
Eric Anholt02354392008-11-26 13:58:13 -08002782 /* Not valid to be called on unbound objects. */
2783 if (obj_priv->gtt_space == NULL)
2784 return -EINVAL;
2785
Eric Anholte47c68e2008-11-14 13:35:19 -08002786 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002787 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002788 ret = i915_gem_object_wait_rendering(obj);
2789 if (ret != 0)
2790 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002791
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002792 old_write_domain = obj->write_domain;
2793 old_read_domains = obj->read_domains;
2794
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002795 /* If we're writing through the GTT domain, then CPU and GPU caches
2796 * will need to be invalidated at next use.
2797 */
2798 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002799 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002800
Eric Anholte47c68e2008-11-14 13:35:19 -08002801 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002802
2803 /* It should now be out of any other write domains, and we can update
2804 * the domain values for our changes.
2805 */
2806 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2807 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002808 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002809 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002810 obj_priv->dirty = 1;
2811 }
2812
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002813 trace_i915_gem_object_change_domain(obj,
2814 old_read_domains,
2815 old_write_domain);
2816
Eric Anholte47c68e2008-11-14 13:35:19 -08002817 return 0;
2818}
2819
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002820/*
2821 * Prepare buffer for display plane. Use uninterruptible for possible flush
2822 * wait, as in modesetting process we're not supposed to be interrupted.
2823 */
2824int
2825i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2826{
2827 struct drm_device *dev = obj->dev;
2828 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2829 uint32_t old_write_domain, old_read_domains;
2830 int ret;
2831
2832 /* Not valid to be called on unbound objects. */
2833 if (obj_priv->gtt_space == NULL)
2834 return -EINVAL;
2835
2836 i915_gem_object_flush_gpu_write_domain(obj);
2837
2838 /* Wait on any GPU rendering and flushing to occur. */
2839 if (obj_priv->active) {
2840#if WATCH_BUF
2841 DRM_INFO("%s: object %p wait for seqno %08x\n",
2842 __func__, obj, obj_priv->last_rendering_seqno);
2843#endif
2844 ret = i915_do_wait_request(dev, obj_priv->last_rendering_seqno, 0);
2845 if (ret != 0)
2846 return ret;
2847 }
2848
2849 old_write_domain = obj->write_domain;
2850 old_read_domains = obj->read_domains;
2851
2852 obj->read_domains &= I915_GEM_DOMAIN_GTT;
2853
2854 i915_gem_object_flush_cpu_write_domain(obj);
2855
2856 /* It should now be out of any other write domains, and we can update
2857 * the domain values for our changes.
2858 */
2859 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2860 obj->read_domains |= I915_GEM_DOMAIN_GTT;
2861 obj->write_domain = I915_GEM_DOMAIN_GTT;
2862 obj_priv->dirty = 1;
2863
2864 trace_i915_gem_object_change_domain(obj,
2865 old_read_domains,
2866 old_write_domain);
2867
2868 return 0;
2869}
2870
Eric Anholte47c68e2008-11-14 13:35:19 -08002871/**
2872 * Moves a single object to the CPU read, and possibly write domain.
2873 *
2874 * This function returns when the move is complete, including waiting on
2875 * flushes to occur.
2876 */
2877static int
2878i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2879{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002880 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002881 int ret;
2882
2883 i915_gem_object_flush_gpu_write_domain(obj);
2884 /* Wait on any GPU rendering and flushing to occur. */
2885 ret = i915_gem_object_wait_rendering(obj);
2886 if (ret != 0)
2887 return ret;
2888
2889 i915_gem_object_flush_gtt_write_domain(obj);
2890
2891 /* If we have a partially-valid cache of the object in the CPU,
2892 * finish invalidating it and free the per-page flags.
2893 */
2894 i915_gem_object_set_to_full_cpu_read_domain(obj);
2895
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002896 old_write_domain = obj->write_domain;
2897 old_read_domains = obj->read_domains;
2898
Eric Anholte47c68e2008-11-14 13:35:19 -08002899 /* Flush the CPU cache if it's still invalid. */
2900 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2901 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002902
2903 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2904 }
2905
2906 /* It should now be out of any other write domains, and we can update
2907 * the domain values for our changes.
2908 */
2909 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2910
2911 /* If we're writing through the CPU, then the GPU read domains will
2912 * need to be invalidated at next use.
2913 */
2914 if (write) {
2915 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2916 obj->write_domain = I915_GEM_DOMAIN_CPU;
2917 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002918
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002919 trace_i915_gem_object_change_domain(obj,
2920 old_read_domains,
2921 old_write_domain);
2922
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002923 return 0;
2924}
2925
Eric Anholt673a3942008-07-30 12:06:12 -07002926/*
2927 * Set the next domain for the specified object. This
2928 * may not actually perform the necessary flushing/invaliding though,
2929 * as that may want to be batched with other set_domain operations
2930 *
2931 * This is (we hope) the only really tricky part of gem. The goal
2932 * is fairly simple -- track which caches hold bits of the object
2933 * and make sure they remain coherent. A few concrete examples may
2934 * help to explain how it works. For shorthand, we use the notation
2935 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2936 * a pair of read and write domain masks.
2937 *
2938 * Case 1: the batch buffer
2939 *
2940 * 1. Allocated
2941 * 2. Written by CPU
2942 * 3. Mapped to GTT
2943 * 4. Read by GPU
2944 * 5. Unmapped from GTT
2945 * 6. Freed
2946 *
2947 * Let's take these a step at a time
2948 *
2949 * 1. Allocated
2950 * Pages allocated from the kernel may still have
2951 * cache contents, so we set them to (CPU, CPU) always.
2952 * 2. Written by CPU (using pwrite)
2953 * The pwrite function calls set_domain (CPU, CPU) and
2954 * this function does nothing (as nothing changes)
2955 * 3. Mapped by GTT
2956 * This function asserts that the object is not
2957 * currently in any GPU-based read or write domains
2958 * 4. Read by GPU
2959 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2960 * As write_domain is zero, this function adds in the
2961 * current read domains (CPU+COMMAND, 0).
2962 * flush_domains is set to CPU.
2963 * invalidate_domains is set to COMMAND
2964 * clflush is run to get data out of the CPU caches
2965 * then i915_dev_set_domain calls i915_gem_flush to
2966 * emit an MI_FLUSH and drm_agp_chipset_flush
2967 * 5. Unmapped from GTT
2968 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2969 * flush_domains and invalidate_domains end up both zero
2970 * so no flushing/invalidating happens
2971 * 6. Freed
2972 * yay, done
2973 *
2974 * Case 2: The shared render buffer
2975 *
2976 * 1. Allocated
2977 * 2. Mapped to GTT
2978 * 3. Read/written by GPU
2979 * 4. set_domain to (CPU,CPU)
2980 * 5. Read/written by CPU
2981 * 6. Read/written by GPU
2982 *
2983 * 1. Allocated
2984 * Same as last example, (CPU, CPU)
2985 * 2. Mapped to GTT
2986 * Nothing changes (assertions find that it is not in the GPU)
2987 * 3. Read/written by GPU
2988 * execbuffer calls set_domain (RENDER, RENDER)
2989 * flush_domains gets CPU
2990 * invalidate_domains gets GPU
2991 * clflush (obj)
2992 * MI_FLUSH and drm_agp_chipset_flush
2993 * 4. set_domain (CPU, CPU)
2994 * flush_domains gets GPU
2995 * invalidate_domains gets CPU
2996 * wait_rendering (obj) to make sure all drawing is complete.
2997 * This will include an MI_FLUSH to get the data from GPU
2998 * to memory
2999 * clflush (obj) to invalidate the CPU cache
3000 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3001 * 5. Read/written by CPU
3002 * cache lines are loaded and dirtied
3003 * 6. Read written by GPU
3004 * Same as last GPU access
3005 *
3006 * Case 3: The constant buffer
3007 *
3008 * 1. Allocated
3009 * 2. Written by CPU
3010 * 3. Read by GPU
3011 * 4. Updated (written) by CPU again
3012 * 5. Read by GPU
3013 *
3014 * 1. Allocated
3015 * (CPU, CPU)
3016 * 2. Written by CPU
3017 * (CPU, CPU)
3018 * 3. Read by GPU
3019 * (CPU+RENDER, 0)
3020 * flush_domains = CPU
3021 * invalidate_domains = RENDER
3022 * clflush (obj)
3023 * MI_FLUSH
3024 * drm_agp_chipset_flush
3025 * 4. Updated (written) by CPU again
3026 * (CPU, CPU)
3027 * flush_domains = 0 (no previous write domain)
3028 * invalidate_domains = 0 (no new read domains)
3029 * 5. Read by GPU
3030 * (CPU+RENDER, 0)
3031 * flush_domains = CPU
3032 * invalidate_domains = RENDER
3033 * clflush (obj)
3034 * MI_FLUSH
3035 * drm_agp_chipset_flush
3036 */
Keith Packardc0d90822008-11-20 23:11:08 -08003037static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003038i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003039{
3040 struct drm_device *dev = obj->dev;
3041 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3042 uint32_t invalidate_domains = 0;
3043 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003044 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003045
Eric Anholt8b0e3782009-02-19 14:40:50 -08003046 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3047 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003048
Jesse Barnes652c3932009-08-17 13:31:43 -07003049 intel_mark_busy(dev, obj);
3050
Eric Anholt673a3942008-07-30 12:06:12 -07003051#if WATCH_BUF
3052 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3053 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08003054 obj->read_domains, obj->pending_read_domains,
3055 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003056#endif
3057 /*
3058 * If the object isn't moving to a new write domain,
3059 * let the object stay in multiple read domains
3060 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003061 if (obj->pending_write_domain == 0)
3062 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003063 else
3064 obj_priv->dirty = 1;
3065
3066 /*
3067 * Flush the current write domain if
3068 * the new read domains don't match. Invalidate
3069 * any read domains which differ from the old
3070 * write domain
3071 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003072 if (obj->write_domain &&
3073 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003074 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003075 invalidate_domains |=
3076 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003077 }
3078 /*
3079 * Invalidate any read caches which may have
3080 * stale data. That is, any new read domains.
3081 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003082 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003083 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3084#if WATCH_BUF
3085 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3086 __func__, flush_domains, invalidate_domains);
3087#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003088 i915_gem_clflush_object(obj);
3089 }
3090
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003091 old_read_domains = obj->read_domains;
3092
Eric Anholtefbeed92009-02-19 14:54:51 -08003093 /* The actual obj->write_domain will be updated with
3094 * pending_write_domain after we emit the accumulated flush for all
3095 * of our domain changes in execbuffers (which clears objects'
3096 * write_domains). So if we have a current write domain that we
3097 * aren't changing, set pending_write_domain to that.
3098 */
3099 if (flush_domains == 0 && obj->pending_write_domain == 0)
3100 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003101 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003102
3103 dev->invalidate_domains |= invalidate_domains;
3104 dev->flush_domains |= flush_domains;
3105#if WATCH_BUF
3106 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3107 __func__,
3108 obj->read_domains, obj->write_domain,
3109 dev->invalidate_domains, dev->flush_domains);
3110#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003111
3112 trace_i915_gem_object_change_domain(obj,
3113 old_read_domains,
3114 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003115}
3116
3117/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003118 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003119 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003120 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3121 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3122 */
3123static void
3124i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3125{
Eric Anholte47c68e2008-11-14 13:35:19 -08003126 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3127
3128 if (!obj_priv->page_cpu_valid)
3129 return;
3130
3131 /* If we're partially in the CPU read domain, finish moving it in.
3132 */
3133 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3134 int i;
3135
3136 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3137 if (obj_priv->page_cpu_valid[i])
3138 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003139 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003140 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003141 }
3142
3143 /* Free the page_cpu_valid mappings which are now stale, whether
3144 * or not we've got I915_GEM_DOMAIN_CPU.
3145 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003146 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003147 obj_priv->page_cpu_valid = NULL;
3148}
3149
3150/**
3151 * Set the CPU read domain on a range of the object.
3152 *
3153 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3154 * not entirely valid. The page_cpu_valid member of the object flags which
3155 * pages have been flushed, and will be respected by
3156 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3157 * of the whole object.
3158 *
3159 * This function returns when the move is complete, including waiting on
3160 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003161 */
3162static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003163i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3164 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003165{
3166 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003167 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003168 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003169
Eric Anholte47c68e2008-11-14 13:35:19 -08003170 if (offset == 0 && size == obj->size)
3171 return i915_gem_object_set_to_cpu_domain(obj, 0);
3172
3173 i915_gem_object_flush_gpu_write_domain(obj);
3174 /* Wait on any GPU rendering and flushing to occur. */
3175 ret = i915_gem_object_wait_rendering(obj);
3176 if (ret != 0)
3177 return ret;
3178 i915_gem_object_flush_gtt_write_domain(obj);
3179
3180 /* If we're already fully in the CPU read domain, we're done. */
3181 if (obj_priv->page_cpu_valid == NULL &&
3182 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003183 return 0;
3184
Eric Anholte47c68e2008-11-14 13:35:19 -08003185 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3186 * newly adding I915_GEM_DOMAIN_CPU
3187 */
Eric Anholt673a3942008-07-30 12:06:12 -07003188 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003189 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3190 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003191 if (obj_priv->page_cpu_valid == NULL)
3192 return -ENOMEM;
3193 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3194 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003195
3196 /* Flush the cache on any pages that are still invalid from the CPU's
3197 * perspective.
3198 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003199 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3200 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003201 if (obj_priv->page_cpu_valid[i])
3202 continue;
3203
Eric Anholt856fa192009-03-19 14:10:50 -07003204 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003205
3206 obj_priv->page_cpu_valid[i] = 1;
3207 }
3208
Eric Anholte47c68e2008-11-14 13:35:19 -08003209 /* It should now be out of any other write domains, and we can update
3210 * the domain values for our changes.
3211 */
3212 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3213
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003214 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003215 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3216
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003217 trace_i915_gem_object_change_domain(obj,
3218 old_read_domains,
3219 obj->write_domain);
3220
Eric Anholt673a3942008-07-30 12:06:12 -07003221 return 0;
3222}
3223
3224/**
Eric Anholt673a3942008-07-30 12:06:12 -07003225 * Pin an object to the GTT and evaluate the relocations landing in it.
3226 */
3227static int
3228i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3229 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003230 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003231 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003232{
3233 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003234 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003235 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3236 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003237 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003238 bool need_fence;
3239
3240 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3241 obj_priv->tiling_mode != I915_TILING_NONE;
3242
3243 /* Check fence reg constraints and rebind if necessary */
3244 if (need_fence && !i915_obj_fenceable(dev, obj))
3245 i915_gem_object_unbind(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003246
3247 /* Choose the GTT offset for our buffer and put it there. */
3248 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3249 if (ret)
3250 return ret;
3251
Jesse Barnes76446ca2009-12-17 22:05:42 -05003252 /*
3253 * Pre-965 chips need a fence register set up in order to
3254 * properly handle blits to/from tiled surfaces.
3255 */
3256 if (need_fence) {
3257 ret = i915_gem_object_get_fence_reg(obj);
3258 if (ret != 0) {
3259 if (ret != -EBUSY && ret != -ERESTARTSYS)
3260 DRM_ERROR("Failure to install fence: %d\n",
3261 ret);
3262 i915_gem_object_unpin(obj);
3263 return ret;
3264 }
3265 }
3266
Eric Anholt673a3942008-07-30 12:06:12 -07003267 entry->offset = obj_priv->gtt_offset;
3268
Eric Anholt673a3942008-07-30 12:06:12 -07003269 /* Apply the relocations, using the GTT aperture to avoid cache
3270 * flushing requirements.
3271 */
3272 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003273 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003274 struct drm_gem_object *target_obj;
3275 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003276 uint32_t reloc_val, reloc_offset;
3277 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003278
Eric Anholt673a3942008-07-30 12:06:12 -07003279 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003280 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003281 if (target_obj == NULL) {
3282 i915_gem_object_unpin(obj);
3283 return -EBADF;
3284 }
3285 target_obj_priv = target_obj->driver_private;
3286
Chris Wilson8542a0b2009-09-09 21:15:15 +01003287#if WATCH_RELOC
3288 DRM_INFO("%s: obj %p offset %08x target %d "
3289 "read %08x write %08x gtt %08x "
3290 "presumed %08x delta %08x\n",
3291 __func__,
3292 obj,
3293 (int) reloc->offset,
3294 (int) reloc->target_handle,
3295 (int) reloc->read_domains,
3296 (int) reloc->write_domain,
3297 (int) target_obj_priv->gtt_offset,
3298 (int) reloc->presumed_offset,
3299 reloc->delta);
3300#endif
3301
Eric Anholt673a3942008-07-30 12:06:12 -07003302 /* The target buffer should have appeared before us in the
3303 * exec_object list, so it should have a GTT space bound by now.
3304 */
3305 if (target_obj_priv->gtt_space == NULL) {
3306 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003307 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003308 drm_gem_object_unreference(target_obj);
3309 i915_gem_object_unpin(obj);
3310 return -EINVAL;
3311 }
3312
Chris Wilson8542a0b2009-09-09 21:15:15 +01003313 /* Validate that the target is in a valid r/w GPU domain */
3314 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3315 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3316 DRM_ERROR("reloc with read/write CPU domains: "
3317 "obj %p target %d offset %d "
3318 "read %08x write %08x",
3319 obj, reloc->target_handle,
3320 (int) reloc->offset,
3321 reloc->read_domains,
3322 reloc->write_domain);
3323 drm_gem_object_unreference(target_obj);
3324 i915_gem_object_unpin(obj);
3325 return -EINVAL;
3326 }
3327 if (reloc->write_domain && target_obj->pending_write_domain &&
3328 reloc->write_domain != target_obj->pending_write_domain) {
3329 DRM_ERROR("Write domain conflict: "
3330 "obj %p target %d offset %d "
3331 "new %08x old %08x\n",
3332 obj, reloc->target_handle,
3333 (int) reloc->offset,
3334 reloc->write_domain,
3335 target_obj->pending_write_domain);
3336 drm_gem_object_unreference(target_obj);
3337 i915_gem_object_unpin(obj);
3338 return -EINVAL;
3339 }
3340
3341 target_obj->pending_read_domains |= reloc->read_domains;
3342 target_obj->pending_write_domain |= reloc->write_domain;
3343
3344 /* If the relocation already has the right value in it, no
3345 * more work needs to be done.
3346 */
3347 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3348 drm_gem_object_unreference(target_obj);
3349 continue;
3350 }
3351
3352 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003353 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003354 DRM_ERROR("Relocation beyond object bounds: "
3355 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003356 obj, reloc->target_handle,
3357 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003358 drm_gem_object_unreference(target_obj);
3359 i915_gem_object_unpin(obj);
3360 return -EINVAL;
3361 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003362 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003363 DRM_ERROR("Relocation not 4-byte aligned: "
3364 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003365 obj, reloc->target_handle,
3366 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003367 drm_gem_object_unreference(target_obj);
3368 i915_gem_object_unpin(obj);
3369 return -EINVAL;
3370 }
3371
Chris Wilson8542a0b2009-09-09 21:15:15 +01003372 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003373 if (reloc->delta >= target_obj->size) {
3374 DRM_ERROR("Relocation beyond target object bounds: "
3375 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003376 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003377 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003378 drm_gem_object_unreference(target_obj);
3379 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003380 return -EINVAL;
3381 }
3382
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003383 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3384 if (ret != 0) {
3385 drm_gem_object_unreference(target_obj);
3386 i915_gem_object_unpin(obj);
3387 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003388 }
3389
3390 /* Map the page containing the relocation we're going to
3391 * perform.
3392 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003393 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003394 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3395 (reloc_offset &
3396 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003397 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003398 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003399 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003400
3401#if WATCH_BUF
3402 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003403 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003404 readl(reloc_entry), reloc_val);
3405#endif
3406 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003407 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003408
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003409 /* The updated presumed offset for this entry will be
3410 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003411 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003412 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003413
3414 drm_gem_object_unreference(target_obj);
3415 }
3416
Eric Anholt673a3942008-07-30 12:06:12 -07003417#if WATCH_BUF
3418 if (0)
3419 i915_gem_dump_object(obj, 128, __func__, ~0);
3420#endif
3421 return 0;
3422}
3423
3424/** Dispatch a batchbuffer to the ring
3425 */
3426static int
3427i915_dispatch_gem_execbuffer(struct drm_device *dev,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003428 struct drm_i915_gem_execbuffer2 *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003429 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003430 uint64_t exec_offset)
3431{
3432 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003433 int nbox = exec->num_cliprects;
3434 int i = 0, count;
Chris Wilson83d60792009-06-06 09:45:57 +01003435 uint32_t exec_start, exec_len;
Eric Anholt673a3942008-07-30 12:06:12 -07003436 RING_LOCALS;
3437
3438 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3439 exec_len = (uint32_t) exec->batch_len;
3440
Chris Wilson8f0dc5b2009-09-24 00:43:17 +01003441 trace_i915_gem_request_submit(dev, dev_priv->mm.next_gem_seqno + 1);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003442
Eric Anholt673a3942008-07-30 12:06:12 -07003443 count = nbox ? nbox : 1;
3444
3445 for (i = 0; i < count; i++) {
3446 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003447 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003448 exec->DR1, exec->DR4);
3449 if (ret)
3450 return ret;
3451 }
3452
3453 if (IS_I830(dev) || IS_845G(dev)) {
3454 BEGIN_LP_RING(4);
3455 OUT_RING(MI_BATCH_BUFFER);
3456 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3457 OUT_RING(exec_start + exec_len - 4);
3458 OUT_RING(0);
3459 ADVANCE_LP_RING();
3460 } else {
3461 BEGIN_LP_RING(2);
3462 if (IS_I965G(dev)) {
3463 OUT_RING(MI_BATCH_BUFFER_START |
3464 (2 << 6) |
3465 MI_BATCH_NON_SECURE_I965);
3466 OUT_RING(exec_start);
3467 } else {
3468 OUT_RING(MI_BATCH_BUFFER_START |
3469 (2 << 6));
3470 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3471 }
3472 ADVANCE_LP_RING();
3473 }
3474 }
3475
3476 /* XXX breadcrumb */
3477 return 0;
3478}
3479
3480/* Throttle our rendering by waiting until the ring has completed our requests
3481 * emitted over 20 msec ago.
3482 *
Eric Anholtb9624422009-06-03 07:27:35 +00003483 * Note that if we were to use the current jiffies each time around the loop,
3484 * we wouldn't escape the function with any frames outstanding if the time to
3485 * render a frame was over 20ms.
3486 *
Eric Anholt673a3942008-07-30 12:06:12 -07003487 * This should get us reasonable parallelism between CPU and GPU but also
3488 * relatively low latency when blocking on a particular request to finish.
3489 */
3490static int
3491i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3492{
3493 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3494 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003495 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003496
3497 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003498 while (!list_empty(&i915_file_priv->mm.request_list)) {
3499 struct drm_i915_gem_request *request;
3500
3501 request = list_first_entry(&i915_file_priv->mm.request_list,
3502 struct drm_i915_gem_request,
3503 client_list);
3504
3505 if (time_after_eq(request->emitted_jiffies, recent_enough))
3506 break;
3507
3508 ret = i915_wait_request(dev, request->seqno);
3509 if (ret != 0)
3510 break;
3511 }
Eric Anholt673a3942008-07-30 12:06:12 -07003512 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003513
Eric Anholt673a3942008-07-30 12:06:12 -07003514 return ret;
3515}
3516
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003517static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003518i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003519 uint32_t buffer_count,
3520 struct drm_i915_gem_relocation_entry **relocs)
3521{
3522 uint32_t reloc_count = 0, reloc_index = 0, i;
3523 int ret;
3524
3525 *relocs = NULL;
3526 for (i = 0; i < buffer_count; i++) {
3527 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3528 return -EINVAL;
3529 reloc_count += exec_list[i].relocation_count;
3530 }
3531
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003532 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003533 if (*relocs == NULL) {
3534 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003535 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003536 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003537
3538 for (i = 0; i < buffer_count; i++) {
3539 struct drm_i915_gem_relocation_entry __user *user_relocs;
3540
3541 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3542
3543 ret = copy_from_user(&(*relocs)[reloc_index],
3544 user_relocs,
3545 exec_list[i].relocation_count *
3546 sizeof(**relocs));
3547 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003548 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003549 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003550 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003551 }
3552
3553 reloc_index += exec_list[i].relocation_count;
3554 }
3555
Florian Mickler2bc43b52009-04-06 22:55:41 +02003556 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003557}
3558
3559static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003560i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003561 uint32_t buffer_count,
3562 struct drm_i915_gem_relocation_entry *relocs)
3563{
3564 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003565 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003566
3567 for (i = 0; i < buffer_count; i++) {
3568 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003569 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003570
3571 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3572
Florian Mickler2bc43b52009-04-06 22:55:41 +02003573 unwritten = copy_to_user(user_relocs,
3574 &relocs[reloc_count],
3575 exec_list[i].relocation_count *
3576 sizeof(*relocs));
3577
3578 if (unwritten) {
3579 ret = -EFAULT;
3580 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003581 }
3582
3583 reloc_count += exec_list[i].relocation_count;
3584 }
3585
Florian Mickler2bc43b52009-04-06 22:55:41 +02003586err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003587 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003588
3589 return ret;
3590}
3591
Chris Wilson83d60792009-06-06 09:45:57 +01003592static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003593i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003594 uint64_t exec_offset)
3595{
3596 uint32_t exec_start, exec_len;
3597
3598 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3599 exec_len = (uint32_t) exec->batch_len;
3600
3601 if ((exec_start | exec_len) & 0x7)
3602 return -EINVAL;
3603
3604 if (!exec_start)
3605 return -EINVAL;
3606
3607 return 0;
3608}
3609
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003610static int
3611i915_gem_wait_for_pending_flip(struct drm_device *dev,
3612 struct drm_gem_object **object_list,
3613 int count)
3614{
3615 drm_i915_private_t *dev_priv = dev->dev_private;
3616 struct drm_i915_gem_object *obj_priv;
3617 DEFINE_WAIT(wait);
3618 int i, ret = 0;
3619
3620 for (;;) {
3621 prepare_to_wait(&dev_priv->pending_flip_queue,
3622 &wait, TASK_INTERRUPTIBLE);
3623 for (i = 0; i < count; i++) {
3624 obj_priv = object_list[i]->driver_private;
3625 if (atomic_read(&obj_priv->pending_flip) > 0)
3626 break;
3627 }
3628 if (i == count)
3629 break;
3630
3631 if (!signal_pending(current)) {
3632 mutex_unlock(&dev->struct_mutex);
3633 schedule();
3634 mutex_lock(&dev->struct_mutex);
3635 continue;
3636 }
3637 ret = -ERESTARTSYS;
3638 break;
3639 }
3640 finish_wait(&dev_priv->pending_flip_queue, &wait);
3641
3642 return ret;
3643}
3644
Eric Anholt673a3942008-07-30 12:06:12 -07003645int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003646i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3647 struct drm_file *file_priv,
3648 struct drm_i915_gem_execbuffer2 *args,
3649 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003650{
3651 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003652 struct drm_gem_object **object_list = NULL;
3653 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003654 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003655 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003656 struct drm_i915_gem_relocation_entry *relocs;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003657 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003658 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003659 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003660 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003661
3662#if WATCH_EXEC
3663 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3664 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3665#endif
3666
Eric Anholt4f481ed2008-09-10 14:22:49 -07003667 if (args->buffer_count < 1) {
3668 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3669 return -EINVAL;
3670 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003671 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003672 if (object_list == NULL) {
3673 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003674 args->buffer_count);
3675 ret = -ENOMEM;
3676 goto pre_mutex_err;
3677 }
Eric Anholt673a3942008-07-30 12:06:12 -07003678
Eric Anholt201361a2009-03-11 12:30:04 -07003679 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003680 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3681 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -07003682 if (cliprects == NULL)
3683 goto pre_mutex_err;
3684
3685 ret = copy_from_user(cliprects,
3686 (struct drm_clip_rect __user *)
3687 (uintptr_t) args->cliprects_ptr,
3688 sizeof(*cliprects) * args->num_cliprects);
3689 if (ret != 0) {
3690 DRM_ERROR("copy %d cliprects failed: %d\n",
3691 args->num_cliprects, ret);
3692 goto pre_mutex_err;
3693 }
3694 }
3695
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003696 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3697 &relocs);
3698 if (ret != 0)
3699 goto pre_mutex_err;
3700
Eric Anholt673a3942008-07-30 12:06:12 -07003701 mutex_lock(&dev->struct_mutex);
3702
3703 i915_verify_inactive(dev, __FILE__, __LINE__);
3704
Ben Gamariba1234d2009-09-14 17:48:47 -04003705 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003706 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003707 ret = -EIO;
3708 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003709 }
3710
3711 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003712 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003713 ret = -EBUSY;
3714 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003715 }
3716
Keith Packardac94a962008-11-20 23:30:27 -08003717 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003718 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003719 for (i = 0; i < args->buffer_count; i++) {
3720 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3721 exec_list[i].handle);
3722 if (object_list[i] == NULL) {
3723 DRM_ERROR("Invalid object handle %d at index %d\n",
3724 exec_list[i].handle, i);
3725 ret = -EBADF;
3726 goto err;
3727 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003728
3729 obj_priv = object_list[i]->driver_private;
3730 if (obj_priv->in_execbuffer) {
3731 DRM_ERROR("Object %p appears more than once in object list\n",
3732 object_list[i]);
3733 ret = -EBADF;
3734 goto err;
3735 }
3736 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003737 flips += atomic_read(&obj_priv->pending_flip);
3738 }
3739
3740 if (flips > 0) {
3741 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3742 args->buffer_count);
3743 if (ret)
3744 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003745 }
Eric Anholt673a3942008-07-30 12:06:12 -07003746
Keith Packardac94a962008-11-20 23:30:27 -08003747 /* Pin and relocate */
3748 for (pin_tries = 0; ; pin_tries++) {
3749 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003750 reloc_index = 0;
3751
Keith Packardac94a962008-11-20 23:30:27 -08003752 for (i = 0; i < args->buffer_count; i++) {
3753 object_list[i]->pending_read_domains = 0;
3754 object_list[i]->pending_write_domain = 0;
3755 ret = i915_gem_object_pin_and_relocate(object_list[i],
3756 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003757 &exec_list[i],
3758 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003759 if (ret)
3760 break;
3761 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003762 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003763 }
3764 /* success */
3765 if (ret == 0)
3766 break;
3767
3768 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003769 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003770 if (ret != -ERESTARTSYS) {
3771 unsigned long long total_size = 0;
3772 for (i = 0; i < args->buffer_count; i++)
3773 total_size += object_list[i]->size;
3774 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes: %d\n",
3775 pinned+1, args->buffer_count,
3776 total_size, ret);
3777 DRM_ERROR("%d objects [%d pinned], "
3778 "%d object bytes [%d pinned], "
3779 "%d/%d gtt bytes\n",
3780 atomic_read(&dev->object_count),
3781 atomic_read(&dev->pin_count),
3782 atomic_read(&dev->object_memory),
3783 atomic_read(&dev->pin_memory),
3784 atomic_read(&dev->gtt_memory),
3785 dev->gtt_total);
3786 }
Eric Anholt673a3942008-07-30 12:06:12 -07003787 goto err;
3788 }
Keith Packardac94a962008-11-20 23:30:27 -08003789
3790 /* unpin all of our buffers */
3791 for (i = 0; i < pinned; i++)
3792 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003793 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003794
3795 /* evict everyone we can from the aperture */
3796 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003797 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003798 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003799 }
3800
3801 /* Set the pending read domains for the batch buffer to COMMAND */
3802 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003803 if (batch_obj->pending_write_domain) {
3804 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3805 ret = -EINVAL;
3806 goto err;
3807 }
3808 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003809
Chris Wilson83d60792009-06-06 09:45:57 +01003810 /* Sanity check the batch buffer, prior to moving objects */
3811 exec_offset = exec_list[args->buffer_count - 1].offset;
3812 ret = i915_gem_check_execbuffer (args, exec_offset);
3813 if (ret != 0) {
3814 DRM_ERROR("execbuf with invalid offset/length\n");
3815 goto err;
3816 }
3817
Eric Anholt673a3942008-07-30 12:06:12 -07003818 i915_verify_inactive(dev, __FILE__, __LINE__);
3819
Keith Packard646f0f62008-11-20 23:23:03 -08003820 /* Zero the global flush/invalidate flags. These
3821 * will be modified as new domains are computed
3822 * for each object
3823 */
3824 dev->invalidate_domains = 0;
3825 dev->flush_domains = 0;
3826
Eric Anholt673a3942008-07-30 12:06:12 -07003827 for (i = 0; i < args->buffer_count; i++) {
3828 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003829
Keith Packard646f0f62008-11-20 23:23:03 -08003830 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003831 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003832 }
3833
3834 i915_verify_inactive(dev, __FILE__, __LINE__);
3835
Keith Packard646f0f62008-11-20 23:23:03 -08003836 if (dev->invalidate_domains | dev->flush_domains) {
3837#if WATCH_EXEC
3838 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3839 __func__,
3840 dev->invalidate_domains,
3841 dev->flush_domains);
3842#endif
3843 i915_gem_flush(dev,
3844 dev->invalidate_domains,
3845 dev->flush_domains);
3846 if (dev->flush_domains)
Eric Anholtb9624422009-06-03 07:27:35 +00003847 (void)i915_add_request(dev, file_priv,
3848 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003849 }
Eric Anholt673a3942008-07-30 12:06:12 -07003850
Eric Anholtefbeed92009-02-19 14:54:51 -08003851 for (i = 0; i < args->buffer_count; i++) {
3852 struct drm_gem_object *obj = object_list[i];
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003853 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003854
3855 obj->write_domain = obj->pending_write_domain;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003856 trace_i915_gem_object_change_domain(obj,
3857 obj->read_domains,
3858 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003859 }
3860
Eric Anholt673a3942008-07-30 12:06:12 -07003861 i915_verify_inactive(dev, __FILE__, __LINE__);
3862
3863#if WATCH_COHERENCY
3864 for (i = 0; i < args->buffer_count; i++) {
3865 i915_gem_object_check_coherency(object_list[i],
3866 exec_list[i].handle);
3867 }
3868#endif
3869
Eric Anholt673a3942008-07-30 12:06:12 -07003870#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003871 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003872 args->batch_len,
3873 __func__,
3874 ~0);
3875#endif
3876
Eric Anholt673a3942008-07-30 12:06:12 -07003877 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003878 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003879 if (ret) {
3880 DRM_ERROR("dispatch failed %d\n", ret);
3881 goto err;
3882 }
3883
3884 /*
3885 * Ensure that the commands in the batch buffer are
3886 * finished before the interrupt fires
3887 */
3888 flush_domains = i915_retire_commands(dev);
3889
3890 i915_verify_inactive(dev, __FILE__, __LINE__);
3891
3892 /*
3893 * Get a seqno representing the execution of the current buffer,
3894 * which we can wait on. We would like to mitigate these interrupts,
3895 * likely by only creating seqnos occasionally (so that we have
3896 * *some* interrupts representing completion of buffers that we can
3897 * wait on when trying to clear up gtt space).
3898 */
Eric Anholtb9624422009-06-03 07:27:35 +00003899 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003900 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003901 for (i = 0; i < args->buffer_count; i++) {
3902 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003903
Eric Anholtce44b0e2008-11-06 16:00:31 -08003904 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003905#if WATCH_LRU
3906 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3907#endif
3908 }
3909#if WATCH_LRU
3910 i915_dump_lru(dev, __func__);
3911#endif
3912
3913 i915_verify_inactive(dev, __FILE__, __LINE__);
3914
Eric Anholt673a3942008-07-30 12:06:12 -07003915err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003916 for (i = 0; i < pinned; i++)
3917 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003918
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003919 for (i = 0; i < args->buffer_count; i++) {
3920 if (object_list[i]) {
3921 obj_priv = object_list[i]->driver_private;
3922 obj_priv->in_execbuffer = false;
3923 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003924 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003925 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003926
Eric Anholt673a3942008-07-30 12:06:12 -07003927 mutex_unlock(&dev->struct_mutex);
3928
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003929 /* Copy the updated relocations out regardless of current error
3930 * state. Failure to update the relocs would mean that the next
3931 * time userland calls execbuf, it would do so with presumed offset
3932 * state that didn't match the actual object state.
3933 */
3934 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3935 relocs);
3936 if (ret2 != 0) {
3937 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3938
3939 if (ret == 0)
3940 ret = ret2;
3941 }
3942
Eric Anholt673a3942008-07-30 12:06:12 -07003943pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003944 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003945 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003946
3947 return ret;
3948}
3949
Jesse Barnes76446ca2009-12-17 22:05:42 -05003950/*
3951 * Legacy execbuffer just creates an exec2 list from the original exec object
3952 * list array and passes it to the real function.
3953 */
3954int
3955i915_gem_execbuffer(struct drm_device *dev, void *data,
3956 struct drm_file *file_priv)
3957{
3958 struct drm_i915_gem_execbuffer *args = data;
3959 struct drm_i915_gem_execbuffer2 exec2;
3960 struct drm_i915_gem_exec_object *exec_list = NULL;
3961 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3962 int ret, i;
3963
3964#if WATCH_EXEC
3965 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3966 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3967#endif
3968
3969 if (args->buffer_count < 1) {
3970 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3971 return -EINVAL;
3972 }
3973
3974 /* Copy in the exec list from userland */
3975 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3976 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3977 if (exec_list == NULL || exec2_list == NULL) {
3978 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3979 args->buffer_count);
3980 drm_free_large(exec_list);
3981 drm_free_large(exec2_list);
3982 return -ENOMEM;
3983 }
3984 ret = copy_from_user(exec_list,
3985 (struct drm_i915_relocation_entry __user *)
3986 (uintptr_t) args->buffers_ptr,
3987 sizeof(*exec_list) * args->buffer_count);
3988 if (ret != 0) {
3989 DRM_ERROR("copy %d exec entries failed %d\n",
3990 args->buffer_count, ret);
3991 drm_free_large(exec_list);
3992 drm_free_large(exec2_list);
3993 return -EFAULT;
3994 }
3995
3996 for (i = 0; i < args->buffer_count; i++) {
3997 exec2_list[i].handle = exec_list[i].handle;
3998 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3999 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4000 exec2_list[i].alignment = exec_list[i].alignment;
4001 exec2_list[i].offset = exec_list[i].offset;
4002 if (!IS_I965G(dev))
4003 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4004 else
4005 exec2_list[i].flags = 0;
4006 }
4007
4008 exec2.buffers_ptr = args->buffers_ptr;
4009 exec2.buffer_count = args->buffer_count;
4010 exec2.batch_start_offset = args->batch_start_offset;
4011 exec2.batch_len = args->batch_len;
4012 exec2.DR1 = args->DR1;
4013 exec2.DR4 = args->DR4;
4014 exec2.num_cliprects = args->num_cliprects;
4015 exec2.cliprects_ptr = args->cliprects_ptr;
4016 exec2.flags = 0;
4017
4018 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4019 if (!ret) {
4020 /* Copy the new buffer offsets back to the user's exec list. */
4021 for (i = 0; i < args->buffer_count; i++)
4022 exec_list[i].offset = exec2_list[i].offset;
4023 /* ... and back out to userspace */
4024 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4025 (uintptr_t) args->buffers_ptr,
4026 exec_list,
4027 sizeof(*exec_list) * args->buffer_count);
4028 if (ret) {
4029 ret = -EFAULT;
4030 DRM_ERROR("failed to copy %d exec entries "
4031 "back to user (%d)\n",
4032 args->buffer_count, ret);
4033 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004034 }
4035
4036 drm_free_large(exec_list);
4037 drm_free_large(exec2_list);
4038 return ret;
4039}
4040
4041int
4042i915_gem_execbuffer2(struct drm_device *dev, void *data,
4043 struct drm_file *file_priv)
4044{
4045 struct drm_i915_gem_execbuffer2 *args = data;
4046 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4047 int ret;
4048
4049#if WATCH_EXEC
4050 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4051 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4052#endif
4053
4054 if (args->buffer_count < 1) {
4055 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4056 return -EINVAL;
4057 }
4058
4059 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4060 if (exec2_list == NULL) {
4061 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4062 args->buffer_count);
4063 return -ENOMEM;
4064 }
4065 ret = copy_from_user(exec2_list,
4066 (struct drm_i915_relocation_entry __user *)
4067 (uintptr_t) args->buffers_ptr,
4068 sizeof(*exec2_list) * args->buffer_count);
4069 if (ret != 0) {
4070 DRM_ERROR("copy %d exec entries failed %d\n",
4071 args->buffer_count, ret);
4072 drm_free_large(exec2_list);
4073 return -EFAULT;
4074 }
4075
4076 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4077 if (!ret) {
4078 /* Copy the new buffer offsets back to the user's exec list. */
4079 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4080 (uintptr_t) args->buffers_ptr,
4081 exec2_list,
4082 sizeof(*exec2_list) * args->buffer_count);
4083 if (ret) {
4084 ret = -EFAULT;
4085 DRM_ERROR("failed to copy %d exec entries "
4086 "back to user (%d)\n",
4087 args->buffer_count, ret);
4088 }
4089 }
4090
4091 drm_free_large(exec2_list);
4092 return ret;
4093}
4094
Eric Anholt673a3942008-07-30 12:06:12 -07004095int
4096i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4097{
4098 struct drm_device *dev = obj->dev;
4099 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4100 int ret;
4101
4102 i915_verify_inactive(dev, __FILE__, __LINE__);
4103 if (obj_priv->gtt_space == NULL) {
4104 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004105 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004106 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004107 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004108
Eric Anholt673a3942008-07-30 12:06:12 -07004109 obj_priv->pin_count++;
4110
4111 /* If the object is not active and not pending a flush,
4112 * remove it from the inactive list
4113 */
4114 if (obj_priv->pin_count == 1) {
4115 atomic_inc(&dev->pin_count);
4116 atomic_add(obj->size, &dev->pin_memory);
4117 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004118 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07004119 !list_empty(&obj_priv->list))
4120 list_del_init(&obj_priv->list);
4121 }
4122 i915_verify_inactive(dev, __FILE__, __LINE__);
4123
4124 return 0;
4125}
4126
4127void
4128i915_gem_object_unpin(struct drm_gem_object *obj)
4129{
4130 struct drm_device *dev = obj->dev;
4131 drm_i915_private_t *dev_priv = dev->dev_private;
4132 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4133
4134 i915_verify_inactive(dev, __FILE__, __LINE__);
4135 obj_priv->pin_count--;
4136 BUG_ON(obj_priv->pin_count < 0);
4137 BUG_ON(obj_priv->gtt_space == NULL);
4138
4139 /* If the object is no longer pinned, and is
4140 * neither active nor being flushed, then stick it on
4141 * the inactive list
4142 */
4143 if (obj_priv->pin_count == 0) {
4144 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004145 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004146 list_move_tail(&obj_priv->list,
4147 &dev_priv->mm.inactive_list);
4148 atomic_dec(&dev->pin_count);
4149 atomic_sub(obj->size, &dev->pin_memory);
4150 }
4151 i915_verify_inactive(dev, __FILE__, __LINE__);
4152}
4153
4154int
4155i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4156 struct drm_file *file_priv)
4157{
4158 struct drm_i915_gem_pin *args = data;
4159 struct drm_gem_object *obj;
4160 struct drm_i915_gem_object *obj_priv;
4161 int ret;
4162
4163 mutex_lock(&dev->struct_mutex);
4164
4165 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4166 if (obj == NULL) {
4167 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4168 args->handle);
4169 mutex_unlock(&dev->struct_mutex);
4170 return -EBADF;
4171 }
4172 obj_priv = obj->driver_private;
4173
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004174 if (obj_priv->madv != I915_MADV_WILLNEED) {
4175 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004176 drm_gem_object_unreference(obj);
4177 mutex_unlock(&dev->struct_mutex);
4178 return -EINVAL;
4179 }
4180
Jesse Barnes79e53942008-11-07 14:24:08 -08004181 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4182 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4183 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004184 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004185 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004186 return -EINVAL;
4187 }
4188
4189 obj_priv->user_pin_count++;
4190 obj_priv->pin_filp = file_priv;
4191 if (obj_priv->user_pin_count == 1) {
4192 ret = i915_gem_object_pin(obj, args->alignment);
4193 if (ret != 0) {
4194 drm_gem_object_unreference(obj);
4195 mutex_unlock(&dev->struct_mutex);
4196 return ret;
4197 }
Eric Anholt673a3942008-07-30 12:06:12 -07004198 }
4199
4200 /* XXX - flush the CPU caches for pinned objects
4201 * as the X server doesn't manage domains yet
4202 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004203 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004204 args->offset = obj_priv->gtt_offset;
4205 drm_gem_object_unreference(obj);
4206 mutex_unlock(&dev->struct_mutex);
4207
4208 return 0;
4209}
4210
4211int
4212i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4213 struct drm_file *file_priv)
4214{
4215 struct drm_i915_gem_pin *args = data;
4216 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004217 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004218
4219 mutex_lock(&dev->struct_mutex);
4220
4221 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4222 if (obj == NULL) {
4223 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4224 args->handle);
4225 mutex_unlock(&dev->struct_mutex);
4226 return -EBADF;
4227 }
4228
Jesse Barnes79e53942008-11-07 14:24:08 -08004229 obj_priv = obj->driver_private;
4230 if (obj_priv->pin_filp != file_priv) {
4231 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4232 args->handle);
4233 drm_gem_object_unreference(obj);
4234 mutex_unlock(&dev->struct_mutex);
4235 return -EINVAL;
4236 }
4237 obj_priv->user_pin_count--;
4238 if (obj_priv->user_pin_count == 0) {
4239 obj_priv->pin_filp = NULL;
4240 i915_gem_object_unpin(obj);
4241 }
Eric Anholt673a3942008-07-30 12:06:12 -07004242
4243 drm_gem_object_unreference(obj);
4244 mutex_unlock(&dev->struct_mutex);
4245 return 0;
4246}
4247
4248int
4249i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4250 struct drm_file *file_priv)
4251{
4252 struct drm_i915_gem_busy *args = data;
4253 struct drm_gem_object *obj;
4254 struct drm_i915_gem_object *obj_priv;
4255
Eric Anholt673a3942008-07-30 12:06:12 -07004256 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4257 if (obj == NULL) {
4258 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4259 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07004260 return -EBADF;
4261 }
4262
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004263 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08004264 /* Update the active list for the hardware's current position.
4265 * Otherwise this only updates on a delayed timer or when irqs are
4266 * actually unmasked, and our working set ends up being larger than
4267 * required.
4268 */
4269 i915_gem_retire_requests(dev);
4270
Eric Anholt673a3942008-07-30 12:06:12 -07004271 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08004272 /* Don't count being on the flushing list against the object being
4273 * done. Otherwise, a buffer left on the flushing list but not getting
4274 * flushed (because nobody's flushing that domain) won't ever return
4275 * unbusy and get reused by libdrm's bo cache. The other expected
4276 * consumer of this interface, OpenGL's occlusion queries, also specs
4277 * that the objects get unbusy "eventually" without any interference.
4278 */
4279 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004280
4281 drm_gem_object_unreference(obj);
4282 mutex_unlock(&dev->struct_mutex);
4283 return 0;
4284}
4285
4286int
4287i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4288 struct drm_file *file_priv)
4289{
4290 return i915_gem_ring_throttle(dev, file_priv);
4291}
4292
Chris Wilson3ef94da2009-09-14 16:50:29 +01004293int
4294i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4295 struct drm_file *file_priv)
4296{
4297 struct drm_i915_gem_madvise *args = data;
4298 struct drm_gem_object *obj;
4299 struct drm_i915_gem_object *obj_priv;
4300
4301 switch (args->madv) {
4302 case I915_MADV_DONTNEED:
4303 case I915_MADV_WILLNEED:
4304 break;
4305 default:
4306 return -EINVAL;
4307 }
4308
4309 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4310 if (obj == NULL) {
4311 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4312 args->handle);
4313 return -EBADF;
4314 }
4315
4316 mutex_lock(&dev->struct_mutex);
4317 obj_priv = obj->driver_private;
4318
4319 if (obj_priv->pin_count) {
4320 drm_gem_object_unreference(obj);
4321 mutex_unlock(&dev->struct_mutex);
4322
4323 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4324 return -EINVAL;
4325 }
4326
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004327 if (obj_priv->madv != __I915_MADV_PURGED)
4328 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004329
Chris Wilson2d7ef392009-09-20 23:13:10 +01004330 /* if the object is no longer bound, discard its backing storage */
4331 if (i915_gem_object_is_purgeable(obj_priv) &&
4332 obj_priv->gtt_space == NULL)
4333 i915_gem_object_truncate(obj);
4334
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004335 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4336
Chris Wilson3ef94da2009-09-14 16:50:29 +01004337 drm_gem_object_unreference(obj);
4338 mutex_unlock(&dev->struct_mutex);
4339
4340 return 0;
4341}
4342
Eric Anholt673a3942008-07-30 12:06:12 -07004343int i915_gem_init_object(struct drm_gem_object *obj)
4344{
4345 struct drm_i915_gem_object *obj_priv;
4346
Eric Anholt9a298b22009-03-24 12:23:04 -07004347 obj_priv = kzalloc(sizeof(*obj_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07004348 if (obj_priv == NULL)
4349 return -ENOMEM;
4350
4351 /*
4352 * We've just allocated pages from the kernel,
4353 * so they've just been written by the CPU with
4354 * zeros. They'll need to be clflushed before we
4355 * use them with the GPU.
4356 */
4357 obj->write_domain = I915_GEM_DOMAIN_CPU;
4358 obj->read_domains = I915_GEM_DOMAIN_CPU;
4359
Keith Packardba1eb1d2008-10-14 19:55:10 -07004360 obj_priv->agp_type = AGP_USER_MEMORY;
4361
Eric Anholt673a3942008-07-30 12:06:12 -07004362 obj->driver_private = obj_priv;
4363 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004364 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07004365 INIT_LIST_HEAD(&obj_priv->list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004366 INIT_LIST_HEAD(&obj_priv->fence_list);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004367 obj_priv->madv = I915_MADV_WILLNEED;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004368
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004369 trace_i915_gem_object_create(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004370
4371 return 0;
4372}
4373
4374void i915_gem_free_object(struct drm_gem_object *obj)
4375{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004376 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07004377 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4378
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004379 trace_i915_gem_object_destroy(obj);
4380
Eric Anholt673a3942008-07-30 12:06:12 -07004381 while (obj_priv->pin_count > 0)
4382 i915_gem_object_unpin(obj);
4383
Dave Airlie71acb5e2008-12-30 20:31:46 +10004384 if (obj_priv->phys_obj)
4385 i915_gem_detach_phys_object(dev, obj);
4386
Eric Anholt673a3942008-07-30 12:06:12 -07004387 i915_gem_object_unbind(obj);
4388
Chris Wilson7e616152009-09-10 08:53:04 +01004389 if (obj_priv->mmap_offset)
4390 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08004391
Eric Anholt9a298b22009-03-24 12:23:04 -07004392 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07004393 kfree(obj_priv->bit_17);
Eric Anholt9a298b22009-03-24 12:23:04 -07004394 kfree(obj->driver_private);
Eric Anholt673a3942008-07-30 12:06:12 -07004395}
4396
Chris Wilsonab5ee572009-09-20 19:25:47 +01004397/** Unbinds all inactive objects. */
Eric Anholt673a3942008-07-30 12:06:12 -07004398static int
Chris Wilsonab5ee572009-09-20 19:25:47 +01004399i915_gem_evict_from_inactive_list(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004400{
Chris Wilsonab5ee572009-09-20 19:25:47 +01004401 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004402
Chris Wilsonab5ee572009-09-20 19:25:47 +01004403 while (!list_empty(&dev_priv->mm.inactive_list)) {
4404 struct drm_gem_object *obj;
4405 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004406
Chris Wilsonab5ee572009-09-20 19:25:47 +01004407 obj = list_first_entry(&dev_priv->mm.inactive_list,
4408 struct drm_i915_gem_object,
4409 list)->obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004410
4411 ret = i915_gem_object_unbind(obj);
4412 if (ret != 0) {
Chris Wilsonab5ee572009-09-20 19:25:47 +01004413 DRM_ERROR("Error unbinding object: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004414 return ret;
4415 }
4416 }
4417
Eric Anholt673a3942008-07-30 12:06:12 -07004418 return 0;
4419}
4420
Jesse Barnes5669fca2009-02-17 15:13:31 -08004421int
Eric Anholt673a3942008-07-30 12:06:12 -07004422i915_gem_idle(struct drm_device *dev)
4423{
4424 drm_i915_private_t *dev_priv = dev->dev_private;
4425 uint32_t seqno, cur_seqno, last_seqno;
4426 int stuck, ret;
4427
Keith Packard6dbe2772008-10-14 21:41:13 -07004428 mutex_lock(&dev->struct_mutex);
4429
4430 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
4431 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004432 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004433 }
Eric Anholt673a3942008-07-30 12:06:12 -07004434
4435 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4436 * We need to replace this with a semaphore, or something.
4437 */
4438 dev_priv->mm.suspended = 1;
Ben Gamarif65d9422009-09-14 17:48:44 -04004439 del_timer(&dev_priv->hangcheck_timer);
Eric Anholt673a3942008-07-30 12:06:12 -07004440
Keith Packard6dbe2772008-10-14 21:41:13 -07004441 /* Cancel the retire work handler, wait for it to finish if running
4442 */
4443 mutex_unlock(&dev->struct_mutex);
4444 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4445 mutex_lock(&dev->struct_mutex);
4446
Eric Anholt673a3942008-07-30 12:06:12 -07004447 i915_kernel_lost_context(dev);
4448
4449 /* Flush the GPU along with all non-CPU write domains
4450 */
Chris Wilson21d509e2009-06-06 09:46:02 +01004451 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
4452 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07004453
4454 if (seqno == 0) {
4455 mutex_unlock(&dev->struct_mutex);
4456 return -ENOMEM;
4457 }
4458
4459 dev_priv->mm.waiting_gem_seqno = seqno;
4460 last_seqno = 0;
4461 stuck = 0;
4462 for (;;) {
4463 cur_seqno = i915_get_gem_seqno(dev);
4464 if (i915_seqno_passed(cur_seqno, seqno))
4465 break;
4466 if (last_seqno == cur_seqno) {
4467 if (stuck++ > 100) {
4468 DRM_ERROR("hardware wedged\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004469 atomic_set(&dev_priv->mm.wedged, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07004470 DRM_WAKEUP(&dev_priv->irq_queue);
4471 break;
4472 }
4473 }
4474 msleep(10);
4475 last_seqno = cur_seqno;
4476 }
4477 dev_priv->mm.waiting_gem_seqno = 0;
4478
4479 i915_gem_retire_requests(dev);
4480
Carl Worth5e118f42009-03-20 11:54:25 -07004481 spin_lock(&dev_priv->mm.active_list_lock);
Ben Gamariba1234d2009-09-14 17:48:47 -04004482 if (!atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt28dfe522008-11-13 15:00:55 -08004483 /* Active and flushing should now be empty as we've
4484 * waited for a sequence higher than any pending execbuffer
4485 */
4486 WARN_ON(!list_empty(&dev_priv->mm.active_list));
4487 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
4488 /* Request should now be empty as we've also waited
4489 * for the last request in the list
4490 */
4491 WARN_ON(!list_empty(&dev_priv->mm.request_list));
4492 }
Eric Anholt673a3942008-07-30 12:06:12 -07004493
Eric Anholt28dfe522008-11-13 15:00:55 -08004494 /* Empty the active and flushing lists to inactive. If there's
4495 * anything left at this point, it means that we're wedged and
4496 * nothing good's going to happen by leaving them there. So strip
4497 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07004498 */
Eric Anholt28dfe522008-11-13 15:00:55 -08004499 while (!list_empty(&dev_priv->mm.active_list)) {
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004500 struct drm_gem_object *obj;
4501 uint32_t old_write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07004502
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004503 obj = list_first_entry(&dev_priv->mm.active_list,
4504 struct drm_i915_gem_object,
4505 list)->obj;
4506 old_write_domain = obj->write_domain;
4507 obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
4508 i915_gem_object_move_to_inactive(obj);
4509
4510 trace_i915_gem_object_change_domain(obj,
4511 obj->read_domains,
4512 old_write_domain);
Eric Anholt28dfe522008-11-13 15:00:55 -08004513 }
Carl Worth5e118f42009-03-20 11:54:25 -07004514 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08004515
4516 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004517 struct drm_gem_object *obj;
4518 uint32_t old_write_domain;
Eric Anholt28dfe522008-11-13 15:00:55 -08004519
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004520 obj = list_first_entry(&dev_priv->mm.flushing_list,
4521 struct drm_i915_gem_object,
4522 list)->obj;
4523 old_write_domain = obj->write_domain;
4524 obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
4525 i915_gem_object_move_to_inactive(obj);
4526
4527 trace_i915_gem_object_change_domain(obj,
4528 obj->read_domains,
4529 old_write_domain);
Eric Anholt28dfe522008-11-13 15:00:55 -08004530 }
4531
4532
4533 /* Move all inactive buffers out of the GTT. */
Chris Wilsonab5ee572009-09-20 19:25:47 +01004534 ret = i915_gem_evict_from_inactive_list(dev);
Eric Anholt28dfe522008-11-13 15:00:55 -08004535 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07004536 if (ret) {
4537 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004538 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004539 }
Eric Anholt673a3942008-07-30 12:06:12 -07004540
Keith Packard6dbe2772008-10-14 21:41:13 -07004541 i915_gem_cleanup_ringbuffer(dev);
4542 mutex_unlock(&dev->struct_mutex);
4543
Eric Anholt673a3942008-07-30 12:06:12 -07004544 return 0;
4545}
4546
4547static int
4548i915_gem_init_hws(struct drm_device *dev)
4549{
4550 drm_i915_private_t *dev_priv = dev->dev_private;
4551 struct drm_gem_object *obj;
4552 struct drm_i915_gem_object *obj_priv;
4553 int ret;
4554
4555 /* If we need a physical address for the status page, it's already
4556 * initialized at driver load time.
4557 */
4558 if (!I915_NEED_GFX_HWS(dev))
4559 return 0;
4560
4561 obj = drm_gem_object_alloc(dev, 4096);
4562 if (obj == NULL) {
4563 DRM_ERROR("Failed to allocate status page\n");
4564 return -ENOMEM;
4565 }
4566 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07004567 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07004568
4569 ret = i915_gem_object_pin(obj, 4096);
4570 if (ret != 0) {
4571 drm_gem_object_unreference(obj);
4572 return ret;
4573 }
4574
4575 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07004576
Eric Anholt856fa192009-03-19 14:10:50 -07004577 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004578 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004579 DRM_ERROR("Failed to map status page.\n");
4580 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00004581 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004582 drm_gem_object_unreference(obj);
4583 return -EINVAL;
4584 }
4585 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004586 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
4587 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004588 I915_READ(HWS_PGA); /* posting read */
Zhao Yakui44d98a62009-10-09 11:39:40 +08004589 DRM_DEBUG_DRIVER("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
Eric Anholt673a3942008-07-30 12:06:12 -07004590
4591 return 0;
4592}
4593
Chris Wilson85a7bb92009-02-11 14:52:44 +00004594static void
4595i915_gem_cleanup_hws(struct drm_device *dev)
4596{
4597 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004598 struct drm_gem_object *obj;
4599 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00004600
4601 if (dev_priv->hws_obj == NULL)
4602 return;
4603
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004604 obj = dev_priv->hws_obj;
4605 obj_priv = obj->driver_private;
4606
Eric Anholt856fa192009-03-19 14:10:50 -07004607 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004608 i915_gem_object_unpin(obj);
4609 drm_gem_object_unreference(obj);
4610 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004611
Chris Wilson85a7bb92009-02-11 14:52:44 +00004612 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4613 dev_priv->hw_status_page = NULL;
4614
4615 /* Write high address into HWS_PGA when disabling. */
4616 I915_WRITE(HWS_PGA, 0x1ffff000);
4617}
4618
Jesse Barnes79e53942008-11-07 14:24:08 -08004619int
Eric Anholt673a3942008-07-30 12:06:12 -07004620i915_gem_init_ringbuffer(struct drm_device *dev)
4621{
4622 drm_i915_private_t *dev_priv = dev->dev_private;
4623 struct drm_gem_object *obj;
4624 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08004625 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07004626 int ret;
Keith Packard50aa2532008-10-14 17:20:35 -07004627 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07004628
4629 ret = i915_gem_init_hws(dev);
4630 if (ret != 0)
4631 return ret;
4632
4633 obj = drm_gem_object_alloc(dev, 128 * 1024);
4634 if (obj == NULL) {
4635 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004636 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004637 return -ENOMEM;
4638 }
4639 obj_priv = obj->driver_private;
4640
4641 ret = i915_gem_object_pin(obj, 4096);
4642 if (ret != 0) {
4643 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004644 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004645 return ret;
4646 }
4647
4648 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004649 ring->Size = obj->size;
Eric Anholt673a3942008-07-30 12:06:12 -07004650
Jesse Barnes79e53942008-11-07 14:24:08 -08004651 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4652 ring->map.size = obj->size;
4653 ring->map.type = 0;
4654 ring->map.flags = 0;
4655 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004656
Jesse Barnes79e53942008-11-07 14:24:08 -08004657 drm_core_ioremap_wc(&ring->map, dev);
4658 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004659 DRM_ERROR("Failed to map ringbuffer.\n");
4660 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004661 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004662 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004663 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004664 return -EINVAL;
4665 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004666 ring->ring_obj = obj;
4667 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004668
4669 /* Stop the ring if it's running. */
4670 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004671 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa2532008-10-14 17:20:35 -07004672 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004673
4674 /* Initialize the ring. */
4675 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa2532008-10-14 17:20:35 -07004676 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4677
4678 /* G45 ring initialization fails to reset head to zero */
4679 if (head != 0) {
4680 DRM_ERROR("Ring head not reset to zero "
4681 "ctl %08x head %08x tail %08x start %08x\n",
4682 I915_READ(PRB0_CTL),
4683 I915_READ(PRB0_HEAD),
4684 I915_READ(PRB0_TAIL),
4685 I915_READ(PRB0_START));
4686 I915_WRITE(PRB0_HEAD, 0);
4687
4688 DRM_ERROR("Ring head forced to zero "
4689 "ctl %08x head %08x tail %08x start %08x\n",
4690 I915_READ(PRB0_CTL),
4691 I915_READ(PRB0_HEAD),
4692 I915_READ(PRB0_TAIL),
4693 I915_READ(PRB0_START));
4694 }
4695
Eric Anholt673a3942008-07-30 12:06:12 -07004696 I915_WRITE(PRB0_CTL,
4697 ((obj->size - 4096) & RING_NR_PAGES) |
4698 RING_NO_REPORT |
4699 RING_VALID);
4700
Keith Packard50aa2532008-10-14 17:20:35 -07004701 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4702
4703 /* If the head is still not zero, the ring is dead */
4704 if (head != 0) {
4705 DRM_ERROR("Ring initialization failed "
4706 "ctl %08x head %08x tail %08x start %08x\n",
4707 I915_READ(PRB0_CTL),
4708 I915_READ(PRB0_HEAD),
4709 I915_READ(PRB0_TAIL),
4710 I915_READ(PRB0_START));
4711 return -EIO;
4712 }
4713
Eric Anholt673a3942008-07-30 12:06:12 -07004714 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004715 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4716 i915_kernel_lost_context(dev);
4717 else {
4718 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4719 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4720 ring->space = ring->head - (ring->tail + 8);
4721 if (ring->space < 0)
4722 ring->space += ring->Size;
4723 }
Eric Anholt673a3942008-07-30 12:06:12 -07004724
4725 return 0;
4726}
4727
Jesse Barnes79e53942008-11-07 14:24:08 -08004728void
Eric Anholt673a3942008-07-30 12:06:12 -07004729i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4730{
4731 drm_i915_private_t *dev_priv = dev->dev_private;
4732
4733 if (dev_priv->ring.ring_obj == NULL)
4734 return;
4735
4736 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4737
4738 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4739 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4740 dev_priv->ring.ring_obj = NULL;
4741 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4742
Chris Wilson85a7bb92009-02-11 14:52:44 +00004743 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004744}
4745
4746int
4747i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4748 struct drm_file *file_priv)
4749{
4750 drm_i915_private_t *dev_priv = dev->dev_private;
4751 int ret;
4752
Jesse Barnes79e53942008-11-07 14:24:08 -08004753 if (drm_core_check_feature(dev, DRIVER_MODESET))
4754 return 0;
4755
Ben Gamariba1234d2009-09-14 17:48:47 -04004756 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004757 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004758 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004759 }
4760
Eric Anholt673a3942008-07-30 12:06:12 -07004761 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004762 dev_priv->mm.suspended = 0;
4763
4764 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004765 if (ret != 0) {
4766 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004767 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004768 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004769
Carl Worth5e118f42009-03-20 11:54:25 -07004770 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004771 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004772 spin_unlock(&dev_priv->mm.active_list_lock);
4773
Eric Anholt673a3942008-07-30 12:06:12 -07004774 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4775 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4776 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004777 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004778
4779 drm_irq_install(dev);
4780
Eric Anholt673a3942008-07-30 12:06:12 -07004781 return 0;
4782}
4783
4784int
4785i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4786 struct drm_file *file_priv)
4787{
Jesse Barnes79e53942008-11-07 14:24:08 -08004788 if (drm_core_check_feature(dev, DRIVER_MODESET))
4789 return 0;
4790
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004791 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004792 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004793}
4794
4795void
4796i915_gem_lastclose(struct drm_device *dev)
4797{
4798 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004799
Eric Anholte806b492009-01-22 09:56:58 -08004800 if (drm_core_check_feature(dev, DRIVER_MODESET))
4801 return;
4802
Keith Packard6dbe2772008-10-14 21:41:13 -07004803 ret = i915_gem_idle(dev);
4804 if (ret)
4805 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004806}
4807
4808void
4809i915_gem_load(struct drm_device *dev)
4810{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004811 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004812 drm_i915_private_t *dev_priv = dev->dev_private;
4813
Carl Worth5e118f42009-03-20 11:54:25 -07004814 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004815 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4816 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4817 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4818 INIT_LIST_HEAD(&dev_priv->mm.request_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004819 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004820 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4821 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004822 dev_priv->mm.next_gem_seqno = 1;
4823
Chris Wilson31169712009-09-14 16:50:28 +01004824 spin_lock(&shrink_list_lock);
4825 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4826 spin_unlock(&shrink_list_lock);
4827
Jesse Barnesde151cf2008-11-12 10:03:55 -08004828 /* Old X drivers will take 0-2 for front, back, depth buffers */
4829 dev_priv->fence_reg_start = 3;
4830
Jesse Barnes0f973f22009-01-26 17:10:45 -08004831 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004832 dev_priv->num_fence_regs = 16;
4833 else
4834 dev_priv->num_fence_regs = 8;
4835
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004836 /* Initialize fence registers to zero */
4837 if (IS_I965G(dev)) {
4838 for (i = 0; i < 16; i++)
4839 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4840 } else {
4841 for (i = 0; i < 8; i++)
4842 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4843 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4844 for (i = 0; i < 8; i++)
4845 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4846 }
Eric Anholt673a3942008-07-30 12:06:12 -07004847 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004848 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004849}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004850
4851/*
4852 * Create a physically contiguous memory object for this object
4853 * e.g. for cursor + overlay regs
4854 */
4855int i915_gem_init_phys_object(struct drm_device *dev,
4856 int id, int size)
4857{
4858 drm_i915_private_t *dev_priv = dev->dev_private;
4859 struct drm_i915_gem_phys_object *phys_obj;
4860 int ret;
4861
4862 if (dev_priv->mm.phys_objs[id - 1] || !size)
4863 return 0;
4864
Eric Anholt9a298b22009-03-24 12:23:04 -07004865 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004866 if (!phys_obj)
4867 return -ENOMEM;
4868
4869 phys_obj->id = id;
4870
Zhenyu Wange6be8d92010-01-05 11:25:05 +08004871 phys_obj->handle = drm_pci_alloc(dev, size, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004872 if (!phys_obj->handle) {
4873 ret = -ENOMEM;
4874 goto kfree_obj;
4875 }
4876#ifdef CONFIG_X86
4877 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4878#endif
4879
4880 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4881
4882 return 0;
4883kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004884 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004885 return ret;
4886}
4887
4888void i915_gem_free_phys_object(struct drm_device *dev, int id)
4889{
4890 drm_i915_private_t *dev_priv = dev->dev_private;
4891 struct drm_i915_gem_phys_object *phys_obj;
4892
4893 if (!dev_priv->mm.phys_objs[id - 1])
4894 return;
4895
4896 phys_obj = dev_priv->mm.phys_objs[id - 1];
4897 if (phys_obj->cur_obj) {
4898 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4899 }
4900
4901#ifdef CONFIG_X86
4902 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4903#endif
4904 drm_pci_free(dev, phys_obj->handle);
4905 kfree(phys_obj);
4906 dev_priv->mm.phys_objs[id - 1] = NULL;
4907}
4908
4909void i915_gem_free_all_phys_object(struct drm_device *dev)
4910{
4911 int i;
4912
Dave Airlie260883c2009-01-22 17:58:49 +10004913 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004914 i915_gem_free_phys_object(dev, i);
4915}
4916
4917void i915_gem_detach_phys_object(struct drm_device *dev,
4918 struct drm_gem_object *obj)
4919{
4920 struct drm_i915_gem_object *obj_priv;
4921 int i;
4922 int ret;
4923 int page_count;
4924
4925 obj_priv = obj->driver_private;
4926 if (!obj_priv->phys_obj)
4927 return;
4928
Chris Wilson4bdadb92010-01-27 13:36:32 +00004929 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004930 if (ret)
4931 goto out;
4932
4933 page_count = obj->size / PAGE_SIZE;
4934
4935 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004936 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004937 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4938
4939 memcpy(dst, src, PAGE_SIZE);
4940 kunmap_atomic(dst, KM_USER0);
4941 }
Eric Anholt856fa192009-03-19 14:10:50 -07004942 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004943 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004944
4945 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004946out:
4947 obj_priv->phys_obj->cur_obj = NULL;
4948 obj_priv->phys_obj = NULL;
4949}
4950
4951int
4952i915_gem_attach_phys_object(struct drm_device *dev,
4953 struct drm_gem_object *obj, int id)
4954{
4955 drm_i915_private_t *dev_priv = dev->dev_private;
4956 struct drm_i915_gem_object *obj_priv;
4957 int ret = 0;
4958 int page_count;
4959 int i;
4960
4961 if (id > I915_MAX_PHYS_OBJECT)
4962 return -EINVAL;
4963
4964 obj_priv = obj->driver_private;
4965
4966 if (obj_priv->phys_obj) {
4967 if (obj_priv->phys_obj->id == id)
4968 return 0;
4969 i915_gem_detach_phys_object(dev, obj);
4970 }
4971
4972
4973 /* create a new object */
4974 if (!dev_priv->mm.phys_objs[id - 1]) {
4975 ret = i915_gem_init_phys_object(dev, id,
4976 obj->size);
4977 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004978 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004979 goto out;
4980 }
4981 }
4982
4983 /* bind to the object */
4984 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4985 obj_priv->phys_obj->cur_obj = obj;
4986
Chris Wilson4bdadb92010-01-27 13:36:32 +00004987 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004988 if (ret) {
4989 DRM_ERROR("failed to get page list\n");
4990 goto out;
4991 }
4992
4993 page_count = obj->size / PAGE_SIZE;
4994
4995 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004996 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004997 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4998
4999 memcpy(dst, src, PAGE_SIZE);
5000 kunmap_atomic(src, KM_USER0);
5001 }
5002
Chris Wilsond78b47b2009-06-17 21:52:49 +01005003 i915_gem_object_put_pages(obj);
5004
Dave Airlie71acb5e2008-12-30 20:31:46 +10005005 return 0;
5006out:
5007 return ret;
5008}
5009
5010static int
5011i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
5012 struct drm_i915_gem_pwrite *args,
5013 struct drm_file *file_priv)
5014{
5015 struct drm_i915_gem_object *obj_priv = obj->driver_private;
5016 void *obj_addr;
5017 int ret;
5018 char __user *user_data;
5019
5020 user_data = (char __user *) (uintptr_t) args->data_ptr;
5021 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
5022
Zhao Yakui44d98a62009-10-09 11:39:40 +08005023 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005024 ret = copy_from_user(obj_addr, user_data, args->size);
5025 if (ret)
5026 return -EFAULT;
5027
5028 drm_agp_chipset_flush(dev);
5029 return 0;
5030}
Eric Anholtb9624422009-06-03 07:27:35 +00005031
5032void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
5033{
5034 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
5035
5036 /* Clean up our request list when the client is going away, so that
5037 * later retire_requests won't dereference our soon-to-be-gone
5038 * file_priv.
5039 */
5040 mutex_lock(&dev->struct_mutex);
5041 while (!list_empty(&i915_file_priv->mm.request_list))
5042 list_del_init(i915_file_priv->mm.request_list.next);
5043 mutex_unlock(&dev->struct_mutex);
5044}
Chris Wilson31169712009-09-14 16:50:28 +01005045
Chris Wilson31169712009-09-14 16:50:28 +01005046static int
5047i915_gem_shrink(int nr_to_scan, gfp_t gfp_mask)
5048{
5049 drm_i915_private_t *dev_priv, *next_dev;
5050 struct drm_i915_gem_object *obj_priv, *next_obj;
5051 int cnt = 0;
5052 int would_deadlock = 1;
5053
5054 /* "fast-path" to count number of available objects */
5055 if (nr_to_scan == 0) {
5056 spin_lock(&shrink_list_lock);
5057 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5058 struct drm_device *dev = dev_priv->dev;
5059
5060 if (mutex_trylock(&dev->struct_mutex)) {
5061 list_for_each_entry(obj_priv,
5062 &dev_priv->mm.inactive_list,
5063 list)
5064 cnt++;
5065 mutex_unlock(&dev->struct_mutex);
5066 }
5067 }
5068 spin_unlock(&shrink_list_lock);
5069
5070 return (cnt / 100) * sysctl_vfs_cache_pressure;
5071 }
5072
5073 spin_lock(&shrink_list_lock);
5074
5075 /* first scan for clean buffers */
5076 list_for_each_entry_safe(dev_priv, next_dev,
5077 &shrink_list, mm.shrink_list) {
5078 struct drm_device *dev = dev_priv->dev;
5079
5080 if (! mutex_trylock(&dev->struct_mutex))
5081 continue;
5082
5083 spin_unlock(&shrink_list_lock);
5084
5085 i915_gem_retire_requests(dev);
5086
5087 list_for_each_entry_safe(obj_priv, next_obj,
5088 &dev_priv->mm.inactive_list,
5089 list) {
5090 if (i915_gem_object_is_purgeable(obj_priv)) {
Chris Wilson963b4832009-09-20 23:03:54 +01005091 i915_gem_object_unbind(obj_priv->obj);
Chris Wilson31169712009-09-14 16:50:28 +01005092 if (--nr_to_scan <= 0)
5093 break;
5094 }
5095 }
5096
5097 spin_lock(&shrink_list_lock);
5098 mutex_unlock(&dev->struct_mutex);
5099
Chris Wilson963b4832009-09-20 23:03:54 +01005100 would_deadlock = 0;
5101
Chris Wilson31169712009-09-14 16:50:28 +01005102 if (nr_to_scan <= 0)
5103 break;
5104 }
5105
5106 /* second pass, evict/count anything still on the inactive list */
5107 list_for_each_entry_safe(dev_priv, next_dev,
5108 &shrink_list, mm.shrink_list) {
5109 struct drm_device *dev = dev_priv->dev;
5110
5111 if (! mutex_trylock(&dev->struct_mutex))
5112 continue;
5113
5114 spin_unlock(&shrink_list_lock);
5115
5116 list_for_each_entry_safe(obj_priv, next_obj,
5117 &dev_priv->mm.inactive_list,
5118 list) {
5119 if (nr_to_scan > 0) {
Chris Wilson963b4832009-09-20 23:03:54 +01005120 i915_gem_object_unbind(obj_priv->obj);
Chris Wilson31169712009-09-14 16:50:28 +01005121 nr_to_scan--;
5122 } else
5123 cnt++;
5124 }
5125
5126 spin_lock(&shrink_list_lock);
5127 mutex_unlock(&dev->struct_mutex);
5128
5129 would_deadlock = 0;
5130 }
5131
5132 spin_unlock(&shrink_list_lock);
5133
5134 if (would_deadlock)
5135 return -1;
5136 else if (cnt > 0)
5137 return (cnt / 100) * sysctl_vfs_cache_pressure;
5138 else
5139 return 0;
5140}
5141
5142static struct shrinker shrinker = {
5143 .shrink = i915_gem_shrink,
5144 .seeks = DEFAULT_SEEKS,
5145};
5146
5147__init void
5148i915_gem_shrinker_init(void)
5149{
5150 register_shrinker(&shrinker);
5151}
5152
5153__exit void
5154i915_gem_shrinker_exit(void)
5155{
5156 unregister_shrinker(&shrinker);
5157}