blob: 73b58193afed72d1e43338871731b04b259150b6 [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;
115 int handle, ret;
116
117 args->size = roundup(args->size, PAGE_SIZE);
118
119 /* Allocate the new object */
120 obj = drm_gem_object_alloc(dev, args->size);
121 if (obj == NULL)
122 return -ENOMEM;
123
124 ret = drm_gem_handle_create(file_priv, obj, &handle);
125 mutex_lock(&dev->struct_mutex);
126 drm_gem_object_handle_unreference(obj);
127 mutex_unlock(&dev->struct_mutex);
128
129 if (ret)
130 return ret;
131
132 args->handle = handle;
133
134 return 0;
135}
136
Eric Anholt40123c12009-03-09 13:42:30 -0700137static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700138fast_shmem_read(struct page **pages,
139 loff_t page_base, int page_offset,
140 char __user *data,
141 int length)
142{
143 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200144 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700145
146 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
147 if (vaddr == NULL)
148 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200149 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700150 kunmap_atomic(vaddr, KM_USER0);
151
Florian Mickler2bc43b52009-04-06 22:55:41 +0200152 if (unwritten)
153 return -EFAULT;
154
155 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700156}
157
Eric Anholt280b7132009-03-12 16:56:27 -0700158static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
159{
160 drm_i915_private_t *dev_priv = obj->dev->dev_private;
161 struct drm_i915_gem_object *obj_priv = obj->driver_private;
162
163 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
164 obj_priv->tiling_mode != I915_TILING_NONE;
165}
166
Eric Anholteb014592009-03-10 11:44:52 -0700167static inline int
Eric Anholt40123c12009-03-09 13:42:30 -0700168slow_shmem_copy(struct page *dst_page,
169 int dst_offset,
170 struct page *src_page,
171 int src_offset,
172 int length)
173{
174 char *dst_vaddr, *src_vaddr;
175
176 dst_vaddr = kmap_atomic(dst_page, KM_USER0);
177 if (dst_vaddr == NULL)
178 return -ENOMEM;
179
180 src_vaddr = kmap_atomic(src_page, KM_USER1);
181 if (src_vaddr == NULL) {
182 kunmap_atomic(dst_vaddr, KM_USER0);
183 return -ENOMEM;
184 }
185
186 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
187
188 kunmap_atomic(src_vaddr, KM_USER1);
189 kunmap_atomic(dst_vaddr, KM_USER0);
190
191 return 0;
192}
193
Eric Anholt280b7132009-03-12 16:56:27 -0700194static inline int
195slow_shmem_bit17_copy(struct page *gpu_page,
196 int gpu_offset,
197 struct page *cpu_page,
198 int cpu_offset,
199 int length,
200 int is_read)
201{
202 char *gpu_vaddr, *cpu_vaddr;
203
204 /* Use the unswizzled path if this page isn't affected. */
205 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
206 if (is_read)
207 return slow_shmem_copy(cpu_page, cpu_offset,
208 gpu_page, gpu_offset, length);
209 else
210 return slow_shmem_copy(gpu_page, gpu_offset,
211 cpu_page, cpu_offset, length);
212 }
213
214 gpu_vaddr = kmap_atomic(gpu_page, KM_USER0);
215 if (gpu_vaddr == NULL)
216 return -ENOMEM;
217
218 cpu_vaddr = kmap_atomic(cpu_page, KM_USER1);
219 if (cpu_vaddr == NULL) {
220 kunmap_atomic(gpu_vaddr, KM_USER0);
221 return -ENOMEM;
222 }
223
224 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
225 * XORing with the other bits (A9 for Y, A9 and A10 for X)
226 */
227 while (length > 0) {
228 int cacheline_end = ALIGN(gpu_offset + 1, 64);
229 int this_length = min(cacheline_end - gpu_offset, length);
230 int swizzled_gpu_offset = gpu_offset ^ 64;
231
232 if (is_read) {
233 memcpy(cpu_vaddr + cpu_offset,
234 gpu_vaddr + swizzled_gpu_offset,
235 this_length);
236 } else {
237 memcpy(gpu_vaddr + swizzled_gpu_offset,
238 cpu_vaddr + cpu_offset,
239 this_length);
240 }
241 cpu_offset += this_length;
242 gpu_offset += this_length;
243 length -= this_length;
244 }
245
246 kunmap_atomic(cpu_vaddr, KM_USER1);
247 kunmap_atomic(gpu_vaddr, KM_USER0);
248
249 return 0;
250}
251
Eric Anholt673a3942008-07-30 12:06:12 -0700252/**
Eric Anholteb014592009-03-10 11:44:52 -0700253 * This is the fast shmem pread path, which attempts to copy_from_user directly
254 * from the backing pages of the object to the user's address space. On a
255 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
256 */
257static int
258i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
259 struct drm_i915_gem_pread *args,
260 struct drm_file *file_priv)
261{
262 struct drm_i915_gem_object *obj_priv = obj->driver_private;
263 ssize_t remain;
264 loff_t offset, page_base;
265 char __user *user_data;
266 int page_offset, page_length;
267 int ret;
268
269 user_data = (char __user *) (uintptr_t) args->data_ptr;
270 remain = args->size;
271
272 mutex_lock(&dev->struct_mutex);
273
274 ret = i915_gem_object_get_pages(obj);
275 if (ret != 0)
276 goto fail_unlock;
277
278 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
279 args->size);
280 if (ret != 0)
281 goto fail_put_pages;
282
283 obj_priv = obj->driver_private;
284 offset = args->offset;
285
286 while (remain > 0) {
287 /* Operation in this page
288 *
289 * page_base = page offset within aperture
290 * page_offset = offset within page
291 * page_length = bytes to copy for this page
292 */
293 page_base = (offset & ~(PAGE_SIZE-1));
294 page_offset = offset & (PAGE_SIZE-1);
295 page_length = remain;
296 if ((page_offset + remain) > PAGE_SIZE)
297 page_length = PAGE_SIZE - page_offset;
298
299 ret = fast_shmem_read(obj_priv->pages,
300 page_base, page_offset,
301 user_data, page_length);
302 if (ret)
303 goto fail_put_pages;
304
305 remain -= page_length;
306 user_data += page_length;
307 offset += page_length;
308 }
309
310fail_put_pages:
311 i915_gem_object_put_pages(obj);
312fail_unlock:
313 mutex_unlock(&dev->struct_mutex);
314
315 return ret;
316}
317
318/**
319 * This is the fallback shmem pread path, which allocates temporary storage
320 * in kernel space to copy_to_user into outside of the struct_mutex, so we
321 * can copy out of the object's backing pages while holding the struct mutex
322 * and not take page faults.
323 */
324static int
325i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
326 struct drm_i915_gem_pread *args,
327 struct drm_file *file_priv)
328{
329 struct drm_i915_gem_object *obj_priv = obj->driver_private;
330 struct mm_struct *mm = current->mm;
331 struct page **user_pages;
332 ssize_t remain;
333 loff_t offset, pinned_pages, i;
334 loff_t first_data_page, last_data_page, num_pages;
335 int shmem_page_index, shmem_page_offset;
336 int data_page_index, data_page_offset;
337 int page_length;
338 int ret;
339 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700340 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700341
342 remain = args->size;
343
344 /* Pin the user pages containing the data. We can't fault while
345 * holding the struct mutex, yet we want to hold it while
346 * dereferencing the user data.
347 */
348 first_data_page = data_ptr / PAGE_SIZE;
349 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
350 num_pages = last_data_page - first_data_page + 1;
351
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700352 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700353 if (user_pages == NULL)
354 return -ENOMEM;
355
356 down_read(&mm->mmap_sem);
357 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700358 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700359 up_read(&mm->mmap_sem);
360 if (pinned_pages < num_pages) {
361 ret = -EFAULT;
362 goto fail_put_user_pages;
363 }
364
Eric Anholt280b7132009-03-12 16:56:27 -0700365 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
366
Eric Anholteb014592009-03-10 11:44:52 -0700367 mutex_lock(&dev->struct_mutex);
368
369 ret = i915_gem_object_get_pages(obj);
370 if (ret != 0)
371 goto fail_unlock;
372
373 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
374 args->size);
375 if (ret != 0)
376 goto fail_put_pages;
377
378 obj_priv = obj->driver_private;
379 offset = args->offset;
380
381 while (remain > 0) {
382 /* Operation in this page
383 *
384 * shmem_page_index = page number within shmem file
385 * shmem_page_offset = offset within page in shmem file
386 * data_page_index = page number in get_user_pages return
387 * data_page_offset = offset with data_page_index page.
388 * page_length = bytes to copy for this page
389 */
390 shmem_page_index = offset / PAGE_SIZE;
391 shmem_page_offset = offset & ~PAGE_MASK;
392 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
393 data_page_offset = data_ptr & ~PAGE_MASK;
394
395 page_length = remain;
396 if ((shmem_page_offset + page_length) > PAGE_SIZE)
397 page_length = PAGE_SIZE - shmem_page_offset;
398 if ((data_page_offset + page_length) > PAGE_SIZE)
399 page_length = PAGE_SIZE - data_page_offset;
400
Eric Anholt280b7132009-03-12 16:56:27 -0700401 if (do_bit17_swizzling) {
402 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
403 shmem_page_offset,
404 user_pages[data_page_index],
405 data_page_offset,
406 page_length,
407 1);
408 } else {
409 ret = slow_shmem_copy(user_pages[data_page_index],
410 data_page_offset,
411 obj_priv->pages[shmem_page_index],
412 shmem_page_offset,
413 page_length);
414 }
Eric Anholteb014592009-03-10 11:44:52 -0700415 if (ret)
416 goto fail_put_pages;
417
418 remain -= page_length;
419 data_ptr += page_length;
420 offset += page_length;
421 }
422
423fail_put_pages:
424 i915_gem_object_put_pages(obj);
425fail_unlock:
426 mutex_unlock(&dev->struct_mutex);
427fail_put_user_pages:
428 for (i = 0; i < pinned_pages; i++) {
429 SetPageDirty(user_pages[i]);
430 page_cache_release(user_pages[i]);
431 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700432 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700433
434 return ret;
435}
436
Eric Anholt673a3942008-07-30 12:06:12 -0700437/**
438 * Reads data from the object referenced by handle.
439 *
440 * On error, the contents of *data are undefined.
441 */
442int
443i915_gem_pread_ioctl(struct drm_device *dev, void *data,
444 struct drm_file *file_priv)
445{
446 struct drm_i915_gem_pread *args = data;
447 struct drm_gem_object *obj;
448 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700449 int ret;
450
451 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
452 if (obj == NULL)
453 return -EBADF;
454 obj_priv = obj->driver_private;
455
456 /* Bounds check source.
457 *
458 * XXX: This could use review for overflow issues...
459 */
460 if (args->offset > obj->size || args->size > obj->size ||
461 args->offset + args->size > obj->size) {
462 drm_gem_object_unreference(obj);
463 return -EINVAL;
464 }
465
Eric Anholt280b7132009-03-12 16:56:27 -0700466 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700467 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700468 } else {
469 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
470 if (ret != 0)
471 ret = i915_gem_shmem_pread_slow(dev, obj, args,
472 file_priv);
473 }
Eric Anholt673a3942008-07-30 12:06:12 -0700474
475 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700476
Eric Anholteb014592009-03-10 11:44:52 -0700477 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700478}
479
Keith Packard0839ccb2008-10-30 19:38:48 -0700480/* This is the fast write path which cannot handle
481 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700482 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700483
Keith Packard0839ccb2008-10-30 19:38:48 -0700484static inline int
485fast_user_write(struct io_mapping *mapping,
486 loff_t page_base, int page_offset,
487 char __user *user_data,
488 int length)
489{
490 char *vaddr_atomic;
491 unsigned long unwritten;
492
493 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
494 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
495 user_data, length);
496 io_mapping_unmap_atomic(vaddr_atomic);
497 if (unwritten)
498 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700499 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700500}
501
502/* Here's the write path which can sleep for
503 * page faults
504 */
505
506static inline int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700507slow_kernel_write(struct io_mapping *mapping,
508 loff_t gtt_base, int gtt_offset,
509 struct page *user_page, int user_offset,
510 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700511{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700512 char *src_vaddr, *dst_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700513 unsigned long unwritten;
514
Eric Anholt3de09aa2009-03-09 09:42:23 -0700515 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
516 src_vaddr = kmap_atomic(user_page, KM_USER1);
517 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
518 src_vaddr + user_offset,
519 length);
520 kunmap_atomic(src_vaddr, KM_USER1);
521 io_mapping_unmap_atomic(dst_vaddr);
Keith Packard0839ccb2008-10-30 19:38:48 -0700522 if (unwritten)
523 return -EFAULT;
524 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700525}
526
Eric Anholt40123c12009-03-09 13:42:30 -0700527static inline int
528fast_shmem_write(struct page **pages,
529 loff_t page_base, int page_offset,
530 char __user *data,
531 int length)
532{
533 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400534 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700535
536 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
537 if (vaddr == NULL)
538 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400539 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700540 kunmap_atomic(vaddr, KM_USER0);
541
Dave Airlied0088772009-03-28 20:29:48 -0400542 if (unwritten)
543 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700544 return 0;
545}
546
Eric Anholt3de09aa2009-03-09 09:42:23 -0700547/**
548 * This is the fast pwrite path, where we copy the data directly from the
549 * user into the GTT, uncached.
550 */
Eric Anholt673a3942008-07-30 12:06:12 -0700551static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700552i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
553 struct drm_i915_gem_pwrite *args,
554 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700555{
556 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Keith Packard0839ccb2008-10-30 19:38:48 -0700557 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700558 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700559 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700560 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700561 int page_offset, page_length;
562 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700563
564 user_data = (char __user *) (uintptr_t) args->data_ptr;
565 remain = args->size;
566 if (!access_ok(VERIFY_READ, user_data, remain))
567 return -EFAULT;
568
569
570 mutex_lock(&dev->struct_mutex);
571 ret = i915_gem_object_pin(obj, 0);
572 if (ret) {
573 mutex_unlock(&dev->struct_mutex);
574 return ret;
575 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800576 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700577 if (ret)
578 goto fail;
579
580 obj_priv = obj->driver_private;
581 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700582
583 while (remain > 0) {
584 /* Operation in this page
585 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700586 * page_base = page offset within aperture
587 * page_offset = offset within page
588 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700589 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700590 page_base = (offset & ~(PAGE_SIZE-1));
591 page_offset = offset & (PAGE_SIZE-1);
592 page_length = remain;
593 if ((page_offset + remain) > PAGE_SIZE)
594 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700595
Keith Packard0839ccb2008-10-30 19:38:48 -0700596 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
597 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700598
Keith Packard0839ccb2008-10-30 19:38:48 -0700599 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700600 * source page isn't available. Return the error and we'll
601 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700602 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700603 if (ret)
604 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700605
Keith Packard0839ccb2008-10-30 19:38:48 -0700606 remain -= page_length;
607 user_data += page_length;
608 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700609 }
Eric Anholt673a3942008-07-30 12:06:12 -0700610
611fail:
612 i915_gem_object_unpin(obj);
613 mutex_unlock(&dev->struct_mutex);
614
615 return ret;
616}
617
Eric Anholt3de09aa2009-03-09 09:42:23 -0700618/**
619 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
620 * the memory and maps it using kmap_atomic for copying.
621 *
622 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
623 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
624 */
Eric Anholt3043c602008-10-02 12:24:47 -0700625static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700626i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
627 struct drm_i915_gem_pwrite *args,
628 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700629{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700630 struct drm_i915_gem_object *obj_priv = obj->driver_private;
631 drm_i915_private_t *dev_priv = dev->dev_private;
632 ssize_t remain;
633 loff_t gtt_page_base, offset;
634 loff_t first_data_page, last_data_page, num_pages;
635 loff_t pinned_pages, i;
636 struct page **user_pages;
637 struct mm_struct *mm = current->mm;
638 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700639 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700640 uint64_t data_ptr = args->data_ptr;
641
642 remain = args->size;
643
644 /* Pin the user pages containing the data. We can't fault while
645 * holding the struct mutex, and all of the pwrite implementations
646 * want to hold it while dereferencing the user data.
647 */
648 first_data_page = data_ptr / PAGE_SIZE;
649 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
650 num_pages = last_data_page - first_data_page + 1;
651
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700652 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700653 if (user_pages == NULL)
654 return -ENOMEM;
655
656 down_read(&mm->mmap_sem);
657 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
658 num_pages, 0, 0, user_pages, NULL);
659 up_read(&mm->mmap_sem);
660 if (pinned_pages < num_pages) {
661 ret = -EFAULT;
662 goto out_unpin_pages;
663 }
664
665 mutex_lock(&dev->struct_mutex);
666 ret = i915_gem_object_pin(obj, 0);
667 if (ret)
668 goto out_unlock;
669
670 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
671 if (ret)
672 goto out_unpin_object;
673
674 obj_priv = obj->driver_private;
675 offset = obj_priv->gtt_offset + args->offset;
676
677 while (remain > 0) {
678 /* Operation in this page
679 *
680 * gtt_page_base = page offset within aperture
681 * gtt_page_offset = offset within page in aperture
682 * data_page_index = page number in get_user_pages return
683 * data_page_offset = offset with data_page_index page.
684 * page_length = bytes to copy for this page
685 */
686 gtt_page_base = offset & PAGE_MASK;
687 gtt_page_offset = offset & ~PAGE_MASK;
688 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
689 data_page_offset = data_ptr & ~PAGE_MASK;
690
691 page_length = remain;
692 if ((gtt_page_offset + page_length) > PAGE_SIZE)
693 page_length = PAGE_SIZE - gtt_page_offset;
694 if ((data_page_offset + page_length) > PAGE_SIZE)
695 page_length = PAGE_SIZE - data_page_offset;
696
697 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
698 gtt_page_base, gtt_page_offset,
699 user_pages[data_page_index],
700 data_page_offset,
701 page_length);
702
703 /* If we get a fault while copying data, then (presumably) our
704 * source page isn't available. Return the error and we'll
705 * retry in the slow path.
706 */
707 if (ret)
708 goto out_unpin_object;
709
710 remain -= page_length;
711 offset += page_length;
712 data_ptr += page_length;
713 }
714
715out_unpin_object:
716 i915_gem_object_unpin(obj);
717out_unlock:
718 mutex_unlock(&dev->struct_mutex);
719out_unpin_pages:
720 for (i = 0; i < pinned_pages; i++)
721 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700722 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700723
724 return ret;
725}
726
Eric Anholt40123c12009-03-09 13:42:30 -0700727/**
728 * This is the fast shmem pwrite path, which attempts to directly
729 * copy_from_user into the kmapped pages backing the object.
730 */
Eric Anholt673a3942008-07-30 12:06:12 -0700731static int
Eric Anholt40123c12009-03-09 13:42:30 -0700732i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
733 struct drm_i915_gem_pwrite *args,
734 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700735{
Eric Anholt40123c12009-03-09 13:42:30 -0700736 struct drm_i915_gem_object *obj_priv = obj->driver_private;
737 ssize_t remain;
738 loff_t offset, page_base;
739 char __user *user_data;
740 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700741 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700742
743 user_data = (char __user *) (uintptr_t) args->data_ptr;
744 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700745
746 mutex_lock(&dev->struct_mutex);
747
Eric Anholt40123c12009-03-09 13:42:30 -0700748 ret = i915_gem_object_get_pages(obj);
749 if (ret != 0)
750 goto fail_unlock;
751
Eric Anholte47c68e2008-11-14 13:35:19 -0800752 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700753 if (ret != 0)
754 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700755
Eric Anholt40123c12009-03-09 13:42:30 -0700756 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700757 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700758 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700759
Eric Anholt40123c12009-03-09 13:42:30 -0700760 while (remain > 0) {
761 /* Operation in this page
762 *
763 * page_base = page offset within aperture
764 * page_offset = offset within page
765 * page_length = bytes to copy for this page
766 */
767 page_base = (offset & ~(PAGE_SIZE-1));
768 page_offset = offset & (PAGE_SIZE-1);
769 page_length = remain;
770 if ((page_offset + remain) > PAGE_SIZE)
771 page_length = PAGE_SIZE - page_offset;
772
773 ret = fast_shmem_write(obj_priv->pages,
774 page_base, page_offset,
775 user_data, page_length);
776 if (ret)
777 goto fail_put_pages;
778
779 remain -= page_length;
780 user_data += page_length;
781 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700782 }
783
Eric Anholt40123c12009-03-09 13:42:30 -0700784fail_put_pages:
785 i915_gem_object_put_pages(obj);
786fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700787 mutex_unlock(&dev->struct_mutex);
788
Eric Anholt40123c12009-03-09 13:42:30 -0700789 return ret;
790}
791
792/**
793 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
794 * the memory and maps it using kmap_atomic for copying.
795 *
796 * This avoids taking mmap_sem for faulting on the user's address while the
797 * struct_mutex is held.
798 */
799static int
800i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
801 struct drm_i915_gem_pwrite *args,
802 struct drm_file *file_priv)
803{
804 struct drm_i915_gem_object *obj_priv = obj->driver_private;
805 struct mm_struct *mm = current->mm;
806 struct page **user_pages;
807 ssize_t remain;
808 loff_t offset, pinned_pages, i;
809 loff_t first_data_page, last_data_page, num_pages;
810 int shmem_page_index, shmem_page_offset;
811 int data_page_index, data_page_offset;
812 int page_length;
813 int ret;
814 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700815 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700816
817 remain = args->size;
818
819 /* Pin the user pages containing the data. We can't fault while
820 * holding the struct mutex, and all of the pwrite implementations
821 * want to hold it while dereferencing the user data.
822 */
823 first_data_page = data_ptr / PAGE_SIZE;
824 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
825 num_pages = last_data_page - first_data_page + 1;
826
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700827 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700828 if (user_pages == NULL)
829 return -ENOMEM;
830
831 down_read(&mm->mmap_sem);
832 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
833 num_pages, 0, 0, user_pages, NULL);
834 up_read(&mm->mmap_sem);
835 if (pinned_pages < num_pages) {
836 ret = -EFAULT;
837 goto fail_put_user_pages;
838 }
839
Eric Anholt280b7132009-03-12 16:56:27 -0700840 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
841
Eric Anholt40123c12009-03-09 13:42:30 -0700842 mutex_lock(&dev->struct_mutex);
843
844 ret = i915_gem_object_get_pages(obj);
845 if (ret != 0)
846 goto fail_unlock;
847
848 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
849 if (ret != 0)
850 goto fail_put_pages;
851
852 obj_priv = obj->driver_private;
853 offset = args->offset;
854 obj_priv->dirty = 1;
855
856 while (remain > 0) {
857 /* Operation in this page
858 *
859 * shmem_page_index = page number within shmem file
860 * shmem_page_offset = offset within page in shmem file
861 * data_page_index = page number in get_user_pages return
862 * data_page_offset = offset with data_page_index page.
863 * page_length = bytes to copy for this page
864 */
865 shmem_page_index = offset / PAGE_SIZE;
866 shmem_page_offset = offset & ~PAGE_MASK;
867 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
868 data_page_offset = data_ptr & ~PAGE_MASK;
869
870 page_length = remain;
871 if ((shmem_page_offset + page_length) > PAGE_SIZE)
872 page_length = PAGE_SIZE - shmem_page_offset;
873 if ((data_page_offset + page_length) > PAGE_SIZE)
874 page_length = PAGE_SIZE - data_page_offset;
875
Eric Anholt280b7132009-03-12 16:56:27 -0700876 if (do_bit17_swizzling) {
877 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
878 shmem_page_offset,
879 user_pages[data_page_index],
880 data_page_offset,
881 page_length,
882 0);
883 } else {
884 ret = slow_shmem_copy(obj_priv->pages[shmem_page_index],
885 shmem_page_offset,
886 user_pages[data_page_index],
887 data_page_offset,
888 page_length);
889 }
Eric Anholt40123c12009-03-09 13:42:30 -0700890 if (ret)
891 goto fail_put_pages;
892
893 remain -= page_length;
894 data_ptr += page_length;
895 offset += page_length;
896 }
897
898fail_put_pages:
899 i915_gem_object_put_pages(obj);
900fail_unlock:
901 mutex_unlock(&dev->struct_mutex);
902fail_put_user_pages:
903 for (i = 0; i < pinned_pages; i++)
904 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700905 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700906
907 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700908}
909
910/**
911 * Writes data to the object referenced by handle.
912 *
913 * On error, the contents of the buffer that were to be modified are undefined.
914 */
915int
916i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
917 struct drm_file *file_priv)
918{
919 struct drm_i915_gem_pwrite *args = data;
920 struct drm_gem_object *obj;
921 struct drm_i915_gem_object *obj_priv;
922 int ret = 0;
923
924 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
925 if (obj == NULL)
926 return -EBADF;
927 obj_priv = obj->driver_private;
928
929 /* Bounds check destination.
930 *
931 * XXX: This could use review for overflow issues...
932 */
933 if (args->offset > obj->size || args->size > obj->size ||
934 args->offset + args->size > obj->size) {
935 drm_gem_object_unreference(obj);
936 return -EINVAL;
937 }
938
939 /* We can only do the GTT pwrite on untiled buffers, as otherwise
940 * it would end up going through the fenced access, and we'll get
941 * different detiling behavior between reading and writing.
942 * pread/pwrite currently are reading and writing from the CPU
943 * perspective, requiring manual detiling by the client.
944 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000945 if (obj_priv->phys_obj)
946 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
947 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Eric Anholt3de09aa2009-03-09 09:42:23 -0700948 dev->gtt_total != 0) {
949 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
950 if (ret == -EFAULT) {
951 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
952 file_priv);
953 }
Eric Anholt280b7132009-03-12 16:56:27 -0700954 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
955 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700956 } else {
957 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
958 if (ret == -EFAULT) {
959 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
960 file_priv);
961 }
962 }
Eric Anholt673a3942008-07-30 12:06:12 -0700963
964#if WATCH_PWRITE
965 if (ret)
966 DRM_INFO("pwrite failed %d\n", ret);
967#endif
968
969 drm_gem_object_unreference(obj);
970
971 return ret;
972}
973
974/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800975 * Called when user space prepares to use an object with the CPU, either
976 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700977 */
978int
979i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
980 struct drm_file *file_priv)
981{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700982 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700983 struct drm_i915_gem_set_domain *args = data;
984 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700985 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800986 uint32_t read_domains = args->read_domains;
987 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700988 int ret;
989
990 if (!(dev->driver->driver_features & DRIVER_GEM))
991 return -ENODEV;
992
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800993 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100994 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800995 return -EINVAL;
996
Chris Wilson21d509e2009-06-06 09:46:02 +0100997 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800998 return -EINVAL;
999
1000 /* Having something in the write domain implies it's in the read
1001 * domain, and only that read domain. Enforce that in the request.
1002 */
1003 if (write_domain != 0 && read_domains != write_domain)
1004 return -EINVAL;
1005
Eric Anholt673a3942008-07-30 12:06:12 -07001006 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1007 if (obj == NULL)
1008 return -EBADF;
Jesse Barnes652c3932009-08-17 13:31:43 -07001009 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001010
1011 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001012
1013 intel_mark_busy(dev, obj);
1014
Eric Anholt673a3942008-07-30 12:06:12 -07001015#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001016 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001017 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001018#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001019 if (read_domains & I915_GEM_DOMAIN_GTT) {
1020 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001021
Eric Anholta09ba7f2009-08-29 12:49:51 -07001022 /* Update the LRU on the fence for the CPU access that's
1023 * about to occur.
1024 */
1025 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1026 list_move_tail(&obj_priv->fence_list,
1027 &dev_priv->mm.fence_list);
1028 }
1029
Eric Anholt02354392008-11-26 13:58:13 -08001030 /* Silently promote "you're not bound, there was nothing to do"
1031 * to success, since the client was just asking us to
1032 * make sure everything was done.
1033 */
1034 if (ret == -EINVAL)
1035 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001036 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001037 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001038 }
1039
Eric Anholt673a3942008-07-30 12:06:12 -07001040 drm_gem_object_unreference(obj);
1041 mutex_unlock(&dev->struct_mutex);
1042 return ret;
1043}
1044
1045/**
1046 * Called when user space has done writes to this buffer
1047 */
1048int
1049i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1050 struct drm_file *file_priv)
1051{
1052 struct drm_i915_gem_sw_finish *args = data;
1053 struct drm_gem_object *obj;
1054 struct drm_i915_gem_object *obj_priv;
1055 int ret = 0;
1056
1057 if (!(dev->driver->driver_features & DRIVER_GEM))
1058 return -ENODEV;
1059
1060 mutex_lock(&dev->struct_mutex);
1061 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1062 if (obj == NULL) {
1063 mutex_unlock(&dev->struct_mutex);
1064 return -EBADF;
1065 }
1066
1067#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001068 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001069 __func__, args->handle, obj, obj->size);
1070#endif
1071 obj_priv = obj->driver_private;
1072
1073 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001074 if (obj_priv->pin_count)
1075 i915_gem_object_flush_cpu_write_domain(obj);
1076
Eric Anholt673a3942008-07-30 12:06:12 -07001077 drm_gem_object_unreference(obj);
1078 mutex_unlock(&dev->struct_mutex);
1079 return ret;
1080}
1081
1082/**
1083 * Maps the contents of an object, returning the address it is mapped
1084 * into.
1085 *
1086 * While the mapping holds a reference on the contents of the object, it doesn't
1087 * imply a ref on the object itself.
1088 */
1089int
1090i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1091 struct drm_file *file_priv)
1092{
1093 struct drm_i915_gem_mmap *args = data;
1094 struct drm_gem_object *obj;
1095 loff_t offset;
1096 unsigned long addr;
1097
1098 if (!(dev->driver->driver_features & DRIVER_GEM))
1099 return -ENODEV;
1100
1101 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1102 if (obj == NULL)
1103 return -EBADF;
1104
1105 offset = args->offset;
1106
1107 down_write(&current->mm->mmap_sem);
1108 addr = do_mmap(obj->filp, 0, args->size,
1109 PROT_READ | PROT_WRITE, MAP_SHARED,
1110 args->offset);
1111 up_write(&current->mm->mmap_sem);
1112 mutex_lock(&dev->struct_mutex);
1113 drm_gem_object_unreference(obj);
1114 mutex_unlock(&dev->struct_mutex);
1115 if (IS_ERR((void *)addr))
1116 return addr;
1117
1118 args->addr_ptr = (uint64_t) addr;
1119
1120 return 0;
1121}
1122
Jesse Barnesde151cf2008-11-12 10:03:55 -08001123/**
1124 * i915_gem_fault - fault a page into the GTT
1125 * vma: VMA in question
1126 * vmf: fault info
1127 *
1128 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1129 * from userspace. The fault handler takes care of binding the object to
1130 * the GTT (if needed), allocating and programming a fence register (again,
1131 * only if needed based on whether the old reg is still valid or the object
1132 * is tiled) and inserting a new PTE into the faulting process.
1133 *
1134 * Note that the faulting process may involve evicting existing objects
1135 * from the GTT and/or fence registers to make room. So performance may
1136 * suffer if the GTT working set is large or there are few fence registers
1137 * left.
1138 */
1139int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1140{
1141 struct drm_gem_object *obj = vma->vm_private_data;
1142 struct drm_device *dev = obj->dev;
1143 struct drm_i915_private *dev_priv = dev->dev_private;
1144 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1145 pgoff_t page_offset;
1146 unsigned long pfn;
1147 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001148 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001149
1150 /* We don't use vmf->pgoff since that has the fake offset */
1151 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1152 PAGE_SHIFT;
1153
1154 /* Now bind it into the GTT if needed */
1155 mutex_lock(&dev->struct_mutex);
1156 if (!obj_priv->gtt_space) {
1157 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1158 if (ret) {
1159 mutex_unlock(&dev->struct_mutex);
1160 return VM_FAULT_SIGBUS;
1161 }
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001162
1163 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1164 if (ret) {
1165 mutex_unlock(&dev->struct_mutex);
1166 return VM_FAULT_SIGBUS;
1167 }
1168
Jesse Barnes14b60392009-05-20 16:47:08 -04001169 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001170 }
1171
1172 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001173 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001174 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001175 if (ret) {
1176 mutex_unlock(&dev->struct_mutex);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001177 return VM_FAULT_SIGBUS;
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001178 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001179 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001180
1181 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1182 page_offset;
1183
1184 /* Finally, remap it using the new GTT offset */
1185 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1186
1187 mutex_unlock(&dev->struct_mutex);
1188
1189 switch (ret) {
1190 case -ENOMEM:
1191 case -EAGAIN:
1192 return VM_FAULT_OOM;
1193 case -EFAULT:
Jesse Barnes959b8872009-03-20 14:16:33 -07001194 case -EINVAL:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001195 return VM_FAULT_SIGBUS;
1196 default:
1197 return VM_FAULT_NOPAGE;
1198 }
1199}
1200
1201/**
1202 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1203 * @obj: obj in question
1204 *
1205 * GEM memory mapping works by handing back to userspace a fake mmap offset
1206 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1207 * up the object based on the offset and sets up the various memory mapping
1208 * structures.
1209 *
1210 * This routine allocates and attaches a fake offset for @obj.
1211 */
1212static int
1213i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1214{
1215 struct drm_device *dev = obj->dev;
1216 struct drm_gem_mm *mm = dev->mm_private;
1217 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1218 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001219 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001220 int ret = 0;
1221
1222 /* Set the object up for mmap'ing */
1223 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001224 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001225 if (!list->map)
1226 return -ENOMEM;
1227
1228 map = list->map;
1229 map->type = _DRM_GEM;
1230 map->size = obj->size;
1231 map->handle = obj;
1232
1233 /* Get a DRM GEM mmap offset allocated... */
1234 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1235 obj->size / PAGE_SIZE, 0, 0);
1236 if (!list->file_offset_node) {
1237 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1238 ret = -ENOMEM;
1239 goto out_free_list;
1240 }
1241
1242 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1243 obj->size / PAGE_SIZE, 0);
1244 if (!list->file_offset_node) {
1245 ret = -ENOMEM;
1246 goto out_free_list;
1247 }
1248
1249 list->hash.key = list->file_offset_node->start;
1250 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1251 DRM_ERROR("failed to add to map hash\n");
1252 goto out_free_mm;
1253 }
1254
1255 /* By now we should be all set, any drm_mmap request on the offset
1256 * below will get to our mmap & fault handler */
1257 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1258
1259 return 0;
1260
1261out_free_mm:
1262 drm_mm_put_block(list->file_offset_node);
1263out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001264 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001265
1266 return ret;
1267}
1268
Chris Wilson901782b2009-07-10 08:18:50 +01001269/**
1270 * i915_gem_release_mmap - remove physical page mappings
1271 * @obj: obj in question
1272 *
1273 * Preserve the reservation of the mmaping with the DRM core code, but
1274 * relinquish ownership of the pages back to the system.
1275 *
1276 * It is vital that we remove the page mapping if we have mapped a tiled
1277 * object through the GTT and then lose the fence register due to
1278 * resource pressure. Similarly if the object has been moved out of the
1279 * aperture, than pages mapped into userspace must be revoked. Removing the
1280 * mapping will then trigger a page fault on the next user access, allowing
1281 * fixup by i915_gem_fault().
1282 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001283void
Chris Wilson901782b2009-07-10 08:18:50 +01001284i915_gem_release_mmap(struct drm_gem_object *obj)
1285{
1286 struct drm_device *dev = obj->dev;
1287 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1288
1289 if (dev->dev_mapping)
1290 unmap_mapping_range(dev->dev_mapping,
1291 obj_priv->mmap_offset, obj->size, 1);
1292}
1293
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001294static void
1295i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1296{
1297 struct drm_device *dev = obj->dev;
1298 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1299 struct drm_gem_mm *mm = dev->mm_private;
1300 struct drm_map_list *list;
1301
1302 list = &obj->map_list;
1303 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1304
1305 if (list->file_offset_node) {
1306 drm_mm_put_block(list->file_offset_node);
1307 list->file_offset_node = NULL;
1308 }
1309
1310 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001311 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001312 list->map = NULL;
1313 }
1314
1315 obj_priv->mmap_offset = 0;
1316}
1317
Jesse Barnesde151cf2008-11-12 10:03:55 -08001318/**
1319 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1320 * @obj: object to check
1321 *
1322 * Return the required GTT alignment for an object, taking into account
1323 * potential fence register mapping if needed.
1324 */
1325static uint32_t
1326i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1327{
1328 struct drm_device *dev = obj->dev;
1329 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1330 int start, i;
1331
1332 /*
1333 * Minimum alignment is 4k (GTT page size), but might be greater
1334 * if a fence register is needed for the object.
1335 */
1336 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1337 return 4096;
1338
1339 /*
1340 * Previous chips need to be aligned to the size of the smallest
1341 * fence register that can contain the object.
1342 */
1343 if (IS_I9XX(dev))
1344 start = 1024*1024;
1345 else
1346 start = 512*1024;
1347
1348 for (i = start; i < obj->size; i <<= 1)
1349 ;
1350
1351 return i;
1352}
1353
1354/**
1355 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1356 * @dev: DRM device
1357 * @data: GTT mapping ioctl data
1358 * @file_priv: GEM object info
1359 *
1360 * Simply returns the fake offset to userspace so it can mmap it.
1361 * The mmap call will end up in drm_gem_mmap(), which will set things
1362 * up so we can get faults in the handler above.
1363 *
1364 * The fault handler will take care of binding the object into the GTT
1365 * (since it may have been evicted to make room for something), allocating
1366 * a fence register, and mapping the appropriate aperture address into
1367 * userspace.
1368 */
1369int
1370i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1371 struct drm_file *file_priv)
1372{
1373 struct drm_i915_gem_mmap_gtt *args = data;
1374 struct drm_i915_private *dev_priv = dev->dev_private;
1375 struct drm_gem_object *obj;
1376 struct drm_i915_gem_object *obj_priv;
1377 int ret;
1378
1379 if (!(dev->driver->driver_features & DRIVER_GEM))
1380 return -ENODEV;
1381
1382 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1383 if (obj == NULL)
1384 return -EBADF;
1385
1386 mutex_lock(&dev->struct_mutex);
1387
1388 obj_priv = obj->driver_private;
1389
1390 if (!obj_priv->mmap_offset) {
1391 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001392 if (ret) {
1393 drm_gem_object_unreference(obj);
1394 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001395 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001396 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001397 }
1398
1399 args->offset = obj_priv->mmap_offset;
1400
1401 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
1402
1403 /* Make sure the alignment is correct for fence regs etc */
1404 if (obj_priv->agp_mem &&
1405 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
1406 drm_gem_object_unreference(obj);
1407 mutex_unlock(&dev->struct_mutex);
1408 return -EINVAL;
1409 }
1410
1411 /*
1412 * Pull it into the GTT so that we have a page list (makes the
1413 * initial fault faster and any subsequent flushing possible).
1414 */
1415 if (!obj_priv->agp_mem) {
1416 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1417 if (ret) {
1418 drm_gem_object_unreference(obj);
1419 mutex_unlock(&dev->struct_mutex);
1420 return ret;
1421 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001422 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001423 }
1424
1425 drm_gem_object_unreference(obj);
1426 mutex_unlock(&dev->struct_mutex);
1427
1428 return 0;
1429}
1430
Ben Gamari6911a9b2009-04-02 11:24:54 -07001431void
Eric Anholt856fa192009-03-19 14:10:50 -07001432i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001433{
1434 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1435 int page_count = obj->size / PAGE_SIZE;
1436 int i;
1437
Eric Anholt856fa192009-03-19 14:10:50 -07001438 BUG_ON(obj_priv->pages_refcount == 0);
1439
1440 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001441 return;
1442
Eric Anholt280b7132009-03-12 16:56:27 -07001443 if (obj_priv->tiling_mode != I915_TILING_NONE)
1444 i915_gem_object_save_bit_17_swizzle(obj);
1445
Eric Anholt673a3942008-07-30 12:06:12 -07001446 for (i = 0; i < page_count; i++)
Eric Anholt856fa192009-03-19 14:10:50 -07001447 if (obj_priv->pages[i] != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07001448 if (obj_priv->dirty)
Eric Anholt856fa192009-03-19 14:10:50 -07001449 set_page_dirty(obj_priv->pages[i]);
1450 mark_page_accessed(obj_priv->pages[i]);
1451 page_cache_release(obj_priv->pages[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07001452 }
1453 obj_priv->dirty = 0;
1454
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001455 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001456 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001457}
1458
1459static void
Eric Anholtce44b0e2008-11-06 16:00:31 -08001460i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001461{
1462 struct drm_device *dev = obj->dev;
1463 drm_i915_private_t *dev_priv = dev->dev_private;
1464 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1465
1466 /* Add a reference if we're newly entering the active list. */
1467 if (!obj_priv->active) {
1468 drm_gem_object_reference(obj);
1469 obj_priv->active = 1;
1470 }
1471 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001472 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001473 list_move_tail(&obj_priv->list,
1474 &dev_priv->mm.active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001475 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001476 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001477}
1478
Eric Anholtce44b0e2008-11-06 16:00:31 -08001479static void
1480i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1481{
1482 struct drm_device *dev = obj->dev;
1483 drm_i915_private_t *dev_priv = dev->dev_private;
1484 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1485
1486 BUG_ON(!obj_priv->active);
1487 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1488 obj_priv->last_rendering_seqno = 0;
1489}
Eric Anholt673a3942008-07-30 12:06:12 -07001490
1491static void
1492i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1493{
1494 struct drm_device *dev = obj->dev;
1495 drm_i915_private_t *dev_priv = dev->dev_private;
1496 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1497
1498 i915_verify_inactive(dev, __FILE__, __LINE__);
1499 if (obj_priv->pin_count != 0)
1500 list_del_init(&obj_priv->list);
1501 else
1502 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1503
Eric Anholtce44b0e2008-11-06 16:00:31 -08001504 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001505 if (obj_priv->active) {
1506 obj_priv->active = 0;
1507 drm_gem_object_unreference(obj);
1508 }
1509 i915_verify_inactive(dev, __FILE__, __LINE__);
1510}
1511
1512/**
1513 * Creates a new sequence number, emitting a write of it to the status page
1514 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1515 *
1516 * Must be called with struct_lock held.
1517 *
1518 * Returned sequence numbers are nonzero on success.
1519 */
1520static uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001521i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
1522 uint32_t flush_domains)
Eric Anholt673a3942008-07-30 12:06:12 -07001523{
1524 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001525 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001526 struct drm_i915_gem_request *request;
1527 uint32_t seqno;
1528 int was_empty;
1529 RING_LOCALS;
1530
Eric Anholtb9624422009-06-03 07:27:35 +00001531 if (file_priv != NULL)
1532 i915_file_priv = file_priv->driver_priv;
1533
Eric Anholt9a298b22009-03-24 12:23:04 -07001534 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001535 if (request == NULL)
1536 return 0;
1537
1538 /* Grab the seqno we're going to make this request be, and bump the
1539 * next (skipping 0 so it can be the reserved no-seqno value).
1540 */
1541 seqno = dev_priv->mm.next_gem_seqno;
1542 dev_priv->mm.next_gem_seqno++;
1543 if (dev_priv->mm.next_gem_seqno == 0)
1544 dev_priv->mm.next_gem_seqno++;
1545
1546 BEGIN_LP_RING(4);
1547 OUT_RING(MI_STORE_DWORD_INDEX);
1548 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1549 OUT_RING(seqno);
1550
1551 OUT_RING(MI_USER_INTERRUPT);
1552 ADVANCE_LP_RING();
1553
1554 DRM_DEBUG("%d\n", seqno);
1555
1556 request->seqno = seqno;
1557 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -07001558 was_empty = list_empty(&dev_priv->mm.request_list);
1559 list_add_tail(&request->list, &dev_priv->mm.request_list);
Eric Anholtb9624422009-06-03 07:27:35 +00001560 if (i915_file_priv) {
1561 list_add_tail(&request->client_list,
1562 &i915_file_priv->mm.request_list);
1563 } else {
1564 INIT_LIST_HEAD(&request->client_list);
1565 }
Eric Anholt673a3942008-07-30 12:06:12 -07001566
Eric Anholtce44b0e2008-11-06 16:00:31 -08001567 /* Associate any objects on the flushing list matching the write
1568 * domain we're flushing with our flush.
1569 */
1570 if (flush_domains != 0) {
1571 struct drm_i915_gem_object *obj_priv, *next;
1572
1573 list_for_each_entry_safe(obj_priv, next,
1574 &dev_priv->mm.flushing_list, list) {
1575 struct drm_gem_object *obj = obj_priv->obj;
1576
1577 if ((obj->write_domain & flush_domains) ==
1578 obj->write_domain) {
1579 obj->write_domain = 0;
1580 i915_gem_object_move_to_active(obj, seqno);
1581 }
1582 }
1583
1584 }
1585
Keith Packard6dbe2772008-10-14 21:41:13 -07001586 if (was_empty && !dev_priv->mm.suspended)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001587 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001588 return seqno;
1589}
1590
1591/**
1592 * Command execution barrier
1593 *
1594 * Ensures that all commands in the ring are finished
1595 * before signalling the CPU
1596 */
Eric Anholt3043c602008-10-02 12:24:47 -07001597static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -07001598i915_retire_commands(struct drm_device *dev)
1599{
1600 drm_i915_private_t *dev_priv = dev->dev_private;
1601 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1602 uint32_t flush_domains = 0;
1603 RING_LOCALS;
1604
1605 /* The sampler always gets flushed on i965 (sigh) */
1606 if (IS_I965G(dev))
1607 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1608 BEGIN_LP_RING(2);
1609 OUT_RING(cmd);
1610 OUT_RING(0); /* noop */
1611 ADVANCE_LP_RING();
1612 return flush_domains;
1613}
1614
1615/**
1616 * Moves buffers associated only with the given active seqno from the active
1617 * to inactive list, potentially freeing them.
1618 */
1619static void
1620i915_gem_retire_request(struct drm_device *dev,
1621 struct drm_i915_gem_request *request)
1622{
1623 drm_i915_private_t *dev_priv = dev->dev_private;
1624
1625 /* Move any buffers on the active list that are no longer referenced
1626 * by the ringbuffer to the flushing/inactive lists as appropriate.
1627 */
Carl Worth5e118f42009-03-20 11:54:25 -07001628 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001629 while (!list_empty(&dev_priv->mm.active_list)) {
1630 struct drm_gem_object *obj;
1631 struct drm_i915_gem_object *obj_priv;
1632
1633 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1634 struct drm_i915_gem_object,
1635 list);
1636 obj = obj_priv->obj;
1637
1638 /* If the seqno being retired doesn't match the oldest in the
1639 * list, then the oldest in the list must still be newer than
1640 * this seqno.
1641 */
1642 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001643 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001644
Eric Anholt673a3942008-07-30 12:06:12 -07001645#if WATCH_LRU
1646 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1647 __func__, request->seqno, obj);
1648#endif
1649
Eric Anholtce44b0e2008-11-06 16:00:31 -08001650 if (obj->write_domain != 0)
1651 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001652 else {
1653 /* Take a reference on the object so it won't be
1654 * freed while the spinlock is held. The list
1655 * protection for this spinlock is safe when breaking
1656 * the lock like this since the next thing we do
1657 * is just get the head of the list again.
1658 */
1659 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001660 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001661 spin_unlock(&dev_priv->mm.active_list_lock);
1662 drm_gem_object_unreference(obj);
1663 spin_lock(&dev_priv->mm.active_list_lock);
1664 }
Eric Anholt673a3942008-07-30 12:06:12 -07001665 }
Carl Worth5e118f42009-03-20 11:54:25 -07001666out:
1667 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001668}
1669
1670/**
1671 * Returns true if seq1 is later than seq2.
1672 */
1673static int
1674i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1675{
1676 return (int32_t)(seq1 - seq2) >= 0;
1677}
1678
1679uint32_t
1680i915_get_gem_seqno(struct drm_device *dev)
1681{
1682 drm_i915_private_t *dev_priv = dev->dev_private;
1683
1684 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1685}
1686
1687/**
1688 * This function clears the request list as sequence numbers are passed.
1689 */
1690void
1691i915_gem_retire_requests(struct drm_device *dev)
1692{
1693 drm_i915_private_t *dev_priv = dev->dev_private;
1694 uint32_t seqno;
1695
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001696 if (!dev_priv->hw_status_page)
1697 return;
1698
Eric Anholt673a3942008-07-30 12:06:12 -07001699 seqno = i915_get_gem_seqno(dev);
1700
1701 while (!list_empty(&dev_priv->mm.request_list)) {
1702 struct drm_i915_gem_request *request;
1703 uint32_t retiring_seqno;
1704
1705 request = list_first_entry(&dev_priv->mm.request_list,
1706 struct drm_i915_gem_request,
1707 list);
1708 retiring_seqno = request->seqno;
1709
1710 if (i915_seqno_passed(seqno, retiring_seqno) ||
1711 dev_priv->mm.wedged) {
1712 i915_gem_retire_request(dev, request);
1713
1714 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001715 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001716 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001717 } else
1718 break;
1719 }
1720}
1721
1722void
1723i915_gem_retire_work_handler(struct work_struct *work)
1724{
1725 drm_i915_private_t *dev_priv;
1726 struct drm_device *dev;
1727
1728 dev_priv = container_of(work, drm_i915_private_t,
1729 mm.retire_work.work);
1730 dev = dev_priv->dev;
1731
1732 mutex_lock(&dev->struct_mutex);
1733 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001734 if (!dev_priv->mm.suspended &&
1735 !list_empty(&dev_priv->mm.request_list))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001736 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001737 mutex_unlock(&dev->struct_mutex);
1738}
1739
1740/**
1741 * Waits for a sequence number to be signaled, and cleans up the
1742 * request and object lists appropriately for that event.
1743 */
Eric Anholt3043c602008-10-02 12:24:47 -07001744static int
Eric Anholt673a3942008-07-30 12:06:12 -07001745i915_wait_request(struct drm_device *dev, uint32_t seqno)
1746{
1747 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001748 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001749 int ret = 0;
1750
1751 BUG_ON(seqno == 0);
1752
1753 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001754 if (IS_IGDNG(dev))
1755 ier = I915_READ(DEIER) | I915_READ(GTIER);
1756 else
1757 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001758 if (!ier) {
1759 DRM_ERROR("something (likely vbetool) disabled "
1760 "interrupts, re-enabling\n");
1761 i915_driver_irq_preinstall(dev);
1762 i915_driver_irq_postinstall(dev);
1763 }
1764
Eric Anholt673a3942008-07-30 12:06:12 -07001765 dev_priv->mm.waiting_gem_seqno = seqno;
1766 i915_user_irq_get(dev);
1767 ret = wait_event_interruptible(dev_priv->irq_queue,
1768 i915_seqno_passed(i915_get_gem_seqno(dev),
1769 seqno) ||
1770 dev_priv->mm.wedged);
1771 i915_user_irq_put(dev);
1772 dev_priv->mm.waiting_gem_seqno = 0;
1773 }
1774 if (dev_priv->mm.wedged)
1775 ret = -EIO;
1776
1777 if (ret && ret != -ERESTARTSYS)
1778 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1779 __func__, ret, seqno, i915_get_gem_seqno(dev));
1780
1781 /* Directly dispatch request retiring. While we have the work queue
1782 * to handle this, the waiter on a request often wants an associated
1783 * buffer to have made it to the inactive list, and we would need
1784 * a separate wait queue to handle that.
1785 */
1786 if (ret == 0)
1787 i915_gem_retire_requests(dev);
1788
1789 return ret;
1790}
1791
1792static void
1793i915_gem_flush(struct drm_device *dev,
1794 uint32_t invalidate_domains,
1795 uint32_t flush_domains)
1796{
1797 drm_i915_private_t *dev_priv = dev->dev_private;
1798 uint32_t cmd;
1799 RING_LOCALS;
1800
1801#if WATCH_EXEC
1802 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1803 invalidate_domains, flush_domains);
1804#endif
1805
1806 if (flush_domains & I915_GEM_DOMAIN_CPU)
1807 drm_agp_chipset_flush(dev);
1808
Chris Wilson21d509e2009-06-06 09:46:02 +01001809 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
Eric Anholt673a3942008-07-30 12:06:12 -07001810 /*
1811 * read/write caches:
1812 *
1813 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1814 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1815 * also flushed at 2d versus 3d pipeline switches.
1816 *
1817 * read-only caches:
1818 *
1819 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1820 * MI_READ_FLUSH is set, and is always flushed on 965.
1821 *
1822 * I915_GEM_DOMAIN_COMMAND may not exist?
1823 *
1824 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1825 * invalidated when MI_EXE_FLUSH is set.
1826 *
1827 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1828 * invalidated with every MI_FLUSH.
1829 *
1830 * TLBs:
1831 *
1832 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1833 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1834 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1835 * are flushed at any MI_FLUSH.
1836 */
1837
1838 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1839 if ((invalidate_domains|flush_domains) &
1840 I915_GEM_DOMAIN_RENDER)
1841 cmd &= ~MI_NO_WRITE_FLUSH;
1842 if (!IS_I965G(dev)) {
1843 /*
1844 * On the 965, the sampler cache always gets flushed
1845 * and this bit is reserved.
1846 */
1847 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1848 cmd |= MI_READ_FLUSH;
1849 }
1850 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1851 cmd |= MI_EXE_FLUSH;
1852
1853#if WATCH_EXEC
1854 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1855#endif
1856 BEGIN_LP_RING(2);
1857 OUT_RING(cmd);
1858 OUT_RING(0); /* noop */
1859 ADVANCE_LP_RING();
1860 }
1861}
1862
1863/**
1864 * Ensures that all rendering to the object has completed and the object is
1865 * safe to unbind from the GTT or access from the CPU.
1866 */
1867static int
1868i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1869{
1870 struct drm_device *dev = obj->dev;
1871 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1872 int ret;
1873
Eric Anholte47c68e2008-11-14 13:35:19 -08001874 /* This function only exists to support waiting for existing rendering,
1875 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001876 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001877 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001878
1879 /* If there is rendering queued on the buffer being evicted, wait for
1880 * it.
1881 */
1882 if (obj_priv->active) {
1883#if WATCH_BUF
1884 DRM_INFO("%s: object %p wait for seqno %08x\n",
1885 __func__, obj, obj_priv->last_rendering_seqno);
1886#endif
1887 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1888 if (ret != 0)
1889 return ret;
1890 }
1891
1892 return 0;
1893}
1894
1895/**
1896 * Unbinds an object from the GTT aperture.
1897 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001898int
Eric Anholt673a3942008-07-30 12:06:12 -07001899i915_gem_object_unbind(struct drm_gem_object *obj)
1900{
1901 struct drm_device *dev = obj->dev;
1902 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1903 int ret = 0;
1904
1905#if WATCH_BUF
1906 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1907 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1908#endif
1909 if (obj_priv->gtt_space == NULL)
1910 return 0;
1911
1912 if (obj_priv->pin_count != 0) {
1913 DRM_ERROR("Attempting to unbind pinned buffer\n");
1914 return -EINVAL;
1915 }
1916
Eric Anholt673a3942008-07-30 12:06:12 -07001917 /* Move the object to the CPU domain to ensure that
1918 * any possible CPU writes while it's not in the GTT
1919 * are flushed when we go to remap it. This will
1920 * also ensure that all pending GPU writes are finished
1921 * before we unbind.
1922 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001923 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001924 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001925 if (ret != -ERESTARTSYS)
1926 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001927 return ret;
1928 }
1929
1930 if (obj_priv->agp_mem != NULL) {
1931 drm_unbind_agp(obj_priv->agp_mem);
1932 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1933 obj_priv->agp_mem = NULL;
1934 }
1935
1936 BUG_ON(obj_priv->active);
1937
Jesse Barnesde151cf2008-11-12 10:03:55 -08001938 /* blow away mappings if mapped through GTT */
Chris Wilson901782b2009-07-10 08:18:50 +01001939 i915_gem_release_mmap(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001940
1941 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1942 i915_gem_clear_fence_reg(obj);
1943
Eric Anholt856fa192009-03-19 14:10:50 -07001944 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001945
1946 if (obj_priv->gtt_space) {
1947 atomic_dec(&dev->gtt_count);
1948 atomic_sub(obj->size, &dev->gtt_memory);
1949
1950 drm_mm_put_block(obj_priv->gtt_space);
1951 obj_priv->gtt_space = NULL;
1952 }
1953
1954 /* Remove ourselves from the LRU list if present. */
1955 if (!list_empty(&obj_priv->list))
1956 list_del_init(&obj_priv->list);
1957
1958 return 0;
1959}
1960
1961static int
1962i915_gem_evict_something(struct drm_device *dev)
1963{
1964 drm_i915_private_t *dev_priv = dev->dev_private;
1965 struct drm_gem_object *obj;
1966 struct drm_i915_gem_object *obj_priv;
1967 int ret = 0;
1968
1969 for (;;) {
1970 /* If there's an inactive buffer available now, grab it
1971 * and be done.
1972 */
1973 if (!list_empty(&dev_priv->mm.inactive_list)) {
1974 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1975 struct drm_i915_gem_object,
1976 list);
1977 obj = obj_priv->obj;
1978 BUG_ON(obj_priv->pin_count != 0);
1979#if WATCH_LRU
1980 DRM_INFO("%s: evicting %p\n", __func__, obj);
1981#endif
1982 BUG_ON(obj_priv->active);
1983
1984 /* Wait on the rendering and unbind the buffer. */
1985 ret = i915_gem_object_unbind(obj);
1986 break;
1987 }
1988
1989 /* If we didn't get anything, but the ring is still processing
1990 * things, wait for one of those things to finish and hopefully
1991 * leave us a buffer to evict.
1992 */
1993 if (!list_empty(&dev_priv->mm.request_list)) {
1994 struct drm_i915_gem_request *request;
1995
1996 request = list_first_entry(&dev_priv->mm.request_list,
1997 struct drm_i915_gem_request,
1998 list);
1999
2000 ret = i915_wait_request(dev, request->seqno);
2001 if (ret)
2002 break;
2003
2004 /* if waiting caused an object to become inactive,
2005 * then loop around and wait for it. Otherwise, we
2006 * assume that waiting freed and unbound something,
2007 * so there should now be some space in the GTT
2008 */
2009 if (!list_empty(&dev_priv->mm.inactive_list))
2010 continue;
2011 break;
2012 }
2013
2014 /* If we didn't have anything on the request list but there
2015 * are buffers awaiting a flush, emit one and try again.
2016 * When we wait on it, those buffers waiting for that flush
2017 * will get moved to inactive.
2018 */
2019 if (!list_empty(&dev_priv->mm.flushing_list)) {
2020 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
2021 struct drm_i915_gem_object,
2022 list);
2023 obj = obj_priv->obj;
2024
2025 i915_gem_flush(dev,
2026 obj->write_domain,
2027 obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002028 i915_add_request(dev, NULL, obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002029
2030 obj = NULL;
2031 continue;
2032 }
2033
2034 DRM_ERROR("inactive empty %d request empty %d "
2035 "flushing empty %d\n",
2036 list_empty(&dev_priv->mm.inactive_list),
2037 list_empty(&dev_priv->mm.request_list),
2038 list_empty(&dev_priv->mm.flushing_list));
2039 /* If we didn't do any of the above, there's nothing to be done
2040 * and we just can't fit it in.
2041 */
Chris Wilson2939e1f2009-06-06 09:46:03 +01002042 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002043 }
2044 return ret;
2045}
2046
2047static int
Keith Packardac94a962008-11-20 23:30:27 -08002048i915_gem_evict_everything(struct drm_device *dev)
2049{
2050 int ret;
2051
2052 for (;;) {
2053 ret = i915_gem_evict_something(dev);
2054 if (ret != 0)
2055 break;
2056 }
Chris Wilson2939e1f2009-06-06 09:46:03 +01002057 if (ret == -ENOSPC)
Owain Ainsworth15c35332008-12-06 20:42:20 -08002058 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08002059 return ret;
2060}
2061
Ben Gamari6911a9b2009-04-02 11:24:54 -07002062int
Eric Anholt856fa192009-03-19 14:10:50 -07002063i915_gem_object_get_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002064{
2065 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2066 int page_count, i;
2067 struct address_space *mapping;
2068 struct inode *inode;
2069 struct page *page;
2070 int ret;
2071
Eric Anholt856fa192009-03-19 14:10:50 -07002072 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002073 return 0;
2074
2075 /* Get the list of pages out of our struct file. They'll be pinned
2076 * at this point until we release them.
2077 */
2078 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002079 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002080 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002081 if (obj_priv->pages == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002082 DRM_ERROR("Faled to allocate page list\n");
Eric Anholt856fa192009-03-19 14:10:50 -07002083 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002084 return -ENOMEM;
2085 }
2086
2087 inode = obj->filp->f_path.dentry->d_inode;
2088 mapping = inode->i_mapping;
2089 for (i = 0; i < page_count; i++) {
2090 page = read_mapping_page(mapping, i, NULL);
2091 if (IS_ERR(page)) {
2092 ret = PTR_ERR(page);
2093 DRM_ERROR("read_mapping_page failed: %d\n", ret);
Eric Anholt856fa192009-03-19 14:10:50 -07002094 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002095 return ret;
2096 }
Eric Anholt856fa192009-03-19 14:10:50 -07002097 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002098 }
Eric Anholt280b7132009-03-12 16:56:27 -07002099
2100 if (obj_priv->tiling_mode != I915_TILING_NONE)
2101 i915_gem_object_do_bit_17_swizzle(obj);
2102
Eric Anholt673a3942008-07-30 12:06:12 -07002103 return 0;
2104}
2105
Jesse Barnesde151cf2008-11-12 10:03:55 -08002106static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2107{
2108 struct drm_gem_object *obj = reg->obj;
2109 struct drm_device *dev = obj->dev;
2110 drm_i915_private_t *dev_priv = dev->dev_private;
2111 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2112 int regnum = obj_priv->fence_reg;
2113 uint64_t val;
2114
2115 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2116 0xfffff000) << 32;
2117 val |= obj_priv->gtt_offset & 0xfffff000;
2118 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2119 if (obj_priv->tiling_mode == I915_TILING_Y)
2120 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2121 val |= I965_FENCE_REG_VALID;
2122
2123 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2124}
2125
2126static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2127{
2128 struct drm_gem_object *obj = reg->obj;
2129 struct drm_device *dev = obj->dev;
2130 drm_i915_private_t *dev_priv = dev->dev_private;
2131 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2132 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002133 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002134 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002135 uint32_t pitch_val;
2136
2137 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2138 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002139 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002140 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002141 return;
2142 }
2143
Jesse Barnes0f973f22009-01-26 17:10:45 -08002144 if (obj_priv->tiling_mode == I915_TILING_Y &&
2145 HAS_128_BYTE_Y_TILING(dev))
2146 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002147 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002148 tile_width = 512;
2149
2150 /* Note: pitch better be a power of two tile widths */
2151 pitch_val = obj_priv->stride / tile_width;
2152 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002153
2154 val = obj_priv->gtt_offset;
2155 if (obj_priv->tiling_mode == I915_TILING_Y)
2156 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2157 val |= I915_FENCE_SIZE_BITS(obj->size);
2158 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2159 val |= I830_FENCE_REG_VALID;
2160
Eric Anholtdc529a42009-03-10 22:34:49 -07002161 if (regnum < 8)
2162 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2163 else
2164 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2165 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002166}
2167
2168static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2169{
2170 struct drm_gem_object *obj = reg->obj;
2171 struct drm_device *dev = obj->dev;
2172 drm_i915_private_t *dev_priv = dev->dev_private;
2173 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2174 int regnum = obj_priv->fence_reg;
2175 uint32_t val;
2176 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002177 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002178
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002179 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002180 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002181 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002182 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002183 return;
2184 }
2185
Eric Anholte76a16d2009-05-26 17:44:56 -07002186 pitch_val = obj_priv->stride / 128;
2187 pitch_val = ffs(pitch_val) - 1;
2188 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2189
Jesse Barnesde151cf2008-11-12 10:03:55 -08002190 val = obj_priv->gtt_offset;
2191 if (obj_priv->tiling_mode == I915_TILING_Y)
2192 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002193 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2194 WARN_ON(fence_size_bits & ~0x00000f00);
2195 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002196 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2197 val |= I830_FENCE_REG_VALID;
2198
2199 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002200}
2201
2202/**
2203 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2204 * @obj: object to map through a fence reg
2205 *
2206 * When mapping objects through the GTT, userspace wants to be able to write
2207 * to them without having to worry about swizzling if the object is tiled.
2208 *
2209 * This function walks the fence regs looking for a free one for @obj,
2210 * stealing one if it can't find any.
2211 *
2212 * It then sets up the reg based on the object's properties: address, pitch
2213 * and tiling format.
2214 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002215int
2216i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002217{
2218 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002219 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002220 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2221 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002222 struct drm_i915_gem_object *old_obj_priv = NULL;
2223 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002224
Eric Anholta09ba7f2009-08-29 12:49:51 -07002225 /* Just update our place in the LRU if our fence is getting used. */
2226 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
2227 list_move_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2228 return 0;
2229 }
2230
Jesse Barnesde151cf2008-11-12 10:03:55 -08002231 switch (obj_priv->tiling_mode) {
2232 case I915_TILING_NONE:
2233 WARN(1, "allocating a fence for non-tiled object?\n");
2234 break;
2235 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002236 if (!obj_priv->stride)
2237 return -EINVAL;
2238 WARN((obj_priv->stride & (512 - 1)),
2239 "object 0x%08x is X tiled but has non-512B pitch\n",
2240 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002241 break;
2242 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002243 if (!obj_priv->stride)
2244 return -EINVAL;
2245 WARN((obj_priv->stride & (128 - 1)),
2246 "object 0x%08x is Y tiled but has non-128B pitch\n",
2247 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002248 break;
2249 }
2250
2251 /* First try to find a free reg */
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002252 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002253 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2254 reg = &dev_priv->fence_regs[i];
2255 if (!reg->obj)
2256 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002257
2258 old_obj_priv = reg->obj->driver_private;
2259 if (!old_obj_priv->pin_count)
2260 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002261 }
2262
2263 /* None available, try to steal one or wait for a user to finish */
2264 if (i == dev_priv->num_fence_regs) {
Eric Anholta09ba7f2009-08-29 12:49:51 -07002265 struct drm_gem_object *old_obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002266
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002267 if (avail == 0)
Chris Wilson2939e1f2009-06-06 09:46:03 +01002268 return -ENOSPC;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002269
Eric Anholta09ba7f2009-08-29 12:49:51 -07002270 list_for_each_entry(old_obj_priv, &dev_priv->mm.fence_list,
2271 fence_list) {
2272 old_obj = old_obj_priv->obj;
Chris Wilsond7619c42009-02-11 14:26:47 +00002273
Chris Wilsond7619c42009-02-11 14:26:47 +00002274 if (old_obj_priv->pin_count)
2275 continue;
2276
Eric Anholta09ba7f2009-08-29 12:49:51 -07002277 /* Take a reference, as otherwise the wait_rendering
2278 * below may cause the object to get freed out from
2279 * under us.
2280 */
2281 drm_gem_object_reference(old_obj);
2282
Chris Wilsond7619c42009-02-11 14:26:47 +00002283 /* i915 uses fences for GPU access to tiled buffers */
2284 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002285 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002286
Eric Anholta09ba7f2009-08-29 12:49:51 -07002287 /* This brings the object to the head of the LRU if it
2288 * had been written to. The only way this should
2289 * result in us waiting longer than the expected
2290 * optimal amount of time is if there was a
2291 * fence-using buffer later that was read-only.
2292 */
2293 i915_gem_object_flush_gpu_write_domain(old_obj);
2294 ret = i915_gem_object_wait_rendering(old_obj);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002295 if (ret != 0) {
2296 drm_gem_object_unreference(old_obj);
Chris Wilsond7619c42009-02-11 14:26:47 +00002297 return ret;
Chris Wilson58c2fb62009-09-01 12:02:39 +01002298 }
2299
Eric Anholta09ba7f2009-08-29 12:49:51 -07002300 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002301 }
2302
2303 /*
2304 * Zap this virtual mapping so we can set up a fence again
2305 * for this object next time we need it.
2306 */
Chris Wilson58c2fb62009-09-01 12:02:39 +01002307 i915_gem_release_mmap(old_obj);
2308
Eric Anholta09ba7f2009-08-29 12:49:51 -07002309 i = old_obj_priv->fence_reg;
Chris Wilson58c2fb62009-09-01 12:02:39 +01002310 reg = &dev_priv->fence_regs[i];
2311
Jesse Barnesde151cf2008-11-12 10:03:55 -08002312 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002313 list_del_init(&old_obj_priv->fence_list);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002314
Eric Anholta09ba7f2009-08-29 12:49:51 -07002315 drm_gem_object_unreference(old_obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002316 }
2317
2318 obj_priv->fence_reg = i;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002319 list_add_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2320
Jesse Barnesde151cf2008-11-12 10:03:55 -08002321 reg->obj = obj;
2322
2323 if (IS_I965G(dev))
2324 i965_write_fence_reg(reg);
2325 else if (IS_I9XX(dev))
2326 i915_write_fence_reg(reg);
2327 else
2328 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002329
2330 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002331}
2332
2333/**
2334 * i915_gem_clear_fence_reg - clear out fence register info
2335 * @obj: object to clear
2336 *
2337 * Zeroes out the fence register itself and clears out the associated
2338 * data structures in dev_priv and obj_priv.
2339 */
2340static void
2341i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2342{
2343 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002344 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002345 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2346
2347 if (IS_I965G(dev))
2348 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002349 else {
2350 uint32_t fence_reg;
2351
2352 if (obj_priv->fence_reg < 8)
2353 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2354 else
2355 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2356 8) * 4;
2357
2358 I915_WRITE(fence_reg, 0);
2359 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002360
2361 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2362 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002363 list_del_init(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002364}
2365
Eric Anholt673a3942008-07-30 12:06:12 -07002366/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002367 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2368 * to the buffer to finish, and then resets the fence register.
2369 * @obj: tiled object holding a fence register.
2370 *
2371 * Zeroes out the fence register itself and clears out the associated
2372 * data structures in dev_priv and obj_priv.
2373 */
2374int
2375i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2376{
2377 struct drm_device *dev = obj->dev;
2378 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2379
2380 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2381 return 0;
2382
2383 /* On the i915, GPU access to tiled buffers is via a fence,
2384 * therefore we must wait for any outstanding access to complete
2385 * before clearing the fence.
2386 */
2387 if (!IS_I965G(dev)) {
2388 int ret;
2389
2390 i915_gem_object_flush_gpu_write_domain(obj);
2391 i915_gem_object_flush_gtt_write_domain(obj);
2392 ret = i915_gem_object_wait_rendering(obj);
2393 if (ret != 0)
2394 return ret;
2395 }
2396
2397 i915_gem_clear_fence_reg (obj);
2398
2399 return 0;
2400}
2401
2402/**
Eric Anholt673a3942008-07-30 12:06:12 -07002403 * Finds free space in the GTT aperture and binds the object there.
2404 */
2405static int
2406i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2407{
2408 struct drm_device *dev = obj->dev;
2409 drm_i915_private_t *dev_priv = dev->dev_private;
2410 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2411 struct drm_mm_node *free_space;
2412 int page_count, ret;
2413
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002414 if (dev_priv->mm.suspended)
2415 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002416 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002417 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002418 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002419 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2420 return -EINVAL;
2421 }
2422
2423 search_free:
2424 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2425 obj->size, alignment, 0);
2426 if (free_space != NULL) {
2427 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2428 alignment);
2429 if (obj_priv->gtt_space != NULL) {
2430 obj_priv->gtt_space->private = obj;
2431 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2432 }
2433 }
2434 if (obj_priv->gtt_space == NULL) {
Carl Worth5e118f42009-03-20 11:54:25 -07002435 bool lists_empty;
2436
Eric Anholt673a3942008-07-30 12:06:12 -07002437 /* If the gtt is empty and we're still having trouble
2438 * fitting our object in, we're out of memory.
2439 */
2440#if WATCH_LRU
2441 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2442#endif
Carl Worth5e118f42009-03-20 11:54:25 -07002443 spin_lock(&dev_priv->mm.active_list_lock);
2444 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2445 list_empty(&dev_priv->mm.flushing_list) &&
2446 list_empty(&dev_priv->mm.active_list));
2447 spin_unlock(&dev_priv->mm.active_list_lock);
2448 if (lists_empty) {
Eric Anholt673a3942008-07-30 12:06:12 -07002449 DRM_ERROR("GTT full, but LRU list empty\n");
Chris Wilson2939e1f2009-06-06 09:46:03 +01002450 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002451 }
2452
2453 ret = i915_gem_evict_something(dev);
2454 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08002455 if (ret != -ERESTARTSYS)
2456 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002457 return ret;
2458 }
2459 goto search_free;
2460 }
2461
2462#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002463 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002464 obj->size, obj_priv->gtt_offset);
2465#endif
Eric Anholt856fa192009-03-19 14:10:50 -07002466 ret = i915_gem_object_get_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002467 if (ret) {
2468 drm_mm_put_block(obj_priv->gtt_space);
2469 obj_priv->gtt_space = NULL;
2470 return ret;
2471 }
2472
2473 page_count = obj->size / PAGE_SIZE;
2474 /* Create an AGP memory structure pointing at our pages, and bind it
2475 * into the GTT.
2476 */
2477 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002478 obj_priv->pages,
Eric Anholt673a3942008-07-30 12:06:12 -07002479 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002480 obj_priv->gtt_offset,
2481 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002482 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002483 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002484 drm_mm_put_block(obj_priv->gtt_space);
2485 obj_priv->gtt_space = NULL;
2486 return -ENOMEM;
2487 }
2488 atomic_inc(&dev->gtt_count);
2489 atomic_add(obj->size, &dev->gtt_memory);
2490
2491 /* Assert that the object is not currently in any GPU domain. As it
2492 * wasn't in the GTT, there shouldn't be any way it could have been in
2493 * a GPU cache
2494 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002495 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2496 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002497
2498 return 0;
2499}
2500
2501void
2502i915_gem_clflush_object(struct drm_gem_object *obj)
2503{
2504 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2505
2506 /* If we don't have a page list set up, then we're not pinned
2507 * to GPU, and we can ignore the cache flush because it'll happen
2508 * again at bind time.
2509 */
Eric Anholt856fa192009-03-19 14:10:50 -07002510 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002511 return;
2512
Eric Anholtcfa16a02009-05-26 18:46:16 -07002513 /* XXX: The 865 in particular appears to be weird in how it handles
2514 * cache flushing. We haven't figured it out, but the
2515 * clflush+agp_chipset_flush doesn't appear to successfully get the
2516 * data visible to the PGU, while wbinvd + agp_chipset_flush does.
2517 */
2518 if (IS_I865G(obj->dev)) {
2519 wbinvd();
2520 return;
2521 }
2522
Eric Anholt856fa192009-03-19 14:10:50 -07002523 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002524}
2525
Eric Anholte47c68e2008-11-14 13:35:19 -08002526/** Flushes any GPU write domain for the object if it's dirty. */
2527static void
2528i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2529{
2530 struct drm_device *dev = obj->dev;
2531 uint32_t seqno;
2532
2533 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2534 return;
2535
2536 /* Queue the GPU write cache flushing we need. */
2537 i915_gem_flush(dev, 0, obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002538 seqno = i915_add_request(dev, NULL, obj->write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002539 obj->write_domain = 0;
2540 i915_gem_object_move_to_active(obj, seqno);
2541}
2542
2543/** Flushes the GTT write domain for the object if it's dirty. */
2544static void
2545i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2546{
2547 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2548 return;
2549
2550 /* No actual flushing is required for the GTT write domain. Writes
2551 * to it immediately go to main memory as far as we know, so there's
2552 * no chipset flush. It also doesn't land in render cache.
2553 */
2554 obj->write_domain = 0;
2555}
2556
2557/** Flushes the CPU write domain for the object if it's dirty. */
2558static void
2559i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2560{
2561 struct drm_device *dev = obj->dev;
2562
2563 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2564 return;
2565
2566 i915_gem_clflush_object(obj);
2567 drm_agp_chipset_flush(dev);
2568 obj->write_domain = 0;
2569}
2570
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002571/**
2572 * Moves a single object to the GTT read, and possibly write domain.
2573 *
2574 * This function returns when the move is complete, including waiting on
2575 * flushes to occur.
2576 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002577int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002578i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2579{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002580 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002581 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002582
Eric Anholt02354392008-11-26 13:58:13 -08002583 /* Not valid to be called on unbound objects. */
2584 if (obj_priv->gtt_space == NULL)
2585 return -EINVAL;
2586
Eric Anholte47c68e2008-11-14 13:35:19 -08002587 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002588 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002589 ret = i915_gem_object_wait_rendering(obj);
2590 if (ret != 0)
2591 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002592
2593 /* If we're writing through the GTT domain, then CPU and GPU caches
2594 * will need to be invalidated at next use.
2595 */
2596 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002597 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002598
Eric Anholte47c68e2008-11-14 13:35:19 -08002599 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002600
2601 /* It should now be out of any other write domains, and we can update
2602 * the domain values for our changes.
2603 */
2604 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2605 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002606 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002607 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002608 obj_priv->dirty = 1;
2609 }
2610
2611 return 0;
2612}
2613
2614/**
2615 * Moves a single object to the CPU read, and possibly write domain.
2616 *
2617 * This function returns when the move is complete, including waiting on
2618 * flushes to occur.
2619 */
2620static int
2621i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2622{
Eric Anholte47c68e2008-11-14 13:35:19 -08002623 int ret;
2624
2625 i915_gem_object_flush_gpu_write_domain(obj);
2626 /* Wait on any GPU rendering and flushing to occur. */
2627 ret = i915_gem_object_wait_rendering(obj);
2628 if (ret != 0)
2629 return ret;
2630
2631 i915_gem_object_flush_gtt_write_domain(obj);
2632
2633 /* If we have a partially-valid cache of the object in the CPU,
2634 * finish invalidating it and free the per-page flags.
2635 */
2636 i915_gem_object_set_to_full_cpu_read_domain(obj);
2637
2638 /* Flush the CPU cache if it's still invalid. */
2639 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2640 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002641
2642 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2643 }
2644
2645 /* It should now be out of any other write domains, and we can update
2646 * the domain values for our changes.
2647 */
2648 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2649
2650 /* If we're writing through the CPU, then the GPU read domains will
2651 * need to be invalidated at next use.
2652 */
2653 if (write) {
2654 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2655 obj->write_domain = I915_GEM_DOMAIN_CPU;
2656 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002657
2658 return 0;
2659}
2660
Eric Anholt673a3942008-07-30 12:06:12 -07002661/*
2662 * Set the next domain for the specified object. This
2663 * may not actually perform the necessary flushing/invaliding though,
2664 * as that may want to be batched with other set_domain operations
2665 *
2666 * This is (we hope) the only really tricky part of gem. The goal
2667 * is fairly simple -- track which caches hold bits of the object
2668 * and make sure they remain coherent. A few concrete examples may
2669 * help to explain how it works. For shorthand, we use the notation
2670 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2671 * a pair of read and write domain masks.
2672 *
2673 * Case 1: the batch buffer
2674 *
2675 * 1. Allocated
2676 * 2. Written by CPU
2677 * 3. Mapped to GTT
2678 * 4. Read by GPU
2679 * 5. Unmapped from GTT
2680 * 6. Freed
2681 *
2682 * Let's take these a step at a time
2683 *
2684 * 1. Allocated
2685 * Pages allocated from the kernel may still have
2686 * cache contents, so we set them to (CPU, CPU) always.
2687 * 2. Written by CPU (using pwrite)
2688 * The pwrite function calls set_domain (CPU, CPU) and
2689 * this function does nothing (as nothing changes)
2690 * 3. Mapped by GTT
2691 * This function asserts that the object is not
2692 * currently in any GPU-based read or write domains
2693 * 4. Read by GPU
2694 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2695 * As write_domain is zero, this function adds in the
2696 * current read domains (CPU+COMMAND, 0).
2697 * flush_domains is set to CPU.
2698 * invalidate_domains is set to COMMAND
2699 * clflush is run to get data out of the CPU caches
2700 * then i915_dev_set_domain calls i915_gem_flush to
2701 * emit an MI_FLUSH and drm_agp_chipset_flush
2702 * 5. Unmapped from GTT
2703 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2704 * flush_domains and invalidate_domains end up both zero
2705 * so no flushing/invalidating happens
2706 * 6. Freed
2707 * yay, done
2708 *
2709 * Case 2: The shared render buffer
2710 *
2711 * 1. Allocated
2712 * 2. Mapped to GTT
2713 * 3. Read/written by GPU
2714 * 4. set_domain to (CPU,CPU)
2715 * 5. Read/written by CPU
2716 * 6. Read/written by GPU
2717 *
2718 * 1. Allocated
2719 * Same as last example, (CPU, CPU)
2720 * 2. Mapped to GTT
2721 * Nothing changes (assertions find that it is not in the GPU)
2722 * 3. Read/written by GPU
2723 * execbuffer calls set_domain (RENDER, RENDER)
2724 * flush_domains gets CPU
2725 * invalidate_domains gets GPU
2726 * clflush (obj)
2727 * MI_FLUSH and drm_agp_chipset_flush
2728 * 4. set_domain (CPU, CPU)
2729 * flush_domains gets GPU
2730 * invalidate_domains gets CPU
2731 * wait_rendering (obj) to make sure all drawing is complete.
2732 * This will include an MI_FLUSH to get the data from GPU
2733 * to memory
2734 * clflush (obj) to invalidate the CPU cache
2735 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2736 * 5. Read/written by CPU
2737 * cache lines are loaded and dirtied
2738 * 6. Read written by GPU
2739 * Same as last GPU access
2740 *
2741 * Case 3: The constant buffer
2742 *
2743 * 1. Allocated
2744 * 2. Written by CPU
2745 * 3. Read by GPU
2746 * 4. Updated (written) by CPU again
2747 * 5. Read by GPU
2748 *
2749 * 1. Allocated
2750 * (CPU, CPU)
2751 * 2. Written by CPU
2752 * (CPU, CPU)
2753 * 3. Read by GPU
2754 * (CPU+RENDER, 0)
2755 * flush_domains = CPU
2756 * invalidate_domains = RENDER
2757 * clflush (obj)
2758 * MI_FLUSH
2759 * drm_agp_chipset_flush
2760 * 4. Updated (written) by CPU again
2761 * (CPU, CPU)
2762 * flush_domains = 0 (no previous write domain)
2763 * invalidate_domains = 0 (no new read domains)
2764 * 5. Read by GPU
2765 * (CPU+RENDER, 0)
2766 * flush_domains = CPU
2767 * invalidate_domains = RENDER
2768 * clflush (obj)
2769 * MI_FLUSH
2770 * drm_agp_chipset_flush
2771 */
Keith Packardc0d90822008-11-20 23:11:08 -08002772static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002773i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002774{
2775 struct drm_device *dev = obj->dev;
2776 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2777 uint32_t invalidate_domains = 0;
2778 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002779
Eric Anholt8b0e3782009-02-19 14:40:50 -08002780 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2781 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002782
Jesse Barnes652c3932009-08-17 13:31:43 -07002783 intel_mark_busy(dev, obj);
2784
Eric Anholt673a3942008-07-30 12:06:12 -07002785#if WATCH_BUF
2786 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2787 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002788 obj->read_domains, obj->pending_read_domains,
2789 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002790#endif
2791 /*
2792 * If the object isn't moving to a new write domain,
2793 * let the object stay in multiple read domains
2794 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002795 if (obj->pending_write_domain == 0)
2796 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002797 else
2798 obj_priv->dirty = 1;
2799
2800 /*
2801 * Flush the current write domain if
2802 * the new read domains don't match. Invalidate
2803 * any read domains which differ from the old
2804 * write domain
2805 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002806 if (obj->write_domain &&
2807 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002808 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002809 invalidate_domains |=
2810 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002811 }
2812 /*
2813 * Invalidate any read caches which may have
2814 * stale data. That is, any new read domains.
2815 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002816 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002817 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2818#if WATCH_BUF
2819 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2820 __func__, flush_domains, invalidate_domains);
2821#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002822 i915_gem_clflush_object(obj);
2823 }
2824
Eric Anholtefbeed92009-02-19 14:54:51 -08002825 /* The actual obj->write_domain will be updated with
2826 * pending_write_domain after we emit the accumulated flush for all
2827 * of our domain changes in execbuffers (which clears objects'
2828 * write_domains). So if we have a current write domain that we
2829 * aren't changing, set pending_write_domain to that.
2830 */
2831 if (flush_domains == 0 && obj->pending_write_domain == 0)
2832 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002833 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002834
2835 dev->invalidate_domains |= invalidate_domains;
2836 dev->flush_domains |= flush_domains;
2837#if WATCH_BUF
2838 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2839 __func__,
2840 obj->read_domains, obj->write_domain,
2841 dev->invalidate_domains, dev->flush_domains);
2842#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002843}
2844
2845/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002846 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002847 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002848 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2849 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2850 */
2851static void
2852i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2853{
Eric Anholte47c68e2008-11-14 13:35:19 -08002854 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2855
2856 if (!obj_priv->page_cpu_valid)
2857 return;
2858
2859 /* If we're partially in the CPU read domain, finish moving it in.
2860 */
2861 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2862 int i;
2863
2864 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2865 if (obj_priv->page_cpu_valid[i])
2866 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07002867 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08002868 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002869 }
2870
2871 /* Free the page_cpu_valid mappings which are now stale, whether
2872 * or not we've got I915_GEM_DOMAIN_CPU.
2873 */
Eric Anholt9a298b22009-03-24 12:23:04 -07002874 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08002875 obj_priv->page_cpu_valid = NULL;
2876}
2877
2878/**
2879 * Set the CPU read domain on a range of the object.
2880 *
2881 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2882 * not entirely valid. The page_cpu_valid member of the object flags which
2883 * pages have been flushed, and will be respected by
2884 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2885 * of the whole object.
2886 *
2887 * This function returns when the move is complete, including waiting on
2888 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002889 */
2890static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002891i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2892 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002893{
2894 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002895 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002896
Eric Anholte47c68e2008-11-14 13:35:19 -08002897 if (offset == 0 && size == obj->size)
2898 return i915_gem_object_set_to_cpu_domain(obj, 0);
2899
2900 i915_gem_object_flush_gpu_write_domain(obj);
2901 /* Wait on any GPU rendering and flushing to occur. */
2902 ret = i915_gem_object_wait_rendering(obj);
2903 if (ret != 0)
2904 return ret;
2905 i915_gem_object_flush_gtt_write_domain(obj);
2906
2907 /* If we're already fully in the CPU read domain, we're done. */
2908 if (obj_priv->page_cpu_valid == NULL &&
2909 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002910 return 0;
2911
Eric Anholte47c68e2008-11-14 13:35:19 -08002912 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2913 * newly adding I915_GEM_DOMAIN_CPU
2914 */
Eric Anholt673a3942008-07-30 12:06:12 -07002915 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07002916 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
2917 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08002918 if (obj_priv->page_cpu_valid == NULL)
2919 return -ENOMEM;
2920 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2921 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002922
2923 /* Flush the cache on any pages that are still invalid from the CPU's
2924 * perspective.
2925 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002926 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2927 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002928 if (obj_priv->page_cpu_valid[i])
2929 continue;
2930
Eric Anholt856fa192009-03-19 14:10:50 -07002931 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002932
2933 obj_priv->page_cpu_valid[i] = 1;
2934 }
2935
Eric Anholte47c68e2008-11-14 13:35:19 -08002936 /* It should now be out of any other write domains, and we can update
2937 * the domain values for our changes.
2938 */
2939 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2940
2941 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2942
Eric Anholt673a3942008-07-30 12:06:12 -07002943 return 0;
2944}
2945
2946/**
Eric Anholt673a3942008-07-30 12:06:12 -07002947 * Pin an object to the GTT and evaluate the relocations landing in it.
2948 */
2949static int
2950i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2951 struct drm_file *file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002952 struct drm_i915_gem_exec_object *entry,
2953 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07002954{
2955 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002956 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002957 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2958 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002959 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002960
2961 /* Choose the GTT offset for our buffer and put it there. */
2962 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2963 if (ret)
2964 return ret;
2965
2966 entry->offset = obj_priv->gtt_offset;
2967
Eric Anholt673a3942008-07-30 12:06:12 -07002968 /* Apply the relocations, using the GTT aperture to avoid cache
2969 * flushing requirements.
2970 */
2971 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002972 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002973 struct drm_gem_object *target_obj;
2974 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002975 uint32_t reloc_val, reloc_offset;
2976 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002977
Eric Anholt673a3942008-07-30 12:06:12 -07002978 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002979 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002980 if (target_obj == NULL) {
2981 i915_gem_object_unpin(obj);
2982 return -EBADF;
2983 }
2984 target_obj_priv = target_obj->driver_private;
2985
2986 /* The target buffer should have appeared before us in the
2987 * exec_object list, so it should have a GTT space bound by now.
2988 */
2989 if (target_obj_priv->gtt_space == NULL) {
2990 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002991 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002992 drm_gem_object_unreference(target_obj);
2993 i915_gem_object_unpin(obj);
2994 return -EINVAL;
2995 }
2996
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002997 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07002998 DRM_ERROR("Relocation beyond object bounds: "
2999 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003000 obj, reloc->target_handle,
3001 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003002 drm_gem_object_unreference(target_obj);
3003 i915_gem_object_unpin(obj);
3004 return -EINVAL;
3005 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003006 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003007 DRM_ERROR("Relocation not 4-byte aligned: "
3008 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003009 obj, reloc->target_handle,
3010 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003011 drm_gem_object_unreference(target_obj);
3012 i915_gem_object_unpin(obj);
3013 return -EINVAL;
3014 }
3015
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003016 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3017 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003018 DRM_ERROR("reloc with read/write CPU domains: "
3019 "obj %p target %d offset %d "
3020 "read %08x write %08x",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003021 obj, reloc->target_handle,
3022 (int) reloc->offset,
3023 reloc->read_domains,
3024 reloc->write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00003025 drm_gem_object_unreference(target_obj);
3026 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003027 return -EINVAL;
3028 }
3029
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003030 if (reloc->write_domain && target_obj->pending_write_domain &&
3031 reloc->write_domain != target_obj->pending_write_domain) {
Eric Anholt673a3942008-07-30 12:06:12 -07003032 DRM_ERROR("Write domain conflict: "
3033 "obj %p target %d offset %d "
3034 "new %08x old %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003035 obj, reloc->target_handle,
3036 (int) reloc->offset,
3037 reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003038 target_obj->pending_write_domain);
3039 drm_gem_object_unreference(target_obj);
3040 i915_gem_object_unpin(obj);
3041 return -EINVAL;
3042 }
3043
3044#if WATCH_RELOC
3045 DRM_INFO("%s: obj %p offset %08x target %d "
3046 "read %08x write %08x gtt %08x "
3047 "presumed %08x delta %08x\n",
3048 __func__,
3049 obj,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003050 (int) reloc->offset,
3051 (int) reloc->target_handle,
3052 (int) reloc->read_domains,
3053 (int) reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003054 (int) target_obj_priv->gtt_offset,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003055 (int) reloc->presumed_offset,
3056 reloc->delta);
Eric Anholt673a3942008-07-30 12:06:12 -07003057#endif
3058
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003059 target_obj->pending_read_domains |= reloc->read_domains;
3060 target_obj->pending_write_domain |= reloc->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003061
3062 /* If the relocation already has the right value in it, no
3063 * more work needs to be done.
3064 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003065 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
Eric Anholt673a3942008-07-30 12:06:12 -07003066 drm_gem_object_unreference(target_obj);
3067 continue;
3068 }
3069
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003070 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3071 if (ret != 0) {
3072 drm_gem_object_unreference(target_obj);
3073 i915_gem_object_unpin(obj);
3074 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003075 }
3076
3077 /* Map the page containing the relocation we're going to
3078 * perform.
3079 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003080 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003081 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3082 (reloc_offset &
3083 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003084 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003085 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003086 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003087
3088#if WATCH_BUF
3089 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003090 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003091 readl(reloc_entry), reloc_val);
3092#endif
3093 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003094 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003095
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003096 /* The updated presumed offset for this entry will be
3097 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003098 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003099 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003100
3101 drm_gem_object_unreference(target_obj);
3102 }
3103
Eric Anholt673a3942008-07-30 12:06:12 -07003104#if WATCH_BUF
3105 if (0)
3106 i915_gem_dump_object(obj, 128, __func__, ~0);
3107#endif
3108 return 0;
3109}
3110
3111/** Dispatch a batchbuffer to the ring
3112 */
3113static int
3114i915_dispatch_gem_execbuffer(struct drm_device *dev,
3115 struct drm_i915_gem_execbuffer *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003116 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003117 uint64_t exec_offset)
3118{
3119 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003120 int nbox = exec->num_cliprects;
3121 int i = 0, count;
Chris Wilson83d60792009-06-06 09:45:57 +01003122 uint32_t exec_start, exec_len;
Eric Anholt673a3942008-07-30 12:06:12 -07003123 RING_LOCALS;
3124
3125 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3126 exec_len = (uint32_t) exec->batch_len;
3127
Eric Anholt673a3942008-07-30 12:06:12 -07003128 count = nbox ? nbox : 1;
3129
3130 for (i = 0; i < count; i++) {
3131 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003132 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003133 exec->DR1, exec->DR4);
3134 if (ret)
3135 return ret;
3136 }
3137
3138 if (IS_I830(dev) || IS_845G(dev)) {
3139 BEGIN_LP_RING(4);
3140 OUT_RING(MI_BATCH_BUFFER);
3141 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3142 OUT_RING(exec_start + exec_len - 4);
3143 OUT_RING(0);
3144 ADVANCE_LP_RING();
3145 } else {
3146 BEGIN_LP_RING(2);
3147 if (IS_I965G(dev)) {
3148 OUT_RING(MI_BATCH_BUFFER_START |
3149 (2 << 6) |
3150 MI_BATCH_NON_SECURE_I965);
3151 OUT_RING(exec_start);
3152 } else {
3153 OUT_RING(MI_BATCH_BUFFER_START |
3154 (2 << 6));
3155 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3156 }
3157 ADVANCE_LP_RING();
3158 }
3159 }
3160
3161 /* XXX breadcrumb */
3162 return 0;
3163}
3164
3165/* Throttle our rendering by waiting until the ring has completed our requests
3166 * emitted over 20 msec ago.
3167 *
Eric Anholtb9624422009-06-03 07:27:35 +00003168 * Note that if we were to use the current jiffies each time around the loop,
3169 * we wouldn't escape the function with any frames outstanding if the time to
3170 * render a frame was over 20ms.
3171 *
Eric Anholt673a3942008-07-30 12:06:12 -07003172 * This should get us reasonable parallelism between CPU and GPU but also
3173 * relatively low latency when blocking on a particular request to finish.
3174 */
3175static int
3176i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3177{
3178 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3179 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003180 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003181
3182 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003183 while (!list_empty(&i915_file_priv->mm.request_list)) {
3184 struct drm_i915_gem_request *request;
3185
3186 request = list_first_entry(&i915_file_priv->mm.request_list,
3187 struct drm_i915_gem_request,
3188 client_list);
3189
3190 if (time_after_eq(request->emitted_jiffies, recent_enough))
3191 break;
3192
3193 ret = i915_wait_request(dev, request->seqno);
3194 if (ret != 0)
3195 break;
3196 }
Eric Anholt673a3942008-07-30 12:06:12 -07003197 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003198
Eric Anholt673a3942008-07-30 12:06:12 -07003199 return ret;
3200}
3201
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003202static int
3203i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3204 uint32_t buffer_count,
3205 struct drm_i915_gem_relocation_entry **relocs)
3206{
3207 uint32_t reloc_count = 0, reloc_index = 0, i;
3208 int ret;
3209
3210 *relocs = NULL;
3211 for (i = 0; i < buffer_count; i++) {
3212 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3213 return -EINVAL;
3214 reloc_count += exec_list[i].relocation_count;
3215 }
3216
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003217 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003218 if (*relocs == NULL)
3219 return -ENOMEM;
3220
3221 for (i = 0; i < buffer_count; i++) {
3222 struct drm_i915_gem_relocation_entry __user *user_relocs;
3223
3224 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3225
3226 ret = copy_from_user(&(*relocs)[reloc_index],
3227 user_relocs,
3228 exec_list[i].relocation_count *
3229 sizeof(**relocs));
3230 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003231 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003232 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003233 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003234 }
3235
3236 reloc_index += exec_list[i].relocation_count;
3237 }
3238
Florian Mickler2bc43b52009-04-06 22:55:41 +02003239 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003240}
3241
3242static int
3243i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3244 uint32_t buffer_count,
3245 struct drm_i915_gem_relocation_entry *relocs)
3246{
3247 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003248 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003249
3250 for (i = 0; i < buffer_count; i++) {
3251 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003252 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003253
3254 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3255
Florian Mickler2bc43b52009-04-06 22:55:41 +02003256 unwritten = copy_to_user(user_relocs,
3257 &relocs[reloc_count],
3258 exec_list[i].relocation_count *
3259 sizeof(*relocs));
3260
3261 if (unwritten) {
3262 ret = -EFAULT;
3263 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003264 }
3265
3266 reloc_count += exec_list[i].relocation_count;
3267 }
3268
Florian Mickler2bc43b52009-04-06 22:55:41 +02003269err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003270 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003271
3272 return ret;
3273}
3274
Chris Wilson83d60792009-06-06 09:45:57 +01003275static int
3276i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer *exec,
3277 uint64_t exec_offset)
3278{
3279 uint32_t exec_start, exec_len;
3280
3281 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3282 exec_len = (uint32_t) exec->batch_len;
3283
3284 if ((exec_start | exec_len) & 0x7)
3285 return -EINVAL;
3286
3287 if (!exec_start)
3288 return -EINVAL;
3289
3290 return 0;
3291}
3292
Eric Anholt673a3942008-07-30 12:06:12 -07003293int
3294i915_gem_execbuffer(struct drm_device *dev, void *data,
3295 struct drm_file *file_priv)
3296{
3297 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003298 struct drm_i915_gem_execbuffer *args = data;
3299 struct drm_i915_gem_exec_object *exec_list = NULL;
3300 struct drm_gem_object **object_list = NULL;
3301 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003302 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003303 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003304 struct drm_i915_gem_relocation_entry *relocs;
3305 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003306 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003307 uint32_t seqno, flush_domains, reloc_index;
Keith Packardac94a962008-11-20 23:30:27 -08003308 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07003309
3310#if WATCH_EXEC
3311 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3312 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3313#endif
3314
Eric Anholt4f481ed2008-09-10 14:22:49 -07003315 if (args->buffer_count < 1) {
3316 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3317 return -EINVAL;
3318 }
Eric Anholt673a3942008-07-30 12:06:12 -07003319 /* Copy in the exec list from userland */
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003320 exec_list = drm_calloc_large(sizeof(*exec_list), args->buffer_count);
3321 object_list = drm_calloc_large(sizeof(*object_list), args->buffer_count);
Eric Anholt673a3942008-07-30 12:06:12 -07003322 if (exec_list == NULL || object_list == NULL) {
3323 DRM_ERROR("Failed to allocate exec or object list "
3324 "for %d buffers\n",
3325 args->buffer_count);
3326 ret = -ENOMEM;
3327 goto pre_mutex_err;
3328 }
3329 ret = copy_from_user(exec_list,
3330 (struct drm_i915_relocation_entry __user *)
3331 (uintptr_t) args->buffers_ptr,
3332 sizeof(*exec_list) * args->buffer_count);
3333 if (ret != 0) {
3334 DRM_ERROR("copy %d exec entries failed %d\n",
3335 args->buffer_count, ret);
3336 goto pre_mutex_err;
3337 }
3338
Eric Anholt201361a2009-03-11 12:30:04 -07003339 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003340 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3341 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -07003342 if (cliprects == NULL)
3343 goto pre_mutex_err;
3344
3345 ret = copy_from_user(cliprects,
3346 (struct drm_clip_rect __user *)
3347 (uintptr_t) args->cliprects_ptr,
3348 sizeof(*cliprects) * args->num_cliprects);
3349 if (ret != 0) {
3350 DRM_ERROR("copy %d cliprects failed: %d\n",
3351 args->num_cliprects, ret);
3352 goto pre_mutex_err;
3353 }
3354 }
3355
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003356 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3357 &relocs);
3358 if (ret != 0)
3359 goto pre_mutex_err;
3360
Eric Anholt673a3942008-07-30 12:06:12 -07003361 mutex_lock(&dev->struct_mutex);
3362
3363 i915_verify_inactive(dev, __FILE__, __LINE__);
3364
3365 if (dev_priv->mm.wedged) {
3366 DRM_ERROR("Execbuf while wedged\n");
3367 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003368 ret = -EIO;
3369 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003370 }
3371
3372 if (dev_priv->mm.suspended) {
3373 DRM_ERROR("Execbuf while VT-switched.\n");
3374 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003375 ret = -EBUSY;
3376 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003377 }
3378
Keith Packardac94a962008-11-20 23:30:27 -08003379 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003380 for (i = 0; i < args->buffer_count; i++) {
3381 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3382 exec_list[i].handle);
3383 if (object_list[i] == NULL) {
3384 DRM_ERROR("Invalid object handle %d at index %d\n",
3385 exec_list[i].handle, i);
3386 ret = -EBADF;
3387 goto err;
3388 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003389
3390 obj_priv = object_list[i]->driver_private;
3391 if (obj_priv->in_execbuffer) {
3392 DRM_ERROR("Object %p appears more than once in object list\n",
3393 object_list[i]);
3394 ret = -EBADF;
3395 goto err;
3396 }
3397 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003398 }
Eric Anholt673a3942008-07-30 12:06:12 -07003399
Keith Packardac94a962008-11-20 23:30:27 -08003400 /* Pin and relocate */
3401 for (pin_tries = 0; ; pin_tries++) {
3402 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003403 reloc_index = 0;
3404
Keith Packardac94a962008-11-20 23:30:27 -08003405 for (i = 0; i < args->buffer_count; i++) {
3406 object_list[i]->pending_read_domains = 0;
3407 object_list[i]->pending_write_domain = 0;
3408 ret = i915_gem_object_pin_and_relocate(object_list[i],
3409 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003410 &exec_list[i],
3411 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003412 if (ret)
3413 break;
3414 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003415 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003416 }
3417 /* success */
3418 if (ret == 0)
3419 break;
3420
3421 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003422 if (ret != -ENOSPC || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08003423 if (ret != -ERESTARTSYS)
3424 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003425 goto err;
3426 }
Keith Packardac94a962008-11-20 23:30:27 -08003427
3428 /* unpin all of our buffers */
3429 for (i = 0; i < pinned; i++)
3430 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003431 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003432
3433 /* evict everyone we can from the aperture */
3434 ret = i915_gem_evict_everything(dev);
3435 if (ret)
3436 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003437 }
3438
3439 /* Set the pending read domains for the batch buffer to COMMAND */
3440 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003441 if (batch_obj->pending_write_domain) {
3442 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3443 ret = -EINVAL;
3444 goto err;
3445 }
3446 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003447
Chris Wilson83d60792009-06-06 09:45:57 +01003448 /* Sanity check the batch buffer, prior to moving objects */
3449 exec_offset = exec_list[args->buffer_count - 1].offset;
3450 ret = i915_gem_check_execbuffer (args, exec_offset);
3451 if (ret != 0) {
3452 DRM_ERROR("execbuf with invalid offset/length\n");
3453 goto err;
3454 }
3455
Eric Anholt673a3942008-07-30 12:06:12 -07003456 i915_verify_inactive(dev, __FILE__, __LINE__);
3457
Keith Packard646f0f62008-11-20 23:23:03 -08003458 /* Zero the global flush/invalidate flags. These
3459 * will be modified as new domains are computed
3460 * for each object
3461 */
3462 dev->invalidate_domains = 0;
3463 dev->flush_domains = 0;
3464
Eric Anholt673a3942008-07-30 12:06:12 -07003465 for (i = 0; i < args->buffer_count; i++) {
3466 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003467
Keith Packard646f0f62008-11-20 23:23:03 -08003468 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003469 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003470 }
3471
3472 i915_verify_inactive(dev, __FILE__, __LINE__);
3473
Keith Packard646f0f62008-11-20 23:23:03 -08003474 if (dev->invalidate_domains | dev->flush_domains) {
3475#if WATCH_EXEC
3476 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3477 __func__,
3478 dev->invalidate_domains,
3479 dev->flush_domains);
3480#endif
3481 i915_gem_flush(dev,
3482 dev->invalidate_domains,
3483 dev->flush_domains);
3484 if (dev->flush_domains)
Eric Anholtb9624422009-06-03 07:27:35 +00003485 (void)i915_add_request(dev, file_priv,
3486 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003487 }
Eric Anholt673a3942008-07-30 12:06:12 -07003488
Eric Anholtefbeed92009-02-19 14:54:51 -08003489 for (i = 0; i < args->buffer_count; i++) {
3490 struct drm_gem_object *obj = object_list[i];
3491
3492 obj->write_domain = obj->pending_write_domain;
3493 }
3494
Eric Anholt673a3942008-07-30 12:06:12 -07003495 i915_verify_inactive(dev, __FILE__, __LINE__);
3496
3497#if WATCH_COHERENCY
3498 for (i = 0; i < args->buffer_count; i++) {
3499 i915_gem_object_check_coherency(object_list[i],
3500 exec_list[i].handle);
3501 }
3502#endif
3503
Eric Anholt673a3942008-07-30 12:06:12 -07003504#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003505 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003506 args->batch_len,
3507 __func__,
3508 ~0);
3509#endif
3510
Eric Anholt673a3942008-07-30 12:06:12 -07003511 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003512 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003513 if (ret) {
3514 DRM_ERROR("dispatch failed %d\n", ret);
3515 goto err;
3516 }
3517
3518 /*
3519 * Ensure that the commands in the batch buffer are
3520 * finished before the interrupt fires
3521 */
3522 flush_domains = i915_retire_commands(dev);
3523
3524 i915_verify_inactive(dev, __FILE__, __LINE__);
3525
3526 /*
3527 * Get a seqno representing the execution of the current buffer,
3528 * which we can wait on. We would like to mitigate these interrupts,
3529 * likely by only creating seqnos occasionally (so that we have
3530 * *some* interrupts representing completion of buffers that we can
3531 * wait on when trying to clear up gtt space).
3532 */
Eric Anholtb9624422009-06-03 07:27:35 +00003533 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003534 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003535 for (i = 0; i < args->buffer_count; i++) {
3536 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003537
Eric Anholtce44b0e2008-11-06 16:00:31 -08003538 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003539#if WATCH_LRU
3540 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3541#endif
3542 }
3543#if WATCH_LRU
3544 i915_dump_lru(dev, __func__);
3545#endif
3546
3547 i915_verify_inactive(dev, __FILE__, __LINE__);
3548
Eric Anholt673a3942008-07-30 12:06:12 -07003549err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003550 for (i = 0; i < pinned; i++)
3551 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003552
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003553 for (i = 0; i < args->buffer_count; i++) {
3554 if (object_list[i]) {
3555 obj_priv = object_list[i]->driver_private;
3556 obj_priv->in_execbuffer = false;
3557 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003558 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003559 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003560
Eric Anholt673a3942008-07-30 12:06:12 -07003561 mutex_unlock(&dev->struct_mutex);
3562
Roland Dreiera35f2e22009-02-06 17:48:09 -08003563 if (!ret) {
3564 /* Copy the new buffer offsets back to the user's exec list. */
3565 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3566 (uintptr_t) args->buffers_ptr,
3567 exec_list,
3568 sizeof(*exec_list) * args->buffer_count);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003569 if (ret) {
3570 ret = -EFAULT;
Roland Dreiera35f2e22009-02-06 17:48:09 -08003571 DRM_ERROR("failed to copy %d exec entries "
3572 "back to user (%d)\n",
3573 args->buffer_count, ret);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003574 }
Roland Dreiera35f2e22009-02-06 17:48:09 -08003575 }
3576
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003577 /* Copy the updated relocations out regardless of current error
3578 * state. Failure to update the relocs would mean that the next
3579 * time userland calls execbuf, it would do so with presumed offset
3580 * state that didn't match the actual object state.
3581 */
3582 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3583 relocs);
3584 if (ret2 != 0) {
3585 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3586
3587 if (ret == 0)
3588 ret = ret2;
3589 }
3590
Eric Anholt673a3942008-07-30 12:06:12 -07003591pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003592 drm_free_large(object_list);
3593 drm_free_large(exec_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003594 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003595
3596 return ret;
3597}
3598
3599int
3600i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3601{
3602 struct drm_device *dev = obj->dev;
3603 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3604 int ret;
3605
3606 i915_verify_inactive(dev, __FILE__, __LINE__);
3607 if (obj_priv->gtt_space == NULL) {
3608 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3609 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003610 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05003611 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003612 return ret;
3613 }
Chris Wilson22c344e2009-02-11 14:26:45 +00003614 }
3615 /*
3616 * Pre-965 chips need a fence register set up in order to
3617 * properly handle tiled surfaces.
3618 */
Eric Anholta09ba7f2009-08-29 12:49:51 -07003619 if (!IS_I965G(dev) && obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003620 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson22c344e2009-02-11 14:26:45 +00003621 if (ret != 0) {
3622 if (ret != -EBUSY && ret != -ERESTARTSYS)
3623 DRM_ERROR("Failure to install fence: %d\n",
3624 ret);
3625 return ret;
3626 }
Eric Anholt673a3942008-07-30 12:06:12 -07003627 }
3628 obj_priv->pin_count++;
3629
3630 /* If the object is not active and not pending a flush,
3631 * remove it from the inactive list
3632 */
3633 if (obj_priv->pin_count == 1) {
3634 atomic_inc(&dev->pin_count);
3635 atomic_add(obj->size, &dev->pin_memory);
3636 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003637 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07003638 !list_empty(&obj_priv->list))
3639 list_del_init(&obj_priv->list);
3640 }
3641 i915_verify_inactive(dev, __FILE__, __LINE__);
3642
3643 return 0;
3644}
3645
3646void
3647i915_gem_object_unpin(struct drm_gem_object *obj)
3648{
3649 struct drm_device *dev = obj->dev;
3650 drm_i915_private_t *dev_priv = dev->dev_private;
3651 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3652
3653 i915_verify_inactive(dev, __FILE__, __LINE__);
3654 obj_priv->pin_count--;
3655 BUG_ON(obj_priv->pin_count < 0);
3656 BUG_ON(obj_priv->gtt_space == NULL);
3657
3658 /* If the object is no longer pinned, and is
3659 * neither active nor being flushed, then stick it on
3660 * the inactive list
3661 */
3662 if (obj_priv->pin_count == 0) {
3663 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003664 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003665 list_move_tail(&obj_priv->list,
3666 &dev_priv->mm.inactive_list);
3667 atomic_dec(&dev->pin_count);
3668 atomic_sub(obj->size, &dev->pin_memory);
3669 }
3670 i915_verify_inactive(dev, __FILE__, __LINE__);
3671}
3672
3673int
3674i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3675 struct drm_file *file_priv)
3676{
3677 struct drm_i915_gem_pin *args = data;
3678 struct drm_gem_object *obj;
3679 struct drm_i915_gem_object *obj_priv;
3680 int ret;
3681
3682 mutex_lock(&dev->struct_mutex);
3683
3684 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3685 if (obj == NULL) {
3686 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3687 args->handle);
3688 mutex_unlock(&dev->struct_mutex);
3689 return -EBADF;
3690 }
3691 obj_priv = obj->driver_private;
3692
Jesse Barnes79e53942008-11-07 14:24:08 -08003693 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3694 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3695 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00003696 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003697 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003698 return -EINVAL;
3699 }
3700
3701 obj_priv->user_pin_count++;
3702 obj_priv->pin_filp = file_priv;
3703 if (obj_priv->user_pin_count == 1) {
3704 ret = i915_gem_object_pin(obj, args->alignment);
3705 if (ret != 0) {
3706 drm_gem_object_unreference(obj);
3707 mutex_unlock(&dev->struct_mutex);
3708 return ret;
3709 }
Eric Anholt673a3942008-07-30 12:06:12 -07003710 }
3711
3712 /* XXX - flush the CPU caches for pinned objects
3713 * as the X server doesn't manage domains yet
3714 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003715 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003716 args->offset = obj_priv->gtt_offset;
3717 drm_gem_object_unreference(obj);
3718 mutex_unlock(&dev->struct_mutex);
3719
3720 return 0;
3721}
3722
3723int
3724i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3725 struct drm_file *file_priv)
3726{
3727 struct drm_i915_gem_pin *args = data;
3728 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08003729 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003730
3731 mutex_lock(&dev->struct_mutex);
3732
3733 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3734 if (obj == NULL) {
3735 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3736 args->handle);
3737 mutex_unlock(&dev->struct_mutex);
3738 return -EBADF;
3739 }
3740
Jesse Barnes79e53942008-11-07 14:24:08 -08003741 obj_priv = obj->driver_private;
3742 if (obj_priv->pin_filp != file_priv) {
3743 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3744 args->handle);
3745 drm_gem_object_unreference(obj);
3746 mutex_unlock(&dev->struct_mutex);
3747 return -EINVAL;
3748 }
3749 obj_priv->user_pin_count--;
3750 if (obj_priv->user_pin_count == 0) {
3751 obj_priv->pin_filp = NULL;
3752 i915_gem_object_unpin(obj);
3753 }
Eric Anholt673a3942008-07-30 12:06:12 -07003754
3755 drm_gem_object_unreference(obj);
3756 mutex_unlock(&dev->struct_mutex);
3757 return 0;
3758}
3759
3760int
3761i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3762 struct drm_file *file_priv)
3763{
3764 struct drm_i915_gem_busy *args = data;
3765 struct drm_gem_object *obj;
3766 struct drm_i915_gem_object *obj_priv;
3767
Eric Anholt673a3942008-07-30 12:06:12 -07003768 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3769 if (obj == NULL) {
3770 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3771 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003772 return -EBADF;
3773 }
3774
Chris Wilsonb1ce7862009-06-06 09:46:00 +01003775 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08003776 /* Update the active list for the hardware's current position.
3777 * Otherwise this only updates on a delayed timer or when irqs are
3778 * actually unmasked, and our working set ends up being larger than
3779 * required.
3780 */
3781 i915_gem_retire_requests(dev);
3782
Eric Anholt673a3942008-07-30 12:06:12 -07003783 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08003784 /* Don't count being on the flushing list against the object being
3785 * done. Otherwise, a buffer left on the flushing list but not getting
3786 * flushed (because nobody's flushing that domain) won't ever return
3787 * unbusy and get reused by libdrm's bo cache. The other expected
3788 * consumer of this interface, OpenGL's occlusion queries, also specs
3789 * that the objects get unbusy "eventually" without any interference.
3790 */
3791 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003792
3793 drm_gem_object_unreference(obj);
3794 mutex_unlock(&dev->struct_mutex);
3795 return 0;
3796}
3797
3798int
3799i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3800 struct drm_file *file_priv)
3801{
3802 return i915_gem_ring_throttle(dev, file_priv);
3803}
3804
3805int i915_gem_init_object(struct drm_gem_object *obj)
3806{
3807 struct drm_i915_gem_object *obj_priv;
3808
Eric Anholt9a298b22009-03-24 12:23:04 -07003809 obj_priv = kzalloc(sizeof(*obj_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07003810 if (obj_priv == NULL)
3811 return -ENOMEM;
3812
3813 /*
3814 * We've just allocated pages from the kernel,
3815 * so they've just been written by the CPU with
3816 * zeros. They'll need to be clflushed before we
3817 * use them with the GPU.
3818 */
3819 obj->write_domain = I915_GEM_DOMAIN_CPU;
3820 obj->read_domains = I915_GEM_DOMAIN_CPU;
3821
Keith Packardba1eb1d2008-10-14 19:55:10 -07003822 obj_priv->agp_type = AGP_USER_MEMORY;
3823
Eric Anholt673a3942008-07-30 12:06:12 -07003824 obj->driver_private = obj_priv;
3825 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003826 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07003827 INIT_LIST_HEAD(&obj_priv->list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003828 INIT_LIST_HEAD(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003829
Eric Anholt673a3942008-07-30 12:06:12 -07003830 return 0;
3831}
3832
3833void i915_gem_free_object(struct drm_gem_object *obj)
3834{
Jesse Barnesde151cf2008-11-12 10:03:55 -08003835 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003836 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3837
3838 while (obj_priv->pin_count > 0)
3839 i915_gem_object_unpin(obj);
3840
Dave Airlie71acb5e2008-12-30 20:31:46 +10003841 if (obj_priv->phys_obj)
3842 i915_gem_detach_phys_object(dev, obj);
3843
Eric Anholt673a3942008-07-30 12:06:12 -07003844 i915_gem_object_unbind(obj);
3845
Jesse Barnesab00b3e2009-02-11 14:01:46 -08003846 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003847
Eric Anholt9a298b22009-03-24 12:23:04 -07003848 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07003849 kfree(obj_priv->bit_17);
Eric Anholt9a298b22009-03-24 12:23:04 -07003850 kfree(obj->driver_private);
Eric Anholt673a3942008-07-30 12:06:12 -07003851}
3852
Eric Anholt673a3942008-07-30 12:06:12 -07003853/** Unbinds all objects that are on the given buffer list. */
3854static int
3855i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3856{
3857 struct drm_gem_object *obj;
3858 struct drm_i915_gem_object *obj_priv;
3859 int ret;
3860
3861 while (!list_empty(head)) {
3862 obj_priv = list_first_entry(head,
3863 struct drm_i915_gem_object,
3864 list);
3865 obj = obj_priv->obj;
3866
3867 if (obj_priv->pin_count != 0) {
3868 DRM_ERROR("Pinned object in unbind list\n");
3869 mutex_unlock(&dev->struct_mutex);
3870 return -EINVAL;
3871 }
3872
3873 ret = i915_gem_object_unbind(obj);
3874 if (ret != 0) {
3875 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3876 ret);
3877 mutex_unlock(&dev->struct_mutex);
3878 return ret;
3879 }
3880 }
3881
3882
3883 return 0;
3884}
3885
Jesse Barnes5669fca2009-02-17 15:13:31 -08003886int
Eric Anholt673a3942008-07-30 12:06:12 -07003887i915_gem_idle(struct drm_device *dev)
3888{
3889 drm_i915_private_t *dev_priv = dev->dev_private;
3890 uint32_t seqno, cur_seqno, last_seqno;
3891 int stuck, ret;
3892
Keith Packard6dbe2772008-10-14 21:41:13 -07003893 mutex_lock(&dev->struct_mutex);
3894
3895 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3896 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003897 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003898 }
Eric Anholt673a3942008-07-30 12:06:12 -07003899
3900 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3901 * We need to replace this with a semaphore, or something.
3902 */
3903 dev_priv->mm.suspended = 1;
3904
Keith Packard6dbe2772008-10-14 21:41:13 -07003905 /* Cancel the retire work handler, wait for it to finish if running
3906 */
3907 mutex_unlock(&dev->struct_mutex);
3908 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3909 mutex_lock(&dev->struct_mutex);
3910
Eric Anholt673a3942008-07-30 12:06:12 -07003911 i915_kernel_lost_context(dev);
3912
3913 /* Flush the GPU along with all non-CPU write domains
3914 */
Chris Wilson21d509e2009-06-06 09:46:02 +01003915 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
3916 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07003917
3918 if (seqno == 0) {
3919 mutex_unlock(&dev->struct_mutex);
3920 return -ENOMEM;
3921 }
3922
3923 dev_priv->mm.waiting_gem_seqno = seqno;
3924 last_seqno = 0;
3925 stuck = 0;
3926 for (;;) {
3927 cur_seqno = i915_get_gem_seqno(dev);
3928 if (i915_seqno_passed(cur_seqno, seqno))
3929 break;
3930 if (last_seqno == cur_seqno) {
3931 if (stuck++ > 100) {
3932 DRM_ERROR("hardware wedged\n");
3933 dev_priv->mm.wedged = 1;
3934 DRM_WAKEUP(&dev_priv->irq_queue);
3935 break;
3936 }
3937 }
3938 msleep(10);
3939 last_seqno = cur_seqno;
3940 }
3941 dev_priv->mm.waiting_gem_seqno = 0;
3942
3943 i915_gem_retire_requests(dev);
3944
Carl Worth5e118f42009-03-20 11:54:25 -07003945 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003946 if (!dev_priv->mm.wedged) {
3947 /* Active and flushing should now be empty as we've
3948 * waited for a sequence higher than any pending execbuffer
3949 */
3950 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3951 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3952 /* Request should now be empty as we've also waited
3953 * for the last request in the list
3954 */
3955 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3956 }
Eric Anholt673a3942008-07-30 12:06:12 -07003957
Eric Anholt28dfe522008-11-13 15:00:55 -08003958 /* Empty the active and flushing lists to inactive. If there's
3959 * anything left at this point, it means that we're wedged and
3960 * nothing good's going to happen by leaving them there. So strip
3961 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003962 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003963 while (!list_empty(&dev_priv->mm.active_list)) {
3964 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003965
Eric Anholt28dfe522008-11-13 15:00:55 -08003966 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3967 struct drm_i915_gem_object,
3968 list);
3969 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3970 i915_gem_object_move_to_inactive(obj_priv->obj);
3971 }
Carl Worth5e118f42009-03-20 11:54:25 -07003972 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003973
3974 while (!list_empty(&dev_priv->mm.flushing_list)) {
3975 struct drm_i915_gem_object *obj_priv;
3976
Eric Anholt151903d2008-12-01 10:23:21 +10003977 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003978 struct drm_i915_gem_object,
3979 list);
3980 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3981 i915_gem_object_move_to_inactive(obj_priv->obj);
3982 }
3983
3984
3985 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003986 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003987 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003988 if (ret) {
3989 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003990 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003991 }
Eric Anholt673a3942008-07-30 12:06:12 -07003992
Keith Packard6dbe2772008-10-14 21:41:13 -07003993 i915_gem_cleanup_ringbuffer(dev);
3994 mutex_unlock(&dev->struct_mutex);
3995
Eric Anholt673a3942008-07-30 12:06:12 -07003996 return 0;
3997}
3998
3999static int
4000i915_gem_init_hws(struct drm_device *dev)
4001{
4002 drm_i915_private_t *dev_priv = dev->dev_private;
4003 struct drm_gem_object *obj;
4004 struct drm_i915_gem_object *obj_priv;
4005 int ret;
4006
4007 /* If we need a physical address for the status page, it's already
4008 * initialized at driver load time.
4009 */
4010 if (!I915_NEED_GFX_HWS(dev))
4011 return 0;
4012
4013 obj = drm_gem_object_alloc(dev, 4096);
4014 if (obj == NULL) {
4015 DRM_ERROR("Failed to allocate status page\n");
4016 return -ENOMEM;
4017 }
4018 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07004019 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07004020
4021 ret = i915_gem_object_pin(obj, 4096);
4022 if (ret != 0) {
4023 drm_gem_object_unreference(obj);
4024 return ret;
4025 }
4026
4027 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07004028
Eric Anholt856fa192009-03-19 14:10:50 -07004029 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004030 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004031 DRM_ERROR("Failed to map status page.\n");
4032 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00004033 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004034 drm_gem_object_unreference(obj);
4035 return -EINVAL;
4036 }
4037 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004038 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
4039 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004040 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07004041 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
4042
4043 return 0;
4044}
4045
Chris Wilson85a7bb92009-02-11 14:52:44 +00004046static void
4047i915_gem_cleanup_hws(struct drm_device *dev)
4048{
4049 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004050 struct drm_gem_object *obj;
4051 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00004052
4053 if (dev_priv->hws_obj == NULL)
4054 return;
4055
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004056 obj = dev_priv->hws_obj;
4057 obj_priv = obj->driver_private;
4058
Eric Anholt856fa192009-03-19 14:10:50 -07004059 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004060 i915_gem_object_unpin(obj);
4061 drm_gem_object_unreference(obj);
4062 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004063
Chris Wilson85a7bb92009-02-11 14:52:44 +00004064 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4065 dev_priv->hw_status_page = NULL;
4066
4067 /* Write high address into HWS_PGA when disabling. */
4068 I915_WRITE(HWS_PGA, 0x1ffff000);
4069}
4070
Jesse Barnes79e53942008-11-07 14:24:08 -08004071int
Eric Anholt673a3942008-07-30 12:06:12 -07004072i915_gem_init_ringbuffer(struct drm_device *dev)
4073{
4074 drm_i915_private_t *dev_priv = dev->dev_private;
4075 struct drm_gem_object *obj;
4076 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08004077 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07004078 int ret;
Keith Packard50aa2532008-10-14 17:20:35 -07004079 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07004080
4081 ret = i915_gem_init_hws(dev);
4082 if (ret != 0)
4083 return ret;
4084
4085 obj = drm_gem_object_alloc(dev, 128 * 1024);
4086 if (obj == NULL) {
4087 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004088 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004089 return -ENOMEM;
4090 }
4091 obj_priv = obj->driver_private;
4092
4093 ret = i915_gem_object_pin(obj, 4096);
4094 if (ret != 0) {
4095 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004096 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004097 return ret;
4098 }
4099
4100 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004101 ring->Size = obj->size;
4102 ring->tail_mask = obj->size - 1;
Eric Anholt673a3942008-07-30 12:06:12 -07004103
Jesse Barnes79e53942008-11-07 14:24:08 -08004104 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4105 ring->map.size = obj->size;
4106 ring->map.type = 0;
4107 ring->map.flags = 0;
4108 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004109
Jesse Barnes79e53942008-11-07 14:24:08 -08004110 drm_core_ioremap_wc(&ring->map, dev);
4111 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004112 DRM_ERROR("Failed to map ringbuffer.\n");
4113 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004114 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004115 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004116 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004117 return -EINVAL;
4118 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004119 ring->ring_obj = obj;
4120 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004121
4122 /* Stop the ring if it's running. */
4123 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004124 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa2532008-10-14 17:20:35 -07004125 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004126
4127 /* Initialize the ring. */
4128 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa2532008-10-14 17:20:35 -07004129 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4130
4131 /* G45 ring initialization fails to reset head to zero */
4132 if (head != 0) {
4133 DRM_ERROR("Ring head not reset to zero "
4134 "ctl %08x head %08x tail %08x start %08x\n",
4135 I915_READ(PRB0_CTL),
4136 I915_READ(PRB0_HEAD),
4137 I915_READ(PRB0_TAIL),
4138 I915_READ(PRB0_START));
4139 I915_WRITE(PRB0_HEAD, 0);
4140
4141 DRM_ERROR("Ring head forced to zero "
4142 "ctl %08x head %08x tail %08x start %08x\n",
4143 I915_READ(PRB0_CTL),
4144 I915_READ(PRB0_HEAD),
4145 I915_READ(PRB0_TAIL),
4146 I915_READ(PRB0_START));
4147 }
4148
Eric Anholt673a3942008-07-30 12:06:12 -07004149 I915_WRITE(PRB0_CTL,
4150 ((obj->size - 4096) & RING_NR_PAGES) |
4151 RING_NO_REPORT |
4152 RING_VALID);
4153
Keith Packard50aa2532008-10-14 17:20:35 -07004154 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4155
4156 /* If the head is still not zero, the ring is dead */
4157 if (head != 0) {
4158 DRM_ERROR("Ring initialization failed "
4159 "ctl %08x head %08x tail %08x start %08x\n",
4160 I915_READ(PRB0_CTL),
4161 I915_READ(PRB0_HEAD),
4162 I915_READ(PRB0_TAIL),
4163 I915_READ(PRB0_START));
4164 return -EIO;
4165 }
4166
Eric Anholt673a3942008-07-30 12:06:12 -07004167 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004168 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4169 i915_kernel_lost_context(dev);
4170 else {
4171 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4172 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4173 ring->space = ring->head - (ring->tail + 8);
4174 if (ring->space < 0)
4175 ring->space += ring->Size;
4176 }
Eric Anholt673a3942008-07-30 12:06:12 -07004177
4178 return 0;
4179}
4180
Jesse Barnes79e53942008-11-07 14:24:08 -08004181void
Eric Anholt673a3942008-07-30 12:06:12 -07004182i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4183{
4184 drm_i915_private_t *dev_priv = dev->dev_private;
4185
4186 if (dev_priv->ring.ring_obj == NULL)
4187 return;
4188
4189 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4190
4191 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4192 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4193 dev_priv->ring.ring_obj = NULL;
4194 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4195
Chris Wilson85a7bb92009-02-11 14:52:44 +00004196 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004197}
4198
4199int
4200i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4201 struct drm_file *file_priv)
4202{
4203 drm_i915_private_t *dev_priv = dev->dev_private;
4204 int ret;
4205
Jesse Barnes79e53942008-11-07 14:24:08 -08004206 if (drm_core_check_feature(dev, DRIVER_MODESET))
4207 return 0;
4208
Eric Anholt673a3942008-07-30 12:06:12 -07004209 if (dev_priv->mm.wedged) {
4210 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4211 dev_priv->mm.wedged = 0;
4212 }
4213
Eric Anholt673a3942008-07-30 12:06:12 -07004214 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004215 dev_priv->mm.suspended = 0;
4216
4217 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004218 if (ret != 0) {
4219 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004220 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004221 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004222
Carl Worth5e118f42009-03-20 11:54:25 -07004223 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004224 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004225 spin_unlock(&dev_priv->mm.active_list_lock);
4226
Eric Anholt673a3942008-07-30 12:06:12 -07004227 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4228 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4229 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004230 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004231
4232 drm_irq_install(dev);
4233
Eric Anholt673a3942008-07-30 12:06:12 -07004234 return 0;
4235}
4236
4237int
4238i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4239 struct drm_file *file_priv)
4240{
4241 int ret;
4242
Jesse Barnes79e53942008-11-07 14:24:08 -08004243 if (drm_core_check_feature(dev, DRIVER_MODESET))
4244 return 0;
4245
Eric Anholt673a3942008-07-30 12:06:12 -07004246 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004247 drm_irq_uninstall(dev);
4248
Keith Packard6dbe2772008-10-14 21:41:13 -07004249 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004250}
4251
4252void
4253i915_gem_lastclose(struct drm_device *dev)
4254{
4255 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004256
Eric Anholte806b492009-01-22 09:56:58 -08004257 if (drm_core_check_feature(dev, DRIVER_MODESET))
4258 return;
4259
Keith Packard6dbe2772008-10-14 21:41:13 -07004260 ret = i915_gem_idle(dev);
4261 if (ret)
4262 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004263}
4264
4265void
4266i915_gem_load(struct drm_device *dev)
4267{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004268 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004269 drm_i915_private_t *dev_priv = dev->dev_private;
4270
Carl Worth5e118f42009-03-20 11:54:25 -07004271 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004272 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4273 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4274 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4275 INIT_LIST_HEAD(&dev_priv->mm.request_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004276 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004277 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4278 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004279 dev_priv->mm.next_gem_seqno = 1;
4280
Jesse Barnesde151cf2008-11-12 10:03:55 -08004281 /* Old X drivers will take 0-2 for front, back, depth buffers */
4282 dev_priv->fence_reg_start = 3;
4283
Jesse Barnes0f973f22009-01-26 17:10:45 -08004284 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004285 dev_priv->num_fence_regs = 16;
4286 else
4287 dev_priv->num_fence_regs = 8;
4288
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004289 /* Initialize fence registers to zero */
4290 if (IS_I965G(dev)) {
4291 for (i = 0; i < 16; i++)
4292 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4293 } else {
4294 for (i = 0; i < 8; i++)
4295 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4296 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4297 for (i = 0; i < 8; i++)
4298 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4299 }
4300
Eric Anholt673a3942008-07-30 12:06:12 -07004301 i915_gem_detect_bit_6_swizzle(dev);
4302}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004303
4304/*
4305 * Create a physically contiguous memory object for this object
4306 * e.g. for cursor + overlay regs
4307 */
4308int i915_gem_init_phys_object(struct drm_device *dev,
4309 int id, int size)
4310{
4311 drm_i915_private_t *dev_priv = dev->dev_private;
4312 struct drm_i915_gem_phys_object *phys_obj;
4313 int ret;
4314
4315 if (dev_priv->mm.phys_objs[id - 1] || !size)
4316 return 0;
4317
Eric Anholt9a298b22009-03-24 12:23:04 -07004318 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004319 if (!phys_obj)
4320 return -ENOMEM;
4321
4322 phys_obj->id = id;
4323
4324 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4325 if (!phys_obj->handle) {
4326 ret = -ENOMEM;
4327 goto kfree_obj;
4328 }
4329#ifdef CONFIG_X86
4330 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4331#endif
4332
4333 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4334
4335 return 0;
4336kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004337 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004338 return ret;
4339}
4340
4341void i915_gem_free_phys_object(struct drm_device *dev, int id)
4342{
4343 drm_i915_private_t *dev_priv = dev->dev_private;
4344 struct drm_i915_gem_phys_object *phys_obj;
4345
4346 if (!dev_priv->mm.phys_objs[id - 1])
4347 return;
4348
4349 phys_obj = dev_priv->mm.phys_objs[id - 1];
4350 if (phys_obj->cur_obj) {
4351 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4352 }
4353
4354#ifdef CONFIG_X86
4355 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4356#endif
4357 drm_pci_free(dev, phys_obj->handle);
4358 kfree(phys_obj);
4359 dev_priv->mm.phys_objs[id - 1] = NULL;
4360}
4361
4362void i915_gem_free_all_phys_object(struct drm_device *dev)
4363{
4364 int i;
4365
Dave Airlie260883c2009-01-22 17:58:49 +10004366 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004367 i915_gem_free_phys_object(dev, i);
4368}
4369
4370void i915_gem_detach_phys_object(struct drm_device *dev,
4371 struct drm_gem_object *obj)
4372{
4373 struct drm_i915_gem_object *obj_priv;
4374 int i;
4375 int ret;
4376 int page_count;
4377
4378 obj_priv = obj->driver_private;
4379 if (!obj_priv->phys_obj)
4380 return;
4381
Eric Anholt856fa192009-03-19 14:10:50 -07004382 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004383 if (ret)
4384 goto out;
4385
4386 page_count = obj->size / PAGE_SIZE;
4387
4388 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004389 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004390 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4391
4392 memcpy(dst, src, PAGE_SIZE);
4393 kunmap_atomic(dst, KM_USER0);
4394 }
Eric Anholt856fa192009-03-19 14:10:50 -07004395 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004396 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004397
4398 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004399out:
4400 obj_priv->phys_obj->cur_obj = NULL;
4401 obj_priv->phys_obj = NULL;
4402}
4403
4404int
4405i915_gem_attach_phys_object(struct drm_device *dev,
4406 struct drm_gem_object *obj, int id)
4407{
4408 drm_i915_private_t *dev_priv = dev->dev_private;
4409 struct drm_i915_gem_object *obj_priv;
4410 int ret = 0;
4411 int page_count;
4412 int i;
4413
4414 if (id > I915_MAX_PHYS_OBJECT)
4415 return -EINVAL;
4416
4417 obj_priv = obj->driver_private;
4418
4419 if (obj_priv->phys_obj) {
4420 if (obj_priv->phys_obj->id == id)
4421 return 0;
4422 i915_gem_detach_phys_object(dev, obj);
4423 }
4424
4425
4426 /* create a new object */
4427 if (!dev_priv->mm.phys_objs[id - 1]) {
4428 ret = i915_gem_init_phys_object(dev, id,
4429 obj->size);
4430 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004431 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004432 goto out;
4433 }
4434 }
4435
4436 /* bind to the object */
4437 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4438 obj_priv->phys_obj->cur_obj = obj;
4439
Eric Anholt856fa192009-03-19 14:10:50 -07004440 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004441 if (ret) {
4442 DRM_ERROR("failed to get page list\n");
4443 goto out;
4444 }
4445
4446 page_count = obj->size / PAGE_SIZE;
4447
4448 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004449 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004450 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4451
4452 memcpy(dst, src, PAGE_SIZE);
4453 kunmap_atomic(src, KM_USER0);
4454 }
4455
Chris Wilsond78b47b2009-06-17 21:52:49 +01004456 i915_gem_object_put_pages(obj);
4457
Dave Airlie71acb5e2008-12-30 20:31:46 +10004458 return 0;
4459out:
4460 return ret;
4461}
4462
4463static int
4464i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4465 struct drm_i915_gem_pwrite *args,
4466 struct drm_file *file_priv)
4467{
4468 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4469 void *obj_addr;
4470 int ret;
4471 char __user *user_data;
4472
4473 user_data = (char __user *) (uintptr_t) args->data_ptr;
4474 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4475
Dave Airliee08fb4f2009-02-25 14:52:30 +10004476 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004477 ret = copy_from_user(obj_addr, user_data, args->size);
4478 if (ret)
4479 return -EFAULT;
4480
4481 drm_agp_chipset_flush(dev);
4482 return 0;
4483}
Eric Anholtb9624422009-06-03 07:27:35 +00004484
4485void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4486{
4487 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4488
4489 /* Clean up our request list when the client is going away, so that
4490 * later retire_requests won't dereference our soon-to-be-gone
4491 * file_priv.
4492 */
4493 mutex_lock(&dev->struct_mutex);
4494 while (!list_empty(&i915_file_priv->mm.request_list))
4495 list_del_init(i915_file_priv->mm.request_list.next);
4496 mutex_unlock(&dev->struct_mutex);
4497}