blob: 91005132110460e19f63b8a54104cbf095b92f4c [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
Ben Gamariffed1d02009-09-14 17:48:41 -04001754 if (dev_priv->mm.wedged)
1755 return -EIO;
1756
Eric Anholt673a3942008-07-30 12:06:12 -07001757 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001758 if (IS_IGDNG(dev))
1759 ier = I915_READ(DEIER) | I915_READ(GTIER);
1760 else
1761 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001762 if (!ier) {
1763 DRM_ERROR("something (likely vbetool) disabled "
1764 "interrupts, re-enabling\n");
1765 i915_driver_irq_preinstall(dev);
1766 i915_driver_irq_postinstall(dev);
1767 }
1768
Eric Anholt673a3942008-07-30 12:06:12 -07001769 dev_priv->mm.waiting_gem_seqno = seqno;
1770 i915_user_irq_get(dev);
1771 ret = wait_event_interruptible(dev_priv->irq_queue,
1772 i915_seqno_passed(i915_get_gem_seqno(dev),
1773 seqno) ||
1774 dev_priv->mm.wedged);
1775 i915_user_irq_put(dev);
1776 dev_priv->mm.waiting_gem_seqno = 0;
1777 }
1778 if (dev_priv->mm.wedged)
1779 ret = -EIO;
1780
1781 if (ret && ret != -ERESTARTSYS)
1782 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1783 __func__, ret, seqno, i915_get_gem_seqno(dev));
1784
1785 /* Directly dispatch request retiring. While we have the work queue
1786 * to handle this, the waiter on a request often wants an associated
1787 * buffer to have made it to the inactive list, and we would need
1788 * a separate wait queue to handle that.
1789 */
1790 if (ret == 0)
1791 i915_gem_retire_requests(dev);
1792
1793 return ret;
1794}
1795
1796static void
1797i915_gem_flush(struct drm_device *dev,
1798 uint32_t invalidate_domains,
1799 uint32_t flush_domains)
1800{
1801 drm_i915_private_t *dev_priv = dev->dev_private;
1802 uint32_t cmd;
1803 RING_LOCALS;
1804
1805#if WATCH_EXEC
1806 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1807 invalidate_domains, flush_domains);
1808#endif
1809
1810 if (flush_domains & I915_GEM_DOMAIN_CPU)
1811 drm_agp_chipset_flush(dev);
1812
Chris Wilson21d509e2009-06-06 09:46:02 +01001813 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
Eric Anholt673a3942008-07-30 12:06:12 -07001814 /*
1815 * read/write caches:
1816 *
1817 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1818 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1819 * also flushed at 2d versus 3d pipeline switches.
1820 *
1821 * read-only caches:
1822 *
1823 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1824 * MI_READ_FLUSH is set, and is always flushed on 965.
1825 *
1826 * I915_GEM_DOMAIN_COMMAND may not exist?
1827 *
1828 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1829 * invalidated when MI_EXE_FLUSH is set.
1830 *
1831 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1832 * invalidated with every MI_FLUSH.
1833 *
1834 * TLBs:
1835 *
1836 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1837 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1838 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1839 * are flushed at any MI_FLUSH.
1840 */
1841
1842 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1843 if ((invalidate_domains|flush_domains) &
1844 I915_GEM_DOMAIN_RENDER)
1845 cmd &= ~MI_NO_WRITE_FLUSH;
1846 if (!IS_I965G(dev)) {
1847 /*
1848 * On the 965, the sampler cache always gets flushed
1849 * and this bit is reserved.
1850 */
1851 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1852 cmd |= MI_READ_FLUSH;
1853 }
1854 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1855 cmd |= MI_EXE_FLUSH;
1856
1857#if WATCH_EXEC
1858 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1859#endif
1860 BEGIN_LP_RING(2);
1861 OUT_RING(cmd);
1862 OUT_RING(0); /* noop */
1863 ADVANCE_LP_RING();
1864 }
1865}
1866
1867/**
1868 * Ensures that all rendering to the object has completed and the object is
1869 * safe to unbind from the GTT or access from the CPU.
1870 */
1871static int
1872i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1873{
1874 struct drm_device *dev = obj->dev;
1875 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1876 int ret;
1877
Eric Anholte47c68e2008-11-14 13:35:19 -08001878 /* This function only exists to support waiting for existing rendering,
1879 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001880 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001881 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001882
1883 /* If there is rendering queued on the buffer being evicted, wait for
1884 * it.
1885 */
1886 if (obj_priv->active) {
1887#if WATCH_BUF
1888 DRM_INFO("%s: object %p wait for seqno %08x\n",
1889 __func__, obj, obj_priv->last_rendering_seqno);
1890#endif
1891 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1892 if (ret != 0)
1893 return ret;
1894 }
1895
1896 return 0;
1897}
1898
1899/**
1900 * Unbinds an object from the GTT aperture.
1901 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001902int
Eric Anholt673a3942008-07-30 12:06:12 -07001903i915_gem_object_unbind(struct drm_gem_object *obj)
1904{
1905 struct drm_device *dev = obj->dev;
1906 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1907 int ret = 0;
1908
1909#if WATCH_BUF
1910 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1911 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1912#endif
1913 if (obj_priv->gtt_space == NULL)
1914 return 0;
1915
1916 if (obj_priv->pin_count != 0) {
1917 DRM_ERROR("Attempting to unbind pinned buffer\n");
1918 return -EINVAL;
1919 }
1920
Eric Anholt5323fd02009-09-09 11:50:45 -07001921 /* blow away mappings if mapped through GTT */
1922 i915_gem_release_mmap(obj);
1923
1924 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1925 i915_gem_clear_fence_reg(obj);
1926
Eric Anholt673a3942008-07-30 12:06:12 -07001927 /* Move the object to the CPU domain to ensure that
1928 * any possible CPU writes while it's not in the GTT
1929 * are flushed when we go to remap it. This will
1930 * also ensure that all pending GPU writes are finished
1931 * before we unbind.
1932 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001933 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001934 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001935 if (ret != -ERESTARTSYS)
1936 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001937 return ret;
1938 }
1939
Eric Anholt5323fd02009-09-09 11:50:45 -07001940 BUG_ON(obj_priv->active);
1941
Eric Anholt673a3942008-07-30 12:06:12 -07001942 if (obj_priv->agp_mem != NULL) {
1943 drm_unbind_agp(obj_priv->agp_mem);
1944 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1945 obj_priv->agp_mem = NULL;
1946 }
1947
Eric Anholt856fa192009-03-19 14:10:50 -07001948 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001949
1950 if (obj_priv->gtt_space) {
1951 atomic_dec(&dev->gtt_count);
1952 atomic_sub(obj->size, &dev->gtt_memory);
1953
1954 drm_mm_put_block(obj_priv->gtt_space);
1955 obj_priv->gtt_space = NULL;
1956 }
1957
1958 /* Remove ourselves from the LRU list if present. */
1959 if (!list_empty(&obj_priv->list))
1960 list_del_init(&obj_priv->list);
1961
1962 return 0;
1963}
1964
1965static int
1966i915_gem_evict_something(struct drm_device *dev)
1967{
1968 drm_i915_private_t *dev_priv = dev->dev_private;
1969 struct drm_gem_object *obj;
1970 struct drm_i915_gem_object *obj_priv;
1971 int ret = 0;
1972
1973 for (;;) {
1974 /* If there's an inactive buffer available now, grab it
1975 * and be done.
1976 */
1977 if (!list_empty(&dev_priv->mm.inactive_list)) {
1978 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1979 struct drm_i915_gem_object,
1980 list);
1981 obj = obj_priv->obj;
1982 BUG_ON(obj_priv->pin_count != 0);
1983#if WATCH_LRU
1984 DRM_INFO("%s: evicting %p\n", __func__, obj);
1985#endif
1986 BUG_ON(obj_priv->active);
1987
1988 /* Wait on the rendering and unbind the buffer. */
1989 ret = i915_gem_object_unbind(obj);
1990 break;
1991 }
1992
1993 /* If we didn't get anything, but the ring is still processing
1994 * things, wait for one of those things to finish and hopefully
1995 * leave us a buffer to evict.
1996 */
1997 if (!list_empty(&dev_priv->mm.request_list)) {
1998 struct drm_i915_gem_request *request;
1999
2000 request = list_first_entry(&dev_priv->mm.request_list,
2001 struct drm_i915_gem_request,
2002 list);
2003
2004 ret = i915_wait_request(dev, request->seqno);
2005 if (ret)
2006 break;
2007
2008 /* if waiting caused an object to become inactive,
2009 * then loop around and wait for it. Otherwise, we
2010 * assume that waiting freed and unbound something,
2011 * so there should now be some space in the GTT
2012 */
2013 if (!list_empty(&dev_priv->mm.inactive_list))
2014 continue;
2015 break;
2016 }
2017
2018 /* If we didn't have anything on the request list but there
2019 * are buffers awaiting a flush, emit one and try again.
2020 * When we wait on it, those buffers waiting for that flush
2021 * will get moved to inactive.
2022 */
2023 if (!list_empty(&dev_priv->mm.flushing_list)) {
2024 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
2025 struct drm_i915_gem_object,
2026 list);
2027 obj = obj_priv->obj;
2028
2029 i915_gem_flush(dev,
2030 obj->write_domain,
2031 obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002032 i915_add_request(dev, NULL, obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002033
2034 obj = NULL;
2035 continue;
2036 }
2037
2038 DRM_ERROR("inactive empty %d request empty %d "
2039 "flushing empty %d\n",
2040 list_empty(&dev_priv->mm.inactive_list),
2041 list_empty(&dev_priv->mm.request_list),
2042 list_empty(&dev_priv->mm.flushing_list));
2043 /* If we didn't do any of the above, there's nothing to be done
2044 * and we just can't fit it in.
2045 */
Chris Wilson2939e1f2009-06-06 09:46:03 +01002046 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002047 }
2048 return ret;
2049}
2050
2051static int
Keith Packardac94a962008-11-20 23:30:27 -08002052i915_gem_evict_everything(struct drm_device *dev)
2053{
2054 int ret;
2055
2056 for (;;) {
2057 ret = i915_gem_evict_something(dev);
2058 if (ret != 0)
2059 break;
2060 }
Chris Wilson2939e1f2009-06-06 09:46:03 +01002061 if (ret == -ENOSPC)
Owain Ainsworth15c35332008-12-06 20:42:20 -08002062 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08002063 return ret;
2064}
2065
Ben Gamari6911a9b2009-04-02 11:24:54 -07002066int
Eric Anholt856fa192009-03-19 14:10:50 -07002067i915_gem_object_get_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002068{
2069 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2070 int page_count, i;
2071 struct address_space *mapping;
2072 struct inode *inode;
2073 struct page *page;
2074 int ret;
2075
Eric Anholt856fa192009-03-19 14:10:50 -07002076 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002077 return 0;
2078
2079 /* Get the list of pages out of our struct file. They'll be pinned
2080 * at this point until we release them.
2081 */
2082 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002083 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002084 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002085 if (obj_priv->pages == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002086 DRM_ERROR("Faled to allocate page list\n");
Eric Anholt856fa192009-03-19 14:10:50 -07002087 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002088 return -ENOMEM;
2089 }
2090
2091 inode = obj->filp->f_path.dentry->d_inode;
2092 mapping = inode->i_mapping;
2093 for (i = 0; i < page_count; i++) {
2094 page = read_mapping_page(mapping, i, NULL);
2095 if (IS_ERR(page)) {
2096 ret = PTR_ERR(page);
2097 DRM_ERROR("read_mapping_page failed: %d\n", ret);
Eric Anholt856fa192009-03-19 14:10:50 -07002098 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002099 return ret;
2100 }
Eric Anholt856fa192009-03-19 14:10:50 -07002101 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002102 }
Eric Anholt280b7132009-03-12 16:56:27 -07002103
2104 if (obj_priv->tiling_mode != I915_TILING_NONE)
2105 i915_gem_object_do_bit_17_swizzle(obj);
2106
Eric Anholt673a3942008-07-30 12:06:12 -07002107 return 0;
2108}
2109
Jesse Barnesde151cf2008-11-12 10:03:55 -08002110static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2111{
2112 struct drm_gem_object *obj = reg->obj;
2113 struct drm_device *dev = obj->dev;
2114 drm_i915_private_t *dev_priv = dev->dev_private;
2115 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2116 int regnum = obj_priv->fence_reg;
2117 uint64_t val;
2118
2119 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2120 0xfffff000) << 32;
2121 val |= obj_priv->gtt_offset & 0xfffff000;
2122 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2123 if (obj_priv->tiling_mode == I915_TILING_Y)
2124 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2125 val |= I965_FENCE_REG_VALID;
2126
2127 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2128}
2129
2130static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2131{
2132 struct drm_gem_object *obj = reg->obj;
2133 struct drm_device *dev = obj->dev;
2134 drm_i915_private_t *dev_priv = dev->dev_private;
2135 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2136 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002137 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002138 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002139 uint32_t pitch_val;
2140
2141 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2142 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002143 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002144 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002145 return;
2146 }
2147
Jesse Barnes0f973f22009-01-26 17:10:45 -08002148 if (obj_priv->tiling_mode == I915_TILING_Y &&
2149 HAS_128_BYTE_Y_TILING(dev))
2150 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002151 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002152 tile_width = 512;
2153
2154 /* Note: pitch better be a power of two tile widths */
2155 pitch_val = obj_priv->stride / tile_width;
2156 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002157
2158 val = obj_priv->gtt_offset;
2159 if (obj_priv->tiling_mode == I915_TILING_Y)
2160 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2161 val |= I915_FENCE_SIZE_BITS(obj->size);
2162 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2163 val |= I830_FENCE_REG_VALID;
2164
Eric Anholtdc529a42009-03-10 22:34:49 -07002165 if (regnum < 8)
2166 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2167 else
2168 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2169 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002170}
2171
2172static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2173{
2174 struct drm_gem_object *obj = reg->obj;
2175 struct drm_device *dev = obj->dev;
2176 drm_i915_private_t *dev_priv = dev->dev_private;
2177 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2178 int regnum = obj_priv->fence_reg;
2179 uint32_t val;
2180 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002181 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002182
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002183 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002184 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002185 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002186 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002187 return;
2188 }
2189
Eric Anholte76a16d2009-05-26 17:44:56 -07002190 pitch_val = obj_priv->stride / 128;
2191 pitch_val = ffs(pitch_val) - 1;
2192 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2193
Jesse Barnesde151cf2008-11-12 10:03:55 -08002194 val = obj_priv->gtt_offset;
2195 if (obj_priv->tiling_mode == I915_TILING_Y)
2196 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002197 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2198 WARN_ON(fence_size_bits & ~0x00000f00);
2199 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002200 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2201 val |= I830_FENCE_REG_VALID;
2202
2203 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002204}
2205
2206/**
2207 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2208 * @obj: object to map through a fence reg
2209 *
2210 * When mapping objects through the GTT, userspace wants to be able to write
2211 * to them without having to worry about swizzling if the object is tiled.
2212 *
2213 * This function walks the fence regs looking for a free one for @obj,
2214 * stealing one if it can't find any.
2215 *
2216 * It then sets up the reg based on the object's properties: address, pitch
2217 * and tiling format.
2218 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002219int
2220i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002221{
2222 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002223 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002224 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2225 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002226 struct drm_i915_gem_object *old_obj_priv = NULL;
2227 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002228
Eric Anholta09ba7f2009-08-29 12:49:51 -07002229 /* Just update our place in the LRU if our fence is getting used. */
2230 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
2231 list_move_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2232 return 0;
2233 }
2234
Jesse Barnesde151cf2008-11-12 10:03:55 -08002235 switch (obj_priv->tiling_mode) {
2236 case I915_TILING_NONE:
2237 WARN(1, "allocating a fence for non-tiled object?\n");
2238 break;
2239 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002240 if (!obj_priv->stride)
2241 return -EINVAL;
2242 WARN((obj_priv->stride & (512 - 1)),
2243 "object 0x%08x is X tiled but has non-512B pitch\n",
2244 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002245 break;
2246 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002247 if (!obj_priv->stride)
2248 return -EINVAL;
2249 WARN((obj_priv->stride & (128 - 1)),
2250 "object 0x%08x is Y tiled but has non-128B pitch\n",
2251 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002252 break;
2253 }
2254
2255 /* First try to find a free reg */
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002256 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002257 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2258 reg = &dev_priv->fence_regs[i];
2259 if (!reg->obj)
2260 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002261
2262 old_obj_priv = reg->obj->driver_private;
2263 if (!old_obj_priv->pin_count)
2264 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002265 }
2266
2267 /* None available, try to steal one or wait for a user to finish */
2268 if (i == dev_priv->num_fence_regs) {
Eric Anholta09ba7f2009-08-29 12:49:51 -07002269 struct drm_gem_object *old_obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002270
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002271 if (avail == 0)
Chris Wilson2939e1f2009-06-06 09:46:03 +01002272 return -ENOSPC;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002273
Eric Anholta09ba7f2009-08-29 12:49:51 -07002274 list_for_each_entry(old_obj_priv, &dev_priv->mm.fence_list,
2275 fence_list) {
2276 old_obj = old_obj_priv->obj;
Chris Wilsond7619c42009-02-11 14:26:47 +00002277
2278 if (old_obj_priv->pin_count)
2279 continue;
2280
Eric Anholta09ba7f2009-08-29 12:49:51 -07002281 /* Take a reference, as otherwise the wait_rendering
2282 * below may cause the object to get freed out from
2283 * under us.
2284 */
2285 drm_gem_object_reference(old_obj);
2286
Chris Wilsond7619c42009-02-11 14:26:47 +00002287 /* i915 uses fences for GPU access to tiled buffers */
2288 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002289 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002290
Eric Anholta09ba7f2009-08-29 12:49:51 -07002291 /* This brings the object to the head of the LRU if it
2292 * had been written to. The only way this should
2293 * result in us waiting longer than the expected
2294 * optimal amount of time is if there was a
2295 * fence-using buffer later that was read-only.
2296 */
2297 i915_gem_object_flush_gpu_write_domain(old_obj);
2298 ret = i915_gem_object_wait_rendering(old_obj);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002299 if (ret != 0) {
2300 drm_gem_object_unreference(old_obj);
Chris Wilsond7619c42009-02-11 14:26:47 +00002301 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002302 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002303
Eric Anholta09ba7f2009-08-29 12:49:51 -07002304 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002305 }
2306
2307 /*
2308 * Zap this virtual mapping so we can set up a fence again
2309 * for this object next time we need it.
2310 */
Chris Wilson58c2fb62009-09-01 12:02:39 +01002311 i915_gem_release_mmap(old_obj);
2312
Eric Anholta09ba7f2009-08-29 12:49:51 -07002313 i = old_obj_priv->fence_reg;
Chris Wilson58c2fb62009-09-01 12:02:39 +01002314 reg = &dev_priv->fence_regs[i];
2315
Jesse Barnesde151cf2008-11-12 10:03:55 -08002316 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002317 list_del_init(&old_obj_priv->fence_list);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002318
Eric Anholta09ba7f2009-08-29 12:49:51 -07002319 drm_gem_object_unreference(old_obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002320 }
2321
2322 obj_priv->fence_reg = i;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002323 list_add_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2324
Jesse Barnesde151cf2008-11-12 10:03:55 -08002325 reg->obj = obj;
2326
2327 if (IS_I965G(dev))
2328 i965_write_fence_reg(reg);
2329 else if (IS_I9XX(dev))
2330 i915_write_fence_reg(reg);
2331 else
2332 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002333
2334 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002335}
2336
2337/**
2338 * i915_gem_clear_fence_reg - clear out fence register info
2339 * @obj: object to clear
2340 *
2341 * Zeroes out the fence register itself and clears out the associated
2342 * data structures in dev_priv and obj_priv.
2343 */
2344static void
2345i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2346{
2347 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002348 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002349 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2350
2351 if (IS_I965G(dev))
2352 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002353 else {
2354 uint32_t fence_reg;
2355
2356 if (obj_priv->fence_reg < 8)
2357 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2358 else
2359 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2360 8) * 4;
2361
2362 I915_WRITE(fence_reg, 0);
2363 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002364
2365 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2366 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002367 list_del_init(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002368}
2369
Eric Anholt673a3942008-07-30 12:06:12 -07002370/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002371 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2372 * to the buffer to finish, and then resets the fence register.
2373 * @obj: tiled object holding a fence register.
2374 *
2375 * Zeroes out the fence register itself and clears out the associated
2376 * data structures in dev_priv and obj_priv.
2377 */
2378int
2379i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2380{
2381 struct drm_device *dev = obj->dev;
2382 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2383
2384 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2385 return 0;
2386
2387 /* On the i915, GPU access to tiled buffers is via a fence,
2388 * therefore we must wait for any outstanding access to complete
2389 * before clearing the fence.
2390 */
2391 if (!IS_I965G(dev)) {
2392 int ret;
2393
2394 i915_gem_object_flush_gpu_write_domain(obj);
2395 i915_gem_object_flush_gtt_write_domain(obj);
2396 ret = i915_gem_object_wait_rendering(obj);
2397 if (ret != 0)
2398 return ret;
2399 }
2400
2401 i915_gem_clear_fence_reg (obj);
2402
2403 return 0;
2404}
2405
2406/**
Eric Anholt673a3942008-07-30 12:06:12 -07002407 * Finds free space in the GTT aperture and binds the object there.
2408 */
2409static int
2410i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2411{
2412 struct drm_device *dev = obj->dev;
2413 drm_i915_private_t *dev_priv = dev->dev_private;
2414 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2415 struct drm_mm_node *free_space;
2416 int page_count, ret;
2417
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002418 if (dev_priv->mm.suspended)
2419 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002420 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002421 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002422 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002423 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2424 return -EINVAL;
2425 }
2426
2427 search_free:
2428 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2429 obj->size, alignment, 0);
2430 if (free_space != NULL) {
2431 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2432 alignment);
2433 if (obj_priv->gtt_space != NULL) {
2434 obj_priv->gtt_space->private = obj;
2435 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2436 }
2437 }
2438 if (obj_priv->gtt_space == NULL) {
Carl Worth5e118f42009-03-20 11:54:25 -07002439 bool lists_empty;
2440
Eric Anholt673a3942008-07-30 12:06:12 -07002441 /* If the gtt is empty and we're still having trouble
2442 * fitting our object in, we're out of memory.
2443 */
2444#if WATCH_LRU
2445 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2446#endif
Carl Worth5e118f42009-03-20 11:54:25 -07002447 spin_lock(&dev_priv->mm.active_list_lock);
2448 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2449 list_empty(&dev_priv->mm.flushing_list) &&
2450 list_empty(&dev_priv->mm.active_list));
2451 spin_unlock(&dev_priv->mm.active_list_lock);
2452 if (lists_empty) {
Eric Anholt673a3942008-07-30 12:06:12 -07002453 DRM_ERROR("GTT full, but LRU list empty\n");
Chris Wilson2939e1f2009-06-06 09:46:03 +01002454 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002455 }
2456
2457 ret = i915_gem_evict_something(dev);
2458 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08002459 if (ret != -ERESTARTSYS)
2460 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002461 return ret;
2462 }
2463 goto search_free;
2464 }
2465
2466#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002467 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002468 obj->size, obj_priv->gtt_offset);
2469#endif
Eric Anholt856fa192009-03-19 14:10:50 -07002470 ret = i915_gem_object_get_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002471 if (ret) {
2472 drm_mm_put_block(obj_priv->gtt_space);
2473 obj_priv->gtt_space = NULL;
2474 return ret;
2475 }
2476
2477 page_count = obj->size / PAGE_SIZE;
2478 /* Create an AGP memory structure pointing at our pages, and bind it
2479 * into the GTT.
2480 */
2481 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002482 obj_priv->pages,
Eric Anholt673a3942008-07-30 12:06:12 -07002483 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002484 obj_priv->gtt_offset,
2485 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002486 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002487 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002488 drm_mm_put_block(obj_priv->gtt_space);
2489 obj_priv->gtt_space = NULL;
2490 return -ENOMEM;
2491 }
2492 atomic_inc(&dev->gtt_count);
2493 atomic_add(obj->size, &dev->gtt_memory);
2494
2495 /* Assert that the object is not currently in any GPU domain. As it
2496 * wasn't in the GTT, there shouldn't be any way it could have been in
2497 * a GPU cache
2498 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002499 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2500 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002501
2502 return 0;
2503}
2504
2505void
2506i915_gem_clflush_object(struct drm_gem_object *obj)
2507{
2508 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2509
2510 /* If we don't have a page list set up, then we're not pinned
2511 * to GPU, and we can ignore the cache flush because it'll happen
2512 * again at bind time.
2513 */
Eric Anholt856fa192009-03-19 14:10:50 -07002514 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002515 return;
2516
Eric Anholt856fa192009-03-19 14:10:50 -07002517 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002518}
2519
Eric Anholte47c68e2008-11-14 13:35:19 -08002520/** Flushes any GPU write domain for the object if it's dirty. */
2521static void
2522i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2523{
2524 struct drm_device *dev = obj->dev;
2525 uint32_t seqno;
2526
2527 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2528 return;
2529
2530 /* Queue the GPU write cache flushing we need. */
2531 i915_gem_flush(dev, 0, obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002532 seqno = i915_add_request(dev, NULL, obj->write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002533 obj->write_domain = 0;
2534 i915_gem_object_move_to_active(obj, seqno);
2535}
2536
2537/** Flushes the GTT write domain for the object if it's dirty. */
2538static void
2539i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2540{
2541 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2542 return;
2543
2544 /* No actual flushing is required for the GTT write domain. Writes
2545 * to it immediately go to main memory as far as we know, so there's
2546 * no chipset flush. It also doesn't land in render cache.
2547 */
2548 obj->write_domain = 0;
2549}
2550
2551/** Flushes the CPU write domain for the object if it's dirty. */
2552static void
2553i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2554{
2555 struct drm_device *dev = obj->dev;
2556
2557 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2558 return;
2559
2560 i915_gem_clflush_object(obj);
2561 drm_agp_chipset_flush(dev);
2562 obj->write_domain = 0;
2563}
2564
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002565/**
2566 * Moves a single object to the GTT read, and possibly write domain.
2567 *
2568 * This function returns when the move is complete, including waiting on
2569 * flushes to occur.
2570 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002571int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002572i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2573{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002574 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002575 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002576
Eric Anholt02354392008-11-26 13:58:13 -08002577 /* Not valid to be called on unbound objects. */
2578 if (obj_priv->gtt_space == NULL)
2579 return -EINVAL;
2580
Eric Anholte47c68e2008-11-14 13:35:19 -08002581 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002582 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002583 ret = i915_gem_object_wait_rendering(obj);
2584 if (ret != 0)
2585 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002586
2587 /* If we're writing through the GTT domain, then CPU and GPU caches
2588 * will need to be invalidated at next use.
2589 */
2590 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002591 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002592
Eric Anholte47c68e2008-11-14 13:35:19 -08002593 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002594
2595 /* It should now be out of any other write domains, and we can update
2596 * the domain values for our changes.
2597 */
2598 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2599 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002600 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002601 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002602 obj_priv->dirty = 1;
2603 }
2604
2605 return 0;
2606}
2607
2608/**
2609 * Moves a single object to the CPU read, and possibly write domain.
2610 *
2611 * This function returns when the move is complete, including waiting on
2612 * flushes to occur.
2613 */
2614static int
2615i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2616{
Eric Anholte47c68e2008-11-14 13:35:19 -08002617 int ret;
2618
2619 i915_gem_object_flush_gpu_write_domain(obj);
2620 /* Wait on any GPU rendering and flushing to occur. */
2621 ret = i915_gem_object_wait_rendering(obj);
2622 if (ret != 0)
2623 return ret;
2624
2625 i915_gem_object_flush_gtt_write_domain(obj);
2626
2627 /* If we have a partially-valid cache of the object in the CPU,
2628 * finish invalidating it and free the per-page flags.
2629 */
2630 i915_gem_object_set_to_full_cpu_read_domain(obj);
2631
2632 /* Flush the CPU cache if it's still invalid. */
2633 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2634 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002635
2636 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2637 }
2638
2639 /* It should now be out of any other write domains, and we can update
2640 * the domain values for our changes.
2641 */
2642 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2643
2644 /* If we're writing through the CPU, then the GPU read domains will
2645 * need to be invalidated at next use.
2646 */
2647 if (write) {
2648 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2649 obj->write_domain = I915_GEM_DOMAIN_CPU;
2650 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002651
2652 return 0;
2653}
2654
Eric Anholt673a3942008-07-30 12:06:12 -07002655/*
2656 * Set the next domain for the specified object. This
2657 * may not actually perform the necessary flushing/invaliding though,
2658 * as that may want to be batched with other set_domain operations
2659 *
2660 * This is (we hope) the only really tricky part of gem. The goal
2661 * is fairly simple -- track which caches hold bits of the object
2662 * and make sure they remain coherent. A few concrete examples may
2663 * help to explain how it works. For shorthand, we use the notation
2664 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2665 * a pair of read and write domain masks.
2666 *
2667 * Case 1: the batch buffer
2668 *
2669 * 1. Allocated
2670 * 2. Written by CPU
2671 * 3. Mapped to GTT
2672 * 4. Read by GPU
2673 * 5. Unmapped from GTT
2674 * 6. Freed
2675 *
2676 * Let's take these a step at a time
2677 *
2678 * 1. Allocated
2679 * Pages allocated from the kernel may still have
2680 * cache contents, so we set them to (CPU, CPU) always.
2681 * 2. Written by CPU (using pwrite)
2682 * The pwrite function calls set_domain (CPU, CPU) and
2683 * this function does nothing (as nothing changes)
2684 * 3. Mapped by GTT
2685 * This function asserts that the object is not
2686 * currently in any GPU-based read or write domains
2687 * 4. Read by GPU
2688 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2689 * As write_domain is zero, this function adds in the
2690 * current read domains (CPU+COMMAND, 0).
2691 * flush_domains is set to CPU.
2692 * invalidate_domains is set to COMMAND
2693 * clflush is run to get data out of the CPU caches
2694 * then i915_dev_set_domain calls i915_gem_flush to
2695 * emit an MI_FLUSH and drm_agp_chipset_flush
2696 * 5. Unmapped from GTT
2697 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2698 * flush_domains and invalidate_domains end up both zero
2699 * so no flushing/invalidating happens
2700 * 6. Freed
2701 * yay, done
2702 *
2703 * Case 2: The shared render buffer
2704 *
2705 * 1. Allocated
2706 * 2. Mapped to GTT
2707 * 3. Read/written by GPU
2708 * 4. set_domain to (CPU,CPU)
2709 * 5. Read/written by CPU
2710 * 6. Read/written by GPU
2711 *
2712 * 1. Allocated
2713 * Same as last example, (CPU, CPU)
2714 * 2. Mapped to GTT
2715 * Nothing changes (assertions find that it is not in the GPU)
2716 * 3. Read/written by GPU
2717 * execbuffer calls set_domain (RENDER, RENDER)
2718 * flush_domains gets CPU
2719 * invalidate_domains gets GPU
2720 * clflush (obj)
2721 * MI_FLUSH and drm_agp_chipset_flush
2722 * 4. set_domain (CPU, CPU)
2723 * flush_domains gets GPU
2724 * invalidate_domains gets CPU
2725 * wait_rendering (obj) to make sure all drawing is complete.
2726 * This will include an MI_FLUSH to get the data from GPU
2727 * to memory
2728 * clflush (obj) to invalidate the CPU cache
2729 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2730 * 5. Read/written by CPU
2731 * cache lines are loaded and dirtied
2732 * 6. Read written by GPU
2733 * Same as last GPU access
2734 *
2735 * Case 3: The constant buffer
2736 *
2737 * 1. Allocated
2738 * 2. Written by CPU
2739 * 3. Read by GPU
2740 * 4. Updated (written) by CPU again
2741 * 5. Read by GPU
2742 *
2743 * 1. Allocated
2744 * (CPU, CPU)
2745 * 2. Written by CPU
2746 * (CPU, CPU)
2747 * 3. Read by GPU
2748 * (CPU+RENDER, 0)
2749 * flush_domains = CPU
2750 * invalidate_domains = RENDER
2751 * clflush (obj)
2752 * MI_FLUSH
2753 * drm_agp_chipset_flush
2754 * 4. Updated (written) by CPU again
2755 * (CPU, CPU)
2756 * flush_domains = 0 (no previous write domain)
2757 * invalidate_domains = 0 (no new read domains)
2758 * 5. Read by GPU
2759 * (CPU+RENDER, 0)
2760 * flush_domains = CPU
2761 * invalidate_domains = RENDER
2762 * clflush (obj)
2763 * MI_FLUSH
2764 * drm_agp_chipset_flush
2765 */
Keith Packardc0d90822008-11-20 23:11:08 -08002766static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002767i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002768{
2769 struct drm_device *dev = obj->dev;
2770 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2771 uint32_t invalidate_domains = 0;
2772 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002773
Eric Anholt8b0e3782009-02-19 14:40:50 -08002774 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2775 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002776
Jesse Barnes652c3932009-08-17 13:31:43 -07002777 intel_mark_busy(dev, obj);
2778
Eric Anholt673a3942008-07-30 12:06:12 -07002779#if WATCH_BUF
2780 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2781 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002782 obj->read_domains, obj->pending_read_domains,
2783 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002784#endif
2785 /*
2786 * If the object isn't moving to a new write domain,
2787 * let the object stay in multiple read domains
2788 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002789 if (obj->pending_write_domain == 0)
2790 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002791 else
2792 obj_priv->dirty = 1;
2793
2794 /*
2795 * Flush the current write domain if
2796 * the new read domains don't match. Invalidate
2797 * any read domains which differ from the old
2798 * write domain
2799 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002800 if (obj->write_domain &&
2801 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002802 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002803 invalidate_domains |=
2804 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002805 }
2806 /*
2807 * Invalidate any read caches which may have
2808 * stale data. That is, any new read domains.
2809 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002810 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002811 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2812#if WATCH_BUF
2813 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2814 __func__, flush_domains, invalidate_domains);
2815#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002816 i915_gem_clflush_object(obj);
2817 }
2818
Eric Anholtefbeed92009-02-19 14:54:51 -08002819 /* The actual obj->write_domain will be updated with
2820 * pending_write_domain after we emit the accumulated flush for all
2821 * of our domain changes in execbuffers (which clears objects'
2822 * write_domains). So if we have a current write domain that we
2823 * aren't changing, set pending_write_domain to that.
2824 */
2825 if (flush_domains == 0 && obj->pending_write_domain == 0)
2826 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002827 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002828
2829 dev->invalidate_domains |= invalidate_domains;
2830 dev->flush_domains |= flush_domains;
2831#if WATCH_BUF
2832 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2833 __func__,
2834 obj->read_domains, obj->write_domain,
2835 dev->invalidate_domains, dev->flush_domains);
2836#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002837}
2838
2839/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002840 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002841 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002842 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2843 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2844 */
2845static void
2846i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2847{
Eric Anholte47c68e2008-11-14 13:35:19 -08002848 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2849
2850 if (!obj_priv->page_cpu_valid)
2851 return;
2852
2853 /* If we're partially in the CPU read domain, finish moving it in.
2854 */
2855 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2856 int i;
2857
2858 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2859 if (obj_priv->page_cpu_valid[i])
2860 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07002861 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08002862 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002863 }
2864
2865 /* Free the page_cpu_valid mappings which are now stale, whether
2866 * or not we've got I915_GEM_DOMAIN_CPU.
2867 */
Eric Anholt9a298b22009-03-24 12:23:04 -07002868 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08002869 obj_priv->page_cpu_valid = NULL;
2870}
2871
2872/**
2873 * Set the CPU read domain on a range of the object.
2874 *
2875 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2876 * not entirely valid. The page_cpu_valid member of the object flags which
2877 * pages have been flushed, and will be respected by
2878 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2879 * of the whole object.
2880 *
2881 * This function returns when the move is complete, including waiting on
2882 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002883 */
2884static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002885i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2886 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002887{
2888 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002889 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002890
Eric Anholte47c68e2008-11-14 13:35:19 -08002891 if (offset == 0 && size == obj->size)
2892 return i915_gem_object_set_to_cpu_domain(obj, 0);
2893
2894 i915_gem_object_flush_gpu_write_domain(obj);
2895 /* Wait on any GPU rendering and flushing to occur. */
2896 ret = i915_gem_object_wait_rendering(obj);
2897 if (ret != 0)
2898 return ret;
2899 i915_gem_object_flush_gtt_write_domain(obj);
2900
2901 /* If we're already fully in the CPU read domain, we're done. */
2902 if (obj_priv->page_cpu_valid == NULL &&
2903 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002904 return 0;
2905
Eric Anholte47c68e2008-11-14 13:35:19 -08002906 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2907 * newly adding I915_GEM_DOMAIN_CPU
2908 */
Eric Anholt673a3942008-07-30 12:06:12 -07002909 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07002910 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
2911 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08002912 if (obj_priv->page_cpu_valid == NULL)
2913 return -ENOMEM;
2914 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2915 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002916
2917 /* Flush the cache on any pages that are still invalid from the CPU's
2918 * perspective.
2919 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002920 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2921 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002922 if (obj_priv->page_cpu_valid[i])
2923 continue;
2924
Eric Anholt856fa192009-03-19 14:10:50 -07002925 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002926
2927 obj_priv->page_cpu_valid[i] = 1;
2928 }
2929
Eric Anholte47c68e2008-11-14 13:35:19 -08002930 /* It should now be out of any other write domains, and we can update
2931 * the domain values for our changes.
2932 */
2933 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2934
2935 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2936
Eric Anholt673a3942008-07-30 12:06:12 -07002937 return 0;
2938}
2939
2940/**
Eric Anholt673a3942008-07-30 12:06:12 -07002941 * Pin an object to the GTT and evaluate the relocations landing in it.
2942 */
2943static int
2944i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2945 struct drm_file *file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002946 struct drm_i915_gem_exec_object *entry,
2947 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07002948{
2949 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002950 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002951 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2952 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002953 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002954
2955 /* Choose the GTT offset for our buffer and put it there. */
2956 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2957 if (ret)
2958 return ret;
2959
2960 entry->offset = obj_priv->gtt_offset;
2961
Eric Anholt673a3942008-07-30 12:06:12 -07002962 /* Apply the relocations, using the GTT aperture to avoid cache
2963 * flushing requirements.
2964 */
2965 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002966 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002967 struct drm_gem_object *target_obj;
2968 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002969 uint32_t reloc_val, reloc_offset;
2970 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002971
Eric Anholt673a3942008-07-30 12:06:12 -07002972 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002973 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002974 if (target_obj == NULL) {
2975 i915_gem_object_unpin(obj);
2976 return -EBADF;
2977 }
2978 target_obj_priv = target_obj->driver_private;
2979
2980 /* The target buffer should have appeared before us in the
2981 * exec_object list, so it should have a GTT space bound by now.
2982 */
2983 if (target_obj_priv->gtt_space == NULL) {
2984 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002985 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002986 drm_gem_object_unreference(target_obj);
2987 i915_gem_object_unpin(obj);
2988 return -EINVAL;
2989 }
2990
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002991 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07002992 DRM_ERROR("Relocation beyond object bounds: "
2993 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002994 obj, reloc->target_handle,
2995 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07002996 drm_gem_object_unreference(target_obj);
2997 i915_gem_object_unpin(obj);
2998 return -EINVAL;
2999 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003000 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003001 DRM_ERROR("Relocation not 4-byte aligned: "
3002 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003003 obj, reloc->target_handle,
3004 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003005 drm_gem_object_unreference(target_obj);
3006 i915_gem_object_unpin(obj);
3007 return -EINVAL;
3008 }
3009
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003010 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3011 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003012 DRM_ERROR("reloc with read/write CPU domains: "
3013 "obj %p target %d offset %d "
3014 "read %08x write %08x",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003015 obj, reloc->target_handle,
3016 (int) reloc->offset,
3017 reloc->read_domains,
3018 reloc->write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00003019 drm_gem_object_unreference(target_obj);
3020 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003021 return -EINVAL;
3022 }
3023
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003024 if (reloc->write_domain && target_obj->pending_write_domain &&
3025 reloc->write_domain != target_obj->pending_write_domain) {
Eric Anholt673a3942008-07-30 12:06:12 -07003026 DRM_ERROR("Write domain conflict: "
3027 "obj %p target %d offset %d "
3028 "new %08x old %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003029 obj, reloc->target_handle,
3030 (int) reloc->offset,
3031 reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003032 target_obj->pending_write_domain);
3033 drm_gem_object_unreference(target_obj);
3034 i915_gem_object_unpin(obj);
3035 return -EINVAL;
3036 }
3037
3038#if WATCH_RELOC
3039 DRM_INFO("%s: obj %p offset %08x target %d "
3040 "read %08x write %08x gtt %08x "
3041 "presumed %08x delta %08x\n",
3042 __func__,
3043 obj,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003044 (int) reloc->offset,
3045 (int) reloc->target_handle,
3046 (int) reloc->read_domains,
3047 (int) reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003048 (int) target_obj_priv->gtt_offset,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003049 (int) reloc->presumed_offset,
3050 reloc->delta);
Eric Anholt673a3942008-07-30 12:06:12 -07003051#endif
3052
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003053 target_obj->pending_read_domains |= reloc->read_domains;
3054 target_obj->pending_write_domain |= reloc->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003055
3056 /* If the relocation already has the right value in it, no
3057 * more work needs to be done.
3058 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003059 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
Eric Anholt673a3942008-07-30 12:06:12 -07003060 drm_gem_object_unreference(target_obj);
3061 continue;
3062 }
3063
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003064 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3065 if (ret != 0) {
3066 drm_gem_object_unreference(target_obj);
3067 i915_gem_object_unpin(obj);
3068 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003069 }
3070
3071 /* Map the page containing the relocation we're going to
3072 * perform.
3073 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003074 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003075 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3076 (reloc_offset &
3077 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003078 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003079 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003080 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003081
3082#if WATCH_BUF
3083 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003084 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003085 readl(reloc_entry), reloc_val);
3086#endif
3087 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003088 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003089
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003090 /* The updated presumed offset for this entry will be
3091 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003092 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003093 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003094
3095 drm_gem_object_unreference(target_obj);
3096 }
3097
Eric Anholt673a3942008-07-30 12:06:12 -07003098#if WATCH_BUF
3099 if (0)
3100 i915_gem_dump_object(obj, 128, __func__, ~0);
3101#endif
3102 return 0;
3103}
3104
3105/** Dispatch a batchbuffer to the ring
3106 */
3107static int
3108i915_dispatch_gem_execbuffer(struct drm_device *dev,
3109 struct drm_i915_gem_execbuffer *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003110 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003111 uint64_t exec_offset)
3112{
3113 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003114 int nbox = exec->num_cliprects;
3115 int i = 0, count;
Chris Wilson83d60792009-06-06 09:45:57 +01003116 uint32_t exec_start, exec_len;
Eric Anholt673a3942008-07-30 12:06:12 -07003117 RING_LOCALS;
3118
3119 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3120 exec_len = (uint32_t) exec->batch_len;
3121
Eric Anholt673a3942008-07-30 12:06:12 -07003122 count = nbox ? nbox : 1;
3123
3124 for (i = 0; i < count; i++) {
3125 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003126 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003127 exec->DR1, exec->DR4);
3128 if (ret)
3129 return ret;
3130 }
3131
3132 if (IS_I830(dev) || IS_845G(dev)) {
3133 BEGIN_LP_RING(4);
3134 OUT_RING(MI_BATCH_BUFFER);
3135 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3136 OUT_RING(exec_start + exec_len - 4);
3137 OUT_RING(0);
3138 ADVANCE_LP_RING();
3139 } else {
3140 BEGIN_LP_RING(2);
3141 if (IS_I965G(dev)) {
3142 OUT_RING(MI_BATCH_BUFFER_START |
3143 (2 << 6) |
3144 MI_BATCH_NON_SECURE_I965);
3145 OUT_RING(exec_start);
3146 } else {
3147 OUT_RING(MI_BATCH_BUFFER_START |
3148 (2 << 6));
3149 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3150 }
3151 ADVANCE_LP_RING();
3152 }
3153 }
3154
3155 /* XXX breadcrumb */
3156 return 0;
3157}
3158
3159/* Throttle our rendering by waiting until the ring has completed our requests
3160 * emitted over 20 msec ago.
3161 *
Eric Anholtb9624422009-06-03 07:27:35 +00003162 * Note that if we were to use the current jiffies each time around the loop,
3163 * we wouldn't escape the function with any frames outstanding if the time to
3164 * render a frame was over 20ms.
3165 *
Eric Anholt673a3942008-07-30 12:06:12 -07003166 * This should get us reasonable parallelism between CPU and GPU but also
3167 * relatively low latency when blocking on a particular request to finish.
3168 */
3169static int
3170i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3171{
3172 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3173 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003174 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003175
3176 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003177 while (!list_empty(&i915_file_priv->mm.request_list)) {
3178 struct drm_i915_gem_request *request;
3179
3180 request = list_first_entry(&i915_file_priv->mm.request_list,
3181 struct drm_i915_gem_request,
3182 client_list);
3183
3184 if (time_after_eq(request->emitted_jiffies, recent_enough))
3185 break;
3186
3187 ret = i915_wait_request(dev, request->seqno);
3188 if (ret != 0)
3189 break;
3190 }
Eric Anholt673a3942008-07-30 12:06:12 -07003191 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003192
Eric Anholt673a3942008-07-30 12:06:12 -07003193 return ret;
3194}
3195
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003196static int
3197i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3198 uint32_t buffer_count,
3199 struct drm_i915_gem_relocation_entry **relocs)
3200{
3201 uint32_t reloc_count = 0, reloc_index = 0, i;
3202 int ret;
3203
3204 *relocs = NULL;
3205 for (i = 0; i < buffer_count; i++) {
3206 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3207 return -EINVAL;
3208 reloc_count += exec_list[i].relocation_count;
3209 }
3210
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003211 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003212 if (*relocs == NULL)
3213 return -ENOMEM;
3214
3215 for (i = 0; i < buffer_count; i++) {
3216 struct drm_i915_gem_relocation_entry __user *user_relocs;
3217
3218 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3219
3220 ret = copy_from_user(&(*relocs)[reloc_index],
3221 user_relocs,
3222 exec_list[i].relocation_count *
3223 sizeof(**relocs));
3224 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003225 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003226 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003227 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003228 }
3229
3230 reloc_index += exec_list[i].relocation_count;
3231 }
3232
Florian Mickler2bc43b52009-04-06 22:55:41 +02003233 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003234}
3235
3236static int
3237i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3238 uint32_t buffer_count,
3239 struct drm_i915_gem_relocation_entry *relocs)
3240{
3241 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003242 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003243
3244 for (i = 0; i < buffer_count; i++) {
3245 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003246 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003247
3248 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3249
Florian Mickler2bc43b52009-04-06 22:55:41 +02003250 unwritten = copy_to_user(user_relocs,
3251 &relocs[reloc_count],
3252 exec_list[i].relocation_count *
3253 sizeof(*relocs));
3254
3255 if (unwritten) {
3256 ret = -EFAULT;
3257 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003258 }
3259
3260 reloc_count += exec_list[i].relocation_count;
3261 }
3262
Florian Mickler2bc43b52009-04-06 22:55:41 +02003263err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003264 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003265
3266 return ret;
3267}
3268
Chris Wilson83d60792009-06-06 09:45:57 +01003269static int
3270i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer *exec,
3271 uint64_t exec_offset)
3272{
3273 uint32_t exec_start, exec_len;
3274
3275 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3276 exec_len = (uint32_t) exec->batch_len;
3277
3278 if ((exec_start | exec_len) & 0x7)
3279 return -EINVAL;
3280
3281 if (!exec_start)
3282 return -EINVAL;
3283
3284 return 0;
3285}
3286
Eric Anholt673a3942008-07-30 12:06:12 -07003287int
3288i915_gem_execbuffer(struct drm_device *dev, void *data,
3289 struct drm_file *file_priv)
3290{
3291 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003292 struct drm_i915_gem_execbuffer *args = data;
3293 struct drm_i915_gem_exec_object *exec_list = NULL;
3294 struct drm_gem_object **object_list = NULL;
3295 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003296 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003297 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003298 struct drm_i915_gem_relocation_entry *relocs;
3299 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003300 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003301 uint32_t seqno, flush_domains, reloc_index;
Keith Packardac94a962008-11-20 23:30:27 -08003302 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07003303
3304#if WATCH_EXEC
3305 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3306 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3307#endif
3308
Eric Anholt4f481ed2008-09-10 14:22:49 -07003309 if (args->buffer_count < 1) {
3310 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3311 return -EINVAL;
3312 }
Eric Anholt673a3942008-07-30 12:06:12 -07003313 /* Copy in the exec list from userland */
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003314 exec_list = drm_calloc_large(sizeof(*exec_list), args->buffer_count);
3315 object_list = drm_calloc_large(sizeof(*object_list), args->buffer_count);
Eric Anholt673a3942008-07-30 12:06:12 -07003316 if (exec_list == NULL || object_list == NULL) {
3317 DRM_ERROR("Failed to allocate exec or object list "
3318 "for %d buffers\n",
3319 args->buffer_count);
3320 ret = -ENOMEM;
3321 goto pre_mutex_err;
3322 }
3323 ret = copy_from_user(exec_list,
3324 (struct drm_i915_relocation_entry __user *)
3325 (uintptr_t) args->buffers_ptr,
3326 sizeof(*exec_list) * args->buffer_count);
3327 if (ret != 0) {
3328 DRM_ERROR("copy %d exec entries failed %d\n",
3329 args->buffer_count, ret);
3330 goto pre_mutex_err;
3331 }
3332
Eric Anholt201361a2009-03-11 12:30:04 -07003333 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003334 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3335 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -07003336 if (cliprects == NULL)
3337 goto pre_mutex_err;
3338
3339 ret = copy_from_user(cliprects,
3340 (struct drm_clip_rect __user *)
3341 (uintptr_t) args->cliprects_ptr,
3342 sizeof(*cliprects) * args->num_cliprects);
3343 if (ret != 0) {
3344 DRM_ERROR("copy %d cliprects failed: %d\n",
3345 args->num_cliprects, ret);
3346 goto pre_mutex_err;
3347 }
3348 }
3349
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003350 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3351 &relocs);
3352 if (ret != 0)
3353 goto pre_mutex_err;
3354
Eric Anholt673a3942008-07-30 12:06:12 -07003355 mutex_lock(&dev->struct_mutex);
3356
3357 i915_verify_inactive(dev, __FILE__, __LINE__);
3358
3359 if (dev_priv->mm.wedged) {
3360 DRM_ERROR("Execbuf while wedged\n");
3361 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003362 ret = -EIO;
3363 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003364 }
3365
3366 if (dev_priv->mm.suspended) {
3367 DRM_ERROR("Execbuf while VT-switched.\n");
3368 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003369 ret = -EBUSY;
3370 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003371 }
3372
Keith Packardac94a962008-11-20 23:30:27 -08003373 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003374 for (i = 0; i < args->buffer_count; i++) {
3375 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3376 exec_list[i].handle);
3377 if (object_list[i] == NULL) {
3378 DRM_ERROR("Invalid object handle %d at index %d\n",
3379 exec_list[i].handle, i);
3380 ret = -EBADF;
3381 goto err;
3382 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003383
3384 obj_priv = object_list[i]->driver_private;
3385 if (obj_priv->in_execbuffer) {
3386 DRM_ERROR("Object %p appears more than once in object list\n",
3387 object_list[i]);
3388 ret = -EBADF;
3389 goto err;
3390 }
3391 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003392 }
Eric Anholt673a3942008-07-30 12:06:12 -07003393
Keith Packardac94a962008-11-20 23:30:27 -08003394 /* Pin and relocate */
3395 for (pin_tries = 0; ; pin_tries++) {
3396 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003397 reloc_index = 0;
3398
Keith Packardac94a962008-11-20 23:30:27 -08003399 for (i = 0; i < args->buffer_count; i++) {
3400 object_list[i]->pending_read_domains = 0;
3401 object_list[i]->pending_write_domain = 0;
3402 ret = i915_gem_object_pin_and_relocate(object_list[i],
3403 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003404 &exec_list[i],
3405 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003406 if (ret)
3407 break;
3408 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003409 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003410 }
3411 /* success */
3412 if (ret == 0)
3413 break;
3414
3415 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003416 if (ret != -ENOSPC || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08003417 if (ret != -ERESTARTSYS)
3418 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003419 goto err;
3420 }
Keith Packardac94a962008-11-20 23:30:27 -08003421
3422 /* unpin all of our buffers */
3423 for (i = 0; i < pinned; i++)
3424 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003425 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003426
3427 /* evict everyone we can from the aperture */
3428 ret = i915_gem_evict_everything(dev);
3429 if (ret)
3430 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003431 }
3432
3433 /* Set the pending read domains for the batch buffer to COMMAND */
3434 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003435 if (batch_obj->pending_write_domain) {
3436 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3437 ret = -EINVAL;
3438 goto err;
3439 }
3440 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003441
Chris Wilson83d60792009-06-06 09:45:57 +01003442 /* Sanity check the batch buffer, prior to moving objects */
3443 exec_offset = exec_list[args->buffer_count - 1].offset;
3444 ret = i915_gem_check_execbuffer (args, exec_offset);
3445 if (ret != 0) {
3446 DRM_ERROR("execbuf with invalid offset/length\n");
3447 goto err;
3448 }
3449
Eric Anholt673a3942008-07-30 12:06:12 -07003450 i915_verify_inactive(dev, __FILE__, __LINE__);
3451
Keith Packard646f0f62008-11-20 23:23:03 -08003452 /* Zero the global flush/invalidate flags. These
3453 * will be modified as new domains are computed
3454 * for each object
3455 */
3456 dev->invalidate_domains = 0;
3457 dev->flush_domains = 0;
3458
Eric Anholt673a3942008-07-30 12:06:12 -07003459 for (i = 0; i < args->buffer_count; i++) {
3460 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003461
Keith Packard646f0f62008-11-20 23:23:03 -08003462 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003463 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003464 }
3465
3466 i915_verify_inactive(dev, __FILE__, __LINE__);
3467
Keith Packard646f0f62008-11-20 23:23:03 -08003468 if (dev->invalidate_domains | dev->flush_domains) {
3469#if WATCH_EXEC
3470 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3471 __func__,
3472 dev->invalidate_domains,
3473 dev->flush_domains);
3474#endif
3475 i915_gem_flush(dev,
3476 dev->invalidate_domains,
3477 dev->flush_domains);
3478 if (dev->flush_domains)
Eric Anholtb9624422009-06-03 07:27:35 +00003479 (void)i915_add_request(dev, file_priv,
3480 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003481 }
Eric Anholt673a3942008-07-30 12:06:12 -07003482
Eric Anholtefbeed92009-02-19 14:54:51 -08003483 for (i = 0; i < args->buffer_count; i++) {
3484 struct drm_gem_object *obj = object_list[i];
3485
3486 obj->write_domain = obj->pending_write_domain;
3487 }
3488
Eric Anholt673a3942008-07-30 12:06:12 -07003489 i915_verify_inactive(dev, __FILE__, __LINE__);
3490
3491#if WATCH_COHERENCY
3492 for (i = 0; i < args->buffer_count; i++) {
3493 i915_gem_object_check_coherency(object_list[i],
3494 exec_list[i].handle);
3495 }
3496#endif
3497
Eric Anholt673a3942008-07-30 12:06:12 -07003498#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003499 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003500 args->batch_len,
3501 __func__,
3502 ~0);
3503#endif
3504
Eric Anholt673a3942008-07-30 12:06:12 -07003505 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003506 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003507 if (ret) {
3508 DRM_ERROR("dispatch failed %d\n", ret);
3509 goto err;
3510 }
3511
3512 /*
3513 * Ensure that the commands in the batch buffer are
3514 * finished before the interrupt fires
3515 */
3516 flush_domains = i915_retire_commands(dev);
3517
3518 i915_verify_inactive(dev, __FILE__, __LINE__);
3519
3520 /*
3521 * Get a seqno representing the execution of the current buffer,
3522 * which we can wait on. We would like to mitigate these interrupts,
3523 * likely by only creating seqnos occasionally (so that we have
3524 * *some* interrupts representing completion of buffers that we can
3525 * wait on when trying to clear up gtt space).
3526 */
Eric Anholtb9624422009-06-03 07:27:35 +00003527 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003528 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003529 for (i = 0; i < args->buffer_count; i++) {
3530 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003531
Eric Anholtce44b0e2008-11-06 16:00:31 -08003532 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003533#if WATCH_LRU
3534 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3535#endif
3536 }
3537#if WATCH_LRU
3538 i915_dump_lru(dev, __func__);
3539#endif
3540
3541 i915_verify_inactive(dev, __FILE__, __LINE__);
3542
Eric Anholt673a3942008-07-30 12:06:12 -07003543err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003544 for (i = 0; i < pinned; i++)
3545 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003546
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003547 for (i = 0; i < args->buffer_count; i++) {
3548 if (object_list[i]) {
3549 obj_priv = object_list[i]->driver_private;
3550 obj_priv->in_execbuffer = false;
3551 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003552 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003553 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003554
Eric Anholt673a3942008-07-30 12:06:12 -07003555 mutex_unlock(&dev->struct_mutex);
3556
Roland Dreiera35f2e22009-02-06 17:48:09 -08003557 if (!ret) {
3558 /* Copy the new buffer offsets back to the user's exec list. */
3559 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3560 (uintptr_t) args->buffers_ptr,
3561 exec_list,
3562 sizeof(*exec_list) * args->buffer_count);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003563 if (ret) {
3564 ret = -EFAULT;
Roland Dreiera35f2e22009-02-06 17:48:09 -08003565 DRM_ERROR("failed to copy %d exec entries "
3566 "back to user (%d)\n",
3567 args->buffer_count, ret);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003568 }
Roland Dreiera35f2e22009-02-06 17:48:09 -08003569 }
3570
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003571 /* Copy the updated relocations out regardless of current error
3572 * state. Failure to update the relocs would mean that the next
3573 * time userland calls execbuf, it would do so with presumed offset
3574 * state that didn't match the actual object state.
3575 */
3576 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3577 relocs);
3578 if (ret2 != 0) {
3579 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3580
3581 if (ret == 0)
3582 ret = ret2;
3583 }
3584
Eric Anholt673a3942008-07-30 12:06:12 -07003585pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003586 drm_free_large(object_list);
3587 drm_free_large(exec_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003588 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003589
3590 return ret;
3591}
3592
3593int
3594i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3595{
3596 struct drm_device *dev = obj->dev;
3597 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3598 int ret;
3599
3600 i915_verify_inactive(dev, __FILE__, __LINE__);
3601 if (obj_priv->gtt_space == NULL) {
3602 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3603 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003604 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05003605 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003606 return ret;
3607 }
Chris Wilson22c344e2009-02-11 14:26:45 +00003608 }
3609 /*
3610 * Pre-965 chips need a fence register set up in order to
3611 * properly handle tiled surfaces.
3612 */
Eric Anholta09ba7f2009-08-29 12:49:51 -07003613 if (!IS_I965G(dev) && obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003614 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson22c344e2009-02-11 14:26:45 +00003615 if (ret != 0) {
3616 if (ret != -EBUSY && ret != -ERESTARTSYS)
3617 DRM_ERROR("Failure to install fence: %d\n",
3618 ret);
3619 return ret;
3620 }
Eric Anholt673a3942008-07-30 12:06:12 -07003621 }
3622 obj_priv->pin_count++;
3623
3624 /* If the object is not active and not pending a flush,
3625 * remove it from the inactive list
3626 */
3627 if (obj_priv->pin_count == 1) {
3628 atomic_inc(&dev->pin_count);
3629 atomic_add(obj->size, &dev->pin_memory);
3630 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003631 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07003632 !list_empty(&obj_priv->list))
3633 list_del_init(&obj_priv->list);
3634 }
3635 i915_verify_inactive(dev, __FILE__, __LINE__);
3636
3637 return 0;
3638}
3639
3640void
3641i915_gem_object_unpin(struct drm_gem_object *obj)
3642{
3643 struct drm_device *dev = obj->dev;
3644 drm_i915_private_t *dev_priv = dev->dev_private;
3645 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3646
3647 i915_verify_inactive(dev, __FILE__, __LINE__);
3648 obj_priv->pin_count--;
3649 BUG_ON(obj_priv->pin_count < 0);
3650 BUG_ON(obj_priv->gtt_space == NULL);
3651
3652 /* If the object is no longer pinned, and is
3653 * neither active nor being flushed, then stick it on
3654 * the inactive list
3655 */
3656 if (obj_priv->pin_count == 0) {
3657 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003658 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003659 list_move_tail(&obj_priv->list,
3660 &dev_priv->mm.inactive_list);
3661 atomic_dec(&dev->pin_count);
3662 atomic_sub(obj->size, &dev->pin_memory);
3663 }
3664 i915_verify_inactive(dev, __FILE__, __LINE__);
3665}
3666
3667int
3668i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3669 struct drm_file *file_priv)
3670{
3671 struct drm_i915_gem_pin *args = data;
3672 struct drm_gem_object *obj;
3673 struct drm_i915_gem_object *obj_priv;
3674 int ret;
3675
3676 mutex_lock(&dev->struct_mutex);
3677
3678 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3679 if (obj == NULL) {
3680 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3681 args->handle);
3682 mutex_unlock(&dev->struct_mutex);
3683 return -EBADF;
3684 }
3685 obj_priv = obj->driver_private;
3686
Jesse Barnes79e53942008-11-07 14:24:08 -08003687 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3688 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3689 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00003690 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003691 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003692 return -EINVAL;
3693 }
3694
3695 obj_priv->user_pin_count++;
3696 obj_priv->pin_filp = file_priv;
3697 if (obj_priv->user_pin_count == 1) {
3698 ret = i915_gem_object_pin(obj, args->alignment);
3699 if (ret != 0) {
3700 drm_gem_object_unreference(obj);
3701 mutex_unlock(&dev->struct_mutex);
3702 return ret;
3703 }
Eric Anholt673a3942008-07-30 12:06:12 -07003704 }
3705
3706 /* XXX - flush the CPU caches for pinned objects
3707 * as the X server doesn't manage domains yet
3708 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003709 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003710 args->offset = obj_priv->gtt_offset;
3711 drm_gem_object_unreference(obj);
3712 mutex_unlock(&dev->struct_mutex);
3713
3714 return 0;
3715}
3716
3717int
3718i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3719 struct drm_file *file_priv)
3720{
3721 struct drm_i915_gem_pin *args = data;
3722 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08003723 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003724
3725 mutex_lock(&dev->struct_mutex);
3726
3727 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3728 if (obj == NULL) {
3729 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3730 args->handle);
3731 mutex_unlock(&dev->struct_mutex);
3732 return -EBADF;
3733 }
3734
Jesse Barnes79e53942008-11-07 14:24:08 -08003735 obj_priv = obj->driver_private;
3736 if (obj_priv->pin_filp != file_priv) {
3737 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3738 args->handle);
3739 drm_gem_object_unreference(obj);
3740 mutex_unlock(&dev->struct_mutex);
3741 return -EINVAL;
3742 }
3743 obj_priv->user_pin_count--;
3744 if (obj_priv->user_pin_count == 0) {
3745 obj_priv->pin_filp = NULL;
3746 i915_gem_object_unpin(obj);
3747 }
Eric Anholt673a3942008-07-30 12:06:12 -07003748
3749 drm_gem_object_unreference(obj);
3750 mutex_unlock(&dev->struct_mutex);
3751 return 0;
3752}
3753
3754int
3755i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3756 struct drm_file *file_priv)
3757{
3758 struct drm_i915_gem_busy *args = data;
3759 struct drm_gem_object *obj;
3760 struct drm_i915_gem_object *obj_priv;
3761
Eric Anholt673a3942008-07-30 12:06:12 -07003762 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3763 if (obj == NULL) {
3764 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3765 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003766 return -EBADF;
3767 }
3768
Chris Wilsonb1ce7862009-06-06 09:46:00 +01003769 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08003770 /* Update the active list for the hardware's current position.
3771 * Otherwise this only updates on a delayed timer or when irqs are
3772 * actually unmasked, and our working set ends up being larger than
3773 * required.
3774 */
3775 i915_gem_retire_requests(dev);
3776
Eric Anholt673a3942008-07-30 12:06:12 -07003777 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08003778 /* Don't count being on the flushing list against the object being
3779 * done. Otherwise, a buffer left on the flushing list but not getting
3780 * flushed (because nobody's flushing that domain) won't ever return
3781 * unbusy and get reused by libdrm's bo cache. The other expected
3782 * consumer of this interface, OpenGL's occlusion queries, also specs
3783 * that the objects get unbusy "eventually" without any interference.
3784 */
3785 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003786
3787 drm_gem_object_unreference(obj);
3788 mutex_unlock(&dev->struct_mutex);
3789 return 0;
3790}
3791
3792int
3793i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3794 struct drm_file *file_priv)
3795{
3796 return i915_gem_ring_throttle(dev, file_priv);
3797}
3798
3799int i915_gem_init_object(struct drm_gem_object *obj)
3800{
3801 struct drm_i915_gem_object *obj_priv;
3802
Eric Anholt9a298b22009-03-24 12:23:04 -07003803 obj_priv = kzalloc(sizeof(*obj_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07003804 if (obj_priv == NULL)
3805 return -ENOMEM;
3806
3807 /*
3808 * We've just allocated pages from the kernel,
3809 * so they've just been written by the CPU with
3810 * zeros. They'll need to be clflushed before we
3811 * use them with the GPU.
3812 */
3813 obj->write_domain = I915_GEM_DOMAIN_CPU;
3814 obj->read_domains = I915_GEM_DOMAIN_CPU;
3815
Keith Packardba1eb1d2008-10-14 19:55:10 -07003816 obj_priv->agp_type = AGP_USER_MEMORY;
3817
Eric Anholt673a3942008-07-30 12:06:12 -07003818 obj->driver_private = obj_priv;
3819 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003820 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07003821 INIT_LIST_HEAD(&obj_priv->list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003822 INIT_LIST_HEAD(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003823
Eric Anholt673a3942008-07-30 12:06:12 -07003824 return 0;
3825}
3826
3827void i915_gem_free_object(struct drm_gem_object *obj)
3828{
Jesse Barnesde151cf2008-11-12 10:03:55 -08003829 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003830 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3831
3832 while (obj_priv->pin_count > 0)
3833 i915_gem_object_unpin(obj);
3834
Dave Airlie71acb5e2008-12-30 20:31:46 +10003835 if (obj_priv->phys_obj)
3836 i915_gem_detach_phys_object(dev, obj);
3837
Eric Anholt673a3942008-07-30 12:06:12 -07003838 i915_gem_object_unbind(obj);
3839
Chris Wilson7e616152009-09-10 08:53:04 +01003840 if (obj_priv->mmap_offset)
3841 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003842
Eric Anholt9a298b22009-03-24 12:23:04 -07003843 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07003844 kfree(obj_priv->bit_17);
Eric Anholt9a298b22009-03-24 12:23:04 -07003845 kfree(obj->driver_private);
Eric Anholt673a3942008-07-30 12:06:12 -07003846}
3847
Eric Anholt673a3942008-07-30 12:06:12 -07003848/** Unbinds all objects that are on the given buffer list. */
3849static int
3850i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3851{
3852 struct drm_gem_object *obj;
3853 struct drm_i915_gem_object *obj_priv;
3854 int ret;
3855
3856 while (!list_empty(head)) {
3857 obj_priv = list_first_entry(head,
3858 struct drm_i915_gem_object,
3859 list);
3860 obj = obj_priv->obj;
3861
3862 if (obj_priv->pin_count != 0) {
3863 DRM_ERROR("Pinned object in unbind list\n");
3864 mutex_unlock(&dev->struct_mutex);
3865 return -EINVAL;
3866 }
3867
3868 ret = i915_gem_object_unbind(obj);
3869 if (ret != 0) {
3870 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3871 ret);
3872 mutex_unlock(&dev->struct_mutex);
3873 return ret;
3874 }
3875 }
3876
3877
3878 return 0;
3879}
3880
Jesse Barnes5669fca2009-02-17 15:13:31 -08003881int
Eric Anholt673a3942008-07-30 12:06:12 -07003882i915_gem_idle(struct drm_device *dev)
3883{
3884 drm_i915_private_t *dev_priv = dev->dev_private;
3885 uint32_t seqno, cur_seqno, last_seqno;
3886 int stuck, ret;
3887
Keith Packard6dbe2772008-10-14 21:41:13 -07003888 mutex_lock(&dev->struct_mutex);
3889
3890 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3891 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003892 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003893 }
Eric Anholt673a3942008-07-30 12:06:12 -07003894
3895 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3896 * We need to replace this with a semaphore, or something.
3897 */
3898 dev_priv->mm.suspended = 1;
3899
Keith Packard6dbe2772008-10-14 21:41:13 -07003900 /* Cancel the retire work handler, wait for it to finish if running
3901 */
3902 mutex_unlock(&dev->struct_mutex);
3903 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3904 mutex_lock(&dev->struct_mutex);
3905
Eric Anholt673a3942008-07-30 12:06:12 -07003906 i915_kernel_lost_context(dev);
3907
3908 /* Flush the GPU along with all non-CPU write domains
3909 */
Chris Wilson21d509e2009-06-06 09:46:02 +01003910 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
3911 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07003912
3913 if (seqno == 0) {
3914 mutex_unlock(&dev->struct_mutex);
3915 return -ENOMEM;
3916 }
3917
3918 dev_priv->mm.waiting_gem_seqno = seqno;
3919 last_seqno = 0;
3920 stuck = 0;
3921 for (;;) {
3922 cur_seqno = i915_get_gem_seqno(dev);
3923 if (i915_seqno_passed(cur_seqno, seqno))
3924 break;
3925 if (last_seqno == cur_seqno) {
3926 if (stuck++ > 100) {
3927 DRM_ERROR("hardware wedged\n");
3928 dev_priv->mm.wedged = 1;
3929 DRM_WAKEUP(&dev_priv->irq_queue);
3930 break;
3931 }
3932 }
3933 msleep(10);
3934 last_seqno = cur_seqno;
3935 }
3936 dev_priv->mm.waiting_gem_seqno = 0;
3937
3938 i915_gem_retire_requests(dev);
3939
Carl Worth5e118f42009-03-20 11:54:25 -07003940 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003941 if (!dev_priv->mm.wedged) {
3942 /* Active and flushing should now be empty as we've
3943 * waited for a sequence higher than any pending execbuffer
3944 */
3945 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3946 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3947 /* Request should now be empty as we've also waited
3948 * for the last request in the list
3949 */
3950 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3951 }
Eric Anholt673a3942008-07-30 12:06:12 -07003952
Eric Anholt28dfe522008-11-13 15:00:55 -08003953 /* Empty the active and flushing lists to inactive. If there's
3954 * anything left at this point, it means that we're wedged and
3955 * nothing good's going to happen by leaving them there. So strip
3956 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003957 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003958 while (!list_empty(&dev_priv->mm.active_list)) {
3959 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003960
Eric Anholt28dfe522008-11-13 15:00:55 -08003961 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3962 struct drm_i915_gem_object,
3963 list);
3964 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3965 i915_gem_object_move_to_inactive(obj_priv->obj);
3966 }
Carl Worth5e118f42009-03-20 11:54:25 -07003967 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003968
3969 while (!list_empty(&dev_priv->mm.flushing_list)) {
3970 struct drm_i915_gem_object *obj_priv;
3971
Eric Anholt151903d2008-12-01 10:23:21 +10003972 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003973 struct drm_i915_gem_object,
3974 list);
3975 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3976 i915_gem_object_move_to_inactive(obj_priv->obj);
3977 }
3978
3979
3980 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003981 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003982 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003983 if (ret) {
3984 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003985 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003986 }
Eric Anholt673a3942008-07-30 12:06:12 -07003987
Keith Packard6dbe2772008-10-14 21:41:13 -07003988 i915_gem_cleanup_ringbuffer(dev);
3989 mutex_unlock(&dev->struct_mutex);
3990
Eric Anholt673a3942008-07-30 12:06:12 -07003991 return 0;
3992}
3993
3994static int
3995i915_gem_init_hws(struct drm_device *dev)
3996{
3997 drm_i915_private_t *dev_priv = dev->dev_private;
3998 struct drm_gem_object *obj;
3999 struct drm_i915_gem_object *obj_priv;
4000 int ret;
4001
4002 /* If we need a physical address for the status page, it's already
4003 * initialized at driver load time.
4004 */
4005 if (!I915_NEED_GFX_HWS(dev))
4006 return 0;
4007
4008 obj = drm_gem_object_alloc(dev, 4096);
4009 if (obj == NULL) {
4010 DRM_ERROR("Failed to allocate status page\n");
4011 return -ENOMEM;
4012 }
4013 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07004014 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07004015
4016 ret = i915_gem_object_pin(obj, 4096);
4017 if (ret != 0) {
4018 drm_gem_object_unreference(obj);
4019 return ret;
4020 }
4021
4022 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07004023
Eric Anholt856fa192009-03-19 14:10:50 -07004024 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004025 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004026 DRM_ERROR("Failed to map status page.\n");
4027 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00004028 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004029 drm_gem_object_unreference(obj);
4030 return -EINVAL;
4031 }
4032 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004033 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
4034 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004035 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07004036 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
4037
4038 return 0;
4039}
4040
Chris Wilson85a7bb92009-02-11 14:52:44 +00004041static void
4042i915_gem_cleanup_hws(struct drm_device *dev)
4043{
4044 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004045 struct drm_gem_object *obj;
4046 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00004047
4048 if (dev_priv->hws_obj == NULL)
4049 return;
4050
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004051 obj = dev_priv->hws_obj;
4052 obj_priv = obj->driver_private;
4053
Eric Anholt856fa192009-03-19 14:10:50 -07004054 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004055 i915_gem_object_unpin(obj);
4056 drm_gem_object_unreference(obj);
4057 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004058
Chris Wilson85a7bb92009-02-11 14:52:44 +00004059 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4060 dev_priv->hw_status_page = NULL;
4061
4062 /* Write high address into HWS_PGA when disabling. */
4063 I915_WRITE(HWS_PGA, 0x1ffff000);
4064}
4065
Jesse Barnes79e53942008-11-07 14:24:08 -08004066int
Eric Anholt673a3942008-07-30 12:06:12 -07004067i915_gem_init_ringbuffer(struct drm_device *dev)
4068{
4069 drm_i915_private_t *dev_priv = dev->dev_private;
4070 struct drm_gem_object *obj;
4071 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08004072 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07004073 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07004074 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07004075
4076 ret = i915_gem_init_hws(dev);
4077 if (ret != 0)
4078 return ret;
4079
4080 obj = drm_gem_object_alloc(dev, 128 * 1024);
4081 if (obj == NULL) {
4082 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004083 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004084 return -ENOMEM;
4085 }
4086 obj_priv = obj->driver_private;
4087
4088 ret = i915_gem_object_pin(obj, 4096);
4089 if (ret != 0) {
4090 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004091 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004092 return ret;
4093 }
4094
4095 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004096 ring->Size = obj->size;
Eric Anholt673a3942008-07-30 12:06:12 -07004097
Jesse Barnes79e53942008-11-07 14:24:08 -08004098 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4099 ring->map.size = obj->size;
4100 ring->map.type = 0;
4101 ring->map.flags = 0;
4102 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004103
Jesse Barnes79e53942008-11-07 14:24:08 -08004104 drm_core_ioremap_wc(&ring->map, dev);
4105 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004106 DRM_ERROR("Failed to map ringbuffer.\n");
4107 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004108 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004109 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004110 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004111 return -EINVAL;
4112 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004113 ring->ring_obj = obj;
4114 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004115
4116 /* Stop the ring if it's running. */
4117 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004118 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07004119 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004120
4121 /* Initialize the ring. */
4122 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07004123 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4124
4125 /* G45 ring initialization fails to reset head to zero */
4126 if (head != 0) {
4127 DRM_ERROR("Ring head not reset to zero "
4128 "ctl %08x head %08x tail %08x start %08x\n",
4129 I915_READ(PRB0_CTL),
4130 I915_READ(PRB0_HEAD),
4131 I915_READ(PRB0_TAIL),
4132 I915_READ(PRB0_START));
4133 I915_WRITE(PRB0_HEAD, 0);
4134
4135 DRM_ERROR("Ring head forced to zero "
4136 "ctl %08x head %08x tail %08x start %08x\n",
4137 I915_READ(PRB0_CTL),
4138 I915_READ(PRB0_HEAD),
4139 I915_READ(PRB0_TAIL),
4140 I915_READ(PRB0_START));
4141 }
4142
Eric Anholt673a3942008-07-30 12:06:12 -07004143 I915_WRITE(PRB0_CTL,
4144 ((obj->size - 4096) & RING_NR_PAGES) |
4145 RING_NO_REPORT |
4146 RING_VALID);
4147
Keith Packard50aa253d2008-10-14 17:20:35 -07004148 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4149
4150 /* If the head is still not zero, the ring is dead */
4151 if (head != 0) {
4152 DRM_ERROR("Ring initialization failed "
4153 "ctl %08x head %08x tail %08x start %08x\n",
4154 I915_READ(PRB0_CTL),
4155 I915_READ(PRB0_HEAD),
4156 I915_READ(PRB0_TAIL),
4157 I915_READ(PRB0_START));
4158 return -EIO;
4159 }
4160
Eric Anholt673a3942008-07-30 12:06:12 -07004161 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004162 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4163 i915_kernel_lost_context(dev);
4164 else {
4165 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4166 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4167 ring->space = ring->head - (ring->tail + 8);
4168 if (ring->space < 0)
4169 ring->space += ring->Size;
4170 }
Eric Anholt673a3942008-07-30 12:06:12 -07004171
4172 return 0;
4173}
4174
Jesse Barnes79e53942008-11-07 14:24:08 -08004175void
Eric Anholt673a3942008-07-30 12:06:12 -07004176i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4177{
4178 drm_i915_private_t *dev_priv = dev->dev_private;
4179
4180 if (dev_priv->ring.ring_obj == NULL)
4181 return;
4182
4183 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4184
4185 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4186 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4187 dev_priv->ring.ring_obj = NULL;
4188 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4189
Chris Wilson85a7bb92009-02-11 14:52:44 +00004190 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004191}
4192
4193int
4194i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4195 struct drm_file *file_priv)
4196{
4197 drm_i915_private_t *dev_priv = dev->dev_private;
4198 int ret;
4199
Jesse Barnes79e53942008-11-07 14:24:08 -08004200 if (drm_core_check_feature(dev, DRIVER_MODESET))
4201 return 0;
4202
Eric Anholt673a3942008-07-30 12:06:12 -07004203 if (dev_priv->mm.wedged) {
4204 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4205 dev_priv->mm.wedged = 0;
4206 }
4207
Eric Anholt673a3942008-07-30 12:06:12 -07004208 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004209 dev_priv->mm.suspended = 0;
4210
4211 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004212 if (ret != 0) {
4213 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004214 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004215 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004216
Carl Worth5e118f42009-03-20 11:54:25 -07004217 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004218 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004219 spin_unlock(&dev_priv->mm.active_list_lock);
4220
Eric Anholt673a3942008-07-30 12:06:12 -07004221 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4222 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4223 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004224 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004225
4226 drm_irq_install(dev);
4227
Eric Anholt673a3942008-07-30 12:06:12 -07004228 return 0;
4229}
4230
4231int
4232i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4233 struct drm_file *file_priv)
4234{
4235 int ret;
4236
Jesse Barnes79e53942008-11-07 14:24:08 -08004237 if (drm_core_check_feature(dev, DRIVER_MODESET))
4238 return 0;
4239
Eric Anholt673a3942008-07-30 12:06:12 -07004240 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004241 drm_irq_uninstall(dev);
4242
Keith Packard6dbe2772008-10-14 21:41:13 -07004243 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004244}
4245
4246void
4247i915_gem_lastclose(struct drm_device *dev)
4248{
4249 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004250
Eric Anholte806b492009-01-22 09:56:58 -08004251 if (drm_core_check_feature(dev, DRIVER_MODESET))
4252 return;
4253
Keith Packard6dbe2772008-10-14 21:41:13 -07004254 ret = i915_gem_idle(dev);
4255 if (ret)
4256 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004257}
4258
4259void
4260i915_gem_load(struct drm_device *dev)
4261{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004262 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004263 drm_i915_private_t *dev_priv = dev->dev_private;
4264
Carl Worth5e118f42009-03-20 11:54:25 -07004265 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004266 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4267 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4268 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4269 INIT_LIST_HEAD(&dev_priv->mm.request_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004270 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004271 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4272 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004273 dev_priv->mm.next_gem_seqno = 1;
4274
Jesse Barnesde151cf2008-11-12 10:03:55 -08004275 /* Old X drivers will take 0-2 for front, back, depth buffers */
4276 dev_priv->fence_reg_start = 3;
4277
Jesse Barnes0f973f22009-01-26 17:10:45 -08004278 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004279 dev_priv->num_fence_regs = 16;
4280 else
4281 dev_priv->num_fence_regs = 8;
4282
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004283 /* Initialize fence registers to zero */
4284 if (IS_I965G(dev)) {
4285 for (i = 0; i < 16; i++)
4286 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4287 } else {
4288 for (i = 0; i < 8; i++)
4289 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4290 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4291 for (i = 0; i < 8; i++)
4292 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4293 }
4294
Eric Anholt673a3942008-07-30 12:06:12 -07004295 i915_gem_detect_bit_6_swizzle(dev);
4296}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004297
4298/*
4299 * Create a physically contiguous memory object for this object
4300 * e.g. for cursor + overlay regs
4301 */
4302int i915_gem_init_phys_object(struct drm_device *dev,
4303 int id, int size)
4304{
4305 drm_i915_private_t *dev_priv = dev->dev_private;
4306 struct drm_i915_gem_phys_object *phys_obj;
4307 int ret;
4308
4309 if (dev_priv->mm.phys_objs[id - 1] || !size)
4310 return 0;
4311
Eric Anholt9a298b22009-03-24 12:23:04 -07004312 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004313 if (!phys_obj)
4314 return -ENOMEM;
4315
4316 phys_obj->id = id;
4317
4318 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4319 if (!phys_obj->handle) {
4320 ret = -ENOMEM;
4321 goto kfree_obj;
4322 }
4323#ifdef CONFIG_X86
4324 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4325#endif
4326
4327 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4328
4329 return 0;
4330kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004331 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004332 return ret;
4333}
4334
4335void i915_gem_free_phys_object(struct drm_device *dev, int id)
4336{
4337 drm_i915_private_t *dev_priv = dev->dev_private;
4338 struct drm_i915_gem_phys_object *phys_obj;
4339
4340 if (!dev_priv->mm.phys_objs[id - 1])
4341 return;
4342
4343 phys_obj = dev_priv->mm.phys_objs[id - 1];
4344 if (phys_obj->cur_obj) {
4345 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4346 }
4347
4348#ifdef CONFIG_X86
4349 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4350#endif
4351 drm_pci_free(dev, phys_obj->handle);
4352 kfree(phys_obj);
4353 dev_priv->mm.phys_objs[id - 1] = NULL;
4354}
4355
4356void i915_gem_free_all_phys_object(struct drm_device *dev)
4357{
4358 int i;
4359
Dave Airlie260883c2009-01-22 17:58:49 +10004360 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004361 i915_gem_free_phys_object(dev, i);
4362}
4363
4364void i915_gem_detach_phys_object(struct drm_device *dev,
4365 struct drm_gem_object *obj)
4366{
4367 struct drm_i915_gem_object *obj_priv;
4368 int i;
4369 int ret;
4370 int page_count;
4371
4372 obj_priv = obj->driver_private;
4373 if (!obj_priv->phys_obj)
4374 return;
4375
Eric Anholt856fa192009-03-19 14:10:50 -07004376 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004377 if (ret)
4378 goto out;
4379
4380 page_count = obj->size / PAGE_SIZE;
4381
4382 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004383 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004384 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4385
4386 memcpy(dst, src, PAGE_SIZE);
4387 kunmap_atomic(dst, KM_USER0);
4388 }
Eric Anholt856fa192009-03-19 14:10:50 -07004389 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004390 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004391
4392 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004393out:
4394 obj_priv->phys_obj->cur_obj = NULL;
4395 obj_priv->phys_obj = NULL;
4396}
4397
4398int
4399i915_gem_attach_phys_object(struct drm_device *dev,
4400 struct drm_gem_object *obj, int id)
4401{
4402 drm_i915_private_t *dev_priv = dev->dev_private;
4403 struct drm_i915_gem_object *obj_priv;
4404 int ret = 0;
4405 int page_count;
4406 int i;
4407
4408 if (id > I915_MAX_PHYS_OBJECT)
4409 return -EINVAL;
4410
4411 obj_priv = obj->driver_private;
4412
4413 if (obj_priv->phys_obj) {
4414 if (obj_priv->phys_obj->id == id)
4415 return 0;
4416 i915_gem_detach_phys_object(dev, obj);
4417 }
4418
4419
4420 /* create a new object */
4421 if (!dev_priv->mm.phys_objs[id - 1]) {
4422 ret = i915_gem_init_phys_object(dev, id,
4423 obj->size);
4424 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004425 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004426 goto out;
4427 }
4428 }
4429
4430 /* bind to the object */
4431 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4432 obj_priv->phys_obj->cur_obj = obj;
4433
Eric Anholt856fa192009-03-19 14:10:50 -07004434 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004435 if (ret) {
4436 DRM_ERROR("failed to get page list\n");
4437 goto out;
4438 }
4439
4440 page_count = obj->size / PAGE_SIZE;
4441
4442 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004443 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004444 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4445
4446 memcpy(dst, src, PAGE_SIZE);
4447 kunmap_atomic(src, KM_USER0);
4448 }
4449
Chris Wilsond78b47b2009-06-17 21:52:49 +01004450 i915_gem_object_put_pages(obj);
4451
Dave Airlie71acb5e2008-12-30 20:31:46 +10004452 return 0;
4453out:
4454 return ret;
4455}
4456
4457static int
4458i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4459 struct drm_i915_gem_pwrite *args,
4460 struct drm_file *file_priv)
4461{
4462 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4463 void *obj_addr;
4464 int ret;
4465 char __user *user_data;
4466
4467 user_data = (char __user *) (uintptr_t) args->data_ptr;
4468 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4469
Dave Airliee08fb4f2009-02-25 14:52:30 +10004470 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004471 ret = copy_from_user(obj_addr, user_data, args->size);
4472 if (ret)
4473 return -EFAULT;
4474
4475 drm_agp_chipset_flush(dev);
4476 return 0;
4477}
Eric Anholtb9624422009-06-03 07:27:35 +00004478
4479void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4480{
4481 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4482
4483 /* Clean up our request list when the client is going away, so that
4484 * later retire_requests won't dereference our soon-to-be-gone
4485 * file_priv.
4486 */
4487 mutex_lock(&dev->struct_mutex);
4488 while (!list_empty(&i915_file_priv->mm.request_list))
4489 list_del_init(i915_file_priv->mm.request_list.next);
4490 mutex_unlock(&dev->struct_mutex);
4491}