blob: 579b3b04ff1237643ce3b94dcbebde5e3d95b5b7 [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
Ben Gamarif65d9422009-09-14 17:48:44 -04001587 if (!dev_priv->mm.suspended) {
1588 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1589 if (was_empty)
1590 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1591 }
Eric Anholt673a3942008-07-30 12:06:12 -07001592 return seqno;
1593}
1594
1595/**
1596 * Command execution barrier
1597 *
1598 * Ensures that all commands in the ring are finished
1599 * before signalling the CPU
1600 */
Eric Anholt3043c602008-10-02 12:24:47 -07001601static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -07001602i915_retire_commands(struct drm_device *dev)
1603{
1604 drm_i915_private_t *dev_priv = dev->dev_private;
1605 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1606 uint32_t flush_domains = 0;
1607 RING_LOCALS;
1608
1609 /* The sampler always gets flushed on i965 (sigh) */
1610 if (IS_I965G(dev))
1611 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1612 BEGIN_LP_RING(2);
1613 OUT_RING(cmd);
1614 OUT_RING(0); /* noop */
1615 ADVANCE_LP_RING();
1616 return flush_domains;
1617}
1618
1619/**
1620 * Moves buffers associated only with the given active seqno from the active
1621 * to inactive list, potentially freeing them.
1622 */
1623static void
1624i915_gem_retire_request(struct drm_device *dev,
1625 struct drm_i915_gem_request *request)
1626{
1627 drm_i915_private_t *dev_priv = dev->dev_private;
1628
1629 /* Move any buffers on the active list that are no longer referenced
1630 * by the ringbuffer to the flushing/inactive lists as appropriate.
1631 */
Carl Worth5e118f42009-03-20 11:54:25 -07001632 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001633 while (!list_empty(&dev_priv->mm.active_list)) {
1634 struct drm_gem_object *obj;
1635 struct drm_i915_gem_object *obj_priv;
1636
1637 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1638 struct drm_i915_gem_object,
1639 list);
1640 obj = obj_priv->obj;
1641
1642 /* If the seqno being retired doesn't match the oldest in the
1643 * list, then the oldest in the list must still be newer than
1644 * this seqno.
1645 */
1646 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001647 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001648
Eric Anholt673a3942008-07-30 12:06:12 -07001649#if WATCH_LRU
1650 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1651 __func__, request->seqno, obj);
1652#endif
1653
Eric Anholtce44b0e2008-11-06 16:00:31 -08001654 if (obj->write_domain != 0)
1655 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001656 else {
1657 /* Take a reference on the object so it won't be
1658 * freed while the spinlock is held. The list
1659 * protection for this spinlock is safe when breaking
1660 * the lock like this since the next thing we do
1661 * is just get the head of the list again.
1662 */
1663 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001664 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001665 spin_unlock(&dev_priv->mm.active_list_lock);
1666 drm_gem_object_unreference(obj);
1667 spin_lock(&dev_priv->mm.active_list_lock);
1668 }
Eric Anholt673a3942008-07-30 12:06:12 -07001669 }
Carl Worth5e118f42009-03-20 11:54:25 -07001670out:
1671 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001672}
1673
1674/**
1675 * Returns true if seq1 is later than seq2.
1676 */
Ben Gamari22be1722009-09-14 17:48:43 -04001677bool
Eric Anholt673a3942008-07-30 12:06:12 -07001678i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1679{
1680 return (int32_t)(seq1 - seq2) >= 0;
1681}
1682
1683uint32_t
1684i915_get_gem_seqno(struct drm_device *dev)
1685{
1686 drm_i915_private_t *dev_priv = dev->dev_private;
1687
1688 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1689}
1690
1691/**
1692 * This function clears the request list as sequence numbers are passed.
1693 */
1694void
1695i915_gem_retire_requests(struct drm_device *dev)
1696{
1697 drm_i915_private_t *dev_priv = dev->dev_private;
1698 uint32_t seqno;
1699
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001700 if (!dev_priv->hw_status_page)
1701 return;
1702
Eric Anholt673a3942008-07-30 12:06:12 -07001703 seqno = i915_get_gem_seqno(dev);
1704
1705 while (!list_empty(&dev_priv->mm.request_list)) {
1706 struct drm_i915_gem_request *request;
1707 uint32_t retiring_seqno;
1708
1709 request = list_first_entry(&dev_priv->mm.request_list,
1710 struct drm_i915_gem_request,
1711 list);
1712 retiring_seqno = request->seqno;
1713
1714 if (i915_seqno_passed(seqno, retiring_seqno) ||
1715 dev_priv->mm.wedged) {
1716 i915_gem_retire_request(dev, request);
1717
1718 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001719 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001720 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001721 } else
1722 break;
1723 }
1724}
1725
1726void
1727i915_gem_retire_work_handler(struct work_struct *work)
1728{
1729 drm_i915_private_t *dev_priv;
1730 struct drm_device *dev;
1731
1732 dev_priv = container_of(work, drm_i915_private_t,
1733 mm.retire_work.work);
1734 dev = dev_priv->dev;
1735
1736 mutex_lock(&dev->struct_mutex);
1737 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001738 if (!dev_priv->mm.suspended &&
1739 !list_empty(&dev_priv->mm.request_list))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001740 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001741 mutex_unlock(&dev->struct_mutex);
1742}
1743
1744/**
1745 * Waits for a sequence number to be signaled, and cleans up the
1746 * request and object lists appropriately for that event.
1747 */
Eric Anholt3043c602008-10-02 12:24:47 -07001748static int
Eric Anholt673a3942008-07-30 12:06:12 -07001749i915_wait_request(struct drm_device *dev, uint32_t seqno)
1750{
1751 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001752 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001753 int ret = 0;
1754
1755 BUG_ON(seqno == 0);
1756
Ben Gamariffed1d02009-09-14 17:48:41 -04001757 if (dev_priv->mm.wedged)
1758 return -EIO;
1759
Eric Anholt673a3942008-07-30 12:06:12 -07001760 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001761 if (IS_IGDNG(dev))
1762 ier = I915_READ(DEIER) | I915_READ(GTIER);
1763 else
1764 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001765 if (!ier) {
1766 DRM_ERROR("something (likely vbetool) disabled "
1767 "interrupts, re-enabling\n");
1768 i915_driver_irq_preinstall(dev);
1769 i915_driver_irq_postinstall(dev);
1770 }
1771
Eric Anholt673a3942008-07-30 12:06:12 -07001772 dev_priv->mm.waiting_gem_seqno = seqno;
1773 i915_user_irq_get(dev);
1774 ret = wait_event_interruptible(dev_priv->irq_queue,
1775 i915_seqno_passed(i915_get_gem_seqno(dev),
1776 seqno) ||
1777 dev_priv->mm.wedged);
1778 i915_user_irq_put(dev);
1779 dev_priv->mm.waiting_gem_seqno = 0;
1780 }
1781 if (dev_priv->mm.wedged)
1782 ret = -EIO;
1783
1784 if (ret && ret != -ERESTARTSYS)
1785 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1786 __func__, ret, seqno, i915_get_gem_seqno(dev));
1787
1788 /* Directly dispatch request retiring. While we have the work queue
1789 * to handle this, the waiter on a request often wants an associated
1790 * buffer to have made it to the inactive list, and we would need
1791 * a separate wait queue to handle that.
1792 */
1793 if (ret == 0)
1794 i915_gem_retire_requests(dev);
1795
1796 return ret;
1797}
1798
1799static void
1800i915_gem_flush(struct drm_device *dev,
1801 uint32_t invalidate_domains,
1802 uint32_t flush_domains)
1803{
1804 drm_i915_private_t *dev_priv = dev->dev_private;
1805 uint32_t cmd;
1806 RING_LOCALS;
1807
1808#if WATCH_EXEC
1809 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1810 invalidate_domains, flush_domains);
1811#endif
1812
1813 if (flush_domains & I915_GEM_DOMAIN_CPU)
1814 drm_agp_chipset_flush(dev);
1815
Chris Wilson21d509e2009-06-06 09:46:02 +01001816 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
Eric Anholt673a3942008-07-30 12:06:12 -07001817 /*
1818 * read/write caches:
1819 *
1820 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1821 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1822 * also flushed at 2d versus 3d pipeline switches.
1823 *
1824 * read-only caches:
1825 *
1826 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1827 * MI_READ_FLUSH is set, and is always flushed on 965.
1828 *
1829 * I915_GEM_DOMAIN_COMMAND may not exist?
1830 *
1831 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1832 * invalidated when MI_EXE_FLUSH is set.
1833 *
1834 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1835 * invalidated with every MI_FLUSH.
1836 *
1837 * TLBs:
1838 *
1839 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1840 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1841 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1842 * are flushed at any MI_FLUSH.
1843 */
1844
1845 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1846 if ((invalidate_domains|flush_domains) &
1847 I915_GEM_DOMAIN_RENDER)
1848 cmd &= ~MI_NO_WRITE_FLUSH;
1849 if (!IS_I965G(dev)) {
1850 /*
1851 * On the 965, the sampler cache always gets flushed
1852 * and this bit is reserved.
1853 */
1854 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1855 cmd |= MI_READ_FLUSH;
1856 }
1857 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1858 cmd |= MI_EXE_FLUSH;
1859
1860#if WATCH_EXEC
1861 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1862#endif
1863 BEGIN_LP_RING(2);
1864 OUT_RING(cmd);
1865 OUT_RING(0); /* noop */
1866 ADVANCE_LP_RING();
1867 }
1868}
1869
1870/**
1871 * Ensures that all rendering to the object has completed and the object is
1872 * safe to unbind from the GTT or access from the CPU.
1873 */
1874static int
1875i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1876{
1877 struct drm_device *dev = obj->dev;
1878 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1879 int ret;
1880
Eric Anholte47c68e2008-11-14 13:35:19 -08001881 /* This function only exists to support waiting for existing rendering,
1882 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001883 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001884 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001885
1886 /* If there is rendering queued on the buffer being evicted, wait for
1887 * it.
1888 */
1889 if (obj_priv->active) {
1890#if WATCH_BUF
1891 DRM_INFO("%s: object %p wait for seqno %08x\n",
1892 __func__, obj, obj_priv->last_rendering_seqno);
1893#endif
1894 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1895 if (ret != 0)
1896 return ret;
1897 }
1898
1899 return 0;
1900}
1901
1902/**
1903 * Unbinds an object from the GTT aperture.
1904 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001905int
Eric Anholt673a3942008-07-30 12:06:12 -07001906i915_gem_object_unbind(struct drm_gem_object *obj)
1907{
1908 struct drm_device *dev = obj->dev;
1909 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1910 int ret = 0;
1911
1912#if WATCH_BUF
1913 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1914 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1915#endif
1916 if (obj_priv->gtt_space == NULL)
1917 return 0;
1918
1919 if (obj_priv->pin_count != 0) {
1920 DRM_ERROR("Attempting to unbind pinned buffer\n");
1921 return -EINVAL;
1922 }
1923
Eric Anholt5323fd02009-09-09 11:50:45 -07001924 /* blow away mappings if mapped through GTT */
1925 i915_gem_release_mmap(obj);
1926
1927 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1928 i915_gem_clear_fence_reg(obj);
1929
Eric Anholt673a3942008-07-30 12:06:12 -07001930 /* Move the object to the CPU domain to ensure that
1931 * any possible CPU writes while it's not in the GTT
1932 * are flushed when we go to remap it. This will
1933 * also ensure that all pending GPU writes are finished
1934 * before we unbind.
1935 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001936 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001937 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001938 if (ret != -ERESTARTSYS)
1939 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001940 return ret;
1941 }
1942
Eric Anholt5323fd02009-09-09 11:50:45 -07001943 BUG_ON(obj_priv->active);
1944
Eric Anholt673a3942008-07-30 12:06:12 -07001945 if (obj_priv->agp_mem != NULL) {
1946 drm_unbind_agp(obj_priv->agp_mem);
1947 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1948 obj_priv->agp_mem = NULL;
1949 }
1950
Eric Anholt856fa192009-03-19 14:10:50 -07001951 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001952
1953 if (obj_priv->gtt_space) {
1954 atomic_dec(&dev->gtt_count);
1955 atomic_sub(obj->size, &dev->gtt_memory);
1956
1957 drm_mm_put_block(obj_priv->gtt_space);
1958 obj_priv->gtt_space = NULL;
1959 }
1960
1961 /* Remove ourselves from the LRU list if present. */
1962 if (!list_empty(&obj_priv->list))
1963 list_del_init(&obj_priv->list);
1964
1965 return 0;
1966}
1967
1968static int
1969i915_gem_evict_something(struct drm_device *dev)
1970{
1971 drm_i915_private_t *dev_priv = dev->dev_private;
1972 struct drm_gem_object *obj;
1973 struct drm_i915_gem_object *obj_priv;
1974 int ret = 0;
1975
1976 for (;;) {
1977 /* If there's an inactive buffer available now, grab it
1978 * and be done.
1979 */
1980 if (!list_empty(&dev_priv->mm.inactive_list)) {
1981 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1982 struct drm_i915_gem_object,
1983 list);
1984 obj = obj_priv->obj;
1985 BUG_ON(obj_priv->pin_count != 0);
1986#if WATCH_LRU
1987 DRM_INFO("%s: evicting %p\n", __func__, obj);
1988#endif
1989 BUG_ON(obj_priv->active);
1990
1991 /* Wait on the rendering and unbind the buffer. */
1992 ret = i915_gem_object_unbind(obj);
1993 break;
1994 }
1995
1996 /* If we didn't get anything, but the ring is still processing
1997 * things, wait for one of those things to finish and hopefully
1998 * leave us a buffer to evict.
1999 */
2000 if (!list_empty(&dev_priv->mm.request_list)) {
2001 struct drm_i915_gem_request *request;
2002
2003 request = list_first_entry(&dev_priv->mm.request_list,
2004 struct drm_i915_gem_request,
2005 list);
2006
2007 ret = i915_wait_request(dev, request->seqno);
2008 if (ret)
2009 break;
2010
2011 /* if waiting caused an object to become inactive,
2012 * then loop around and wait for it. Otherwise, we
2013 * assume that waiting freed and unbound something,
2014 * so there should now be some space in the GTT
2015 */
2016 if (!list_empty(&dev_priv->mm.inactive_list))
2017 continue;
2018 break;
2019 }
2020
2021 /* If we didn't have anything on the request list but there
2022 * are buffers awaiting a flush, emit one and try again.
2023 * When we wait on it, those buffers waiting for that flush
2024 * will get moved to inactive.
2025 */
2026 if (!list_empty(&dev_priv->mm.flushing_list)) {
2027 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
2028 struct drm_i915_gem_object,
2029 list);
2030 obj = obj_priv->obj;
2031
2032 i915_gem_flush(dev,
2033 obj->write_domain,
2034 obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002035 i915_add_request(dev, NULL, obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002036
2037 obj = NULL;
2038 continue;
2039 }
2040
2041 DRM_ERROR("inactive empty %d request empty %d "
2042 "flushing empty %d\n",
2043 list_empty(&dev_priv->mm.inactive_list),
2044 list_empty(&dev_priv->mm.request_list),
2045 list_empty(&dev_priv->mm.flushing_list));
2046 /* If we didn't do any of the above, there's nothing to be done
2047 * and we just can't fit it in.
2048 */
Chris Wilson2939e1f2009-06-06 09:46:03 +01002049 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002050 }
2051 return ret;
2052}
2053
2054static int
Keith Packardac94a962008-11-20 23:30:27 -08002055i915_gem_evict_everything(struct drm_device *dev)
2056{
2057 int ret;
2058
2059 for (;;) {
2060 ret = i915_gem_evict_something(dev);
2061 if (ret != 0)
2062 break;
2063 }
Chris Wilson2939e1f2009-06-06 09:46:03 +01002064 if (ret == -ENOSPC)
Owain Ainsworth15c35332008-12-06 20:42:20 -08002065 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08002066 return ret;
2067}
2068
Ben Gamari6911a9b2009-04-02 11:24:54 -07002069int
Eric Anholt856fa192009-03-19 14:10:50 -07002070i915_gem_object_get_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002071{
2072 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2073 int page_count, i;
2074 struct address_space *mapping;
2075 struct inode *inode;
2076 struct page *page;
2077 int ret;
2078
Eric Anholt856fa192009-03-19 14:10:50 -07002079 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002080 return 0;
2081
2082 /* Get the list of pages out of our struct file. They'll be pinned
2083 * at this point until we release them.
2084 */
2085 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002086 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002087 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002088 if (obj_priv->pages == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002089 DRM_ERROR("Faled to allocate page list\n");
Eric Anholt856fa192009-03-19 14:10:50 -07002090 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002091 return -ENOMEM;
2092 }
2093
2094 inode = obj->filp->f_path.dentry->d_inode;
2095 mapping = inode->i_mapping;
2096 for (i = 0; i < page_count; i++) {
2097 page = read_mapping_page(mapping, i, NULL);
2098 if (IS_ERR(page)) {
2099 ret = PTR_ERR(page);
2100 DRM_ERROR("read_mapping_page failed: %d\n", ret);
Eric Anholt856fa192009-03-19 14:10:50 -07002101 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002102 return ret;
2103 }
Eric Anholt856fa192009-03-19 14:10:50 -07002104 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002105 }
Eric Anholt280b7132009-03-12 16:56:27 -07002106
2107 if (obj_priv->tiling_mode != I915_TILING_NONE)
2108 i915_gem_object_do_bit_17_swizzle(obj);
2109
Eric Anholt673a3942008-07-30 12:06:12 -07002110 return 0;
2111}
2112
Jesse Barnesde151cf2008-11-12 10:03:55 -08002113static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2114{
2115 struct drm_gem_object *obj = reg->obj;
2116 struct drm_device *dev = obj->dev;
2117 drm_i915_private_t *dev_priv = dev->dev_private;
2118 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2119 int regnum = obj_priv->fence_reg;
2120 uint64_t val;
2121
2122 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2123 0xfffff000) << 32;
2124 val |= obj_priv->gtt_offset & 0xfffff000;
2125 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2126 if (obj_priv->tiling_mode == I915_TILING_Y)
2127 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2128 val |= I965_FENCE_REG_VALID;
2129
2130 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2131}
2132
2133static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2134{
2135 struct drm_gem_object *obj = reg->obj;
2136 struct drm_device *dev = obj->dev;
2137 drm_i915_private_t *dev_priv = dev->dev_private;
2138 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2139 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002140 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002141 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002142 uint32_t pitch_val;
2143
2144 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2145 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002146 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002147 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002148 return;
2149 }
2150
Jesse Barnes0f973f22009-01-26 17:10:45 -08002151 if (obj_priv->tiling_mode == I915_TILING_Y &&
2152 HAS_128_BYTE_Y_TILING(dev))
2153 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002154 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002155 tile_width = 512;
2156
2157 /* Note: pitch better be a power of two tile widths */
2158 pitch_val = obj_priv->stride / tile_width;
2159 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002160
2161 val = obj_priv->gtt_offset;
2162 if (obj_priv->tiling_mode == I915_TILING_Y)
2163 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2164 val |= I915_FENCE_SIZE_BITS(obj->size);
2165 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2166 val |= I830_FENCE_REG_VALID;
2167
Eric Anholtdc529a42009-03-10 22:34:49 -07002168 if (regnum < 8)
2169 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2170 else
2171 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2172 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002173}
2174
2175static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2176{
2177 struct drm_gem_object *obj = reg->obj;
2178 struct drm_device *dev = obj->dev;
2179 drm_i915_private_t *dev_priv = dev->dev_private;
2180 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2181 int regnum = obj_priv->fence_reg;
2182 uint32_t val;
2183 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002184 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002185
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002186 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002187 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002188 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002189 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002190 return;
2191 }
2192
Eric Anholte76a16d2009-05-26 17:44:56 -07002193 pitch_val = obj_priv->stride / 128;
2194 pitch_val = ffs(pitch_val) - 1;
2195 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2196
Jesse Barnesde151cf2008-11-12 10:03:55 -08002197 val = obj_priv->gtt_offset;
2198 if (obj_priv->tiling_mode == I915_TILING_Y)
2199 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002200 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2201 WARN_ON(fence_size_bits & ~0x00000f00);
2202 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002203 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2204 val |= I830_FENCE_REG_VALID;
2205
2206 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002207}
2208
2209/**
2210 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2211 * @obj: object to map through a fence reg
2212 *
2213 * When mapping objects through the GTT, userspace wants to be able to write
2214 * to them without having to worry about swizzling if the object is tiled.
2215 *
2216 * This function walks the fence regs looking for a free one for @obj,
2217 * stealing one if it can't find any.
2218 *
2219 * It then sets up the reg based on the object's properties: address, pitch
2220 * and tiling format.
2221 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002222int
2223i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002224{
2225 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002226 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002227 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2228 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002229 struct drm_i915_gem_object *old_obj_priv = NULL;
2230 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002231
Eric Anholta09ba7f2009-08-29 12:49:51 -07002232 /* Just update our place in the LRU if our fence is getting used. */
2233 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
2234 list_move_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2235 return 0;
2236 }
2237
Jesse Barnesde151cf2008-11-12 10:03:55 -08002238 switch (obj_priv->tiling_mode) {
2239 case I915_TILING_NONE:
2240 WARN(1, "allocating a fence for non-tiled object?\n");
2241 break;
2242 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002243 if (!obj_priv->stride)
2244 return -EINVAL;
2245 WARN((obj_priv->stride & (512 - 1)),
2246 "object 0x%08x is X tiled but has non-512B pitch\n",
2247 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002248 break;
2249 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002250 if (!obj_priv->stride)
2251 return -EINVAL;
2252 WARN((obj_priv->stride & (128 - 1)),
2253 "object 0x%08x is Y tiled but has non-128B pitch\n",
2254 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002255 break;
2256 }
2257
2258 /* First try to find a free reg */
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002259 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002260 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2261 reg = &dev_priv->fence_regs[i];
2262 if (!reg->obj)
2263 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002264
2265 old_obj_priv = reg->obj->driver_private;
2266 if (!old_obj_priv->pin_count)
2267 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002268 }
2269
2270 /* None available, try to steal one or wait for a user to finish */
2271 if (i == dev_priv->num_fence_regs) {
Eric Anholta09ba7f2009-08-29 12:49:51 -07002272 struct drm_gem_object *old_obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002273
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002274 if (avail == 0)
Chris Wilson2939e1f2009-06-06 09:46:03 +01002275 return -ENOSPC;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002276
Eric Anholta09ba7f2009-08-29 12:49:51 -07002277 list_for_each_entry(old_obj_priv, &dev_priv->mm.fence_list,
2278 fence_list) {
2279 old_obj = old_obj_priv->obj;
Chris Wilsond7619c42009-02-11 14:26:47 +00002280
2281 if (old_obj_priv->pin_count)
2282 continue;
2283
Eric Anholta09ba7f2009-08-29 12:49:51 -07002284 /* Take a reference, as otherwise the wait_rendering
2285 * below may cause the object to get freed out from
2286 * under us.
2287 */
2288 drm_gem_object_reference(old_obj);
2289
Chris Wilsond7619c42009-02-11 14:26:47 +00002290 /* i915 uses fences for GPU access to tiled buffers */
2291 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002292 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002293
Eric Anholta09ba7f2009-08-29 12:49:51 -07002294 /* This brings the object to the head of the LRU if it
2295 * had been written to. The only way this should
2296 * result in us waiting longer than the expected
2297 * optimal amount of time is if there was a
2298 * fence-using buffer later that was read-only.
2299 */
2300 i915_gem_object_flush_gpu_write_domain(old_obj);
2301 ret = i915_gem_object_wait_rendering(old_obj);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002302 if (ret != 0) {
2303 drm_gem_object_unreference(old_obj);
Chris Wilsond7619c42009-02-11 14:26:47 +00002304 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002305 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002306
Eric Anholta09ba7f2009-08-29 12:49:51 -07002307 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308 }
2309
2310 /*
2311 * Zap this virtual mapping so we can set up a fence again
2312 * for this object next time we need it.
2313 */
Chris Wilson58c2fb62009-09-01 12:02:39 +01002314 i915_gem_release_mmap(old_obj);
2315
Eric Anholta09ba7f2009-08-29 12:49:51 -07002316 i = old_obj_priv->fence_reg;
Chris Wilson58c2fb62009-09-01 12:02:39 +01002317 reg = &dev_priv->fence_regs[i];
2318
Jesse Barnesde151cf2008-11-12 10:03:55 -08002319 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002320 list_del_init(&old_obj_priv->fence_list);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002321
Eric Anholta09ba7f2009-08-29 12:49:51 -07002322 drm_gem_object_unreference(old_obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002323 }
2324
2325 obj_priv->fence_reg = i;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002326 list_add_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2327
Jesse Barnesde151cf2008-11-12 10:03:55 -08002328 reg->obj = obj;
2329
2330 if (IS_I965G(dev))
2331 i965_write_fence_reg(reg);
2332 else if (IS_I9XX(dev))
2333 i915_write_fence_reg(reg);
2334 else
2335 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002336
2337 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002338}
2339
2340/**
2341 * i915_gem_clear_fence_reg - clear out fence register info
2342 * @obj: object to clear
2343 *
2344 * Zeroes out the fence register itself and clears out the associated
2345 * data structures in dev_priv and obj_priv.
2346 */
2347static void
2348i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2349{
2350 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002351 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2353
2354 if (IS_I965G(dev))
2355 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002356 else {
2357 uint32_t fence_reg;
2358
2359 if (obj_priv->fence_reg < 8)
2360 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2361 else
2362 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2363 8) * 4;
2364
2365 I915_WRITE(fence_reg, 0);
2366 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002367
2368 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2369 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002370 list_del_init(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002371}
2372
Eric Anholt673a3942008-07-30 12:06:12 -07002373/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002374 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2375 * to the buffer to finish, and then resets the fence register.
2376 * @obj: tiled object holding a fence register.
2377 *
2378 * Zeroes out the fence register itself and clears out the associated
2379 * data structures in dev_priv and obj_priv.
2380 */
2381int
2382i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2383{
2384 struct drm_device *dev = obj->dev;
2385 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2386
2387 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2388 return 0;
2389
2390 /* On the i915, GPU access to tiled buffers is via a fence,
2391 * therefore we must wait for any outstanding access to complete
2392 * before clearing the fence.
2393 */
2394 if (!IS_I965G(dev)) {
2395 int ret;
2396
2397 i915_gem_object_flush_gpu_write_domain(obj);
2398 i915_gem_object_flush_gtt_write_domain(obj);
2399 ret = i915_gem_object_wait_rendering(obj);
2400 if (ret != 0)
2401 return ret;
2402 }
2403
2404 i915_gem_clear_fence_reg (obj);
2405
2406 return 0;
2407}
2408
2409/**
Eric Anholt673a3942008-07-30 12:06:12 -07002410 * Finds free space in the GTT aperture and binds the object there.
2411 */
2412static int
2413i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2414{
2415 struct drm_device *dev = obj->dev;
2416 drm_i915_private_t *dev_priv = dev->dev_private;
2417 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2418 struct drm_mm_node *free_space;
2419 int page_count, ret;
2420
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002421 if (dev_priv->mm.suspended)
2422 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002423 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002424 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002425 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002426 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2427 return -EINVAL;
2428 }
2429
2430 search_free:
2431 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2432 obj->size, alignment, 0);
2433 if (free_space != NULL) {
2434 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2435 alignment);
2436 if (obj_priv->gtt_space != NULL) {
2437 obj_priv->gtt_space->private = obj;
2438 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2439 }
2440 }
2441 if (obj_priv->gtt_space == NULL) {
Carl Worth5e118f42009-03-20 11:54:25 -07002442 bool lists_empty;
2443
Eric Anholt673a3942008-07-30 12:06:12 -07002444 /* If the gtt is empty and we're still having trouble
2445 * fitting our object in, we're out of memory.
2446 */
2447#if WATCH_LRU
2448 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2449#endif
Carl Worth5e118f42009-03-20 11:54:25 -07002450 spin_lock(&dev_priv->mm.active_list_lock);
2451 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2452 list_empty(&dev_priv->mm.flushing_list) &&
2453 list_empty(&dev_priv->mm.active_list));
2454 spin_unlock(&dev_priv->mm.active_list_lock);
2455 if (lists_empty) {
Eric Anholt673a3942008-07-30 12:06:12 -07002456 DRM_ERROR("GTT full, but LRU list empty\n");
Chris Wilson2939e1f2009-06-06 09:46:03 +01002457 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002458 }
2459
2460 ret = i915_gem_evict_something(dev);
2461 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08002462 if (ret != -ERESTARTSYS)
2463 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002464 return ret;
2465 }
2466 goto search_free;
2467 }
2468
2469#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002470 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002471 obj->size, obj_priv->gtt_offset);
2472#endif
Eric Anholt856fa192009-03-19 14:10:50 -07002473 ret = i915_gem_object_get_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002474 if (ret) {
2475 drm_mm_put_block(obj_priv->gtt_space);
2476 obj_priv->gtt_space = NULL;
2477 return ret;
2478 }
2479
2480 page_count = obj->size / PAGE_SIZE;
2481 /* Create an AGP memory structure pointing at our pages, and bind it
2482 * into the GTT.
2483 */
2484 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002485 obj_priv->pages,
Eric Anholt673a3942008-07-30 12:06:12 -07002486 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002487 obj_priv->gtt_offset,
2488 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002489 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002490 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002491 drm_mm_put_block(obj_priv->gtt_space);
2492 obj_priv->gtt_space = NULL;
2493 return -ENOMEM;
2494 }
2495 atomic_inc(&dev->gtt_count);
2496 atomic_add(obj->size, &dev->gtt_memory);
2497
2498 /* Assert that the object is not currently in any GPU domain. As it
2499 * wasn't in the GTT, there shouldn't be any way it could have been in
2500 * a GPU cache
2501 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002502 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2503 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002504
2505 return 0;
2506}
2507
2508void
2509i915_gem_clflush_object(struct drm_gem_object *obj)
2510{
2511 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2512
2513 /* If we don't have a page list set up, then we're not pinned
2514 * to GPU, and we can ignore the cache flush because it'll happen
2515 * again at bind time.
2516 */
Eric Anholt856fa192009-03-19 14:10:50 -07002517 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002518 return;
2519
Eric Anholt856fa192009-03-19 14:10:50 -07002520 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002521}
2522
Eric Anholte47c68e2008-11-14 13:35:19 -08002523/** Flushes any GPU write domain for the object if it's dirty. */
2524static void
2525i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2526{
2527 struct drm_device *dev = obj->dev;
2528 uint32_t seqno;
2529
2530 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2531 return;
2532
2533 /* Queue the GPU write cache flushing we need. */
2534 i915_gem_flush(dev, 0, obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002535 seqno = i915_add_request(dev, NULL, obj->write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002536 obj->write_domain = 0;
2537 i915_gem_object_move_to_active(obj, seqno);
2538}
2539
2540/** Flushes the GTT write domain for the object if it's dirty. */
2541static void
2542i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2543{
2544 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2545 return;
2546
2547 /* No actual flushing is required for the GTT write domain. Writes
2548 * to it immediately go to main memory as far as we know, so there's
2549 * no chipset flush. It also doesn't land in render cache.
2550 */
2551 obj->write_domain = 0;
2552}
2553
2554/** Flushes the CPU write domain for the object if it's dirty. */
2555static void
2556i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2557{
2558 struct drm_device *dev = obj->dev;
2559
2560 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2561 return;
2562
2563 i915_gem_clflush_object(obj);
2564 drm_agp_chipset_flush(dev);
2565 obj->write_domain = 0;
2566}
2567
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002568/**
2569 * Moves a single object to the GTT read, and possibly write domain.
2570 *
2571 * This function returns when the move is complete, including waiting on
2572 * flushes to occur.
2573 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002574int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002575i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2576{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002577 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002578 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002579
Eric Anholt02354392008-11-26 13:58:13 -08002580 /* Not valid to be called on unbound objects. */
2581 if (obj_priv->gtt_space == NULL)
2582 return -EINVAL;
2583
Eric Anholte47c68e2008-11-14 13:35:19 -08002584 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002585 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002586 ret = i915_gem_object_wait_rendering(obj);
2587 if (ret != 0)
2588 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002589
2590 /* If we're writing through the GTT domain, then CPU and GPU caches
2591 * will need to be invalidated at next use.
2592 */
2593 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002594 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002595
Eric Anholte47c68e2008-11-14 13:35:19 -08002596 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002597
2598 /* It should now be out of any other write domains, and we can update
2599 * the domain values for our changes.
2600 */
2601 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2602 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002603 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002604 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002605 obj_priv->dirty = 1;
2606 }
2607
2608 return 0;
2609}
2610
2611/**
2612 * Moves a single object to the CPU read, and possibly write domain.
2613 *
2614 * This function returns when the move is complete, including waiting on
2615 * flushes to occur.
2616 */
2617static int
2618i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2619{
Eric Anholte47c68e2008-11-14 13:35:19 -08002620 int ret;
2621
2622 i915_gem_object_flush_gpu_write_domain(obj);
2623 /* Wait on any GPU rendering and flushing to occur. */
2624 ret = i915_gem_object_wait_rendering(obj);
2625 if (ret != 0)
2626 return ret;
2627
2628 i915_gem_object_flush_gtt_write_domain(obj);
2629
2630 /* If we have a partially-valid cache of the object in the CPU,
2631 * finish invalidating it and free the per-page flags.
2632 */
2633 i915_gem_object_set_to_full_cpu_read_domain(obj);
2634
2635 /* Flush the CPU cache if it's still invalid. */
2636 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2637 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002638
2639 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2640 }
2641
2642 /* It should now be out of any other write domains, and we can update
2643 * the domain values for our changes.
2644 */
2645 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2646
2647 /* If we're writing through the CPU, then the GPU read domains will
2648 * need to be invalidated at next use.
2649 */
2650 if (write) {
2651 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2652 obj->write_domain = I915_GEM_DOMAIN_CPU;
2653 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002654
2655 return 0;
2656}
2657
Eric Anholt673a3942008-07-30 12:06:12 -07002658/*
2659 * Set the next domain for the specified object. This
2660 * may not actually perform the necessary flushing/invaliding though,
2661 * as that may want to be batched with other set_domain operations
2662 *
2663 * This is (we hope) the only really tricky part of gem. The goal
2664 * is fairly simple -- track which caches hold bits of the object
2665 * and make sure they remain coherent. A few concrete examples may
2666 * help to explain how it works. For shorthand, we use the notation
2667 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2668 * a pair of read and write domain masks.
2669 *
2670 * Case 1: the batch buffer
2671 *
2672 * 1. Allocated
2673 * 2. Written by CPU
2674 * 3. Mapped to GTT
2675 * 4. Read by GPU
2676 * 5. Unmapped from GTT
2677 * 6. Freed
2678 *
2679 * Let's take these a step at a time
2680 *
2681 * 1. Allocated
2682 * Pages allocated from the kernel may still have
2683 * cache contents, so we set them to (CPU, CPU) always.
2684 * 2. Written by CPU (using pwrite)
2685 * The pwrite function calls set_domain (CPU, CPU) and
2686 * this function does nothing (as nothing changes)
2687 * 3. Mapped by GTT
2688 * This function asserts that the object is not
2689 * currently in any GPU-based read or write domains
2690 * 4. Read by GPU
2691 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2692 * As write_domain is zero, this function adds in the
2693 * current read domains (CPU+COMMAND, 0).
2694 * flush_domains is set to CPU.
2695 * invalidate_domains is set to COMMAND
2696 * clflush is run to get data out of the CPU caches
2697 * then i915_dev_set_domain calls i915_gem_flush to
2698 * emit an MI_FLUSH and drm_agp_chipset_flush
2699 * 5. Unmapped from GTT
2700 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2701 * flush_domains and invalidate_domains end up both zero
2702 * so no flushing/invalidating happens
2703 * 6. Freed
2704 * yay, done
2705 *
2706 * Case 2: The shared render buffer
2707 *
2708 * 1. Allocated
2709 * 2. Mapped to GTT
2710 * 3. Read/written by GPU
2711 * 4. set_domain to (CPU,CPU)
2712 * 5. Read/written by CPU
2713 * 6. Read/written by GPU
2714 *
2715 * 1. Allocated
2716 * Same as last example, (CPU, CPU)
2717 * 2. Mapped to GTT
2718 * Nothing changes (assertions find that it is not in the GPU)
2719 * 3. Read/written by GPU
2720 * execbuffer calls set_domain (RENDER, RENDER)
2721 * flush_domains gets CPU
2722 * invalidate_domains gets GPU
2723 * clflush (obj)
2724 * MI_FLUSH and drm_agp_chipset_flush
2725 * 4. set_domain (CPU, CPU)
2726 * flush_domains gets GPU
2727 * invalidate_domains gets CPU
2728 * wait_rendering (obj) to make sure all drawing is complete.
2729 * This will include an MI_FLUSH to get the data from GPU
2730 * to memory
2731 * clflush (obj) to invalidate the CPU cache
2732 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2733 * 5. Read/written by CPU
2734 * cache lines are loaded and dirtied
2735 * 6. Read written by GPU
2736 * Same as last GPU access
2737 *
2738 * Case 3: The constant buffer
2739 *
2740 * 1. Allocated
2741 * 2. Written by CPU
2742 * 3. Read by GPU
2743 * 4. Updated (written) by CPU again
2744 * 5. Read by GPU
2745 *
2746 * 1. Allocated
2747 * (CPU, CPU)
2748 * 2. Written by CPU
2749 * (CPU, CPU)
2750 * 3. Read by GPU
2751 * (CPU+RENDER, 0)
2752 * flush_domains = CPU
2753 * invalidate_domains = RENDER
2754 * clflush (obj)
2755 * MI_FLUSH
2756 * drm_agp_chipset_flush
2757 * 4. Updated (written) by CPU again
2758 * (CPU, CPU)
2759 * flush_domains = 0 (no previous write domain)
2760 * invalidate_domains = 0 (no new read domains)
2761 * 5. Read by GPU
2762 * (CPU+RENDER, 0)
2763 * flush_domains = CPU
2764 * invalidate_domains = RENDER
2765 * clflush (obj)
2766 * MI_FLUSH
2767 * drm_agp_chipset_flush
2768 */
Keith Packardc0d90822008-11-20 23:11:08 -08002769static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002770i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002771{
2772 struct drm_device *dev = obj->dev;
2773 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2774 uint32_t invalidate_domains = 0;
2775 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002776
Eric Anholt8b0e3782009-02-19 14:40:50 -08002777 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2778 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002779
Jesse Barnes652c3932009-08-17 13:31:43 -07002780 intel_mark_busy(dev, obj);
2781
Eric Anholt673a3942008-07-30 12:06:12 -07002782#if WATCH_BUF
2783 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2784 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002785 obj->read_domains, obj->pending_read_domains,
2786 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002787#endif
2788 /*
2789 * If the object isn't moving to a new write domain,
2790 * let the object stay in multiple read domains
2791 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002792 if (obj->pending_write_domain == 0)
2793 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002794 else
2795 obj_priv->dirty = 1;
2796
2797 /*
2798 * Flush the current write domain if
2799 * the new read domains don't match. Invalidate
2800 * any read domains which differ from the old
2801 * write domain
2802 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002803 if (obj->write_domain &&
2804 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002805 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002806 invalidate_domains |=
2807 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002808 }
2809 /*
2810 * Invalidate any read caches which may have
2811 * stale data. That is, any new read domains.
2812 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002813 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002814 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2815#if WATCH_BUF
2816 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2817 __func__, flush_domains, invalidate_domains);
2818#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002819 i915_gem_clflush_object(obj);
2820 }
2821
Eric Anholtefbeed92009-02-19 14:54:51 -08002822 /* The actual obj->write_domain will be updated with
2823 * pending_write_domain after we emit the accumulated flush for all
2824 * of our domain changes in execbuffers (which clears objects'
2825 * write_domains). So if we have a current write domain that we
2826 * aren't changing, set pending_write_domain to that.
2827 */
2828 if (flush_domains == 0 && obj->pending_write_domain == 0)
2829 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002830 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002831
2832 dev->invalidate_domains |= invalidate_domains;
2833 dev->flush_domains |= flush_domains;
2834#if WATCH_BUF
2835 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2836 __func__,
2837 obj->read_domains, obj->write_domain,
2838 dev->invalidate_domains, dev->flush_domains);
2839#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002840}
2841
2842/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002843 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002844 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002845 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2846 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2847 */
2848static void
2849i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2850{
Eric Anholte47c68e2008-11-14 13:35:19 -08002851 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2852
2853 if (!obj_priv->page_cpu_valid)
2854 return;
2855
2856 /* If we're partially in the CPU read domain, finish moving it in.
2857 */
2858 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2859 int i;
2860
2861 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2862 if (obj_priv->page_cpu_valid[i])
2863 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07002864 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08002865 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002866 }
2867
2868 /* Free the page_cpu_valid mappings which are now stale, whether
2869 * or not we've got I915_GEM_DOMAIN_CPU.
2870 */
Eric Anholt9a298b22009-03-24 12:23:04 -07002871 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08002872 obj_priv->page_cpu_valid = NULL;
2873}
2874
2875/**
2876 * Set the CPU read domain on a range of the object.
2877 *
2878 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2879 * not entirely valid. The page_cpu_valid member of the object flags which
2880 * pages have been flushed, and will be respected by
2881 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2882 * of the whole object.
2883 *
2884 * This function returns when the move is complete, including waiting on
2885 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002886 */
2887static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002888i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2889 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002890{
2891 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002892 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002893
Eric Anholte47c68e2008-11-14 13:35:19 -08002894 if (offset == 0 && size == obj->size)
2895 return i915_gem_object_set_to_cpu_domain(obj, 0);
2896
2897 i915_gem_object_flush_gpu_write_domain(obj);
2898 /* Wait on any GPU rendering and flushing to occur. */
2899 ret = i915_gem_object_wait_rendering(obj);
2900 if (ret != 0)
2901 return ret;
2902 i915_gem_object_flush_gtt_write_domain(obj);
2903
2904 /* If we're already fully in the CPU read domain, we're done. */
2905 if (obj_priv->page_cpu_valid == NULL &&
2906 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002907 return 0;
2908
Eric Anholte47c68e2008-11-14 13:35:19 -08002909 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2910 * newly adding I915_GEM_DOMAIN_CPU
2911 */
Eric Anholt673a3942008-07-30 12:06:12 -07002912 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07002913 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
2914 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08002915 if (obj_priv->page_cpu_valid == NULL)
2916 return -ENOMEM;
2917 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2918 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002919
2920 /* Flush the cache on any pages that are still invalid from the CPU's
2921 * perspective.
2922 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002923 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2924 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002925 if (obj_priv->page_cpu_valid[i])
2926 continue;
2927
Eric Anholt856fa192009-03-19 14:10:50 -07002928 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002929
2930 obj_priv->page_cpu_valid[i] = 1;
2931 }
2932
Eric Anholte47c68e2008-11-14 13:35:19 -08002933 /* It should now be out of any other write domains, and we can update
2934 * the domain values for our changes.
2935 */
2936 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2937
2938 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2939
Eric Anholt673a3942008-07-30 12:06:12 -07002940 return 0;
2941}
2942
2943/**
Eric Anholt673a3942008-07-30 12:06:12 -07002944 * Pin an object to the GTT and evaluate the relocations landing in it.
2945 */
2946static int
2947i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2948 struct drm_file *file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002949 struct drm_i915_gem_exec_object *entry,
2950 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07002951{
2952 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002953 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002954 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2955 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002956 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002957
2958 /* Choose the GTT offset for our buffer and put it there. */
2959 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2960 if (ret)
2961 return ret;
2962
2963 entry->offset = obj_priv->gtt_offset;
2964
Eric Anholt673a3942008-07-30 12:06:12 -07002965 /* Apply the relocations, using the GTT aperture to avoid cache
2966 * flushing requirements.
2967 */
2968 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002969 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002970 struct drm_gem_object *target_obj;
2971 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002972 uint32_t reloc_val, reloc_offset;
2973 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002974
Eric Anholt673a3942008-07-30 12:06:12 -07002975 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002976 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002977 if (target_obj == NULL) {
2978 i915_gem_object_unpin(obj);
2979 return -EBADF;
2980 }
2981 target_obj_priv = target_obj->driver_private;
2982
2983 /* The target buffer should have appeared before us in the
2984 * exec_object list, so it should have a GTT space bound by now.
2985 */
2986 if (target_obj_priv->gtt_space == NULL) {
2987 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002988 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002989 drm_gem_object_unreference(target_obj);
2990 i915_gem_object_unpin(obj);
2991 return -EINVAL;
2992 }
2993
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002994 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07002995 DRM_ERROR("Relocation beyond object bounds: "
2996 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002997 obj, reloc->target_handle,
2998 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07002999 drm_gem_object_unreference(target_obj);
3000 i915_gem_object_unpin(obj);
3001 return -EINVAL;
3002 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003003 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003004 DRM_ERROR("Relocation not 4-byte aligned: "
3005 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003006 obj, reloc->target_handle,
3007 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003008 drm_gem_object_unreference(target_obj);
3009 i915_gem_object_unpin(obj);
3010 return -EINVAL;
3011 }
3012
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003013 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3014 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003015 DRM_ERROR("reloc with read/write CPU domains: "
3016 "obj %p target %d offset %d "
3017 "read %08x write %08x",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003018 obj, reloc->target_handle,
3019 (int) reloc->offset,
3020 reloc->read_domains,
3021 reloc->write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00003022 drm_gem_object_unreference(target_obj);
3023 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003024 return -EINVAL;
3025 }
3026
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003027 if (reloc->write_domain && target_obj->pending_write_domain &&
3028 reloc->write_domain != target_obj->pending_write_domain) {
Eric Anholt673a3942008-07-30 12:06:12 -07003029 DRM_ERROR("Write domain conflict: "
3030 "obj %p target %d offset %d "
3031 "new %08x old %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003032 obj, reloc->target_handle,
3033 (int) reloc->offset,
3034 reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003035 target_obj->pending_write_domain);
3036 drm_gem_object_unreference(target_obj);
3037 i915_gem_object_unpin(obj);
3038 return -EINVAL;
3039 }
3040
3041#if WATCH_RELOC
3042 DRM_INFO("%s: obj %p offset %08x target %d "
3043 "read %08x write %08x gtt %08x "
3044 "presumed %08x delta %08x\n",
3045 __func__,
3046 obj,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003047 (int) reloc->offset,
3048 (int) reloc->target_handle,
3049 (int) reloc->read_domains,
3050 (int) reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003051 (int) target_obj_priv->gtt_offset,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003052 (int) reloc->presumed_offset,
3053 reloc->delta);
Eric Anholt673a3942008-07-30 12:06:12 -07003054#endif
3055
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003056 target_obj->pending_read_domains |= reloc->read_domains;
3057 target_obj->pending_write_domain |= reloc->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003058
3059 /* If the relocation already has the right value in it, no
3060 * more work needs to be done.
3061 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003062 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
Eric Anholt673a3942008-07-30 12:06:12 -07003063 drm_gem_object_unreference(target_obj);
3064 continue;
3065 }
3066
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003067 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3068 if (ret != 0) {
3069 drm_gem_object_unreference(target_obj);
3070 i915_gem_object_unpin(obj);
3071 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003072 }
3073
3074 /* Map the page containing the relocation we're going to
3075 * perform.
3076 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003077 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003078 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3079 (reloc_offset &
3080 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003081 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003082 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003083 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003084
3085#if WATCH_BUF
3086 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003087 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003088 readl(reloc_entry), reloc_val);
3089#endif
3090 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003091 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003092
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003093 /* The updated presumed offset for this entry will be
3094 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003095 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003096 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003097
3098 drm_gem_object_unreference(target_obj);
3099 }
3100
Eric Anholt673a3942008-07-30 12:06:12 -07003101#if WATCH_BUF
3102 if (0)
3103 i915_gem_dump_object(obj, 128, __func__, ~0);
3104#endif
3105 return 0;
3106}
3107
3108/** Dispatch a batchbuffer to the ring
3109 */
3110static int
3111i915_dispatch_gem_execbuffer(struct drm_device *dev,
3112 struct drm_i915_gem_execbuffer *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003113 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003114 uint64_t exec_offset)
3115{
3116 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003117 int nbox = exec->num_cliprects;
3118 int i = 0, count;
Chris Wilson83d60792009-06-06 09:45:57 +01003119 uint32_t exec_start, exec_len;
Eric Anholt673a3942008-07-30 12:06:12 -07003120 RING_LOCALS;
3121
3122 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3123 exec_len = (uint32_t) exec->batch_len;
3124
Eric Anholt673a3942008-07-30 12:06:12 -07003125 count = nbox ? nbox : 1;
3126
3127 for (i = 0; i < count; i++) {
3128 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003129 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003130 exec->DR1, exec->DR4);
3131 if (ret)
3132 return ret;
3133 }
3134
3135 if (IS_I830(dev) || IS_845G(dev)) {
3136 BEGIN_LP_RING(4);
3137 OUT_RING(MI_BATCH_BUFFER);
3138 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3139 OUT_RING(exec_start + exec_len - 4);
3140 OUT_RING(0);
3141 ADVANCE_LP_RING();
3142 } else {
3143 BEGIN_LP_RING(2);
3144 if (IS_I965G(dev)) {
3145 OUT_RING(MI_BATCH_BUFFER_START |
3146 (2 << 6) |
3147 MI_BATCH_NON_SECURE_I965);
3148 OUT_RING(exec_start);
3149 } else {
3150 OUT_RING(MI_BATCH_BUFFER_START |
3151 (2 << 6));
3152 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3153 }
3154 ADVANCE_LP_RING();
3155 }
3156 }
3157
3158 /* XXX breadcrumb */
3159 return 0;
3160}
3161
3162/* Throttle our rendering by waiting until the ring has completed our requests
3163 * emitted over 20 msec ago.
3164 *
Eric Anholtb9624422009-06-03 07:27:35 +00003165 * Note that if we were to use the current jiffies each time around the loop,
3166 * we wouldn't escape the function with any frames outstanding if the time to
3167 * render a frame was over 20ms.
3168 *
Eric Anholt673a3942008-07-30 12:06:12 -07003169 * This should get us reasonable parallelism between CPU and GPU but also
3170 * relatively low latency when blocking on a particular request to finish.
3171 */
3172static int
3173i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3174{
3175 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3176 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003177 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003178
3179 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003180 while (!list_empty(&i915_file_priv->mm.request_list)) {
3181 struct drm_i915_gem_request *request;
3182
3183 request = list_first_entry(&i915_file_priv->mm.request_list,
3184 struct drm_i915_gem_request,
3185 client_list);
3186
3187 if (time_after_eq(request->emitted_jiffies, recent_enough))
3188 break;
3189
3190 ret = i915_wait_request(dev, request->seqno);
3191 if (ret != 0)
3192 break;
3193 }
Eric Anholt673a3942008-07-30 12:06:12 -07003194 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003195
Eric Anholt673a3942008-07-30 12:06:12 -07003196 return ret;
3197}
3198
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003199static int
3200i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3201 uint32_t buffer_count,
3202 struct drm_i915_gem_relocation_entry **relocs)
3203{
3204 uint32_t reloc_count = 0, reloc_index = 0, i;
3205 int ret;
3206
3207 *relocs = NULL;
3208 for (i = 0; i < buffer_count; i++) {
3209 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3210 return -EINVAL;
3211 reloc_count += exec_list[i].relocation_count;
3212 }
3213
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003214 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003215 if (*relocs == NULL)
3216 return -ENOMEM;
3217
3218 for (i = 0; i < buffer_count; i++) {
3219 struct drm_i915_gem_relocation_entry __user *user_relocs;
3220
3221 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3222
3223 ret = copy_from_user(&(*relocs)[reloc_index],
3224 user_relocs,
3225 exec_list[i].relocation_count *
3226 sizeof(**relocs));
3227 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003228 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003229 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003230 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003231 }
3232
3233 reloc_index += exec_list[i].relocation_count;
3234 }
3235
Florian Mickler2bc43b52009-04-06 22:55:41 +02003236 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003237}
3238
3239static int
3240i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3241 uint32_t buffer_count,
3242 struct drm_i915_gem_relocation_entry *relocs)
3243{
3244 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003245 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003246
3247 for (i = 0; i < buffer_count; i++) {
3248 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003249 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003250
3251 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3252
Florian Mickler2bc43b52009-04-06 22:55:41 +02003253 unwritten = copy_to_user(user_relocs,
3254 &relocs[reloc_count],
3255 exec_list[i].relocation_count *
3256 sizeof(*relocs));
3257
3258 if (unwritten) {
3259 ret = -EFAULT;
3260 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003261 }
3262
3263 reloc_count += exec_list[i].relocation_count;
3264 }
3265
Florian Mickler2bc43b52009-04-06 22:55:41 +02003266err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003267 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003268
3269 return ret;
3270}
3271
Chris Wilson83d60792009-06-06 09:45:57 +01003272static int
3273i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer *exec,
3274 uint64_t exec_offset)
3275{
3276 uint32_t exec_start, exec_len;
3277
3278 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3279 exec_len = (uint32_t) exec->batch_len;
3280
3281 if ((exec_start | exec_len) & 0x7)
3282 return -EINVAL;
3283
3284 if (!exec_start)
3285 return -EINVAL;
3286
3287 return 0;
3288}
3289
Eric Anholt673a3942008-07-30 12:06:12 -07003290int
3291i915_gem_execbuffer(struct drm_device *dev, void *data,
3292 struct drm_file *file_priv)
3293{
3294 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003295 struct drm_i915_gem_execbuffer *args = data;
3296 struct drm_i915_gem_exec_object *exec_list = NULL;
3297 struct drm_gem_object **object_list = NULL;
3298 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003299 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003300 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003301 struct drm_i915_gem_relocation_entry *relocs;
3302 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003303 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003304 uint32_t seqno, flush_domains, reloc_index;
Keith Packardac94a962008-11-20 23:30:27 -08003305 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07003306
3307#if WATCH_EXEC
3308 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3309 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3310#endif
3311
Eric Anholt4f481ed2008-09-10 14:22:49 -07003312 if (args->buffer_count < 1) {
3313 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3314 return -EINVAL;
3315 }
Eric Anholt673a3942008-07-30 12:06:12 -07003316 /* Copy in the exec list from userland */
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003317 exec_list = drm_calloc_large(sizeof(*exec_list), args->buffer_count);
3318 object_list = drm_calloc_large(sizeof(*object_list), args->buffer_count);
Eric Anholt673a3942008-07-30 12:06:12 -07003319 if (exec_list == NULL || object_list == NULL) {
3320 DRM_ERROR("Failed to allocate exec or object list "
3321 "for %d buffers\n",
3322 args->buffer_count);
3323 ret = -ENOMEM;
3324 goto pre_mutex_err;
3325 }
3326 ret = copy_from_user(exec_list,
3327 (struct drm_i915_relocation_entry __user *)
3328 (uintptr_t) args->buffers_ptr,
3329 sizeof(*exec_list) * args->buffer_count);
3330 if (ret != 0) {
3331 DRM_ERROR("copy %d exec entries failed %d\n",
3332 args->buffer_count, ret);
3333 goto pre_mutex_err;
3334 }
3335
Eric Anholt201361a2009-03-11 12:30:04 -07003336 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003337 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3338 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -07003339 if (cliprects == NULL)
3340 goto pre_mutex_err;
3341
3342 ret = copy_from_user(cliprects,
3343 (struct drm_clip_rect __user *)
3344 (uintptr_t) args->cliprects_ptr,
3345 sizeof(*cliprects) * args->num_cliprects);
3346 if (ret != 0) {
3347 DRM_ERROR("copy %d cliprects failed: %d\n",
3348 args->num_cliprects, ret);
3349 goto pre_mutex_err;
3350 }
3351 }
3352
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003353 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3354 &relocs);
3355 if (ret != 0)
3356 goto pre_mutex_err;
3357
Eric Anholt673a3942008-07-30 12:06:12 -07003358 mutex_lock(&dev->struct_mutex);
3359
3360 i915_verify_inactive(dev, __FILE__, __LINE__);
3361
3362 if (dev_priv->mm.wedged) {
3363 DRM_ERROR("Execbuf while wedged\n");
3364 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003365 ret = -EIO;
3366 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003367 }
3368
3369 if (dev_priv->mm.suspended) {
3370 DRM_ERROR("Execbuf while VT-switched.\n");
3371 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003372 ret = -EBUSY;
3373 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003374 }
3375
Keith Packardac94a962008-11-20 23:30:27 -08003376 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003377 for (i = 0; i < args->buffer_count; i++) {
3378 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3379 exec_list[i].handle);
3380 if (object_list[i] == NULL) {
3381 DRM_ERROR("Invalid object handle %d at index %d\n",
3382 exec_list[i].handle, i);
3383 ret = -EBADF;
3384 goto err;
3385 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003386
3387 obj_priv = object_list[i]->driver_private;
3388 if (obj_priv->in_execbuffer) {
3389 DRM_ERROR("Object %p appears more than once in object list\n",
3390 object_list[i]);
3391 ret = -EBADF;
3392 goto err;
3393 }
3394 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003395 }
Eric Anholt673a3942008-07-30 12:06:12 -07003396
Keith Packardac94a962008-11-20 23:30:27 -08003397 /* Pin and relocate */
3398 for (pin_tries = 0; ; pin_tries++) {
3399 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003400 reloc_index = 0;
3401
Keith Packardac94a962008-11-20 23:30:27 -08003402 for (i = 0; i < args->buffer_count; i++) {
3403 object_list[i]->pending_read_domains = 0;
3404 object_list[i]->pending_write_domain = 0;
3405 ret = i915_gem_object_pin_and_relocate(object_list[i],
3406 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003407 &exec_list[i],
3408 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003409 if (ret)
3410 break;
3411 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003412 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003413 }
3414 /* success */
3415 if (ret == 0)
3416 break;
3417
3418 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003419 if (ret != -ENOSPC || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08003420 if (ret != -ERESTARTSYS)
3421 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003422 goto err;
3423 }
Keith Packardac94a962008-11-20 23:30:27 -08003424
3425 /* unpin all of our buffers */
3426 for (i = 0; i < pinned; i++)
3427 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003428 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003429
3430 /* evict everyone we can from the aperture */
3431 ret = i915_gem_evict_everything(dev);
3432 if (ret)
3433 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003434 }
3435
3436 /* Set the pending read domains for the batch buffer to COMMAND */
3437 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003438 if (batch_obj->pending_write_domain) {
3439 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3440 ret = -EINVAL;
3441 goto err;
3442 }
3443 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003444
Chris Wilson83d60792009-06-06 09:45:57 +01003445 /* Sanity check the batch buffer, prior to moving objects */
3446 exec_offset = exec_list[args->buffer_count - 1].offset;
3447 ret = i915_gem_check_execbuffer (args, exec_offset);
3448 if (ret != 0) {
3449 DRM_ERROR("execbuf with invalid offset/length\n");
3450 goto err;
3451 }
3452
Eric Anholt673a3942008-07-30 12:06:12 -07003453 i915_verify_inactive(dev, __FILE__, __LINE__);
3454
Keith Packard646f0f62008-11-20 23:23:03 -08003455 /* Zero the global flush/invalidate flags. These
3456 * will be modified as new domains are computed
3457 * for each object
3458 */
3459 dev->invalidate_domains = 0;
3460 dev->flush_domains = 0;
3461
Eric Anholt673a3942008-07-30 12:06:12 -07003462 for (i = 0; i < args->buffer_count; i++) {
3463 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003464
Keith Packard646f0f62008-11-20 23:23:03 -08003465 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003466 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003467 }
3468
3469 i915_verify_inactive(dev, __FILE__, __LINE__);
3470
Keith Packard646f0f62008-11-20 23:23:03 -08003471 if (dev->invalidate_domains | dev->flush_domains) {
3472#if WATCH_EXEC
3473 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3474 __func__,
3475 dev->invalidate_domains,
3476 dev->flush_domains);
3477#endif
3478 i915_gem_flush(dev,
3479 dev->invalidate_domains,
3480 dev->flush_domains);
3481 if (dev->flush_domains)
Eric Anholtb9624422009-06-03 07:27:35 +00003482 (void)i915_add_request(dev, file_priv,
3483 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003484 }
Eric Anholt673a3942008-07-30 12:06:12 -07003485
Eric Anholtefbeed92009-02-19 14:54:51 -08003486 for (i = 0; i < args->buffer_count; i++) {
3487 struct drm_gem_object *obj = object_list[i];
3488
3489 obj->write_domain = obj->pending_write_domain;
3490 }
3491
Eric Anholt673a3942008-07-30 12:06:12 -07003492 i915_verify_inactive(dev, __FILE__, __LINE__);
3493
3494#if WATCH_COHERENCY
3495 for (i = 0; i < args->buffer_count; i++) {
3496 i915_gem_object_check_coherency(object_list[i],
3497 exec_list[i].handle);
3498 }
3499#endif
3500
Eric Anholt673a3942008-07-30 12:06:12 -07003501#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003502 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003503 args->batch_len,
3504 __func__,
3505 ~0);
3506#endif
3507
Eric Anholt673a3942008-07-30 12:06:12 -07003508 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003509 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003510 if (ret) {
3511 DRM_ERROR("dispatch failed %d\n", ret);
3512 goto err;
3513 }
3514
3515 /*
3516 * Ensure that the commands in the batch buffer are
3517 * finished before the interrupt fires
3518 */
3519 flush_domains = i915_retire_commands(dev);
3520
3521 i915_verify_inactive(dev, __FILE__, __LINE__);
3522
3523 /*
3524 * Get a seqno representing the execution of the current buffer,
3525 * which we can wait on. We would like to mitigate these interrupts,
3526 * likely by only creating seqnos occasionally (so that we have
3527 * *some* interrupts representing completion of buffers that we can
3528 * wait on when trying to clear up gtt space).
3529 */
Eric Anholtb9624422009-06-03 07:27:35 +00003530 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003531 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003532 for (i = 0; i < args->buffer_count; i++) {
3533 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003534
Eric Anholtce44b0e2008-11-06 16:00:31 -08003535 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003536#if WATCH_LRU
3537 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3538#endif
3539 }
3540#if WATCH_LRU
3541 i915_dump_lru(dev, __func__);
3542#endif
3543
3544 i915_verify_inactive(dev, __FILE__, __LINE__);
3545
Eric Anholt673a3942008-07-30 12:06:12 -07003546err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003547 for (i = 0; i < pinned; i++)
3548 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003549
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003550 for (i = 0; i < args->buffer_count; i++) {
3551 if (object_list[i]) {
3552 obj_priv = object_list[i]->driver_private;
3553 obj_priv->in_execbuffer = false;
3554 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003555 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003556 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003557
Eric Anholt673a3942008-07-30 12:06:12 -07003558 mutex_unlock(&dev->struct_mutex);
3559
Roland Dreiera35f2e22009-02-06 17:48:09 -08003560 if (!ret) {
3561 /* Copy the new buffer offsets back to the user's exec list. */
3562 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3563 (uintptr_t) args->buffers_ptr,
3564 exec_list,
3565 sizeof(*exec_list) * args->buffer_count);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003566 if (ret) {
3567 ret = -EFAULT;
Roland Dreiera35f2e22009-02-06 17:48:09 -08003568 DRM_ERROR("failed to copy %d exec entries "
3569 "back to user (%d)\n",
3570 args->buffer_count, ret);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003571 }
Roland Dreiera35f2e22009-02-06 17:48:09 -08003572 }
3573
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003574 /* Copy the updated relocations out regardless of current error
3575 * state. Failure to update the relocs would mean that the next
3576 * time userland calls execbuf, it would do so with presumed offset
3577 * state that didn't match the actual object state.
3578 */
3579 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3580 relocs);
3581 if (ret2 != 0) {
3582 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3583
3584 if (ret == 0)
3585 ret = ret2;
3586 }
3587
Eric Anholt673a3942008-07-30 12:06:12 -07003588pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003589 drm_free_large(object_list);
3590 drm_free_large(exec_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003591 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003592
3593 return ret;
3594}
3595
3596int
3597i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3598{
3599 struct drm_device *dev = obj->dev;
3600 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3601 int ret;
3602
3603 i915_verify_inactive(dev, __FILE__, __LINE__);
3604 if (obj_priv->gtt_space == NULL) {
3605 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3606 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003607 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05003608 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003609 return ret;
3610 }
Chris Wilson22c344e2009-02-11 14:26:45 +00003611 }
3612 /*
3613 * Pre-965 chips need a fence register set up in order to
3614 * properly handle tiled surfaces.
3615 */
Eric Anholta09ba7f2009-08-29 12:49:51 -07003616 if (!IS_I965G(dev) && obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003617 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson22c344e2009-02-11 14:26:45 +00003618 if (ret != 0) {
3619 if (ret != -EBUSY && ret != -ERESTARTSYS)
3620 DRM_ERROR("Failure to install fence: %d\n",
3621 ret);
3622 return ret;
3623 }
Eric Anholt673a3942008-07-30 12:06:12 -07003624 }
3625 obj_priv->pin_count++;
3626
3627 /* If the object is not active and not pending a flush,
3628 * remove it from the inactive list
3629 */
3630 if (obj_priv->pin_count == 1) {
3631 atomic_inc(&dev->pin_count);
3632 atomic_add(obj->size, &dev->pin_memory);
3633 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003634 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07003635 !list_empty(&obj_priv->list))
3636 list_del_init(&obj_priv->list);
3637 }
3638 i915_verify_inactive(dev, __FILE__, __LINE__);
3639
3640 return 0;
3641}
3642
3643void
3644i915_gem_object_unpin(struct drm_gem_object *obj)
3645{
3646 struct drm_device *dev = obj->dev;
3647 drm_i915_private_t *dev_priv = dev->dev_private;
3648 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3649
3650 i915_verify_inactive(dev, __FILE__, __LINE__);
3651 obj_priv->pin_count--;
3652 BUG_ON(obj_priv->pin_count < 0);
3653 BUG_ON(obj_priv->gtt_space == NULL);
3654
3655 /* If the object is no longer pinned, and is
3656 * neither active nor being flushed, then stick it on
3657 * the inactive list
3658 */
3659 if (obj_priv->pin_count == 0) {
3660 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003661 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003662 list_move_tail(&obj_priv->list,
3663 &dev_priv->mm.inactive_list);
3664 atomic_dec(&dev->pin_count);
3665 atomic_sub(obj->size, &dev->pin_memory);
3666 }
3667 i915_verify_inactive(dev, __FILE__, __LINE__);
3668}
3669
3670int
3671i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3672 struct drm_file *file_priv)
3673{
3674 struct drm_i915_gem_pin *args = data;
3675 struct drm_gem_object *obj;
3676 struct drm_i915_gem_object *obj_priv;
3677 int ret;
3678
3679 mutex_lock(&dev->struct_mutex);
3680
3681 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3682 if (obj == NULL) {
3683 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3684 args->handle);
3685 mutex_unlock(&dev->struct_mutex);
3686 return -EBADF;
3687 }
3688 obj_priv = obj->driver_private;
3689
Jesse Barnes79e53942008-11-07 14:24:08 -08003690 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3691 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3692 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00003693 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003694 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003695 return -EINVAL;
3696 }
3697
3698 obj_priv->user_pin_count++;
3699 obj_priv->pin_filp = file_priv;
3700 if (obj_priv->user_pin_count == 1) {
3701 ret = i915_gem_object_pin(obj, args->alignment);
3702 if (ret != 0) {
3703 drm_gem_object_unreference(obj);
3704 mutex_unlock(&dev->struct_mutex);
3705 return ret;
3706 }
Eric Anholt673a3942008-07-30 12:06:12 -07003707 }
3708
3709 /* XXX - flush the CPU caches for pinned objects
3710 * as the X server doesn't manage domains yet
3711 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003712 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003713 args->offset = obj_priv->gtt_offset;
3714 drm_gem_object_unreference(obj);
3715 mutex_unlock(&dev->struct_mutex);
3716
3717 return 0;
3718}
3719
3720int
3721i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3722 struct drm_file *file_priv)
3723{
3724 struct drm_i915_gem_pin *args = data;
3725 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08003726 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003727
3728 mutex_lock(&dev->struct_mutex);
3729
3730 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3731 if (obj == NULL) {
3732 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3733 args->handle);
3734 mutex_unlock(&dev->struct_mutex);
3735 return -EBADF;
3736 }
3737
Jesse Barnes79e53942008-11-07 14:24:08 -08003738 obj_priv = obj->driver_private;
3739 if (obj_priv->pin_filp != file_priv) {
3740 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3741 args->handle);
3742 drm_gem_object_unreference(obj);
3743 mutex_unlock(&dev->struct_mutex);
3744 return -EINVAL;
3745 }
3746 obj_priv->user_pin_count--;
3747 if (obj_priv->user_pin_count == 0) {
3748 obj_priv->pin_filp = NULL;
3749 i915_gem_object_unpin(obj);
3750 }
Eric Anholt673a3942008-07-30 12:06:12 -07003751
3752 drm_gem_object_unreference(obj);
3753 mutex_unlock(&dev->struct_mutex);
3754 return 0;
3755}
3756
3757int
3758i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3759 struct drm_file *file_priv)
3760{
3761 struct drm_i915_gem_busy *args = data;
3762 struct drm_gem_object *obj;
3763 struct drm_i915_gem_object *obj_priv;
3764
Eric Anholt673a3942008-07-30 12:06:12 -07003765 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3766 if (obj == NULL) {
3767 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3768 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003769 return -EBADF;
3770 }
3771
Chris Wilsonb1ce7862009-06-06 09:46:00 +01003772 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08003773 /* Update the active list for the hardware's current position.
3774 * Otherwise this only updates on a delayed timer or when irqs are
3775 * actually unmasked, and our working set ends up being larger than
3776 * required.
3777 */
3778 i915_gem_retire_requests(dev);
3779
Eric Anholt673a3942008-07-30 12:06:12 -07003780 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08003781 /* Don't count being on the flushing list against the object being
3782 * done. Otherwise, a buffer left on the flushing list but not getting
3783 * flushed (because nobody's flushing that domain) won't ever return
3784 * unbusy and get reused by libdrm's bo cache. The other expected
3785 * consumer of this interface, OpenGL's occlusion queries, also specs
3786 * that the objects get unbusy "eventually" without any interference.
3787 */
3788 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003789
3790 drm_gem_object_unreference(obj);
3791 mutex_unlock(&dev->struct_mutex);
3792 return 0;
3793}
3794
3795int
3796i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3797 struct drm_file *file_priv)
3798{
3799 return i915_gem_ring_throttle(dev, file_priv);
3800}
3801
3802int i915_gem_init_object(struct drm_gem_object *obj)
3803{
3804 struct drm_i915_gem_object *obj_priv;
3805
Eric Anholt9a298b22009-03-24 12:23:04 -07003806 obj_priv = kzalloc(sizeof(*obj_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07003807 if (obj_priv == NULL)
3808 return -ENOMEM;
3809
3810 /*
3811 * We've just allocated pages from the kernel,
3812 * so they've just been written by the CPU with
3813 * zeros. They'll need to be clflushed before we
3814 * use them with the GPU.
3815 */
3816 obj->write_domain = I915_GEM_DOMAIN_CPU;
3817 obj->read_domains = I915_GEM_DOMAIN_CPU;
3818
Keith Packardba1eb1d2008-10-14 19:55:10 -07003819 obj_priv->agp_type = AGP_USER_MEMORY;
3820
Eric Anholt673a3942008-07-30 12:06:12 -07003821 obj->driver_private = obj_priv;
3822 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003823 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07003824 INIT_LIST_HEAD(&obj_priv->list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003825 INIT_LIST_HEAD(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003826
Eric Anholt673a3942008-07-30 12:06:12 -07003827 return 0;
3828}
3829
3830void i915_gem_free_object(struct drm_gem_object *obj)
3831{
Jesse Barnesde151cf2008-11-12 10:03:55 -08003832 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003833 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3834
3835 while (obj_priv->pin_count > 0)
3836 i915_gem_object_unpin(obj);
3837
Dave Airlie71acb5e2008-12-30 20:31:46 +10003838 if (obj_priv->phys_obj)
3839 i915_gem_detach_phys_object(dev, obj);
3840
Eric Anholt673a3942008-07-30 12:06:12 -07003841 i915_gem_object_unbind(obj);
3842
Chris Wilson7e616152009-09-10 08:53:04 +01003843 if (obj_priv->mmap_offset)
3844 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003845
Eric Anholt9a298b22009-03-24 12:23:04 -07003846 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07003847 kfree(obj_priv->bit_17);
Eric Anholt9a298b22009-03-24 12:23:04 -07003848 kfree(obj->driver_private);
Eric Anholt673a3942008-07-30 12:06:12 -07003849}
3850
Eric Anholt673a3942008-07-30 12:06:12 -07003851/** Unbinds all objects that are on the given buffer list. */
3852static int
3853i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3854{
3855 struct drm_gem_object *obj;
3856 struct drm_i915_gem_object *obj_priv;
3857 int ret;
3858
3859 while (!list_empty(head)) {
3860 obj_priv = list_first_entry(head,
3861 struct drm_i915_gem_object,
3862 list);
3863 obj = obj_priv->obj;
3864
3865 if (obj_priv->pin_count != 0) {
3866 DRM_ERROR("Pinned object in unbind list\n");
3867 mutex_unlock(&dev->struct_mutex);
3868 return -EINVAL;
3869 }
3870
3871 ret = i915_gem_object_unbind(obj);
3872 if (ret != 0) {
3873 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3874 ret);
3875 mutex_unlock(&dev->struct_mutex);
3876 return ret;
3877 }
3878 }
3879
3880
3881 return 0;
3882}
3883
Jesse Barnes5669fca2009-02-17 15:13:31 -08003884int
Eric Anholt673a3942008-07-30 12:06:12 -07003885i915_gem_idle(struct drm_device *dev)
3886{
3887 drm_i915_private_t *dev_priv = dev->dev_private;
3888 uint32_t seqno, cur_seqno, last_seqno;
3889 int stuck, ret;
3890
Keith Packard6dbe2772008-10-14 21:41:13 -07003891 mutex_lock(&dev->struct_mutex);
3892
3893 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3894 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003895 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003896 }
Eric Anholt673a3942008-07-30 12:06:12 -07003897
3898 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3899 * We need to replace this with a semaphore, or something.
3900 */
3901 dev_priv->mm.suspended = 1;
Ben Gamarif65d9422009-09-14 17:48:44 -04003902 del_timer(&dev_priv->hangcheck_timer);
Eric Anholt673a3942008-07-30 12:06:12 -07003903
Keith Packard6dbe2772008-10-14 21:41:13 -07003904 /* Cancel the retire work handler, wait for it to finish if running
3905 */
3906 mutex_unlock(&dev->struct_mutex);
3907 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3908 mutex_lock(&dev->struct_mutex);
3909
Eric Anholt673a3942008-07-30 12:06:12 -07003910 i915_kernel_lost_context(dev);
3911
3912 /* Flush the GPU along with all non-CPU write domains
3913 */
Chris Wilson21d509e2009-06-06 09:46:02 +01003914 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
3915 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07003916
3917 if (seqno == 0) {
3918 mutex_unlock(&dev->struct_mutex);
3919 return -ENOMEM;
3920 }
3921
3922 dev_priv->mm.waiting_gem_seqno = seqno;
3923 last_seqno = 0;
3924 stuck = 0;
3925 for (;;) {
3926 cur_seqno = i915_get_gem_seqno(dev);
3927 if (i915_seqno_passed(cur_seqno, seqno))
3928 break;
3929 if (last_seqno == cur_seqno) {
3930 if (stuck++ > 100) {
3931 DRM_ERROR("hardware wedged\n");
3932 dev_priv->mm.wedged = 1;
3933 DRM_WAKEUP(&dev_priv->irq_queue);
3934 break;
3935 }
3936 }
3937 msleep(10);
3938 last_seqno = cur_seqno;
3939 }
3940 dev_priv->mm.waiting_gem_seqno = 0;
3941
3942 i915_gem_retire_requests(dev);
3943
Carl Worth5e118f42009-03-20 11:54:25 -07003944 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003945 if (!dev_priv->mm.wedged) {
3946 /* Active and flushing should now be empty as we've
3947 * waited for a sequence higher than any pending execbuffer
3948 */
3949 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3950 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3951 /* Request should now be empty as we've also waited
3952 * for the last request in the list
3953 */
3954 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3955 }
Eric Anholt673a3942008-07-30 12:06:12 -07003956
Eric Anholt28dfe522008-11-13 15:00:55 -08003957 /* Empty the active and flushing lists to inactive. If there's
3958 * anything left at this point, it means that we're wedged and
3959 * nothing good's going to happen by leaving them there. So strip
3960 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003961 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003962 while (!list_empty(&dev_priv->mm.active_list)) {
3963 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003964
Eric Anholt28dfe522008-11-13 15:00:55 -08003965 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3966 struct drm_i915_gem_object,
3967 list);
3968 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3969 i915_gem_object_move_to_inactive(obj_priv->obj);
3970 }
Carl Worth5e118f42009-03-20 11:54:25 -07003971 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003972
3973 while (!list_empty(&dev_priv->mm.flushing_list)) {
3974 struct drm_i915_gem_object *obj_priv;
3975
Eric Anholt151903d2008-12-01 10:23:21 +10003976 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003977 struct drm_i915_gem_object,
3978 list);
3979 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3980 i915_gem_object_move_to_inactive(obj_priv->obj);
3981 }
3982
3983
3984 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003985 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003986 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003987 if (ret) {
3988 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003989 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003990 }
Eric Anholt673a3942008-07-30 12:06:12 -07003991
Keith Packard6dbe2772008-10-14 21:41:13 -07003992 i915_gem_cleanup_ringbuffer(dev);
3993 mutex_unlock(&dev->struct_mutex);
3994
Eric Anholt673a3942008-07-30 12:06:12 -07003995 return 0;
3996}
3997
3998static int
3999i915_gem_init_hws(struct drm_device *dev)
4000{
4001 drm_i915_private_t *dev_priv = dev->dev_private;
4002 struct drm_gem_object *obj;
4003 struct drm_i915_gem_object *obj_priv;
4004 int ret;
4005
4006 /* If we need a physical address for the status page, it's already
4007 * initialized at driver load time.
4008 */
4009 if (!I915_NEED_GFX_HWS(dev))
4010 return 0;
4011
4012 obj = drm_gem_object_alloc(dev, 4096);
4013 if (obj == NULL) {
4014 DRM_ERROR("Failed to allocate status page\n");
4015 return -ENOMEM;
4016 }
4017 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07004018 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07004019
4020 ret = i915_gem_object_pin(obj, 4096);
4021 if (ret != 0) {
4022 drm_gem_object_unreference(obj);
4023 return ret;
4024 }
4025
4026 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07004027
Eric Anholt856fa192009-03-19 14:10:50 -07004028 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004029 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004030 DRM_ERROR("Failed to map status page.\n");
4031 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00004032 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004033 drm_gem_object_unreference(obj);
4034 return -EINVAL;
4035 }
4036 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004037 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
4038 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004039 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07004040 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
4041
4042 return 0;
4043}
4044
Chris Wilson85a7bb92009-02-11 14:52:44 +00004045static void
4046i915_gem_cleanup_hws(struct drm_device *dev)
4047{
4048 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004049 struct drm_gem_object *obj;
4050 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00004051
4052 if (dev_priv->hws_obj == NULL)
4053 return;
4054
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004055 obj = dev_priv->hws_obj;
4056 obj_priv = obj->driver_private;
4057
Eric Anholt856fa192009-03-19 14:10:50 -07004058 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004059 i915_gem_object_unpin(obj);
4060 drm_gem_object_unreference(obj);
4061 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004062
Chris Wilson85a7bb92009-02-11 14:52:44 +00004063 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4064 dev_priv->hw_status_page = NULL;
4065
4066 /* Write high address into HWS_PGA when disabling. */
4067 I915_WRITE(HWS_PGA, 0x1ffff000);
4068}
4069
Jesse Barnes79e53942008-11-07 14:24:08 -08004070int
Eric Anholt673a3942008-07-30 12:06:12 -07004071i915_gem_init_ringbuffer(struct drm_device *dev)
4072{
4073 drm_i915_private_t *dev_priv = dev->dev_private;
4074 struct drm_gem_object *obj;
4075 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08004076 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07004077 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07004078 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07004079
4080 ret = i915_gem_init_hws(dev);
4081 if (ret != 0)
4082 return ret;
4083
4084 obj = drm_gem_object_alloc(dev, 128 * 1024);
4085 if (obj == NULL) {
4086 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004087 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004088 return -ENOMEM;
4089 }
4090 obj_priv = obj->driver_private;
4091
4092 ret = i915_gem_object_pin(obj, 4096);
4093 if (ret != 0) {
4094 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004095 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004096 return ret;
4097 }
4098
4099 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004100 ring->Size = obj->size;
Eric Anholt673a3942008-07-30 12:06:12 -07004101
Jesse Barnes79e53942008-11-07 14:24:08 -08004102 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4103 ring->map.size = obj->size;
4104 ring->map.type = 0;
4105 ring->map.flags = 0;
4106 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004107
Jesse Barnes79e53942008-11-07 14:24:08 -08004108 drm_core_ioremap_wc(&ring->map, dev);
4109 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004110 DRM_ERROR("Failed to map ringbuffer.\n");
4111 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004112 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004113 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004114 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004115 return -EINVAL;
4116 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004117 ring->ring_obj = obj;
4118 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004119
4120 /* Stop the ring if it's running. */
4121 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004122 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07004123 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004124
4125 /* Initialize the ring. */
4126 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07004127 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4128
4129 /* G45 ring initialization fails to reset head to zero */
4130 if (head != 0) {
4131 DRM_ERROR("Ring head not reset to zero "
4132 "ctl %08x head %08x tail %08x start %08x\n",
4133 I915_READ(PRB0_CTL),
4134 I915_READ(PRB0_HEAD),
4135 I915_READ(PRB0_TAIL),
4136 I915_READ(PRB0_START));
4137 I915_WRITE(PRB0_HEAD, 0);
4138
4139 DRM_ERROR("Ring head forced to zero "
4140 "ctl %08x head %08x tail %08x start %08x\n",
4141 I915_READ(PRB0_CTL),
4142 I915_READ(PRB0_HEAD),
4143 I915_READ(PRB0_TAIL),
4144 I915_READ(PRB0_START));
4145 }
4146
Eric Anholt673a3942008-07-30 12:06:12 -07004147 I915_WRITE(PRB0_CTL,
4148 ((obj->size - 4096) & RING_NR_PAGES) |
4149 RING_NO_REPORT |
4150 RING_VALID);
4151
Keith Packard50aa253d2008-10-14 17:20:35 -07004152 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4153
4154 /* If the head is still not zero, the ring is dead */
4155 if (head != 0) {
4156 DRM_ERROR("Ring initialization failed "
4157 "ctl %08x head %08x tail %08x start %08x\n",
4158 I915_READ(PRB0_CTL),
4159 I915_READ(PRB0_HEAD),
4160 I915_READ(PRB0_TAIL),
4161 I915_READ(PRB0_START));
4162 return -EIO;
4163 }
4164
Eric Anholt673a3942008-07-30 12:06:12 -07004165 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004166 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4167 i915_kernel_lost_context(dev);
4168 else {
4169 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4170 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4171 ring->space = ring->head - (ring->tail + 8);
4172 if (ring->space < 0)
4173 ring->space += ring->Size;
4174 }
Eric Anholt673a3942008-07-30 12:06:12 -07004175
4176 return 0;
4177}
4178
Jesse Barnes79e53942008-11-07 14:24:08 -08004179void
Eric Anholt673a3942008-07-30 12:06:12 -07004180i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4181{
4182 drm_i915_private_t *dev_priv = dev->dev_private;
4183
4184 if (dev_priv->ring.ring_obj == NULL)
4185 return;
4186
4187 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4188
4189 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4190 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4191 dev_priv->ring.ring_obj = NULL;
4192 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4193
Chris Wilson85a7bb92009-02-11 14:52:44 +00004194 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004195}
4196
4197int
4198i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4199 struct drm_file *file_priv)
4200{
4201 drm_i915_private_t *dev_priv = dev->dev_private;
4202 int ret;
4203
Jesse Barnes79e53942008-11-07 14:24:08 -08004204 if (drm_core_check_feature(dev, DRIVER_MODESET))
4205 return 0;
4206
Eric Anholt673a3942008-07-30 12:06:12 -07004207 if (dev_priv->mm.wedged) {
4208 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4209 dev_priv->mm.wedged = 0;
4210 }
4211
Eric Anholt673a3942008-07-30 12:06:12 -07004212 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004213 dev_priv->mm.suspended = 0;
4214
4215 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004216 if (ret != 0) {
4217 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004218 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004219 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004220
Carl Worth5e118f42009-03-20 11:54:25 -07004221 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004222 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004223 spin_unlock(&dev_priv->mm.active_list_lock);
4224
Eric Anholt673a3942008-07-30 12:06:12 -07004225 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4226 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4227 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004228 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004229
4230 drm_irq_install(dev);
4231
Eric Anholt673a3942008-07-30 12:06:12 -07004232 return 0;
4233}
4234
4235int
4236i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4237 struct drm_file *file_priv)
4238{
4239 int ret;
4240
Jesse Barnes79e53942008-11-07 14:24:08 -08004241 if (drm_core_check_feature(dev, DRIVER_MODESET))
4242 return 0;
4243
Eric Anholt673a3942008-07-30 12:06:12 -07004244 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004245 drm_irq_uninstall(dev);
4246
Keith Packard6dbe2772008-10-14 21:41:13 -07004247 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004248}
4249
4250void
4251i915_gem_lastclose(struct drm_device *dev)
4252{
4253 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004254
Eric Anholte806b492009-01-22 09:56:58 -08004255 if (drm_core_check_feature(dev, DRIVER_MODESET))
4256 return;
4257
Keith Packard6dbe2772008-10-14 21:41:13 -07004258 ret = i915_gem_idle(dev);
4259 if (ret)
4260 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004261}
4262
4263void
4264i915_gem_load(struct drm_device *dev)
4265{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004266 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004267 drm_i915_private_t *dev_priv = dev->dev_private;
4268
Carl Worth5e118f42009-03-20 11:54:25 -07004269 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004270 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4271 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4272 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4273 INIT_LIST_HEAD(&dev_priv->mm.request_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004274 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004275 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4276 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004277 dev_priv->mm.next_gem_seqno = 1;
4278
Jesse Barnesde151cf2008-11-12 10:03:55 -08004279 /* Old X drivers will take 0-2 for front, back, depth buffers */
4280 dev_priv->fence_reg_start = 3;
4281
Jesse Barnes0f973f22009-01-26 17:10:45 -08004282 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004283 dev_priv->num_fence_regs = 16;
4284 else
4285 dev_priv->num_fence_regs = 8;
4286
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004287 /* Initialize fence registers to zero */
4288 if (IS_I965G(dev)) {
4289 for (i = 0; i < 16; i++)
4290 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4291 } else {
4292 for (i = 0; i < 8; i++)
4293 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4294 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4295 for (i = 0; i < 8; i++)
4296 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4297 }
4298
Eric Anholt673a3942008-07-30 12:06:12 -07004299 i915_gem_detect_bit_6_swizzle(dev);
4300}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004301
4302/*
4303 * Create a physically contiguous memory object for this object
4304 * e.g. for cursor + overlay regs
4305 */
4306int i915_gem_init_phys_object(struct drm_device *dev,
4307 int id, int size)
4308{
4309 drm_i915_private_t *dev_priv = dev->dev_private;
4310 struct drm_i915_gem_phys_object *phys_obj;
4311 int ret;
4312
4313 if (dev_priv->mm.phys_objs[id - 1] || !size)
4314 return 0;
4315
Eric Anholt9a298b22009-03-24 12:23:04 -07004316 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004317 if (!phys_obj)
4318 return -ENOMEM;
4319
4320 phys_obj->id = id;
4321
4322 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4323 if (!phys_obj->handle) {
4324 ret = -ENOMEM;
4325 goto kfree_obj;
4326 }
4327#ifdef CONFIG_X86
4328 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4329#endif
4330
4331 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4332
4333 return 0;
4334kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004335 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004336 return ret;
4337}
4338
4339void i915_gem_free_phys_object(struct drm_device *dev, int id)
4340{
4341 drm_i915_private_t *dev_priv = dev->dev_private;
4342 struct drm_i915_gem_phys_object *phys_obj;
4343
4344 if (!dev_priv->mm.phys_objs[id - 1])
4345 return;
4346
4347 phys_obj = dev_priv->mm.phys_objs[id - 1];
4348 if (phys_obj->cur_obj) {
4349 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4350 }
4351
4352#ifdef CONFIG_X86
4353 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4354#endif
4355 drm_pci_free(dev, phys_obj->handle);
4356 kfree(phys_obj);
4357 dev_priv->mm.phys_objs[id - 1] = NULL;
4358}
4359
4360void i915_gem_free_all_phys_object(struct drm_device *dev)
4361{
4362 int i;
4363
Dave Airlie260883c2009-01-22 17:58:49 +10004364 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004365 i915_gem_free_phys_object(dev, i);
4366}
4367
4368void i915_gem_detach_phys_object(struct drm_device *dev,
4369 struct drm_gem_object *obj)
4370{
4371 struct drm_i915_gem_object *obj_priv;
4372 int i;
4373 int ret;
4374 int page_count;
4375
4376 obj_priv = obj->driver_private;
4377 if (!obj_priv->phys_obj)
4378 return;
4379
Eric Anholt856fa192009-03-19 14:10:50 -07004380 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004381 if (ret)
4382 goto out;
4383
4384 page_count = obj->size / PAGE_SIZE;
4385
4386 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004387 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004388 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4389
4390 memcpy(dst, src, PAGE_SIZE);
4391 kunmap_atomic(dst, KM_USER0);
4392 }
Eric Anholt856fa192009-03-19 14:10:50 -07004393 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004394 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004395
4396 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004397out:
4398 obj_priv->phys_obj->cur_obj = NULL;
4399 obj_priv->phys_obj = NULL;
4400}
4401
4402int
4403i915_gem_attach_phys_object(struct drm_device *dev,
4404 struct drm_gem_object *obj, int id)
4405{
4406 drm_i915_private_t *dev_priv = dev->dev_private;
4407 struct drm_i915_gem_object *obj_priv;
4408 int ret = 0;
4409 int page_count;
4410 int i;
4411
4412 if (id > I915_MAX_PHYS_OBJECT)
4413 return -EINVAL;
4414
4415 obj_priv = obj->driver_private;
4416
4417 if (obj_priv->phys_obj) {
4418 if (obj_priv->phys_obj->id == id)
4419 return 0;
4420 i915_gem_detach_phys_object(dev, obj);
4421 }
4422
4423
4424 /* create a new object */
4425 if (!dev_priv->mm.phys_objs[id - 1]) {
4426 ret = i915_gem_init_phys_object(dev, id,
4427 obj->size);
4428 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004429 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004430 goto out;
4431 }
4432 }
4433
4434 /* bind to the object */
4435 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4436 obj_priv->phys_obj->cur_obj = obj;
4437
Eric Anholt856fa192009-03-19 14:10:50 -07004438 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004439 if (ret) {
4440 DRM_ERROR("failed to get page list\n");
4441 goto out;
4442 }
4443
4444 page_count = obj->size / PAGE_SIZE;
4445
4446 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004447 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004448 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4449
4450 memcpy(dst, src, PAGE_SIZE);
4451 kunmap_atomic(src, KM_USER0);
4452 }
4453
Chris Wilsond78b47b2009-06-17 21:52:49 +01004454 i915_gem_object_put_pages(obj);
4455
Dave Airlie71acb5e2008-12-30 20:31:46 +10004456 return 0;
4457out:
4458 return ret;
4459}
4460
4461static int
4462i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4463 struct drm_i915_gem_pwrite *args,
4464 struct drm_file *file_priv)
4465{
4466 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4467 void *obj_addr;
4468 int ret;
4469 char __user *user_data;
4470
4471 user_data = (char __user *) (uintptr_t) args->data_ptr;
4472 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4473
Dave Airliee08fb4f2009-02-25 14:52:30 +10004474 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004475 ret = copy_from_user(obj_addr, user_data, args->size);
4476 if (ret)
4477 return -EFAULT;
4478
4479 drm_agp_chipset_flush(dev);
4480 return 0;
4481}
Eric Anholtb9624422009-06-03 07:27:35 +00004482
4483void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4484{
4485 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4486
4487 /* Clean up our request list when the client is going away, so that
4488 * later retire_requests won't dereference our soon-to-be-gone
4489 * file_priv.
4490 */
4491 mutex_lock(&dev->struct_mutex);
4492 while (!list_empty(&i915_file_priv->mm.request_list))
4493 list_del_init(i915_file_priv->mm.request_list.next);
4494 mutex_unlock(&dev->struct_mutex);
4495}