blob: e0da98602eed4c988d6635f5cf7f11ca2e06f3a6 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070032#include "intel_drv.h"
Eric Anholt673a3942008-07-30 12:06:12 -070033#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080034#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035
Eric Anholt28dfe522008-11-13 15:00:55 -080036#define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
37
Eric Anholte47c68e2008-11-14 13:35:19 -080038static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
39static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
40static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080041static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
42 int write);
43static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
44 uint64_t offset,
45 uint64_t size);
46static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070047static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080048static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
49 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080050static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
51static int i915_gem_evict_something(struct drm_device *dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +100052static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
53 struct drm_i915_gem_pwrite *args,
54 struct drm_file *file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -070055
Jesse Barnes79e53942008-11-07 14:24:08 -080056int i915_gem_do_init(struct drm_device *dev, unsigned long start,
57 unsigned long end)
58{
59 drm_i915_private_t *dev_priv = dev->dev_private;
60
61 if (start >= end ||
62 (start & (PAGE_SIZE - 1)) != 0 ||
63 (end & (PAGE_SIZE - 1)) != 0) {
64 return -EINVAL;
65 }
66
67 drm_mm_init(&dev_priv->mm.gtt_space, start,
68 end - start);
69
70 dev->gtt_total = (uint32_t) (end - start);
71
72 return 0;
73}
Keith Packard6dbe2772008-10-14 21:41:13 -070074
Eric Anholt673a3942008-07-30 12:06:12 -070075int
76i915_gem_init_ioctl(struct drm_device *dev, void *data,
77 struct drm_file *file_priv)
78{
Eric Anholt673a3942008-07-30 12:06:12 -070079 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080080 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070081
82 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080083 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070084 mutex_unlock(&dev->struct_mutex);
85
Jesse Barnes79e53942008-11-07 14:24:08 -080086 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070087}
88
Eric Anholt5a125c32008-10-22 21:40:13 -070089int
90i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
91 struct drm_file *file_priv)
92{
Eric Anholt5a125c32008-10-22 21:40:13 -070093 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -070094
95 if (!(dev->driver->driver_features & DRIVER_GEM))
96 return -ENODEV;
97
98 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -080099 args->aper_available_size = (args->aper_size -
100 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700101
102 return 0;
103}
104
Eric Anholt673a3942008-07-30 12:06:12 -0700105
106/**
107 * Creates a new mm object and returns a handle to it.
108 */
109int
110i915_gem_create_ioctl(struct drm_device *dev, void *data,
111 struct drm_file *file_priv)
112{
113 struct drm_i915_gem_create *args = data;
114 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300115 int ret;
116 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700117
118 args->size = roundup(args->size, PAGE_SIZE);
119
120 /* Allocate the new object */
121 obj = drm_gem_object_alloc(dev, args->size);
122 if (obj == NULL)
123 return -ENOMEM;
124
125 ret = drm_gem_handle_create(file_priv, obj, &handle);
126 mutex_lock(&dev->struct_mutex);
127 drm_gem_object_handle_unreference(obj);
128 mutex_unlock(&dev->struct_mutex);
129
130 if (ret)
131 return ret;
132
133 args->handle = handle;
134
135 return 0;
136}
137
Eric Anholt40123c12009-03-09 13:42:30 -0700138static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700139fast_shmem_read(struct page **pages,
140 loff_t page_base, int page_offset,
141 char __user *data,
142 int length)
143{
144 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200145 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700146
147 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
148 if (vaddr == NULL)
149 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200150 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700151 kunmap_atomic(vaddr, KM_USER0);
152
Florian Mickler2bc43b52009-04-06 22:55:41 +0200153 if (unwritten)
154 return -EFAULT;
155
156 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700157}
158
Eric Anholt280b7132009-03-12 16:56:27 -0700159static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
160{
161 drm_i915_private_t *dev_priv = obj->dev->dev_private;
162 struct drm_i915_gem_object *obj_priv = obj->driver_private;
163
164 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
165 obj_priv->tiling_mode != I915_TILING_NONE;
166}
167
Eric Anholteb014592009-03-10 11:44:52 -0700168static inline int
Eric Anholt40123c12009-03-09 13:42:30 -0700169slow_shmem_copy(struct page *dst_page,
170 int dst_offset,
171 struct page *src_page,
172 int src_offset,
173 int length)
174{
175 char *dst_vaddr, *src_vaddr;
176
177 dst_vaddr = kmap_atomic(dst_page, KM_USER0);
178 if (dst_vaddr == NULL)
179 return -ENOMEM;
180
181 src_vaddr = kmap_atomic(src_page, KM_USER1);
182 if (src_vaddr == NULL) {
183 kunmap_atomic(dst_vaddr, KM_USER0);
184 return -ENOMEM;
185 }
186
187 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
188
189 kunmap_atomic(src_vaddr, KM_USER1);
190 kunmap_atomic(dst_vaddr, KM_USER0);
191
192 return 0;
193}
194
Eric Anholt280b7132009-03-12 16:56:27 -0700195static inline int
196slow_shmem_bit17_copy(struct page *gpu_page,
197 int gpu_offset,
198 struct page *cpu_page,
199 int cpu_offset,
200 int length,
201 int is_read)
202{
203 char *gpu_vaddr, *cpu_vaddr;
204
205 /* Use the unswizzled path if this page isn't affected. */
206 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
207 if (is_read)
208 return slow_shmem_copy(cpu_page, cpu_offset,
209 gpu_page, gpu_offset, length);
210 else
211 return slow_shmem_copy(gpu_page, gpu_offset,
212 cpu_page, cpu_offset, length);
213 }
214
215 gpu_vaddr = kmap_atomic(gpu_page, KM_USER0);
216 if (gpu_vaddr == NULL)
217 return -ENOMEM;
218
219 cpu_vaddr = kmap_atomic(cpu_page, KM_USER1);
220 if (cpu_vaddr == NULL) {
221 kunmap_atomic(gpu_vaddr, KM_USER0);
222 return -ENOMEM;
223 }
224
225 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
226 * XORing with the other bits (A9 for Y, A9 and A10 for X)
227 */
228 while (length > 0) {
229 int cacheline_end = ALIGN(gpu_offset + 1, 64);
230 int this_length = min(cacheline_end - gpu_offset, length);
231 int swizzled_gpu_offset = gpu_offset ^ 64;
232
233 if (is_read) {
234 memcpy(cpu_vaddr + cpu_offset,
235 gpu_vaddr + swizzled_gpu_offset,
236 this_length);
237 } else {
238 memcpy(gpu_vaddr + swizzled_gpu_offset,
239 cpu_vaddr + cpu_offset,
240 this_length);
241 }
242 cpu_offset += this_length;
243 gpu_offset += this_length;
244 length -= this_length;
245 }
246
247 kunmap_atomic(cpu_vaddr, KM_USER1);
248 kunmap_atomic(gpu_vaddr, KM_USER0);
249
250 return 0;
251}
252
Eric Anholt673a3942008-07-30 12:06:12 -0700253/**
Eric Anholteb014592009-03-10 11:44:52 -0700254 * This is the fast shmem pread path, which attempts to copy_from_user directly
255 * from the backing pages of the object to the user's address space. On a
256 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
257 */
258static int
259i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
260 struct drm_i915_gem_pread *args,
261 struct drm_file *file_priv)
262{
263 struct drm_i915_gem_object *obj_priv = obj->driver_private;
264 ssize_t remain;
265 loff_t offset, page_base;
266 char __user *user_data;
267 int page_offset, page_length;
268 int ret;
269
270 user_data = (char __user *) (uintptr_t) args->data_ptr;
271 remain = args->size;
272
273 mutex_lock(&dev->struct_mutex);
274
275 ret = i915_gem_object_get_pages(obj);
276 if (ret != 0)
277 goto fail_unlock;
278
279 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
280 args->size);
281 if (ret != 0)
282 goto fail_put_pages;
283
284 obj_priv = obj->driver_private;
285 offset = args->offset;
286
287 while (remain > 0) {
288 /* Operation in this page
289 *
290 * page_base = page offset within aperture
291 * page_offset = offset within page
292 * page_length = bytes to copy for this page
293 */
294 page_base = (offset & ~(PAGE_SIZE-1));
295 page_offset = offset & (PAGE_SIZE-1);
296 page_length = remain;
297 if ((page_offset + remain) > PAGE_SIZE)
298 page_length = PAGE_SIZE - page_offset;
299
300 ret = fast_shmem_read(obj_priv->pages,
301 page_base, page_offset,
302 user_data, page_length);
303 if (ret)
304 goto fail_put_pages;
305
306 remain -= page_length;
307 user_data += page_length;
308 offset += page_length;
309 }
310
311fail_put_pages:
312 i915_gem_object_put_pages(obj);
313fail_unlock:
314 mutex_unlock(&dev->struct_mutex);
315
316 return ret;
317}
318
319/**
320 * This is the fallback shmem pread path, which allocates temporary storage
321 * in kernel space to copy_to_user into outside of the struct_mutex, so we
322 * can copy out of the object's backing pages while holding the struct mutex
323 * and not take page faults.
324 */
325static int
326i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
327 struct drm_i915_gem_pread *args,
328 struct drm_file *file_priv)
329{
330 struct drm_i915_gem_object *obj_priv = obj->driver_private;
331 struct mm_struct *mm = current->mm;
332 struct page **user_pages;
333 ssize_t remain;
334 loff_t offset, pinned_pages, i;
335 loff_t first_data_page, last_data_page, num_pages;
336 int shmem_page_index, shmem_page_offset;
337 int data_page_index, data_page_offset;
338 int page_length;
339 int ret;
340 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700341 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700342
343 remain = args->size;
344
345 /* Pin the user pages containing the data. We can't fault while
346 * holding the struct mutex, yet we want to hold it while
347 * dereferencing the user data.
348 */
349 first_data_page = data_ptr / PAGE_SIZE;
350 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
351 num_pages = last_data_page - first_data_page + 1;
352
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700353 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700354 if (user_pages == NULL)
355 return -ENOMEM;
356
357 down_read(&mm->mmap_sem);
358 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700359 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700360 up_read(&mm->mmap_sem);
361 if (pinned_pages < num_pages) {
362 ret = -EFAULT;
363 goto fail_put_user_pages;
364 }
365
Eric Anholt280b7132009-03-12 16:56:27 -0700366 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
367
Eric Anholteb014592009-03-10 11:44:52 -0700368 mutex_lock(&dev->struct_mutex);
369
370 ret = i915_gem_object_get_pages(obj);
371 if (ret != 0)
372 goto fail_unlock;
373
374 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
375 args->size);
376 if (ret != 0)
377 goto fail_put_pages;
378
379 obj_priv = obj->driver_private;
380 offset = args->offset;
381
382 while (remain > 0) {
383 /* Operation in this page
384 *
385 * shmem_page_index = page number within shmem file
386 * shmem_page_offset = offset within page in shmem file
387 * data_page_index = page number in get_user_pages return
388 * data_page_offset = offset with data_page_index page.
389 * page_length = bytes to copy for this page
390 */
391 shmem_page_index = offset / PAGE_SIZE;
392 shmem_page_offset = offset & ~PAGE_MASK;
393 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
394 data_page_offset = data_ptr & ~PAGE_MASK;
395
396 page_length = remain;
397 if ((shmem_page_offset + page_length) > PAGE_SIZE)
398 page_length = PAGE_SIZE - shmem_page_offset;
399 if ((data_page_offset + page_length) > PAGE_SIZE)
400 page_length = PAGE_SIZE - data_page_offset;
401
Eric Anholt280b7132009-03-12 16:56:27 -0700402 if (do_bit17_swizzling) {
403 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
404 shmem_page_offset,
405 user_pages[data_page_index],
406 data_page_offset,
407 page_length,
408 1);
409 } else {
410 ret = slow_shmem_copy(user_pages[data_page_index],
411 data_page_offset,
412 obj_priv->pages[shmem_page_index],
413 shmem_page_offset,
414 page_length);
415 }
Eric Anholteb014592009-03-10 11:44:52 -0700416 if (ret)
417 goto fail_put_pages;
418
419 remain -= page_length;
420 data_ptr += page_length;
421 offset += page_length;
422 }
423
424fail_put_pages:
425 i915_gem_object_put_pages(obj);
426fail_unlock:
427 mutex_unlock(&dev->struct_mutex);
428fail_put_user_pages:
429 for (i = 0; i < pinned_pages; i++) {
430 SetPageDirty(user_pages[i]);
431 page_cache_release(user_pages[i]);
432 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700433 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700434
435 return ret;
436}
437
Eric Anholt673a3942008-07-30 12:06:12 -0700438/**
439 * Reads data from the object referenced by handle.
440 *
441 * On error, the contents of *data are undefined.
442 */
443int
444i915_gem_pread_ioctl(struct drm_device *dev, void *data,
445 struct drm_file *file_priv)
446{
447 struct drm_i915_gem_pread *args = data;
448 struct drm_gem_object *obj;
449 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700450 int ret;
451
452 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
453 if (obj == NULL)
454 return -EBADF;
455 obj_priv = obj->driver_private;
456
457 /* Bounds check source.
458 *
459 * XXX: This could use review for overflow issues...
460 */
461 if (args->offset > obj->size || args->size > obj->size ||
462 args->offset + args->size > obj->size) {
463 drm_gem_object_unreference(obj);
464 return -EINVAL;
465 }
466
Eric Anholt280b7132009-03-12 16:56:27 -0700467 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700468 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700469 } else {
470 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
471 if (ret != 0)
472 ret = i915_gem_shmem_pread_slow(dev, obj, args,
473 file_priv);
474 }
Eric Anholt673a3942008-07-30 12:06:12 -0700475
476 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700477
Eric Anholteb014592009-03-10 11:44:52 -0700478 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700479}
480
Keith Packard0839ccb2008-10-30 19:38:48 -0700481/* This is the fast write path which cannot handle
482 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700483 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700484
Keith Packard0839ccb2008-10-30 19:38:48 -0700485static inline int
486fast_user_write(struct io_mapping *mapping,
487 loff_t page_base, int page_offset,
488 char __user *user_data,
489 int length)
490{
491 char *vaddr_atomic;
492 unsigned long unwritten;
493
494 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
495 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
496 user_data, length);
497 io_mapping_unmap_atomic(vaddr_atomic);
498 if (unwritten)
499 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700500 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700501}
502
503/* Here's the write path which can sleep for
504 * page faults
505 */
506
507static inline int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700508slow_kernel_write(struct io_mapping *mapping,
509 loff_t gtt_base, int gtt_offset,
510 struct page *user_page, int user_offset,
511 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700512{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700513 char *src_vaddr, *dst_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700514 unsigned long unwritten;
515
Eric Anholt3de09aa2009-03-09 09:42:23 -0700516 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
517 src_vaddr = kmap_atomic(user_page, KM_USER1);
518 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
519 src_vaddr + user_offset,
520 length);
521 kunmap_atomic(src_vaddr, KM_USER1);
522 io_mapping_unmap_atomic(dst_vaddr);
Keith Packard0839ccb2008-10-30 19:38:48 -0700523 if (unwritten)
524 return -EFAULT;
525 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700526}
527
Eric Anholt40123c12009-03-09 13:42:30 -0700528static inline int
529fast_shmem_write(struct page **pages,
530 loff_t page_base, int page_offset,
531 char __user *data,
532 int length)
533{
534 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400535 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700536
537 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
538 if (vaddr == NULL)
539 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400540 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700541 kunmap_atomic(vaddr, KM_USER0);
542
Dave Airlied0088772009-03-28 20:29:48 -0400543 if (unwritten)
544 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700545 return 0;
546}
547
Eric Anholt3de09aa2009-03-09 09:42:23 -0700548/**
549 * This is the fast pwrite path, where we copy the data directly from the
550 * user into the GTT, uncached.
551 */
Eric Anholt673a3942008-07-30 12:06:12 -0700552static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700553i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
554 struct drm_i915_gem_pwrite *args,
555 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700556{
557 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Keith Packard0839ccb2008-10-30 19:38:48 -0700558 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700559 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700560 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700561 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700562 int page_offset, page_length;
563 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700564
565 user_data = (char __user *) (uintptr_t) args->data_ptr;
566 remain = args->size;
567 if (!access_ok(VERIFY_READ, user_data, remain))
568 return -EFAULT;
569
570
571 mutex_lock(&dev->struct_mutex);
572 ret = i915_gem_object_pin(obj, 0);
573 if (ret) {
574 mutex_unlock(&dev->struct_mutex);
575 return ret;
576 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800577 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700578 if (ret)
579 goto fail;
580
581 obj_priv = obj->driver_private;
582 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700583
584 while (remain > 0) {
585 /* Operation in this page
586 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700587 * page_base = page offset within aperture
588 * page_offset = offset within page
589 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700590 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700591 page_base = (offset & ~(PAGE_SIZE-1));
592 page_offset = offset & (PAGE_SIZE-1);
593 page_length = remain;
594 if ((page_offset + remain) > PAGE_SIZE)
595 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700596
Keith Packard0839ccb2008-10-30 19:38:48 -0700597 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
598 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700599
Keith Packard0839ccb2008-10-30 19:38:48 -0700600 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700601 * source page isn't available. Return the error and we'll
602 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700603 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700604 if (ret)
605 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700606
Keith Packard0839ccb2008-10-30 19:38:48 -0700607 remain -= page_length;
608 user_data += page_length;
609 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700610 }
Eric Anholt673a3942008-07-30 12:06:12 -0700611
612fail:
613 i915_gem_object_unpin(obj);
614 mutex_unlock(&dev->struct_mutex);
615
616 return ret;
617}
618
Eric Anholt3de09aa2009-03-09 09:42:23 -0700619/**
620 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
621 * the memory and maps it using kmap_atomic for copying.
622 *
623 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
624 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
625 */
Eric Anholt3043c602008-10-02 12:24:47 -0700626static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700627i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
628 struct drm_i915_gem_pwrite *args,
629 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700630{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700631 struct drm_i915_gem_object *obj_priv = obj->driver_private;
632 drm_i915_private_t *dev_priv = dev->dev_private;
633 ssize_t remain;
634 loff_t gtt_page_base, offset;
635 loff_t first_data_page, last_data_page, num_pages;
636 loff_t pinned_pages, i;
637 struct page **user_pages;
638 struct mm_struct *mm = current->mm;
639 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700640 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700641 uint64_t data_ptr = args->data_ptr;
642
643 remain = args->size;
644
645 /* Pin the user pages containing the data. We can't fault while
646 * holding the struct mutex, and all of the pwrite implementations
647 * want to hold it while dereferencing the user data.
648 */
649 first_data_page = data_ptr / PAGE_SIZE;
650 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
651 num_pages = last_data_page - first_data_page + 1;
652
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700653 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700654 if (user_pages == NULL)
655 return -ENOMEM;
656
657 down_read(&mm->mmap_sem);
658 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
659 num_pages, 0, 0, user_pages, NULL);
660 up_read(&mm->mmap_sem);
661 if (pinned_pages < num_pages) {
662 ret = -EFAULT;
663 goto out_unpin_pages;
664 }
665
666 mutex_lock(&dev->struct_mutex);
667 ret = i915_gem_object_pin(obj, 0);
668 if (ret)
669 goto out_unlock;
670
671 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
672 if (ret)
673 goto out_unpin_object;
674
675 obj_priv = obj->driver_private;
676 offset = obj_priv->gtt_offset + args->offset;
677
678 while (remain > 0) {
679 /* Operation in this page
680 *
681 * gtt_page_base = page offset within aperture
682 * gtt_page_offset = offset within page in aperture
683 * data_page_index = page number in get_user_pages return
684 * data_page_offset = offset with data_page_index page.
685 * page_length = bytes to copy for this page
686 */
687 gtt_page_base = offset & PAGE_MASK;
688 gtt_page_offset = offset & ~PAGE_MASK;
689 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
690 data_page_offset = data_ptr & ~PAGE_MASK;
691
692 page_length = remain;
693 if ((gtt_page_offset + page_length) > PAGE_SIZE)
694 page_length = PAGE_SIZE - gtt_page_offset;
695 if ((data_page_offset + page_length) > PAGE_SIZE)
696 page_length = PAGE_SIZE - data_page_offset;
697
698 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
699 gtt_page_base, gtt_page_offset,
700 user_pages[data_page_index],
701 data_page_offset,
702 page_length);
703
704 /* If we get a fault while copying data, then (presumably) our
705 * source page isn't available. Return the error and we'll
706 * retry in the slow path.
707 */
708 if (ret)
709 goto out_unpin_object;
710
711 remain -= page_length;
712 offset += page_length;
713 data_ptr += page_length;
714 }
715
716out_unpin_object:
717 i915_gem_object_unpin(obj);
718out_unlock:
719 mutex_unlock(&dev->struct_mutex);
720out_unpin_pages:
721 for (i = 0; i < pinned_pages; i++)
722 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700723 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700724
725 return ret;
726}
727
Eric Anholt40123c12009-03-09 13:42:30 -0700728/**
729 * This is the fast shmem pwrite path, which attempts to directly
730 * copy_from_user into the kmapped pages backing the object.
731 */
Eric Anholt673a3942008-07-30 12:06:12 -0700732static int
Eric Anholt40123c12009-03-09 13:42:30 -0700733i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
734 struct drm_i915_gem_pwrite *args,
735 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700736{
Eric Anholt40123c12009-03-09 13:42:30 -0700737 struct drm_i915_gem_object *obj_priv = obj->driver_private;
738 ssize_t remain;
739 loff_t offset, page_base;
740 char __user *user_data;
741 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700742 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700743
744 user_data = (char __user *) (uintptr_t) args->data_ptr;
745 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700746
747 mutex_lock(&dev->struct_mutex);
748
Eric Anholt40123c12009-03-09 13:42:30 -0700749 ret = i915_gem_object_get_pages(obj);
750 if (ret != 0)
751 goto fail_unlock;
752
Eric Anholte47c68e2008-11-14 13:35:19 -0800753 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700754 if (ret != 0)
755 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700756
Eric Anholt40123c12009-03-09 13:42:30 -0700757 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700758 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700759 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700760
Eric Anholt40123c12009-03-09 13:42:30 -0700761 while (remain > 0) {
762 /* Operation in this page
763 *
764 * page_base = page offset within aperture
765 * page_offset = offset within page
766 * page_length = bytes to copy for this page
767 */
768 page_base = (offset & ~(PAGE_SIZE-1));
769 page_offset = offset & (PAGE_SIZE-1);
770 page_length = remain;
771 if ((page_offset + remain) > PAGE_SIZE)
772 page_length = PAGE_SIZE - page_offset;
773
774 ret = fast_shmem_write(obj_priv->pages,
775 page_base, page_offset,
776 user_data, page_length);
777 if (ret)
778 goto fail_put_pages;
779
780 remain -= page_length;
781 user_data += page_length;
782 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700783 }
784
Eric Anholt40123c12009-03-09 13:42:30 -0700785fail_put_pages:
786 i915_gem_object_put_pages(obj);
787fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700788 mutex_unlock(&dev->struct_mutex);
789
Eric Anholt40123c12009-03-09 13:42:30 -0700790 return ret;
791}
792
793/**
794 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
795 * the memory and maps it using kmap_atomic for copying.
796 *
797 * This avoids taking mmap_sem for faulting on the user's address while the
798 * struct_mutex is held.
799 */
800static int
801i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
802 struct drm_i915_gem_pwrite *args,
803 struct drm_file *file_priv)
804{
805 struct drm_i915_gem_object *obj_priv = obj->driver_private;
806 struct mm_struct *mm = current->mm;
807 struct page **user_pages;
808 ssize_t remain;
809 loff_t offset, pinned_pages, i;
810 loff_t first_data_page, last_data_page, num_pages;
811 int shmem_page_index, shmem_page_offset;
812 int data_page_index, data_page_offset;
813 int page_length;
814 int ret;
815 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700816 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700817
818 remain = args->size;
819
820 /* Pin the user pages containing the data. We can't fault while
821 * holding the struct mutex, and all of the pwrite implementations
822 * want to hold it while dereferencing the user data.
823 */
824 first_data_page = data_ptr / PAGE_SIZE;
825 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
826 num_pages = last_data_page - first_data_page + 1;
827
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700828 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700829 if (user_pages == NULL)
830 return -ENOMEM;
831
832 down_read(&mm->mmap_sem);
833 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
834 num_pages, 0, 0, user_pages, NULL);
835 up_read(&mm->mmap_sem);
836 if (pinned_pages < num_pages) {
837 ret = -EFAULT;
838 goto fail_put_user_pages;
839 }
840
Eric Anholt280b7132009-03-12 16:56:27 -0700841 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
842
Eric Anholt40123c12009-03-09 13:42:30 -0700843 mutex_lock(&dev->struct_mutex);
844
845 ret = i915_gem_object_get_pages(obj);
846 if (ret != 0)
847 goto fail_unlock;
848
849 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
850 if (ret != 0)
851 goto fail_put_pages;
852
853 obj_priv = obj->driver_private;
854 offset = args->offset;
855 obj_priv->dirty = 1;
856
857 while (remain > 0) {
858 /* Operation in this page
859 *
860 * shmem_page_index = page number within shmem file
861 * shmem_page_offset = offset within page in shmem file
862 * data_page_index = page number in get_user_pages return
863 * data_page_offset = offset with data_page_index page.
864 * page_length = bytes to copy for this page
865 */
866 shmem_page_index = offset / PAGE_SIZE;
867 shmem_page_offset = offset & ~PAGE_MASK;
868 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
869 data_page_offset = data_ptr & ~PAGE_MASK;
870
871 page_length = remain;
872 if ((shmem_page_offset + page_length) > PAGE_SIZE)
873 page_length = PAGE_SIZE - shmem_page_offset;
874 if ((data_page_offset + page_length) > PAGE_SIZE)
875 page_length = PAGE_SIZE - data_page_offset;
876
Eric Anholt280b7132009-03-12 16:56:27 -0700877 if (do_bit17_swizzling) {
878 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
879 shmem_page_offset,
880 user_pages[data_page_index],
881 data_page_offset,
882 page_length,
883 0);
884 } else {
885 ret = slow_shmem_copy(obj_priv->pages[shmem_page_index],
886 shmem_page_offset,
887 user_pages[data_page_index],
888 data_page_offset,
889 page_length);
890 }
Eric Anholt40123c12009-03-09 13:42:30 -0700891 if (ret)
892 goto fail_put_pages;
893
894 remain -= page_length;
895 data_ptr += page_length;
896 offset += page_length;
897 }
898
899fail_put_pages:
900 i915_gem_object_put_pages(obj);
901fail_unlock:
902 mutex_unlock(&dev->struct_mutex);
903fail_put_user_pages:
904 for (i = 0; i < pinned_pages; i++)
905 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700906 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700907
908 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700909}
910
911/**
912 * Writes data to the object referenced by handle.
913 *
914 * On error, the contents of the buffer that were to be modified are undefined.
915 */
916int
917i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
918 struct drm_file *file_priv)
919{
920 struct drm_i915_gem_pwrite *args = data;
921 struct drm_gem_object *obj;
922 struct drm_i915_gem_object *obj_priv;
923 int ret = 0;
924
925 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
926 if (obj == NULL)
927 return -EBADF;
928 obj_priv = obj->driver_private;
929
930 /* Bounds check destination.
931 *
932 * XXX: This could use review for overflow issues...
933 */
934 if (args->offset > obj->size || args->size > obj->size ||
935 args->offset + args->size > obj->size) {
936 drm_gem_object_unreference(obj);
937 return -EINVAL;
938 }
939
940 /* We can only do the GTT pwrite on untiled buffers, as otherwise
941 * it would end up going through the fenced access, and we'll get
942 * different detiling behavior between reading and writing.
943 * pread/pwrite currently are reading and writing from the CPU
944 * perspective, requiring manual detiling by the client.
945 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000946 if (obj_priv->phys_obj)
947 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
948 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Eric Anholt3de09aa2009-03-09 09:42:23 -0700949 dev->gtt_total != 0) {
950 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
951 if (ret == -EFAULT) {
952 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
953 file_priv);
954 }
Eric Anholt280b7132009-03-12 16:56:27 -0700955 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
956 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700957 } else {
958 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
959 if (ret == -EFAULT) {
960 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
961 file_priv);
962 }
963 }
Eric Anholt673a3942008-07-30 12:06:12 -0700964
965#if WATCH_PWRITE
966 if (ret)
967 DRM_INFO("pwrite failed %d\n", ret);
968#endif
969
970 drm_gem_object_unreference(obj);
971
972 return ret;
973}
974
975/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800976 * Called when user space prepares to use an object with the CPU, either
977 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700978 */
979int
980i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
981 struct drm_file *file_priv)
982{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700983 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700984 struct drm_i915_gem_set_domain *args = data;
985 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700986 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800987 uint32_t read_domains = args->read_domains;
988 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700989 int ret;
990
991 if (!(dev->driver->driver_features & DRIVER_GEM))
992 return -ENODEV;
993
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800994 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100995 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800996 return -EINVAL;
997
Chris Wilson21d509e2009-06-06 09:46:02 +0100998 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800999 return -EINVAL;
1000
1001 /* Having something in the write domain implies it's in the read
1002 * domain, and only that read domain. Enforce that in the request.
1003 */
1004 if (write_domain != 0 && read_domains != write_domain)
1005 return -EINVAL;
1006
Eric Anholt673a3942008-07-30 12:06:12 -07001007 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1008 if (obj == NULL)
1009 return -EBADF;
Jesse Barnes652c3932009-08-17 13:31:43 -07001010 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001011
1012 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001013
1014 intel_mark_busy(dev, obj);
1015
Eric Anholt673a3942008-07-30 12:06:12 -07001016#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001017 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001018 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001019#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001020 if (read_domains & I915_GEM_DOMAIN_GTT) {
1021 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001022
Eric Anholta09ba7f2009-08-29 12:49:51 -07001023 /* Update the LRU on the fence for the CPU access that's
1024 * about to occur.
1025 */
1026 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1027 list_move_tail(&obj_priv->fence_list,
1028 &dev_priv->mm.fence_list);
1029 }
1030
Eric Anholt02354392008-11-26 13:58:13 -08001031 /* Silently promote "you're not bound, there was nothing to do"
1032 * to success, since the client was just asking us to
1033 * make sure everything was done.
1034 */
1035 if (ret == -EINVAL)
1036 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001037 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001038 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001039 }
1040
Eric Anholt673a3942008-07-30 12:06:12 -07001041 drm_gem_object_unreference(obj);
1042 mutex_unlock(&dev->struct_mutex);
1043 return ret;
1044}
1045
1046/**
1047 * Called when user space has done writes to this buffer
1048 */
1049int
1050i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1051 struct drm_file *file_priv)
1052{
1053 struct drm_i915_gem_sw_finish *args = data;
1054 struct drm_gem_object *obj;
1055 struct drm_i915_gem_object *obj_priv;
1056 int ret = 0;
1057
1058 if (!(dev->driver->driver_features & DRIVER_GEM))
1059 return -ENODEV;
1060
1061 mutex_lock(&dev->struct_mutex);
1062 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1063 if (obj == NULL) {
1064 mutex_unlock(&dev->struct_mutex);
1065 return -EBADF;
1066 }
1067
1068#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001069 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001070 __func__, args->handle, obj, obj->size);
1071#endif
1072 obj_priv = obj->driver_private;
1073
1074 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001075 if (obj_priv->pin_count)
1076 i915_gem_object_flush_cpu_write_domain(obj);
1077
Eric Anholt673a3942008-07-30 12:06:12 -07001078 drm_gem_object_unreference(obj);
1079 mutex_unlock(&dev->struct_mutex);
1080 return ret;
1081}
1082
1083/**
1084 * Maps the contents of an object, returning the address it is mapped
1085 * into.
1086 *
1087 * While the mapping holds a reference on the contents of the object, it doesn't
1088 * imply a ref on the object itself.
1089 */
1090int
1091i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1092 struct drm_file *file_priv)
1093{
1094 struct drm_i915_gem_mmap *args = data;
1095 struct drm_gem_object *obj;
1096 loff_t offset;
1097 unsigned long addr;
1098
1099 if (!(dev->driver->driver_features & DRIVER_GEM))
1100 return -ENODEV;
1101
1102 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1103 if (obj == NULL)
1104 return -EBADF;
1105
1106 offset = args->offset;
1107
1108 down_write(&current->mm->mmap_sem);
1109 addr = do_mmap(obj->filp, 0, args->size,
1110 PROT_READ | PROT_WRITE, MAP_SHARED,
1111 args->offset);
1112 up_write(&current->mm->mmap_sem);
1113 mutex_lock(&dev->struct_mutex);
1114 drm_gem_object_unreference(obj);
1115 mutex_unlock(&dev->struct_mutex);
1116 if (IS_ERR((void *)addr))
1117 return addr;
1118
1119 args->addr_ptr = (uint64_t) addr;
1120
1121 return 0;
1122}
1123
Jesse Barnesde151cf2008-11-12 10:03:55 -08001124/**
1125 * i915_gem_fault - fault a page into the GTT
1126 * vma: VMA in question
1127 * vmf: fault info
1128 *
1129 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1130 * from userspace. The fault handler takes care of binding the object to
1131 * the GTT (if needed), allocating and programming a fence register (again,
1132 * only if needed based on whether the old reg is still valid or the object
1133 * is tiled) and inserting a new PTE into the faulting process.
1134 *
1135 * Note that the faulting process may involve evicting existing objects
1136 * from the GTT and/or fence registers to make room. So performance may
1137 * suffer if the GTT working set is large or there are few fence registers
1138 * left.
1139 */
1140int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1141{
1142 struct drm_gem_object *obj = vma->vm_private_data;
1143 struct drm_device *dev = obj->dev;
1144 struct drm_i915_private *dev_priv = dev->dev_private;
1145 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1146 pgoff_t page_offset;
1147 unsigned long pfn;
1148 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001149 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001150
1151 /* We don't use vmf->pgoff since that has the fake offset */
1152 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1153 PAGE_SHIFT;
1154
1155 /* Now bind it into the GTT if needed */
1156 mutex_lock(&dev->struct_mutex);
1157 if (!obj_priv->gtt_space) {
1158 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1159 if (ret) {
1160 mutex_unlock(&dev->struct_mutex);
1161 return VM_FAULT_SIGBUS;
1162 }
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001163
1164 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1165 if (ret) {
1166 mutex_unlock(&dev->struct_mutex);
1167 return VM_FAULT_SIGBUS;
1168 }
1169
Jesse Barnes14b60392009-05-20 16:47:08 -04001170 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001171 }
1172
1173 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001174 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001175 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001176 if (ret) {
1177 mutex_unlock(&dev->struct_mutex);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001178 return VM_FAULT_SIGBUS;
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001179 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001180 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001181
1182 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1183 page_offset;
1184
1185 /* Finally, remap it using the new GTT offset */
1186 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1187
1188 mutex_unlock(&dev->struct_mutex);
1189
1190 switch (ret) {
1191 case -ENOMEM:
1192 case -EAGAIN:
1193 return VM_FAULT_OOM;
1194 case -EFAULT:
Jesse Barnes959b8872009-03-20 14:16:33 -07001195 case -EINVAL:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001196 return VM_FAULT_SIGBUS;
1197 default:
1198 return VM_FAULT_NOPAGE;
1199 }
1200}
1201
1202/**
1203 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1204 * @obj: obj in question
1205 *
1206 * GEM memory mapping works by handing back to userspace a fake mmap offset
1207 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1208 * up the object based on the offset and sets up the various memory mapping
1209 * structures.
1210 *
1211 * This routine allocates and attaches a fake offset for @obj.
1212 */
1213static int
1214i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1215{
1216 struct drm_device *dev = obj->dev;
1217 struct drm_gem_mm *mm = dev->mm_private;
1218 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1219 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001220 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001221 int ret = 0;
1222
1223 /* Set the object up for mmap'ing */
1224 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001225 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001226 if (!list->map)
1227 return -ENOMEM;
1228
1229 map = list->map;
1230 map->type = _DRM_GEM;
1231 map->size = obj->size;
1232 map->handle = obj;
1233
1234 /* Get a DRM GEM mmap offset allocated... */
1235 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1236 obj->size / PAGE_SIZE, 0, 0);
1237 if (!list->file_offset_node) {
1238 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1239 ret = -ENOMEM;
1240 goto out_free_list;
1241 }
1242
1243 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1244 obj->size / PAGE_SIZE, 0);
1245 if (!list->file_offset_node) {
1246 ret = -ENOMEM;
1247 goto out_free_list;
1248 }
1249
1250 list->hash.key = list->file_offset_node->start;
1251 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1252 DRM_ERROR("failed to add to map hash\n");
1253 goto out_free_mm;
1254 }
1255
1256 /* By now we should be all set, any drm_mmap request on the offset
1257 * below will get to our mmap & fault handler */
1258 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1259
1260 return 0;
1261
1262out_free_mm:
1263 drm_mm_put_block(list->file_offset_node);
1264out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001265 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001266
1267 return ret;
1268}
1269
Chris Wilson901782b2009-07-10 08:18:50 +01001270/**
1271 * i915_gem_release_mmap - remove physical page mappings
1272 * @obj: obj in question
1273 *
1274 * Preserve the reservation of the mmaping with the DRM core code, but
1275 * relinquish ownership of the pages back to the system.
1276 *
1277 * It is vital that we remove the page mapping if we have mapped a tiled
1278 * object through the GTT and then lose the fence register due to
1279 * resource pressure. Similarly if the object has been moved out of the
1280 * aperture, than pages mapped into userspace must be revoked. Removing the
1281 * mapping will then trigger a page fault on the next user access, allowing
1282 * fixup by i915_gem_fault().
1283 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001284void
Chris Wilson901782b2009-07-10 08:18:50 +01001285i915_gem_release_mmap(struct drm_gem_object *obj)
1286{
1287 struct drm_device *dev = obj->dev;
1288 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1289
1290 if (dev->dev_mapping)
1291 unmap_mapping_range(dev->dev_mapping,
1292 obj_priv->mmap_offset, obj->size, 1);
1293}
1294
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001295static void
1296i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1297{
1298 struct drm_device *dev = obj->dev;
1299 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1300 struct drm_gem_mm *mm = dev->mm_private;
1301 struct drm_map_list *list;
1302
1303 list = &obj->map_list;
1304 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1305
1306 if (list->file_offset_node) {
1307 drm_mm_put_block(list->file_offset_node);
1308 list->file_offset_node = NULL;
1309 }
1310
1311 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001312 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001313 list->map = NULL;
1314 }
1315
1316 obj_priv->mmap_offset = 0;
1317}
1318
Jesse Barnesde151cf2008-11-12 10:03:55 -08001319/**
1320 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1321 * @obj: object to check
1322 *
1323 * Return the required GTT alignment for an object, taking into account
1324 * potential fence register mapping if needed.
1325 */
1326static uint32_t
1327i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1328{
1329 struct drm_device *dev = obj->dev;
1330 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1331 int start, i;
1332
1333 /*
1334 * Minimum alignment is 4k (GTT page size), but might be greater
1335 * if a fence register is needed for the object.
1336 */
1337 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1338 return 4096;
1339
1340 /*
1341 * Previous chips need to be aligned to the size of the smallest
1342 * fence register that can contain the object.
1343 */
1344 if (IS_I9XX(dev))
1345 start = 1024*1024;
1346 else
1347 start = 512*1024;
1348
1349 for (i = start; i < obj->size; i <<= 1)
1350 ;
1351
1352 return i;
1353}
1354
1355/**
1356 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1357 * @dev: DRM device
1358 * @data: GTT mapping ioctl data
1359 * @file_priv: GEM object info
1360 *
1361 * Simply returns the fake offset to userspace so it can mmap it.
1362 * The mmap call will end up in drm_gem_mmap(), which will set things
1363 * up so we can get faults in the handler above.
1364 *
1365 * The fault handler will take care of binding the object into the GTT
1366 * (since it may have been evicted to make room for something), allocating
1367 * a fence register, and mapping the appropriate aperture address into
1368 * userspace.
1369 */
1370int
1371i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1372 struct drm_file *file_priv)
1373{
1374 struct drm_i915_gem_mmap_gtt *args = data;
1375 struct drm_i915_private *dev_priv = dev->dev_private;
1376 struct drm_gem_object *obj;
1377 struct drm_i915_gem_object *obj_priv;
1378 int ret;
1379
1380 if (!(dev->driver->driver_features & DRIVER_GEM))
1381 return -ENODEV;
1382
1383 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1384 if (obj == NULL)
1385 return -EBADF;
1386
1387 mutex_lock(&dev->struct_mutex);
1388
1389 obj_priv = obj->driver_private;
1390
1391 if (!obj_priv->mmap_offset) {
1392 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001393 if (ret) {
1394 drm_gem_object_unreference(obj);
1395 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001396 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001397 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001398 }
1399
1400 args->offset = obj_priv->mmap_offset;
1401
1402 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
1403
1404 /* Make sure the alignment is correct for fence regs etc */
1405 if (obj_priv->agp_mem &&
1406 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
1407 drm_gem_object_unreference(obj);
1408 mutex_unlock(&dev->struct_mutex);
1409 return -EINVAL;
1410 }
1411
1412 /*
1413 * Pull it into the GTT so that we have a page list (makes the
1414 * initial fault faster and any subsequent flushing possible).
1415 */
1416 if (!obj_priv->agp_mem) {
1417 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1418 if (ret) {
1419 drm_gem_object_unreference(obj);
1420 mutex_unlock(&dev->struct_mutex);
1421 return ret;
1422 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001423 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001424 }
1425
1426 drm_gem_object_unreference(obj);
1427 mutex_unlock(&dev->struct_mutex);
1428
1429 return 0;
1430}
1431
Ben Gamari6911a9b2009-04-02 11:24:54 -07001432void
Eric Anholt856fa192009-03-19 14:10:50 -07001433i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001434{
1435 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1436 int page_count = obj->size / PAGE_SIZE;
1437 int i;
1438
Eric Anholt856fa192009-03-19 14:10:50 -07001439 BUG_ON(obj_priv->pages_refcount == 0);
1440
1441 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001442 return;
1443
Eric Anholt280b7132009-03-12 16:56:27 -07001444 if (obj_priv->tiling_mode != I915_TILING_NONE)
1445 i915_gem_object_save_bit_17_swizzle(obj);
1446
Eric Anholt673a3942008-07-30 12:06:12 -07001447 for (i = 0; i < page_count; i++)
Eric Anholt856fa192009-03-19 14:10:50 -07001448 if (obj_priv->pages[i] != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07001449 if (obj_priv->dirty)
Eric Anholt856fa192009-03-19 14:10:50 -07001450 set_page_dirty(obj_priv->pages[i]);
1451 mark_page_accessed(obj_priv->pages[i]);
1452 page_cache_release(obj_priv->pages[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07001453 }
1454 obj_priv->dirty = 0;
1455
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001456 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001457 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001458}
1459
1460static void
Eric Anholtce44b0e2008-11-06 16:00:31 -08001461i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001462{
1463 struct drm_device *dev = obj->dev;
1464 drm_i915_private_t *dev_priv = dev->dev_private;
1465 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1466
1467 /* Add a reference if we're newly entering the active list. */
1468 if (!obj_priv->active) {
1469 drm_gem_object_reference(obj);
1470 obj_priv->active = 1;
1471 }
1472 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001473 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001474 list_move_tail(&obj_priv->list,
1475 &dev_priv->mm.active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001476 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001477 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001478}
1479
Eric Anholtce44b0e2008-11-06 16:00:31 -08001480static void
1481i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1482{
1483 struct drm_device *dev = obj->dev;
1484 drm_i915_private_t *dev_priv = dev->dev_private;
1485 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1486
1487 BUG_ON(!obj_priv->active);
1488 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1489 obj_priv->last_rendering_seqno = 0;
1490}
Eric Anholt673a3942008-07-30 12:06:12 -07001491
1492static void
1493i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1494{
1495 struct drm_device *dev = obj->dev;
1496 drm_i915_private_t *dev_priv = dev->dev_private;
1497 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1498
1499 i915_verify_inactive(dev, __FILE__, __LINE__);
1500 if (obj_priv->pin_count != 0)
1501 list_del_init(&obj_priv->list);
1502 else
1503 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1504
Eric Anholtce44b0e2008-11-06 16:00:31 -08001505 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001506 if (obj_priv->active) {
1507 obj_priv->active = 0;
1508 drm_gem_object_unreference(obj);
1509 }
1510 i915_verify_inactive(dev, __FILE__, __LINE__);
1511}
1512
1513/**
1514 * Creates a new sequence number, emitting a write of it to the status page
1515 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1516 *
1517 * Must be called with struct_lock held.
1518 *
1519 * Returned sequence numbers are nonzero on success.
1520 */
1521static uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001522i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
1523 uint32_t flush_domains)
Eric Anholt673a3942008-07-30 12:06:12 -07001524{
1525 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001526 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001527 struct drm_i915_gem_request *request;
1528 uint32_t seqno;
1529 int was_empty;
1530 RING_LOCALS;
1531
Eric Anholtb9624422009-06-03 07:27:35 +00001532 if (file_priv != NULL)
1533 i915_file_priv = file_priv->driver_priv;
1534
Eric Anholt9a298b22009-03-24 12:23:04 -07001535 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001536 if (request == NULL)
1537 return 0;
1538
1539 /* Grab the seqno we're going to make this request be, and bump the
1540 * next (skipping 0 so it can be the reserved no-seqno value).
1541 */
1542 seqno = dev_priv->mm.next_gem_seqno;
1543 dev_priv->mm.next_gem_seqno++;
1544 if (dev_priv->mm.next_gem_seqno == 0)
1545 dev_priv->mm.next_gem_seqno++;
1546
1547 BEGIN_LP_RING(4);
1548 OUT_RING(MI_STORE_DWORD_INDEX);
1549 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1550 OUT_RING(seqno);
1551
1552 OUT_RING(MI_USER_INTERRUPT);
1553 ADVANCE_LP_RING();
1554
1555 DRM_DEBUG("%d\n", seqno);
1556
1557 request->seqno = seqno;
1558 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -07001559 was_empty = list_empty(&dev_priv->mm.request_list);
1560 list_add_tail(&request->list, &dev_priv->mm.request_list);
Eric Anholtb9624422009-06-03 07:27:35 +00001561 if (i915_file_priv) {
1562 list_add_tail(&request->client_list,
1563 &i915_file_priv->mm.request_list);
1564 } else {
1565 INIT_LIST_HEAD(&request->client_list);
1566 }
Eric Anholt673a3942008-07-30 12:06:12 -07001567
Eric Anholtce44b0e2008-11-06 16:00:31 -08001568 /* Associate any objects on the flushing list matching the write
1569 * domain we're flushing with our flush.
1570 */
1571 if (flush_domains != 0) {
1572 struct drm_i915_gem_object *obj_priv, *next;
1573
1574 list_for_each_entry_safe(obj_priv, next,
1575 &dev_priv->mm.flushing_list, list) {
1576 struct drm_gem_object *obj = obj_priv->obj;
1577
1578 if ((obj->write_domain & flush_domains) ==
1579 obj->write_domain) {
1580 obj->write_domain = 0;
1581 i915_gem_object_move_to_active(obj, seqno);
1582 }
1583 }
1584
1585 }
1586
Keith Packard6dbe2772008-10-14 21:41:13 -07001587 if (was_empty && !dev_priv->mm.suspended)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001588 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001589 return seqno;
1590}
1591
1592/**
1593 * Command execution barrier
1594 *
1595 * Ensures that all commands in the ring are finished
1596 * before signalling the CPU
1597 */
Eric Anholt3043c602008-10-02 12:24:47 -07001598static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -07001599i915_retire_commands(struct drm_device *dev)
1600{
1601 drm_i915_private_t *dev_priv = dev->dev_private;
1602 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1603 uint32_t flush_domains = 0;
1604 RING_LOCALS;
1605
1606 /* The sampler always gets flushed on i965 (sigh) */
1607 if (IS_I965G(dev))
1608 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1609 BEGIN_LP_RING(2);
1610 OUT_RING(cmd);
1611 OUT_RING(0); /* noop */
1612 ADVANCE_LP_RING();
1613 return flush_domains;
1614}
1615
1616/**
1617 * Moves buffers associated only with the given active seqno from the active
1618 * to inactive list, potentially freeing them.
1619 */
1620static void
1621i915_gem_retire_request(struct drm_device *dev,
1622 struct drm_i915_gem_request *request)
1623{
1624 drm_i915_private_t *dev_priv = dev->dev_private;
1625
1626 /* Move any buffers on the active list that are no longer referenced
1627 * by the ringbuffer to the flushing/inactive lists as appropriate.
1628 */
Carl Worth5e118f42009-03-20 11:54:25 -07001629 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001630 while (!list_empty(&dev_priv->mm.active_list)) {
1631 struct drm_gem_object *obj;
1632 struct drm_i915_gem_object *obj_priv;
1633
1634 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1635 struct drm_i915_gem_object,
1636 list);
1637 obj = obj_priv->obj;
1638
1639 /* If the seqno being retired doesn't match the oldest in the
1640 * list, then the oldest in the list must still be newer than
1641 * this seqno.
1642 */
1643 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001644 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001645
Eric Anholt673a3942008-07-30 12:06:12 -07001646#if WATCH_LRU
1647 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1648 __func__, request->seqno, obj);
1649#endif
1650
Eric Anholtce44b0e2008-11-06 16:00:31 -08001651 if (obj->write_domain != 0)
1652 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001653 else {
1654 /* Take a reference on the object so it won't be
1655 * freed while the spinlock is held. The list
1656 * protection for this spinlock is safe when breaking
1657 * the lock like this since the next thing we do
1658 * is just get the head of the list again.
1659 */
1660 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001661 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001662 spin_unlock(&dev_priv->mm.active_list_lock);
1663 drm_gem_object_unreference(obj);
1664 spin_lock(&dev_priv->mm.active_list_lock);
1665 }
Eric Anholt673a3942008-07-30 12:06:12 -07001666 }
Carl Worth5e118f42009-03-20 11:54:25 -07001667out:
1668 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001669}
1670
1671/**
1672 * Returns true if seq1 is later than seq2.
1673 */
1674static int
1675i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1676{
1677 return (int32_t)(seq1 - seq2) >= 0;
1678}
1679
1680uint32_t
1681i915_get_gem_seqno(struct drm_device *dev)
1682{
1683 drm_i915_private_t *dev_priv = dev->dev_private;
1684
1685 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1686}
1687
1688/**
1689 * This function clears the request list as sequence numbers are passed.
1690 */
1691void
1692i915_gem_retire_requests(struct drm_device *dev)
1693{
1694 drm_i915_private_t *dev_priv = dev->dev_private;
1695 uint32_t seqno;
1696
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001697 if (!dev_priv->hw_status_page)
1698 return;
1699
Eric Anholt673a3942008-07-30 12:06:12 -07001700 seqno = i915_get_gem_seqno(dev);
1701
1702 while (!list_empty(&dev_priv->mm.request_list)) {
1703 struct drm_i915_gem_request *request;
1704 uint32_t retiring_seqno;
1705
1706 request = list_first_entry(&dev_priv->mm.request_list,
1707 struct drm_i915_gem_request,
1708 list);
1709 retiring_seqno = request->seqno;
1710
1711 if (i915_seqno_passed(seqno, retiring_seqno) ||
1712 dev_priv->mm.wedged) {
1713 i915_gem_retire_request(dev, request);
1714
1715 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001716 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001717 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001718 } else
1719 break;
1720 }
1721}
1722
1723void
1724i915_gem_retire_work_handler(struct work_struct *work)
1725{
1726 drm_i915_private_t *dev_priv;
1727 struct drm_device *dev;
1728
1729 dev_priv = container_of(work, drm_i915_private_t,
1730 mm.retire_work.work);
1731 dev = dev_priv->dev;
1732
1733 mutex_lock(&dev->struct_mutex);
1734 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001735 if (!dev_priv->mm.suspended &&
1736 !list_empty(&dev_priv->mm.request_list))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001737 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001738 mutex_unlock(&dev->struct_mutex);
1739}
1740
1741/**
1742 * Waits for a sequence number to be signaled, and cleans up the
1743 * request and object lists appropriately for that event.
1744 */
Eric Anholt3043c602008-10-02 12:24:47 -07001745static int
Eric Anholt673a3942008-07-30 12:06:12 -07001746i915_wait_request(struct drm_device *dev, uint32_t seqno)
1747{
1748 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001749 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001750 int ret = 0;
1751
1752 BUG_ON(seqno == 0);
1753
1754 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001755 if (IS_IGDNG(dev))
1756 ier = I915_READ(DEIER) | I915_READ(GTIER);
1757 else
1758 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001759 if (!ier) {
1760 DRM_ERROR("something (likely vbetool) disabled "
1761 "interrupts, re-enabling\n");
1762 i915_driver_irq_preinstall(dev);
1763 i915_driver_irq_postinstall(dev);
1764 }
1765
Eric Anholt673a3942008-07-30 12:06:12 -07001766 dev_priv->mm.waiting_gem_seqno = seqno;
1767 i915_user_irq_get(dev);
1768 ret = wait_event_interruptible(dev_priv->irq_queue,
1769 i915_seqno_passed(i915_get_gem_seqno(dev),
1770 seqno) ||
1771 dev_priv->mm.wedged);
1772 i915_user_irq_put(dev);
1773 dev_priv->mm.waiting_gem_seqno = 0;
1774 }
1775 if (dev_priv->mm.wedged)
1776 ret = -EIO;
1777
1778 if (ret && ret != -ERESTARTSYS)
1779 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1780 __func__, ret, seqno, i915_get_gem_seqno(dev));
1781
1782 /* Directly dispatch request retiring. While we have the work queue
1783 * to handle this, the waiter on a request often wants an associated
1784 * buffer to have made it to the inactive list, and we would need
1785 * a separate wait queue to handle that.
1786 */
1787 if (ret == 0)
1788 i915_gem_retire_requests(dev);
1789
1790 return ret;
1791}
1792
1793static void
1794i915_gem_flush(struct drm_device *dev,
1795 uint32_t invalidate_domains,
1796 uint32_t flush_domains)
1797{
1798 drm_i915_private_t *dev_priv = dev->dev_private;
1799 uint32_t cmd;
1800 RING_LOCALS;
1801
1802#if WATCH_EXEC
1803 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1804 invalidate_domains, flush_domains);
1805#endif
1806
1807 if (flush_domains & I915_GEM_DOMAIN_CPU)
1808 drm_agp_chipset_flush(dev);
1809
Chris Wilson21d509e2009-06-06 09:46:02 +01001810 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
Eric Anholt673a3942008-07-30 12:06:12 -07001811 /*
1812 * read/write caches:
1813 *
1814 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1815 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1816 * also flushed at 2d versus 3d pipeline switches.
1817 *
1818 * read-only caches:
1819 *
1820 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1821 * MI_READ_FLUSH is set, and is always flushed on 965.
1822 *
1823 * I915_GEM_DOMAIN_COMMAND may not exist?
1824 *
1825 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1826 * invalidated when MI_EXE_FLUSH is set.
1827 *
1828 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1829 * invalidated with every MI_FLUSH.
1830 *
1831 * TLBs:
1832 *
1833 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1834 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1835 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1836 * are flushed at any MI_FLUSH.
1837 */
1838
1839 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1840 if ((invalidate_domains|flush_domains) &
1841 I915_GEM_DOMAIN_RENDER)
1842 cmd &= ~MI_NO_WRITE_FLUSH;
1843 if (!IS_I965G(dev)) {
1844 /*
1845 * On the 965, the sampler cache always gets flushed
1846 * and this bit is reserved.
1847 */
1848 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1849 cmd |= MI_READ_FLUSH;
1850 }
1851 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1852 cmd |= MI_EXE_FLUSH;
1853
1854#if WATCH_EXEC
1855 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1856#endif
1857 BEGIN_LP_RING(2);
1858 OUT_RING(cmd);
1859 OUT_RING(0); /* noop */
1860 ADVANCE_LP_RING();
1861 }
1862}
1863
1864/**
1865 * Ensures that all rendering to the object has completed and the object is
1866 * safe to unbind from the GTT or access from the CPU.
1867 */
1868static int
1869i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1870{
1871 struct drm_device *dev = obj->dev;
1872 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1873 int ret;
1874
Eric Anholte47c68e2008-11-14 13:35:19 -08001875 /* This function only exists to support waiting for existing rendering,
1876 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001877 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001878 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001879
1880 /* If there is rendering queued on the buffer being evicted, wait for
1881 * it.
1882 */
1883 if (obj_priv->active) {
1884#if WATCH_BUF
1885 DRM_INFO("%s: object %p wait for seqno %08x\n",
1886 __func__, obj, obj_priv->last_rendering_seqno);
1887#endif
1888 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1889 if (ret != 0)
1890 return ret;
1891 }
1892
1893 return 0;
1894}
1895
1896/**
1897 * Unbinds an object from the GTT aperture.
1898 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001899int
Eric Anholt673a3942008-07-30 12:06:12 -07001900i915_gem_object_unbind(struct drm_gem_object *obj)
1901{
1902 struct drm_device *dev = obj->dev;
1903 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1904 int ret = 0;
1905
1906#if WATCH_BUF
1907 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1908 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1909#endif
1910 if (obj_priv->gtt_space == NULL)
1911 return 0;
1912
1913 if (obj_priv->pin_count != 0) {
1914 DRM_ERROR("Attempting to unbind pinned buffer\n");
1915 return -EINVAL;
1916 }
1917
Eric Anholt5323fd02009-09-09 11:50:45 -07001918 /* blow away mappings if mapped through GTT */
1919 i915_gem_release_mmap(obj);
1920
1921 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1922 i915_gem_clear_fence_reg(obj);
1923
Eric Anholt673a3942008-07-30 12:06:12 -07001924 /* Move the object to the CPU domain to ensure that
1925 * any possible CPU writes while it's not in the GTT
1926 * are flushed when we go to remap it. This will
1927 * also ensure that all pending GPU writes are finished
1928 * before we unbind.
1929 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001930 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001931 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001932 if (ret != -ERESTARTSYS)
1933 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001934 return ret;
1935 }
1936
Eric Anholt5323fd02009-09-09 11:50:45 -07001937 BUG_ON(obj_priv->active);
1938
Eric Anholt673a3942008-07-30 12:06:12 -07001939 if (obj_priv->agp_mem != NULL) {
1940 drm_unbind_agp(obj_priv->agp_mem);
1941 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1942 obj_priv->agp_mem = NULL;
1943 }
1944
Eric Anholt856fa192009-03-19 14:10:50 -07001945 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001946
1947 if (obj_priv->gtt_space) {
1948 atomic_dec(&dev->gtt_count);
1949 atomic_sub(obj->size, &dev->gtt_memory);
1950
1951 drm_mm_put_block(obj_priv->gtt_space);
1952 obj_priv->gtt_space = NULL;
1953 }
1954
1955 /* Remove ourselves from the LRU list if present. */
1956 if (!list_empty(&obj_priv->list))
1957 list_del_init(&obj_priv->list);
1958
1959 return 0;
1960}
1961
1962static int
1963i915_gem_evict_something(struct drm_device *dev)
1964{
1965 drm_i915_private_t *dev_priv = dev->dev_private;
1966 struct drm_gem_object *obj;
1967 struct drm_i915_gem_object *obj_priv;
1968 int ret = 0;
1969
1970 for (;;) {
1971 /* If there's an inactive buffer available now, grab it
1972 * and be done.
1973 */
1974 if (!list_empty(&dev_priv->mm.inactive_list)) {
1975 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1976 struct drm_i915_gem_object,
1977 list);
1978 obj = obj_priv->obj;
1979 BUG_ON(obj_priv->pin_count != 0);
1980#if WATCH_LRU
1981 DRM_INFO("%s: evicting %p\n", __func__, obj);
1982#endif
1983 BUG_ON(obj_priv->active);
1984
1985 /* Wait on the rendering and unbind the buffer. */
1986 ret = i915_gem_object_unbind(obj);
1987 break;
1988 }
1989
1990 /* If we didn't get anything, but the ring is still processing
1991 * things, wait for one of those things to finish and hopefully
1992 * leave us a buffer to evict.
1993 */
1994 if (!list_empty(&dev_priv->mm.request_list)) {
1995 struct drm_i915_gem_request *request;
1996
1997 request = list_first_entry(&dev_priv->mm.request_list,
1998 struct drm_i915_gem_request,
1999 list);
2000
2001 ret = i915_wait_request(dev, request->seqno);
2002 if (ret)
2003 break;
2004
2005 /* if waiting caused an object to become inactive,
2006 * then loop around and wait for it. Otherwise, we
2007 * assume that waiting freed and unbound something,
2008 * so there should now be some space in the GTT
2009 */
2010 if (!list_empty(&dev_priv->mm.inactive_list))
2011 continue;
2012 break;
2013 }
2014
2015 /* If we didn't have anything on the request list but there
2016 * are buffers awaiting a flush, emit one and try again.
2017 * When we wait on it, those buffers waiting for that flush
2018 * will get moved to inactive.
2019 */
2020 if (!list_empty(&dev_priv->mm.flushing_list)) {
2021 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
2022 struct drm_i915_gem_object,
2023 list);
2024 obj = obj_priv->obj;
2025
2026 i915_gem_flush(dev,
2027 obj->write_domain,
2028 obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002029 i915_add_request(dev, NULL, obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002030
2031 obj = NULL;
2032 continue;
2033 }
2034
2035 DRM_ERROR("inactive empty %d request empty %d "
2036 "flushing empty %d\n",
2037 list_empty(&dev_priv->mm.inactive_list),
2038 list_empty(&dev_priv->mm.request_list),
2039 list_empty(&dev_priv->mm.flushing_list));
2040 /* If we didn't do any of the above, there's nothing to be done
2041 * and we just can't fit it in.
2042 */
Chris Wilson2939e1f2009-06-06 09:46:03 +01002043 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002044 }
2045 return ret;
2046}
2047
2048static int
Keith Packardac94a962008-11-20 23:30:27 -08002049i915_gem_evict_everything(struct drm_device *dev)
2050{
2051 int ret;
2052
2053 for (;;) {
2054 ret = i915_gem_evict_something(dev);
2055 if (ret != 0)
2056 break;
2057 }
Chris Wilson2939e1f2009-06-06 09:46:03 +01002058 if (ret == -ENOSPC)
Owain Ainsworth15c35332008-12-06 20:42:20 -08002059 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08002060 return ret;
2061}
2062
Ben Gamari6911a9b2009-04-02 11:24:54 -07002063int
Eric Anholt856fa192009-03-19 14:10:50 -07002064i915_gem_object_get_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002065{
2066 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2067 int page_count, i;
2068 struct address_space *mapping;
2069 struct inode *inode;
2070 struct page *page;
2071 int ret;
2072
Eric Anholt856fa192009-03-19 14:10:50 -07002073 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002074 return 0;
2075
2076 /* Get the list of pages out of our struct file. They'll be pinned
2077 * at this point until we release them.
2078 */
2079 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002080 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002081 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002082 if (obj_priv->pages == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002083 DRM_ERROR("Faled to allocate page list\n");
Eric Anholt856fa192009-03-19 14:10:50 -07002084 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002085 return -ENOMEM;
2086 }
2087
2088 inode = obj->filp->f_path.dentry->d_inode;
2089 mapping = inode->i_mapping;
2090 for (i = 0; i < page_count; i++) {
2091 page = read_mapping_page(mapping, i, NULL);
2092 if (IS_ERR(page)) {
2093 ret = PTR_ERR(page);
2094 DRM_ERROR("read_mapping_page failed: %d\n", ret);
Eric Anholt856fa192009-03-19 14:10:50 -07002095 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002096 return ret;
2097 }
Eric Anholt856fa192009-03-19 14:10:50 -07002098 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002099 }
Eric Anholt280b7132009-03-12 16:56:27 -07002100
2101 if (obj_priv->tiling_mode != I915_TILING_NONE)
2102 i915_gem_object_do_bit_17_swizzle(obj);
2103
Eric Anholt673a3942008-07-30 12:06:12 -07002104 return 0;
2105}
2106
Jesse Barnesde151cf2008-11-12 10:03:55 -08002107static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2108{
2109 struct drm_gem_object *obj = reg->obj;
2110 struct drm_device *dev = obj->dev;
2111 drm_i915_private_t *dev_priv = dev->dev_private;
2112 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2113 int regnum = obj_priv->fence_reg;
2114 uint64_t val;
2115
2116 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2117 0xfffff000) << 32;
2118 val |= obj_priv->gtt_offset & 0xfffff000;
2119 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2120 if (obj_priv->tiling_mode == I915_TILING_Y)
2121 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2122 val |= I965_FENCE_REG_VALID;
2123
2124 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2125}
2126
2127static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2128{
2129 struct drm_gem_object *obj = reg->obj;
2130 struct drm_device *dev = obj->dev;
2131 drm_i915_private_t *dev_priv = dev->dev_private;
2132 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2133 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002134 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002135 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002136 uint32_t pitch_val;
2137
2138 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2139 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002140 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002141 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002142 return;
2143 }
2144
Jesse Barnes0f973f22009-01-26 17:10:45 -08002145 if (obj_priv->tiling_mode == I915_TILING_Y &&
2146 HAS_128_BYTE_Y_TILING(dev))
2147 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002148 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002149 tile_width = 512;
2150
2151 /* Note: pitch better be a power of two tile widths */
2152 pitch_val = obj_priv->stride / tile_width;
2153 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002154
2155 val = obj_priv->gtt_offset;
2156 if (obj_priv->tiling_mode == I915_TILING_Y)
2157 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2158 val |= I915_FENCE_SIZE_BITS(obj->size);
2159 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2160 val |= I830_FENCE_REG_VALID;
2161
Eric Anholtdc529a42009-03-10 22:34:49 -07002162 if (regnum < 8)
2163 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2164 else
2165 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2166 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002167}
2168
2169static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2170{
2171 struct drm_gem_object *obj = reg->obj;
2172 struct drm_device *dev = obj->dev;
2173 drm_i915_private_t *dev_priv = dev->dev_private;
2174 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2175 int regnum = obj_priv->fence_reg;
2176 uint32_t val;
2177 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002178 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002179
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002180 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002181 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002182 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002183 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002184 return;
2185 }
2186
Eric Anholte76a16d2009-05-26 17:44:56 -07002187 pitch_val = obj_priv->stride / 128;
2188 pitch_val = ffs(pitch_val) - 1;
2189 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2190
Jesse Barnesde151cf2008-11-12 10:03:55 -08002191 val = obj_priv->gtt_offset;
2192 if (obj_priv->tiling_mode == I915_TILING_Y)
2193 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002194 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2195 WARN_ON(fence_size_bits & ~0x00000f00);
2196 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002197 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2198 val |= I830_FENCE_REG_VALID;
2199
2200 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002201}
2202
2203/**
2204 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2205 * @obj: object to map through a fence reg
2206 *
2207 * When mapping objects through the GTT, userspace wants to be able to write
2208 * to them without having to worry about swizzling if the object is tiled.
2209 *
2210 * This function walks the fence regs looking for a free one for @obj,
2211 * stealing one if it can't find any.
2212 *
2213 * It then sets up the reg based on the object's properties: address, pitch
2214 * and tiling format.
2215 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002216int
2217i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002218{
2219 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002220 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002221 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2222 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002223 struct drm_i915_gem_object *old_obj_priv = NULL;
2224 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002225
Eric Anholta09ba7f2009-08-29 12:49:51 -07002226 /* Just update our place in the LRU if our fence is getting used. */
2227 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
2228 list_move_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2229 return 0;
2230 }
2231
Jesse Barnesde151cf2008-11-12 10:03:55 -08002232 switch (obj_priv->tiling_mode) {
2233 case I915_TILING_NONE:
2234 WARN(1, "allocating a fence for non-tiled object?\n");
2235 break;
2236 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002237 if (!obj_priv->stride)
2238 return -EINVAL;
2239 WARN((obj_priv->stride & (512 - 1)),
2240 "object 0x%08x is X tiled but has non-512B pitch\n",
2241 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002242 break;
2243 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002244 if (!obj_priv->stride)
2245 return -EINVAL;
2246 WARN((obj_priv->stride & (128 - 1)),
2247 "object 0x%08x is Y tiled but has non-128B pitch\n",
2248 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002249 break;
2250 }
2251
2252 /* First try to find a free reg */
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002253 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002254 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2255 reg = &dev_priv->fence_regs[i];
2256 if (!reg->obj)
2257 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002258
2259 old_obj_priv = reg->obj->driver_private;
2260 if (!old_obj_priv->pin_count)
2261 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002262 }
2263
2264 /* None available, try to steal one or wait for a user to finish */
2265 if (i == dev_priv->num_fence_regs) {
Eric Anholta09ba7f2009-08-29 12:49:51 -07002266 struct drm_gem_object *old_obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002267
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002268 if (avail == 0)
Chris Wilson2939e1f2009-06-06 09:46:03 +01002269 return -ENOSPC;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002270
Eric Anholta09ba7f2009-08-29 12:49:51 -07002271 list_for_each_entry(old_obj_priv, &dev_priv->mm.fence_list,
2272 fence_list) {
2273 old_obj = old_obj_priv->obj;
Chris Wilsond7619c42009-02-11 14:26:47 +00002274
2275 if (old_obj_priv->pin_count)
2276 continue;
2277
Eric Anholta09ba7f2009-08-29 12:49:51 -07002278 /* Take a reference, as otherwise the wait_rendering
2279 * below may cause the object to get freed out from
2280 * under us.
2281 */
2282 drm_gem_object_reference(old_obj);
2283
Chris Wilsond7619c42009-02-11 14:26:47 +00002284 /* i915 uses fences for GPU access to tiled buffers */
2285 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002286 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002287
Eric Anholta09ba7f2009-08-29 12:49:51 -07002288 /* This brings the object to the head of the LRU if it
2289 * had been written to. The only way this should
2290 * result in us waiting longer than the expected
2291 * optimal amount of time is if there was a
2292 * fence-using buffer later that was read-only.
2293 */
2294 i915_gem_object_flush_gpu_write_domain(old_obj);
2295 ret = i915_gem_object_wait_rendering(old_obj);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002296 if (ret != 0) {
2297 drm_gem_object_unreference(old_obj);
Chris Wilsond7619c42009-02-11 14:26:47 +00002298 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002299 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002300
Eric Anholta09ba7f2009-08-29 12:49:51 -07002301 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002302 }
2303
2304 /*
2305 * Zap this virtual mapping so we can set up a fence again
2306 * for this object next time we need it.
2307 */
Chris Wilson58c2fb62009-09-01 12:02:39 +01002308 i915_gem_release_mmap(old_obj);
2309
Eric Anholta09ba7f2009-08-29 12:49:51 -07002310 i = old_obj_priv->fence_reg;
Chris Wilson58c2fb62009-09-01 12:02:39 +01002311 reg = &dev_priv->fence_regs[i];
2312
Jesse Barnesde151cf2008-11-12 10:03:55 -08002313 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002314 list_del_init(&old_obj_priv->fence_list);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002315
Eric Anholta09ba7f2009-08-29 12:49:51 -07002316 drm_gem_object_unreference(old_obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317 }
2318
2319 obj_priv->fence_reg = i;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002320 list_add_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2321
Jesse Barnesde151cf2008-11-12 10:03:55 -08002322 reg->obj = obj;
2323
2324 if (IS_I965G(dev))
2325 i965_write_fence_reg(reg);
2326 else if (IS_I9XX(dev))
2327 i915_write_fence_reg(reg);
2328 else
2329 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002330
2331 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002332}
2333
2334/**
2335 * i915_gem_clear_fence_reg - clear out fence register info
2336 * @obj: object to clear
2337 *
2338 * Zeroes out the fence register itself and clears out the associated
2339 * data structures in dev_priv and obj_priv.
2340 */
2341static void
2342i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2343{
2344 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002345 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002346 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2347
2348 if (IS_I965G(dev))
2349 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002350 else {
2351 uint32_t fence_reg;
2352
2353 if (obj_priv->fence_reg < 8)
2354 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2355 else
2356 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2357 8) * 4;
2358
2359 I915_WRITE(fence_reg, 0);
2360 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002361
2362 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2363 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002364 list_del_init(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002365}
2366
Eric Anholt673a3942008-07-30 12:06:12 -07002367/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002368 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2369 * to the buffer to finish, and then resets the fence register.
2370 * @obj: tiled object holding a fence register.
2371 *
2372 * Zeroes out the fence register itself and clears out the associated
2373 * data structures in dev_priv and obj_priv.
2374 */
2375int
2376i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2377{
2378 struct drm_device *dev = obj->dev;
2379 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2380
2381 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2382 return 0;
2383
2384 /* On the i915, GPU access to tiled buffers is via a fence,
2385 * therefore we must wait for any outstanding access to complete
2386 * before clearing the fence.
2387 */
2388 if (!IS_I965G(dev)) {
2389 int ret;
2390
2391 i915_gem_object_flush_gpu_write_domain(obj);
2392 i915_gem_object_flush_gtt_write_domain(obj);
2393 ret = i915_gem_object_wait_rendering(obj);
2394 if (ret != 0)
2395 return ret;
2396 }
2397
2398 i915_gem_clear_fence_reg (obj);
2399
2400 return 0;
2401}
2402
2403/**
Eric Anholt673a3942008-07-30 12:06:12 -07002404 * Finds free space in the GTT aperture and binds the object there.
2405 */
2406static int
2407i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2408{
2409 struct drm_device *dev = obj->dev;
2410 drm_i915_private_t *dev_priv = dev->dev_private;
2411 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2412 struct drm_mm_node *free_space;
2413 int page_count, ret;
2414
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002415 if (dev_priv->mm.suspended)
2416 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002417 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002418 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002419 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002420 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2421 return -EINVAL;
2422 }
2423
2424 search_free:
2425 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2426 obj->size, alignment, 0);
2427 if (free_space != NULL) {
2428 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2429 alignment);
2430 if (obj_priv->gtt_space != NULL) {
2431 obj_priv->gtt_space->private = obj;
2432 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2433 }
2434 }
2435 if (obj_priv->gtt_space == NULL) {
Carl Worth5e118f42009-03-20 11:54:25 -07002436 bool lists_empty;
2437
Eric Anholt673a3942008-07-30 12:06:12 -07002438 /* If the gtt is empty and we're still having trouble
2439 * fitting our object in, we're out of memory.
2440 */
2441#if WATCH_LRU
2442 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2443#endif
Carl Worth5e118f42009-03-20 11:54:25 -07002444 spin_lock(&dev_priv->mm.active_list_lock);
2445 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2446 list_empty(&dev_priv->mm.flushing_list) &&
2447 list_empty(&dev_priv->mm.active_list));
2448 spin_unlock(&dev_priv->mm.active_list_lock);
2449 if (lists_empty) {
Eric Anholt673a3942008-07-30 12:06:12 -07002450 DRM_ERROR("GTT full, but LRU list empty\n");
Chris Wilson2939e1f2009-06-06 09:46:03 +01002451 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002452 }
2453
2454 ret = i915_gem_evict_something(dev);
2455 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08002456 if (ret != -ERESTARTSYS)
2457 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002458 return ret;
2459 }
2460 goto search_free;
2461 }
2462
2463#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002464 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002465 obj->size, obj_priv->gtt_offset);
2466#endif
Eric Anholt856fa192009-03-19 14:10:50 -07002467 ret = i915_gem_object_get_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002468 if (ret) {
2469 drm_mm_put_block(obj_priv->gtt_space);
2470 obj_priv->gtt_space = NULL;
2471 return ret;
2472 }
2473
2474 page_count = obj->size / PAGE_SIZE;
2475 /* Create an AGP memory structure pointing at our pages, and bind it
2476 * into the GTT.
2477 */
2478 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002479 obj_priv->pages,
Eric Anholt673a3942008-07-30 12:06:12 -07002480 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002481 obj_priv->gtt_offset,
2482 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002483 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002484 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002485 drm_mm_put_block(obj_priv->gtt_space);
2486 obj_priv->gtt_space = NULL;
2487 return -ENOMEM;
2488 }
2489 atomic_inc(&dev->gtt_count);
2490 atomic_add(obj->size, &dev->gtt_memory);
2491
2492 /* Assert that the object is not currently in any GPU domain. As it
2493 * wasn't in the GTT, there shouldn't be any way it could have been in
2494 * a GPU cache
2495 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002496 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2497 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002498
2499 return 0;
2500}
2501
2502void
2503i915_gem_clflush_object(struct drm_gem_object *obj)
2504{
2505 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2506
2507 /* If we don't have a page list set up, then we're not pinned
2508 * to GPU, and we can ignore the cache flush because it'll happen
2509 * again at bind time.
2510 */
Eric Anholt856fa192009-03-19 14:10:50 -07002511 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002512 return;
2513
Eric Anholt856fa192009-03-19 14:10:50 -07002514 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002515}
2516
Eric Anholte47c68e2008-11-14 13:35:19 -08002517/** Flushes any GPU write domain for the object if it's dirty. */
2518static void
2519i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2520{
2521 struct drm_device *dev = obj->dev;
2522 uint32_t seqno;
2523
2524 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2525 return;
2526
2527 /* Queue the GPU write cache flushing we need. */
2528 i915_gem_flush(dev, 0, obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002529 seqno = i915_add_request(dev, NULL, obj->write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002530 obj->write_domain = 0;
2531 i915_gem_object_move_to_active(obj, seqno);
2532}
2533
2534/** Flushes the GTT write domain for the object if it's dirty. */
2535static void
2536i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2537{
2538 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2539 return;
2540
2541 /* No actual flushing is required for the GTT write domain. Writes
2542 * to it immediately go to main memory as far as we know, so there's
2543 * no chipset flush. It also doesn't land in render cache.
2544 */
2545 obj->write_domain = 0;
2546}
2547
2548/** Flushes the CPU write domain for the object if it's dirty. */
2549static void
2550i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2551{
2552 struct drm_device *dev = obj->dev;
2553
2554 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2555 return;
2556
2557 i915_gem_clflush_object(obj);
2558 drm_agp_chipset_flush(dev);
2559 obj->write_domain = 0;
2560}
2561
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002562/**
2563 * Moves a single object to the GTT read, and possibly write domain.
2564 *
2565 * This function returns when the move is complete, including waiting on
2566 * flushes to occur.
2567 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002568int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002569i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2570{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002571 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002572 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002573
Eric Anholt02354392008-11-26 13:58:13 -08002574 /* Not valid to be called on unbound objects. */
2575 if (obj_priv->gtt_space == NULL)
2576 return -EINVAL;
2577
Eric Anholte47c68e2008-11-14 13:35:19 -08002578 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002579 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002580 ret = i915_gem_object_wait_rendering(obj);
2581 if (ret != 0)
2582 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002583
2584 /* If we're writing through the GTT domain, then CPU and GPU caches
2585 * will need to be invalidated at next use.
2586 */
2587 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002588 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002589
Eric Anholte47c68e2008-11-14 13:35:19 -08002590 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002591
2592 /* It should now be out of any other write domains, and we can update
2593 * the domain values for our changes.
2594 */
2595 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2596 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002597 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002598 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002599 obj_priv->dirty = 1;
2600 }
2601
2602 return 0;
2603}
2604
2605/**
2606 * Moves a single object to the CPU read, and possibly write domain.
2607 *
2608 * This function returns when the move is complete, including waiting on
2609 * flushes to occur.
2610 */
2611static int
2612i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2613{
Eric Anholte47c68e2008-11-14 13:35:19 -08002614 int ret;
2615
2616 i915_gem_object_flush_gpu_write_domain(obj);
2617 /* Wait on any GPU rendering and flushing to occur. */
2618 ret = i915_gem_object_wait_rendering(obj);
2619 if (ret != 0)
2620 return ret;
2621
2622 i915_gem_object_flush_gtt_write_domain(obj);
2623
2624 /* If we have a partially-valid cache of the object in the CPU,
2625 * finish invalidating it and free the per-page flags.
2626 */
2627 i915_gem_object_set_to_full_cpu_read_domain(obj);
2628
2629 /* Flush the CPU cache if it's still invalid. */
2630 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2631 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002632
2633 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2634 }
2635
2636 /* It should now be out of any other write domains, and we can update
2637 * the domain values for our changes.
2638 */
2639 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2640
2641 /* If we're writing through the CPU, then the GPU read domains will
2642 * need to be invalidated at next use.
2643 */
2644 if (write) {
2645 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2646 obj->write_domain = I915_GEM_DOMAIN_CPU;
2647 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002648
2649 return 0;
2650}
2651
Eric Anholt673a3942008-07-30 12:06:12 -07002652/*
2653 * Set the next domain for the specified object. This
2654 * may not actually perform the necessary flushing/invaliding though,
2655 * as that may want to be batched with other set_domain operations
2656 *
2657 * This is (we hope) the only really tricky part of gem. The goal
2658 * is fairly simple -- track which caches hold bits of the object
2659 * and make sure they remain coherent. A few concrete examples may
2660 * help to explain how it works. For shorthand, we use the notation
2661 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2662 * a pair of read and write domain masks.
2663 *
2664 * Case 1: the batch buffer
2665 *
2666 * 1. Allocated
2667 * 2. Written by CPU
2668 * 3. Mapped to GTT
2669 * 4. Read by GPU
2670 * 5. Unmapped from GTT
2671 * 6. Freed
2672 *
2673 * Let's take these a step at a time
2674 *
2675 * 1. Allocated
2676 * Pages allocated from the kernel may still have
2677 * cache contents, so we set them to (CPU, CPU) always.
2678 * 2. Written by CPU (using pwrite)
2679 * The pwrite function calls set_domain (CPU, CPU) and
2680 * this function does nothing (as nothing changes)
2681 * 3. Mapped by GTT
2682 * This function asserts that the object is not
2683 * currently in any GPU-based read or write domains
2684 * 4. Read by GPU
2685 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2686 * As write_domain is zero, this function adds in the
2687 * current read domains (CPU+COMMAND, 0).
2688 * flush_domains is set to CPU.
2689 * invalidate_domains is set to COMMAND
2690 * clflush is run to get data out of the CPU caches
2691 * then i915_dev_set_domain calls i915_gem_flush to
2692 * emit an MI_FLUSH and drm_agp_chipset_flush
2693 * 5. Unmapped from GTT
2694 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2695 * flush_domains and invalidate_domains end up both zero
2696 * so no flushing/invalidating happens
2697 * 6. Freed
2698 * yay, done
2699 *
2700 * Case 2: The shared render buffer
2701 *
2702 * 1. Allocated
2703 * 2. Mapped to GTT
2704 * 3. Read/written by GPU
2705 * 4. set_domain to (CPU,CPU)
2706 * 5. Read/written by CPU
2707 * 6. Read/written by GPU
2708 *
2709 * 1. Allocated
2710 * Same as last example, (CPU, CPU)
2711 * 2. Mapped to GTT
2712 * Nothing changes (assertions find that it is not in the GPU)
2713 * 3. Read/written by GPU
2714 * execbuffer calls set_domain (RENDER, RENDER)
2715 * flush_domains gets CPU
2716 * invalidate_domains gets GPU
2717 * clflush (obj)
2718 * MI_FLUSH and drm_agp_chipset_flush
2719 * 4. set_domain (CPU, CPU)
2720 * flush_domains gets GPU
2721 * invalidate_domains gets CPU
2722 * wait_rendering (obj) to make sure all drawing is complete.
2723 * This will include an MI_FLUSH to get the data from GPU
2724 * to memory
2725 * clflush (obj) to invalidate the CPU cache
2726 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2727 * 5. Read/written by CPU
2728 * cache lines are loaded and dirtied
2729 * 6. Read written by GPU
2730 * Same as last GPU access
2731 *
2732 * Case 3: The constant buffer
2733 *
2734 * 1. Allocated
2735 * 2. Written by CPU
2736 * 3. Read by GPU
2737 * 4. Updated (written) by CPU again
2738 * 5. Read by GPU
2739 *
2740 * 1. Allocated
2741 * (CPU, CPU)
2742 * 2. Written by CPU
2743 * (CPU, CPU)
2744 * 3. Read by GPU
2745 * (CPU+RENDER, 0)
2746 * flush_domains = CPU
2747 * invalidate_domains = RENDER
2748 * clflush (obj)
2749 * MI_FLUSH
2750 * drm_agp_chipset_flush
2751 * 4. Updated (written) by CPU again
2752 * (CPU, CPU)
2753 * flush_domains = 0 (no previous write domain)
2754 * invalidate_domains = 0 (no new read domains)
2755 * 5. Read by GPU
2756 * (CPU+RENDER, 0)
2757 * flush_domains = CPU
2758 * invalidate_domains = RENDER
2759 * clflush (obj)
2760 * MI_FLUSH
2761 * drm_agp_chipset_flush
2762 */
Keith Packardc0d90822008-11-20 23:11:08 -08002763static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002764i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002765{
2766 struct drm_device *dev = obj->dev;
2767 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2768 uint32_t invalidate_domains = 0;
2769 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002770
Eric Anholt8b0e3782009-02-19 14:40:50 -08002771 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2772 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002773
Jesse Barnes652c3932009-08-17 13:31:43 -07002774 intel_mark_busy(dev, obj);
2775
Eric Anholt673a3942008-07-30 12:06:12 -07002776#if WATCH_BUF
2777 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2778 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002779 obj->read_domains, obj->pending_read_domains,
2780 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002781#endif
2782 /*
2783 * If the object isn't moving to a new write domain,
2784 * let the object stay in multiple read domains
2785 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002786 if (obj->pending_write_domain == 0)
2787 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002788 else
2789 obj_priv->dirty = 1;
2790
2791 /*
2792 * Flush the current write domain if
2793 * the new read domains don't match. Invalidate
2794 * any read domains which differ from the old
2795 * write domain
2796 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002797 if (obj->write_domain &&
2798 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002799 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002800 invalidate_domains |=
2801 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002802 }
2803 /*
2804 * Invalidate any read caches which may have
2805 * stale data. That is, any new read domains.
2806 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002807 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002808 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2809#if WATCH_BUF
2810 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2811 __func__, flush_domains, invalidate_domains);
2812#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002813 i915_gem_clflush_object(obj);
2814 }
2815
Eric Anholtefbeed92009-02-19 14:54:51 -08002816 /* The actual obj->write_domain will be updated with
2817 * pending_write_domain after we emit the accumulated flush for all
2818 * of our domain changes in execbuffers (which clears objects'
2819 * write_domains). So if we have a current write domain that we
2820 * aren't changing, set pending_write_domain to that.
2821 */
2822 if (flush_domains == 0 && obj->pending_write_domain == 0)
2823 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002824 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002825
2826 dev->invalidate_domains |= invalidate_domains;
2827 dev->flush_domains |= flush_domains;
2828#if WATCH_BUF
2829 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2830 __func__,
2831 obj->read_domains, obj->write_domain,
2832 dev->invalidate_domains, dev->flush_domains);
2833#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002834}
2835
2836/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002837 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002838 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002839 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2840 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2841 */
2842static void
2843i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2844{
Eric Anholte47c68e2008-11-14 13:35:19 -08002845 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2846
2847 if (!obj_priv->page_cpu_valid)
2848 return;
2849
2850 /* If we're partially in the CPU read domain, finish moving it in.
2851 */
2852 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2853 int i;
2854
2855 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2856 if (obj_priv->page_cpu_valid[i])
2857 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07002858 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08002859 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002860 }
2861
2862 /* Free the page_cpu_valid mappings which are now stale, whether
2863 * or not we've got I915_GEM_DOMAIN_CPU.
2864 */
Eric Anholt9a298b22009-03-24 12:23:04 -07002865 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08002866 obj_priv->page_cpu_valid = NULL;
2867}
2868
2869/**
2870 * Set the CPU read domain on a range of the object.
2871 *
2872 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2873 * not entirely valid. The page_cpu_valid member of the object flags which
2874 * pages have been flushed, and will be respected by
2875 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2876 * of the whole object.
2877 *
2878 * This function returns when the move is complete, including waiting on
2879 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002880 */
2881static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002882i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2883 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002884{
2885 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002886 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002887
Eric Anholte47c68e2008-11-14 13:35:19 -08002888 if (offset == 0 && size == obj->size)
2889 return i915_gem_object_set_to_cpu_domain(obj, 0);
2890
2891 i915_gem_object_flush_gpu_write_domain(obj);
2892 /* Wait on any GPU rendering and flushing to occur. */
2893 ret = i915_gem_object_wait_rendering(obj);
2894 if (ret != 0)
2895 return ret;
2896 i915_gem_object_flush_gtt_write_domain(obj);
2897
2898 /* If we're already fully in the CPU read domain, we're done. */
2899 if (obj_priv->page_cpu_valid == NULL &&
2900 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002901 return 0;
2902
Eric Anholte47c68e2008-11-14 13:35:19 -08002903 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2904 * newly adding I915_GEM_DOMAIN_CPU
2905 */
Eric Anholt673a3942008-07-30 12:06:12 -07002906 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07002907 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
2908 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08002909 if (obj_priv->page_cpu_valid == NULL)
2910 return -ENOMEM;
2911 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2912 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002913
2914 /* Flush the cache on any pages that are still invalid from the CPU's
2915 * perspective.
2916 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002917 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2918 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002919 if (obj_priv->page_cpu_valid[i])
2920 continue;
2921
Eric Anholt856fa192009-03-19 14:10:50 -07002922 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002923
2924 obj_priv->page_cpu_valid[i] = 1;
2925 }
2926
Eric Anholte47c68e2008-11-14 13:35:19 -08002927 /* It should now be out of any other write domains, and we can update
2928 * the domain values for our changes.
2929 */
2930 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2931
2932 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2933
Eric Anholt673a3942008-07-30 12:06:12 -07002934 return 0;
2935}
2936
2937/**
Eric Anholt673a3942008-07-30 12:06:12 -07002938 * Pin an object to the GTT and evaluate the relocations landing in it.
2939 */
2940static int
2941i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2942 struct drm_file *file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002943 struct drm_i915_gem_exec_object *entry,
2944 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07002945{
2946 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002947 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002948 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2949 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002950 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002951
2952 /* Choose the GTT offset for our buffer and put it there. */
2953 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2954 if (ret)
2955 return ret;
2956
2957 entry->offset = obj_priv->gtt_offset;
2958
Eric Anholt673a3942008-07-30 12:06:12 -07002959 /* Apply the relocations, using the GTT aperture to avoid cache
2960 * flushing requirements.
2961 */
2962 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002963 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002964 struct drm_gem_object *target_obj;
2965 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002966 uint32_t reloc_val, reloc_offset;
2967 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002968
Eric Anholt673a3942008-07-30 12:06:12 -07002969 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002970 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002971 if (target_obj == NULL) {
2972 i915_gem_object_unpin(obj);
2973 return -EBADF;
2974 }
2975 target_obj_priv = target_obj->driver_private;
2976
2977 /* The target buffer should have appeared before us in the
2978 * exec_object list, so it should have a GTT space bound by now.
2979 */
2980 if (target_obj_priv->gtt_space == NULL) {
2981 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002982 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002983 drm_gem_object_unreference(target_obj);
2984 i915_gem_object_unpin(obj);
2985 return -EINVAL;
2986 }
2987
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002988 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07002989 DRM_ERROR("Relocation beyond object bounds: "
2990 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002991 obj, reloc->target_handle,
2992 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07002993 drm_gem_object_unreference(target_obj);
2994 i915_gem_object_unpin(obj);
2995 return -EINVAL;
2996 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002997 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07002998 DRM_ERROR("Relocation not 4-byte aligned: "
2999 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003000 obj, reloc->target_handle,
3001 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003002 drm_gem_object_unreference(target_obj);
3003 i915_gem_object_unpin(obj);
3004 return -EINVAL;
3005 }
3006
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003007 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3008 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003009 DRM_ERROR("reloc with read/write CPU domains: "
3010 "obj %p target %d offset %d "
3011 "read %08x write %08x",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003012 obj, reloc->target_handle,
3013 (int) reloc->offset,
3014 reloc->read_domains,
3015 reloc->write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00003016 drm_gem_object_unreference(target_obj);
3017 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003018 return -EINVAL;
3019 }
3020
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003021 if (reloc->write_domain && target_obj->pending_write_domain &&
3022 reloc->write_domain != target_obj->pending_write_domain) {
Eric Anholt673a3942008-07-30 12:06:12 -07003023 DRM_ERROR("Write domain conflict: "
3024 "obj %p target %d offset %d "
3025 "new %08x old %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003026 obj, reloc->target_handle,
3027 (int) reloc->offset,
3028 reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003029 target_obj->pending_write_domain);
3030 drm_gem_object_unreference(target_obj);
3031 i915_gem_object_unpin(obj);
3032 return -EINVAL;
3033 }
3034
3035#if WATCH_RELOC
3036 DRM_INFO("%s: obj %p offset %08x target %d "
3037 "read %08x write %08x gtt %08x "
3038 "presumed %08x delta %08x\n",
3039 __func__,
3040 obj,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003041 (int) reloc->offset,
3042 (int) reloc->target_handle,
3043 (int) reloc->read_domains,
3044 (int) reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003045 (int) target_obj_priv->gtt_offset,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003046 (int) reloc->presumed_offset,
3047 reloc->delta);
Eric Anholt673a3942008-07-30 12:06:12 -07003048#endif
3049
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003050 target_obj->pending_read_domains |= reloc->read_domains;
3051 target_obj->pending_write_domain |= reloc->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003052
3053 /* If the relocation already has the right value in it, no
3054 * more work needs to be done.
3055 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003056 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
Eric Anholt673a3942008-07-30 12:06:12 -07003057 drm_gem_object_unreference(target_obj);
3058 continue;
3059 }
3060
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003061 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3062 if (ret != 0) {
3063 drm_gem_object_unreference(target_obj);
3064 i915_gem_object_unpin(obj);
3065 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003066 }
3067
3068 /* Map the page containing the relocation we're going to
3069 * perform.
3070 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003071 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003072 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3073 (reloc_offset &
3074 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003075 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003076 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003077 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003078
3079#if WATCH_BUF
3080 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003081 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003082 readl(reloc_entry), reloc_val);
3083#endif
3084 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003085 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003086
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003087 /* The updated presumed offset for this entry will be
3088 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003089 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003090 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003091
3092 drm_gem_object_unreference(target_obj);
3093 }
3094
Eric Anholt673a3942008-07-30 12:06:12 -07003095#if WATCH_BUF
3096 if (0)
3097 i915_gem_dump_object(obj, 128, __func__, ~0);
3098#endif
3099 return 0;
3100}
3101
3102/** Dispatch a batchbuffer to the ring
3103 */
3104static int
3105i915_dispatch_gem_execbuffer(struct drm_device *dev,
3106 struct drm_i915_gem_execbuffer *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003107 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003108 uint64_t exec_offset)
3109{
3110 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003111 int nbox = exec->num_cliprects;
3112 int i = 0, count;
Chris Wilson83d60792009-06-06 09:45:57 +01003113 uint32_t exec_start, exec_len;
Eric Anholt673a3942008-07-30 12:06:12 -07003114 RING_LOCALS;
3115
3116 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3117 exec_len = (uint32_t) exec->batch_len;
3118
Eric Anholt673a3942008-07-30 12:06:12 -07003119 count = nbox ? nbox : 1;
3120
3121 for (i = 0; i < count; i++) {
3122 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003123 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003124 exec->DR1, exec->DR4);
3125 if (ret)
3126 return ret;
3127 }
3128
3129 if (IS_I830(dev) || IS_845G(dev)) {
3130 BEGIN_LP_RING(4);
3131 OUT_RING(MI_BATCH_BUFFER);
3132 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3133 OUT_RING(exec_start + exec_len - 4);
3134 OUT_RING(0);
3135 ADVANCE_LP_RING();
3136 } else {
3137 BEGIN_LP_RING(2);
3138 if (IS_I965G(dev)) {
3139 OUT_RING(MI_BATCH_BUFFER_START |
3140 (2 << 6) |
3141 MI_BATCH_NON_SECURE_I965);
3142 OUT_RING(exec_start);
3143 } else {
3144 OUT_RING(MI_BATCH_BUFFER_START |
3145 (2 << 6));
3146 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3147 }
3148 ADVANCE_LP_RING();
3149 }
3150 }
3151
3152 /* XXX breadcrumb */
3153 return 0;
3154}
3155
3156/* Throttle our rendering by waiting until the ring has completed our requests
3157 * emitted over 20 msec ago.
3158 *
Eric Anholtb9624422009-06-03 07:27:35 +00003159 * Note that if we were to use the current jiffies each time around the loop,
3160 * we wouldn't escape the function with any frames outstanding if the time to
3161 * render a frame was over 20ms.
3162 *
Eric Anholt673a3942008-07-30 12:06:12 -07003163 * This should get us reasonable parallelism between CPU and GPU but also
3164 * relatively low latency when blocking on a particular request to finish.
3165 */
3166static int
3167i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3168{
3169 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3170 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003171 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003172
3173 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003174 while (!list_empty(&i915_file_priv->mm.request_list)) {
3175 struct drm_i915_gem_request *request;
3176
3177 request = list_first_entry(&i915_file_priv->mm.request_list,
3178 struct drm_i915_gem_request,
3179 client_list);
3180
3181 if (time_after_eq(request->emitted_jiffies, recent_enough))
3182 break;
3183
3184 ret = i915_wait_request(dev, request->seqno);
3185 if (ret != 0)
3186 break;
3187 }
Eric Anholt673a3942008-07-30 12:06:12 -07003188 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003189
Eric Anholt673a3942008-07-30 12:06:12 -07003190 return ret;
3191}
3192
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003193static int
3194i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3195 uint32_t buffer_count,
3196 struct drm_i915_gem_relocation_entry **relocs)
3197{
3198 uint32_t reloc_count = 0, reloc_index = 0, i;
3199 int ret;
3200
3201 *relocs = NULL;
3202 for (i = 0; i < buffer_count; i++) {
3203 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3204 return -EINVAL;
3205 reloc_count += exec_list[i].relocation_count;
3206 }
3207
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003208 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003209 if (*relocs == NULL)
3210 return -ENOMEM;
3211
3212 for (i = 0; i < buffer_count; i++) {
3213 struct drm_i915_gem_relocation_entry __user *user_relocs;
3214
3215 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3216
3217 ret = copy_from_user(&(*relocs)[reloc_index],
3218 user_relocs,
3219 exec_list[i].relocation_count *
3220 sizeof(**relocs));
3221 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003222 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003223 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003224 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003225 }
3226
3227 reloc_index += exec_list[i].relocation_count;
3228 }
3229
Florian Mickler2bc43b52009-04-06 22:55:41 +02003230 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003231}
3232
3233static int
3234i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3235 uint32_t buffer_count,
3236 struct drm_i915_gem_relocation_entry *relocs)
3237{
3238 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003239 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003240
3241 for (i = 0; i < buffer_count; i++) {
3242 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003243 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003244
3245 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3246
Florian Mickler2bc43b52009-04-06 22:55:41 +02003247 unwritten = copy_to_user(user_relocs,
3248 &relocs[reloc_count],
3249 exec_list[i].relocation_count *
3250 sizeof(*relocs));
3251
3252 if (unwritten) {
3253 ret = -EFAULT;
3254 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003255 }
3256
3257 reloc_count += exec_list[i].relocation_count;
3258 }
3259
Florian Mickler2bc43b52009-04-06 22:55:41 +02003260err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003261 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003262
3263 return ret;
3264}
3265
Chris Wilson83d60792009-06-06 09:45:57 +01003266static int
3267i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer *exec,
3268 uint64_t exec_offset)
3269{
3270 uint32_t exec_start, exec_len;
3271
3272 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3273 exec_len = (uint32_t) exec->batch_len;
3274
3275 if ((exec_start | exec_len) & 0x7)
3276 return -EINVAL;
3277
3278 if (!exec_start)
3279 return -EINVAL;
3280
3281 return 0;
3282}
3283
Eric Anholt673a3942008-07-30 12:06:12 -07003284int
3285i915_gem_execbuffer(struct drm_device *dev, void *data,
3286 struct drm_file *file_priv)
3287{
3288 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003289 struct drm_i915_gem_execbuffer *args = data;
3290 struct drm_i915_gem_exec_object *exec_list = NULL;
3291 struct drm_gem_object **object_list = NULL;
3292 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003293 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003294 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003295 struct drm_i915_gem_relocation_entry *relocs;
3296 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003297 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003298 uint32_t seqno, flush_domains, reloc_index;
Keith Packardac94a962008-11-20 23:30:27 -08003299 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07003300
3301#if WATCH_EXEC
3302 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3303 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3304#endif
3305
Eric Anholt4f481ed2008-09-10 14:22:49 -07003306 if (args->buffer_count < 1) {
3307 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3308 return -EINVAL;
3309 }
Eric Anholt673a3942008-07-30 12:06:12 -07003310 /* Copy in the exec list from userland */
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003311 exec_list = drm_calloc_large(sizeof(*exec_list), args->buffer_count);
3312 object_list = drm_calloc_large(sizeof(*object_list), args->buffer_count);
Eric Anholt673a3942008-07-30 12:06:12 -07003313 if (exec_list == NULL || object_list == NULL) {
3314 DRM_ERROR("Failed to allocate exec or object list "
3315 "for %d buffers\n",
3316 args->buffer_count);
3317 ret = -ENOMEM;
3318 goto pre_mutex_err;
3319 }
3320 ret = copy_from_user(exec_list,
3321 (struct drm_i915_relocation_entry __user *)
3322 (uintptr_t) args->buffers_ptr,
3323 sizeof(*exec_list) * args->buffer_count);
3324 if (ret != 0) {
3325 DRM_ERROR("copy %d exec entries failed %d\n",
3326 args->buffer_count, ret);
3327 goto pre_mutex_err;
3328 }
3329
Eric Anholt201361a2009-03-11 12:30:04 -07003330 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003331 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3332 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -07003333 if (cliprects == NULL)
3334 goto pre_mutex_err;
3335
3336 ret = copy_from_user(cliprects,
3337 (struct drm_clip_rect __user *)
3338 (uintptr_t) args->cliprects_ptr,
3339 sizeof(*cliprects) * args->num_cliprects);
3340 if (ret != 0) {
3341 DRM_ERROR("copy %d cliprects failed: %d\n",
3342 args->num_cliprects, ret);
3343 goto pre_mutex_err;
3344 }
3345 }
3346
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003347 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3348 &relocs);
3349 if (ret != 0)
3350 goto pre_mutex_err;
3351
Eric Anholt673a3942008-07-30 12:06:12 -07003352 mutex_lock(&dev->struct_mutex);
3353
3354 i915_verify_inactive(dev, __FILE__, __LINE__);
3355
3356 if (dev_priv->mm.wedged) {
3357 DRM_ERROR("Execbuf while wedged\n");
3358 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003359 ret = -EIO;
3360 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003361 }
3362
3363 if (dev_priv->mm.suspended) {
3364 DRM_ERROR("Execbuf while VT-switched.\n");
3365 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003366 ret = -EBUSY;
3367 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003368 }
3369
Keith Packardac94a962008-11-20 23:30:27 -08003370 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003371 for (i = 0; i < args->buffer_count; i++) {
3372 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3373 exec_list[i].handle);
3374 if (object_list[i] == NULL) {
3375 DRM_ERROR("Invalid object handle %d at index %d\n",
3376 exec_list[i].handle, i);
3377 ret = -EBADF;
3378 goto err;
3379 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003380
3381 obj_priv = object_list[i]->driver_private;
3382 if (obj_priv->in_execbuffer) {
3383 DRM_ERROR("Object %p appears more than once in object list\n",
3384 object_list[i]);
3385 ret = -EBADF;
3386 goto err;
3387 }
3388 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003389 }
Eric Anholt673a3942008-07-30 12:06:12 -07003390
Keith Packardac94a962008-11-20 23:30:27 -08003391 /* Pin and relocate */
3392 for (pin_tries = 0; ; pin_tries++) {
3393 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003394 reloc_index = 0;
3395
Keith Packardac94a962008-11-20 23:30:27 -08003396 for (i = 0; i < args->buffer_count; i++) {
3397 object_list[i]->pending_read_domains = 0;
3398 object_list[i]->pending_write_domain = 0;
3399 ret = i915_gem_object_pin_and_relocate(object_list[i],
3400 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003401 &exec_list[i],
3402 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003403 if (ret)
3404 break;
3405 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003406 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003407 }
3408 /* success */
3409 if (ret == 0)
3410 break;
3411
3412 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003413 if (ret != -ENOSPC || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08003414 if (ret != -ERESTARTSYS)
3415 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003416 goto err;
3417 }
Keith Packardac94a962008-11-20 23:30:27 -08003418
3419 /* unpin all of our buffers */
3420 for (i = 0; i < pinned; i++)
3421 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003422 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003423
3424 /* evict everyone we can from the aperture */
3425 ret = i915_gem_evict_everything(dev);
3426 if (ret)
3427 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003428 }
3429
3430 /* Set the pending read domains for the batch buffer to COMMAND */
3431 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003432 if (batch_obj->pending_write_domain) {
3433 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3434 ret = -EINVAL;
3435 goto err;
3436 }
3437 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003438
Chris Wilson83d60792009-06-06 09:45:57 +01003439 /* Sanity check the batch buffer, prior to moving objects */
3440 exec_offset = exec_list[args->buffer_count - 1].offset;
3441 ret = i915_gem_check_execbuffer (args, exec_offset);
3442 if (ret != 0) {
3443 DRM_ERROR("execbuf with invalid offset/length\n");
3444 goto err;
3445 }
3446
Eric Anholt673a3942008-07-30 12:06:12 -07003447 i915_verify_inactive(dev, __FILE__, __LINE__);
3448
Keith Packard646f0f62008-11-20 23:23:03 -08003449 /* Zero the global flush/invalidate flags. These
3450 * will be modified as new domains are computed
3451 * for each object
3452 */
3453 dev->invalidate_domains = 0;
3454 dev->flush_domains = 0;
3455
Eric Anholt673a3942008-07-30 12:06:12 -07003456 for (i = 0; i < args->buffer_count; i++) {
3457 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003458
Keith Packard646f0f62008-11-20 23:23:03 -08003459 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003460 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003461 }
3462
3463 i915_verify_inactive(dev, __FILE__, __LINE__);
3464
Keith Packard646f0f62008-11-20 23:23:03 -08003465 if (dev->invalidate_domains | dev->flush_domains) {
3466#if WATCH_EXEC
3467 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3468 __func__,
3469 dev->invalidate_domains,
3470 dev->flush_domains);
3471#endif
3472 i915_gem_flush(dev,
3473 dev->invalidate_domains,
3474 dev->flush_domains);
3475 if (dev->flush_domains)
Eric Anholtb9624422009-06-03 07:27:35 +00003476 (void)i915_add_request(dev, file_priv,
3477 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003478 }
Eric Anholt673a3942008-07-30 12:06:12 -07003479
Eric Anholtefbeed92009-02-19 14:54:51 -08003480 for (i = 0; i < args->buffer_count; i++) {
3481 struct drm_gem_object *obj = object_list[i];
3482
3483 obj->write_domain = obj->pending_write_domain;
3484 }
3485
Eric Anholt673a3942008-07-30 12:06:12 -07003486 i915_verify_inactive(dev, __FILE__, __LINE__);
3487
3488#if WATCH_COHERENCY
3489 for (i = 0; i < args->buffer_count; i++) {
3490 i915_gem_object_check_coherency(object_list[i],
3491 exec_list[i].handle);
3492 }
3493#endif
3494
Eric Anholt673a3942008-07-30 12:06:12 -07003495#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003496 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003497 args->batch_len,
3498 __func__,
3499 ~0);
3500#endif
3501
Eric Anholt673a3942008-07-30 12:06:12 -07003502 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003503 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003504 if (ret) {
3505 DRM_ERROR("dispatch failed %d\n", ret);
3506 goto err;
3507 }
3508
3509 /*
3510 * Ensure that the commands in the batch buffer are
3511 * finished before the interrupt fires
3512 */
3513 flush_domains = i915_retire_commands(dev);
3514
3515 i915_verify_inactive(dev, __FILE__, __LINE__);
3516
3517 /*
3518 * Get a seqno representing the execution of the current buffer,
3519 * which we can wait on. We would like to mitigate these interrupts,
3520 * likely by only creating seqnos occasionally (so that we have
3521 * *some* interrupts representing completion of buffers that we can
3522 * wait on when trying to clear up gtt space).
3523 */
Eric Anholtb9624422009-06-03 07:27:35 +00003524 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003525 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003526 for (i = 0; i < args->buffer_count; i++) {
3527 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003528
Eric Anholtce44b0e2008-11-06 16:00:31 -08003529 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003530#if WATCH_LRU
3531 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3532#endif
3533 }
3534#if WATCH_LRU
3535 i915_dump_lru(dev, __func__);
3536#endif
3537
3538 i915_verify_inactive(dev, __FILE__, __LINE__);
3539
Eric Anholt673a3942008-07-30 12:06:12 -07003540err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003541 for (i = 0; i < pinned; i++)
3542 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003543
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003544 for (i = 0; i < args->buffer_count; i++) {
3545 if (object_list[i]) {
3546 obj_priv = object_list[i]->driver_private;
3547 obj_priv->in_execbuffer = false;
3548 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003549 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003550 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003551
Eric Anholt673a3942008-07-30 12:06:12 -07003552 mutex_unlock(&dev->struct_mutex);
3553
Roland Dreiera35f2e22009-02-06 17:48:09 -08003554 if (!ret) {
3555 /* Copy the new buffer offsets back to the user's exec list. */
3556 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3557 (uintptr_t) args->buffers_ptr,
3558 exec_list,
3559 sizeof(*exec_list) * args->buffer_count);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003560 if (ret) {
3561 ret = -EFAULT;
Roland Dreiera35f2e22009-02-06 17:48:09 -08003562 DRM_ERROR("failed to copy %d exec entries "
3563 "back to user (%d)\n",
3564 args->buffer_count, ret);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003565 }
Roland Dreiera35f2e22009-02-06 17:48:09 -08003566 }
3567
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003568 /* Copy the updated relocations out regardless of current error
3569 * state. Failure to update the relocs would mean that the next
3570 * time userland calls execbuf, it would do so with presumed offset
3571 * state that didn't match the actual object state.
3572 */
3573 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3574 relocs);
3575 if (ret2 != 0) {
3576 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3577
3578 if (ret == 0)
3579 ret = ret2;
3580 }
3581
Eric Anholt673a3942008-07-30 12:06:12 -07003582pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003583 drm_free_large(object_list);
3584 drm_free_large(exec_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003585 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003586
3587 return ret;
3588}
3589
3590int
3591i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3592{
3593 struct drm_device *dev = obj->dev;
3594 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3595 int ret;
3596
3597 i915_verify_inactive(dev, __FILE__, __LINE__);
3598 if (obj_priv->gtt_space == NULL) {
3599 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3600 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003601 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05003602 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003603 return ret;
3604 }
Chris Wilson22c344e2009-02-11 14:26:45 +00003605 }
3606 /*
3607 * Pre-965 chips need a fence register set up in order to
3608 * properly handle tiled surfaces.
3609 */
Eric Anholta09ba7f2009-08-29 12:49:51 -07003610 if (!IS_I965G(dev) && obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003611 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson22c344e2009-02-11 14:26:45 +00003612 if (ret != 0) {
3613 if (ret != -EBUSY && ret != -ERESTARTSYS)
3614 DRM_ERROR("Failure to install fence: %d\n",
3615 ret);
3616 return ret;
3617 }
Eric Anholt673a3942008-07-30 12:06:12 -07003618 }
3619 obj_priv->pin_count++;
3620
3621 /* If the object is not active and not pending a flush,
3622 * remove it from the inactive list
3623 */
3624 if (obj_priv->pin_count == 1) {
3625 atomic_inc(&dev->pin_count);
3626 atomic_add(obj->size, &dev->pin_memory);
3627 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003628 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07003629 !list_empty(&obj_priv->list))
3630 list_del_init(&obj_priv->list);
3631 }
3632 i915_verify_inactive(dev, __FILE__, __LINE__);
3633
3634 return 0;
3635}
3636
3637void
3638i915_gem_object_unpin(struct drm_gem_object *obj)
3639{
3640 struct drm_device *dev = obj->dev;
3641 drm_i915_private_t *dev_priv = dev->dev_private;
3642 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3643
3644 i915_verify_inactive(dev, __FILE__, __LINE__);
3645 obj_priv->pin_count--;
3646 BUG_ON(obj_priv->pin_count < 0);
3647 BUG_ON(obj_priv->gtt_space == NULL);
3648
3649 /* If the object is no longer pinned, and is
3650 * neither active nor being flushed, then stick it on
3651 * the inactive list
3652 */
3653 if (obj_priv->pin_count == 0) {
3654 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003655 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003656 list_move_tail(&obj_priv->list,
3657 &dev_priv->mm.inactive_list);
3658 atomic_dec(&dev->pin_count);
3659 atomic_sub(obj->size, &dev->pin_memory);
3660 }
3661 i915_verify_inactive(dev, __FILE__, __LINE__);
3662}
3663
3664int
3665i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3666 struct drm_file *file_priv)
3667{
3668 struct drm_i915_gem_pin *args = data;
3669 struct drm_gem_object *obj;
3670 struct drm_i915_gem_object *obj_priv;
3671 int ret;
3672
3673 mutex_lock(&dev->struct_mutex);
3674
3675 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3676 if (obj == NULL) {
3677 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3678 args->handle);
3679 mutex_unlock(&dev->struct_mutex);
3680 return -EBADF;
3681 }
3682 obj_priv = obj->driver_private;
3683
Jesse Barnes79e53942008-11-07 14:24:08 -08003684 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3685 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3686 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00003687 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003688 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003689 return -EINVAL;
3690 }
3691
3692 obj_priv->user_pin_count++;
3693 obj_priv->pin_filp = file_priv;
3694 if (obj_priv->user_pin_count == 1) {
3695 ret = i915_gem_object_pin(obj, args->alignment);
3696 if (ret != 0) {
3697 drm_gem_object_unreference(obj);
3698 mutex_unlock(&dev->struct_mutex);
3699 return ret;
3700 }
Eric Anholt673a3942008-07-30 12:06:12 -07003701 }
3702
3703 /* XXX - flush the CPU caches for pinned objects
3704 * as the X server doesn't manage domains yet
3705 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003706 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003707 args->offset = obj_priv->gtt_offset;
3708 drm_gem_object_unreference(obj);
3709 mutex_unlock(&dev->struct_mutex);
3710
3711 return 0;
3712}
3713
3714int
3715i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3716 struct drm_file *file_priv)
3717{
3718 struct drm_i915_gem_pin *args = data;
3719 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08003720 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003721
3722 mutex_lock(&dev->struct_mutex);
3723
3724 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3725 if (obj == NULL) {
3726 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3727 args->handle);
3728 mutex_unlock(&dev->struct_mutex);
3729 return -EBADF;
3730 }
3731
Jesse Barnes79e53942008-11-07 14:24:08 -08003732 obj_priv = obj->driver_private;
3733 if (obj_priv->pin_filp != file_priv) {
3734 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3735 args->handle);
3736 drm_gem_object_unreference(obj);
3737 mutex_unlock(&dev->struct_mutex);
3738 return -EINVAL;
3739 }
3740 obj_priv->user_pin_count--;
3741 if (obj_priv->user_pin_count == 0) {
3742 obj_priv->pin_filp = NULL;
3743 i915_gem_object_unpin(obj);
3744 }
Eric Anholt673a3942008-07-30 12:06:12 -07003745
3746 drm_gem_object_unreference(obj);
3747 mutex_unlock(&dev->struct_mutex);
3748 return 0;
3749}
3750
3751int
3752i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3753 struct drm_file *file_priv)
3754{
3755 struct drm_i915_gem_busy *args = data;
3756 struct drm_gem_object *obj;
3757 struct drm_i915_gem_object *obj_priv;
3758
Eric Anholt673a3942008-07-30 12:06:12 -07003759 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3760 if (obj == NULL) {
3761 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3762 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003763 return -EBADF;
3764 }
3765
Chris Wilsonb1ce7862009-06-06 09:46:00 +01003766 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08003767 /* Update the active list for the hardware's current position.
3768 * Otherwise this only updates on a delayed timer or when irqs are
3769 * actually unmasked, and our working set ends up being larger than
3770 * required.
3771 */
3772 i915_gem_retire_requests(dev);
3773
Eric Anholt673a3942008-07-30 12:06:12 -07003774 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08003775 /* Don't count being on the flushing list against the object being
3776 * done. Otherwise, a buffer left on the flushing list but not getting
3777 * flushed (because nobody's flushing that domain) won't ever return
3778 * unbusy and get reused by libdrm's bo cache. The other expected
3779 * consumer of this interface, OpenGL's occlusion queries, also specs
3780 * that the objects get unbusy "eventually" without any interference.
3781 */
3782 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003783
3784 drm_gem_object_unreference(obj);
3785 mutex_unlock(&dev->struct_mutex);
3786 return 0;
3787}
3788
3789int
3790i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3791 struct drm_file *file_priv)
3792{
3793 return i915_gem_ring_throttle(dev, file_priv);
3794}
3795
3796int i915_gem_init_object(struct drm_gem_object *obj)
3797{
3798 struct drm_i915_gem_object *obj_priv;
3799
Eric Anholt9a298b22009-03-24 12:23:04 -07003800 obj_priv = kzalloc(sizeof(*obj_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07003801 if (obj_priv == NULL)
3802 return -ENOMEM;
3803
3804 /*
3805 * We've just allocated pages from the kernel,
3806 * so they've just been written by the CPU with
3807 * zeros. They'll need to be clflushed before we
3808 * use them with the GPU.
3809 */
3810 obj->write_domain = I915_GEM_DOMAIN_CPU;
3811 obj->read_domains = I915_GEM_DOMAIN_CPU;
3812
Keith Packardba1eb1d2008-10-14 19:55:10 -07003813 obj_priv->agp_type = AGP_USER_MEMORY;
3814
Eric Anholt673a3942008-07-30 12:06:12 -07003815 obj->driver_private = obj_priv;
3816 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003817 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07003818 INIT_LIST_HEAD(&obj_priv->list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003819 INIT_LIST_HEAD(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003820
Eric Anholt673a3942008-07-30 12:06:12 -07003821 return 0;
3822}
3823
3824void i915_gem_free_object(struct drm_gem_object *obj)
3825{
Jesse Barnesde151cf2008-11-12 10:03:55 -08003826 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003827 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3828
3829 while (obj_priv->pin_count > 0)
3830 i915_gem_object_unpin(obj);
3831
Dave Airlie71acb5e2008-12-30 20:31:46 +10003832 if (obj_priv->phys_obj)
3833 i915_gem_detach_phys_object(dev, obj);
3834
Eric Anholt673a3942008-07-30 12:06:12 -07003835 i915_gem_object_unbind(obj);
3836
Chris Wilson7e616152009-09-10 08:53:04 +01003837 if (obj_priv->mmap_offset)
3838 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003839
Eric Anholt9a298b22009-03-24 12:23:04 -07003840 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07003841 kfree(obj_priv->bit_17);
Eric Anholt9a298b22009-03-24 12:23:04 -07003842 kfree(obj->driver_private);
Eric Anholt673a3942008-07-30 12:06:12 -07003843}
3844
Eric Anholt673a3942008-07-30 12:06:12 -07003845/** Unbinds all objects that are on the given buffer list. */
3846static int
3847i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3848{
3849 struct drm_gem_object *obj;
3850 struct drm_i915_gem_object *obj_priv;
3851 int ret;
3852
3853 while (!list_empty(head)) {
3854 obj_priv = list_first_entry(head,
3855 struct drm_i915_gem_object,
3856 list);
3857 obj = obj_priv->obj;
3858
3859 if (obj_priv->pin_count != 0) {
3860 DRM_ERROR("Pinned object in unbind list\n");
3861 mutex_unlock(&dev->struct_mutex);
3862 return -EINVAL;
3863 }
3864
3865 ret = i915_gem_object_unbind(obj);
3866 if (ret != 0) {
3867 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3868 ret);
3869 mutex_unlock(&dev->struct_mutex);
3870 return ret;
3871 }
3872 }
3873
3874
3875 return 0;
3876}
3877
Jesse Barnes5669fca2009-02-17 15:13:31 -08003878int
Eric Anholt673a3942008-07-30 12:06:12 -07003879i915_gem_idle(struct drm_device *dev)
3880{
3881 drm_i915_private_t *dev_priv = dev->dev_private;
3882 uint32_t seqno, cur_seqno, last_seqno;
3883 int stuck, ret;
3884
Keith Packard6dbe2772008-10-14 21:41:13 -07003885 mutex_lock(&dev->struct_mutex);
3886
3887 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3888 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003889 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003890 }
Eric Anholt673a3942008-07-30 12:06:12 -07003891
3892 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3893 * We need to replace this with a semaphore, or something.
3894 */
3895 dev_priv->mm.suspended = 1;
3896
Keith Packard6dbe2772008-10-14 21:41:13 -07003897 /* Cancel the retire work handler, wait for it to finish if running
3898 */
3899 mutex_unlock(&dev->struct_mutex);
3900 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3901 mutex_lock(&dev->struct_mutex);
3902
Eric Anholt673a3942008-07-30 12:06:12 -07003903 i915_kernel_lost_context(dev);
3904
3905 /* Flush the GPU along with all non-CPU write domains
3906 */
Chris Wilson21d509e2009-06-06 09:46:02 +01003907 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
3908 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07003909
3910 if (seqno == 0) {
3911 mutex_unlock(&dev->struct_mutex);
3912 return -ENOMEM;
3913 }
3914
3915 dev_priv->mm.waiting_gem_seqno = seqno;
3916 last_seqno = 0;
3917 stuck = 0;
3918 for (;;) {
3919 cur_seqno = i915_get_gem_seqno(dev);
3920 if (i915_seqno_passed(cur_seqno, seqno))
3921 break;
3922 if (last_seqno == cur_seqno) {
3923 if (stuck++ > 100) {
3924 DRM_ERROR("hardware wedged\n");
3925 dev_priv->mm.wedged = 1;
3926 DRM_WAKEUP(&dev_priv->irq_queue);
3927 break;
3928 }
3929 }
3930 msleep(10);
3931 last_seqno = cur_seqno;
3932 }
3933 dev_priv->mm.waiting_gem_seqno = 0;
3934
3935 i915_gem_retire_requests(dev);
3936
Carl Worth5e118f42009-03-20 11:54:25 -07003937 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003938 if (!dev_priv->mm.wedged) {
3939 /* Active and flushing should now be empty as we've
3940 * waited for a sequence higher than any pending execbuffer
3941 */
3942 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3943 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3944 /* Request should now be empty as we've also waited
3945 * for the last request in the list
3946 */
3947 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3948 }
Eric Anholt673a3942008-07-30 12:06:12 -07003949
Eric Anholt28dfe522008-11-13 15:00:55 -08003950 /* Empty the active and flushing lists to inactive. If there's
3951 * anything left at this point, it means that we're wedged and
3952 * nothing good's going to happen by leaving them there. So strip
3953 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003954 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003955 while (!list_empty(&dev_priv->mm.active_list)) {
3956 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003957
Eric Anholt28dfe522008-11-13 15:00:55 -08003958 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3959 struct drm_i915_gem_object,
3960 list);
3961 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3962 i915_gem_object_move_to_inactive(obj_priv->obj);
3963 }
Carl Worth5e118f42009-03-20 11:54:25 -07003964 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003965
3966 while (!list_empty(&dev_priv->mm.flushing_list)) {
3967 struct drm_i915_gem_object *obj_priv;
3968
Eric Anholt151903d2008-12-01 10:23:21 +10003969 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003970 struct drm_i915_gem_object,
3971 list);
3972 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3973 i915_gem_object_move_to_inactive(obj_priv->obj);
3974 }
3975
3976
3977 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003978 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003979 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003980 if (ret) {
3981 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003982 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003983 }
Eric Anholt673a3942008-07-30 12:06:12 -07003984
Keith Packard6dbe2772008-10-14 21:41:13 -07003985 i915_gem_cleanup_ringbuffer(dev);
3986 mutex_unlock(&dev->struct_mutex);
3987
Eric Anholt673a3942008-07-30 12:06:12 -07003988 return 0;
3989}
3990
3991static int
3992i915_gem_init_hws(struct drm_device *dev)
3993{
3994 drm_i915_private_t *dev_priv = dev->dev_private;
3995 struct drm_gem_object *obj;
3996 struct drm_i915_gem_object *obj_priv;
3997 int ret;
3998
3999 /* If we need a physical address for the status page, it's already
4000 * initialized at driver load time.
4001 */
4002 if (!I915_NEED_GFX_HWS(dev))
4003 return 0;
4004
4005 obj = drm_gem_object_alloc(dev, 4096);
4006 if (obj == NULL) {
4007 DRM_ERROR("Failed to allocate status page\n");
4008 return -ENOMEM;
4009 }
4010 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07004011 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07004012
4013 ret = i915_gem_object_pin(obj, 4096);
4014 if (ret != 0) {
4015 drm_gem_object_unreference(obj);
4016 return ret;
4017 }
4018
4019 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07004020
Eric Anholt856fa192009-03-19 14:10:50 -07004021 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004022 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004023 DRM_ERROR("Failed to map status page.\n");
4024 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00004025 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004026 drm_gem_object_unreference(obj);
4027 return -EINVAL;
4028 }
4029 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004030 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
4031 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004032 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07004033 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
4034
4035 return 0;
4036}
4037
Chris Wilson85a7bb92009-02-11 14:52:44 +00004038static void
4039i915_gem_cleanup_hws(struct drm_device *dev)
4040{
4041 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004042 struct drm_gem_object *obj;
4043 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00004044
4045 if (dev_priv->hws_obj == NULL)
4046 return;
4047
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004048 obj = dev_priv->hws_obj;
4049 obj_priv = obj->driver_private;
4050
Eric Anholt856fa192009-03-19 14:10:50 -07004051 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004052 i915_gem_object_unpin(obj);
4053 drm_gem_object_unreference(obj);
4054 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004055
Chris Wilson85a7bb92009-02-11 14:52:44 +00004056 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4057 dev_priv->hw_status_page = NULL;
4058
4059 /* Write high address into HWS_PGA when disabling. */
4060 I915_WRITE(HWS_PGA, 0x1ffff000);
4061}
4062
Jesse Barnes79e53942008-11-07 14:24:08 -08004063int
Eric Anholt673a3942008-07-30 12:06:12 -07004064i915_gem_init_ringbuffer(struct drm_device *dev)
4065{
4066 drm_i915_private_t *dev_priv = dev->dev_private;
4067 struct drm_gem_object *obj;
4068 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08004069 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07004070 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07004071 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07004072
4073 ret = i915_gem_init_hws(dev);
4074 if (ret != 0)
4075 return ret;
4076
4077 obj = drm_gem_object_alloc(dev, 128 * 1024);
4078 if (obj == NULL) {
4079 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004080 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004081 return -ENOMEM;
4082 }
4083 obj_priv = obj->driver_private;
4084
4085 ret = i915_gem_object_pin(obj, 4096);
4086 if (ret != 0) {
4087 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004088 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004089 return ret;
4090 }
4091
4092 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004093 ring->Size = obj->size;
Eric Anholt673a3942008-07-30 12:06:12 -07004094
Jesse Barnes79e53942008-11-07 14:24:08 -08004095 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4096 ring->map.size = obj->size;
4097 ring->map.type = 0;
4098 ring->map.flags = 0;
4099 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004100
Jesse Barnes79e53942008-11-07 14:24:08 -08004101 drm_core_ioremap_wc(&ring->map, dev);
4102 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004103 DRM_ERROR("Failed to map ringbuffer.\n");
4104 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004105 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004106 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004107 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004108 return -EINVAL;
4109 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004110 ring->ring_obj = obj;
4111 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004112
4113 /* Stop the ring if it's running. */
4114 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004115 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07004116 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004117
4118 /* Initialize the ring. */
4119 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07004120 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4121
4122 /* G45 ring initialization fails to reset head to zero */
4123 if (head != 0) {
4124 DRM_ERROR("Ring head not reset to zero "
4125 "ctl %08x head %08x tail %08x start %08x\n",
4126 I915_READ(PRB0_CTL),
4127 I915_READ(PRB0_HEAD),
4128 I915_READ(PRB0_TAIL),
4129 I915_READ(PRB0_START));
4130 I915_WRITE(PRB0_HEAD, 0);
4131
4132 DRM_ERROR("Ring head forced to zero "
4133 "ctl %08x head %08x tail %08x start %08x\n",
4134 I915_READ(PRB0_CTL),
4135 I915_READ(PRB0_HEAD),
4136 I915_READ(PRB0_TAIL),
4137 I915_READ(PRB0_START));
4138 }
4139
Eric Anholt673a3942008-07-30 12:06:12 -07004140 I915_WRITE(PRB0_CTL,
4141 ((obj->size - 4096) & RING_NR_PAGES) |
4142 RING_NO_REPORT |
4143 RING_VALID);
4144
Keith Packard50aa253d2008-10-14 17:20:35 -07004145 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4146
4147 /* If the head is still not zero, the ring is dead */
4148 if (head != 0) {
4149 DRM_ERROR("Ring initialization failed "
4150 "ctl %08x head %08x tail %08x start %08x\n",
4151 I915_READ(PRB0_CTL),
4152 I915_READ(PRB0_HEAD),
4153 I915_READ(PRB0_TAIL),
4154 I915_READ(PRB0_START));
4155 return -EIO;
4156 }
4157
Eric Anholt673a3942008-07-30 12:06:12 -07004158 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004159 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4160 i915_kernel_lost_context(dev);
4161 else {
4162 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4163 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4164 ring->space = ring->head - (ring->tail + 8);
4165 if (ring->space < 0)
4166 ring->space += ring->Size;
4167 }
Eric Anholt673a3942008-07-30 12:06:12 -07004168
4169 return 0;
4170}
4171
Jesse Barnes79e53942008-11-07 14:24:08 -08004172void
Eric Anholt673a3942008-07-30 12:06:12 -07004173i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4174{
4175 drm_i915_private_t *dev_priv = dev->dev_private;
4176
4177 if (dev_priv->ring.ring_obj == NULL)
4178 return;
4179
4180 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4181
4182 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4183 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4184 dev_priv->ring.ring_obj = NULL;
4185 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4186
Chris Wilson85a7bb92009-02-11 14:52:44 +00004187 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004188}
4189
4190int
4191i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4192 struct drm_file *file_priv)
4193{
4194 drm_i915_private_t *dev_priv = dev->dev_private;
4195 int ret;
4196
Jesse Barnes79e53942008-11-07 14:24:08 -08004197 if (drm_core_check_feature(dev, DRIVER_MODESET))
4198 return 0;
4199
Eric Anholt673a3942008-07-30 12:06:12 -07004200 if (dev_priv->mm.wedged) {
4201 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4202 dev_priv->mm.wedged = 0;
4203 }
4204
Eric Anholt673a3942008-07-30 12:06:12 -07004205 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004206 dev_priv->mm.suspended = 0;
4207
4208 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004209 if (ret != 0) {
4210 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004211 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004212 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004213
Carl Worth5e118f42009-03-20 11:54:25 -07004214 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004215 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004216 spin_unlock(&dev_priv->mm.active_list_lock);
4217
Eric Anholt673a3942008-07-30 12:06:12 -07004218 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4219 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4220 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004221 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004222
4223 drm_irq_install(dev);
4224
Eric Anholt673a3942008-07-30 12:06:12 -07004225 return 0;
4226}
4227
4228int
4229i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4230 struct drm_file *file_priv)
4231{
4232 int ret;
4233
Jesse Barnes79e53942008-11-07 14:24:08 -08004234 if (drm_core_check_feature(dev, DRIVER_MODESET))
4235 return 0;
4236
Eric Anholt673a3942008-07-30 12:06:12 -07004237 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004238 drm_irq_uninstall(dev);
4239
Keith Packard6dbe2772008-10-14 21:41:13 -07004240 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004241}
4242
4243void
4244i915_gem_lastclose(struct drm_device *dev)
4245{
4246 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004247
Eric Anholte806b492009-01-22 09:56:58 -08004248 if (drm_core_check_feature(dev, DRIVER_MODESET))
4249 return;
4250
Keith Packard6dbe2772008-10-14 21:41:13 -07004251 ret = i915_gem_idle(dev);
4252 if (ret)
4253 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004254}
4255
4256void
4257i915_gem_load(struct drm_device *dev)
4258{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004259 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004260 drm_i915_private_t *dev_priv = dev->dev_private;
4261
Carl Worth5e118f42009-03-20 11:54:25 -07004262 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004263 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4264 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4265 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4266 INIT_LIST_HEAD(&dev_priv->mm.request_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004267 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004268 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4269 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004270 dev_priv->mm.next_gem_seqno = 1;
4271
Jesse Barnesde151cf2008-11-12 10:03:55 -08004272 /* Old X drivers will take 0-2 for front, back, depth buffers */
4273 dev_priv->fence_reg_start = 3;
4274
Jesse Barnes0f973f22009-01-26 17:10:45 -08004275 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004276 dev_priv->num_fence_regs = 16;
4277 else
4278 dev_priv->num_fence_regs = 8;
4279
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004280 /* Initialize fence registers to zero */
4281 if (IS_I965G(dev)) {
4282 for (i = 0; i < 16; i++)
4283 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4284 } else {
4285 for (i = 0; i < 8; i++)
4286 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4287 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4288 for (i = 0; i < 8; i++)
4289 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4290 }
4291
Eric Anholt673a3942008-07-30 12:06:12 -07004292 i915_gem_detect_bit_6_swizzle(dev);
4293}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004294
4295/*
4296 * Create a physically contiguous memory object for this object
4297 * e.g. for cursor + overlay regs
4298 */
4299int i915_gem_init_phys_object(struct drm_device *dev,
4300 int id, int size)
4301{
4302 drm_i915_private_t *dev_priv = dev->dev_private;
4303 struct drm_i915_gem_phys_object *phys_obj;
4304 int ret;
4305
4306 if (dev_priv->mm.phys_objs[id - 1] || !size)
4307 return 0;
4308
Eric Anholt9a298b22009-03-24 12:23:04 -07004309 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004310 if (!phys_obj)
4311 return -ENOMEM;
4312
4313 phys_obj->id = id;
4314
4315 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4316 if (!phys_obj->handle) {
4317 ret = -ENOMEM;
4318 goto kfree_obj;
4319 }
4320#ifdef CONFIG_X86
4321 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4322#endif
4323
4324 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4325
4326 return 0;
4327kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004328 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004329 return ret;
4330}
4331
4332void i915_gem_free_phys_object(struct drm_device *dev, int id)
4333{
4334 drm_i915_private_t *dev_priv = dev->dev_private;
4335 struct drm_i915_gem_phys_object *phys_obj;
4336
4337 if (!dev_priv->mm.phys_objs[id - 1])
4338 return;
4339
4340 phys_obj = dev_priv->mm.phys_objs[id - 1];
4341 if (phys_obj->cur_obj) {
4342 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4343 }
4344
4345#ifdef CONFIG_X86
4346 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4347#endif
4348 drm_pci_free(dev, phys_obj->handle);
4349 kfree(phys_obj);
4350 dev_priv->mm.phys_objs[id - 1] = NULL;
4351}
4352
4353void i915_gem_free_all_phys_object(struct drm_device *dev)
4354{
4355 int i;
4356
Dave Airlie260883c2009-01-22 17:58:49 +10004357 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004358 i915_gem_free_phys_object(dev, i);
4359}
4360
4361void i915_gem_detach_phys_object(struct drm_device *dev,
4362 struct drm_gem_object *obj)
4363{
4364 struct drm_i915_gem_object *obj_priv;
4365 int i;
4366 int ret;
4367 int page_count;
4368
4369 obj_priv = obj->driver_private;
4370 if (!obj_priv->phys_obj)
4371 return;
4372
Eric Anholt856fa192009-03-19 14:10:50 -07004373 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004374 if (ret)
4375 goto out;
4376
4377 page_count = obj->size / PAGE_SIZE;
4378
4379 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004380 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004381 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4382
4383 memcpy(dst, src, PAGE_SIZE);
4384 kunmap_atomic(dst, KM_USER0);
4385 }
Eric Anholt856fa192009-03-19 14:10:50 -07004386 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004387 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004388
4389 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004390out:
4391 obj_priv->phys_obj->cur_obj = NULL;
4392 obj_priv->phys_obj = NULL;
4393}
4394
4395int
4396i915_gem_attach_phys_object(struct drm_device *dev,
4397 struct drm_gem_object *obj, int id)
4398{
4399 drm_i915_private_t *dev_priv = dev->dev_private;
4400 struct drm_i915_gem_object *obj_priv;
4401 int ret = 0;
4402 int page_count;
4403 int i;
4404
4405 if (id > I915_MAX_PHYS_OBJECT)
4406 return -EINVAL;
4407
4408 obj_priv = obj->driver_private;
4409
4410 if (obj_priv->phys_obj) {
4411 if (obj_priv->phys_obj->id == id)
4412 return 0;
4413 i915_gem_detach_phys_object(dev, obj);
4414 }
4415
4416
4417 /* create a new object */
4418 if (!dev_priv->mm.phys_objs[id - 1]) {
4419 ret = i915_gem_init_phys_object(dev, id,
4420 obj->size);
4421 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004422 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004423 goto out;
4424 }
4425 }
4426
4427 /* bind to the object */
4428 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4429 obj_priv->phys_obj->cur_obj = obj;
4430
Eric Anholt856fa192009-03-19 14:10:50 -07004431 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004432 if (ret) {
4433 DRM_ERROR("failed to get page list\n");
4434 goto out;
4435 }
4436
4437 page_count = obj->size / PAGE_SIZE;
4438
4439 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004440 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004441 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4442
4443 memcpy(dst, src, PAGE_SIZE);
4444 kunmap_atomic(src, KM_USER0);
4445 }
4446
Chris Wilsond78b47b2009-06-17 21:52:49 +01004447 i915_gem_object_put_pages(obj);
4448
Dave Airlie71acb5e2008-12-30 20:31:46 +10004449 return 0;
4450out:
4451 return ret;
4452}
4453
4454static int
4455i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4456 struct drm_i915_gem_pwrite *args,
4457 struct drm_file *file_priv)
4458{
4459 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4460 void *obj_addr;
4461 int ret;
4462 char __user *user_data;
4463
4464 user_data = (char __user *) (uintptr_t) args->data_ptr;
4465 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4466
Dave Airliee08fb4f2009-02-25 14:52:30 +10004467 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004468 ret = copy_from_user(obj_addr, user_data, args->size);
4469 if (ret)
4470 return -EFAULT;
4471
4472 drm_agp_chipset_flush(dev);
4473 return 0;
4474}
Eric Anholtb9624422009-06-03 07:27:35 +00004475
4476void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4477{
4478 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4479
4480 /* Clean up our request list when the client is going away, so that
4481 * later retire_requests won't dereference our soon-to-be-gone
4482 * file_priv.
4483 */
4484 mutex_lock(&dev->struct_mutex);
4485 while (!list_empty(&i915_file_priv->mm.request_list))
4486 list_del_init(i915_file_priv->mm.request_list.next);
4487 mutex_unlock(&dev->struct_mutex);
4488}