blob: 6dca9fc7c1dbd40bb12bad95986d148501af1d49 [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"
32#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080033#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070034
Eric Anholt28dfe522008-11-13 15:00:55 -080035#define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
36
Eric Anholte47c68e2008-11-14 13:35:19 -080037static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
38static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
39static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080040static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
41 int write);
42static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
43 uint64_t offset,
44 uint64_t size);
45static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070046static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080047static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
48 unsigned alignment);
Jesse Barnes0f973f22009-01-26 17:10:45 -080049static int i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write);
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
352 user_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
353 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 }
432 kfree(user_pages);
433
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
652 user_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
653 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]);
722 kfree(user_pages);
723
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
827 user_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
828 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]);
905 kfree(user_pages);
906
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{
982 struct drm_i915_gem_set_domain *args = data;
983 struct drm_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800984 uint32_t read_domains = args->read_domains;
985 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700986 int ret;
987
988 if (!(dev->driver->driver_features & DRIVER_GEM))
989 return -ENODEV;
990
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800991 /* Only handle setting domains to types used by the CPU. */
992 if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
993 return -EINVAL;
994
995 if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
996 return -EINVAL;
997
998 /* Having something in the write domain implies it's in the read
999 * domain, and only that read domain. Enforce that in the request.
1000 */
1001 if (write_domain != 0 && read_domains != write_domain)
1002 return -EINVAL;
1003
Eric Anholt673a3942008-07-30 12:06:12 -07001004 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1005 if (obj == NULL)
1006 return -EBADF;
1007
1008 mutex_lock(&dev->struct_mutex);
1009#if WATCH_BUF
1010 DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001011 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001012#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001013 if (read_domains & I915_GEM_DOMAIN_GTT) {
1014 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001015
1016 /* Silently promote "you're not bound, there was nothing to do"
1017 * to success, since the client was just asking us to
1018 * make sure everything was done.
1019 */
1020 if (ret == -EINVAL)
1021 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001022 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001023 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001024 }
1025
Eric Anholt673a3942008-07-30 12:06:12 -07001026 drm_gem_object_unreference(obj);
1027 mutex_unlock(&dev->struct_mutex);
1028 return ret;
1029}
1030
1031/**
1032 * Called when user space has done writes to this buffer
1033 */
1034int
1035i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1036 struct drm_file *file_priv)
1037{
1038 struct drm_i915_gem_sw_finish *args = data;
1039 struct drm_gem_object *obj;
1040 struct drm_i915_gem_object *obj_priv;
1041 int ret = 0;
1042
1043 if (!(dev->driver->driver_features & DRIVER_GEM))
1044 return -ENODEV;
1045
1046 mutex_lock(&dev->struct_mutex);
1047 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1048 if (obj == NULL) {
1049 mutex_unlock(&dev->struct_mutex);
1050 return -EBADF;
1051 }
1052
1053#if WATCH_BUF
1054 DRM_INFO("%s: sw_finish %d (%p %d)\n",
1055 __func__, args->handle, obj, obj->size);
1056#endif
1057 obj_priv = obj->driver_private;
1058
1059 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001060 if (obj_priv->pin_count)
1061 i915_gem_object_flush_cpu_write_domain(obj);
1062
Eric Anholt673a3942008-07-30 12:06:12 -07001063 drm_gem_object_unreference(obj);
1064 mutex_unlock(&dev->struct_mutex);
1065 return ret;
1066}
1067
1068/**
1069 * Maps the contents of an object, returning the address it is mapped
1070 * into.
1071 *
1072 * While the mapping holds a reference on the contents of the object, it doesn't
1073 * imply a ref on the object itself.
1074 */
1075int
1076i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1077 struct drm_file *file_priv)
1078{
1079 struct drm_i915_gem_mmap *args = data;
1080 struct drm_gem_object *obj;
1081 loff_t offset;
1082 unsigned long addr;
1083
1084 if (!(dev->driver->driver_features & DRIVER_GEM))
1085 return -ENODEV;
1086
1087 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1088 if (obj == NULL)
1089 return -EBADF;
1090
1091 offset = args->offset;
1092
1093 down_write(&current->mm->mmap_sem);
1094 addr = do_mmap(obj->filp, 0, args->size,
1095 PROT_READ | PROT_WRITE, MAP_SHARED,
1096 args->offset);
1097 up_write(&current->mm->mmap_sem);
1098 mutex_lock(&dev->struct_mutex);
1099 drm_gem_object_unreference(obj);
1100 mutex_unlock(&dev->struct_mutex);
1101 if (IS_ERR((void *)addr))
1102 return addr;
1103
1104 args->addr_ptr = (uint64_t) addr;
1105
1106 return 0;
1107}
1108
Jesse Barnesde151cf2008-11-12 10:03:55 -08001109/**
1110 * i915_gem_fault - fault a page into the GTT
1111 * vma: VMA in question
1112 * vmf: fault info
1113 *
1114 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1115 * from userspace. The fault handler takes care of binding the object to
1116 * the GTT (if needed), allocating and programming a fence register (again,
1117 * only if needed based on whether the old reg is still valid or the object
1118 * is tiled) and inserting a new PTE into the faulting process.
1119 *
1120 * Note that the faulting process may involve evicting existing objects
1121 * from the GTT and/or fence registers to make room. So performance may
1122 * suffer if the GTT working set is large or there are few fence registers
1123 * left.
1124 */
1125int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1126{
1127 struct drm_gem_object *obj = vma->vm_private_data;
1128 struct drm_device *dev = obj->dev;
1129 struct drm_i915_private *dev_priv = dev->dev_private;
1130 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1131 pgoff_t page_offset;
1132 unsigned long pfn;
1133 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001134 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001135
1136 /* We don't use vmf->pgoff since that has the fake offset */
1137 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1138 PAGE_SHIFT;
1139
1140 /* Now bind it into the GTT if needed */
1141 mutex_lock(&dev->struct_mutex);
1142 if (!obj_priv->gtt_space) {
1143 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1144 if (ret) {
1145 mutex_unlock(&dev->struct_mutex);
1146 return VM_FAULT_SIGBUS;
1147 }
1148 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
1149 }
1150
1151 /* Need a new fence register? */
1152 if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001153 obj_priv->tiling_mode != I915_TILING_NONE) {
Jesse Barnes0f973f22009-01-26 17:10:45 -08001154 ret = i915_gem_object_get_fence_reg(obj, write);
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001155 if (ret) {
1156 mutex_unlock(&dev->struct_mutex);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001157 return VM_FAULT_SIGBUS;
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001158 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001159 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001160
1161 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1162 page_offset;
1163
1164 /* Finally, remap it using the new GTT offset */
1165 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1166
1167 mutex_unlock(&dev->struct_mutex);
1168
1169 switch (ret) {
1170 case -ENOMEM:
1171 case -EAGAIN:
1172 return VM_FAULT_OOM;
1173 case -EFAULT:
Jesse Barnes959b8872009-03-20 14:16:33 -07001174 case -EINVAL:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001175 return VM_FAULT_SIGBUS;
1176 default:
1177 return VM_FAULT_NOPAGE;
1178 }
1179}
1180
1181/**
1182 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1183 * @obj: obj in question
1184 *
1185 * GEM memory mapping works by handing back to userspace a fake mmap offset
1186 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1187 * up the object based on the offset and sets up the various memory mapping
1188 * structures.
1189 *
1190 * This routine allocates and attaches a fake offset for @obj.
1191 */
1192static int
1193i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1194{
1195 struct drm_device *dev = obj->dev;
1196 struct drm_gem_mm *mm = dev->mm_private;
1197 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1198 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001199 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001200 int ret = 0;
1201
1202 /* Set the object up for mmap'ing */
1203 list = &obj->map_list;
1204 list->map = drm_calloc(1, sizeof(struct drm_map_list),
1205 DRM_MEM_DRIVER);
1206 if (!list->map)
1207 return -ENOMEM;
1208
1209 map = list->map;
1210 map->type = _DRM_GEM;
1211 map->size = obj->size;
1212 map->handle = obj;
1213
1214 /* Get a DRM GEM mmap offset allocated... */
1215 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1216 obj->size / PAGE_SIZE, 0, 0);
1217 if (!list->file_offset_node) {
1218 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1219 ret = -ENOMEM;
1220 goto out_free_list;
1221 }
1222
1223 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1224 obj->size / PAGE_SIZE, 0);
1225 if (!list->file_offset_node) {
1226 ret = -ENOMEM;
1227 goto out_free_list;
1228 }
1229
1230 list->hash.key = list->file_offset_node->start;
1231 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1232 DRM_ERROR("failed to add to map hash\n");
1233 goto out_free_mm;
1234 }
1235
1236 /* By now we should be all set, any drm_mmap request on the offset
1237 * below will get to our mmap & fault handler */
1238 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1239
1240 return 0;
1241
1242out_free_mm:
1243 drm_mm_put_block(list->file_offset_node);
1244out_free_list:
1245 drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
1246
1247 return ret;
1248}
1249
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001250static void
1251i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1252{
1253 struct drm_device *dev = obj->dev;
1254 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1255 struct drm_gem_mm *mm = dev->mm_private;
1256 struct drm_map_list *list;
1257
1258 list = &obj->map_list;
1259 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1260
1261 if (list->file_offset_node) {
1262 drm_mm_put_block(list->file_offset_node);
1263 list->file_offset_node = NULL;
1264 }
1265
1266 if (list->map) {
1267 drm_free(list->map, sizeof(struct drm_map), DRM_MEM_DRIVER);
1268 list->map = NULL;
1269 }
1270
1271 obj_priv->mmap_offset = 0;
1272}
1273
Jesse Barnesde151cf2008-11-12 10:03:55 -08001274/**
1275 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1276 * @obj: object to check
1277 *
1278 * Return the required GTT alignment for an object, taking into account
1279 * potential fence register mapping if needed.
1280 */
1281static uint32_t
1282i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1283{
1284 struct drm_device *dev = obj->dev;
1285 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1286 int start, i;
1287
1288 /*
1289 * Minimum alignment is 4k (GTT page size), but might be greater
1290 * if a fence register is needed for the object.
1291 */
1292 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1293 return 4096;
1294
1295 /*
1296 * Previous chips need to be aligned to the size of the smallest
1297 * fence register that can contain the object.
1298 */
1299 if (IS_I9XX(dev))
1300 start = 1024*1024;
1301 else
1302 start = 512*1024;
1303
1304 for (i = start; i < obj->size; i <<= 1)
1305 ;
1306
1307 return i;
1308}
1309
1310/**
1311 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1312 * @dev: DRM device
1313 * @data: GTT mapping ioctl data
1314 * @file_priv: GEM object info
1315 *
1316 * Simply returns the fake offset to userspace so it can mmap it.
1317 * The mmap call will end up in drm_gem_mmap(), which will set things
1318 * up so we can get faults in the handler above.
1319 *
1320 * The fault handler will take care of binding the object into the GTT
1321 * (since it may have been evicted to make room for something), allocating
1322 * a fence register, and mapping the appropriate aperture address into
1323 * userspace.
1324 */
1325int
1326i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1327 struct drm_file *file_priv)
1328{
1329 struct drm_i915_gem_mmap_gtt *args = data;
1330 struct drm_i915_private *dev_priv = dev->dev_private;
1331 struct drm_gem_object *obj;
1332 struct drm_i915_gem_object *obj_priv;
1333 int ret;
1334
1335 if (!(dev->driver->driver_features & DRIVER_GEM))
1336 return -ENODEV;
1337
1338 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1339 if (obj == NULL)
1340 return -EBADF;
1341
1342 mutex_lock(&dev->struct_mutex);
1343
1344 obj_priv = obj->driver_private;
1345
1346 if (!obj_priv->mmap_offset) {
1347 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001348 if (ret) {
1349 drm_gem_object_unreference(obj);
1350 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001351 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001352 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001353 }
1354
1355 args->offset = obj_priv->mmap_offset;
1356
1357 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
1358
1359 /* Make sure the alignment is correct for fence regs etc */
1360 if (obj_priv->agp_mem &&
1361 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
1362 drm_gem_object_unreference(obj);
1363 mutex_unlock(&dev->struct_mutex);
1364 return -EINVAL;
1365 }
1366
1367 /*
1368 * Pull it into the GTT so that we have a page list (makes the
1369 * initial fault faster and any subsequent flushing possible).
1370 */
1371 if (!obj_priv->agp_mem) {
1372 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1373 if (ret) {
1374 drm_gem_object_unreference(obj);
1375 mutex_unlock(&dev->struct_mutex);
1376 return ret;
1377 }
1378 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
1379 }
1380
1381 drm_gem_object_unreference(obj);
1382 mutex_unlock(&dev->struct_mutex);
1383
1384 return 0;
1385}
1386
Ben Gamari6911a9b2009-04-02 11:24:54 -07001387void
Eric Anholt856fa192009-03-19 14:10:50 -07001388i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001389{
1390 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1391 int page_count = obj->size / PAGE_SIZE;
1392 int i;
1393
Eric Anholt856fa192009-03-19 14:10:50 -07001394 BUG_ON(obj_priv->pages_refcount == 0);
1395
1396 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001397 return;
1398
Eric Anholt280b7132009-03-12 16:56:27 -07001399 if (obj_priv->tiling_mode != I915_TILING_NONE)
1400 i915_gem_object_save_bit_17_swizzle(obj);
1401
Eric Anholt673a3942008-07-30 12:06:12 -07001402 for (i = 0; i < page_count; i++)
Eric Anholt856fa192009-03-19 14:10:50 -07001403 if (obj_priv->pages[i] != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07001404 if (obj_priv->dirty)
Eric Anholt856fa192009-03-19 14:10:50 -07001405 set_page_dirty(obj_priv->pages[i]);
1406 mark_page_accessed(obj_priv->pages[i]);
1407 page_cache_release(obj_priv->pages[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07001408 }
1409 obj_priv->dirty = 0;
1410
Eric Anholt856fa192009-03-19 14:10:50 -07001411 drm_free(obj_priv->pages,
Eric Anholt673a3942008-07-30 12:06:12 -07001412 page_count * sizeof(struct page *),
1413 DRM_MEM_DRIVER);
Eric Anholt856fa192009-03-19 14:10:50 -07001414 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001415}
1416
1417static void
Eric Anholtce44b0e2008-11-06 16:00:31 -08001418i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001419{
1420 struct drm_device *dev = obj->dev;
1421 drm_i915_private_t *dev_priv = dev->dev_private;
1422 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1423
1424 /* Add a reference if we're newly entering the active list. */
1425 if (!obj_priv->active) {
1426 drm_gem_object_reference(obj);
1427 obj_priv->active = 1;
1428 }
1429 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001430 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001431 list_move_tail(&obj_priv->list,
1432 &dev_priv->mm.active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001433 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001434 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001435}
1436
Eric Anholtce44b0e2008-11-06 16:00:31 -08001437static void
1438i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1439{
1440 struct drm_device *dev = obj->dev;
1441 drm_i915_private_t *dev_priv = dev->dev_private;
1442 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1443
1444 BUG_ON(!obj_priv->active);
1445 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1446 obj_priv->last_rendering_seqno = 0;
1447}
Eric Anholt673a3942008-07-30 12:06:12 -07001448
1449static void
1450i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1451{
1452 struct drm_device *dev = obj->dev;
1453 drm_i915_private_t *dev_priv = dev->dev_private;
1454 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1455
1456 i915_verify_inactive(dev, __FILE__, __LINE__);
1457 if (obj_priv->pin_count != 0)
1458 list_del_init(&obj_priv->list);
1459 else
1460 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1461
Eric Anholtce44b0e2008-11-06 16:00:31 -08001462 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001463 if (obj_priv->active) {
1464 obj_priv->active = 0;
1465 drm_gem_object_unreference(obj);
1466 }
1467 i915_verify_inactive(dev, __FILE__, __LINE__);
1468}
1469
1470/**
1471 * Creates a new sequence number, emitting a write of it to the status page
1472 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1473 *
1474 * Must be called with struct_lock held.
1475 *
1476 * Returned sequence numbers are nonzero on success.
1477 */
1478static uint32_t
1479i915_add_request(struct drm_device *dev, uint32_t flush_domains)
1480{
1481 drm_i915_private_t *dev_priv = dev->dev_private;
1482 struct drm_i915_gem_request *request;
1483 uint32_t seqno;
1484 int was_empty;
1485 RING_LOCALS;
1486
1487 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
1488 if (request == NULL)
1489 return 0;
1490
1491 /* Grab the seqno we're going to make this request be, and bump the
1492 * next (skipping 0 so it can be the reserved no-seqno value).
1493 */
1494 seqno = dev_priv->mm.next_gem_seqno;
1495 dev_priv->mm.next_gem_seqno++;
1496 if (dev_priv->mm.next_gem_seqno == 0)
1497 dev_priv->mm.next_gem_seqno++;
1498
1499 BEGIN_LP_RING(4);
1500 OUT_RING(MI_STORE_DWORD_INDEX);
1501 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1502 OUT_RING(seqno);
1503
1504 OUT_RING(MI_USER_INTERRUPT);
1505 ADVANCE_LP_RING();
1506
1507 DRM_DEBUG("%d\n", seqno);
1508
1509 request->seqno = seqno;
1510 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -07001511 was_empty = list_empty(&dev_priv->mm.request_list);
1512 list_add_tail(&request->list, &dev_priv->mm.request_list);
1513
Eric Anholtce44b0e2008-11-06 16:00:31 -08001514 /* Associate any objects on the flushing list matching the write
1515 * domain we're flushing with our flush.
1516 */
1517 if (flush_domains != 0) {
1518 struct drm_i915_gem_object *obj_priv, *next;
1519
1520 list_for_each_entry_safe(obj_priv, next,
1521 &dev_priv->mm.flushing_list, list) {
1522 struct drm_gem_object *obj = obj_priv->obj;
1523
1524 if ((obj->write_domain & flush_domains) ==
1525 obj->write_domain) {
1526 obj->write_domain = 0;
1527 i915_gem_object_move_to_active(obj, seqno);
1528 }
1529 }
1530
1531 }
1532
Keith Packard6dbe2772008-10-14 21:41:13 -07001533 if (was_empty && !dev_priv->mm.suspended)
Eric Anholt673a3942008-07-30 12:06:12 -07001534 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1535 return seqno;
1536}
1537
1538/**
1539 * Command execution barrier
1540 *
1541 * Ensures that all commands in the ring are finished
1542 * before signalling the CPU
1543 */
Eric Anholt3043c602008-10-02 12:24:47 -07001544static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -07001545i915_retire_commands(struct drm_device *dev)
1546{
1547 drm_i915_private_t *dev_priv = dev->dev_private;
1548 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1549 uint32_t flush_domains = 0;
1550 RING_LOCALS;
1551
1552 /* The sampler always gets flushed on i965 (sigh) */
1553 if (IS_I965G(dev))
1554 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1555 BEGIN_LP_RING(2);
1556 OUT_RING(cmd);
1557 OUT_RING(0); /* noop */
1558 ADVANCE_LP_RING();
1559 return flush_domains;
1560}
1561
1562/**
1563 * Moves buffers associated only with the given active seqno from the active
1564 * to inactive list, potentially freeing them.
1565 */
1566static void
1567i915_gem_retire_request(struct drm_device *dev,
1568 struct drm_i915_gem_request *request)
1569{
1570 drm_i915_private_t *dev_priv = dev->dev_private;
1571
1572 /* Move any buffers on the active list that are no longer referenced
1573 * by the ringbuffer to the flushing/inactive lists as appropriate.
1574 */
Carl Worth5e118f42009-03-20 11:54:25 -07001575 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001576 while (!list_empty(&dev_priv->mm.active_list)) {
1577 struct drm_gem_object *obj;
1578 struct drm_i915_gem_object *obj_priv;
1579
1580 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1581 struct drm_i915_gem_object,
1582 list);
1583 obj = obj_priv->obj;
1584
1585 /* If the seqno being retired doesn't match the oldest in the
1586 * list, then the oldest in the list must still be newer than
1587 * this seqno.
1588 */
1589 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001590 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001591
Eric Anholt673a3942008-07-30 12:06:12 -07001592#if WATCH_LRU
1593 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1594 __func__, request->seqno, obj);
1595#endif
1596
Eric Anholtce44b0e2008-11-06 16:00:31 -08001597 if (obj->write_domain != 0)
1598 i915_gem_object_move_to_flushing(obj);
1599 else
Eric Anholt673a3942008-07-30 12:06:12 -07001600 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001601 }
Carl Worth5e118f42009-03-20 11:54:25 -07001602out:
1603 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001604}
1605
1606/**
1607 * Returns true if seq1 is later than seq2.
1608 */
1609static int
1610i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1611{
1612 return (int32_t)(seq1 - seq2) >= 0;
1613}
1614
1615uint32_t
1616i915_get_gem_seqno(struct drm_device *dev)
1617{
1618 drm_i915_private_t *dev_priv = dev->dev_private;
1619
1620 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1621}
1622
1623/**
1624 * This function clears the request list as sequence numbers are passed.
1625 */
1626void
1627i915_gem_retire_requests(struct drm_device *dev)
1628{
1629 drm_i915_private_t *dev_priv = dev->dev_private;
1630 uint32_t seqno;
1631
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001632 if (!dev_priv->hw_status_page)
1633 return;
1634
Eric Anholt673a3942008-07-30 12:06:12 -07001635 seqno = i915_get_gem_seqno(dev);
1636
1637 while (!list_empty(&dev_priv->mm.request_list)) {
1638 struct drm_i915_gem_request *request;
1639 uint32_t retiring_seqno;
1640
1641 request = list_first_entry(&dev_priv->mm.request_list,
1642 struct drm_i915_gem_request,
1643 list);
1644 retiring_seqno = request->seqno;
1645
1646 if (i915_seqno_passed(seqno, retiring_seqno) ||
1647 dev_priv->mm.wedged) {
1648 i915_gem_retire_request(dev, request);
1649
1650 list_del(&request->list);
1651 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
1652 } else
1653 break;
1654 }
1655}
1656
1657void
1658i915_gem_retire_work_handler(struct work_struct *work)
1659{
1660 drm_i915_private_t *dev_priv;
1661 struct drm_device *dev;
1662
1663 dev_priv = container_of(work, drm_i915_private_t,
1664 mm.retire_work.work);
1665 dev = dev_priv->dev;
1666
1667 mutex_lock(&dev->struct_mutex);
1668 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001669 if (!dev_priv->mm.suspended &&
1670 !list_empty(&dev_priv->mm.request_list))
Eric Anholt673a3942008-07-30 12:06:12 -07001671 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1672 mutex_unlock(&dev->struct_mutex);
1673}
1674
1675/**
1676 * Waits for a sequence number to be signaled, and cleans up the
1677 * request and object lists appropriately for that event.
1678 */
Eric Anholt3043c602008-10-02 12:24:47 -07001679static int
Eric Anholt673a3942008-07-30 12:06:12 -07001680i915_wait_request(struct drm_device *dev, uint32_t seqno)
1681{
1682 drm_i915_private_t *dev_priv = dev->dev_private;
1683 int ret = 0;
1684
1685 BUG_ON(seqno == 0);
1686
1687 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
1688 dev_priv->mm.waiting_gem_seqno = seqno;
1689 i915_user_irq_get(dev);
1690 ret = wait_event_interruptible(dev_priv->irq_queue,
1691 i915_seqno_passed(i915_get_gem_seqno(dev),
1692 seqno) ||
1693 dev_priv->mm.wedged);
1694 i915_user_irq_put(dev);
1695 dev_priv->mm.waiting_gem_seqno = 0;
1696 }
1697 if (dev_priv->mm.wedged)
1698 ret = -EIO;
1699
1700 if (ret && ret != -ERESTARTSYS)
1701 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1702 __func__, ret, seqno, i915_get_gem_seqno(dev));
1703
1704 /* Directly dispatch request retiring. While we have the work queue
1705 * to handle this, the waiter on a request often wants an associated
1706 * buffer to have made it to the inactive list, and we would need
1707 * a separate wait queue to handle that.
1708 */
1709 if (ret == 0)
1710 i915_gem_retire_requests(dev);
1711
1712 return ret;
1713}
1714
1715static void
1716i915_gem_flush(struct drm_device *dev,
1717 uint32_t invalidate_domains,
1718 uint32_t flush_domains)
1719{
1720 drm_i915_private_t *dev_priv = dev->dev_private;
1721 uint32_t cmd;
1722 RING_LOCALS;
1723
1724#if WATCH_EXEC
1725 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1726 invalidate_domains, flush_domains);
1727#endif
1728
1729 if (flush_domains & I915_GEM_DOMAIN_CPU)
1730 drm_agp_chipset_flush(dev);
1731
1732 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
1733 I915_GEM_DOMAIN_GTT)) {
1734 /*
1735 * read/write caches:
1736 *
1737 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1738 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1739 * also flushed at 2d versus 3d pipeline switches.
1740 *
1741 * read-only caches:
1742 *
1743 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1744 * MI_READ_FLUSH is set, and is always flushed on 965.
1745 *
1746 * I915_GEM_DOMAIN_COMMAND may not exist?
1747 *
1748 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1749 * invalidated when MI_EXE_FLUSH is set.
1750 *
1751 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1752 * invalidated with every MI_FLUSH.
1753 *
1754 * TLBs:
1755 *
1756 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1757 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1758 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1759 * are flushed at any MI_FLUSH.
1760 */
1761
1762 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1763 if ((invalidate_domains|flush_domains) &
1764 I915_GEM_DOMAIN_RENDER)
1765 cmd &= ~MI_NO_WRITE_FLUSH;
1766 if (!IS_I965G(dev)) {
1767 /*
1768 * On the 965, the sampler cache always gets flushed
1769 * and this bit is reserved.
1770 */
1771 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1772 cmd |= MI_READ_FLUSH;
1773 }
1774 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1775 cmd |= MI_EXE_FLUSH;
1776
1777#if WATCH_EXEC
1778 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1779#endif
1780 BEGIN_LP_RING(2);
1781 OUT_RING(cmd);
1782 OUT_RING(0); /* noop */
1783 ADVANCE_LP_RING();
1784 }
1785}
1786
1787/**
1788 * Ensures that all rendering to the object has completed and the object is
1789 * safe to unbind from the GTT or access from the CPU.
1790 */
1791static int
1792i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1793{
1794 struct drm_device *dev = obj->dev;
1795 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1796 int ret;
1797
Eric Anholte47c68e2008-11-14 13:35:19 -08001798 /* This function only exists to support waiting for existing rendering,
1799 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001800 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001801 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001802
1803 /* If there is rendering queued on the buffer being evicted, wait for
1804 * it.
1805 */
1806 if (obj_priv->active) {
1807#if WATCH_BUF
1808 DRM_INFO("%s: object %p wait for seqno %08x\n",
1809 __func__, obj, obj_priv->last_rendering_seqno);
1810#endif
1811 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1812 if (ret != 0)
1813 return ret;
1814 }
1815
1816 return 0;
1817}
1818
1819/**
1820 * Unbinds an object from the GTT aperture.
1821 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001822int
Eric Anholt673a3942008-07-30 12:06:12 -07001823i915_gem_object_unbind(struct drm_gem_object *obj)
1824{
1825 struct drm_device *dev = obj->dev;
1826 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001827 loff_t offset;
Eric Anholt673a3942008-07-30 12:06:12 -07001828 int ret = 0;
1829
1830#if WATCH_BUF
1831 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1832 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1833#endif
1834 if (obj_priv->gtt_space == NULL)
1835 return 0;
1836
1837 if (obj_priv->pin_count != 0) {
1838 DRM_ERROR("Attempting to unbind pinned buffer\n");
1839 return -EINVAL;
1840 }
1841
Eric Anholt673a3942008-07-30 12:06:12 -07001842 /* Move the object to the CPU domain to ensure that
1843 * any possible CPU writes while it's not in the GTT
1844 * are flushed when we go to remap it. This will
1845 * also ensure that all pending GPU writes are finished
1846 * before we unbind.
1847 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001848 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001849 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001850 if (ret != -ERESTARTSYS)
1851 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001852 return ret;
1853 }
1854
1855 if (obj_priv->agp_mem != NULL) {
1856 drm_unbind_agp(obj_priv->agp_mem);
1857 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1858 obj_priv->agp_mem = NULL;
1859 }
1860
1861 BUG_ON(obj_priv->active);
1862
Jesse Barnesde151cf2008-11-12 10:03:55 -08001863 /* blow away mappings if mapped through GTT */
1864 offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08001865 if (dev->dev_mapping)
1866 unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001867
1868 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1869 i915_gem_clear_fence_reg(obj);
1870
Eric Anholt856fa192009-03-19 14:10:50 -07001871 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001872
1873 if (obj_priv->gtt_space) {
1874 atomic_dec(&dev->gtt_count);
1875 atomic_sub(obj->size, &dev->gtt_memory);
1876
1877 drm_mm_put_block(obj_priv->gtt_space);
1878 obj_priv->gtt_space = NULL;
1879 }
1880
1881 /* Remove ourselves from the LRU list if present. */
1882 if (!list_empty(&obj_priv->list))
1883 list_del_init(&obj_priv->list);
1884
1885 return 0;
1886}
1887
1888static int
1889i915_gem_evict_something(struct drm_device *dev)
1890{
1891 drm_i915_private_t *dev_priv = dev->dev_private;
1892 struct drm_gem_object *obj;
1893 struct drm_i915_gem_object *obj_priv;
1894 int ret = 0;
1895
1896 for (;;) {
1897 /* If there's an inactive buffer available now, grab it
1898 * and be done.
1899 */
1900 if (!list_empty(&dev_priv->mm.inactive_list)) {
1901 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1902 struct drm_i915_gem_object,
1903 list);
1904 obj = obj_priv->obj;
1905 BUG_ON(obj_priv->pin_count != 0);
1906#if WATCH_LRU
1907 DRM_INFO("%s: evicting %p\n", __func__, obj);
1908#endif
1909 BUG_ON(obj_priv->active);
1910
1911 /* Wait on the rendering and unbind the buffer. */
1912 ret = i915_gem_object_unbind(obj);
1913 break;
1914 }
1915
1916 /* If we didn't get anything, but the ring is still processing
1917 * things, wait for one of those things to finish and hopefully
1918 * leave us a buffer to evict.
1919 */
1920 if (!list_empty(&dev_priv->mm.request_list)) {
1921 struct drm_i915_gem_request *request;
1922
1923 request = list_first_entry(&dev_priv->mm.request_list,
1924 struct drm_i915_gem_request,
1925 list);
1926
1927 ret = i915_wait_request(dev, request->seqno);
1928 if (ret)
1929 break;
1930
1931 /* if waiting caused an object to become inactive,
1932 * then loop around and wait for it. Otherwise, we
1933 * assume that waiting freed and unbound something,
1934 * so there should now be some space in the GTT
1935 */
1936 if (!list_empty(&dev_priv->mm.inactive_list))
1937 continue;
1938 break;
1939 }
1940
1941 /* If we didn't have anything on the request list but there
1942 * are buffers awaiting a flush, emit one and try again.
1943 * When we wait on it, those buffers waiting for that flush
1944 * will get moved to inactive.
1945 */
1946 if (!list_empty(&dev_priv->mm.flushing_list)) {
1947 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1948 struct drm_i915_gem_object,
1949 list);
1950 obj = obj_priv->obj;
1951
1952 i915_gem_flush(dev,
1953 obj->write_domain,
1954 obj->write_domain);
1955 i915_add_request(dev, obj->write_domain);
1956
1957 obj = NULL;
1958 continue;
1959 }
1960
1961 DRM_ERROR("inactive empty %d request empty %d "
1962 "flushing empty %d\n",
1963 list_empty(&dev_priv->mm.inactive_list),
1964 list_empty(&dev_priv->mm.request_list),
1965 list_empty(&dev_priv->mm.flushing_list));
1966 /* If we didn't do any of the above, there's nothing to be done
1967 * and we just can't fit it in.
1968 */
1969 return -ENOMEM;
1970 }
1971 return ret;
1972}
1973
1974static int
Keith Packardac94a962008-11-20 23:30:27 -08001975i915_gem_evict_everything(struct drm_device *dev)
1976{
1977 int ret;
1978
1979 for (;;) {
1980 ret = i915_gem_evict_something(dev);
1981 if (ret != 0)
1982 break;
1983 }
Owain Ainsworth15c35332008-12-06 20:42:20 -08001984 if (ret == -ENOMEM)
1985 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08001986 return ret;
1987}
1988
Ben Gamari6911a9b2009-04-02 11:24:54 -07001989int
Eric Anholt856fa192009-03-19 14:10:50 -07001990i915_gem_object_get_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001991{
1992 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1993 int page_count, i;
1994 struct address_space *mapping;
1995 struct inode *inode;
1996 struct page *page;
1997 int ret;
1998
Eric Anholt856fa192009-03-19 14:10:50 -07001999 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002000 return 0;
2001
2002 /* Get the list of pages out of our struct file. They'll be pinned
2003 * at this point until we release them.
2004 */
2005 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002006 BUG_ON(obj_priv->pages != NULL);
2007 obj_priv->pages = drm_calloc(page_count, sizeof(struct page *),
2008 DRM_MEM_DRIVER);
2009 if (obj_priv->pages == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002010 DRM_ERROR("Faled to allocate page list\n");
Eric Anholt856fa192009-03-19 14:10:50 -07002011 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002012 return -ENOMEM;
2013 }
2014
2015 inode = obj->filp->f_path.dentry->d_inode;
2016 mapping = inode->i_mapping;
2017 for (i = 0; i < page_count; i++) {
2018 page = read_mapping_page(mapping, i, NULL);
2019 if (IS_ERR(page)) {
2020 ret = PTR_ERR(page);
2021 DRM_ERROR("read_mapping_page failed: %d\n", ret);
Eric Anholt856fa192009-03-19 14:10:50 -07002022 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002023 return ret;
2024 }
Eric Anholt856fa192009-03-19 14:10:50 -07002025 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002026 }
Eric Anholt280b7132009-03-12 16:56:27 -07002027
2028 if (obj_priv->tiling_mode != I915_TILING_NONE)
2029 i915_gem_object_do_bit_17_swizzle(obj);
2030
Eric Anholt673a3942008-07-30 12:06:12 -07002031 return 0;
2032}
2033
Jesse Barnesde151cf2008-11-12 10:03:55 -08002034static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2035{
2036 struct drm_gem_object *obj = reg->obj;
2037 struct drm_device *dev = obj->dev;
2038 drm_i915_private_t *dev_priv = dev->dev_private;
2039 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2040 int regnum = obj_priv->fence_reg;
2041 uint64_t val;
2042
2043 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2044 0xfffff000) << 32;
2045 val |= obj_priv->gtt_offset & 0xfffff000;
2046 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2047 if (obj_priv->tiling_mode == I915_TILING_Y)
2048 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2049 val |= I965_FENCE_REG_VALID;
2050
2051 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2052}
2053
2054static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2055{
2056 struct drm_gem_object *obj = reg->obj;
2057 struct drm_device *dev = obj->dev;
2058 drm_i915_private_t *dev_priv = dev->dev_private;
2059 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2060 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002061 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002062 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002063 uint32_t pitch_val;
2064
2065 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2066 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002067 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002068 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002069 return;
2070 }
2071
Jesse Barnes0f973f22009-01-26 17:10:45 -08002072 if (obj_priv->tiling_mode == I915_TILING_Y &&
2073 HAS_128_BYTE_Y_TILING(dev))
2074 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002075 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002076 tile_width = 512;
2077
2078 /* Note: pitch better be a power of two tile widths */
2079 pitch_val = obj_priv->stride / tile_width;
2080 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002081
2082 val = obj_priv->gtt_offset;
2083 if (obj_priv->tiling_mode == I915_TILING_Y)
2084 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2085 val |= I915_FENCE_SIZE_BITS(obj->size);
2086 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2087 val |= I830_FENCE_REG_VALID;
2088
Eric Anholtdc529a42009-03-10 22:34:49 -07002089 if (regnum < 8)
2090 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2091 else
2092 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2093 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002094}
2095
2096static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2097{
2098 struct drm_gem_object *obj = reg->obj;
2099 struct drm_device *dev = obj->dev;
2100 drm_i915_private_t *dev_priv = dev->dev_private;
2101 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2102 int regnum = obj_priv->fence_reg;
2103 uint32_t val;
2104 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002105 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002106
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002107 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002108 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002109 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002110 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002111 return;
2112 }
2113
2114 pitch_val = (obj_priv->stride / 128) - 1;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002115 WARN_ON(pitch_val & ~0x0000000f);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002116 val = obj_priv->gtt_offset;
2117 if (obj_priv->tiling_mode == I915_TILING_Y)
2118 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002119 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2120 WARN_ON(fence_size_bits & ~0x00000f00);
2121 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002122 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2123 val |= I830_FENCE_REG_VALID;
2124
2125 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
2126
2127}
2128
2129/**
2130 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2131 * @obj: object to map through a fence reg
Jesse Barnes0f973f22009-01-26 17:10:45 -08002132 * @write: object is about to be written
Jesse Barnesde151cf2008-11-12 10:03:55 -08002133 *
2134 * When mapping objects through the GTT, userspace wants to be able to write
2135 * to them without having to worry about swizzling if the object is tiled.
2136 *
2137 * This function walks the fence regs looking for a free one for @obj,
2138 * stealing one if it can't find any.
2139 *
2140 * It then sets up the reg based on the object's properties: address, pitch
2141 * and tiling format.
2142 */
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002143static int
Jesse Barnes0f973f22009-01-26 17:10:45 -08002144i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002145{
2146 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002147 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002148 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2149 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002150 struct drm_i915_gem_object *old_obj_priv = NULL;
2151 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002152
2153 switch (obj_priv->tiling_mode) {
2154 case I915_TILING_NONE:
2155 WARN(1, "allocating a fence for non-tiled object?\n");
2156 break;
2157 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002158 if (!obj_priv->stride)
2159 return -EINVAL;
2160 WARN((obj_priv->stride & (512 - 1)),
2161 "object 0x%08x is X tiled but has non-512B pitch\n",
2162 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002163 break;
2164 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002165 if (!obj_priv->stride)
2166 return -EINVAL;
2167 WARN((obj_priv->stride & (128 - 1)),
2168 "object 0x%08x is Y tiled but has non-128B pitch\n",
2169 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002170 break;
2171 }
2172
2173 /* First try to find a free reg */
Chris Wilson9b2412f2009-02-11 14:26:44 +00002174try_again:
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002175 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002176 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2177 reg = &dev_priv->fence_regs[i];
2178 if (!reg->obj)
2179 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002180
2181 old_obj_priv = reg->obj->driver_private;
2182 if (!old_obj_priv->pin_count)
2183 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002184 }
2185
2186 /* None available, try to steal one or wait for a user to finish */
2187 if (i == dev_priv->num_fence_regs) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002188 uint32_t seqno = dev_priv->mm.next_gem_seqno;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002189 loff_t offset;
2190
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002191 if (avail == 0)
2192 return -ENOMEM;
2193
Jesse Barnesde151cf2008-11-12 10:03:55 -08002194 for (i = dev_priv->fence_reg_start;
2195 i < dev_priv->num_fence_regs; i++) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002196 uint32_t this_seqno;
2197
Jesse Barnesde151cf2008-11-12 10:03:55 -08002198 reg = &dev_priv->fence_regs[i];
2199 old_obj_priv = reg->obj->driver_private;
Chris Wilsond7619c42009-02-11 14:26:47 +00002200
2201 if (old_obj_priv->pin_count)
2202 continue;
2203
2204 /* i915 uses fences for GPU access to tiled buffers */
2205 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002206 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002207
2208 /* find the seqno of the first available fence */
2209 this_seqno = old_obj_priv->last_rendering_seqno;
2210 if (this_seqno != 0 &&
2211 reg->obj->write_domain == 0 &&
2212 i915_seqno_passed(seqno, this_seqno))
2213 seqno = this_seqno;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002214 }
2215
2216 /*
2217 * Now things get ugly... we have to wait for one of the
2218 * objects to finish before trying again.
2219 */
2220 if (i == dev_priv->num_fence_regs) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002221 if (seqno == dev_priv->mm.next_gem_seqno) {
2222 i915_gem_flush(dev,
2223 I915_GEM_GPU_DOMAINS,
2224 I915_GEM_GPU_DOMAINS);
2225 seqno = i915_add_request(dev,
2226 I915_GEM_GPU_DOMAINS);
2227 if (seqno == 0)
2228 return -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002229 }
Chris Wilsond7619c42009-02-11 14:26:47 +00002230
2231 ret = i915_wait_request(dev, seqno);
2232 if (ret)
2233 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002234 goto try_again;
2235 }
2236
Chris Wilsond7619c42009-02-11 14:26:47 +00002237 BUG_ON(old_obj_priv->active ||
2238 (reg->obj->write_domain & I915_GEM_GPU_DOMAINS));
2239
Jesse Barnesde151cf2008-11-12 10:03:55 -08002240 /*
2241 * Zap this virtual mapping so we can set up a fence again
2242 * for this object next time we need it.
2243 */
2244 offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08002245 if (dev->dev_mapping)
2246 unmap_mapping_range(dev->dev_mapping, offset,
2247 reg->obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002248 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
2249 }
2250
2251 obj_priv->fence_reg = i;
2252 reg->obj = obj;
2253
2254 if (IS_I965G(dev))
2255 i965_write_fence_reg(reg);
2256 else if (IS_I9XX(dev))
2257 i915_write_fence_reg(reg);
2258 else
2259 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002260
2261 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002262}
2263
2264/**
2265 * i915_gem_clear_fence_reg - clear out fence register info
2266 * @obj: object to clear
2267 *
2268 * Zeroes out the fence register itself and clears out the associated
2269 * data structures in dev_priv and obj_priv.
2270 */
2271static void
2272i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2273{
2274 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002275 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002276 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2277
2278 if (IS_I965G(dev))
2279 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002280 else {
2281 uint32_t fence_reg;
2282
2283 if (obj_priv->fence_reg < 8)
2284 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2285 else
2286 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2287 8) * 4;
2288
2289 I915_WRITE(fence_reg, 0);
2290 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002291
2292 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2293 obj_priv->fence_reg = I915_FENCE_REG_NONE;
2294}
2295
Eric Anholt673a3942008-07-30 12:06:12 -07002296/**
2297 * Finds free space in the GTT aperture and binds the object there.
2298 */
2299static int
2300i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2301{
2302 struct drm_device *dev = obj->dev;
2303 drm_i915_private_t *dev_priv = dev->dev_private;
2304 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2305 struct drm_mm_node *free_space;
2306 int page_count, ret;
2307
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002308 if (dev_priv->mm.suspended)
2309 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002310 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002311 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002312 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002313 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2314 return -EINVAL;
2315 }
2316
2317 search_free:
2318 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2319 obj->size, alignment, 0);
2320 if (free_space != NULL) {
2321 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2322 alignment);
2323 if (obj_priv->gtt_space != NULL) {
2324 obj_priv->gtt_space->private = obj;
2325 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2326 }
2327 }
2328 if (obj_priv->gtt_space == NULL) {
Carl Worth5e118f42009-03-20 11:54:25 -07002329 bool lists_empty;
2330
Eric Anholt673a3942008-07-30 12:06:12 -07002331 /* If the gtt is empty and we're still having trouble
2332 * fitting our object in, we're out of memory.
2333 */
2334#if WATCH_LRU
2335 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2336#endif
Carl Worth5e118f42009-03-20 11:54:25 -07002337 spin_lock(&dev_priv->mm.active_list_lock);
2338 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2339 list_empty(&dev_priv->mm.flushing_list) &&
2340 list_empty(&dev_priv->mm.active_list));
2341 spin_unlock(&dev_priv->mm.active_list_lock);
2342 if (lists_empty) {
Eric Anholt673a3942008-07-30 12:06:12 -07002343 DRM_ERROR("GTT full, but LRU list empty\n");
2344 return -ENOMEM;
2345 }
2346
2347 ret = i915_gem_evict_something(dev);
2348 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08002349 if (ret != -ERESTARTSYS)
2350 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002351 return ret;
2352 }
2353 goto search_free;
2354 }
2355
2356#if WATCH_BUF
2357 DRM_INFO("Binding object of size %d at 0x%08x\n",
2358 obj->size, obj_priv->gtt_offset);
2359#endif
Eric Anholt856fa192009-03-19 14:10:50 -07002360 ret = i915_gem_object_get_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002361 if (ret) {
2362 drm_mm_put_block(obj_priv->gtt_space);
2363 obj_priv->gtt_space = NULL;
2364 return ret;
2365 }
2366
2367 page_count = obj->size / PAGE_SIZE;
2368 /* Create an AGP memory structure pointing at our pages, and bind it
2369 * into the GTT.
2370 */
2371 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002372 obj_priv->pages,
Eric Anholt673a3942008-07-30 12:06:12 -07002373 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002374 obj_priv->gtt_offset,
2375 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002376 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002377 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002378 drm_mm_put_block(obj_priv->gtt_space);
2379 obj_priv->gtt_space = NULL;
2380 return -ENOMEM;
2381 }
2382 atomic_inc(&dev->gtt_count);
2383 atomic_add(obj->size, &dev->gtt_memory);
2384
2385 /* Assert that the object is not currently in any GPU domain. As it
2386 * wasn't in the GTT, there shouldn't be any way it could have been in
2387 * a GPU cache
2388 */
2389 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2390 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2391
2392 return 0;
2393}
2394
2395void
2396i915_gem_clflush_object(struct drm_gem_object *obj)
2397{
2398 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2399
2400 /* If we don't have a page list set up, then we're not pinned
2401 * to GPU, and we can ignore the cache flush because it'll happen
2402 * again at bind time.
2403 */
Eric Anholt856fa192009-03-19 14:10:50 -07002404 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002405 return;
2406
Eric Anholt856fa192009-03-19 14:10:50 -07002407 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002408}
2409
Eric Anholte47c68e2008-11-14 13:35:19 -08002410/** Flushes any GPU write domain for the object if it's dirty. */
2411static void
2412i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2413{
2414 struct drm_device *dev = obj->dev;
2415 uint32_t seqno;
2416
2417 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2418 return;
2419
2420 /* Queue the GPU write cache flushing we need. */
2421 i915_gem_flush(dev, 0, obj->write_domain);
2422 seqno = i915_add_request(dev, obj->write_domain);
2423 obj->write_domain = 0;
2424 i915_gem_object_move_to_active(obj, seqno);
2425}
2426
2427/** Flushes the GTT write domain for the object if it's dirty. */
2428static void
2429i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2430{
2431 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2432 return;
2433
2434 /* No actual flushing is required for the GTT write domain. Writes
2435 * to it immediately go to main memory as far as we know, so there's
2436 * no chipset flush. It also doesn't land in render cache.
2437 */
2438 obj->write_domain = 0;
2439}
2440
2441/** Flushes the CPU write domain for the object if it's dirty. */
2442static void
2443i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2444{
2445 struct drm_device *dev = obj->dev;
2446
2447 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2448 return;
2449
2450 i915_gem_clflush_object(obj);
2451 drm_agp_chipset_flush(dev);
2452 obj->write_domain = 0;
2453}
2454
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002455/**
2456 * Moves a single object to the GTT read, and possibly write domain.
2457 *
2458 * This function returns when the move is complete, including waiting on
2459 * flushes to occur.
2460 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002461int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002462i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2463{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002464 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002465 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002466
Eric Anholt02354392008-11-26 13:58:13 -08002467 /* Not valid to be called on unbound objects. */
2468 if (obj_priv->gtt_space == NULL)
2469 return -EINVAL;
2470
Eric Anholte47c68e2008-11-14 13:35:19 -08002471 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002472 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002473 ret = i915_gem_object_wait_rendering(obj);
2474 if (ret != 0)
2475 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002476
2477 /* If we're writing through the GTT domain, then CPU and GPU caches
2478 * will need to be invalidated at next use.
2479 */
2480 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002481 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002482
Eric Anholte47c68e2008-11-14 13:35:19 -08002483 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002484
2485 /* It should now be out of any other write domains, and we can update
2486 * the domain values for our changes.
2487 */
2488 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2489 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002490 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002491 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002492 obj_priv->dirty = 1;
2493 }
2494
2495 return 0;
2496}
2497
2498/**
2499 * Moves a single object to the CPU read, and possibly write domain.
2500 *
2501 * This function returns when the move is complete, including waiting on
2502 * flushes to occur.
2503 */
2504static int
2505i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2506{
Eric Anholte47c68e2008-11-14 13:35:19 -08002507 int ret;
2508
2509 i915_gem_object_flush_gpu_write_domain(obj);
2510 /* Wait on any GPU rendering and flushing to occur. */
2511 ret = i915_gem_object_wait_rendering(obj);
2512 if (ret != 0)
2513 return ret;
2514
2515 i915_gem_object_flush_gtt_write_domain(obj);
2516
2517 /* If we have a partially-valid cache of the object in the CPU,
2518 * finish invalidating it and free the per-page flags.
2519 */
2520 i915_gem_object_set_to_full_cpu_read_domain(obj);
2521
2522 /* Flush the CPU cache if it's still invalid. */
2523 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2524 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002525
2526 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2527 }
2528
2529 /* It should now be out of any other write domains, and we can update
2530 * the domain values for our changes.
2531 */
2532 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2533
2534 /* If we're writing through the CPU, then the GPU read domains will
2535 * need to be invalidated at next use.
2536 */
2537 if (write) {
2538 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2539 obj->write_domain = I915_GEM_DOMAIN_CPU;
2540 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002541
2542 return 0;
2543}
2544
Eric Anholt673a3942008-07-30 12:06:12 -07002545/*
2546 * Set the next domain for the specified object. This
2547 * may not actually perform the necessary flushing/invaliding though,
2548 * as that may want to be batched with other set_domain operations
2549 *
2550 * This is (we hope) the only really tricky part of gem. The goal
2551 * is fairly simple -- track which caches hold bits of the object
2552 * and make sure they remain coherent. A few concrete examples may
2553 * help to explain how it works. For shorthand, we use the notation
2554 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2555 * a pair of read and write domain masks.
2556 *
2557 * Case 1: the batch buffer
2558 *
2559 * 1. Allocated
2560 * 2. Written by CPU
2561 * 3. Mapped to GTT
2562 * 4. Read by GPU
2563 * 5. Unmapped from GTT
2564 * 6. Freed
2565 *
2566 * Let's take these a step at a time
2567 *
2568 * 1. Allocated
2569 * Pages allocated from the kernel may still have
2570 * cache contents, so we set them to (CPU, CPU) always.
2571 * 2. Written by CPU (using pwrite)
2572 * The pwrite function calls set_domain (CPU, CPU) and
2573 * this function does nothing (as nothing changes)
2574 * 3. Mapped by GTT
2575 * This function asserts that the object is not
2576 * currently in any GPU-based read or write domains
2577 * 4. Read by GPU
2578 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2579 * As write_domain is zero, this function adds in the
2580 * current read domains (CPU+COMMAND, 0).
2581 * flush_domains is set to CPU.
2582 * invalidate_domains is set to COMMAND
2583 * clflush is run to get data out of the CPU caches
2584 * then i915_dev_set_domain calls i915_gem_flush to
2585 * emit an MI_FLUSH and drm_agp_chipset_flush
2586 * 5. Unmapped from GTT
2587 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2588 * flush_domains and invalidate_domains end up both zero
2589 * so no flushing/invalidating happens
2590 * 6. Freed
2591 * yay, done
2592 *
2593 * Case 2: The shared render buffer
2594 *
2595 * 1. Allocated
2596 * 2. Mapped to GTT
2597 * 3. Read/written by GPU
2598 * 4. set_domain to (CPU,CPU)
2599 * 5. Read/written by CPU
2600 * 6. Read/written by GPU
2601 *
2602 * 1. Allocated
2603 * Same as last example, (CPU, CPU)
2604 * 2. Mapped to GTT
2605 * Nothing changes (assertions find that it is not in the GPU)
2606 * 3. Read/written by GPU
2607 * execbuffer calls set_domain (RENDER, RENDER)
2608 * flush_domains gets CPU
2609 * invalidate_domains gets GPU
2610 * clflush (obj)
2611 * MI_FLUSH and drm_agp_chipset_flush
2612 * 4. set_domain (CPU, CPU)
2613 * flush_domains gets GPU
2614 * invalidate_domains gets CPU
2615 * wait_rendering (obj) to make sure all drawing is complete.
2616 * This will include an MI_FLUSH to get the data from GPU
2617 * to memory
2618 * clflush (obj) to invalidate the CPU cache
2619 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2620 * 5. Read/written by CPU
2621 * cache lines are loaded and dirtied
2622 * 6. Read written by GPU
2623 * Same as last GPU access
2624 *
2625 * Case 3: The constant buffer
2626 *
2627 * 1. Allocated
2628 * 2. Written by CPU
2629 * 3. Read by GPU
2630 * 4. Updated (written) by CPU again
2631 * 5. Read by GPU
2632 *
2633 * 1. Allocated
2634 * (CPU, CPU)
2635 * 2. Written by CPU
2636 * (CPU, CPU)
2637 * 3. Read by GPU
2638 * (CPU+RENDER, 0)
2639 * flush_domains = CPU
2640 * invalidate_domains = RENDER
2641 * clflush (obj)
2642 * MI_FLUSH
2643 * drm_agp_chipset_flush
2644 * 4. Updated (written) by CPU again
2645 * (CPU, CPU)
2646 * flush_domains = 0 (no previous write domain)
2647 * invalidate_domains = 0 (no new read domains)
2648 * 5. Read by GPU
2649 * (CPU+RENDER, 0)
2650 * flush_domains = CPU
2651 * invalidate_domains = RENDER
2652 * clflush (obj)
2653 * MI_FLUSH
2654 * drm_agp_chipset_flush
2655 */
Keith Packardc0d90822008-11-20 23:11:08 -08002656static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002657i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002658{
2659 struct drm_device *dev = obj->dev;
2660 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2661 uint32_t invalidate_domains = 0;
2662 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002663
Eric Anholt8b0e3782009-02-19 14:40:50 -08002664 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2665 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002666
2667#if WATCH_BUF
2668 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2669 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002670 obj->read_domains, obj->pending_read_domains,
2671 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002672#endif
2673 /*
2674 * If the object isn't moving to a new write domain,
2675 * let the object stay in multiple read domains
2676 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002677 if (obj->pending_write_domain == 0)
2678 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002679 else
2680 obj_priv->dirty = 1;
2681
2682 /*
2683 * Flush the current write domain if
2684 * the new read domains don't match. Invalidate
2685 * any read domains which differ from the old
2686 * write domain
2687 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002688 if (obj->write_domain &&
2689 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002690 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002691 invalidate_domains |=
2692 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002693 }
2694 /*
2695 * Invalidate any read caches which may have
2696 * stale data. That is, any new read domains.
2697 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002698 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002699 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2700#if WATCH_BUF
2701 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2702 __func__, flush_domains, invalidate_domains);
2703#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002704 i915_gem_clflush_object(obj);
2705 }
2706
Eric Anholtefbeed92009-02-19 14:54:51 -08002707 /* The actual obj->write_domain will be updated with
2708 * pending_write_domain after we emit the accumulated flush for all
2709 * of our domain changes in execbuffers (which clears objects'
2710 * write_domains). So if we have a current write domain that we
2711 * aren't changing, set pending_write_domain to that.
2712 */
2713 if (flush_domains == 0 && obj->pending_write_domain == 0)
2714 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002715 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002716
2717 dev->invalidate_domains |= invalidate_domains;
2718 dev->flush_domains |= flush_domains;
2719#if WATCH_BUF
2720 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2721 __func__,
2722 obj->read_domains, obj->write_domain,
2723 dev->invalidate_domains, dev->flush_domains);
2724#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002725}
2726
2727/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002728 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002729 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002730 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2731 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2732 */
2733static void
2734i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2735{
Eric Anholte47c68e2008-11-14 13:35:19 -08002736 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2737
2738 if (!obj_priv->page_cpu_valid)
2739 return;
2740
2741 /* If we're partially in the CPU read domain, finish moving it in.
2742 */
2743 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2744 int i;
2745
2746 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2747 if (obj_priv->page_cpu_valid[i])
2748 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07002749 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08002750 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002751 }
2752
2753 /* Free the page_cpu_valid mappings which are now stale, whether
2754 * or not we've got I915_GEM_DOMAIN_CPU.
2755 */
2756 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
2757 DRM_MEM_DRIVER);
2758 obj_priv->page_cpu_valid = NULL;
2759}
2760
2761/**
2762 * Set the CPU read domain on a range of the object.
2763 *
2764 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2765 * not entirely valid. The page_cpu_valid member of the object flags which
2766 * pages have been flushed, and will be respected by
2767 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2768 * of the whole object.
2769 *
2770 * This function returns when the move is complete, including waiting on
2771 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002772 */
2773static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002774i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2775 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002776{
2777 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002778 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002779
Eric Anholte47c68e2008-11-14 13:35:19 -08002780 if (offset == 0 && size == obj->size)
2781 return i915_gem_object_set_to_cpu_domain(obj, 0);
2782
2783 i915_gem_object_flush_gpu_write_domain(obj);
2784 /* Wait on any GPU rendering and flushing to occur. */
2785 ret = i915_gem_object_wait_rendering(obj);
2786 if (ret != 0)
2787 return ret;
2788 i915_gem_object_flush_gtt_write_domain(obj);
2789
2790 /* If we're already fully in the CPU read domain, we're done. */
2791 if (obj_priv->page_cpu_valid == NULL &&
2792 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002793 return 0;
2794
Eric Anholte47c68e2008-11-14 13:35:19 -08002795 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2796 * newly adding I915_GEM_DOMAIN_CPU
2797 */
Eric Anholt673a3942008-07-30 12:06:12 -07002798 if (obj_priv->page_cpu_valid == NULL) {
2799 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2800 DRM_MEM_DRIVER);
Eric Anholte47c68e2008-11-14 13:35:19 -08002801 if (obj_priv->page_cpu_valid == NULL)
2802 return -ENOMEM;
2803 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2804 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002805
2806 /* Flush the cache on any pages that are still invalid from the CPU's
2807 * perspective.
2808 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002809 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2810 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002811 if (obj_priv->page_cpu_valid[i])
2812 continue;
2813
Eric Anholt856fa192009-03-19 14:10:50 -07002814 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002815
2816 obj_priv->page_cpu_valid[i] = 1;
2817 }
2818
Eric Anholte47c68e2008-11-14 13:35:19 -08002819 /* It should now be out of any other write domains, and we can update
2820 * the domain values for our changes.
2821 */
2822 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2823
2824 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2825
Eric Anholt673a3942008-07-30 12:06:12 -07002826 return 0;
2827}
2828
2829/**
Eric Anholt673a3942008-07-30 12:06:12 -07002830 * Pin an object to the GTT and evaluate the relocations landing in it.
2831 */
2832static int
2833i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2834 struct drm_file *file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002835 struct drm_i915_gem_exec_object *entry,
2836 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07002837{
2838 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002839 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002840 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2841 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002842 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002843
2844 /* Choose the GTT offset for our buffer and put it there. */
2845 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2846 if (ret)
2847 return ret;
2848
2849 entry->offset = obj_priv->gtt_offset;
2850
Eric Anholt673a3942008-07-30 12:06:12 -07002851 /* Apply the relocations, using the GTT aperture to avoid cache
2852 * flushing requirements.
2853 */
2854 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002855 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002856 struct drm_gem_object *target_obj;
2857 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002858 uint32_t reloc_val, reloc_offset;
2859 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002860
Eric Anholt673a3942008-07-30 12:06:12 -07002861 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002862 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002863 if (target_obj == NULL) {
2864 i915_gem_object_unpin(obj);
2865 return -EBADF;
2866 }
2867 target_obj_priv = target_obj->driver_private;
2868
2869 /* The target buffer should have appeared before us in the
2870 * exec_object list, so it should have a GTT space bound by now.
2871 */
2872 if (target_obj_priv->gtt_space == NULL) {
2873 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002874 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002875 drm_gem_object_unreference(target_obj);
2876 i915_gem_object_unpin(obj);
2877 return -EINVAL;
2878 }
2879
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002880 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07002881 DRM_ERROR("Relocation beyond object bounds: "
2882 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002883 obj, reloc->target_handle,
2884 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07002885 drm_gem_object_unreference(target_obj);
2886 i915_gem_object_unpin(obj);
2887 return -EINVAL;
2888 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002889 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07002890 DRM_ERROR("Relocation not 4-byte aligned: "
2891 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002892 obj, reloc->target_handle,
2893 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07002894 drm_gem_object_unreference(target_obj);
2895 i915_gem_object_unpin(obj);
2896 return -EINVAL;
2897 }
2898
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002899 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
2900 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08002901 DRM_ERROR("reloc with read/write CPU domains: "
2902 "obj %p target %d offset %d "
2903 "read %08x write %08x",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002904 obj, reloc->target_handle,
2905 (int) reloc->offset,
2906 reloc->read_domains,
2907 reloc->write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00002908 drm_gem_object_unreference(target_obj);
2909 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002910 return -EINVAL;
2911 }
2912
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002913 if (reloc->write_domain && target_obj->pending_write_domain &&
2914 reloc->write_domain != target_obj->pending_write_domain) {
Eric Anholt673a3942008-07-30 12:06:12 -07002915 DRM_ERROR("Write domain conflict: "
2916 "obj %p target %d offset %d "
2917 "new %08x old %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002918 obj, reloc->target_handle,
2919 (int) reloc->offset,
2920 reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07002921 target_obj->pending_write_domain);
2922 drm_gem_object_unreference(target_obj);
2923 i915_gem_object_unpin(obj);
2924 return -EINVAL;
2925 }
2926
2927#if WATCH_RELOC
2928 DRM_INFO("%s: obj %p offset %08x target %d "
2929 "read %08x write %08x gtt %08x "
2930 "presumed %08x delta %08x\n",
2931 __func__,
2932 obj,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002933 (int) reloc->offset,
2934 (int) reloc->target_handle,
2935 (int) reloc->read_domains,
2936 (int) reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07002937 (int) target_obj_priv->gtt_offset,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002938 (int) reloc->presumed_offset,
2939 reloc->delta);
Eric Anholt673a3942008-07-30 12:06:12 -07002940#endif
2941
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002942 target_obj->pending_read_domains |= reloc->read_domains;
2943 target_obj->pending_write_domain |= reloc->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002944
2945 /* If the relocation already has the right value in it, no
2946 * more work needs to be done.
2947 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002948 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
Eric Anholt673a3942008-07-30 12:06:12 -07002949 drm_gem_object_unreference(target_obj);
2950 continue;
2951 }
2952
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002953 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
2954 if (ret != 0) {
2955 drm_gem_object_unreference(target_obj);
2956 i915_gem_object_unpin(obj);
2957 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07002958 }
2959
2960 /* Map the page containing the relocation we're going to
2961 * perform.
2962 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002963 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07002964 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
2965 (reloc_offset &
2966 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07002967 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07002968 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002969 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07002970
2971#if WATCH_BUF
2972 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002973 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07002974 readl(reloc_entry), reloc_val);
2975#endif
2976 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07002977 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07002978
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002979 /* The updated presumed offset for this entry will be
2980 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07002981 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002982 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07002983
2984 drm_gem_object_unreference(target_obj);
2985 }
2986
Eric Anholt673a3942008-07-30 12:06:12 -07002987#if WATCH_BUF
2988 if (0)
2989 i915_gem_dump_object(obj, 128, __func__, ~0);
2990#endif
2991 return 0;
2992}
2993
2994/** Dispatch a batchbuffer to the ring
2995 */
2996static int
2997i915_dispatch_gem_execbuffer(struct drm_device *dev,
2998 struct drm_i915_gem_execbuffer *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07002999 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003000 uint64_t exec_offset)
3001{
3002 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003003 int nbox = exec->num_cliprects;
3004 int i = 0, count;
3005 uint32_t exec_start, exec_len;
3006 RING_LOCALS;
3007
3008 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3009 exec_len = (uint32_t) exec->batch_len;
3010
3011 if ((exec_start | exec_len) & 0x7) {
3012 DRM_ERROR("alignment\n");
3013 return -EINVAL;
3014 }
3015
3016 if (!exec_start)
3017 return -EINVAL;
3018
3019 count = nbox ? nbox : 1;
3020
3021 for (i = 0; i < count; i++) {
3022 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003023 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003024 exec->DR1, exec->DR4);
3025 if (ret)
3026 return ret;
3027 }
3028
3029 if (IS_I830(dev) || IS_845G(dev)) {
3030 BEGIN_LP_RING(4);
3031 OUT_RING(MI_BATCH_BUFFER);
3032 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3033 OUT_RING(exec_start + exec_len - 4);
3034 OUT_RING(0);
3035 ADVANCE_LP_RING();
3036 } else {
3037 BEGIN_LP_RING(2);
3038 if (IS_I965G(dev)) {
3039 OUT_RING(MI_BATCH_BUFFER_START |
3040 (2 << 6) |
3041 MI_BATCH_NON_SECURE_I965);
3042 OUT_RING(exec_start);
3043 } else {
3044 OUT_RING(MI_BATCH_BUFFER_START |
3045 (2 << 6));
3046 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3047 }
3048 ADVANCE_LP_RING();
3049 }
3050 }
3051
3052 /* XXX breadcrumb */
3053 return 0;
3054}
3055
3056/* Throttle our rendering by waiting until the ring has completed our requests
3057 * emitted over 20 msec ago.
3058 *
3059 * This should get us reasonable parallelism between CPU and GPU but also
3060 * relatively low latency when blocking on a particular request to finish.
3061 */
3062static int
3063i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3064{
3065 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3066 int ret = 0;
3067 uint32_t seqno;
3068
3069 mutex_lock(&dev->struct_mutex);
3070 seqno = i915_file_priv->mm.last_gem_throttle_seqno;
3071 i915_file_priv->mm.last_gem_throttle_seqno =
3072 i915_file_priv->mm.last_gem_seqno;
3073 if (seqno)
3074 ret = i915_wait_request(dev, seqno);
3075 mutex_unlock(&dev->struct_mutex);
3076 return ret;
3077}
3078
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003079static int
3080i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3081 uint32_t buffer_count,
3082 struct drm_i915_gem_relocation_entry **relocs)
3083{
3084 uint32_t reloc_count = 0, reloc_index = 0, i;
3085 int ret;
3086
3087 *relocs = NULL;
3088 for (i = 0; i < buffer_count; i++) {
3089 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3090 return -EINVAL;
3091 reloc_count += exec_list[i].relocation_count;
3092 }
3093
3094 *relocs = drm_calloc(reloc_count, sizeof(**relocs), DRM_MEM_DRIVER);
3095 if (*relocs == NULL)
3096 return -ENOMEM;
3097
3098 for (i = 0; i < buffer_count; i++) {
3099 struct drm_i915_gem_relocation_entry __user *user_relocs;
3100
3101 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3102
3103 ret = copy_from_user(&(*relocs)[reloc_index],
3104 user_relocs,
3105 exec_list[i].relocation_count *
3106 sizeof(**relocs));
3107 if (ret != 0) {
3108 drm_free(*relocs, reloc_count * sizeof(**relocs),
3109 DRM_MEM_DRIVER);
3110 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003111 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003112 }
3113
3114 reloc_index += exec_list[i].relocation_count;
3115 }
3116
Florian Mickler2bc43b52009-04-06 22:55:41 +02003117 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003118}
3119
3120static int
3121i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3122 uint32_t buffer_count,
3123 struct drm_i915_gem_relocation_entry *relocs)
3124{
3125 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003126 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003127
3128 for (i = 0; i < buffer_count; i++) {
3129 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003130 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003131
3132 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3133
Florian Mickler2bc43b52009-04-06 22:55:41 +02003134 unwritten = copy_to_user(user_relocs,
3135 &relocs[reloc_count],
3136 exec_list[i].relocation_count *
3137 sizeof(*relocs));
3138
3139 if (unwritten) {
3140 ret = -EFAULT;
3141 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003142 }
3143
3144 reloc_count += exec_list[i].relocation_count;
3145 }
3146
Florian Mickler2bc43b52009-04-06 22:55:41 +02003147err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003148 drm_free(relocs, reloc_count * sizeof(*relocs), DRM_MEM_DRIVER);
3149
3150 return ret;
3151}
3152
Eric Anholt673a3942008-07-30 12:06:12 -07003153int
3154i915_gem_execbuffer(struct drm_device *dev, void *data,
3155 struct drm_file *file_priv)
3156{
3157 drm_i915_private_t *dev_priv = dev->dev_private;
3158 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3159 struct drm_i915_gem_execbuffer *args = data;
3160 struct drm_i915_gem_exec_object *exec_list = NULL;
3161 struct drm_gem_object **object_list = NULL;
3162 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003163 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003164 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003165 struct drm_i915_gem_relocation_entry *relocs;
3166 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003167 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003168 uint32_t seqno, flush_domains, reloc_index;
Keith Packardac94a962008-11-20 23:30:27 -08003169 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07003170
3171#if WATCH_EXEC
3172 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3173 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3174#endif
3175
Eric Anholt4f481ed2008-09-10 14:22:49 -07003176 if (args->buffer_count < 1) {
3177 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3178 return -EINVAL;
3179 }
Eric Anholt673a3942008-07-30 12:06:12 -07003180 /* Copy in the exec list from userland */
3181 exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
3182 DRM_MEM_DRIVER);
3183 object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
3184 DRM_MEM_DRIVER);
3185 if (exec_list == NULL || object_list == NULL) {
3186 DRM_ERROR("Failed to allocate exec or object list "
3187 "for %d buffers\n",
3188 args->buffer_count);
3189 ret = -ENOMEM;
3190 goto pre_mutex_err;
3191 }
3192 ret = copy_from_user(exec_list,
3193 (struct drm_i915_relocation_entry __user *)
3194 (uintptr_t) args->buffers_ptr,
3195 sizeof(*exec_list) * args->buffer_count);
3196 if (ret != 0) {
3197 DRM_ERROR("copy %d exec entries failed %d\n",
3198 args->buffer_count, ret);
3199 goto pre_mutex_err;
3200 }
3201
Eric Anholt201361a2009-03-11 12:30:04 -07003202 if (args->num_cliprects != 0) {
3203 cliprects = drm_calloc(args->num_cliprects, sizeof(*cliprects),
3204 DRM_MEM_DRIVER);
3205 if (cliprects == NULL)
3206 goto pre_mutex_err;
3207
3208 ret = copy_from_user(cliprects,
3209 (struct drm_clip_rect __user *)
3210 (uintptr_t) args->cliprects_ptr,
3211 sizeof(*cliprects) * args->num_cliprects);
3212 if (ret != 0) {
3213 DRM_ERROR("copy %d cliprects failed: %d\n",
3214 args->num_cliprects, ret);
3215 goto pre_mutex_err;
3216 }
3217 }
3218
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003219 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3220 &relocs);
3221 if (ret != 0)
3222 goto pre_mutex_err;
3223
Eric Anholt673a3942008-07-30 12:06:12 -07003224 mutex_lock(&dev->struct_mutex);
3225
3226 i915_verify_inactive(dev, __FILE__, __LINE__);
3227
3228 if (dev_priv->mm.wedged) {
3229 DRM_ERROR("Execbuf while wedged\n");
3230 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003231 ret = -EIO;
3232 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003233 }
3234
3235 if (dev_priv->mm.suspended) {
3236 DRM_ERROR("Execbuf while VT-switched.\n");
3237 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003238 ret = -EBUSY;
3239 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003240 }
3241
Keith Packardac94a962008-11-20 23:30:27 -08003242 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003243 for (i = 0; i < args->buffer_count; i++) {
3244 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3245 exec_list[i].handle);
3246 if (object_list[i] == NULL) {
3247 DRM_ERROR("Invalid object handle %d at index %d\n",
3248 exec_list[i].handle, i);
3249 ret = -EBADF;
3250 goto err;
3251 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003252
3253 obj_priv = object_list[i]->driver_private;
3254 if (obj_priv->in_execbuffer) {
3255 DRM_ERROR("Object %p appears more than once in object list\n",
3256 object_list[i]);
3257 ret = -EBADF;
3258 goto err;
3259 }
3260 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003261 }
Eric Anholt673a3942008-07-30 12:06:12 -07003262
Keith Packardac94a962008-11-20 23:30:27 -08003263 /* Pin and relocate */
3264 for (pin_tries = 0; ; pin_tries++) {
3265 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003266 reloc_index = 0;
3267
Keith Packardac94a962008-11-20 23:30:27 -08003268 for (i = 0; i < args->buffer_count; i++) {
3269 object_list[i]->pending_read_domains = 0;
3270 object_list[i]->pending_write_domain = 0;
3271 ret = i915_gem_object_pin_and_relocate(object_list[i],
3272 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003273 &exec_list[i],
3274 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003275 if (ret)
3276 break;
3277 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003278 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003279 }
3280 /* success */
3281 if (ret == 0)
3282 break;
3283
3284 /* error other than GTT full, or we've already tried again */
3285 if (ret != -ENOMEM || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08003286 if (ret != -ERESTARTSYS)
3287 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003288 goto err;
3289 }
Keith Packardac94a962008-11-20 23:30:27 -08003290
3291 /* unpin all of our buffers */
3292 for (i = 0; i < pinned; i++)
3293 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003294 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003295
3296 /* evict everyone we can from the aperture */
3297 ret = i915_gem_evict_everything(dev);
3298 if (ret)
3299 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003300 }
3301
3302 /* Set the pending read domains for the batch buffer to COMMAND */
3303 batch_obj = object_list[args->buffer_count-1];
3304 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
3305 batch_obj->pending_write_domain = 0;
3306
3307 i915_verify_inactive(dev, __FILE__, __LINE__);
3308
Keith Packard646f0f62008-11-20 23:23:03 -08003309 /* Zero the global flush/invalidate flags. These
3310 * will be modified as new domains are computed
3311 * for each object
3312 */
3313 dev->invalidate_domains = 0;
3314 dev->flush_domains = 0;
3315
Eric Anholt673a3942008-07-30 12:06:12 -07003316 for (i = 0; i < args->buffer_count; i++) {
3317 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003318
Keith Packard646f0f62008-11-20 23:23:03 -08003319 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003320 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003321 }
3322
3323 i915_verify_inactive(dev, __FILE__, __LINE__);
3324
Keith Packard646f0f62008-11-20 23:23:03 -08003325 if (dev->invalidate_domains | dev->flush_domains) {
3326#if WATCH_EXEC
3327 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3328 __func__,
3329 dev->invalidate_domains,
3330 dev->flush_domains);
3331#endif
3332 i915_gem_flush(dev,
3333 dev->invalidate_domains,
3334 dev->flush_domains);
3335 if (dev->flush_domains)
3336 (void)i915_add_request(dev, dev->flush_domains);
3337 }
Eric Anholt673a3942008-07-30 12:06:12 -07003338
Eric Anholtefbeed92009-02-19 14:54:51 -08003339 for (i = 0; i < args->buffer_count; i++) {
3340 struct drm_gem_object *obj = object_list[i];
3341
3342 obj->write_domain = obj->pending_write_domain;
3343 }
3344
Eric Anholt673a3942008-07-30 12:06:12 -07003345 i915_verify_inactive(dev, __FILE__, __LINE__);
3346
3347#if WATCH_COHERENCY
3348 for (i = 0; i < args->buffer_count; i++) {
3349 i915_gem_object_check_coherency(object_list[i],
3350 exec_list[i].handle);
3351 }
3352#endif
3353
3354 exec_offset = exec_list[args->buffer_count - 1].offset;
3355
3356#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003357 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003358 args->batch_len,
3359 __func__,
3360 ~0);
3361#endif
3362
Eric Anholt673a3942008-07-30 12:06:12 -07003363 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003364 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003365 if (ret) {
3366 DRM_ERROR("dispatch failed %d\n", ret);
3367 goto err;
3368 }
3369
3370 /*
3371 * Ensure that the commands in the batch buffer are
3372 * finished before the interrupt fires
3373 */
3374 flush_domains = i915_retire_commands(dev);
3375
3376 i915_verify_inactive(dev, __FILE__, __LINE__);
3377
3378 /*
3379 * Get a seqno representing the execution of the current buffer,
3380 * which we can wait on. We would like to mitigate these interrupts,
3381 * likely by only creating seqnos occasionally (so that we have
3382 * *some* interrupts representing completion of buffers that we can
3383 * wait on when trying to clear up gtt space).
3384 */
3385 seqno = i915_add_request(dev, flush_domains);
3386 BUG_ON(seqno == 0);
3387 i915_file_priv->mm.last_gem_seqno = seqno;
3388 for (i = 0; i < args->buffer_count; i++) {
3389 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003390
Eric Anholtce44b0e2008-11-06 16:00:31 -08003391 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003392#if WATCH_LRU
3393 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3394#endif
3395 }
3396#if WATCH_LRU
3397 i915_dump_lru(dev, __func__);
3398#endif
3399
3400 i915_verify_inactive(dev, __FILE__, __LINE__);
3401
Eric Anholt673a3942008-07-30 12:06:12 -07003402err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003403 for (i = 0; i < pinned; i++)
3404 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003405
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003406 for (i = 0; i < args->buffer_count; i++) {
3407 if (object_list[i]) {
3408 obj_priv = object_list[i]->driver_private;
3409 obj_priv->in_execbuffer = false;
3410 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003411 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003412 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003413
Eric Anholt673a3942008-07-30 12:06:12 -07003414 mutex_unlock(&dev->struct_mutex);
3415
Roland Dreiera35f2e22009-02-06 17:48:09 -08003416 if (!ret) {
3417 /* Copy the new buffer offsets back to the user's exec list. */
3418 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3419 (uintptr_t) args->buffers_ptr,
3420 exec_list,
3421 sizeof(*exec_list) * args->buffer_count);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003422 if (ret) {
3423 ret = -EFAULT;
Roland Dreiera35f2e22009-02-06 17:48:09 -08003424 DRM_ERROR("failed to copy %d exec entries "
3425 "back to user (%d)\n",
3426 args->buffer_count, ret);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003427 }
Roland Dreiera35f2e22009-02-06 17:48:09 -08003428 }
3429
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003430 /* Copy the updated relocations out regardless of current error
3431 * state. Failure to update the relocs would mean that the next
3432 * time userland calls execbuf, it would do so with presumed offset
3433 * state that didn't match the actual object state.
3434 */
3435 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3436 relocs);
3437 if (ret2 != 0) {
3438 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3439
3440 if (ret == 0)
3441 ret = ret2;
3442 }
3443
Eric Anholt673a3942008-07-30 12:06:12 -07003444pre_mutex_err:
3445 drm_free(object_list, sizeof(*object_list) * args->buffer_count,
3446 DRM_MEM_DRIVER);
3447 drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
3448 DRM_MEM_DRIVER);
Eric Anholt201361a2009-03-11 12:30:04 -07003449 drm_free(cliprects, sizeof(*cliprects) * args->num_cliprects,
3450 DRM_MEM_DRIVER);
Eric Anholt673a3942008-07-30 12:06:12 -07003451
3452 return ret;
3453}
3454
3455int
3456i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3457{
3458 struct drm_device *dev = obj->dev;
3459 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3460 int ret;
3461
3462 i915_verify_inactive(dev, __FILE__, __LINE__);
3463 if (obj_priv->gtt_space == NULL) {
3464 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3465 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003466 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05003467 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003468 return ret;
3469 }
Chris Wilson22c344e2009-02-11 14:26:45 +00003470 }
3471 /*
3472 * Pre-965 chips need a fence register set up in order to
3473 * properly handle tiled surfaces.
3474 */
3475 if (!IS_I965G(dev) &&
3476 obj_priv->fence_reg == I915_FENCE_REG_NONE &&
3477 obj_priv->tiling_mode != I915_TILING_NONE) {
3478 ret = i915_gem_object_get_fence_reg(obj, true);
3479 if (ret != 0) {
3480 if (ret != -EBUSY && ret != -ERESTARTSYS)
3481 DRM_ERROR("Failure to install fence: %d\n",
3482 ret);
3483 return ret;
3484 }
Eric Anholt673a3942008-07-30 12:06:12 -07003485 }
3486 obj_priv->pin_count++;
3487
3488 /* If the object is not active and not pending a flush,
3489 * remove it from the inactive list
3490 */
3491 if (obj_priv->pin_count == 1) {
3492 atomic_inc(&dev->pin_count);
3493 atomic_add(obj->size, &dev->pin_memory);
3494 if (!obj_priv->active &&
3495 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
3496 I915_GEM_DOMAIN_GTT)) == 0 &&
3497 !list_empty(&obj_priv->list))
3498 list_del_init(&obj_priv->list);
3499 }
3500 i915_verify_inactive(dev, __FILE__, __LINE__);
3501
3502 return 0;
3503}
3504
3505void
3506i915_gem_object_unpin(struct drm_gem_object *obj)
3507{
3508 struct drm_device *dev = obj->dev;
3509 drm_i915_private_t *dev_priv = dev->dev_private;
3510 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3511
3512 i915_verify_inactive(dev, __FILE__, __LINE__);
3513 obj_priv->pin_count--;
3514 BUG_ON(obj_priv->pin_count < 0);
3515 BUG_ON(obj_priv->gtt_space == NULL);
3516
3517 /* If the object is no longer pinned, and is
3518 * neither active nor being flushed, then stick it on
3519 * the inactive list
3520 */
3521 if (obj_priv->pin_count == 0) {
3522 if (!obj_priv->active &&
3523 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
3524 I915_GEM_DOMAIN_GTT)) == 0)
3525 list_move_tail(&obj_priv->list,
3526 &dev_priv->mm.inactive_list);
3527 atomic_dec(&dev->pin_count);
3528 atomic_sub(obj->size, &dev->pin_memory);
3529 }
3530 i915_verify_inactive(dev, __FILE__, __LINE__);
3531}
3532
3533int
3534i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3535 struct drm_file *file_priv)
3536{
3537 struct drm_i915_gem_pin *args = data;
3538 struct drm_gem_object *obj;
3539 struct drm_i915_gem_object *obj_priv;
3540 int ret;
3541
3542 mutex_lock(&dev->struct_mutex);
3543
3544 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3545 if (obj == NULL) {
3546 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3547 args->handle);
3548 mutex_unlock(&dev->struct_mutex);
3549 return -EBADF;
3550 }
3551 obj_priv = obj->driver_private;
3552
Jesse Barnes79e53942008-11-07 14:24:08 -08003553 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3554 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3555 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00003556 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003557 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003558 return -EINVAL;
3559 }
3560
3561 obj_priv->user_pin_count++;
3562 obj_priv->pin_filp = file_priv;
3563 if (obj_priv->user_pin_count == 1) {
3564 ret = i915_gem_object_pin(obj, args->alignment);
3565 if (ret != 0) {
3566 drm_gem_object_unreference(obj);
3567 mutex_unlock(&dev->struct_mutex);
3568 return ret;
3569 }
Eric Anholt673a3942008-07-30 12:06:12 -07003570 }
3571
3572 /* XXX - flush the CPU caches for pinned objects
3573 * as the X server doesn't manage domains yet
3574 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003575 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003576 args->offset = obj_priv->gtt_offset;
3577 drm_gem_object_unreference(obj);
3578 mutex_unlock(&dev->struct_mutex);
3579
3580 return 0;
3581}
3582
3583int
3584i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3585 struct drm_file *file_priv)
3586{
3587 struct drm_i915_gem_pin *args = data;
3588 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08003589 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003590
3591 mutex_lock(&dev->struct_mutex);
3592
3593 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3594 if (obj == NULL) {
3595 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3596 args->handle);
3597 mutex_unlock(&dev->struct_mutex);
3598 return -EBADF;
3599 }
3600
Jesse Barnes79e53942008-11-07 14:24:08 -08003601 obj_priv = obj->driver_private;
3602 if (obj_priv->pin_filp != file_priv) {
3603 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3604 args->handle);
3605 drm_gem_object_unreference(obj);
3606 mutex_unlock(&dev->struct_mutex);
3607 return -EINVAL;
3608 }
3609 obj_priv->user_pin_count--;
3610 if (obj_priv->user_pin_count == 0) {
3611 obj_priv->pin_filp = NULL;
3612 i915_gem_object_unpin(obj);
3613 }
Eric Anholt673a3942008-07-30 12:06:12 -07003614
3615 drm_gem_object_unreference(obj);
3616 mutex_unlock(&dev->struct_mutex);
3617 return 0;
3618}
3619
3620int
3621i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3622 struct drm_file *file_priv)
3623{
3624 struct drm_i915_gem_busy *args = data;
3625 struct drm_gem_object *obj;
3626 struct drm_i915_gem_object *obj_priv;
3627
3628 mutex_lock(&dev->struct_mutex);
3629 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3630 if (obj == NULL) {
3631 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3632 args->handle);
3633 mutex_unlock(&dev->struct_mutex);
3634 return -EBADF;
3635 }
3636
Eric Anholtf21289b2009-02-18 09:44:56 -08003637 /* Update the active list for the hardware's current position.
3638 * Otherwise this only updates on a delayed timer or when irqs are
3639 * actually unmasked, and our working set ends up being larger than
3640 * required.
3641 */
3642 i915_gem_retire_requests(dev);
3643
Eric Anholt673a3942008-07-30 12:06:12 -07003644 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08003645 /* Don't count being on the flushing list against the object being
3646 * done. Otherwise, a buffer left on the flushing list but not getting
3647 * flushed (because nobody's flushing that domain) won't ever return
3648 * unbusy and get reused by libdrm's bo cache. The other expected
3649 * consumer of this interface, OpenGL's occlusion queries, also specs
3650 * that the objects get unbusy "eventually" without any interference.
3651 */
3652 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003653
3654 drm_gem_object_unreference(obj);
3655 mutex_unlock(&dev->struct_mutex);
3656 return 0;
3657}
3658
3659int
3660i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3661 struct drm_file *file_priv)
3662{
3663 return i915_gem_ring_throttle(dev, file_priv);
3664}
3665
3666int i915_gem_init_object(struct drm_gem_object *obj)
3667{
3668 struct drm_i915_gem_object *obj_priv;
3669
3670 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
3671 if (obj_priv == NULL)
3672 return -ENOMEM;
3673
3674 /*
3675 * We've just allocated pages from the kernel,
3676 * so they've just been written by the CPU with
3677 * zeros. They'll need to be clflushed before we
3678 * use them with the GPU.
3679 */
3680 obj->write_domain = I915_GEM_DOMAIN_CPU;
3681 obj->read_domains = I915_GEM_DOMAIN_CPU;
3682
Keith Packardba1eb1d2008-10-14 19:55:10 -07003683 obj_priv->agp_type = AGP_USER_MEMORY;
3684
Eric Anholt673a3942008-07-30 12:06:12 -07003685 obj->driver_private = obj_priv;
3686 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003687 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07003688 INIT_LIST_HEAD(&obj_priv->list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003689
Eric Anholt673a3942008-07-30 12:06:12 -07003690 return 0;
3691}
3692
3693void i915_gem_free_object(struct drm_gem_object *obj)
3694{
Jesse Barnesde151cf2008-11-12 10:03:55 -08003695 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003696 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3697
3698 while (obj_priv->pin_count > 0)
3699 i915_gem_object_unpin(obj);
3700
Dave Airlie71acb5e2008-12-30 20:31:46 +10003701 if (obj_priv->phys_obj)
3702 i915_gem_detach_phys_object(dev, obj);
3703
Eric Anholt673a3942008-07-30 12:06:12 -07003704 i915_gem_object_unbind(obj);
3705
Jesse Barnesab00b3e2009-02-11 14:01:46 -08003706 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003707
Eric Anholt673a3942008-07-30 12:06:12 -07003708 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
Eric Anholt280b7132009-03-12 16:56:27 -07003709 kfree(obj_priv->bit_17);
Eric Anholt673a3942008-07-30 12:06:12 -07003710 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
3711}
3712
Eric Anholt673a3942008-07-30 12:06:12 -07003713/** Unbinds all objects that are on the given buffer list. */
3714static int
3715i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3716{
3717 struct drm_gem_object *obj;
3718 struct drm_i915_gem_object *obj_priv;
3719 int ret;
3720
3721 while (!list_empty(head)) {
3722 obj_priv = list_first_entry(head,
3723 struct drm_i915_gem_object,
3724 list);
3725 obj = obj_priv->obj;
3726
3727 if (obj_priv->pin_count != 0) {
3728 DRM_ERROR("Pinned object in unbind list\n");
3729 mutex_unlock(&dev->struct_mutex);
3730 return -EINVAL;
3731 }
3732
3733 ret = i915_gem_object_unbind(obj);
3734 if (ret != 0) {
3735 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3736 ret);
3737 mutex_unlock(&dev->struct_mutex);
3738 return ret;
3739 }
3740 }
3741
3742
3743 return 0;
3744}
3745
Jesse Barnes5669fca2009-02-17 15:13:31 -08003746int
Eric Anholt673a3942008-07-30 12:06:12 -07003747i915_gem_idle(struct drm_device *dev)
3748{
3749 drm_i915_private_t *dev_priv = dev->dev_private;
3750 uint32_t seqno, cur_seqno, last_seqno;
3751 int stuck, ret;
3752
Keith Packard6dbe2772008-10-14 21:41:13 -07003753 mutex_lock(&dev->struct_mutex);
3754
3755 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3756 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003757 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003758 }
Eric Anholt673a3942008-07-30 12:06:12 -07003759
3760 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3761 * We need to replace this with a semaphore, or something.
3762 */
3763 dev_priv->mm.suspended = 1;
3764
Keith Packard6dbe2772008-10-14 21:41:13 -07003765 /* Cancel the retire work handler, wait for it to finish if running
3766 */
3767 mutex_unlock(&dev->struct_mutex);
3768 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3769 mutex_lock(&dev->struct_mutex);
3770
Eric Anholt673a3942008-07-30 12:06:12 -07003771 i915_kernel_lost_context(dev);
3772
3773 /* Flush the GPU along with all non-CPU write domains
3774 */
3775 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
3776 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
Jesse Barnesde151cf2008-11-12 10:03:55 -08003777 seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003778
3779 if (seqno == 0) {
3780 mutex_unlock(&dev->struct_mutex);
3781 return -ENOMEM;
3782 }
3783
3784 dev_priv->mm.waiting_gem_seqno = seqno;
3785 last_seqno = 0;
3786 stuck = 0;
3787 for (;;) {
3788 cur_seqno = i915_get_gem_seqno(dev);
3789 if (i915_seqno_passed(cur_seqno, seqno))
3790 break;
3791 if (last_seqno == cur_seqno) {
3792 if (stuck++ > 100) {
3793 DRM_ERROR("hardware wedged\n");
3794 dev_priv->mm.wedged = 1;
3795 DRM_WAKEUP(&dev_priv->irq_queue);
3796 break;
3797 }
3798 }
3799 msleep(10);
3800 last_seqno = cur_seqno;
3801 }
3802 dev_priv->mm.waiting_gem_seqno = 0;
3803
3804 i915_gem_retire_requests(dev);
3805
Carl Worth5e118f42009-03-20 11:54:25 -07003806 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003807 if (!dev_priv->mm.wedged) {
3808 /* Active and flushing should now be empty as we've
3809 * waited for a sequence higher than any pending execbuffer
3810 */
3811 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3812 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3813 /* Request should now be empty as we've also waited
3814 * for the last request in the list
3815 */
3816 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3817 }
Eric Anholt673a3942008-07-30 12:06:12 -07003818
Eric Anholt28dfe522008-11-13 15:00:55 -08003819 /* Empty the active and flushing lists to inactive. If there's
3820 * anything left at this point, it means that we're wedged and
3821 * nothing good's going to happen by leaving them there. So strip
3822 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003823 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003824 while (!list_empty(&dev_priv->mm.active_list)) {
3825 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003826
Eric Anholt28dfe522008-11-13 15:00:55 -08003827 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3828 struct drm_i915_gem_object,
3829 list);
3830 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3831 i915_gem_object_move_to_inactive(obj_priv->obj);
3832 }
Carl Worth5e118f42009-03-20 11:54:25 -07003833 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003834
3835 while (!list_empty(&dev_priv->mm.flushing_list)) {
3836 struct drm_i915_gem_object *obj_priv;
3837
Eric Anholt151903d2008-12-01 10:23:21 +10003838 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003839 struct drm_i915_gem_object,
3840 list);
3841 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3842 i915_gem_object_move_to_inactive(obj_priv->obj);
3843 }
3844
3845
3846 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003847 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003848 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003849 if (ret) {
3850 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003851 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003852 }
Eric Anholt673a3942008-07-30 12:06:12 -07003853
Keith Packard6dbe2772008-10-14 21:41:13 -07003854 i915_gem_cleanup_ringbuffer(dev);
3855 mutex_unlock(&dev->struct_mutex);
3856
Eric Anholt673a3942008-07-30 12:06:12 -07003857 return 0;
3858}
3859
3860static int
3861i915_gem_init_hws(struct drm_device *dev)
3862{
3863 drm_i915_private_t *dev_priv = dev->dev_private;
3864 struct drm_gem_object *obj;
3865 struct drm_i915_gem_object *obj_priv;
3866 int ret;
3867
3868 /* If we need a physical address for the status page, it's already
3869 * initialized at driver load time.
3870 */
3871 if (!I915_NEED_GFX_HWS(dev))
3872 return 0;
3873
3874 obj = drm_gem_object_alloc(dev, 4096);
3875 if (obj == NULL) {
3876 DRM_ERROR("Failed to allocate status page\n");
3877 return -ENOMEM;
3878 }
3879 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07003880 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07003881
3882 ret = i915_gem_object_pin(obj, 4096);
3883 if (ret != 0) {
3884 drm_gem_object_unreference(obj);
3885 return ret;
3886 }
3887
3888 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003889
Eric Anholt856fa192009-03-19 14:10:50 -07003890 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07003891 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003892 DRM_ERROR("Failed to map status page.\n");
3893 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00003894 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003895 drm_gem_object_unreference(obj);
3896 return -EINVAL;
3897 }
3898 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003899 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
3900 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07003901 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07003902 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
3903
3904 return 0;
3905}
3906
Chris Wilson85a7bb92009-02-11 14:52:44 +00003907static void
3908i915_gem_cleanup_hws(struct drm_device *dev)
3909{
3910 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003911 struct drm_gem_object *obj;
3912 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00003913
3914 if (dev_priv->hws_obj == NULL)
3915 return;
3916
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003917 obj = dev_priv->hws_obj;
3918 obj_priv = obj->driver_private;
3919
Eric Anholt856fa192009-03-19 14:10:50 -07003920 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00003921 i915_gem_object_unpin(obj);
3922 drm_gem_object_unreference(obj);
3923 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003924
Chris Wilson85a7bb92009-02-11 14:52:44 +00003925 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3926 dev_priv->hw_status_page = NULL;
3927
3928 /* Write high address into HWS_PGA when disabling. */
3929 I915_WRITE(HWS_PGA, 0x1ffff000);
3930}
3931
Jesse Barnes79e53942008-11-07 14:24:08 -08003932int
Eric Anholt673a3942008-07-30 12:06:12 -07003933i915_gem_init_ringbuffer(struct drm_device *dev)
3934{
3935 drm_i915_private_t *dev_priv = dev->dev_private;
3936 struct drm_gem_object *obj;
3937 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08003938 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07003939 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07003940 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07003941
3942 ret = i915_gem_init_hws(dev);
3943 if (ret != 0)
3944 return ret;
3945
3946 obj = drm_gem_object_alloc(dev, 128 * 1024);
3947 if (obj == NULL) {
3948 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00003949 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003950 return -ENOMEM;
3951 }
3952 obj_priv = obj->driver_private;
3953
3954 ret = i915_gem_object_pin(obj, 4096);
3955 if (ret != 0) {
3956 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00003957 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003958 return ret;
3959 }
3960
3961 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08003962 ring->Size = obj->size;
3963 ring->tail_mask = obj->size - 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003964
Jesse Barnes79e53942008-11-07 14:24:08 -08003965 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
3966 ring->map.size = obj->size;
3967 ring->map.type = 0;
3968 ring->map.flags = 0;
3969 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003970
Jesse Barnes79e53942008-11-07 14:24:08 -08003971 drm_core_ioremap_wc(&ring->map, dev);
3972 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003973 DRM_ERROR("Failed to map ringbuffer.\n");
3974 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00003975 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003976 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00003977 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003978 return -EINVAL;
3979 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003980 ring->ring_obj = obj;
3981 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07003982
3983 /* Stop the ring if it's running. */
3984 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003985 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07003986 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003987
3988 /* Initialize the ring. */
3989 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07003990 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3991
3992 /* G45 ring initialization fails to reset head to zero */
3993 if (head != 0) {
3994 DRM_ERROR("Ring head not reset to zero "
3995 "ctl %08x head %08x tail %08x start %08x\n",
3996 I915_READ(PRB0_CTL),
3997 I915_READ(PRB0_HEAD),
3998 I915_READ(PRB0_TAIL),
3999 I915_READ(PRB0_START));
4000 I915_WRITE(PRB0_HEAD, 0);
4001
4002 DRM_ERROR("Ring head forced to zero "
4003 "ctl %08x head %08x tail %08x start %08x\n",
4004 I915_READ(PRB0_CTL),
4005 I915_READ(PRB0_HEAD),
4006 I915_READ(PRB0_TAIL),
4007 I915_READ(PRB0_START));
4008 }
4009
Eric Anholt673a3942008-07-30 12:06:12 -07004010 I915_WRITE(PRB0_CTL,
4011 ((obj->size - 4096) & RING_NR_PAGES) |
4012 RING_NO_REPORT |
4013 RING_VALID);
4014
Keith Packard50aa253d2008-10-14 17:20:35 -07004015 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4016
4017 /* If the head is still not zero, the ring is dead */
4018 if (head != 0) {
4019 DRM_ERROR("Ring initialization failed "
4020 "ctl %08x head %08x tail %08x start %08x\n",
4021 I915_READ(PRB0_CTL),
4022 I915_READ(PRB0_HEAD),
4023 I915_READ(PRB0_TAIL),
4024 I915_READ(PRB0_START));
4025 return -EIO;
4026 }
4027
Eric Anholt673a3942008-07-30 12:06:12 -07004028 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004029 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4030 i915_kernel_lost_context(dev);
4031 else {
4032 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4033 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4034 ring->space = ring->head - (ring->tail + 8);
4035 if (ring->space < 0)
4036 ring->space += ring->Size;
4037 }
Eric Anholt673a3942008-07-30 12:06:12 -07004038
4039 return 0;
4040}
4041
Jesse Barnes79e53942008-11-07 14:24:08 -08004042void
Eric Anholt673a3942008-07-30 12:06:12 -07004043i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4044{
4045 drm_i915_private_t *dev_priv = dev->dev_private;
4046
4047 if (dev_priv->ring.ring_obj == NULL)
4048 return;
4049
4050 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4051
4052 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4053 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4054 dev_priv->ring.ring_obj = NULL;
4055 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4056
Chris Wilson85a7bb92009-02-11 14:52:44 +00004057 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004058}
4059
4060int
4061i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4062 struct drm_file *file_priv)
4063{
4064 drm_i915_private_t *dev_priv = dev->dev_private;
4065 int ret;
4066
Jesse Barnes79e53942008-11-07 14:24:08 -08004067 if (drm_core_check_feature(dev, DRIVER_MODESET))
4068 return 0;
4069
Eric Anholt673a3942008-07-30 12:06:12 -07004070 if (dev_priv->mm.wedged) {
4071 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4072 dev_priv->mm.wedged = 0;
4073 }
4074
Eric Anholt673a3942008-07-30 12:06:12 -07004075 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004076 dev_priv->mm.suspended = 0;
4077
4078 ret = i915_gem_init_ringbuffer(dev);
4079 if (ret != 0)
4080 return ret;
4081
Carl Worth5e118f42009-03-20 11:54:25 -07004082 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004083 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004084 spin_unlock(&dev_priv->mm.active_list_lock);
4085
Eric Anholt673a3942008-07-30 12:06:12 -07004086 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4087 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4088 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004089 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004090
4091 drm_irq_install(dev);
4092
Eric Anholt673a3942008-07-30 12:06:12 -07004093 return 0;
4094}
4095
4096int
4097i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4098 struct drm_file *file_priv)
4099{
4100 int ret;
4101
Jesse Barnes79e53942008-11-07 14:24:08 -08004102 if (drm_core_check_feature(dev, DRIVER_MODESET))
4103 return 0;
4104
Eric Anholt673a3942008-07-30 12:06:12 -07004105 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004106 drm_irq_uninstall(dev);
4107
Keith Packard6dbe2772008-10-14 21:41:13 -07004108 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004109}
4110
4111void
4112i915_gem_lastclose(struct drm_device *dev)
4113{
4114 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004115
Eric Anholte806b492009-01-22 09:56:58 -08004116 if (drm_core_check_feature(dev, DRIVER_MODESET))
4117 return;
4118
Keith Packard6dbe2772008-10-14 21:41:13 -07004119 ret = i915_gem_idle(dev);
4120 if (ret)
4121 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004122}
4123
4124void
4125i915_gem_load(struct drm_device *dev)
4126{
4127 drm_i915_private_t *dev_priv = dev->dev_private;
4128
Carl Worth5e118f42009-03-20 11:54:25 -07004129 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004130 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4131 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4132 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4133 INIT_LIST_HEAD(&dev_priv->mm.request_list);
4134 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4135 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004136 dev_priv->mm.next_gem_seqno = 1;
4137
Jesse Barnesde151cf2008-11-12 10:03:55 -08004138 /* Old X drivers will take 0-2 for front, back, depth buffers */
4139 dev_priv->fence_reg_start = 3;
4140
Jesse Barnes0f973f22009-01-26 17:10:45 -08004141 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004142 dev_priv->num_fence_regs = 16;
4143 else
4144 dev_priv->num_fence_regs = 8;
4145
Eric Anholt673a3942008-07-30 12:06:12 -07004146 i915_gem_detect_bit_6_swizzle(dev);
4147}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004148
4149/*
4150 * Create a physically contiguous memory object for this object
4151 * e.g. for cursor + overlay regs
4152 */
4153int i915_gem_init_phys_object(struct drm_device *dev,
4154 int id, int size)
4155{
4156 drm_i915_private_t *dev_priv = dev->dev_private;
4157 struct drm_i915_gem_phys_object *phys_obj;
4158 int ret;
4159
4160 if (dev_priv->mm.phys_objs[id - 1] || !size)
4161 return 0;
4162
4163 phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4164 if (!phys_obj)
4165 return -ENOMEM;
4166
4167 phys_obj->id = id;
4168
4169 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4170 if (!phys_obj->handle) {
4171 ret = -ENOMEM;
4172 goto kfree_obj;
4173 }
4174#ifdef CONFIG_X86
4175 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4176#endif
4177
4178 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4179
4180 return 0;
4181kfree_obj:
4182 drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4183 return ret;
4184}
4185
4186void i915_gem_free_phys_object(struct drm_device *dev, int id)
4187{
4188 drm_i915_private_t *dev_priv = dev->dev_private;
4189 struct drm_i915_gem_phys_object *phys_obj;
4190
4191 if (!dev_priv->mm.phys_objs[id - 1])
4192 return;
4193
4194 phys_obj = dev_priv->mm.phys_objs[id - 1];
4195 if (phys_obj->cur_obj) {
4196 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4197 }
4198
4199#ifdef CONFIG_X86
4200 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4201#endif
4202 drm_pci_free(dev, phys_obj->handle);
4203 kfree(phys_obj);
4204 dev_priv->mm.phys_objs[id - 1] = NULL;
4205}
4206
4207void i915_gem_free_all_phys_object(struct drm_device *dev)
4208{
4209 int i;
4210
Dave Airlie260883c2009-01-22 17:58:49 +10004211 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004212 i915_gem_free_phys_object(dev, i);
4213}
4214
4215void i915_gem_detach_phys_object(struct drm_device *dev,
4216 struct drm_gem_object *obj)
4217{
4218 struct drm_i915_gem_object *obj_priv;
4219 int i;
4220 int ret;
4221 int page_count;
4222
4223 obj_priv = obj->driver_private;
4224 if (!obj_priv->phys_obj)
4225 return;
4226
Eric Anholt856fa192009-03-19 14:10:50 -07004227 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004228 if (ret)
4229 goto out;
4230
4231 page_count = obj->size / PAGE_SIZE;
4232
4233 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004234 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004235 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4236
4237 memcpy(dst, src, PAGE_SIZE);
4238 kunmap_atomic(dst, KM_USER0);
4239 }
Eric Anholt856fa192009-03-19 14:10:50 -07004240 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004241 drm_agp_chipset_flush(dev);
4242out:
4243 obj_priv->phys_obj->cur_obj = NULL;
4244 obj_priv->phys_obj = NULL;
4245}
4246
4247int
4248i915_gem_attach_phys_object(struct drm_device *dev,
4249 struct drm_gem_object *obj, int id)
4250{
4251 drm_i915_private_t *dev_priv = dev->dev_private;
4252 struct drm_i915_gem_object *obj_priv;
4253 int ret = 0;
4254 int page_count;
4255 int i;
4256
4257 if (id > I915_MAX_PHYS_OBJECT)
4258 return -EINVAL;
4259
4260 obj_priv = obj->driver_private;
4261
4262 if (obj_priv->phys_obj) {
4263 if (obj_priv->phys_obj->id == id)
4264 return 0;
4265 i915_gem_detach_phys_object(dev, obj);
4266 }
4267
4268
4269 /* create a new object */
4270 if (!dev_priv->mm.phys_objs[id - 1]) {
4271 ret = i915_gem_init_phys_object(dev, id,
4272 obj->size);
4273 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004274 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004275 goto out;
4276 }
4277 }
4278
4279 /* bind to the object */
4280 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4281 obj_priv->phys_obj->cur_obj = obj;
4282
Eric Anholt856fa192009-03-19 14:10:50 -07004283 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004284 if (ret) {
4285 DRM_ERROR("failed to get page list\n");
4286 goto out;
4287 }
4288
4289 page_count = obj->size / PAGE_SIZE;
4290
4291 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004292 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004293 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4294
4295 memcpy(dst, src, PAGE_SIZE);
4296 kunmap_atomic(src, KM_USER0);
4297 }
4298
4299 return 0;
4300out:
4301 return ret;
4302}
4303
4304static int
4305i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4306 struct drm_i915_gem_pwrite *args,
4307 struct drm_file *file_priv)
4308{
4309 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4310 void *obj_addr;
4311 int ret;
4312 char __user *user_data;
4313
4314 user_data = (char __user *) (uintptr_t) args->data_ptr;
4315 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4316
Dave Airliee08fb4f2009-02-25 14:52:30 +10004317 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004318 ret = copy_from_user(obj_addr, user_data, args->size);
4319 if (ret)
4320 return -EFAULT;
4321
4322 drm_agp_chipset_flush(dev);
4323 return 0;
4324}