blob: f3758f9fc9795e4ae4f09f757d64c6a3d16db546 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070032#include "intel_drv.h"
Eric Anholt673a3942008-07-30 12:06:12 -070033#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080034#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035
Eric Anholt28dfe522008-11-13 15:00:55 -080036#define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
37
Eric Anholte47c68e2008-11-14 13:35:19 -080038static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
39static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
40static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080041static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
42 int write);
43static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
44 uint64_t offset,
45 uint64_t size);
46static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070047static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080048static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
49 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080050static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
51static int i915_gem_evict_something(struct drm_device *dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +100052static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
53 struct drm_i915_gem_pwrite *args,
54 struct drm_file *file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -070055
Jesse Barnes79e53942008-11-07 14:24:08 -080056int i915_gem_do_init(struct drm_device *dev, unsigned long start,
57 unsigned long end)
58{
59 drm_i915_private_t *dev_priv = dev->dev_private;
60
61 if (start >= end ||
62 (start & (PAGE_SIZE - 1)) != 0 ||
63 (end & (PAGE_SIZE - 1)) != 0) {
64 return -EINVAL;
65 }
66
67 drm_mm_init(&dev_priv->mm.gtt_space, start,
68 end - start);
69
70 dev->gtt_total = (uint32_t) (end - start);
71
72 return 0;
73}
Keith Packard6dbe2772008-10-14 21:41:13 -070074
Eric Anholt673a3942008-07-30 12:06:12 -070075int
76i915_gem_init_ioctl(struct drm_device *dev, void *data,
77 struct drm_file *file_priv)
78{
Eric Anholt673a3942008-07-30 12:06:12 -070079 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080080 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070081
82 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080083 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070084 mutex_unlock(&dev->struct_mutex);
85
Jesse Barnes79e53942008-11-07 14:24:08 -080086 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070087}
88
Eric Anholt5a125c32008-10-22 21:40:13 -070089int
90i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
91 struct drm_file *file_priv)
92{
Eric Anholt5a125c32008-10-22 21:40:13 -070093 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -070094
95 if (!(dev->driver->driver_features & DRIVER_GEM))
96 return -ENODEV;
97
98 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -080099 args->aper_available_size = (args->aper_size -
100 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700101
102 return 0;
103}
104
Eric Anholt673a3942008-07-30 12:06:12 -0700105
106/**
107 * Creates a new mm object and returns a handle to it.
108 */
109int
110i915_gem_create_ioctl(struct drm_device *dev, void *data,
111 struct drm_file *file_priv)
112{
113 struct drm_i915_gem_create *args = data;
114 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300115 int ret;
116 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700117
118 args->size = roundup(args->size, PAGE_SIZE);
119
120 /* Allocate the new object */
121 obj = drm_gem_object_alloc(dev, args->size);
122 if (obj == NULL)
123 return -ENOMEM;
124
125 ret = drm_gem_handle_create(file_priv, obj, &handle);
126 mutex_lock(&dev->struct_mutex);
127 drm_gem_object_handle_unreference(obj);
128 mutex_unlock(&dev->struct_mutex);
129
130 if (ret)
131 return ret;
132
133 args->handle = handle;
134
135 return 0;
136}
137
Eric Anholt40123c12009-03-09 13:42:30 -0700138static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700139fast_shmem_read(struct page **pages,
140 loff_t page_base, int page_offset,
141 char __user *data,
142 int length)
143{
144 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200145 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700146
147 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
148 if (vaddr == NULL)
149 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200150 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700151 kunmap_atomic(vaddr, KM_USER0);
152
Florian Mickler2bc43b52009-04-06 22:55:41 +0200153 if (unwritten)
154 return -EFAULT;
155
156 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700157}
158
Eric Anholt280b7132009-03-12 16:56:27 -0700159static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
160{
161 drm_i915_private_t *dev_priv = obj->dev->dev_private;
162 struct drm_i915_gem_object *obj_priv = obj->driver_private;
163
164 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
165 obj_priv->tiling_mode != I915_TILING_NONE;
166}
167
Eric Anholteb014592009-03-10 11:44:52 -0700168static inline int
Eric Anholt40123c12009-03-09 13:42:30 -0700169slow_shmem_copy(struct page *dst_page,
170 int dst_offset,
171 struct page *src_page,
172 int src_offset,
173 int length)
174{
175 char *dst_vaddr, *src_vaddr;
176
177 dst_vaddr = kmap_atomic(dst_page, KM_USER0);
178 if (dst_vaddr == NULL)
179 return -ENOMEM;
180
181 src_vaddr = kmap_atomic(src_page, KM_USER1);
182 if (src_vaddr == NULL) {
183 kunmap_atomic(dst_vaddr, KM_USER0);
184 return -ENOMEM;
185 }
186
187 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
188
189 kunmap_atomic(src_vaddr, KM_USER1);
190 kunmap_atomic(dst_vaddr, KM_USER0);
191
192 return 0;
193}
194
Eric Anholt280b7132009-03-12 16:56:27 -0700195static inline int
196slow_shmem_bit17_copy(struct page *gpu_page,
197 int gpu_offset,
198 struct page *cpu_page,
199 int cpu_offset,
200 int length,
201 int is_read)
202{
203 char *gpu_vaddr, *cpu_vaddr;
204
205 /* Use the unswizzled path if this page isn't affected. */
206 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
207 if (is_read)
208 return slow_shmem_copy(cpu_page, cpu_offset,
209 gpu_page, gpu_offset, length);
210 else
211 return slow_shmem_copy(gpu_page, gpu_offset,
212 cpu_page, cpu_offset, length);
213 }
214
215 gpu_vaddr = kmap_atomic(gpu_page, KM_USER0);
216 if (gpu_vaddr == NULL)
217 return -ENOMEM;
218
219 cpu_vaddr = kmap_atomic(cpu_page, KM_USER1);
220 if (cpu_vaddr == NULL) {
221 kunmap_atomic(gpu_vaddr, KM_USER0);
222 return -ENOMEM;
223 }
224
225 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
226 * XORing with the other bits (A9 for Y, A9 and A10 for X)
227 */
228 while (length > 0) {
229 int cacheline_end = ALIGN(gpu_offset + 1, 64);
230 int this_length = min(cacheline_end - gpu_offset, length);
231 int swizzled_gpu_offset = gpu_offset ^ 64;
232
233 if (is_read) {
234 memcpy(cpu_vaddr + cpu_offset,
235 gpu_vaddr + swizzled_gpu_offset,
236 this_length);
237 } else {
238 memcpy(gpu_vaddr + swizzled_gpu_offset,
239 cpu_vaddr + cpu_offset,
240 this_length);
241 }
242 cpu_offset += this_length;
243 gpu_offset += this_length;
244 length -= this_length;
245 }
246
247 kunmap_atomic(cpu_vaddr, KM_USER1);
248 kunmap_atomic(gpu_vaddr, KM_USER0);
249
250 return 0;
251}
252
Eric Anholt673a3942008-07-30 12:06:12 -0700253/**
Eric Anholteb014592009-03-10 11:44:52 -0700254 * This is the fast shmem pread path, which attempts to copy_from_user directly
255 * from the backing pages of the object to the user's address space. On a
256 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
257 */
258static int
259i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
260 struct drm_i915_gem_pread *args,
261 struct drm_file *file_priv)
262{
263 struct drm_i915_gem_object *obj_priv = obj->driver_private;
264 ssize_t remain;
265 loff_t offset, page_base;
266 char __user *user_data;
267 int page_offset, page_length;
268 int ret;
269
270 user_data = (char __user *) (uintptr_t) args->data_ptr;
271 remain = args->size;
272
273 mutex_lock(&dev->struct_mutex);
274
275 ret = i915_gem_object_get_pages(obj);
276 if (ret != 0)
277 goto fail_unlock;
278
279 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
280 args->size);
281 if (ret != 0)
282 goto fail_put_pages;
283
284 obj_priv = obj->driver_private;
285 offset = args->offset;
286
287 while (remain > 0) {
288 /* Operation in this page
289 *
290 * page_base = page offset within aperture
291 * page_offset = offset within page
292 * page_length = bytes to copy for this page
293 */
294 page_base = (offset & ~(PAGE_SIZE-1));
295 page_offset = offset & (PAGE_SIZE-1);
296 page_length = remain;
297 if ((page_offset + remain) > PAGE_SIZE)
298 page_length = PAGE_SIZE - page_offset;
299
300 ret = fast_shmem_read(obj_priv->pages,
301 page_base, page_offset,
302 user_data, page_length);
303 if (ret)
304 goto fail_put_pages;
305
306 remain -= page_length;
307 user_data += page_length;
308 offset += page_length;
309 }
310
311fail_put_pages:
312 i915_gem_object_put_pages(obj);
313fail_unlock:
314 mutex_unlock(&dev->struct_mutex);
315
316 return ret;
317}
318
319/**
320 * This is the fallback shmem pread path, which allocates temporary storage
321 * in kernel space to copy_to_user into outside of the struct_mutex, so we
322 * can copy out of the object's backing pages while holding the struct mutex
323 * and not take page faults.
324 */
325static int
326i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
327 struct drm_i915_gem_pread *args,
328 struct drm_file *file_priv)
329{
330 struct drm_i915_gem_object *obj_priv = obj->driver_private;
331 struct mm_struct *mm = current->mm;
332 struct page **user_pages;
333 ssize_t remain;
334 loff_t offset, pinned_pages, i;
335 loff_t first_data_page, last_data_page, num_pages;
336 int shmem_page_index, shmem_page_offset;
337 int data_page_index, data_page_offset;
338 int page_length;
339 int ret;
340 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700341 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700342
343 remain = args->size;
344
345 /* Pin the user pages containing the data. We can't fault while
346 * holding the struct mutex, yet we want to hold it while
347 * dereferencing the user data.
348 */
349 first_data_page = data_ptr / PAGE_SIZE;
350 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
351 num_pages = last_data_page - first_data_page + 1;
352
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700353 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700354 if (user_pages == NULL)
355 return -ENOMEM;
356
357 down_read(&mm->mmap_sem);
358 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700359 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700360 up_read(&mm->mmap_sem);
361 if (pinned_pages < num_pages) {
362 ret = -EFAULT;
363 goto fail_put_user_pages;
364 }
365
Eric Anholt280b7132009-03-12 16:56:27 -0700366 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
367
Eric Anholteb014592009-03-10 11:44:52 -0700368 mutex_lock(&dev->struct_mutex);
369
370 ret = i915_gem_object_get_pages(obj);
371 if (ret != 0)
372 goto fail_unlock;
373
374 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
375 args->size);
376 if (ret != 0)
377 goto fail_put_pages;
378
379 obj_priv = obj->driver_private;
380 offset = args->offset;
381
382 while (remain > 0) {
383 /* Operation in this page
384 *
385 * shmem_page_index = page number within shmem file
386 * shmem_page_offset = offset within page in shmem file
387 * data_page_index = page number in get_user_pages return
388 * data_page_offset = offset with data_page_index page.
389 * page_length = bytes to copy for this page
390 */
391 shmem_page_index = offset / PAGE_SIZE;
392 shmem_page_offset = offset & ~PAGE_MASK;
393 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
394 data_page_offset = data_ptr & ~PAGE_MASK;
395
396 page_length = remain;
397 if ((shmem_page_offset + page_length) > PAGE_SIZE)
398 page_length = PAGE_SIZE - shmem_page_offset;
399 if ((data_page_offset + page_length) > PAGE_SIZE)
400 page_length = PAGE_SIZE - data_page_offset;
401
Eric Anholt280b7132009-03-12 16:56:27 -0700402 if (do_bit17_swizzling) {
403 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
404 shmem_page_offset,
405 user_pages[data_page_index],
406 data_page_offset,
407 page_length,
408 1);
409 } else {
410 ret = slow_shmem_copy(user_pages[data_page_index],
411 data_page_offset,
412 obj_priv->pages[shmem_page_index],
413 shmem_page_offset,
414 page_length);
415 }
Eric Anholteb014592009-03-10 11:44:52 -0700416 if (ret)
417 goto fail_put_pages;
418
419 remain -= page_length;
420 data_ptr += page_length;
421 offset += page_length;
422 }
423
424fail_put_pages:
425 i915_gem_object_put_pages(obj);
426fail_unlock:
427 mutex_unlock(&dev->struct_mutex);
428fail_put_user_pages:
429 for (i = 0; i < pinned_pages; i++) {
430 SetPageDirty(user_pages[i]);
431 page_cache_release(user_pages[i]);
432 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700433 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700434
435 return ret;
436}
437
Eric Anholt673a3942008-07-30 12:06:12 -0700438/**
439 * Reads data from the object referenced by handle.
440 *
441 * On error, the contents of *data are undefined.
442 */
443int
444i915_gem_pread_ioctl(struct drm_device *dev, void *data,
445 struct drm_file *file_priv)
446{
447 struct drm_i915_gem_pread *args = data;
448 struct drm_gem_object *obj;
449 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700450 int ret;
451
452 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
453 if (obj == NULL)
454 return -EBADF;
455 obj_priv = obj->driver_private;
456
457 /* Bounds check source.
458 *
459 * XXX: This could use review for overflow issues...
460 */
461 if (args->offset > obj->size || args->size > obj->size ||
462 args->offset + args->size > obj->size) {
463 drm_gem_object_unreference(obj);
464 return -EINVAL;
465 }
466
Eric Anholt280b7132009-03-12 16:56:27 -0700467 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700468 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700469 } else {
470 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
471 if (ret != 0)
472 ret = i915_gem_shmem_pread_slow(dev, obj, args,
473 file_priv);
474 }
Eric Anholt673a3942008-07-30 12:06:12 -0700475
476 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700477
Eric Anholteb014592009-03-10 11:44:52 -0700478 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700479}
480
Keith Packard0839ccb2008-10-30 19:38:48 -0700481/* This is the fast write path which cannot handle
482 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700483 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700484
Keith Packard0839ccb2008-10-30 19:38:48 -0700485static inline int
486fast_user_write(struct io_mapping *mapping,
487 loff_t page_base, int page_offset,
488 char __user *user_data,
489 int length)
490{
491 char *vaddr_atomic;
492 unsigned long unwritten;
493
494 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
495 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
496 user_data, length);
497 io_mapping_unmap_atomic(vaddr_atomic);
498 if (unwritten)
499 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700500 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700501}
502
503/* Here's the write path which can sleep for
504 * page faults
505 */
506
507static inline int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700508slow_kernel_write(struct io_mapping *mapping,
509 loff_t gtt_base, int gtt_offset,
510 struct page *user_page, int user_offset,
511 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700512{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700513 char *src_vaddr, *dst_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700514 unsigned long unwritten;
515
Eric Anholt3de09aa2009-03-09 09:42:23 -0700516 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
517 src_vaddr = kmap_atomic(user_page, KM_USER1);
518 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
519 src_vaddr + user_offset,
520 length);
521 kunmap_atomic(src_vaddr, KM_USER1);
522 io_mapping_unmap_atomic(dst_vaddr);
Keith Packard0839ccb2008-10-30 19:38:48 -0700523 if (unwritten)
524 return -EFAULT;
525 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700526}
527
Eric Anholt40123c12009-03-09 13:42:30 -0700528static inline int
529fast_shmem_write(struct page **pages,
530 loff_t page_base, int page_offset,
531 char __user *data,
532 int length)
533{
534 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400535 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700536
537 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
538 if (vaddr == NULL)
539 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400540 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700541 kunmap_atomic(vaddr, KM_USER0);
542
Dave Airlied0088772009-03-28 20:29:48 -0400543 if (unwritten)
544 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700545 return 0;
546}
547
Eric Anholt3de09aa2009-03-09 09:42:23 -0700548/**
549 * This is the fast pwrite path, where we copy the data directly from the
550 * user into the GTT, uncached.
551 */
Eric Anholt673a3942008-07-30 12:06:12 -0700552static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700553i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
554 struct drm_i915_gem_pwrite *args,
555 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700556{
557 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Keith Packard0839ccb2008-10-30 19:38:48 -0700558 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700559 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700560 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700561 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700562 int page_offset, page_length;
563 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700564
565 user_data = (char __user *) (uintptr_t) args->data_ptr;
566 remain = args->size;
567 if (!access_ok(VERIFY_READ, user_data, remain))
568 return -EFAULT;
569
570
571 mutex_lock(&dev->struct_mutex);
572 ret = i915_gem_object_pin(obj, 0);
573 if (ret) {
574 mutex_unlock(&dev->struct_mutex);
575 return ret;
576 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800577 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700578 if (ret)
579 goto fail;
580
581 obj_priv = obj->driver_private;
582 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700583
584 while (remain > 0) {
585 /* Operation in this page
586 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700587 * page_base = page offset within aperture
588 * page_offset = offset within page
589 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700590 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700591 page_base = (offset & ~(PAGE_SIZE-1));
592 page_offset = offset & (PAGE_SIZE-1);
593 page_length = remain;
594 if ((page_offset + remain) > PAGE_SIZE)
595 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700596
Keith Packard0839ccb2008-10-30 19:38:48 -0700597 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
598 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700599
Keith Packard0839ccb2008-10-30 19:38:48 -0700600 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700601 * source page isn't available. Return the error and we'll
602 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700603 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700604 if (ret)
605 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700606
Keith Packard0839ccb2008-10-30 19:38:48 -0700607 remain -= page_length;
608 user_data += page_length;
609 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700610 }
Eric Anholt673a3942008-07-30 12:06:12 -0700611
612fail:
613 i915_gem_object_unpin(obj);
614 mutex_unlock(&dev->struct_mutex);
615
616 return ret;
617}
618
Eric Anholt3de09aa2009-03-09 09:42:23 -0700619/**
620 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
621 * the memory and maps it using kmap_atomic for copying.
622 *
623 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
624 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
625 */
Eric Anholt3043c602008-10-02 12:24:47 -0700626static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700627i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
628 struct drm_i915_gem_pwrite *args,
629 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700630{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700631 struct drm_i915_gem_object *obj_priv = obj->driver_private;
632 drm_i915_private_t *dev_priv = dev->dev_private;
633 ssize_t remain;
634 loff_t gtt_page_base, offset;
635 loff_t first_data_page, last_data_page, num_pages;
636 loff_t pinned_pages, i;
637 struct page **user_pages;
638 struct mm_struct *mm = current->mm;
639 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700640 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700641 uint64_t data_ptr = args->data_ptr;
642
643 remain = args->size;
644
645 /* Pin the user pages containing the data. We can't fault while
646 * holding the struct mutex, and all of the pwrite implementations
647 * want to hold it while dereferencing the user data.
648 */
649 first_data_page = data_ptr / PAGE_SIZE;
650 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
651 num_pages = last_data_page - first_data_page + 1;
652
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700653 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700654 if (user_pages == NULL)
655 return -ENOMEM;
656
657 down_read(&mm->mmap_sem);
658 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
659 num_pages, 0, 0, user_pages, NULL);
660 up_read(&mm->mmap_sem);
661 if (pinned_pages < num_pages) {
662 ret = -EFAULT;
663 goto out_unpin_pages;
664 }
665
666 mutex_lock(&dev->struct_mutex);
667 ret = i915_gem_object_pin(obj, 0);
668 if (ret)
669 goto out_unlock;
670
671 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
672 if (ret)
673 goto out_unpin_object;
674
675 obj_priv = obj->driver_private;
676 offset = obj_priv->gtt_offset + args->offset;
677
678 while (remain > 0) {
679 /* Operation in this page
680 *
681 * gtt_page_base = page offset within aperture
682 * gtt_page_offset = offset within page in aperture
683 * data_page_index = page number in get_user_pages return
684 * data_page_offset = offset with data_page_index page.
685 * page_length = bytes to copy for this page
686 */
687 gtt_page_base = offset & PAGE_MASK;
688 gtt_page_offset = offset & ~PAGE_MASK;
689 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
690 data_page_offset = data_ptr & ~PAGE_MASK;
691
692 page_length = remain;
693 if ((gtt_page_offset + page_length) > PAGE_SIZE)
694 page_length = PAGE_SIZE - gtt_page_offset;
695 if ((data_page_offset + page_length) > PAGE_SIZE)
696 page_length = PAGE_SIZE - data_page_offset;
697
698 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
699 gtt_page_base, gtt_page_offset,
700 user_pages[data_page_index],
701 data_page_offset,
702 page_length);
703
704 /* If we get a fault while copying data, then (presumably) our
705 * source page isn't available. Return the error and we'll
706 * retry in the slow path.
707 */
708 if (ret)
709 goto out_unpin_object;
710
711 remain -= page_length;
712 offset += page_length;
713 data_ptr += page_length;
714 }
715
716out_unpin_object:
717 i915_gem_object_unpin(obj);
718out_unlock:
719 mutex_unlock(&dev->struct_mutex);
720out_unpin_pages:
721 for (i = 0; i < pinned_pages; i++)
722 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700723 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700724
725 return ret;
726}
727
Eric Anholt40123c12009-03-09 13:42:30 -0700728/**
729 * This is the fast shmem pwrite path, which attempts to directly
730 * copy_from_user into the kmapped pages backing the object.
731 */
Eric Anholt673a3942008-07-30 12:06:12 -0700732static int
Eric Anholt40123c12009-03-09 13:42:30 -0700733i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
734 struct drm_i915_gem_pwrite *args,
735 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700736{
Eric Anholt40123c12009-03-09 13:42:30 -0700737 struct drm_i915_gem_object *obj_priv = obj->driver_private;
738 ssize_t remain;
739 loff_t offset, page_base;
740 char __user *user_data;
741 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700742 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700743
744 user_data = (char __user *) (uintptr_t) args->data_ptr;
745 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700746
747 mutex_lock(&dev->struct_mutex);
748
Eric Anholt40123c12009-03-09 13:42:30 -0700749 ret = i915_gem_object_get_pages(obj);
750 if (ret != 0)
751 goto fail_unlock;
752
Eric Anholte47c68e2008-11-14 13:35:19 -0800753 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700754 if (ret != 0)
755 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700756
Eric Anholt40123c12009-03-09 13:42:30 -0700757 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700758 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700759 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700760
Eric Anholt40123c12009-03-09 13:42:30 -0700761 while (remain > 0) {
762 /* Operation in this page
763 *
764 * page_base = page offset within aperture
765 * page_offset = offset within page
766 * page_length = bytes to copy for this page
767 */
768 page_base = (offset & ~(PAGE_SIZE-1));
769 page_offset = offset & (PAGE_SIZE-1);
770 page_length = remain;
771 if ((page_offset + remain) > PAGE_SIZE)
772 page_length = PAGE_SIZE - page_offset;
773
774 ret = fast_shmem_write(obj_priv->pages,
775 page_base, page_offset,
776 user_data, page_length);
777 if (ret)
778 goto fail_put_pages;
779
780 remain -= page_length;
781 user_data += page_length;
782 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700783 }
784
Eric Anholt40123c12009-03-09 13:42:30 -0700785fail_put_pages:
786 i915_gem_object_put_pages(obj);
787fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700788 mutex_unlock(&dev->struct_mutex);
789
Eric Anholt40123c12009-03-09 13:42:30 -0700790 return ret;
791}
792
793/**
794 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
795 * the memory and maps it using kmap_atomic for copying.
796 *
797 * This avoids taking mmap_sem for faulting on the user's address while the
798 * struct_mutex is held.
799 */
800static int
801i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
802 struct drm_i915_gem_pwrite *args,
803 struct drm_file *file_priv)
804{
805 struct drm_i915_gem_object *obj_priv = obj->driver_private;
806 struct mm_struct *mm = current->mm;
807 struct page **user_pages;
808 ssize_t remain;
809 loff_t offset, pinned_pages, i;
810 loff_t first_data_page, last_data_page, num_pages;
811 int shmem_page_index, shmem_page_offset;
812 int data_page_index, data_page_offset;
813 int page_length;
814 int ret;
815 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700816 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700817
818 remain = args->size;
819
820 /* Pin the user pages containing the data. We can't fault while
821 * holding the struct mutex, and all of the pwrite implementations
822 * want to hold it while dereferencing the user data.
823 */
824 first_data_page = data_ptr / PAGE_SIZE;
825 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
826 num_pages = last_data_page - first_data_page + 1;
827
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700828 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700829 if (user_pages == NULL)
830 return -ENOMEM;
831
832 down_read(&mm->mmap_sem);
833 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
834 num_pages, 0, 0, user_pages, NULL);
835 up_read(&mm->mmap_sem);
836 if (pinned_pages < num_pages) {
837 ret = -EFAULT;
838 goto fail_put_user_pages;
839 }
840
Eric Anholt280b7132009-03-12 16:56:27 -0700841 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
842
Eric Anholt40123c12009-03-09 13:42:30 -0700843 mutex_lock(&dev->struct_mutex);
844
845 ret = i915_gem_object_get_pages(obj);
846 if (ret != 0)
847 goto fail_unlock;
848
849 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
850 if (ret != 0)
851 goto fail_put_pages;
852
853 obj_priv = obj->driver_private;
854 offset = args->offset;
855 obj_priv->dirty = 1;
856
857 while (remain > 0) {
858 /* Operation in this page
859 *
860 * shmem_page_index = page number within shmem file
861 * shmem_page_offset = offset within page in shmem file
862 * data_page_index = page number in get_user_pages return
863 * data_page_offset = offset with data_page_index page.
864 * page_length = bytes to copy for this page
865 */
866 shmem_page_index = offset / PAGE_SIZE;
867 shmem_page_offset = offset & ~PAGE_MASK;
868 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
869 data_page_offset = data_ptr & ~PAGE_MASK;
870
871 page_length = remain;
872 if ((shmem_page_offset + page_length) > PAGE_SIZE)
873 page_length = PAGE_SIZE - shmem_page_offset;
874 if ((data_page_offset + page_length) > PAGE_SIZE)
875 page_length = PAGE_SIZE - data_page_offset;
876
Eric Anholt280b7132009-03-12 16:56:27 -0700877 if (do_bit17_swizzling) {
878 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
879 shmem_page_offset,
880 user_pages[data_page_index],
881 data_page_offset,
882 page_length,
883 0);
884 } else {
885 ret = slow_shmem_copy(obj_priv->pages[shmem_page_index],
886 shmem_page_offset,
887 user_pages[data_page_index],
888 data_page_offset,
889 page_length);
890 }
Eric Anholt40123c12009-03-09 13:42:30 -0700891 if (ret)
892 goto fail_put_pages;
893
894 remain -= page_length;
895 data_ptr += page_length;
896 offset += page_length;
897 }
898
899fail_put_pages:
900 i915_gem_object_put_pages(obj);
901fail_unlock:
902 mutex_unlock(&dev->struct_mutex);
903fail_put_user_pages:
904 for (i = 0; i < pinned_pages; i++)
905 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700906 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700907
908 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700909}
910
911/**
912 * Writes data to the object referenced by handle.
913 *
914 * On error, the contents of the buffer that were to be modified are undefined.
915 */
916int
917i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
918 struct drm_file *file_priv)
919{
920 struct drm_i915_gem_pwrite *args = data;
921 struct drm_gem_object *obj;
922 struct drm_i915_gem_object *obj_priv;
923 int ret = 0;
924
925 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
926 if (obj == NULL)
927 return -EBADF;
928 obj_priv = obj->driver_private;
929
930 /* Bounds check destination.
931 *
932 * XXX: This could use review for overflow issues...
933 */
934 if (args->offset > obj->size || args->size > obj->size ||
935 args->offset + args->size > obj->size) {
936 drm_gem_object_unreference(obj);
937 return -EINVAL;
938 }
939
940 /* We can only do the GTT pwrite on untiled buffers, as otherwise
941 * it would end up going through the fenced access, and we'll get
942 * different detiling behavior between reading and writing.
943 * pread/pwrite currently are reading and writing from the CPU
944 * perspective, requiring manual detiling by the client.
945 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000946 if (obj_priv->phys_obj)
947 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
948 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Eric Anholt3de09aa2009-03-09 09:42:23 -0700949 dev->gtt_total != 0) {
950 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
951 if (ret == -EFAULT) {
952 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
953 file_priv);
954 }
Eric Anholt280b7132009-03-12 16:56:27 -0700955 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
956 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700957 } else {
958 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
959 if (ret == -EFAULT) {
960 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
961 file_priv);
962 }
963 }
Eric Anholt673a3942008-07-30 12:06:12 -0700964
965#if WATCH_PWRITE
966 if (ret)
967 DRM_INFO("pwrite failed %d\n", ret);
968#endif
969
970 drm_gem_object_unreference(obj);
971
972 return ret;
973}
974
975/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800976 * Called when user space prepares to use an object with the CPU, either
977 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700978 */
979int
980i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
981 struct drm_file *file_priv)
982{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700983 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700984 struct drm_i915_gem_set_domain *args = data;
985 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700986 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800987 uint32_t read_domains = args->read_domains;
988 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700989 int ret;
990
991 if (!(dev->driver->driver_features & DRIVER_GEM))
992 return -ENODEV;
993
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800994 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100995 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800996 return -EINVAL;
997
Chris Wilson21d509e2009-06-06 09:46:02 +0100998 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800999 return -EINVAL;
1000
1001 /* Having something in the write domain implies it's in the read
1002 * domain, and only that read domain. Enforce that in the request.
1003 */
1004 if (write_domain != 0 && read_domains != write_domain)
1005 return -EINVAL;
1006
Eric Anholt673a3942008-07-30 12:06:12 -07001007 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1008 if (obj == NULL)
1009 return -EBADF;
Jesse Barnes652c3932009-08-17 13:31:43 -07001010 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001011
1012 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001013
1014 intel_mark_busy(dev, obj);
1015
Eric Anholt673a3942008-07-30 12:06:12 -07001016#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001017 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001018 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001019#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001020 if (read_domains & I915_GEM_DOMAIN_GTT) {
1021 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001022
Eric Anholta09ba7f2009-08-29 12:49:51 -07001023 /* Update the LRU on the fence for the CPU access that's
1024 * about to occur.
1025 */
1026 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1027 list_move_tail(&obj_priv->fence_list,
1028 &dev_priv->mm.fence_list);
1029 }
1030
Eric Anholt02354392008-11-26 13:58:13 -08001031 /* Silently promote "you're not bound, there was nothing to do"
1032 * to success, since the client was just asking us to
1033 * make sure everything was done.
1034 */
1035 if (ret == -EINVAL)
1036 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001037 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001038 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001039 }
1040
Eric Anholt673a3942008-07-30 12:06:12 -07001041 drm_gem_object_unreference(obj);
1042 mutex_unlock(&dev->struct_mutex);
1043 return ret;
1044}
1045
1046/**
1047 * Called when user space has done writes to this buffer
1048 */
1049int
1050i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1051 struct drm_file *file_priv)
1052{
1053 struct drm_i915_gem_sw_finish *args = data;
1054 struct drm_gem_object *obj;
1055 struct drm_i915_gem_object *obj_priv;
1056 int ret = 0;
1057
1058 if (!(dev->driver->driver_features & DRIVER_GEM))
1059 return -ENODEV;
1060
1061 mutex_lock(&dev->struct_mutex);
1062 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1063 if (obj == NULL) {
1064 mutex_unlock(&dev->struct_mutex);
1065 return -EBADF;
1066 }
1067
1068#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001069 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001070 __func__, args->handle, obj, obj->size);
1071#endif
1072 obj_priv = obj->driver_private;
1073
1074 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001075 if (obj_priv->pin_count)
1076 i915_gem_object_flush_cpu_write_domain(obj);
1077
Eric Anholt673a3942008-07-30 12:06:12 -07001078 drm_gem_object_unreference(obj);
1079 mutex_unlock(&dev->struct_mutex);
1080 return ret;
1081}
1082
1083/**
1084 * Maps the contents of an object, returning the address it is mapped
1085 * into.
1086 *
1087 * While the mapping holds a reference on the contents of the object, it doesn't
1088 * imply a ref on the object itself.
1089 */
1090int
1091i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1092 struct drm_file *file_priv)
1093{
1094 struct drm_i915_gem_mmap *args = data;
1095 struct drm_gem_object *obj;
1096 loff_t offset;
1097 unsigned long addr;
1098
1099 if (!(dev->driver->driver_features & DRIVER_GEM))
1100 return -ENODEV;
1101
1102 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1103 if (obj == NULL)
1104 return -EBADF;
1105
1106 offset = args->offset;
1107
1108 down_write(&current->mm->mmap_sem);
1109 addr = do_mmap(obj->filp, 0, args->size,
1110 PROT_READ | PROT_WRITE, MAP_SHARED,
1111 args->offset);
1112 up_write(&current->mm->mmap_sem);
1113 mutex_lock(&dev->struct_mutex);
1114 drm_gem_object_unreference(obj);
1115 mutex_unlock(&dev->struct_mutex);
1116 if (IS_ERR((void *)addr))
1117 return addr;
1118
1119 args->addr_ptr = (uint64_t) addr;
1120
1121 return 0;
1122}
1123
Jesse Barnesde151cf2008-11-12 10:03:55 -08001124/**
1125 * i915_gem_fault - fault a page into the GTT
1126 * vma: VMA in question
1127 * vmf: fault info
1128 *
1129 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1130 * from userspace. The fault handler takes care of binding the object to
1131 * the GTT (if needed), allocating and programming a fence register (again,
1132 * only if needed based on whether the old reg is still valid or the object
1133 * is tiled) and inserting a new PTE into the faulting process.
1134 *
1135 * Note that the faulting process may involve evicting existing objects
1136 * from the GTT and/or fence registers to make room. So performance may
1137 * suffer if the GTT working set is large or there are few fence registers
1138 * left.
1139 */
1140int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1141{
1142 struct drm_gem_object *obj = vma->vm_private_data;
1143 struct drm_device *dev = obj->dev;
1144 struct drm_i915_private *dev_priv = dev->dev_private;
1145 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1146 pgoff_t page_offset;
1147 unsigned long pfn;
1148 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001149 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001150
1151 /* We don't use vmf->pgoff since that has the fake offset */
1152 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1153 PAGE_SHIFT;
1154
1155 /* Now bind it into the GTT if needed */
1156 mutex_lock(&dev->struct_mutex);
1157 if (!obj_priv->gtt_space) {
1158 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1159 if (ret) {
1160 mutex_unlock(&dev->struct_mutex);
1161 return VM_FAULT_SIGBUS;
1162 }
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001163
1164 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1165 if (ret) {
1166 mutex_unlock(&dev->struct_mutex);
1167 return VM_FAULT_SIGBUS;
1168 }
1169
Jesse Barnes14b60392009-05-20 16:47:08 -04001170 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001171 }
1172
1173 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001174 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001175 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001176 if (ret) {
1177 mutex_unlock(&dev->struct_mutex);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001178 return VM_FAULT_SIGBUS;
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001179 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001180 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001181
1182 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1183 page_offset;
1184
1185 /* Finally, remap it using the new GTT offset */
1186 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1187
1188 mutex_unlock(&dev->struct_mutex);
1189
1190 switch (ret) {
1191 case -ENOMEM:
1192 case -EAGAIN:
1193 return VM_FAULT_OOM;
1194 case -EFAULT:
Jesse Barnes959b8872009-03-20 14:16:33 -07001195 case -EINVAL:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001196 return VM_FAULT_SIGBUS;
1197 default:
1198 return VM_FAULT_NOPAGE;
1199 }
1200}
1201
1202/**
1203 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1204 * @obj: obj in question
1205 *
1206 * GEM memory mapping works by handing back to userspace a fake mmap offset
1207 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1208 * up the object based on the offset and sets up the various memory mapping
1209 * structures.
1210 *
1211 * This routine allocates and attaches a fake offset for @obj.
1212 */
1213static int
1214i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1215{
1216 struct drm_device *dev = obj->dev;
1217 struct drm_gem_mm *mm = dev->mm_private;
1218 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1219 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001220 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001221 int ret = 0;
1222
1223 /* Set the object up for mmap'ing */
1224 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001225 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001226 if (!list->map)
1227 return -ENOMEM;
1228
1229 map = list->map;
1230 map->type = _DRM_GEM;
1231 map->size = obj->size;
1232 map->handle = obj;
1233
1234 /* Get a DRM GEM mmap offset allocated... */
1235 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1236 obj->size / PAGE_SIZE, 0, 0);
1237 if (!list->file_offset_node) {
1238 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1239 ret = -ENOMEM;
1240 goto out_free_list;
1241 }
1242
1243 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1244 obj->size / PAGE_SIZE, 0);
1245 if (!list->file_offset_node) {
1246 ret = -ENOMEM;
1247 goto out_free_list;
1248 }
1249
1250 list->hash.key = list->file_offset_node->start;
1251 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1252 DRM_ERROR("failed to add to map hash\n");
1253 goto out_free_mm;
1254 }
1255
1256 /* By now we should be all set, any drm_mmap request on the offset
1257 * below will get to our mmap & fault handler */
1258 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1259
1260 return 0;
1261
1262out_free_mm:
1263 drm_mm_put_block(list->file_offset_node);
1264out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001265 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001266
1267 return ret;
1268}
1269
Chris Wilson901782b2009-07-10 08:18:50 +01001270/**
1271 * i915_gem_release_mmap - remove physical page mappings
1272 * @obj: obj in question
1273 *
1274 * Preserve the reservation of the mmaping with the DRM core code, but
1275 * relinquish ownership of the pages back to the system.
1276 *
1277 * It is vital that we remove the page mapping if we have mapped a tiled
1278 * object through the GTT and then lose the fence register due to
1279 * resource pressure. Similarly if the object has been moved out of the
1280 * aperture, than pages mapped into userspace must be revoked. Removing the
1281 * mapping will then trigger a page fault on the next user access, allowing
1282 * fixup by i915_gem_fault().
1283 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001284void
Chris Wilson901782b2009-07-10 08:18:50 +01001285i915_gem_release_mmap(struct drm_gem_object *obj)
1286{
1287 struct drm_device *dev = obj->dev;
1288 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1289
1290 if (dev->dev_mapping)
1291 unmap_mapping_range(dev->dev_mapping,
1292 obj_priv->mmap_offset, obj->size, 1);
1293}
1294
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001295static void
1296i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1297{
1298 struct drm_device *dev = obj->dev;
1299 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1300 struct drm_gem_mm *mm = dev->mm_private;
1301 struct drm_map_list *list;
1302
1303 list = &obj->map_list;
1304 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1305
1306 if (list->file_offset_node) {
1307 drm_mm_put_block(list->file_offset_node);
1308 list->file_offset_node = NULL;
1309 }
1310
1311 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001312 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001313 list->map = NULL;
1314 }
1315
1316 obj_priv->mmap_offset = 0;
1317}
1318
Jesse Barnesde151cf2008-11-12 10:03:55 -08001319/**
1320 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1321 * @obj: object to check
1322 *
1323 * Return the required GTT alignment for an object, taking into account
1324 * potential fence register mapping if needed.
1325 */
1326static uint32_t
1327i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1328{
1329 struct drm_device *dev = obj->dev;
1330 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1331 int start, i;
1332
1333 /*
1334 * Minimum alignment is 4k (GTT page size), but might be greater
1335 * if a fence register is needed for the object.
1336 */
1337 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1338 return 4096;
1339
1340 /*
1341 * Previous chips need to be aligned to the size of the smallest
1342 * fence register that can contain the object.
1343 */
1344 if (IS_I9XX(dev))
1345 start = 1024*1024;
1346 else
1347 start = 512*1024;
1348
1349 for (i = start; i < obj->size; i <<= 1)
1350 ;
1351
1352 return i;
1353}
1354
1355/**
1356 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1357 * @dev: DRM device
1358 * @data: GTT mapping ioctl data
1359 * @file_priv: GEM object info
1360 *
1361 * Simply returns the fake offset to userspace so it can mmap it.
1362 * The mmap call will end up in drm_gem_mmap(), which will set things
1363 * up so we can get faults in the handler above.
1364 *
1365 * The fault handler will take care of binding the object into the GTT
1366 * (since it may have been evicted to make room for something), allocating
1367 * a fence register, and mapping the appropriate aperture address into
1368 * userspace.
1369 */
1370int
1371i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1372 struct drm_file *file_priv)
1373{
1374 struct drm_i915_gem_mmap_gtt *args = data;
1375 struct drm_i915_private *dev_priv = dev->dev_private;
1376 struct drm_gem_object *obj;
1377 struct drm_i915_gem_object *obj_priv;
1378 int ret;
1379
1380 if (!(dev->driver->driver_features & DRIVER_GEM))
1381 return -ENODEV;
1382
1383 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1384 if (obj == NULL)
1385 return -EBADF;
1386
1387 mutex_lock(&dev->struct_mutex);
1388
1389 obj_priv = obj->driver_private;
1390
1391 if (!obj_priv->mmap_offset) {
1392 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001393 if (ret) {
1394 drm_gem_object_unreference(obj);
1395 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001396 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001397 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001398 }
1399
1400 args->offset = obj_priv->mmap_offset;
1401
1402 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
1403
1404 /* Make sure the alignment is correct for fence regs etc */
1405 if (obj_priv->agp_mem &&
1406 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
1407 drm_gem_object_unreference(obj);
1408 mutex_unlock(&dev->struct_mutex);
1409 return -EINVAL;
1410 }
1411
1412 /*
1413 * Pull it into the GTT so that we have a page list (makes the
1414 * initial fault faster and any subsequent flushing possible).
1415 */
1416 if (!obj_priv->agp_mem) {
1417 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1418 if (ret) {
1419 drm_gem_object_unreference(obj);
1420 mutex_unlock(&dev->struct_mutex);
1421 return ret;
1422 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001423 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001424 }
1425
1426 drm_gem_object_unreference(obj);
1427 mutex_unlock(&dev->struct_mutex);
1428
1429 return 0;
1430}
1431
Ben Gamari6911a9b2009-04-02 11:24:54 -07001432void
Eric Anholt856fa192009-03-19 14:10:50 -07001433i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001434{
1435 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1436 int page_count = obj->size / PAGE_SIZE;
1437 int i;
1438
Eric Anholt856fa192009-03-19 14:10:50 -07001439 BUG_ON(obj_priv->pages_refcount == 0);
1440
1441 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001442 return;
1443
Eric Anholt280b7132009-03-12 16:56:27 -07001444 if (obj_priv->tiling_mode != I915_TILING_NONE)
1445 i915_gem_object_save_bit_17_swizzle(obj);
1446
Eric Anholt673a3942008-07-30 12:06:12 -07001447 for (i = 0; i < page_count; i++)
Eric Anholt856fa192009-03-19 14:10:50 -07001448 if (obj_priv->pages[i] != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07001449 if (obj_priv->dirty)
Eric Anholt856fa192009-03-19 14:10:50 -07001450 set_page_dirty(obj_priv->pages[i]);
1451 mark_page_accessed(obj_priv->pages[i]);
1452 page_cache_release(obj_priv->pages[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07001453 }
1454 obj_priv->dirty = 0;
1455
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001456 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001457 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001458}
1459
1460static void
Eric Anholtce44b0e2008-11-06 16:00:31 -08001461i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001462{
1463 struct drm_device *dev = obj->dev;
1464 drm_i915_private_t *dev_priv = dev->dev_private;
1465 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1466
1467 /* Add a reference if we're newly entering the active list. */
1468 if (!obj_priv->active) {
1469 drm_gem_object_reference(obj);
1470 obj_priv->active = 1;
1471 }
1472 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001473 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001474 list_move_tail(&obj_priv->list,
1475 &dev_priv->mm.active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001476 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001477 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001478}
1479
Eric Anholtce44b0e2008-11-06 16:00:31 -08001480static void
1481i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1482{
1483 struct drm_device *dev = obj->dev;
1484 drm_i915_private_t *dev_priv = dev->dev_private;
1485 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1486
1487 BUG_ON(!obj_priv->active);
1488 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1489 obj_priv->last_rendering_seqno = 0;
1490}
Eric Anholt673a3942008-07-30 12:06:12 -07001491
1492static void
1493i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1494{
1495 struct drm_device *dev = obj->dev;
1496 drm_i915_private_t *dev_priv = dev->dev_private;
1497 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1498
1499 i915_verify_inactive(dev, __FILE__, __LINE__);
1500 if (obj_priv->pin_count != 0)
1501 list_del_init(&obj_priv->list);
1502 else
1503 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1504
Eric Anholtce44b0e2008-11-06 16:00:31 -08001505 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001506 if (obj_priv->active) {
1507 obj_priv->active = 0;
1508 drm_gem_object_unreference(obj);
1509 }
1510 i915_verify_inactive(dev, __FILE__, __LINE__);
1511}
1512
1513/**
1514 * Creates a new sequence number, emitting a write of it to the status page
1515 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1516 *
1517 * Must be called with struct_lock held.
1518 *
1519 * Returned sequence numbers are nonzero on success.
1520 */
1521static uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001522i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
1523 uint32_t flush_domains)
Eric Anholt673a3942008-07-30 12:06:12 -07001524{
1525 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001526 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001527 struct drm_i915_gem_request *request;
1528 uint32_t seqno;
1529 int was_empty;
1530 RING_LOCALS;
1531
Eric Anholtb9624422009-06-03 07:27:35 +00001532 if (file_priv != NULL)
1533 i915_file_priv = file_priv->driver_priv;
1534
Eric Anholt9a298b22009-03-24 12:23:04 -07001535 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001536 if (request == NULL)
1537 return 0;
1538
1539 /* Grab the seqno we're going to make this request be, and bump the
1540 * next (skipping 0 so it can be the reserved no-seqno value).
1541 */
1542 seqno = dev_priv->mm.next_gem_seqno;
1543 dev_priv->mm.next_gem_seqno++;
1544 if (dev_priv->mm.next_gem_seqno == 0)
1545 dev_priv->mm.next_gem_seqno++;
1546
1547 BEGIN_LP_RING(4);
1548 OUT_RING(MI_STORE_DWORD_INDEX);
1549 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1550 OUT_RING(seqno);
1551
1552 OUT_RING(MI_USER_INTERRUPT);
1553 ADVANCE_LP_RING();
1554
1555 DRM_DEBUG("%d\n", seqno);
1556
1557 request->seqno = seqno;
1558 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -07001559 was_empty = list_empty(&dev_priv->mm.request_list);
1560 list_add_tail(&request->list, &dev_priv->mm.request_list);
Eric Anholtb9624422009-06-03 07:27:35 +00001561 if (i915_file_priv) {
1562 list_add_tail(&request->client_list,
1563 &i915_file_priv->mm.request_list);
1564 } else {
1565 INIT_LIST_HEAD(&request->client_list);
1566 }
Eric Anholt673a3942008-07-30 12:06:12 -07001567
Eric Anholtce44b0e2008-11-06 16:00:31 -08001568 /* Associate any objects on the flushing list matching the write
1569 * domain we're flushing with our flush.
1570 */
1571 if (flush_domains != 0) {
1572 struct drm_i915_gem_object *obj_priv, *next;
1573
1574 list_for_each_entry_safe(obj_priv, next,
1575 &dev_priv->mm.flushing_list, list) {
1576 struct drm_gem_object *obj = obj_priv->obj;
1577
1578 if ((obj->write_domain & flush_domains) ==
1579 obj->write_domain) {
1580 obj->write_domain = 0;
1581 i915_gem_object_move_to_active(obj, seqno);
1582 }
1583 }
1584
1585 }
1586
Keith Packard6dbe2772008-10-14 21:41:13 -07001587 if (was_empty && !dev_priv->mm.suspended)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001588 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001589 return seqno;
1590}
1591
1592/**
1593 * Command execution barrier
1594 *
1595 * Ensures that all commands in the ring are finished
1596 * before signalling the CPU
1597 */
Eric Anholt3043c602008-10-02 12:24:47 -07001598static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -07001599i915_retire_commands(struct drm_device *dev)
1600{
1601 drm_i915_private_t *dev_priv = dev->dev_private;
1602 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1603 uint32_t flush_domains = 0;
1604 RING_LOCALS;
1605
1606 /* The sampler always gets flushed on i965 (sigh) */
1607 if (IS_I965G(dev))
1608 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1609 BEGIN_LP_RING(2);
1610 OUT_RING(cmd);
1611 OUT_RING(0); /* noop */
1612 ADVANCE_LP_RING();
1613 return flush_domains;
1614}
1615
1616/**
1617 * Moves buffers associated only with the given active seqno from the active
1618 * to inactive list, potentially freeing them.
1619 */
1620static void
1621i915_gem_retire_request(struct drm_device *dev,
1622 struct drm_i915_gem_request *request)
1623{
1624 drm_i915_private_t *dev_priv = dev->dev_private;
1625
1626 /* Move any buffers on the active list that are no longer referenced
1627 * by the ringbuffer to the flushing/inactive lists as appropriate.
1628 */
Carl Worth5e118f42009-03-20 11:54:25 -07001629 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001630 while (!list_empty(&dev_priv->mm.active_list)) {
1631 struct drm_gem_object *obj;
1632 struct drm_i915_gem_object *obj_priv;
1633
1634 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1635 struct drm_i915_gem_object,
1636 list);
1637 obj = obj_priv->obj;
1638
1639 /* If the seqno being retired doesn't match the oldest in the
1640 * list, then the oldest in the list must still be newer than
1641 * this seqno.
1642 */
1643 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001644 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001645
Eric Anholt673a3942008-07-30 12:06:12 -07001646#if WATCH_LRU
1647 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1648 __func__, request->seqno, obj);
1649#endif
1650
Eric Anholtce44b0e2008-11-06 16:00:31 -08001651 if (obj->write_domain != 0)
1652 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001653 else {
1654 /* Take a reference on the object so it won't be
1655 * freed while the spinlock is held. The list
1656 * protection for this spinlock is safe when breaking
1657 * the lock like this since the next thing we do
1658 * is just get the head of the list again.
1659 */
1660 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001661 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001662 spin_unlock(&dev_priv->mm.active_list_lock);
1663 drm_gem_object_unreference(obj);
1664 spin_lock(&dev_priv->mm.active_list_lock);
1665 }
Eric Anholt673a3942008-07-30 12:06:12 -07001666 }
Carl Worth5e118f42009-03-20 11:54:25 -07001667out:
1668 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001669}
1670
1671/**
1672 * Returns true if seq1 is later than seq2.
1673 */
1674static int
1675i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1676{
1677 return (int32_t)(seq1 - seq2) >= 0;
1678}
1679
1680uint32_t
1681i915_get_gem_seqno(struct drm_device *dev)
1682{
1683 drm_i915_private_t *dev_priv = dev->dev_private;
1684
1685 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1686}
1687
1688/**
1689 * This function clears the request list as sequence numbers are passed.
1690 */
1691void
1692i915_gem_retire_requests(struct drm_device *dev)
1693{
1694 drm_i915_private_t *dev_priv = dev->dev_private;
1695 uint32_t seqno;
1696
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001697 if (!dev_priv->hw_status_page)
1698 return;
1699
Eric Anholt673a3942008-07-30 12:06:12 -07001700 seqno = i915_get_gem_seqno(dev);
1701
1702 while (!list_empty(&dev_priv->mm.request_list)) {
1703 struct drm_i915_gem_request *request;
1704 uint32_t retiring_seqno;
1705
1706 request = list_first_entry(&dev_priv->mm.request_list,
1707 struct drm_i915_gem_request,
1708 list);
1709 retiring_seqno = request->seqno;
1710
1711 if (i915_seqno_passed(seqno, retiring_seqno) ||
1712 dev_priv->mm.wedged) {
1713 i915_gem_retire_request(dev, request);
1714
1715 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001716 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001717 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001718 } else
1719 break;
1720 }
1721}
1722
1723void
1724i915_gem_retire_work_handler(struct work_struct *work)
1725{
1726 drm_i915_private_t *dev_priv;
1727 struct drm_device *dev;
1728
1729 dev_priv = container_of(work, drm_i915_private_t,
1730 mm.retire_work.work);
1731 dev = dev_priv->dev;
1732
1733 mutex_lock(&dev->struct_mutex);
1734 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001735 if (!dev_priv->mm.suspended &&
1736 !list_empty(&dev_priv->mm.request_list))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001737 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001738 mutex_unlock(&dev->struct_mutex);
1739}
1740
1741/**
1742 * Waits for a sequence number to be signaled, and cleans up the
1743 * request and object lists appropriately for that event.
1744 */
Eric Anholt3043c602008-10-02 12:24:47 -07001745static int
Eric Anholt673a3942008-07-30 12:06:12 -07001746i915_wait_request(struct drm_device *dev, uint32_t seqno)
1747{
1748 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001749 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001750 int ret = 0;
1751
1752 BUG_ON(seqno == 0);
1753
1754 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001755 if (IS_IGDNG(dev))
1756 ier = I915_READ(DEIER) | I915_READ(GTIER);
1757 else
1758 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001759 if (!ier) {
1760 DRM_ERROR("something (likely vbetool) disabled "
1761 "interrupts, re-enabling\n");
1762 i915_driver_irq_preinstall(dev);
1763 i915_driver_irq_postinstall(dev);
1764 }
1765
Eric Anholt673a3942008-07-30 12:06:12 -07001766 dev_priv->mm.waiting_gem_seqno = seqno;
1767 i915_user_irq_get(dev);
1768 ret = wait_event_interruptible(dev_priv->irq_queue,
1769 i915_seqno_passed(i915_get_gem_seqno(dev),
1770 seqno) ||
1771 dev_priv->mm.wedged);
1772 i915_user_irq_put(dev);
1773 dev_priv->mm.waiting_gem_seqno = 0;
1774 }
1775 if (dev_priv->mm.wedged)
1776 ret = -EIO;
1777
1778 if (ret && ret != -ERESTARTSYS)
1779 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1780 __func__, ret, seqno, i915_get_gem_seqno(dev));
1781
1782 /* Directly dispatch request retiring. While we have the work queue
1783 * to handle this, the waiter on a request often wants an associated
1784 * buffer to have made it to the inactive list, and we would need
1785 * a separate wait queue to handle that.
1786 */
1787 if (ret == 0)
1788 i915_gem_retire_requests(dev);
1789
1790 return ret;
1791}
1792
1793static void
1794i915_gem_flush(struct drm_device *dev,
1795 uint32_t invalidate_domains,
1796 uint32_t flush_domains)
1797{
1798 drm_i915_private_t *dev_priv = dev->dev_private;
1799 uint32_t cmd;
1800 RING_LOCALS;
1801
1802#if WATCH_EXEC
1803 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1804 invalidate_domains, flush_domains);
1805#endif
1806
1807 if (flush_domains & I915_GEM_DOMAIN_CPU)
1808 drm_agp_chipset_flush(dev);
1809
Chris Wilson21d509e2009-06-06 09:46:02 +01001810 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
Eric Anholt673a3942008-07-30 12:06:12 -07001811 /*
1812 * read/write caches:
1813 *
1814 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1815 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1816 * also flushed at 2d versus 3d pipeline switches.
1817 *
1818 * read-only caches:
1819 *
1820 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1821 * MI_READ_FLUSH is set, and is always flushed on 965.
1822 *
1823 * I915_GEM_DOMAIN_COMMAND may not exist?
1824 *
1825 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1826 * invalidated when MI_EXE_FLUSH is set.
1827 *
1828 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1829 * invalidated with every MI_FLUSH.
1830 *
1831 * TLBs:
1832 *
1833 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1834 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1835 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1836 * are flushed at any MI_FLUSH.
1837 */
1838
1839 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1840 if ((invalidate_domains|flush_domains) &
1841 I915_GEM_DOMAIN_RENDER)
1842 cmd &= ~MI_NO_WRITE_FLUSH;
1843 if (!IS_I965G(dev)) {
1844 /*
1845 * On the 965, the sampler cache always gets flushed
1846 * and this bit is reserved.
1847 */
1848 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1849 cmd |= MI_READ_FLUSH;
1850 }
1851 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1852 cmd |= MI_EXE_FLUSH;
1853
1854#if WATCH_EXEC
1855 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1856#endif
1857 BEGIN_LP_RING(2);
1858 OUT_RING(cmd);
1859 OUT_RING(0); /* noop */
1860 ADVANCE_LP_RING();
1861 }
1862}
1863
1864/**
1865 * Ensures that all rendering to the object has completed and the object is
1866 * safe to unbind from the GTT or access from the CPU.
1867 */
1868static int
1869i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1870{
1871 struct drm_device *dev = obj->dev;
1872 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1873 int ret;
1874
Eric Anholte47c68e2008-11-14 13:35:19 -08001875 /* This function only exists to support waiting for existing rendering,
1876 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001877 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001878 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001879
1880 /* If there is rendering queued on the buffer being evicted, wait for
1881 * it.
1882 */
1883 if (obj_priv->active) {
1884#if WATCH_BUF
1885 DRM_INFO("%s: object %p wait for seqno %08x\n",
1886 __func__, obj, obj_priv->last_rendering_seqno);
1887#endif
1888 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1889 if (ret != 0)
1890 return ret;
1891 }
1892
1893 return 0;
1894}
1895
1896/**
1897 * Unbinds an object from the GTT aperture.
1898 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001899int
Eric Anholt673a3942008-07-30 12:06:12 -07001900i915_gem_object_unbind(struct drm_gem_object *obj)
1901{
1902 struct drm_device *dev = obj->dev;
1903 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1904 int ret = 0;
1905
1906#if WATCH_BUF
1907 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1908 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1909#endif
1910 if (obj_priv->gtt_space == NULL)
1911 return 0;
1912
1913 if (obj_priv->pin_count != 0) {
1914 DRM_ERROR("Attempting to unbind pinned buffer\n");
1915 return -EINVAL;
1916 }
1917
Eric Anholt5323fd02009-09-09 11:50:45 -07001918 /* blow away mappings if mapped through GTT */
1919 i915_gem_release_mmap(obj);
1920
1921 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1922 i915_gem_clear_fence_reg(obj);
1923
Eric Anholt673a3942008-07-30 12:06:12 -07001924 /* Move the object to the CPU domain to ensure that
1925 * any possible CPU writes while it's not in the GTT
1926 * are flushed when we go to remap it. This will
1927 * also ensure that all pending GPU writes are finished
1928 * before we unbind.
1929 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001930 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001931 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001932 if (ret != -ERESTARTSYS)
1933 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001934 return ret;
1935 }
1936
Eric Anholt5323fd02009-09-09 11:50:45 -07001937 BUG_ON(obj_priv->active);
1938
Eric Anholt673a3942008-07-30 12:06:12 -07001939 if (obj_priv->agp_mem != NULL) {
1940 drm_unbind_agp(obj_priv->agp_mem);
1941 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1942 obj_priv->agp_mem = NULL;
1943 }
1944
Eric Anholt856fa192009-03-19 14:10:50 -07001945 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001946
1947 if (obj_priv->gtt_space) {
1948 atomic_dec(&dev->gtt_count);
1949 atomic_sub(obj->size, &dev->gtt_memory);
1950
1951 drm_mm_put_block(obj_priv->gtt_space);
1952 obj_priv->gtt_space = NULL;
1953 }
1954
1955 /* Remove ourselves from the LRU list if present. */
1956 if (!list_empty(&obj_priv->list))
1957 list_del_init(&obj_priv->list);
1958
1959 return 0;
1960}
1961
1962static int
1963i915_gem_evict_something(struct drm_device *dev)
1964{
1965 drm_i915_private_t *dev_priv = dev->dev_private;
1966 struct drm_gem_object *obj;
1967 struct drm_i915_gem_object *obj_priv;
1968 int ret = 0;
1969
1970 for (;;) {
1971 /* If there's an inactive buffer available now, grab it
1972 * and be done.
1973 */
1974 if (!list_empty(&dev_priv->mm.inactive_list)) {
1975 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1976 struct drm_i915_gem_object,
1977 list);
1978 obj = obj_priv->obj;
1979 BUG_ON(obj_priv->pin_count != 0);
1980#if WATCH_LRU
1981 DRM_INFO("%s: evicting %p\n", __func__, obj);
1982#endif
1983 BUG_ON(obj_priv->active);
1984
1985 /* Wait on the rendering and unbind the buffer. */
1986 ret = i915_gem_object_unbind(obj);
1987 break;
1988 }
1989
1990 /* If we didn't get anything, but the ring is still processing
1991 * things, wait for one of those things to finish and hopefully
1992 * leave us a buffer to evict.
1993 */
1994 if (!list_empty(&dev_priv->mm.request_list)) {
1995 struct drm_i915_gem_request *request;
1996
1997 request = list_first_entry(&dev_priv->mm.request_list,
1998 struct drm_i915_gem_request,
1999 list);
2000
2001 ret = i915_wait_request(dev, request->seqno);
2002 if (ret)
2003 break;
2004
2005 /* if waiting caused an object to become inactive,
2006 * then loop around and wait for it. Otherwise, we
2007 * assume that waiting freed and unbound something,
2008 * so there should now be some space in the GTT
2009 */
2010 if (!list_empty(&dev_priv->mm.inactive_list))
2011 continue;
2012 break;
2013 }
2014
2015 /* If we didn't have anything on the request list but there
2016 * are buffers awaiting a flush, emit one and try again.
2017 * When we wait on it, those buffers waiting for that flush
2018 * will get moved to inactive.
2019 */
2020 if (!list_empty(&dev_priv->mm.flushing_list)) {
2021 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
2022 struct drm_i915_gem_object,
2023 list);
2024 obj = obj_priv->obj;
2025
2026 i915_gem_flush(dev,
2027 obj->write_domain,
2028 obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002029 i915_add_request(dev, NULL, obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002030
2031 obj = NULL;
2032 continue;
2033 }
2034
2035 DRM_ERROR("inactive empty %d request empty %d "
2036 "flushing empty %d\n",
2037 list_empty(&dev_priv->mm.inactive_list),
2038 list_empty(&dev_priv->mm.request_list),
2039 list_empty(&dev_priv->mm.flushing_list));
2040 /* If we didn't do any of the above, there's nothing to be done
2041 * and we just can't fit it in.
2042 */
Chris Wilson2939e1f2009-06-06 09:46:03 +01002043 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002044 }
2045 return ret;
2046}
2047
2048static int
Keith Packardac94a962008-11-20 23:30:27 -08002049i915_gem_evict_everything(struct drm_device *dev)
2050{
2051 int ret;
2052
2053 for (;;) {
2054 ret = i915_gem_evict_something(dev);
2055 if (ret != 0)
2056 break;
2057 }
Chris Wilson2939e1f2009-06-06 09:46:03 +01002058 if (ret == -ENOSPC)
Owain Ainsworth15c35332008-12-06 20:42:20 -08002059 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08002060 return ret;
2061}
2062
Ben Gamari6911a9b2009-04-02 11:24:54 -07002063int
Eric Anholt856fa192009-03-19 14:10:50 -07002064i915_gem_object_get_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002065{
2066 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2067 int page_count, i;
2068 struct address_space *mapping;
2069 struct inode *inode;
2070 struct page *page;
2071 int ret;
2072
Eric Anholt856fa192009-03-19 14:10:50 -07002073 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002074 return 0;
2075
2076 /* Get the list of pages out of our struct file. They'll be pinned
2077 * at this point until we release them.
2078 */
2079 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002080 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002081 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002082 if (obj_priv->pages == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002083 DRM_ERROR("Faled to allocate page list\n");
Eric Anholt856fa192009-03-19 14:10:50 -07002084 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002085 return -ENOMEM;
2086 }
2087
2088 inode = obj->filp->f_path.dentry->d_inode;
2089 mapping = inode->i_mapping;
2090 for (i = 0; i < page_count; i++) {
2091 page = read_mapping_page(mapping, i, NULL);
2092 if (IS_ERR(page)) {
2093 ret = PTR_ERR(page);
2094 DRM_ERROR("read_mapping_page failed: %d\n", ret);
Eric Anholt856fa192009-03-19 14:10:50 -07002095 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002096 return ret;
2097 }
Eric Anholt856fa192009-03-19 14:10:50 -07002098 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002099 }
Eric Anholt280b7132009-03-12 16:56:27 -07002100
2101 if (obj_priv->tiling_mode != I915_TILING_NONE)
2102 i915_gem_object_do_bit_17_swizzle(obj);
2103
Eric Anholt673a3942008-07-30 12:06:12 -07002104 return 0;
2105}
2106
Jesse Barnesde151cf2008-11-12 10:03:55 -08002107static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2108{
2109 struct drm_gem_object *obj = reg->obj;
2110 struct drm_device *dev = obj->dev;
2111 drm_i915_private_t *dev_priv = dev->dev_private;
2112 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2113 int regnum = obj_priv->fence_reg;
2114 uint64_t val;
2115
2116 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2117 0xfffff000) << 32;
2118 val |= obj_priv->gtt_offset & 0xfffff000;
2119 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2120 if (obj_priv->tiling_mode == I915_TILING_Y)
2121 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2122 val |= I965_FENCE_REG_VALID;
2123
2124 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2125}
2126
2127static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2128{
2129 struct drm_gem_object *obj = reg->obj;
2130 struct drm_device *dev = obj->dev;
2131 drm_i915_private_t *dev_priv = dev->dev_private;
2132 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2133 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002134 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002135 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002136 uint32_t pitch_val;
2137
2138 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2139 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002140 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002141 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002142 return;
2143 }
2144
Jesse Barnes0f973f22009-01-26 17:10:45 -08002145 if (obj_priv->tiling_mode == I915_TILING_Y &&
2146 HAS_128_BYTE_Y_TILING(dev))
2147 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002148 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002149 tile_width = 512;
2150
2151 /* Note: pitch better be a power of two tile widths */
2152 pitch_val = obj_priv->stride / tile_width;
2153 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002154
2155 val = obj_priv->gtt_offset;
2156 if (obj_priv->tiling_mode == I915_TILING_Y)
2157 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2158 val |= I915_FENCE_SIZE_BITS(obj->size);
2159 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2160 val |= I830_FENCE_REG_VALID;
2161
Eric Anholtdc529a42009-03-10 22:34:49 -07002162 if (regnum < 8)
2163 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2164 else
2165 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2166 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002167}
2168
2169static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2170{
2171 struct drm_gem_object *obj = reg->obj;
2172 struct drm_device *dev = obj->dev;
2173 drm_i915_private_t *dev_priv = dev->dev_private;
2174 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2175 int regnum = obj_priv->fence_reg;
2176 uint32_t val;
2177 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002178 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002179
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002180 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002181 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002182 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002183 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002184 return;
2185 }
2186
Eric Anholte76a16d2009-05-26 17:44:56 -07002187 pitch_val = obj_priv->stride / 128;
2188 pitch_val = ffs(pitch_val) - 1;
2189 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2190
Jesse Barnesde151cf2008-11-12 10:03:55 -08002191 val = obj_priv->gtt_offset;
2192 if (obj_priv->tiling_mode == I915_TILING_Y)
2193 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002194 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2195 WARN_ON(fence_size_bits & ~0x00000f00);
2196 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002197 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2198 val |= I830_FENCE_REG_VALID;
2199
2200 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002201}
2202
2203/**
2204 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2205 * @obj: object to map through a fence reg
2206 *
2207 * When mapping objects through the GTT, userspace wants to be able to write
2208 * to them without having to worry about swizzling if the object is tiled.
2209 *
2210 * This function walks the fence regs looking for a free one for @obj,
2211 * stealing one if it can't find any.
2212 *
2213 * It then sets up the reg based on the object's properties: address, pitch
2214 * and tiling format.
2215 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002216int
2217i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002218{
2219 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002220 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002221 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2222 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002223 struct drm_i915_gem_object *old_obj_priv = NULL;
2224 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002225
Eric Anholta09ba7f2009-08-29 12:49:51 -07002226 /* Just update our place in the LRU if our fence is getting used. */
2227 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
2228 list_move_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2229 return 0;
2230 }
2231
Jesse Barnesde151cf2008-11-12 10:03:55 -08002232 switch (obj_priv->tiling_mode) {
2233 case I915_TILING_NONE:
2234 WARN(1, "allocating a fence for non-tiled object?\n");
2235 break;
2236 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002237 if (!obj_priv->stride)
2238 return -EINVAL;
2239 WARN((obj_priv->stride & (512 - 1)),
2240 "object 0x%08x is X tiled but has non-512B pitch\n",
2241 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002242 break;
2243 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002244 if (!obj_priv->stride)
2245 return -EINVAL;
2246 WARN((obj_priv->stride & (128 - 1)),
2247 "object 0x%08x is Y tiled but has non-128B pitch\n",
2248 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002249 break;
2250 }
2251
2252 /* First try to find a free reg */
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002253 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002254 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2255 reg = &dev_priv->fence_regs[i];
2256 if (!reg->obj)
2257 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002258
2259 old_obj_priv = reg->obj->driver_private;
2260 if (!old_obj_priv->pin_count)
2261 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002262 }
2263
2264 /* None available, try to steal one or wait for a user to finish */
2265 if (i == dev_priv->num_fence_regs) {
Eric Anholta09ba7f2009-08-29 12:49:51 -07002266 struct drm_gem_object *old_obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002267
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002268 if (avail == 0)
Chris Wilson2939e1f2009-06-06 09:46:03 +01002269 return -ENOSPC;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002270
Eric Anholta09ba7f2009-08-29 12:49:51 -07002271 list_for_each_entry(old_obj_priv, &dev_priv->mm.fence_list,
2272 fence_list) {
2273 old_obj = old_obj_priv->obj;
Chris Wilsond7619c42009-02-11 14:26:47 +00002274
2275 if (old_obj_priv->pin_count)
2276 continue;
2277
Eric Anholta09ba7f2009-08-29 12:49:51 -07002278 /* Take a reference, as otherwise the wait_rendering
2279 * below may cause the object to get freed out from
2280 * under us.
2281 */
2282 drm_gem_object_reference(old_obj);
2283
Chris Wilsond7619c42009-02-11 14:26:47 +00002284 /* i915 uses fences for GPU access to tiled buffers */
2285 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002286 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002287
Eric Anholta09ba7f2009-08-29 12:49:51 -07002288 /* This brings the object to the head of the LRU if it
2289 * had been written to. The only way this should
2290 * result in us waiting longer than the expected
2291 * optimal amount of time is if there was a
2292 * fence-using buffer later that was read-only.
2293 */
2294 i915_gem_object_flush_gpu_write_domain(old_obj);
2295 ret = i915_gem_object_wait_rendering(old_obj);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002296 if (ret != 0) {
2297 drm_gem_object_unreference(old_obj);
Chris Wilsond7619c42009-02-11 14:26:47 +00002298 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002299 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002300
Eric Anholta09ba7f2009-08-29 12:49:51 -07002301 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002302 }
2303
2304 /*
2305 * Zap this virtual mapping so we can set up a fence again
2306 * for this object next time we need it.
2307 */
Chris Wilson58c2fb62009-09-01 12:02:39 +01002308 i915_gem_release_mmap(old_obj);
2309
Eric Anholta09ba7f2009-08-29 12:49:51 -07002310 i = old_obj_priv->fence_reg;
Chris Wilson58c2fb62009-09-01 12:02:39 +01002311 reg = &dev_priv->fence_regs[i];
2312
Jesse Barnesde151cf2008-11-12 10:03:55 -08002313 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002314 list_del_init(&old_obj_priv->fence_list);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002315
Eric Anholta09ba7f2009-08-29 12:49:51 -07002316 drm_gem_object_unreference(old_obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317 }
2318
2319 obj_priv->fence_reg = i;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002320 list_add_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2321
Jesse Barnesde151cf2008-11-12 10:03:55 -08002322 reg->obj = obj;
2323
2324 if (IS_I965G(dev))
2325 i965_write_fence_reg(reg);
2326 else if (IS_I9XX(dev))
2327 i915_write_fence_reg(reg);
2328 else
2329 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002330
2331 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002332}
2333
2334/**
2335 * i915_gem_clear_fence_reg - clear out fence register info
2336 * @obj: object to clear
2337 *
2338 * Zeroes out the fence register itself and clears out the associated
2339 * data structures in dev_priv and obj_priv.
2340 */
2341static void
2342i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2343{
2344 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002345 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002346 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2347
2348 if (IS_I965G(dev))
2349 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002350 else {
2351 uint32_t fence_reg;
2352
2353 if (obj_priv->fence_reg < 8)
2354 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2355 else
2356 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2357 8) * 4;
2358
2359 I915_WRITE(fence_reg, 0);
2360 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002361
2362 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2363 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002364 list_del_init(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002365}
2366
Eric Anholt673a3942008-07-30 12:06:12 -07002367/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002368 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2369 * to the buffer to finish, and then resets the fence register.
2370 * @obj: tiled object holding a fence register.
2371 *
2372 * Zeroes out the fence register itself and clears out the associated
2373 * data structures in dev_priv and obj_priv.
2374 */
2375int
2376i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2377{
2378 struct drm_device *dev = obj->dev;
2379 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2380
2381 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2382 return 0;
2383
2384 /* On the i915, GPU access to tiled buffers is via a fence,
2385 * therefore we must wait for any outstanding access to complete
2386 * before clearing the fence.
2387 */
2388 if (!IS_I965G(dev)) {
2389 int ret;
2390
2391 i915_gem_object_flush_gpu_write_domain(obj);
2392 i915_gem_object_flush_gtt_write_domain(obj);
2393 ret = i915_gem_object_wait_rendering(obj);
2394 if (ret != 0)
2395 return ret;
2396 }
2397
2398 i915_gem_clear_fence_reg (obj);
2399
2400 return 0;
2401}
2402
2403/**
Eric Anholt673a3942008-07-30 12:06:12 -07002404 * Finds free space in the GTT aperture and binds the object there.
2405 */
2406static int
2407i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2408{
2409 struct drm_device *dev = obj->dev;
2410 drm_i915_private_t *dev_priv = dev->dev_private;
2411 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2412 struct drm_mm_node *free_space;
2413 int page_count, ret;
2414
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002415 if (dev_priv->mm.suspended)
2416 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002417 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002418 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002419 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002420 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2421 return -EINVAL;
2422 }
2423
2424 search_free:
2425 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2426 obj->size, alignment, 0);
2427 if (free_space != NULL) {
2428 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2429 alignment);
2430 if (obj_priv->gtt_space != NULL) {
2431 obj_priv->gtt_space->private = obj;
2432 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2433 }
2434 }
2435 if (obj_priv->gtt_space == NULL) {
Carl Worth5e118f42009-03-20 11:54:25 -07002436 bool lists_empty;
2437
Eric Anholt673a3942008-07-30 12:06:12 -07002438 /* If the gtt is empty and we're still having trouble
2439 * fitting our object in, we're out of memory.
2440 */
2441#if WATCH_LRU
2442 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2443#endif
Carl Worth5e118f42009-03-20 11:54:25 -07002444 spin_lock(&dev_priv->mm.active_list_lock);
2445 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2446 list_empty(&dev_priv->mm.flushing_list) &&
2447 list_empty(&dev_priv->mm.active_list));
2448 spin_unlock(&dev_priv->mm.active_list_lock);
2449 if (lists_empty) {
Eric Anholt673a3942008-07-30 12:06:12 -07002450 DRM_ERROR("GTT full, but LRU list empty\n");
Chris Wilson2939e1f2009-06-06 09:46:03 +01002451 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002452 }
2453
2454 ret = i915_gem_evict_something(dev);
2455 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08002456 if (ret != -ERESTARTSYS)
2457 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002458 return ret;
2459 }
2460 goto search_free;
2461 }
2462
2463#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002464 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002465 obj->size, obj_priv->gtt_offset);
2466#endif
Eric Anholt856fa192009-03-19 14:10:50 -07002467 ret = i915_gem_object_get_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002468 if (ret) {
2469 drm_mm_put_block(obj_priv->gtt_space);
2470 obj_priv->gtt_space = NULL;
2471 return ret;
2472 }
2473
2474 page_count = obj->size / PAGE_SIZE;
2475 /* Create an AGP memory structure pointing at our pages, and bind it
2476 * into the GTT.
2477 */
2478 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002479 obj_priv->pages,
Eric Anholt673a3942008-07-30 12:06:12 -07002480 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002481 obj_priv->gtt_offset,
2482 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002483 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002484 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002485 drm_mm_put_block(obj_priv->gtt_space);
2486 obj_priv->gtt_space = NULL;
2487 return -ENOMEM;
2488 }
2489 atomic_inc(&dev->gtt_count);
2490 atomic_add(obj->size, &dev->gtt_memory);
2491
2492 /* Assert that the object is not currently in any GPU domain. As it
2493 * wasn't in the GTT, there shouldn't be any way it could have been in
2494 * a GPU cache
2495 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002496 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2497 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002498
2499 return 0;
2500}
2501
2502void
2503i915_gem_clflush_object(struct drm_gem_object *obj)
2504{
2505 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2506
2507 /* If we don't have a page list set up, then we're not pinned
2508 * to GPU, and we can ignore the cache flush because it'll happen
2509 * again at bind time.
2510 */
Eric Anholt856fa192009-03-19 14:10:50 -07002511 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002512 return;
2513
Eric Anholtcfa16a02009-05-26 18:46:16 -07002514 /* XXX: The 865 in particular appears to be weird in how it handles
2515 * cache flushing. We haven't figured it out, but the
2516 * clflush+agp_chipset_flush doesn't appear to successfully get the
2517 * data visible to the PGU, while wbinvd + agp_chipset_flush does.
2518 */
2519 if (IS_I865G(obj->dev)) {
2520 wbinvd();
2521 return;
2522 }
2523
Eric Anholt856fa192009-03-19 14:10:50 -07002524 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002525}
2526
Eric Anholte47c68e2008-11-14 13:35:19 -08002527/** Flushes any GPU write domain for the object if it's dirty. */
2528static void
2529i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2530{
2531 struct drm_device *dev = obj->dev;
2532 uint32_t seqno;
2533
2534 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2535 return;
2536
2537 /* Queue the GPU write cache flushing we need. */
2538 i915_gem_flush(dev, 0, obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002539 seqno = i915_add_request(dev, NULL, obj->write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002540 obj->write_domain = 0;
2541 i915_gem_object_move_to_active(obj, seqno);
2542}
2543
2544/** Flushes the GTT write domain for the object if it's dirty. */
2545static void
2546i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2547{
2548 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2549 return;
2550
2551 /* No actual flushing is required for the GTT write domain. Writes
2552 * to it immediately go to main memory as far as we know, so there's
2553 * no chipset flush. It also doesn't land in render cache.
2554 */
2555 obj->write_domain = 0;
2556}
2557
2558/** Flushes the CPU write domain for the object if it's dirty. */
2559static void
2560i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2561{
2562 struct drm_device *dev = obj->dev;
2563
2564 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2565 return;
2566
2567 i915_gem_clflush_object(obj);
2568 drm_agp_chipset_flush(dev);
2569 obj->write_domain = 0;
2570}
2571
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002572/**
2573 * Moves a single object to the GTT read, and possibly write domain.
2574 *
2575 * This function returns when the move is complete, including waiting on
2576 * flushes to occur.
2577 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002578int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002579i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2580{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002581 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002582 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002583
Eric Anholt02354392008-11-26 13:58:13 -08002584 /* Not valid to be called on unbound objects. */
2585 if (obj_priv->gtt_space == NULL)
2586 return -EINVAL;
2587
Eric Anholte47c68e2008-11-14 13:35:19 -08002588 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002589 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002590 ret = i915_gem_object_wait_rendering(obj);
2591 if (ret != 0)
2592 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002593
2594 /* If we're writing through the GTT domain, then CPU and GPU caches
2595 * will need to be invalidated at next use.
2596 */
2597 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002598 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002599
Eric Anholte47c68e2008-11-14 13:35:19 -08002600 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002601
2602 /* It should now be out of any other write domains, and we can update
2603 * the domain values for our changes.
2604 */
2605 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2606 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002607 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002608 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002609 obj_priv->dirty = 1;
2610 }
2611
2612 return 0;
2613}
2614
2615/**
2616 * Moves a single object to the CPU read, and possibly write domain.
2617 *
2618 * This function returns when the move is complete, including waiting on
2619 * flushes to occur.
2620 */
2621static int
2622i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2623{
Eric Anholte47c68e2008-11-14 13:35:19 -08002624 int ret;
2625
2626 i915_gem_object_flush_gpu_write_domain(obj);
2627 /* Wait on any GPU rendering and flushing to occur. */
2628 ret = i915_gem_object_wait_rendering(obj);
2629 if (ret != 0)
2630 return ret;
2631
2632 i915_gem_object_flush_gtt_write_domain(obj);
2633
2634 /* If we have a partially-valid cache of the object in the CPU,
2635 * finish invalidating it and free the per-page flags.
2636 */
2637 i915_gem_object_set_to_full_cpu_read_domain(obj);
2638
2639 /* Flush the CPU cache if it's still invalid. */
2640 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2641 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002642
2643 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2644 }
2645
2646 /* It should now be out of any other write domains, and we can update
2647 * the domain values for our changes.
2648 */
2649 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2650
2651 /* If we're writing through the CPU, then the GPU read domains will
2652 * need to be invalidated at next use.
2653 */
2654 if (write) {
2655 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2656 obj->write_domain = I915_GEM_DOMAIN_CPU;
2657 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002658
2659 return 0;
2660}
2661
Eric Anholt673a3942008-07-30 12:06:12 -07002662/*
2663 * Set the next domain for the specified object. This
2664 * may not actually perform the necessary flushing/invaliding though,
2665 * as that may want to be batched with other set_domain operations
2666 *
2667 * This is (we hope) the only really tricky part of gem. The goal
2668 * is fairly simple -- track which caches hold bits of the object
2669 * and make sure they remain coherent. A few concrete examples may
2670 * help to explain how it works. For shorthand, we use the notation
2671 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2672 * a pair of read and write domain masks.
2673 *
2674 * Case 1: the batch buffer
2675 *
2676 * 1. Allocated
2677 * 2. Written by CPU
2678 * 3. Mapped to GTT
2679 * 4. Read by GPU
2680 * 5. Unmapped from GTT
2681 * 6. Freed
2682 *
2683 * Let's take these a step at a time
2684 *
2685 * 1. Allocated
2686 * Pages allocated from the kernel may still have
2687 * cache contents, so we set them to (CPU, CPU) always.
2688 * 2. Written by CPU (using pwrite)
2689 * The pwrite function calls set_domain (CPU, CPU) and
2690 * this function does nothing (as nothing changes)
2691 * 3. Mapped by GTT
2692 * This function asserts that the object is not
2693 * currently in any GPU-based read or write domains
2694 * 4. Read by GPU
2695 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2696 * As write_domain is zero, this function adds in the
2697 * current read domains (CPU+COMMAND, 0).
2698 * flush_domains is set to CPU.
2699 * invalidate_domains is set to COMMAND
2700 * clflush is run to get data out of the CPU caches
2701 * then i915_dev_set_domain calls i915_gem_flush to
2702 * emit an MI_FLUSH and drm_agp_chipset_flush
2703 * 5. Unmapped from GTT
2704 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2705 * flush_domains and invalidate_domains end up both zero
2706 * so no flushing/invalidating happens
2707 * 6. Freed
2708 * yay, done
2709 *
2710 * Case 2: The shared render buffer
2711 *
2712 * 1. Allocated
2713 * 2. Mapped to GTT
2714 * 3. Read/written by GPU
2715 * 4. set_domain to (CPU,CPU)
2716 * 5. Read/written by CPU
2717 * 6. Read/written by GPU
2718 *
2719 * 1. Allocated
2720 * Same as last example, (CPU, CPU)
2721 * 2. Mapped to GTT
2722 * Nothing changes (assertions find that it is not in the GPU)
2723 * 3. Read/written by GPU
2724 * execbuffer calls set_domain (RENDER, RENDER)
2725 * flush_domains gets CPU
2726 * invalidate_domains gets GPU
2727 * clflush (obj)
2728 * MI_FLUSH and drm_agp_chipset_flush
2729 * 4. set_domain (CPU, CPU)
2730 * flush_domains gets GPU
2731 * invalidate_domains gets CPU
2732 * wait_rendering (obj) to make sure all drawing is complete.
2733 * This will include an MI_FLUSH to get the data from GPU
2734 * to memory
2735 * clflush (obj) to invalidate the CPU cache
2736 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2737 * 5. Read/written by CPU
2738 * cache lines are loaded and dirtied
2739 * 6. Read written by GPU
2740 * Same as last GPU access
2741 *
2742 * Case 3: The constant buffer
2743 *
2744 * 1. Allocated
2745 * 2. Written by CPU
2746 * 3. Read by GPU
2747 * 4. Updated (written) by CPU again
2748 * 5. Read by GPU
2749 *
2750 * 1. Allocated
2751 * (CPU, CPU)
2752 * 2. Written by CPU
2753 * (CPU, CPU)
2754 * 3. Read by GPU
2755 * (CPU+RENDER, 0)
2756 * flush_domains = CPU
2757 * invalidate_domains = RENDER
2758 * clflush (obj)
2759 * MI_FLUSH
2760 * drm_agp_chipset_flush
2761 * 4. Updated (written) by CPU again
2762 * (CPU, CPU)
2763 * flush_domains = 0 (no previous write domain)
2764 * invalidate_domains = 0 (no new read domains)
2765 * 5. Read by GPU
2766 * (CPU+RENDER, 0)
2767 * flush_domains = CPU
2768 * invalidate_domains = RENDER
2769 * clflush (obj)
2770 * MI_FLUSH
2771 * drm_agp_chipset_flush
2772 */
Keith Packardc0d90822008-11-20 23:11:08 -08002773static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002774i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002775{
2776 struct drm_device *dev = obj->dev;
2777 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2778 uint32_t invalidate_domains = 0;
2779 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002780
Eric Anholt8b0e3782009-02-19 14:40:50 -08002781 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2782 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002783
Jesse Barnes652c3932009-08-17 13:31:43 -07002784 intel_mark_busy(dev, obj);
2785
Eric Anholt673a3942008-07-30 12:06:12 -07002786#if WATCH_BUF
2787 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2788 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002789 obj->read_domains, obj->pending_read_domains,
2790 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002791#endif
2792 /*
2793 * If the object isn't moving to a new write domain,
2794 * let the object stay in multiple read domains
2795 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002796 if (obj->pending_write_domain == 0)
2797 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002798 else
2799 obj_priv->dirty = 1;
2800
2801 /*
2802 * Flush the current write domain if
2803 * the new read domains don't match. Invalidate
2804 * any read domains which differ from the old
2805 * write domain
2806 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002807 if (obj->write_domain &&
2808 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002809 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002810 invalidate_domains |=
2811 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002812 }
2813 /*
2814 * Invalidate any read caches which may have
2815 * stale data. That is, any new read domains.
2816 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002817 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002818 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2819#if WATCH_BUF
2820 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2821 __func__, flush_domains, invalidate_domains);
2822#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002823 i915_gem_clflush_object(obj);
2824 }
2825
Eric Anholtefbeed92009-02-19 14:54:51 -08002826 /* The actual obj->write_domain will be updated with
2827 * pending_write_domain after we emit the accumulated flush for all
2828 * of our domain changes in execbuffers (which clears objects'
2829 * write_domains). So if we have a current write domain that we
2830 * aren't changing, set pending_write_domain to that.
2831 */
2832 if (flush_domains == 0 && obj->pending_write_domain == 0)
2833 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002834 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002835
2836 dev->invalidate_domains |= invalidate_domains;
2837 dev->flush_domains |= flush_domains;
2838#if WATCH_BUF
2839 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2840 __func__,
2841 obj->read_domains, obj->write_domain,
2842 dev->invalidate_domains, dev->flush_domains);
2843#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002844}
2845
2846/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002847 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002848 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002849 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2850 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2851 */
2852static void
2853i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2854{
Eric Anholte47c68e2008-11-14 13:35:19 -08002855 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2856
2857 if (!obj_priv->page_cpu_valid)
2858 return;
2859
2860 /* If we're partially in the CPU read domain, finish moving it in.
2861 */
2862 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2863 int i;
2864
2865 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2866 if (obj_priv->page_cpu_valid[i])
2867 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07002868 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08002869 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002870 }
2871
2872 /* Free the page_cpu_valid mappings which are now stale, whether
2873 * or not we've got I915_GEM_DOMAIN_CPU.
2874 */
Eric Anholt9a298b22009-03-24 12:23:04 -07002875 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08002876 obj_priv->page_cpu_valid = NULL;
2877}
2878
2879/**
2880 * Set the CPU read domain on a range of the object.
2881 *
2882 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2883 * not entirely valid. The page_cpu_valid member of the object flags which
2884 * pages have been flushed, and will be respected by
2885 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2886 * of the whole object.
2887 *
2888 * This function returns when the move is complete, including waiting on
2889 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002890 */
2891static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002892i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2893 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002894{
2895 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002896 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002897
Eric Anholte47c68e2008-11-14 13:35:19 -08002898 if (offset == 0 && size == obj->size)
2899 return i915_gem_object_set_to_cpu_domain(obj, 0);
2900
2901 i915_gem_object_flush_gpu_write_domain(obj);
2902 /* Wait on any GPU rendering and flushing to occur. */
2903 ret = i915_gem_object_wait_rendering(obj);
2904 if (ret != 0)
2905 return ret;
2906 i915_gem_object_flush_gtt_write_domain(obj);
2907
2908 /* If we're already fully in the CPU read domain, we're done. */
2909 if (obj_priv->page_cpu_valid == NULL &&
2910 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002911 return 0;
2912
Eric Anholte47c68e2008-11-14 13:35:19 -08002913 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2914 * newly adding I915_GEM_DOMAIN_CPU
2915 */
Eric Anholt673a3942008-07-30 12:06:12 -07002916 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07002917 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
2918 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08002919 if (obj_priv->page_cpu_valid == NULL)
2920 return -ENOMEM;
2921 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2922 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002923
2924 /* Flush the cache on any pages that are still invalid from the CPU's
2925 * perspective.
2926 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002927 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2928 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002929 if (obj_priv->page_cpu_valid[i])
2930 continue;
2931
Eric Anholt856fa192009-03-19 14:10:50 -07002932 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002933
2934 obj_priv->page_cpu_valid[i] = 1;
2935 }
2936
Eric Anholte47c68e2008-11-14 13:35:19 -08002937 /* It should now be out of any other write domains, and we can update
2938 * the domain values for our changes.
2939 */
2940 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2941
2942 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2943
Eric Anholt673a3942008-07-30 12:06:12 -07002944 return 0;
2945}
2946
2947/**
Eric Anholt673a3942008-07-30 12:06:12 -07002948 * Pin an object to the GTT and evaluate the relocations landing in it.
2949 */
2950static int
2951i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2952 struct drm_file *file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002953 struct drm_i915_gem_exec_object *entry,
2954 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07002955{
2956 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002957 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002958 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2959 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002960 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002961
2962 /* Choose the GTT offset for our buffer and put it there. */
2963 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2964 if (ret)
2965 return ret;
2966
2967 entry->offset = obj_priv->gtt_offset;
2968
Eric Anholt673a3942008-07-30 12:06:12 -07002969 /* Apply the relocations, using the GTT aperture to avoid cache
2970 * flushing requirements.
2971 */
2972 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002973 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002974 struct drm_gem_object *target_obj;
2975 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002976 uint32_t reloc_val, reloc_offset;
2977 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002978
Eric Anholt673a3942008-07-30 12:06:12 -07002979 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002980 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002981 if (target_obj == NULL) {
2982 i915_gem_object_unpin(obj);
2983 return -EBADF;
2984 }
2985 target_obj_priv = target_obj->driver_private;
2986
2987 /* The target buffer should have appeared before us in the
2988 * exec_object list, so it should have a GTT space bound by now.
2989 */
2990 if (target_obj_priv->gtt_space == NULL) {
2991 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002992 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002993 drm_gem_object_unreference(target_obj);
2994 i915_gem_object_unpin(obj);
2995 return -EINVAL;
2996 }
2997
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002998 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07002999 DRM_ERROR("Relocation beyond object bounds: "
3000 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003001 obj, reloc->target_handle,
3002 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003003 drm_gem_object_unreference(target_obj);
3004 i915_gem_object_unpin(obj);
3005 return -EINVAL;
3006 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003007 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003008 DRM_ERROR("Relocation not 4-byte aligned: "
3009 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003010 obj, reloc->target_handle,
3011 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003012 drm_gem_object_unreference(target_obj);
3013 i915_gem_object_unpin(obj);
3014 return -EINVAL;
3015 }
3016
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003017 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3018 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003019 DRM_ERROR("reloc with read/write CPU domains: "
3020 "obj %p target %d offset %d "
3021 "read %08x write %08x",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003022 obj, reloc->target_handle,
3023 (int) reloc->offset,
3024 reloc->read_domains,
3025 reloc->write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00003026 drm_gem_object_unreference(target_obj);
3027 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003028 return -EINVAL;
3029 }
3030
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003031 if (reloc->write_domain && target_obj->pending_write_domain &&
3032 reloc->write_domain != target_obj->pending_write_domain) {
Eric Anholt673a3942008-07-30 12:06:12 -07003033 DRM_ERROR("Write domain conflict: "
3034 "obj %p target %d offset %d "
3035 "new %08x old %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003036 obj, reloc->target_handle,
3037 (int) reloc->offset,
3038 reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003039 target_obj->pending_write_domain);
3040 drm_gem_object_unreference(target_obj);
3041 i915_gem_object_unpin(obj);
3042 return -EINVAL;
3043 }
3044
3045#if WATCH_RELOC
3046 DRM_INFO("%s: obj %p offset %08x target %d "
3047 "read %08x write %08x gtt %08x "
3048 "presumed %08x delta %08x\n",
3049 __func__,
3050 obj,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003051 (int) reloc->offset,
3052 (int) reloc->target_handle,
3053 (int) reloc->read_domains,
3054 (int) reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003055 (int) target_obj_priv->gtt_offset,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003056 (int) reloc->presumed_offset,
3057 reloc->delta);
Eric Anholt673a3942008-07-30 12:06:12 -07003058#endif
3059
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003060 target_obj->pending_read_domains |= reloc->read_domains;
3061 target_obj->pending_write_domain |= reloc->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003062
3063 /* If the relocation already has the right value in it, no
3064 * more work needs to be done.
3065 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003066 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
Eric Anholt673a3942008-07-30 12:06:12 -07003067 drm_gem_object_unreference(target_obj);
3068 continue;
3069 }
3070
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003071 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3072 if (ret != 0) {
3073 drm_gem_object_unreference(target_obj);
3074 i915_gem_object_unpin(obj);
3075 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003076 }
3077
3078 /* Map the page containing the relocation we're going to
3079 * perform.
3080 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003081 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003082 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3083 (reloc_offset &
3084 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003085 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003086 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003087 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003088
3089#if WATCH_BUF
3090 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003091 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003092 readl(reloc_entry), reloc_val);
3093#endif
3094 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003095 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003096
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003097 /* The updated presumed offset for this entry will be
3098 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003099 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003100 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003101
3102 drm_gem_object_unreference(target_obj);
3103 }
3104
Eric Anholt673a3942008-07-30 12:06:12 -07003105#if WATCH_BUF
3106 if (0)
3107 i915_gem_dump_object(obj, 128, __func__, ~0);
3108#endif
3109 return 0;
3110}
3111
3112/** Dispatch a batchbuffer to the ring
3113 */
3114static int
3115i915_dispatch_gem_execbuffer(struct drm_device *dev,
3116 struct drm_i915_gem_execbuffer *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003117 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003118 uint64_t exec_offset)
3119{
3120 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003121 int nbox = exec->num_cliprects;
3122 int i = 0, count;
Chris Wilson83d60792009-06-06 09:45:57 +01003123 uint32_t exec_start, exec_len;
Eric Anholt673a3942008-07-30 12:06:12 -07003124 RING_LOCALS;
3125
3126 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3127 exec_len = (uint32_t) exec->batch_len;
3128
Eric Anholt673a3942008-07-30 12:06:12 -07003129 count = nbox ? nbox : 1;
3130
3131 for (i = 0; i < count; i++) {
3132 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003133 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003134 exec->DR1, exec->DR4);
3135 if (ret)
3136 return ret;
3137 }
3138
3139 if (IS_I830(dev) || IS_845G(dev)) {
3140 BEGIN_LP_RING(4);
3141 OUT_RING(MI_BATCH_BUFFER);
3142 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3143 OUT_RING(exec_start + exec_len - 4);
3144 OUT_RING(0);
3145 ADVANCE_LP_RING();
3146 } else {
3147 BEGIN_LP_RING(2);
3148 if (IS_I965G(dev)) {
3149 OUT_RING(MI_BATCH_BUFFER_START |
3150 (2 << 6) |
3151 MI_BATCH_NON_SECURE_I965);
3152 OUT_RING(exec_start);
3153 } else {
3154 OUT_RING(MI_BATCH_BUFFER_START |
3155 (2 << 6));
3156 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3157 }
3158 ADVANCE_LP_RING();
3159 }
3160 }
3161
3162 /* XXX breadcrumb */
3163 return 0;
3164}
3165
3166/* Throttle our rendering by waiting until the ring has completed our requests
3167 * emitted over 20 msec ago.
3168 *
Eric Anholtb9624422009-06-03 07:27:35 +00003169 * Note that if we were to use the current jiffies each time around the loop,
3170 * we wouldn't escape the function with any frames outstanding if the time to
3171 * render a frame was over 20ms.
3172 *
Eric Anholt673a3942008-07-30 12:06:12 -07003173 * This should get us reasonable parallelism between CPU and GPU but also
3174 * relatively low latency when blocking on a particular request to finish.
3175 */
3176static int
3177i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3178{
3179 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3180 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003181 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003182
3183 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003184 while (!list_empty(&i915_file_priv->mm.request_list)) {
3185 struct drm_i915_gem_request *request;
3186
3187 request = list_first_entry(&i915_file_priv->mm.request_list,
3188 struct drm_i915_gem_request,
3189 client_list);
3190
3191 if (time_after_eq(request->emitted_jiffies, recent_enough))
3192 break;
3193
3194 ret = i915_wait_request(dev, request->seqno);
3195 if (ret != 0)
3196 break;
3197 }
Eric Anholt673a3942008-07-30 12:06:12 -07003198 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003199
Eric Anholt673a3942008-07-30 12:06:12 -07003200 return ret;
3201}
3202
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003203static int
3204i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3205 uint32_t buffer_count,
3206 struct drm_i915_gem_relocation_entry **relocs)
3207{
3208 uint32_t reloc_count = 0, reloc_index = 0, i;
3209 int ret;
3210
3211 *relocs = NULL;
3212 for (i = 0; i < buffer_count; i++) {
3213 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3214 return -EINVAL;
3215 reloc_count += exec_list[i].relocation_count;
3216 }
3217
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003218 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003219 if (*relocs == NULL)
3220 return -ENOMEM;
3221
3222 for (i = 0; i < buffer_count; i++) {
3223 struct drm_i915_gem_relocation_entry __user *user_relocs;
3224
3225 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3226
3227 ret = copy_from_user(&(*relocs)[reloc_index],
3228 user_relocs,
3229 exec_list[i].relocation_count *
3230 sizeof(**relocs));
3231 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003232 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003233 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003234 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003235 }
3236
3237 reloc_index += exec_list[i].relocation_count;
3238 }
3239
Florian Mickler2bc43b52009-04-06 22:55:41 +02003240 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003241}
3242
3243static int
3244i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3245 uint32_t buffer_count,
3246 struct drm_i915_gem_relocation_entry *relocs)
3247{
3248 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003249 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003250
3251 for (i = 0; i < buffer_count; i++) {
3252 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003253 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003254
3255 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3256
Florian Mickler2bc43b52009-04-06 22:55:41 +02003257 unwritten = copy_to_user(user_relocs,
3258 &relocs[reloc_count],
3259 exec_list[i].relocation_count *
3260 sizeof(*relocs));
3261
3262 if (unwritten) {
3263 ret = -EFAULT;
3264 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003265 }
3266
3267 reloc_count += exec_list[i].relocation_count;
3268 }
3269
Florian Mickler2bc43b52009-04-06 22:55:41 +02003270err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003271 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003272
3273 return ret;
3274}
3275
Chris Wilson83d60792009-06-06 09:45:57 +01003276static int
3277i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer *exec,
3278 uint64_t exec_offset)
3279{
3280 uint32_t exec_start, exec_len;
3281
3282 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3283 exec_len = (uint32_t) exec->batch_len;
3284
3285 if ((exec_start | exec_len) & 0x7)
3286 return -EINVAL;
3287
3288 if (!exec_start)
3289 return -EINVAL;
3290
3291 return 0;
3292}
3293
Eric Anholt673a3942008-07-30 12:06:12 -07003294int
3295i915_gem_execbuffer(struct drm_device *dev, void *data,
3296 struct drm_file *file_priv)
3297{
3298 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003299 struct drm_i915_gem_execbuffer *args = data;
3300 struct drm_i915_gem_exec_object *exec_list = NULL;
3301 struct drm_gem_object **object_list = NULL;
3302 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003303 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003304 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003305 struct drm_i915_gem_relocation_entry *relocs;
3306 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003307 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003308 uint32_t seqno, flush_domains, reloc_index;
Keith Packardac94a962008-11-20 23:30:27 -08003309 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07003310
3311#if WATCH_EXEC
3312 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3313 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3314#endif
3315
Eric Anholt4f481ed2008-09-10 14:22:49 -07003316 if (args->buffer_count < 1) {
3317 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3318 return -EINVAL;
3319 }
Eric Anholt673a3942008-07-30 12:06:12 -07003320 /* Copy in the exec list from userland */
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003321 exec_list = drm_calloc_large(sizeof(*exec_list), args->buffer_count);
3322 object_list = drm_calloc_large(sizeof(*object_list), args->buffer_count);
Eric Anholt673a3942008-07-30 12:06:12 -07003323 if (exec_list == NULL || object_list == NULL) {
3324 DRM_ERROR("Failed to allocate exec or object list "
3325 "for %d buffers\n",
3326 args->buffer_count);
3327 ret = -ENOMEM;
3328 goto pre_mutex_err;
3329 }
3330 ret = copy_from_user(exec_list,
3331 (struct drm_i915_relocation_entry __user *)
3332 (uintptr_t) args->buffers_ptr,
3333 sizeof(*exec_list) * args->buffer_count);
3334 if (ret != 0) {
3335 DRM_ERROR("copy %d exec entries failed %d\n",
3336 args->buffer_count, ret);
3337 goto pre_mutex_err;
3338 }
3339
Eric Anholt201361a2009-03-11 12:30:04 -07003340 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003341 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3342 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -07003343 if (cliprects == NULL)
3344 goto pre_mutex_err;
3345
3346 ret = copy_from_user(cliprects,
3347 (struct drm_clip_rect __user *)
3348 (uintptr_t) args->cliprects_ptr,
3349 sizeof(*cliprects) * args->num_cliprects);
3350 if (ret != 0) {
3351 DRM_ERROR("copy %d cliprects failed: %d\n",
3352 args->num_cliprects, ret);
3353 goto pre_mutex_err;
3354 }
3355 }
3356
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003357 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3358 &relocs);
3359 if (ret != 0)
3360 goto pre_mutex_err;
3361
Eric Anholt673a3942008-07-30 12:06:12 -07003362 mutex_lock(&dev->struct_mutex);
3363
3364 i915_verify_inactive(dev, __FILE__, __LINE__);
3365
3366 if (dev_priv->mm.wedged) {
3367 DRM_ERROR("Execbuf while wedged\n");
3368 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003369 ret = -EIO;
3370 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003371 }
3372
3373 if (dev_priv->mm.suspended) {
3374 DRM_ERROR("Execbuf while VT-switched.\n");
3375 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003376 ret = -EBUSY;
3377 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003378 }
3379
Keith Packardac94a962008-11-20 23:30:27 -08003380 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003381 for (i = 0; i < args->buffer_count; i++) {
3382 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3383 exec_list[i].handle);
3384 if (object_list[i] == NULL) {
3385 DRM_ERROR("Invalid object handle %d at index %d\n",
3386 exec_list[i].handle, i);
3387 ret = -EBADF;
3388 goto err;
3389 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003390
3391 obj_priv = object_list[i]->driver_private;
3392 if (obj_priv->in_execbuffer) {
3393 DRM_ERROR("Object %p appears more than once in object list\n",
3394 object_list[i]);
3395 ret = -EBADF;
3396 goto err;
3397 }
3398 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003399 }
Eric Anholt673a3942008-07-30 12:06:12 -07003400
Keith Packardac94a962008-11-20 23:30:27 -08003401 /* Pin and relocate */
3402 for (pin_tries = 0; ; pin_tries++) {
3403 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003404 reloc_index = 0;
3405
Keith Packardac94a962008-11-20 23:30:27 -08003406 for (i = 0; i < args->buffer_count; i++) {
3407 object_list[i]->pending_read_domains = 0;
3408 object_list[i]->pending_write_domain = 0;
3409 ret = i915_gem_object_pin_and_relocate(object_list[i],
3410 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003411 &exec_list[i],
3412 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003413 if (ret)
3414 break;
3415 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003416 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003417 }
3418 /* success */
3419 if (ret == 0)
3420 break;
3421
3422 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003423 if (ret != -ENOSPC || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08003424 if (ret != -ERESTARTSYS)
3425 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003426 goto err;
3427 }
Keith Packardac94a962008-11-20 23:30:27 -08003428
3429 /* unpin all of our buffers */
3430 for (i = 0; i < pinned; i++)
3431 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003432 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003433
3434 /* evict everyone we can from the aperture */
3435 ret = i915_gem_evict_everything(dev);
3436 if (ret)
3437 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003438 }
3439
3440 /* Set the pending read domains for the batch buffer to COMMAND */
3441 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003442 if (batch_obj->pending_write_domain) {
3443 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3444 ret = -EINVAL;
3445 goto err;
3446 }
3447 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003448
Chris Wilson83d60792009-06-06 09:45:57 +01003449 /* Sanity check the batch buffer, prior to moving objects */
3450 exec_offset = exec_list[args->buffer_count - 1].offset;
3451 ret = i915_gem_check_execbuffer (args, exec_offset);
3452 if (ret != 0) {
3453 DRM_ERROR("execbuf with invalid offset/length\n");
3454 goto err;
3455 }
3456
Eric Anholt673a3942008-07-30 12:06:12 -07003457 i915_verify_inactive(dev, __FILE__, __LINE__);
3458
Keith Packard646f0f62008-11-20 23:23:03 -08003459 /* Zero the global flush/invalidate flags. These
3460 * will be modified as new domains are computed
3461 * for each object
3462 */
3463 dev->invalidate_domains = 0;
3464 dev->flush_domains = 0;
3465
Eric Anholt673a3942008-07-30 12:06:12 -07003466 for (i = 0; i < args->buffer_count; i++) {
3467 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003468
Keith Packard646f0f62008-11-20 23:23:03 -08003469 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003470 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003471 }
3472
3473 i915_verify_inactive(dev, __FILE__, __LINE__);
3474
Keith Packard646f0f62008-11-20 23:23:03 -08003475 if (dev->invalidate_domains | dev->flush_domains) {
3476#if WATCH_EXEC
3477 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3478 __func__,
3479 dev->invalidate_domains,
3480 dev->flush_domains);
3481#endif
3482 i915_gem_flush(dev,
3483 dev->invalidate_domains,
3484 dev->flush_domains);
3485 if (dev->flush_domains)
Eric Anholtb9624422009-06-03 07:27:35 +00003486 (void)i915_add_request(dev, file_priv,
3487 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003488 }
Eric Anholt673a3942008-07-30 12:06:12 -07003489
Eric Anholtefbeed92009-02-19 14:54:51 -08003490 for (i = 0; i < args->buffer_count; i++) {
3491 struct drm_gem_object *obj = object_list[i];
3492
3493 obj->write_domain = obj->pending_write_domain;
3494 }
3495
Eric Anholt673a3942008-07-30 12:06:12 -07003496 i915_verify_inactive(dev, __FILE__, __LINE__);
3497
3498#if WATCH_COHERENCY
3499 for (i = 0; i < args->buffer_count; i++) {
3500 i915_gem_object_check_coherency(object_list[i],
3501 exec_list[i].handle);
3502 }
3503#endif
3504
Eric Anholt673a3942008-07-30 12:06:12 -07003505#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003506 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003507 args->batch_len,
3508 __func__,
3509 ~0);
3510#endif
3511
Eric Anholt673a3942008-07-30 12:06:12 -07003512 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003513 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003514 if (ret) {
3515 DRM_ERROR("dispatch failed %d\n", ret);
3516 goto err;
3517 }
3518
3519 /*
3520 * Ensure that the commands in the batch buffer are
3521 * finished before the interrupt fires
3522 */
3523 flush_domains = i915_retire_commands(dev);
3524
3525 i915_verify_inactive(dev, __FILE__, __LINE__);
3526
3527 /*
3528 * Get a seqno representing the execution of the current buffer,
3529 * which we can wait on. We would like to mitigate these interrupts,
3530 * likely by only creating seqnos occasionally (so that we have
3531 * *some* interrupts representing completion of buffers that we can
3532 * wait on when trying to clear up gtt space).
3533 */
Eric Anholtb9624422009-06-03 07:27:35 +00003534 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003535 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003536 for (i = 0; i < args->buffer_count; i++) {
3537 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003538
Eric Anholtce44b0e2008-11-06 16:00:31 -08003539 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003540#if WATCH_LRU
3541 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3542#endif
3543 }
3544#if WATCH_LRU
3545 i915_dump_lru(dev, __func__);
3546#endif
3547
3548 i915_verify_inactive(dev, __FILE__, __LINE__);
3549
Eric Anholt673a3942008-07-30 12:06:12 -07003550err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003551 for (i = 0; i < pinned; i++)
3552 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003553
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003554 for (i = 0; i < args->buffer_count; i++) {
3555 if (object_list[i]) {
3556 obj_priv = object_list[i]->driver_private;
3557 obj_priv->in_execbuffer = false;
3558 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003559 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003560 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003561
Eric Anholt673a3942008-07-30 12:06:12 -07003562 mutex_unlock(&dev->struct_mutex);
3563
Roland Dreiera35f2e22009-02-06 17:48:09 -08003564 if (!ret) {
3565 /* Copy the new buffer offsets back to the user's exec list. */
3566 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3567 (uintptr_t) args->buffers_ptr,
3568 exec_list,
3569 sizeof(*exec_list) * args->buffer_count);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003570 if (ret) {
3571 ret = -EFAULT;
Roland Dreiera35f2e22009-02-06 17:48:09 -08003572 DRM_ERROR("failed to copy %d exec entries "
3573 "back to user (%d)\n",
3574 args->buffer_count, ret);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003575 }
Roland Dreiera35f2e22009-02-06 17:48:09 -08003576 }
3577
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003578 /* Copy the updated relocations out regardless of current error
3579 * state. Failure to update the relocs would mean that the next
3580 * time userland calls execbuf, it would do so with presumed offset
3581 * state that didn't match the actual object state.
3582 */
3583 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3584 relocs);
3585 if (ret2 != 0) {
3586 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3587
3588 if (ret == 0)
3589 ret = ret2;
3590 }
3591
Eric Anholt673a3942008-07-30 12:06:12 -07003592pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003593 drm_free_large(object_list);
3594 drm_free_large(exec_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003595 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003596
3597 return ret;
3598}
3599
3600int
3601i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3602{
3603 struct drm_device *dev = obj->dev;
3604 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3605 int ret;
3606
3607 i915_verify_inactive(dev, __FILE__, __LINE__);
3608 if (obj_priv->gtt_space == NULL) {
3609 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3610 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003611 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05003612 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003613 return ret;
3614 }
Chris Wilson22c344e2009-02-11 14:26:45 +00003615 }
3616 /*
3617 * Pre-965 chips need a fence register set up in order to
3618 * properly handle tiled surfaces.
3619 */
Eric Anholta09ba7f2009-08-29 12:49:51 -07003620 if (!IS_I965G(dev) && obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003621 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson22c344e2009-02-11 14:26:45 +00003622 if (ret != 0) {
3623 if (ret != -EBUSY && ret != -ERESTARTSYS)
3624 DRM_ERROR("Failure to install fence: %d\n",
3625 ret);
3626 return ret;
3627 }
Eric Anholt673a3942008-07-30 12:06:12 -07003628 }
3629 obj_priv->pin_count++;
3630
3631 /* If the object is not active and not pending a flush,
3632 * remove it from the inactive list
3633 */
3634 if (obj_priv->pin_count == 1) {
3635 atomic_inc(&dev->pin_count);
3636 atomic_add(obj->size, &dev->pin_memory);
3637 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003638 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07003639 !list_empty(&obj_priv->list))
3640 list_del_init(&obj_priv->list);
3641 }
3642 i915_verify_inactive(dev, __FILE__, __LINE__);
3643
3644 return 0;
3645}
3646
3647void
3648i915_gem_object_unpin(struct drm_gem_object *obj)
3649{
3650 struct drm_device *dev = obj->dev;
3651 drm_i915_private_t *dev_priv = dev->dev_private;
3652 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3653
3654 i915_verify_inactive(dev, __FILE__, __LINE__);
3655 obj_priv->pin_count--;
3656 BUG_ON(obj_priv->pin_count < 0);
3657 BUG_ON(obj_priv->gtt_space == NULL);
3658
3659 /* If the object is no longer pinned, and is
3660 * neither active nor being flushed, then stick it on
3661 * the inactive list
3662 */
3663 if (obj_priv->pin_count == 0) {
3664 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003665 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003666 list_move_tail(&obj_priv->list,
3667 &dev_priv->mm.inactive_list);
3668 atomic_dec(&dev->pin_count);
3669 atomic_sub(obj->size, &dev->pin_memory);
3670 }
3671 i915_verify_inactive(dev, __FILE__, __LINE__);
3672}
3673
3674int
3675i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3676 struct drm_file *file_priv)
3677{
3678 struct drm_i915_gem_pin *args = data;
3679 struct drm_gem_object *obj;
3680 struct drm_i915_gem_object *obj_priv;
3681 int ret;
3682
3683 mutex_lock(&dev->struct_mutex);
3684
3685 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3686 if (obj == NULL) {
3687 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3688 args->handle);
3689 mutex_unlock(&dev->struct_mutex);
3690 return -EBADF;
3691 }
3692 obj_priv = obj->driver_private;
3693
Jesse Barnes79e53942008-11-07 14:24:08 -08003694 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3695 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3696 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00003697 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003698 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003699 return -EINVAL;
3700 }
3701
3702 obj_priv->user_pin_count++;
3703 obj_priv->pin_filp = file_priv;
3704 if (obj_priv->user_pin_count == 1) {
3705 ret = i915_gem_object_pin(obj, args->alignment);
3706 if (ret != 0) {
3707 drm_gem_object_unreference(obj);
3708 mutex_unlock(&dev->struct_mutex);
3709 return ret;
3710 }
Eric Anholt673a3942008-07-30 12:06:12 -07003711 }
3712
3713 /* XXX - flush the CPU caches for pinned objects
3714 * as the X server doesn't manage domains yet
3715 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003716 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003717 args->offset = obj_priv->gtt_offset;
3718 drm_gem_object_unreference(obj);
3719 mutex_unlock(&dev->struct_mutex);
3720
3721 return 0;
3722}
3723
3724int
3725i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3726 struct drm_file *file_priv)
3727{
3728 struct drm_i915_gem_pin *args = data;
3729 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08003730 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003731
3732 mutex_lock(&dev->struct_mutex);
3733
3734 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3735 if (obj == NULL) {
3736 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3737 args->handle);
3738 mutex_unlock(&dev->struct_mutex);
3739 return -EBADF;
3740 }
3741
Jesse Barnes79e53942008-11-07 14:24:08 -08003742 obj_priv = obj->driver_private;
3743 if (obj_priv->pin_filp != file_priv) {
3744 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3745 args->handle);
3746 drm_gem_object_unreference(obj);
3747 mutex_unlock(&dev->struct_mutex);
3748 return -EINVAL;
3749 }
3750 obj_priv->user_pin_count--;
3751 if (obj_priv->user_pin_count == 0) {
3752 obj_priv->pin_filp = NULL;
3753 i915_gem_object_unpin(obj);
3754 }
Eric Anholt673a3942008-07-30 12:06:12 -07003755
3756 drm_gem_object_unreference(obj);
3757 mutex_unlock(&dev->struct_mutex);
3758 return 0;
3759}
3760
3761int
3762i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3763 struct drm_file *file_priv)
3764{
3765 struct drm_i915_gem_busy *args = data;
3766 struct drm_gem_object *obj;
3767 struct drm_i915_gem_object *obj_priv;
3768
Eric Anholt673a3942008-07-30 12:06:12 -07003769 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3770 if (obj == NULL) {
3771 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3772 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003773 return -EBADF;
3774 }
3775
Chris Wilsonb1ce7862009-06-06 09:46:00 +01003776 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08003777 /* Update the active list for the hardware's current position.
3778 * Otherwise this only updates on a delayed timer or when irqs are
3779 * actually unmasked, and our working set ends up being larger than
3780 * required.
3781 */
3782 i915_gem_retire_requests(dev);
3783
Eric Anholt673a3942008-07-30 12:06:12 -07003784 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08003785 /* Don't count being on the flushing list against the object being
3786 * done. Otherwise, a buffer left on the flushing list but not getting
3787 * flushed (because nobody's flushing that domain) won't ever return
3788 * unbusy and get reused by libdrm's bo cache. The other expected
3789 * consumer of this interface, OpenGL's occlusion queries, also specs
3790 * that the objects get unbusy "eventually" without any interference.
3791 */
3792 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003793
3794 drm_gem_object_unreference(obj);
3795 mutex_unlock(&dev->struct_mutex);
3796 return 0;
3797}
3798
3799int
3800i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3801 struct drm_file *file_priv)
3802{
3803 return i915_gem_ring_throttle(dev, file_priv);
3804}
3805
3806int i915_gem_init_object(struct drm_gem_object *obj)
3807{
3808 struct drm_i915_gem_object *obj_priv;
3809
Eric Anholt9a298b22009-03-24 12:23:04 -07003810 obj_priv = kzalloc(sizeof(*obj_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07003811 if (obj_priv == NULL)
3812 return -ENOMEM;
3813
3814 /*
3815 * We've just allocated pages from the kernel,
3816 * so they've just been written by the CPU with
3817 * zeros. They'll need to be clflushed before we
3818 * use them with the GPU.
3819 */
3820 obj->write_domain = I915_GEM_DOMAIN_CPU;
3821 obj->read_domains = I915_GEM_DOMAIN_CPU;
3822
Keith Packardba1eb1d2008-10-14 19:55:10 -07003823 obj_priv->agp_type = AGP_USER_MEMORY;
3824
Eric Anholt673a3942008-07-30 12:06:12 -07003825 obj->driver_private = obj_priv;
3826 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003827 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07003828 INIT_LIST_HEAD(&obj_priv->list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003829 INIT_LIST_HEAD(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003830
Eric Anholt673a3942008-07-30 12:06:12 -07003831 return 0;
3832}
3833
3834void i915_gem_free_object(struct drm_gem_object *obj)
3835{
Jesse Barnesde151cf2008-11-12 10:03:55 -08003836 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003837 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3838
3839 while (obj_priv->pin_count > 0)
3840 i915_gem_object_unpin(obj);
3841
Dave Airlie71acb5e2008-12-30 20:31:46 +10003842 if (obj_priv->phys_obj)
3843 i915_gem_detach_phys_object(dev, obj);
3844
Eric Anholt673a3942008-07-30 12:06:12 -07003845 i915_gem_object_unbind(obj);
3846
Jesse Barnesab00b3e2009-02-11 14:01:46 -08003847 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003848
Eric Anholt9a298b22009-03-24 12:23:04 -07003849 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07003850 kfree(obj_priv->bit_17);
Eric Anholt9a298b22009-03-24 12:23:04 -07003851 kfree(obj->driver_private);
Eric Anholt673a3942008-07-30 12:06:12 -07003852}
3853
Eric Anholt673a3942008-07-30 12:06:12 -07003854/** Unbinds all objects that are on the given buffer list. */
3855static int
3856i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3857{
3858 struct drm_gem_object *obj;
3859 struct drm_i915_gem_object *obj_priv;
3860 int ret;
3861
3862 while (!list_empty(head)) {
3863 obj_priv = list_first_entry(head,
3864 struct drm_i915_gem_object,
3865 list);
3866 obj = obj_priv->obj;
3867
3868 if (obj_priv->pin_count != 0) {
3869 DRM_ERROR("Pinned object in unbind list\n");
3870 mutex_unlock(&dev->struct_mutex);
3871 return -EINVAL;
3872 }
3873
3874 ret = i915_gem_object_unbind(obj);
3875 if (ret != 0) {
3876 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3877 ret);
3878 mutex_unlock(&dev->struct_mutex);
3879 return ret;
3880 }
3881 }
3882
3883
3884 return 0;
3885}
3886
Jesse Barnes5669fca2009-02-17 15:13:31 -08003887int
Eric Anholt673a3942008-07-30 12:06:12 -07003888i915_gem_idle(struct drm_device *dev)
3889{
3890 drm_i915_private_t *dev_priv = dev->dev_private;
3891 uint32_t seqno, cur_seqno, last_seqno;
3892 int stuck, ret;
3893
Keith Packard6dbe2772008-10-14 21:41:13 -07003894 mutex_lock(&dev->struct_mutex);
3895
3896 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3897 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003898 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003899 }
Eric Anholt673a3942008-07-30 12:06:12 -07003900
3901 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3902 * We need to replace this with a semaphore, or something.
3903 */
3904 dev_priv->mm.suspended = 1;
3905
Keith Packard6dbe2772008-10-14 21:41:13 -07003906 /* Cancel the retire work handler, wait for it to finish if running
3907 */
3908 mutex_unlock(&dev->struct_mutex);
3909 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3910 mutex_lock(&dev->struct_mutex);
3911
Eric Anholt673a3942008-07-30 12:06:12 -07003912 i915_kernel_lost_context(dev);
3913
3914 /* Flush the GPU along with all non-CPU write domains
3915 */
Chris Wilson21d509e2009-06-06 09:46:02 +01003916 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
3917 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07003918
3919 if (seqno == 0) {
3920 mutex_unlock(&dev->struct_mutex);
3921 return -ENOMEM;
3922 }
3923
3924 dev_priv->mm.waiting_gem_seqno = seqno;
3925 last_seqno = 0;
3926 stuck = 0;
3927 for (;;) {
3928 cur_seqno = i915_get_gem_seqno(dev);
3929 if (i915_seqno_passed(cur_seqno, seqno))
3930 break;
3931 if (last_seqno == cur_seqno) {
3932 if (stuck++ > 100) {
3933 DRM_ERROR("hardware wedged\n");
3934 dev_priv->mm.wedged = 1;
3935 DRM_WAKEUP(&dev_priv->irq_queue);
3936 break;
3937 }
3938 }
3939 msleep(10);
3940 last_seqno = cur_seqno;
3941 }
3942 dev_priv->mm.waiting_gem_seqno = 0;
3943
3944 i915_gem_retire_requests(dev);
3945
Carl Worth5e118f42009-03-20 11:54:25 -07003946 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003947 if (!dev_priv->mm.wedged) {
3948 /* Active and flushing should now be empty as we've
3949 * waited for a sequence higher than any pending execbuffer
3950 */
3951 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3952 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3953 /* Request should now be empty as we've also waited
3954 * for the last request in the list
3955 */
3956 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3957 }
Eric Anholt673a3942008-07-30 12:06:12 -07003958
Eric Anholt28dfe522008-11-13 15:00:55 -08003959 /* Empty the active and flushing lists to inactive. If there's
3960 * anything left at this point, it means that we're wedged and
3961 * nothing good's going to happen by leaving them there. So strip
3962 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003963 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003964 while (!list_empty(&dev_priv->mm.active_list)) {
3965 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003966
Eric Anholt28dfe522008-11-13 15:00:55 -08003967 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3968 struct drm_i915_gem_object,
3969 list);
3970 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3971 i915_gem_object_move_to_inactive(obj_priv->obj);
3972 }
Carl Worth5e118f42009-03-20 11:54:25 -07003973 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003974
3975 while (!list_empty(&dev_priv->mm.flushing_list)) {
3976 struct drm_i915_gem_object *obj_priv;
3977
Eric Anholt151903d2008-12-01 10:23:21 +10003978 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003979 struct drm_i915_gem_object,
3980 list);
3981 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3982 i915_gem_object_move_to_inactive(obj_priv->obj);
3983 }
3984
3985
3986 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003987 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003988 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003989 if (ret) {
3990 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003991 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003992 }
Eric Anholt673a3942008-07-30 12:06:12 -07003993
Keith Packard6dbe2772008-10-14 21:41:13 -07003994 i915_gem_cleanup_ringbuffer(dev);
3995 mutex_unlock(&dev->struct_mutex);
3996
Eric Anholt673a3942008-07-30 12:06:12 -07003997 return 0;
3998}
3999
4000static int
4001i915_gem_init_hws(struct drm_device *dev)
4002{
4003 drm_i915_private_t *dev_priv = dev->dev_private;
4004 struct drm_gem_object *obj;
4005 struct drm_i915_gem_object *obj_priv;
4006 int ret;
4007
4008 /* If we need a physical address for the status page, it's already
4009 * initialized at driver load time.
4010 */
4011 if (!I915_NEED_GFX_HWS(dev))
4012 return 0;
4013
4014 obj = drm_gem_object_alloc(dev, 4096);
4015 if (obj == NULL) {
4016 DRM_ERROR("Failed to allocate status page\n");
4017 return -ENOMEM;
4018 }
4019 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07004020 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07004021
4022 ret = i915_gem_object_pin(obj, 4096);
4023 if (ret != 0) {
4024 drm_gem_object_unreference(obj);
4025 return ret;
4026 }
4027
4028 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07004029
Eric Anholt856fa192009-03-19 14:10:50 -07004030 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004031 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004032 DRM_ERROR("Failed to map status page.\n");
4033 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00004034 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004035 drm_gem_object_unreference(obj);
4036 return -EINVAL;
4037 }
4038 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004039 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
4040 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004041 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07004042 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
4043
4044 return 0;
4045}
4046
Chris Wilson85a7bb92009-02-11 14:52:44 +00004047static void
4048i915_gem_cleanup_hws(struct drm_device *dev)
4049{
4050 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004051 struct drm_gem_object *obj;
4052 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00004053
4054 if (dev_priv->hws_obj == NULL)
4055 return;
4056
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004057 obj = dev_priv->hws_obj;
4058 obj_priv = obj->driver_private;
4059
Eric Anholt856fa192009-03-19 14:10:50 -07004060 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004061 i915_gem_object_unpin(obj);
4062 drm_gem_object_unreference(obj);
4063 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004064
Chris Wilson85a7bb92009-02-11 14:52:44 +00004065 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4066 dev_priv->hw_status_page = NULL;
4067
4068 /* Write high address into HWS_PGA when disabling. */
4069 I915_WRITE(HWS_PGA, 0x1ffff000);
4070}
4071
Jesse Barnes79e53942008-11-07 14:24:08 -08004072int
Eric Anholt673a3942008-07-30 12:06:12 -07004073i915_gem_init_ringbuffer(struct drm_device *dev)
4074{
4075 drm_i915_private_t *dev_priv = dev->dev_private;
4076 struct drm_gem_object *obj;
4077 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08004078 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07004079 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07004080 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07004081
4082 ret = i915_gem_init_hws(dev);
4083 if (ret != 0)
4084 return ret;
4085
4086 obj = drm_gem_object_alloc(dev, 128 * 1024);
4087 if (obj == NULL) {
4088 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004089 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004090 return -ENOMEM;
4091 }
4092 obj_priv = obj->driver_private;
4093
4094 ret = i915_gem_object_pin(obj, 4096);
4095 if (ret != 0) {
4096 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004097 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004098 return ret;
4099 }
4100
4101 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004102 ring->Size = obj->size;
Eric Anholt673a3942008-07-30 12:06:12 -07004103
Jesse Barnes79e53942008-11-07 14:24:08 -08004104 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4105 ring->map.size = obj->size;
4106 ring->map.type = 0;
4107 ring->map.flags = 0;
4108 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004109
Jesse Barnes79e53942008-11-07 14:24:08 -08004110 drm_core_ioremap_wc(&ring->map, dev);
4111 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004112 DRM_ERROR("Failed to map ringbuffer.\n");
4113 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004114 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004115 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004116 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004117 return -EINVAL;
4118 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004119 ring->ring_obj = obj;
4120 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004121
4122 /* Stop the ring if it's running. */
4123 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004124 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07004125 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004126
4127 /* Initialize the ring. */
4128 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07004129 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4130
4131 /* G45 ring initialization fails to reset head to zero */
4132 if (head != 0) {
4133 DRM_ERROR("Ring head not reset to zero "
4134 "ctl %08x head %08x tail %08x start %08x\n",
4135 I915_READ(PRB0_CTL),
4136 I915_READ(PRB0_HEAD),
4137 I915_READ(PRB0_TAIL),
4138 I915_READ(PRB0_START));
4139 I915_WRITE(PRB0_HEAD, 0);
4140
4141 DRM_ERROR("Ring head forced to zero "
4142 "ctl %08x head %08x tail %08x start %08x\n",
4143 I915_READ(PRB0_CTL),
4144 I915_READ(PRB0_HEAD),
4145 I915_READ(PRB0_TAIL),
4146 I915_READ(PRB0_START));
4147 }
4148
Eric Anholt673a3942008-07-30 12:06:12 -07004149 I915_WRITE(PRB0_CTL,
4150 ((obj->size - 4096) & RING_NR_PAGES) |
4151 RING_NO_REPORT |
4152 RING_VALID);
4153
Keith Packard50aa253d2008-10-14 17:20:35 -07004154 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4155
4156 /* If the head is still not zero, the ring is dead */
4157 if (head != 0) {
4158 DRM_ERROR("Ring initialization failed "
4159 "ctl %08x head %08x tail %08x start %08x\n",
4160 I915_READ(PRB0_CTL),
4161 I915_READ(PRB0_HEAD),
4162 I915_READ(PRB0_TAIL),
4163 I915_READ(PRB0_START));
4164 return -EIO;
4165 }
4166
Eric Anholt673a3942008-07-30 12:06:12 -07004167 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004168 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4169 i915_kernel_lost_context(dev);
4170 else {
4171 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4172 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4173 ring->space = ring->head - (ring->tail + 8);
4174 if (ring->space < 0)
4175 ring->space += ring->Size;
4176 }
Eric Anholt673a3942008-07-30 12:06:12 -07004177
4178 return 0;
4179}
4180
Jesse Barnes79e53942008-11-07 14:24:08 -08004181void
Eric Anholt673a3942008-07-30 12:06:12 -07004182i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4183{
4184 drm_i915_private_t *dev_priv = dev->dev_private;
4185
4186 if (dev_priv->ring.ring_obj == NULL)
4187 return;
4188
4189 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4190
4191 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4192 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4193 dev_priv->ring.ring_obj = NULL;
4194 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4195
Chris Wilson85a7bb92009-02-11 14:52:44 +00004196 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004197}
4198
4199int
4200i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4201 struct drm_file *file_priv)
4202{
4203 drm_i915_private_t *dev_priv = dev->dev_private;
4204 int ret;
4205
Jesse Barnes79e53942008-11-07 14:24:08 -08004206 if (drm_core_check_feature(dev, DRIVER_MODESET))
4207 return 0;
4208
Eric Anholt673a3942008-07-30 12:06:12 -07004209 if (dev_priv->mm.wedged) {
4210 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4211 dev_priv->mm.wedged = 0;
4212 }
4213
Eric Anholt673a3942008-07-30 12:06:12 -07004214 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004215 dev_priv->mm.suspended = 0;
4216
4217 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004218 if (ret != 0) {
4219 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004220 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004221 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004222
Carl Worth5e118f42009-03-20 11:54:25 -07004223 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004224 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004225 spin_unlock(&dev_priv->mm.active_list_lock);
4226
Eric Anholt673a3942008-07-30 12:06:12 -07004227 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4228 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4229 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004230 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004231
4232 drm_irq_install(dev);
4233
Eric Anholt673a3942008-07-30 12:06:12 -07004234 return 0;
4235}
4236
4237int
4238i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4239 struct drm_file *file_priv)
4240{
4241 int ret;
4242
Jesse Barnes79e53942008-11-07 14:24:08 -08004243 if (drm_core_check_feature(dev, DRIVER_MODESET))
4244 return 0;
4245
Eric Anholt673a3942008-07-30 12:06:12 -07004246 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004247 drm_irq_uninstall(dev);
4248
Keith Packard6dbe2772008-10-14 21:41:13 -07004249 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004250}
4251
4252void
4253i915_gem_lastclose(struct drm_device *dev)
4254{
4255 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004256
Eric Anholte806b492009-01-22 09:56:58 -08004257 if (drm_core_check_feature(dev, DRIVER_MODESET))
4258 return;
4259
Keith Packard6dbe2772008-10-14 21:41:13 -07004260 ret = i915_gem_idle(dev);
4261 if (ret)
4262 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004263}
4264
4265void
4266i915_gem_load(struct drm_device *dev)
4267{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004268 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004269 drm_i915_private_t *dev_priv = dev->dev_private;
4270
Carl Worth5e118f42009-03-20 11:54:25 -07004271 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004272 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4273 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4274 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4275 INIT_LIST_HEAD(&dev_priv->mm.request_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004276 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004277 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4278 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004279 dev_priv->mm.next_gem_seqno = 1;
4280
Jesse Barnesde151cf2008-11-12 10:03:55 -08004281 /* Old X drivers will take 0-2 for front, back, depth buffers */
4282 dev_priv->fence_reg_start = 3;
4283
Jesse Barnes0f973f22009-01-26 17:10:45 -08004284 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004285 dev_priv->num_fence_regs = 16;
4286 else
4287 dev_priv->num_fence_regs = 8;
4288
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004289 /* Initialize fence registers to zero */
4290 if (IS_I965G(dev)) {
4291 for (i = 0; i < 16; i++)
4292 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4293 } else {
4294 for (i = 0; i < 8; i++)
4295 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4296 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4297 for (i = 0; i < 8; i++)
4298 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4299 }
4300
Eric Anholt673a3942008-07-30 12:06:12 -07004301 i915_gem_detect_bit_6_swizzle(dev);
4302}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004303
4304/*
4305 * Create a physically contiguous memory object for this object
4306 * e.g. for cursor + overlay regs
4307 */
4308int i915_gem_init_phys_object(struct drm_device *dev,
4309 int id, int size)
4310{
4311 drm_i915_private_t *dev_priv = dev->dev_private;
4312 struct drm_i915_gem_phys_object *phys_obj;
4313 int ret;
4314
4315 if (dev_priv->mm.phys_objs[id - 1] || !size)
4316 return 0;
4317
Eric Anholt9a298b22009-03-24 12:23:04 -07004318 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004319 if (!phys_obj)
4320 return -ENOMEM;
4321
4322 phys_obj->id = id;
4323
4324 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4325 if (!phys_obj->handle) {
4326 ret = -ENOMEM;
4327 goto kfree_obj;
4328 }
4329#ifdef CONFIG_X86
4330 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4331#endif
4332
4333 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4334
4335 return 0;
4336kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004337 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004338 return ret;
4339}
4340
4341void i915_gem_free_phys_object(struct drm_device *dev, int id)
4342{
4343 drm_i915_private_t *dev_priv = dev->dev_private;
4344 struct drm_i915_gem_phys_object *phys_obj;
4345
4346 if (!dev_priv->mm.phys_objs[id - 1])
4347 return;
4348
4349 phys_obj = dev_priv->mm.phys_objs[id - 1];
4350 if (phys_obj->cur_obj) {
4351 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4352 }
4353
4354#ifdef CONFIG_X86
4355 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4356#endif
4357 drm_pci_free(dev, phys_obj->handle);
4358 kfree(phys_obj);
4359 dev_priv->mm.phys_objs[id - 1] = NULL;
4360}
4361
4362void i915_gem_free_all_phys_object(struct drm_device *dev)
4363{
4364 int i;
4365
Dave Airlie260883c2009-01-22 17:58:49 +10004366 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004367 i915_gem_free_phys_object(dev, i);
4368}
4369
4370void i915_gem_detach_phys_object(struct drm_device *dev,
4371 struct drm_gem_object *obj)
4372{
4373 struct drm_i915_gem_object *obj_priv;
4374 int i;
4375 int ret;
4376 int page_count;
4377
4378 obj_priv = obj->driver_private;
4379 if (!obj_priv->phys_obj)
4380 return;
4381
Eric Anholt856fa192009-03-19 14:10:50 -07004382 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004383 if (ret)
4384 goto out;
4385
4386 page_count = obj->size / PAGE_SIZE;
4387
4388 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004389 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004390 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4391
4392 memcpy(dst, src, PAGE_SIZE);
4393 kunmap_atomic(dst, KM_USER0);
4394 }
Eric Anholt856fa192009-03-19 14:10:50 -07004395 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004396 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004397
4398 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004399out:
4400 obj_priv->phys_obj->cur_obj = NULL;
4401 obj_priv->phys_obj = NULL;
4402}
4403
4404int
4405i915_gem_attach_phys_object(struct drm_device *dev,
4406 struct drm_gem_object *obj, int id)
4407{
4408 drm_i915_private_t *dev_priv = dev->dev_private;
4409 struct drm_i915_gem_object *obj_priv;
4410 int ret = 0;
4411 int page_count;
4412 int i;
4413
4414 if (id > I915_MAX_PHYS_OBJECT)
4415 return -EINVAL;
4416
4417 obj_priv = obj->driver_private;
4418
4419 if (obj_priv->phys_obj) {
4420 if (obj_priv->phys_obj->id == id)
4421 return 0;
4422 i915_gem_detach_phys_object(dev, obj);
4423 }
4424
4425
4426 /* create a new object */
4427 if (!dev_priv->mm.phys_objs[id - 1]) {
4428 ret = i915_gem_init_phys_object(dev, id,
4429 obj->size);
4430 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004431 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004432 goto out;
4433 }
4434 }
4435
4436 /* bind to the object */
4437 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4438 obj_priv->phys_obj->cur_obj = obj;
4439
Eric Anholt856fa192009-03-19 14:10:50 -07004440 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004441 if (ret) {
4442 DRM_ERROR("failed to get page list\n");
4443 goto out;
4444 }
4445
4446 page_count = obj->size / PAGE_SIZE;
4447
4448 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004449 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004450 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4451
4452 memcpy(dst, src, PAGE_SIZE);
4453 kunmap_atomic(src, KM_USER0);
4454 }
4455
Chris Wilsond78b47b2009-06-17 21:52:49 +01004456 i915_gem_object_put_pages(obj);
4457
Dave Airlie71acb5e2008-12-30 20:31:46 +10004458 return 0;
4459out:
4460 return ret;
4461}
4462
4463static int
4464i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4465 struct drm_i915_gem_pwrite *args,
4466 struct drm_file *file_priv)
4467{
4468 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4469 void *obj_addr;
4470 int ret;
4471 char __user *user_data;
4472
4473 user_data = (char __user *) (uintptr_t) args->data_ptr;
4474 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4475
Dave Airliee08fb4f2009-02-25 14:52:30 +10004476 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004477 ret = copy_from_user(obj_addr, user_data, args->size);
4478 if (ret)
4479 return -EFAULT;
4480
4481 drm_agp_chipset_flush(dev);
4482 return 0;
4483}
Eric Anholtb9624422009-06-03 07:27:35 +00004484
4485void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4486{
4487 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4488
4489 /* Clean up our request list when the client is going away, so that
4490 * later retire_requests won't dereference our soon-to-be-gone
4491 * file_priv.
4492 */
4493 mutex_lock(&dev->struct_mutex);
4494 while (!list_empty(&i915_file_priv->mm.request_list))
4495 list_del_init(i915_file_priv->mm.request_list.next);
4496 mutex_unlock(&dev->struct_mutex);
4497}