blob: 3fbd8a0c40d124a2a47171e60467336c3a158c9f [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
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700352 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700353 if (user_pages == NULL)
354 return -ENOMEM;
355
356 down_read(&mm->mmap_sem);
357 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700358 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700359 up_read(&mm->mmap_sem);
360 if (pinned_pages < num_pages) {
361 ret = -EFAULT;
362 goto fail_put_user_pages;
363 }
364
Eric Anholt280b7132009-03-12 16:56:27 -0700365 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
366
Eric Anholteb014592009-03-10 11:44:52 -0700367 mutex_lock(&dev->struct_mutex);
368
369 ret = i915_gem_object_get_pages(obj);
370 if (ret != 0)
371 goto fail_unlock;
372
373 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
374 args->size);
375 if (ret != 0)
376 goto fail_put_pages;
377
378 obj_priv = obj->driver_private;
379 offset = args->offset;
380
381 while (remain > 0) {
382 /* Operation in this page
383 *
384 * shmem_page_index = page number within shmem file
385 * shmem_page_offset = offset within page in shmem file
386 * data_page_index = page number in get_user_pages return
387 * data_page_offset = offset with data_page_index page.
388 * page_length = bytes to copy for this page
389 */
390 shmem_page_index = offset / PAGE_SIZE;
391 shmem_page_offset = offset & ~PAGE_MASK;
392 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
393 data_page_offset = data_ptr & ~PAGE_MASK;
394
395 page_length = remain;
396 if ((shmem_page_offset + page_length) > PAGE_SIZE)
397 page_length = PAGE_SIZE - shmem_page_offset;
398 if ((data_page_offset + page_length) > PAGE_SIZE)
399 page_length = PAGE_SIZE - data_page_offset;
400
Eric Anholt280b7132009-03-12 16:56:27 -0700401 if (do_bit17_swizzling) {
402 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
403 shmem_page_offset,
404 user_pages[data_page_index],
405 data_page_offset,
406 page_length,
407 1);
408 } else {
409 ret = slow_shmem_copy(user_pages[data_page_index],
410 data_page_offset,
411 obj_priv->pages[shmem_page_index],
412 shmem_page_offset,
413 page_length);
414 }
Eric Anholteb014592009-03-10 11:44:52 -0700415 if (ret)
416 goto fail_put_pages;
417
418 remain -= page_length;
419 data_ptr += page_length;
420 offset += page_length;
421 }
422
423fail_put_pages:
424 i915_gem_object_put_pages(obj);
425fail_unlock:
426 mutex_unlock(&dev->struct_mutex);
427fail_put_user_pages:
428 for (i = 0; i < pinned_pages; i++) {
429 SetPageDirty(user_pages[i]);
430 page_cache_release(user_pages[i]);
431 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700432 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700433
434 return ret;
435}
436
Eric Anholt673a3942008-07-30 12:06:12 -0700437/**
438 * Reads data from the object referenced by handle.
439 *
440 * On error, the contents of *data are undefined.
441 */
442int
443i915_gem_pread_ioctl(struct drm_device *dev, void *data,
444 struct drm_file *file_priv)
445{
446 struct drm_i915_gem_pread *args = data;
447 struct drm_gem_object *obj;
448 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700449 int ret;
450
451 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
452 if (obj == NULL)
453 return -EBADF;
454 obj_priv = obj->driver_private;
455
456 /* Bounds check source.
457 *
458 * XXX: This could use review for overflow issues...
459 */
460 if (args->offset > obj->size || args->size > obj->size ||
461 args->offset + args->size > obj->size) {
462 drm_gem_object_unreference(obj);
463 return -EINVAL;
464 }
465
Eric Anholt280b7132009-03-12 16:56:27 -0700466 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700467 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700468 } else {
469 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
470 if (ret != 0)
471 ret = i915_gem_shmem_pread_slow(dev, obj, args,
472 file_priv);
473 }
Eric Anholt673a3942008-07-30 12:06:12 -0700474
475 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700476
Eric Anholteb014592009-03-10 11:44:52 -0700477 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700478}
479
Keith Packard0839ccb2008-10-30 19:38:48 -0700480/* This is the fast write path which cannot handle
481 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700482 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700483
Keith Packard0839ccb2008-10-30 19:38:48 -0700484static inline int
485fast_user_write(struct io_mapping *mapping,
486 loff_t page_base, int page_offset,
487 char __user *user_data,
488 int length)
489{
490 char *vaddr_atomic;
491 unsigned long unwritten;
492
493 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
494 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
495 user_data, length);
496 io_mapping_unmap_atomic(vaddr_atomic);
497 if (unwritten)
498 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700499 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700500}
501
502/* Here's the write path which can sleep for
503 * page faults
504 */
505
506static inline int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700507slow_kernel_write(struct io_mapping *mapping,
508 loff_t gtt_base, int gtt_offset,
509 struct page *user_page, int user_offset,
510 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700511{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700512 char *src_vaddr, *dst_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700513 unsigned long unwritten;
514
Eric Anholt3de09aa2009-03-09 09:42:23 -0700515 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
516 src_vaddr = kmap_atomic(user_page, KM_USER1);
517 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
518 src_vaddr + user_offset,
519 length);
520 kunmap_atomic(src_vaddr, KM_USER1);
521 io_mapping_unmap_atomic(dst_vaddr);
Keith Packard0839ccb2008-10-30 19:38:48 -0700522 if (unwritten)
523 return -EFAULT;
524 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700525}
526
Eric Anholt40123c12009-03-09 13:42:30 -0700527static inline int
528fast_shmem_write(struct page **pages,
529 loff_t page_base, int page_offset,
530 char __user *data,
531 int length)
532{
533 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400534 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700535
536 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
537 if (vaddr == NULL)
538 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400539 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700540 kunmap_atomic(vaddr, KM_USER0);
541
Dave Airlied0088772009-03-28 20:29:48 -0400542 if (unwritten)
543 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700544 return 0;
545}
546
Eric Anholt3de09aa2009-03-09 09:42:23 -0700547/**
548 * This is the fast pwrite path, where we copy the data directly from the
549 * user into the GTT, uncached.
550 */
Eric Anholt673a3942008-07-30 12:06:12 -0700551static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700552i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
553 struct drm_i915_gem_pwrite *args,
554 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700555{
556 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Keith Packard0839ccb2008-10-30 19:38:48 -0700557 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700558 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700559 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700560 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700561 int page_offset, page_length;
562 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700563
564 user_data = (char __user *) (uintptr_t) args->data_ptr;
565 remain = args->size;
566 if (!access_ok(VERIFY_READ, user_data, remain))
567 return -EFAULT;
568
569
570 mutex_lock(&dev->struct_mutex);
571 ret = i915_gem_object_pin(obj, 0);
572 if (ret) {
573 mutex_unlock(&dev->struct_mutex);
574 return ret;
575 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800576 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700577 if (ret)
578 goto fail;
579
580 obj_priv = obj->driver_private;
581 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700582
583 while (remain > 0) {
584 /* Operation in this page
585 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700586 * page_base = page offset within aperture
587 * page_offset = offset within page
588 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700589 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700590 page_base = (offset & ~(PAGE_SIZE-1));
591 page_offset = offset & (PAGE_SIZE-1);
592 page_length = remain;
593 if ((page_offset + remain) > PAGE_SIZE)
594 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700595
Keith Packard0839ccb2008-10-30 19:38:48 -0700596 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
597 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700598
Keith Packard0839ccb2008-10-30 19:38:48 -0700599 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700600 * source page isn't available. Return the error and we'll
601 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700602 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700603 if (ret)
604 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700605
Keith Packard0839ccb2008-10-30 19:38:48 -0700606 remain -= page_length;
607 user_data += page_length;
608 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700609 }
Eric Anholt673a3942008-07-30 12:06:12 -0700610
611fail:
612 i915_gem_object_unpin(obj);
613 mutex_unlock(&dev->struct_mutex);
614
615 return ret;
616}
617
Eric Anholt3de09aa2009-03-09 09:42:23 -0700618/**
619 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
620 * the memory and maps it using kmap_atomic for copying.
621 *
622 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
623 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
624 */
Eric Anholt3043c602008-10-02 12:24:47 -0700625static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700626i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
627 struct drm_i915_gem_pwrite *args,
628 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700629{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700630 struct drm_i915_gem_object *obj_priv = obj->driver_private;
631 drm_i915_private_t *dev_priv = dev->dev_private;
632 ssize_t remain;
633 loff_t gtt_page_base, offset;
634 loff_t first_data_page, last_data_page, num_pages;
635 loff_t pinned_pages, i;
636 struct page **user_pages;
637 struct mm_struct *mm = current->mm;
638 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700639 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700640 uint64_t data_ptr = args->data_ptr;
641
642 remain = args->size;
643
644 /* Pin the user pages containing the data. We can't fault while
645 * holding the struct mutex, and all of the pwrite implementations
646 * want to hold it while dereferencing the user data.
647 */
648 first_data_page = data_ptr / PAGE_SIZE;
649 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
650 num_pages = last_data_page - first_data_page + 1;
651
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700652 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700653 if (user_pages == NULL)
654 return -ENOMEM;
655
656 down_read(&mm->mmap_sem);
657 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
658 num_pages, 0, 0, user_pages, NULL);
659 up_read(&mm->mmap_sem);
660 if (pinned_pages < num_pages) {
661 ret = -EFAULT;
662 goto out_unpin_pages;
663 }
664
665 mutex_lock(&dev->struct_mutex);
666 ret = i915_gem_object_pin(obj, 0);
667 if (ret)
668 goto out_unlock;
669
670 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
671 if (ret)
672 goto out_unpin_object;
673
674 obj_priv = obj->driver_private;
675 offset = obj_priv->gtt_offset + args->offset;
676
677 while (remain > 0) {
678 /* Operation in this page
679 *
680 * gtt_page_base = page offset within aperture
681 * gtt_page_offset = offset within page in aperture
682 * data_page_index = page number in get_user_pages return
683 * data_page_offset = offset with data_page_index page.
684 * page_length = bytes to copy for this page
685 */
686 gtt_page_base = offset & PAGE_MASK;
687 gtt_page_offset = offset & ~PAGE_MASK;
688 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
689 data_page_offset = data_ptr & ~PAGE_MASK;
690
691 page_length = remain;
692 if ((gtt_page_offset + page_length) > PAGE_SIZE)
693 page_length = PAGE_SIZE - gtt_page_offset;
694 if ((data_page_offset + page_length) > PAGE_SIZE)
695 page_length = PAGE_SIZE - data_page_offset;
696
697 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
698 gtt_page_base, gtt_page_offset,
699 user_pages[data_page_index],
700 data_page_offset,
701 page_length);
702
703 /* If we get a fault while copying data, then (presumably) our
704 * source page isn't available. Return the error and we'll
705 * retry in the slow path.
706 */
707 if (ret)
708 goto out_unpin_object;
709
710 remain -= page_length;
711 offset += page_length;
712 data_ptr += page_length;
713 }
714
715out_unpin_object:
716 i915_gem_object_unpin(obj);
717out_unlock:
718 mutex_unlock(&dev->struct_mutex);
719out_unpin_pages:
720 for (i = 0; i < pinned_pages; i++)
721 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700722 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700723
724 return ret;
725}
726
Eric Anholt40123c12009-03-09 13:42:30 -0700727/**
728 * This is the fast shmem pwrite path, which attempts to directly
729 * copy_from_user into the kmapped pages backing the object.
730 */
Eric Anholt673a3942008-07-30 12:06:12 -0700731static int
Eric Anholt40123c12009-03-09 13:42:30 -0700732i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
733 struct drm_i915_gem_pwrite *args,
734 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700735{
Eric Anholt40123c12009-03-09 13:42:30 -0700736 struct drm_i915_gem_object *obj_priv = obj->driver_private;
737 ssize_t remain;
738 loff_t offset, page_base;
739 char __user *user_data;
740 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700741 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700742
743 user_data = (char __user *) (uintptr_t) args->data_ptr;
744 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700745
746 mutex_lock(&dev->struct_mutex);
747
Eric Anholt40123c12009-03-09 13:42:30 -0700748 ret = i915_gem_object_get_pages(obj);
749 if (ret != 0)
750 goto fail_unlock;
751
Eric Anholte47c68e2008-11-14 13:35:19 -0800752 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700753 if (ret != 0)
754 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700755
Eric Anholt40123c12009-03-09 13:42:30 -0700756 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700757 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700758 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700759
Eric Anholt40123c12009-03-09 13:42:30 -0700760 while (remain > 0) {
761 /* Operation in this page
762 *
763 * page_base = page offset within aperture
764 * page_offset = offset within page
765 * page_length = bytes to copy for this page
766 */
767 page_base = (offset & ~(PAGE_SIZE-1));
768 page_offset = offset & (PAGE_SIZE-1);
769 page_length = remain;
770 if ((page_offset + remain) > PAGE_SIZE)
771 page_length = PAGE_SIZE - page_offset;
772
773 ret = fast_shmem_write(obj_priv->pages,
774 page_base, page_offset,
775 user_data, page_length);
776 if (ret)
777 goto fail_put_pages;
778
779 remain -= page_length;
780 user_data += page_length;
781 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700782 }
783
Eric Anholt40123c12009-03-09 13:42:30 -0700784fail_put_pages:
785 i915_gem_object_put_pages(obj);
786fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700787 mutex_unlock(&dev->struct_mutex);
788
Eric Anholt40123c12009-03-09 13:42:30 -0700789 return ret;
790}
791
792/**
793 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
794 * the memory and maps it using kmap_atomic for copying.
795 *
796 * This avoids taking mmap_sem for faulting on the user's address while the
797 * struct_mutex is held.
798 */
799static int
800i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
801 struct drm_i915_gem_pwrite *args,
802 struct drm_file *file_priv)
803{
804 struct drm_i915_gem_object *obj_priv = obj->driver_private;
805 struct mm_struct *mm = current->mm;
806 struct page **user_pages;
807 ssize_t remain;
808 loff_t offset, pinned_pages, i;
809 loff_t first_data_page, last_data_page, num_pages;
810 int shmem_page_index, shmem_page_offset;
811 int data_page_index, data_page_offset;
812 int page_length;
813 int ret;
814 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700815 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700816
817 remain = args->size;
818
819 /* Pin the user pages containing the data. We can't fault while
820 * holding the struct mutex, and all of the pwrite implementations
821 * want to hold it while dereferencing the user data.
822 */
823 first_data_page = data_ptr / PAGE_SIZE;
824 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
825 num_pages = last_data_page - first_data_page + 1;
826
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700827 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700828 if (user_pages == NULL)
829 return -ENOMEM;
830
831 down_read(&mm->mmap_sem);
832 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
833 num_pages, 0, 0, user_pages, NULL);
834 up_read(&mm->mmap_sem);
835 if (pinned_pages < num_pages) {
836 ret = -EFAULT;
837 goto fail_put_user_pages;
838 }
839
Eric Anholt280b7132009-03-12 16:56:27 -0700840 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
841
Eric Anholt40123c12009-03-09 13:42:30 -0700842 mutex_lock(&dev->struct_mutex);
843
844 ret = i915_gem_object_get_pages(obj);
845 if (ret != 0)
846 goto fail_unlock;
847
848 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
849 if (ret != 0)
850 goto fail_put_pages;
851
852 obj_priv = obj->driver_private;
853 offset = args->offset;
854 obj_priv->dirty = 1;
855
856 while (remain > 0) {
857 /* Operation in this page
858 *
859 * shmem_page_index = page number within shmem file
860 * shmem_page_offset = offset within page in shmem file
861 * data_page_index = page number in get_user_pages return
862 * data_page_offset = offset with data_page_index page.
863 * page_length = bytes to copy for this page
864 */
865 shmem_page_index = offset / PAGE_SIZE;
866 shmem_page_offset = offset & ~PAGE_MASK;
867 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
868 data_page_offset = data_ptr & ~PAGE_MASK;
869
870 page_length = remain;
871 if ((shmem_page_offset + page_length) > PAGE_SIZE)
872 page_length = PAGE_SIZE - shmem_page_offset;
873 if ((data_page_offset + page_length) > PAGE_SIZE)
874 page_length = PAGE_SIZE - data_page_offset;
875
Eric Anholt280b7132009-03-12 16:56:27 -0700876 if (do_bit17_swizzling) {
877 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
878 shmem_page_offset,
879 user_pages[data_page_index],
880 data_page_offset,
881 page_length,
882 0);
883 } else {
884 ret = slow_shmem_copy(obj_priv->pages[shmem_page_index],
885 shmem_page_offset,
886 user_pages[data_page_index],
887 data_page_offset,
888 page_length);
889 }
Eric Anholt40123c12009-03-09 13:42:30 -0700890 if (ret)
891 goto fail_put_pages;
892
893 remain -= page_length;
894 data_ptr += page_length;
895 offset += page_length;
896 }
897
898fail_put_pages:
899 i915_gem_object_put_pages(obj);
900fail_unlock:
901 mutex_unlock(&dev->struct_mutex);
902fail_put_user_pages:
903 for (i = 0; i < pinned_pages; i++)
904 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700905 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700906
907 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700908}
909
910/**
911 * Writes data to the object referenced by handle.
912 *
913 * On error, the contents of the buffer that were to be modified are undefined.
914 */
915int
916i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
917 struct drm_file *file_priv)
918{
919 struct drm_i915_gem_pwrite *args = data;
920 struct drm_gem_object *obj;
921 struct drm_i915_gem_object *obj_priv;
922 int ret = 0;
923
924 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
925 if (obj == NULL)
926 return -EBADF;
927 obj_priv = obj->driver_private;
928
929 /* Bounds check destination.
930 *
931 * XXX: This could use review for overflow issues...
932 */
933 if (args->offset > obj->size || args->size > obj->size ||
934 args->offset + args->size > obj->size) {
935 drm_gem_object_unreference(obj);
936 return -EINVAL;
937 }
938
939 /* We can only do the GTT pwrite on untiled buffers, as otherwise
940 * it would end up going through the fenced access, and we'll get
941 * different detiling behavior between reading and writing.
942 * pread/pwrite currently are reading and writing from the CPU
943 * perspective, requiring manual detiling by the client.
944 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000945 if (obj_priv->phys_obj)
946 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
947 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Eric Anholt3de09aa2009-03-09 09:42:23 -0700948 dev->gtt_total != 0) {
949 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
950 if (ret == -EFAULT) {
951 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
952 file_priv);
953 }
Eric Anholt280b7132009-03-12 16:56:27 -0700954 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
955 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700956 } else {
957 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
958 if (ret == -EFAULT) {
959 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
960 file_priv);
961 }
962 }
Eric Anholt673a3942008-07-30 12:06:12 -0700963
964#if WATCH_PWRITE
965 if (ret)
966 DRM_INFO("pwrite failed %d\n", ret);
967#endif
968
969 drm_gem_object_unreference(obj);
970
971 return ret;
972}
973
974/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800975 * Called when user space prepares to use an object with the CPU, either
976 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700977 */
978int
979i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
980 struct drm_file *file_priv)
981{
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 }
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001148
1149 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1150 if (ret) {
1151 mutex_unlock(&dev->struct_mutex);
1152 return VM_FAULT_SIGBUS;
1153 }
1154
Jesse Barnes14b603912009-05-20 16:47:08 -04001155 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001156 }
1157
1158 /* Need a new fence register? */
1159 if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001160 obj_priv->tiling_mode != I915_TILING_NONE) {
Jesse Barnes0f973f22009-01-26 17:10:45 -08001161 ret = i915_gem_object_get_fence_reg(obj, write);
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001162 if (ret) {
1163 mutex_unlock(&dev->struct_mutex);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001164 return VM_FAULT_SIGBUS;
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001165 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001166 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001167
1168 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1169 page_offset;
1170
1171 /* Finally, remap it using the new GTT offset */
1172 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1173
1174 mutex_unlock(&dev->struct_mutex);
1175
1176 switch (ret) {
1177 case -ENOMEM:
1178 case -EAGAIN:
1179 return VM_FAULT_OOM;
1180 case -EFAULT:
Jesse Barnes959b8872009-03-20 14:16:33 -07001181 case -EINVAL:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001182 return VM_FAULT_SIGBUS;
1183 default:
1184 return VM_FAULT_NOPAGE;
1185 }
1186}
1187
1188/**
1189 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1190 * @obj: obj in question
1191 *
1192 * GEM memory mapping works by handing back to userspace a fake mmap offset
1193 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1194 * up the object based on the offset and sets up the various memory mapping
1195 * structures.
1196 *
1197 * This routine allocates and attaches a fake offset for @obj.
1198 */
1199static int
1200i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1201{
1202 struct drm_device *dev = obj->dev;
1203 struct drm_gem_mm *mm = dev->mm_private;
1204 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1205 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001206 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001207 int ret = 0;
1208
1209 /* Set the object up for mmap'ing */
1210 list = &obj->map_list;
1211 list->map = drm_calloc(1, sizeof(struct drm_map_list),
1212 DRM_MEM_DRIVER);
1213 if (!list->map)
1214 return -ENOMEM;
1215
1216 map = list->map;
1217 map->type = _DRM_GEM;
1218 map->size = obj->size;
1219 map->handle = obj;
1220
1221 /* Get a DRM GEM mmap offset allocated... */
1222 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1223 obj->size / PAGE_SIZE, 0, 0);
1224 if (!list->file_offset_node) {
1225 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1226 ret = -ENOMEM;
1227 goto out_free_list;
1228 }
1229
1230 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1231 obj->size / PAGE_SIZE, 0);
1232 if (!list->file_offset_node) {
1233 ret = -ENOMEM;
1234 goto out_free_list;
1235 }
1236
1237 list->hash.key = list->file_offset_node->start;
1238 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1239 DRM_ERROR("failed to add to map hash\n");
1240 goto out_free_mm;
1241 }
1242
1243 /* By now we should be all set, any drm_mmap request on the offset
1244 * below will get to our mmap & fault handler */
1245 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1246
1247 return 0;
1248
1249out_free_mm:
1250 drm_mm_put_block(list->file_offset_node);
1251out_free_list:
1252 drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
1253
1254 return ret;
1255}
1256
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001257static void
1258i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1259{
1260 struct drm_device *dev = obj->dev;
1261 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1262 struct drm_gem_mm *mm = dev->mm_private;
1263 struct drm_map_list *list;
1264
1265 list = &obj->map_list;
1266 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1267
1268 if (list->file_offset_node) {
1269 drm_mm_put_block(list->file_offset_node);
1270 list->file_offset_node = NULL;
1271 }
1272
1273 if (list->map) {
1274 drm_free(list->map, sizeof(struct drm_map), DRM_MEM_DRIVER);
1275 list->map = NULL;
1276 }
1277
1278 obj_priv->mmap_offset = 0;
1279}
1280
Jesse Barnesde151cf2008-11-12 10:03:55 -08001281/**
1282 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1283 * @obj: object to check
1284 *
1285 * Return the required GTT alignment for an object, taking into account
1286 * potential fence register mapping if needed.
1287 */
1288static uint32_t
1289i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1290{
1291 struct drm_device *dev = obj->dev;
1292 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1293 int start, i;
1294
1295 /*
1296 * Minimum alignment is 4k (GTT page size), but might be greater
1297 * if a fence register is needed for the object.
1298 */
1299 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1300 return 4096;
1301
1302 /*
1303 * Previous chips need to be aligned to the size of the smallest
1304 * fence register that can contain the object.
1305 */
1306 if (IS_I9XX(dev))
1307 start = 1024*1024;
1308 else
1309 start = 512*1024;
1310
1311 for (i = start; i < obj->size; i <<= 1)
1312 ;
1313
1314 return i;
1315}
1316
1317/**
1318 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1319 * @dev: DRM device
1320 * @data: GTT mapping ioctl data
1321 * @file_priv: GEM object info
1322 *
1323 * Simply returns the fake offset to userspace so it can mmap it.
1324 * The mmap call will end up in drm_gem_mmap(), which will set things
1325 * up so we can get faults in the handler above.
1326 *
1327 * The fault handler will take care of binding the object into the GTT
1328 * (since it may have been evicted to make room for something), allocating
1329 * a fence register, and mapping the appropriate aperture address into
1330 * userspace.
1331 */
1332int
1333i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1334 struct drm_file *file_priv)
1335{
1336 struct drm_i915_gem_mmap_gtt *args = data;
1337 struct drm_i915_private *dev_priv = dev->dev_private;
1338 struct drm_gem_object *obj;
1339 struct drm_i915_gem_object *obj_priv;
1340 int ret;
1341
1342 if (!(dev->driver->driver_features & DRIVER_GEM))
1343 return -ENODEV;
1344
1345 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1346 if (obj == NULL)
1347 return -EBADF;
1348
1349 mutex_lock(&dev->struct_mutex);
1350
1351 obj_priv = obj->driver_private;
1352
1353 if (!obj_priv->mmap_offset) {
1354 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001355 if (ret) {
1356 drm_gem_object_unreference(obj);
1357 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001358 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001359 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001360 }
1361
1362 args->offset = obj_priv->mmap_offset;
1363
1364 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
1365
1366 /* Make sure the alignment is correct for fence regs etc */
1367 if (obj_priv->agp_mem &&
1368 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
1369 drm_gem_object_unreference(obj);
1370 mutex_unlock(&dev->struct_mutex);
1371 return -EINVAL;
1372 }
1373
1374 /*
1375 * Pull it into the GTT so that we have a page list (makes the
1376 * initial fault faster and any subsequent flushing possible).
1377 */
1378 if (!obj_priv->agp_mem) {
1379 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1380 if (ret) {
1381 drm_gem_object_unreference(obj);
1382 mutex_unlock(&dev->struct_mutex);
1383 return ret;
1384 }
Jesse Barnes14b603912009-05-20 16:47:08 -04001385 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001386 }
1387
1388 drm_gem_object_unreference(obj);
1389 mutex_unlock(&dev->struct_mutex);
1390
1391 return 0;
1392}
1393
Ben Gamari6911a9b2009-04-02 11:24:54 -07001394void
Eric Anholt856fa192009-03-19 14:10:50 -07001395i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001396{
1397 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1398 int page_count = obj->size / PAGE_SIZE;
1399 int i;
1400
Eric Anholt856fa192009-03-19 14:10:50 -07001401 BUG_ON(obj_priv->pages_refcount == 0);
1402
1403 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001404 return;
1405
Eric Anholt280b7132009-03-12 16:56:27 -07001406 if (obj_priv->tiling_mode != I915_TILING_NONE)
1407 i915_gem_object_save_bit_17_swizzle(obj);
1408
Eric Anholt673a3942008-07-30 12:06:12 -07001409 for (i = 0; i < page_count; i++)
Eric Anholt856fa192009-03-19 14:10:50 -07001410 if (obj_priv->pages[i] != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07001411 if (obj_priv->dirty)
Eric Anholt856fa192009-03-19 14:10:50 -07001412 set_page_dirty(obj_priv->pages[i]);
1413 mark_page_accessed(obj_priv->pages[i]);
1414 page_cache_release(obj_priv->pages[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07001415 }
1416 obj_priv->dirty = 0;
1417
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001418 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001419 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001420}
1421
1422static void
Eric Anholtce44b0e2008-11-06 16:00:31 -08001423i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001424{
1425 struct drm_device *dev = obj->dev;
1426 drm_i915_private_t *dev_priv = dev->dev_private;
1427 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1428
1429 /* Add a reference if we're newly entering the active list. */
1430 if (!obj_priv->active) {
1431 drm_gem_object_reference(obj);
1432 obj_priv->active = 1;
1433 }
1434 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001435 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001436 list_move_tail(&obj_priv->list,
1437 &dev_priv->mm.active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001438 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001439 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001440}
1441
Eric Anholtce44b0e2008-11-06 16:00:31 -08001442static void
1443i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1444{
1445 struct drm_device *dev = obj->dev;
1446 drm_i915_private_t *dev_priv = dev->dev_private;
1447 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1448
1449 BUG_ON(!obj_priv->active);
1450 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1451 obj_priv->last_rendering_seqno = 0;
1452}
Eric Anholt673a3942008-07-30 12:06:12 -07001453
1454static void
1455i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1456{
1457 struct drm_device *dev = obj->dev;
1458 drm_i915_private_t *dev_priv = dev->dev_private;
1459 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1460
1461 i915_verify_inactive(dev, __FILE__, __LINE__);
1462 if (obj_priv->pin_count != 0)
1463 list_del_init(&obj_priv->list);
1464 else
1465 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1466
Eric Anholtce44b0e2008-11-06 16:00:31 -08001467 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001468 if (obj_priv->active) {
1469 obj_priv->active = 0;
1470 drm_gem_object_unreference(obj);
1471 }
1472 i915_verify_inactive(dev, __FILE__, __LINE__);
1473}
1474
1475/**
1476 * Creates a new sequence number, emitting a write of it to the status page
1477 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1478 *
1479 * Must be called with struct_lock held.
1480 *
1481 * Returned sequence numbers are nonzero on success.
1482 */
1483static uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001484i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
1485 uint32_t flush_domains)
Eric Anholt673a3942008-07-30 12:06:12 -07001486{
1487 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001488 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001489 struct drm_i915_gem_request *request;
1490 uint32_t seqno;
1491 int was_empty;
1492 RING_LOCALS;
1493
Eric Anholtb9624422009-06-03 07:27:35 +00001494 if (file_priv != NULL)
1495 i915_file_priv = file_priv->driver_priv;
1496
Eric Anholt673a3942008-07-30 12:06:12 -07001497 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
1498 if (request == NULL)
1499 return 0;
1500
1501 /* Grab the seqno we're going to make this request be, and bump the
1502 * next (skipping 0 so it can be the reserved no-seqno value).
1503 */
1504 seqno = dev_priv->mm.next_gem_seqno;
1505 dev_priv->mm.next_gem_seqno++;
1506 if (dev_priv->mm.next_gem_seqno == 0)
1507 dev_priv->mm.next_gem_seqno++;
1508
1509 BEGIN_LP_RING(4);
1510 OUT_RING(MI_STORE_DWORD_INDEX);
1511 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1512 OUT_RING(seqno);
1513
1514 OUT_RING(MI_USER_INTERRUPT);
1515 ADVANCE_LP_RING();
1516
1517 DRM_DEBUG("%d\n", seqno);
1518
1519 request->seqno = seqno;
1520 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -07001521 was_empty = list_empty(&dev_priv->mm.request_list);
1522 list_add_tail(&request->list, &dev_priv->mm.request_list);
Eric Anholtb9624422009-06-03 07:27:35 +00001523 if (i915_file_priv) {
1524 list_add_tail(&request->client_list,
1525 &i915_file_priv->mm.request_list);
1526 } else {
1527 INIT_LIST_HEAD(&request->client_list);
1528 }
Eric Anholt673a3942008-07-30 12:06:12 -07001529
Eric Anholtce44b0e2008-11-06 16:00:31 -08001530 /* Associate any objects on the flushing list matching the write
1531 * domain we're flushing with our flush.
1532 */
1533 if (flush_domains != 0) {
1534 struct drm_i915_gem_object *obj_priv, *next;
1535
1536 list_for_each_entry_safe(obj_priv, next,
1537 &dev_priv->mm.flushing_list, list) {
1538 struct drm_gem_object *obj = obj_priv->obj;
1539
1540 if ((obj->write_domain & flush_domains) ==
1541 obj->write_domain) {
1542 obj->write_domain = 0;
1543 i915_gem_object_move_to_active(obj, seqno);
1544 }
1545 }
1546
1547 }
1548
Keith Packard6dbe2772008-10-14 21:41:13 -07001549 if (was_empty && !dev_priv->mm.suspended)
Eric Anholt673a3942008-07-30 12:06:12 -07001550 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1551 return seqno;
1552}
1553
1554/**
1555 * Command execution barrier
1556 *
1557 * Ensures that all commands in the ring are finished
1558 * before signalling the CPU
1559 */
Eric Anholt3043c602008-10-02 12:24:47 -07001560static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -07001561i915_retire_commands(struct drm_device *dev)
1562{
1563 drm_i915_private_t *dev_priv = dev->dev_private;
1564 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1565 uint32_t flush_domains = 0;
1566 RING_LOCALS;
1567
1568 /* The sampler always gets flushed on i965 (sigh) */
1569 if (IS_I965G(dev))
1570 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1571 BEGIN_LP_RING(2);
1572 OUT_RING(cmd);
1573 OUT_RING(0); /* noop */
1574 ADVANCE_LP_RING();
1575 return flush_domains;
1576}
1577
1578/**
1579 * Moves buffers associated only with the given active seqno from the active
1580 * to inactive list, potentially freeing them.
1581 */
1582static void
1583i915_gem_retire_request(struct drm_device *dev,
1584 struct drm_i915_gem_request *request)
1585{
1586 drm_i915_private_t *dev_priv = dev->dev_private;
1587
1588 /* Move any buffers on the active list that are no longer referenced
1589 * by the ringbuffer to the flushing/inactive lists as appropriate.
1590 */
Carl Worth5e118f42009-03-20 11:54:25 -07001591 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001592 while (!list_empty(&dev_priv->mm.active_list)) {
1593 struct drm_gem_object *obj;
1594 struct drm_i915_gem_object *obj_priv;
1595
1596 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1597 struct drm_i915_gem_object,
1598 list);
1599 obj = obj_priv->obj;
1600
1601 /* If the seqno being retired doesn't match the oldest in the
1602 * list, then the oldest in the list must still be newer than
1603 * this seqno.
1604 */
1605 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001606 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001607
Eric Anholt673a3942008-07-30 12:06:12 -07001608#if WATCH_LRU
1609 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1610 __func__, request->seqno, obj);
1611#endif
1612
Eric Anholtce44b0e2008-11-06 16:00:31 -08001613 if (obj->write_domain != 0)
1614 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001615 else {
1616 /* Take a reference on the object so it won't be
1617 * freed while the spinlock is held. The list
1618 * protection for this spinlock is safe when breaking
1619 * the lock like this since the next thing we do
1620 * is just get the head of the list again.
1621 */
1622 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001623 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001624 spin_unlock(&dev_priv->mm.active_list_lock);
1625 drm_gem_object_unreference(obj);
1626 spin_lock(&dev_priv->mm.active_list_lock);
1627 }
Eric Anholt673a3942008-07-30 12:06:12 -07001628 }
Carl Worth5e118f42009-03-20 11:54:25 -07001629out:
1630 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001631}
1632
1633/**
1634 * Returns true if seq1 is later than seq2.
1635 */
1636static int
1637i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1638{
1639 return (int32_t)(seq1 - seq2) >= 0;
1640}
1641
1642uint32_t
1643i915_get_gem_seqno(struct drm_device *dev)
1644{
1645 drm_i915_private_t *dev_priv = dev->dev_private;
1646
1647 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1648}
1649
1650/**
1651 * This function clears the request list as sequence numbers are passed.
1652 */
1653void
1654i915_gem_retire_requests(struct drm_device *dev)
1655{
1656 drm_i915_private_t *dev_priv = dev->dev_private;
1657 uint32_t seqno;
1658
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001659 if (!dev_priv->hw_status_page)
1660 return;
1661
Eric Anholt673a3942008-07-30 12:06:12 -07001662 seqno = i915_get_gem_seqno(dev);
1663
1664 while (!list_empty(&dev_priv->mm.request_list)) {
1665 struct drm_i915_gem_request *request;
1666 uint32_t retiring_seqno;
1667
1668 request = list_first_entry(&dev_priv->mm.request_list,
1669 struct drm_i915_gem_request,
1670 list);
1671 retiring_seqno = request->seqno;
1672
1673 if (i915_seqno_passed(seqno, retiring_seqno) ||
1674 dev_priv->mm.wedged) {
1675 i915_gem_retire_request(dev, request);
1676
1677 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001678 list_del(&request->client_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001679 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
1680 } else
1681 break;
1682 }
1683}
1684
1685void
1686i915_gem_retire_work_handler(struct work_struct *work)
1687{
1688 drm_i915_private_t *dev_priv;
1689 struct drm_device *dev;
1690
1691 dev_priv = container_of(work, drm_i915_private_t,
1692 mm.retire_work.work);
1693 dev = dev_priv->dev;
1694
1695 mutex_lock(&dev->struct_mutex);
1696 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001697 if (!dev_priv->mm.suspended &&
1698 !list_empty(&dev_priv->mm.request_list))
Eric Anholt673a3942008-07-30 12:06:12 -07001699 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1700 mutex_unlock(&dev->struct_mutex);
1701}
1702
1703/**
1704 * Waits for a sequence number to be signaled, and cleans up the
1705 * request and object lists appropriately for that event.
1706 */
Eric Anholt3043c602008-10-02 12:24:47 -07001707static int
Eric Anholt673a3942008-07-30 12:06:12 -07001708i915_wait_request(struct drm_device *dev, uint32_t seqno)
1709{
1710 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001711 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001712 int ret = 0;
1713
1714 BUG_ON(seqno == 0);
1715
1716 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001717 ier = I915_READ(IER);
1718 if (!ier) {
1719 DRM_ERROR("something (likely vbetool) disabled "
1720 "interrupts, re-enabling\n");
1721 i915_driver_irq_preinstall(dev);
1722 i915_driver_irq_postinstall(dev);
1723 }
1724
Eric Anholt673a3942008-07-30 12:06:12 -07001725 dev_priv->mm.waiting_gem_seqno = seqno;
1726 i915_user_irq_get(dev);
1727 ret = wait_event_interruptible(dev_priv->irq_queue,
1728 i915_seqno_passed(i915_get_gem_seqno(dev),
1729 seqno) ||
1730 dev_priv->mm.wedged);
1731 i915_user_irq_put(dev);
1732 dev_priv->mm.waiting_gem_seqno = 0;
1733 }
1734 if (dev_priv->mm.wedged)
1735 ret = -EIO;
1736
1737 if (ret && ret != -ERESTARTSYS)
1738 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1739 __func__, ret, seqno, i915_get_gem_seqno(dev));
1740
1741 /* Directly dispatch request retiring. While we have the work queue
1742 * to handle this, the waiter on a request often wants an associated
1743 * buffer to have made it to the inactive list, and we would need
1744 * a separate wait queue to handle that.
1745 */
1746 if (ret == 0)
1747 i915_gem_retire_requests(dev);
1748
1749 return ret;
1750}
1751
1752static void
1753i915_gem_flush(struct drm_device *dev,
1754 uint32_t invalidate_domains,
1755 uint32_t flush_domains)
1756{
1757 drm_i915_private_t *dev_priv = dev->dev_private;
1758 uint32_t cmd;
1759 RING_LOCALS;
1760
1761#if WATCH_EXEC
1762 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1763 invalidate_domains, flush_domains);
1764#endif
1765
1766 if (flush_domains & I915_GEM_DOMAIN_CPU)
1767 drm_agp_chipset_flush(dev);
1768
1769 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
1770 I915_GEM_DOMAIN_GTT)) {
1771 /*
1772 * read/write caches:
1773 *
1774 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1775 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1776 * also flushed at 2d versus 3d pipeline switches.
1777 *
1778 * read-only caches:
1779 *
1780 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1781 * MI_READ_FLUSH is set, and is always flushed on 965.
1782 *
1783 * I915_GEM_DOMAIN_COMMAND may not exist?
1784 *
1785 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1786 * invalidated when MI_EXE_FLUSH is set.
1787 *
1788 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1789 * invalidated with every MI_FLUSH.
1790 *
1791 * TLBs:
1792 *
1793 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1794 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1795 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1796 * are flushed at any MI_FLUSH.
1797 */
1798
1799 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1800 if ((invalidate_domains|flush_domains) &
1801 I915_GEM_DOMAIN_RENDER)
1802 cmd &= ~MI_NO_WRITE_FLUSH;
1803 if (!IS_I965G(dev)) {
1804 /*
1805 * On the 965, the sampler cache always gets flushed
1806 * and this bit is reserved.
1807 */
1808 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1809 cmd |= MI_READ_FLUSH;
1810 }
1811 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1812 cmd |= MI_EXE_FLUSH;
1813
1814#if WATCH_EXEC
1815 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1816#endif
1817 BEGIN_LP_RING(2);
1818 OUT_RING(cmd);
1819 OUT_RING(0); /* noop */
1820 ADVANCE_LP_RING();
1821 }
1822}
1823
1824/**
1825 * Ensures that all rendering to the object has completed and the object is
1826 * safe to unbind from the GTT or access from the CPU.
1827 */
1828static int
1829i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1830{
1831 struct drm_device *dev = obj->dev;
1832 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1833 int ret;
1834
Eric Anholte47c68e2008-11-14 13:35:19 -08001835 /* This function only exists to support waiting for existing rendering,
1836 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001837 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001838 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001839
1840 /* If there is rendering queued on the buffer being evicted, wait for
1841 * it.
1842 */
1843 if (obj_priv->active) {
1844#if WATCH_BUF
1845 DRM_INFO("%s: object %p wait for seqno %08x\n",
1846 __func__, obj, obj_priv->last_rendering_seqno);
1847#endif
1848 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1849 if (ret != 0)
1850 return ret;
1851 }
1852
1853 return 0;
1854}
1855
1856/**
1857 * Unbinds an object from the GTT aperture.
1858 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001859int
Eric Anholt673a3942008-07-30 12:06:12 -07001860i915_gem_object_unbind(struct drm_gem_object *obj)
1861{
1862 struct drm_device *dev = obj->dev;
1863 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001864 loff_t offset;
Eric Anholt673a3942008-07-30 12:06:12 -07001865 int ret = 0;
1866
1867#if WATCH_BUF
1868 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1869 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1870#endif
1871 if (obj_priv->gtt_space == NULL)
1872 return 0;
1873
1874 if (obj_priv->pin_count != 0) {
1875 DRM_ERROR("Attempting to unbind pinned buffer\n");
1876 return -EINVAL;
1877 }
1878
Eric Anholt673a3942008-07-30 12:06:12 -07001879 /* Move the object to the CPU domain to ensure that
1880 * any possible CPU writes while it's not in the GTT
1881 * are flushed when we go to remap it. This will
1882 * also ensure that all pending GPU writes are finished
1883 * before we unbind.
1884 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001885 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001886 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001887 if (ret != -ERESTARTSYS)
1888 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001889 return ret;
1890 }
1891
1892 if (obj_priv->agp_mem != NULL) {
1893 drm_unbind_agp(obj_priv->agp_mem);
1894 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1895 obj_priv->agp_mem = NULL;
1896 }
1897
1898 BUG_ON(obj_priv->active);
1899
Jesse Barnesde151cf2008-11-12 10:03:55 -08001900 /* blow away mappings if mapped through GTT */
1901 offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08001902 if (dev->dev_mapping)
1903 unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001904
1905 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1906 i915_gem_clear_fence_reg(obj);
1907
Eric Anholt856fa192009-03-19 14:10:50 -07001908 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001909
1910 if (obj_priv->gtt_space) {
1911 atomic_dec(&dev->gtt_count);
1912 atomic_sub(obj->size, &dev->gtt_memory);
1913
1914 drm_mm_put_block(obj_priv->gtt_space);
1915 obj_priv->gtt_space = NULL;
1916 }
1917
1918 /* Remove ourselves from the LRU list if present. */
1919 if (!list_empty(&obj_priv->list))
1920 list_del_init(&obj_priv->list);
1921
1922 return 0;
1923}
1924
1925static int
1926i915_gem_evict_something(struct drm_device *dev)
1927{
1928 drm_i915_private_t *dev_priv = dev->dev_private;
1929 struct drm_gem_object *obj;
1930 struct drm_i915_gem_object *obj_priv;
1931 int ret = 0;
1932
1933 for (;;) {
1934 /* If there's an inactive buffer available now, grab it
1935 * and be done.
1936 */
1937 if (!list_empty(&dev_priv->mm.inactive_list)) {
1938 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1939 struct drm_i915_gem_object,
1940 list);
1941 obj = obj_priv->obj;
1942 BUG_ON(obj_priv->pin_count != 0);
1943#if WATCH_LRU
1944 DRM_INFO("%s: evicting %p\n", __func__, obj);
1945#endif
1946 BUG_ON(obj_priv->active);
1947
1948 /* Wait on the rendering and unbind the buffer. */
1949 ret = i915_gem_object_unbind(obj);
1950 break;
1951 }
1952
1953 /* If we didn't get anything, but the ring is still processing
1954 * things, wait for one of those things to finish and hopefully
1955 * leave us a buffer to evict.
1956 */
1957 if (!list_empty(&dev_priv->mm.request_list)) {
1958 struct drm_i915_gem_request *request;
1959
1960 request = list_first_entry(&dev_priv->mm.request_list,
1961 struct drm_i915_gem_request,
1962 list);
1963
1964 ret = i915_wait_request(dev, request->seqno);
1965 if (ret)
1966 break;
1967
1968 /* if waiting caused an object to become inactive,
1969 * then loop around and wait for it. Otherwise, we
1970 * assume that waiting freed and unbound something,
1971 * so there should now be some space in the GTT
1972 */
1973 if (!list_empty(&dev_priv->mm.inactive_list))
1974 continue;
1975 break;
1976 }
1977
1978 /* If we didn't have anything on the request list but there
1979 * are buffers awaiting a flush, emit one and try again.
1980 * When we wait on it, those buffers waiting for that flush
1981 * will get moved to inactive.
1982 */
1983 if (!list_empty(&dev_priv->mm.flushing_list)) {
1984 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1985 struct drm_i915_gem_object,
1986 list);
1987 obj = obj_priv->obj;
1988
1989 i915_gem_flush(dev,
1990 obj->write_domain,
1991 obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00001992 i915_add_request(dev, NULL, obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001993
1994 obj = NULL;
1995 continue;
1996 }
1997
1998 DRM_ERROR("inactive empty %d request empty %d "
1999 "flushing empty %d\n",
2000 list_empty(&dev_priv->mm.inactive_list),
2001 list_empty(&dev_priv->mm.request_list),
2002 list_empty(&dev_priv->mm.flushing_list));
2003 /* If we didn't do any of the above, there's nothing to be done
2004 * and we just can't fit it in.
2005 */
2006 return -ENOMEM;
2007 }
2008 return ret;
2009}
2010
2011static int
Keith Packardac94a962008-11-20 23:30:27 -08002012i915_gem_evict_everything(struct drm_device *dev)
2013{
2014 int ret;
2015
2016 for (;;) {
2017 ret = i915_gem_evict_something(dev);
2018 if (ret != 0)
2019 break;
2020 }
Owain Ainsworth15c35332008-12-06 20:42:20 -08002021 if (ret == -ENOMEM)
2022 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08002023 return ret;
2024}
2025
Ben Gamari6911a9b2009-04-02 11:24:54 -07002026int
Eric Anholt856fa192009-03-19 14:10:50 -07002027i915_gem_object_get_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002028{
2029 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2030 int page_count, i;
2031 struct address_space *mapping;
2032 struct inode *inode;
2033 struct page *page;
2034 int ret;
2035
Eric Anholt856fa192009-03-19 14:10:50 -07002036 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002037 return 0;
2038
2039 /* Get the list of pages out of our struct file. They'll be pinned
2040 * at this point until we release them.
2041 */
2042 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002043 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002044 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002045 if (obj_priv->pages == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002046 DRM_ERROR("Faled to allocate page list\n");
Eric Anholt856fa192009-03-19 14:10:50 -07002047 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002048 return -ENOMEM;
2049 }
2050
2051 inode = obj->filp->f_path.dentry->d_inode;
2052 mapping = inode->i_mapping;
2053 for (i = 0; i < page_count; i++) {
2054 page = read_mapping_page(mapping, i, NULL);
2055 if (IS_ERR(page)) {
2056 ret = PTR_ERR(page);
2057 DRM_ERROR("read_mapping_page failed: %d\n", ret);
Eric Anholt856fa192009-03-19 14:10:50 -07002058 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002059 return ret;
2060 }
Eric Anholt856fa192009-03-19 14:10:50 -07002061 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002062 }
Eric Anholt280b7132009-03-12 16:56:27 -07002063
2064 if (obj_priv->tiling_mode != I915_TILING_NONE)
2065 i915_gem_object_do_bit_17_swizzle(obj);
2066
Eric Anholt673a3942008-07-30 12:06:12 -07002067 return 0;
2068}
2069
Jesse Barnesde151cf2008-11-12 10:03:55 -08002070static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2071{
2072 struct drm_gem_object *obj = reg->obj;
2073 struct drm_device *dev = obj->dev;
2074 drm_i915_private_t *dev_priv = dev->dev_private;
2075 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2076 int regnum = obj_priv->fence_reg;
2077 uint64_t val;
2078
2079 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2080 0xfffff000) << 32;
2081 val |= obj_priv->gtt_offset & 0xfffff000;
2082 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2083 if (obj_priv->tiling_mode == I915_TILING_Y)
2084 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2085 val |= I965_FENCE_REG_VALID;
2086
2087 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2088}
2089
2090static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2091{
2092 struct drm_gem_object *obj = reg->obj;
2093 struct drm_device *dev = obj->dev;
2094 drm_i915_private_t *dev_priv = dev->dev_private;
2095 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2096 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002097 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002098 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002099 uint32_t pitch_val;
2100
2101 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2102 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002103 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002104 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002105 return;
2106 }
2107
Jesse Barnes0f973f22009-01-26 17:10:45 -08002108 if (obj_priv->tiling_mode == I915_TILING_Y &&
2109 HAS_128_BYTE_Y_TILING(dev))
2110 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002111 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002112 tile_width = 512;
2113
2114 /* Note: pitch better be a power of two tile widths */
2115 pitch_val = obj_priv->stride / tile_width;
2116 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002117
2118 val = obj_priv->gtt_offset;
2119 if (obj_priv->tiling_mode == I915_TILING_Y)
2120 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2121 val |= I915_FENCE_SIZE_BITS(obj->size);
2122 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2123 val |= I830_FENCE_REG_VALID;
2124
Eric Anholtdc529a42009-03-10 22:34:49 -07002125 if (regnum < 8)
2126 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2127 else
2128 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2129 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002130}
2131
2132static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2133{
2134 struct drm_gem_object *obj = reg->obj;
2135 struct drm_device *dev = obj->dev;
2136 drm_i915_private_t *dev_priv = dev->dev_private;
2137 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2138 int regnum = obj_priv->fence_reg;
2139 uint32_t val;
2140 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002141 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002142
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002143 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002144 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002145 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002146 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002147 return;
2148 }
2149
Eric Anholte76a16d2009-05-26 17:44:56 -07002150 pitch_val = obj_priv->stride / 128;
2151 pitch_val = ffs(pitch_val) - 1;
2152 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2153
Jesse Barnesde151cf2008-11-12 10:03:55 -08002154 val = obj_priv->gtt_offset;
2155 if (obj_priv->tiling_mode == I915_TILING_Y)
2156 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002157 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2158 WARN_ON(fence_size_bits & ~0x00000f00);
2159 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002160 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2161 val |= I830_FENCE_REG_VALID;
2162
2163 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
2164
2165}
2166
2167/**
2168 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2169 * @obj: object to map through a fence reg
Jesse Barnes0f973f22009-01-26 17:10:45 -08002170 * @write: object is about to be written
Jesse Barnesde151cf2008-11-12 10:03:55 -08002171 *
2172 * When mapping objects through the GTT, userspace wants to be able to write
2173 * to them without having to worry about swizzling if the object is tiled.
2174 *
2175 * This function walks the fence regs looking for a free one for @obj,
2176 * stealing one if it can't find any.
2177 *
2178 * It then sets up the reg based on the object's properties: address, pitch
2179 * and tiling format.
2180 */
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002181static int
Jesse Barnes0f973f22009-01-26 17:10:45 -08002182i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002183{
2184 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002185 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002186 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2187 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002188 struct drm_i915_gem_object *old_obj_priv = NULL;
2189 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002190
2191 switch (obj_priv->tiling_mode) {
2192 case I915_TILING_NONE:
2193 WARN(1, "allocating a fence for non-tiled object?\n");
2194 break;
2195 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002196 if (!obj_priv->stride)
2197 return -EINVAL;
2198 WARN((obj_priv->stride & (512 - 1)),
2199 "object 0x%08x is X tiled but has non-512B pitch\n",
2200 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002201 break;
2202 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002203 if (!obj_priv->stride)
2204 return -EINVAL;
2205 WARN((obj_priv->stride & (128 - 1)),
2206 "object 0x%08x is Y tiled but has non-128B pitch\n",
2207 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002208 break;
2209 }
2210
2211 /* First try to find a free reg */
Chris Wilson9b2412f2009-02-11 14:26:44 +00002212try_again:
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002213 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002214 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2215 reg = &dev_priv->fence_regs[i];
2216 if (!reg->obj)
2217 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002218
2219 old_obj_priv = reg->obj->driver_private;
2220 if (!old_obj_priv->pin_count)
2221 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002222 }
2223
2224 /* None available, try to steal one or wait for a user to finish */
2225 if (i == dev_priv->num_fence_regs) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002226 uint32_t seqno = dev_priv->mm.next_gem_seqno;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002227 loff_t offset;
2228
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002229 if (avail == 0)
2230 return -ENOMEM;
2231
Jesse Barnesde151cf2008-11-12 10:03:55 -08002232 for (i = dev_priv->fence_reg_start;
2233 i < dev_priv->num_fence_regs; i++) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002234 uint32_t this_seqno;
2235
Jesse Barnesde151cf2008-11-12 10:03:55 -08002236 reg = &dev_priv->fence_regs[i];
2237 old_obj_priv = reg->obj->driver_private;
Chris Wilsond7619c42009-02-11 14:26:47 +00002238
2239 if (old_obj_priv->pin_count)
2240 continue;
2241
2242 /* i915 uses fences for GPU access to tiled buffers */
2243 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002244 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002245
2246 /* find the seqno of the first available fence */
2247 this_seqno = old_obj_priv->last_rendering_seqno;
2248 if (this_seqno != 0 &&
2249 reg->obj->write_domain == 0 &&
2250 i915_seqno_passed(seqno, this_seqno))
2251 seqno = this_seqno;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002252 }
2253
2254 /*
2255 * Now things get ugly... we have to wait for one of the
2256 * objects to finish before trying again.
2257 */
2258 if (i == dev_priv->num_fence_regs) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002259 if (seqno == dev_priv->mm.next_gem_seqno) {
2260 i915_gem_flush(dev,
2261 I915_GEM_GPU_DOMAINS,
2262 I915_GEM_GPU_DOMAINS);
Eric Anholtb9624422009-06-03 07:27:35 +00002263 seqno = i915_add_request(dev, NULL,
Chris Wilsond7619c42009-02-11 14:26:47 +00002264 I915_GEM_GPU_DOMAINS);
2265 if (seqno == 0)
2266 return -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002267 }
Chris Wilsond7619c42009-02-11 14:26:47 +00002268
2269 ret = i915_wait_request(dev, seqno);
2270 if (ret)
2271 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002272 goto try_again;
2273 }
2274
2275 /*
2276 * Zap this virtual mapping so we can set up a fence again
2277 * for this object next time we need it.
2278 */
2279 offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08002280 if (dev->dev_mapping)
2281 unmap_mapping_range(dev->dev_mapping, offset,
2282 reg->obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002283 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
2284 }
2285
2286 obj_priv->fence_reg = i;
2287 reg->obj = obj;
2288
2289 if (IS_I965G(dev))
2290 i965_write_fence_reg(reg);
2291 else if (IS_I9XX(dev))
2292 i915_write_fence_reg(reg);
2293 else
2294 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002295
2296 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002297}
2298
2299/**
2300 * i915_gem_clear_fence_reg - clear out fence register info
2301 * @obj: object to clear
2302 *
2303 * Zeroes out the fence register itself and clears out the associated
2304 * data structures in dev_priv and obj_priv.
2305 */
2306static void
2307i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2308{
2309 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002310 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002311 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2312
2313 if (IS_I965G(dev))
2314 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002315 else {
2316 uint32_t fence_reg;
2317
2318 if (obj_priv->fence_reg < 8)
2319 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2320 else
2321 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2322 8) * 4;
2323
2324 I915_WRITE(fence_reg, 0);
2325 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002326
2327 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2328 obj_priv->fence_reg = I915_FENCE_REG_NONE;
2329}
2330
Eric Anholt673a3942008-07-30 12:06:12 -07002331/**
2332 * Finds free space in the GTT aperture and binds the object there.
2333 */
2334static int
2335i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2336{
2337 struct drm_device *dev = obj->dev;
2338 drm_i915_private_t *dev_priv = dev->dev_private;
2339 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2340 struct drm_mm_node *free_space;
2341 int page_count, ret;
2342
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002343 if (dev_priv->mm.suspended)
2344 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002345 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002346 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002347 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002348 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2349 return -EINVAL;
2350 }
2351
2352 search_free:
2353 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2354 obj->size, alignment, 0);
2355 if (free_space != NULL) {
2356 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2357 alignment);
2358 if (obj_priv->gtt_space != NULL) {
2359 obj_priv->gtt_space->private = obj;
2360 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2361 }
2362 }
2363 if (obj_priv->gtt_space == NULL) {
Carl Worth5e118f42009-03-20 11:54:25 -07002364 bool lists_empty;
2365
Eric Anholt673a3942008-07-30 12:06:12 -07002366 /* If the gtt is empty and we're still having trouble
2367 * fitting our object in, we're out of memory.
2368 */
2369#if WATCH_LRU
2370 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2371#endif
Carl Worth5e118f42009-03-20 11:54:25 -07002372 spin_lock(&dev_priv->mm.active_list_lock);
2373 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2374 list_empty(&dev_priv->mm.flushing_list) &&
2375 list_empty(&dev_priv->mm.active_list));
2376 spin_unlock(&dev_priv->mm.active_list_lock);
2377 if (lists_empty) {
Eric Anholt673a3942008-07-30 12:06:12 -07002378 DRM_ERROR("GTT full, but LRU list empty\n");
2379 return -ENOMEM;
2380 }
2381
2382 ret = i915_gem_evict_something(dev);
2383 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08002384 if (ret != -ERESTARTSYS)
2385 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002386 return ret;
2387 }
2388 goto search_free;
2389 }
2390
2391#if WATCH_BUF
2392 DRM_INFO("Binding object of size %d at 0x%08x\n",
2393 obj->size, obj_priv->gtt_offset);
2394#endif
Eric Anholt856fa192009-03-19 14:10:50 -07002395 ret = i915_gem_object_get_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002396 if (ret) {
2397 drm_mm_put_block(obj_priv->gtt_space);
2398 obj_priv->gtt_space = NULL;
2399 return ret;
2400 }
2401
2402 page_count = obj->size / PAGE_SIZE;
2403 /* Create an AGP memory structure pointing at our pages, and bind it
2404 * into the GTT.
2405 */
2406 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002407 obj_priv->pages,
Eric Anholt673a3942008-07-30 12:06:12 -07002408 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002409 obj_priv->gtt_offset,
2410 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002411 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002412 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002413 drm_mm_put_block(obj_priv->gtt_space);
2414 obj_priv->gtt_space = NULL;
2415 return -ENOMEM;
2416 }
2417 atomic_inc(&dev->gtt_count);
2418 atomic_add(obj->size, &dev->gtt_memory);
2419
2420 /* Assert that the object is not currently in any GPU domain. As it
2421 * wasn't in the GTT, there shouldn't be any way it could have been in
2422 * a GPU cache
2423 */
2424 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2425 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2426
2427 return 0;
2428}
2429
2430void
2431i915_gem_clflush_object(struct drm_gem_object *obj)
2432{
2433 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2434
2435 /* If we don't have a page list set up, then we're not pinned
2436 * to GPU, and we can ignore the cache flush because it'll happen
2437 * again at bind time.
2438 */
Eric Anholt856fa192009-03-19 14:10:50 -07002439 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002440 return;
2441
Eric Anholtcfa16a02009-05-26 18:46:16 -07002442 /* XXX: The 865 in particular appears to be weird in how it handles
2443 * cache flushing. We haven't figured it out, but the
2444 * clflush+agp_chipset_flush doesn't appear to successfully get the
2445 * data visible to the PGU, while wbinvd + agp_chipset_flush does.
2446 */
2447 if (IS_I865G(obj->dev)) {
2448 wbinvd();
2449 return;
2450 }
2451
Eric Anholt856fa192009-03-19 14:10:50 -07002452 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002453}
2454
Eric Anholte47c68e2008-11-14 13:35:19 -08002455/** Flushes any GPU write domain for the object if it's dirty. */
2456static void
2457i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2458{
2459 struct drm_device *dev = obj->dev;
2460 uint32_t seqno;
2461
2462 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2463 return;
2464
2465 /* Queue the GPU write cache flushing we need. */
2466 i915_gem_flush(dev, 0, obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002467 seqno = i915_add_request(dev, NULL, obj->write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002468 obj->write_domain = 0;
2469 i915_gem_object_move_to_active(obj, seqno);
2470}
2471
2472/** Flushes the GTT write domain for the object if it's dirty. */
2473static void
2474i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2475{
2476 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2477 return;
2478
2479 /* No actual flushing is required for the GTT write domain. Writes
2480 * to it immediately go to main memory as far as we know, so there's
2481 * no chipset flush. It also doesn't land in render cache.
2482 */
2483 obj->write_domain = 0;
2484}
2485
2486/** Flushes the CPU write domain for the object if it's dirty. */
2487static void
2488i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2489{
2490 struct drm_device *dev = obj->dev;
2491
2492 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2493 return;
2494
2495 i915_gem_clflush_object(obj);
2496 drm_agp_chipset_flush(dev);
2497 obj->write_domain = 0;
2498}
2499
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002500/**
2501 * Moves a single object to the GTT read, and possibly write domain.
2502 *
2503 * This function returns when the move is complete, including waiting on
2504 * flushes to occur.
2505 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002506int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002507i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2508{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002509 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002510 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002511
Eric Anholt02354392008-11-26 13:58:13 -08002512 /* Not valid to be called on unbound objects. */
2513 if (obj_priv->gtt_space == NULL)
2514 return -EINVAL;
2515
Eric Anholte47c68e2008-11-14 13:35:19 -08002516 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002517 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002518 ret = i915_gem_object_wait_rendering(obj);
2519 if (ret != 0)
2520 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002521
2522 /* If we're writing through the GTT domain, then CPU and GPU caches
2523 * will need to be invalidated at next use.
2524 */
2525 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002526 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002527
Eric Anholte47c68e2008-11-14 13:35:19 -08002528 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002529
2530 /* It should now be out of any other write domains, and we can update
2531 * the domain values for our changes.
2532 */
2533 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2534 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002535 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002536 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002537 obj_priv->dirty = 1;
2538 }
2539
2540 return 0;
2541}
2542
2543/**
2544 * Moves a single object to the CPU read, and possibly write domain.
2545 *
2546 * This function returns when the move is complete, including waiting on
2547 * flushes to occur.
2548 */
2549static int
2550i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2551{
Eric Anholte47c68e2008-11-14 13:35:19 -08002552 int ret;
2553
2554 i915_gem_object_flush_gpu_write_domain(obj);
2555 /* Wait on any GPU rendering and flushing to occur. */
2556 ret = i915_gem_object_wait_rendering(obj);
2557 if (ret != 0)
2558 return ret;
2559
2560 i915_gem_object_flush_gtt_write_domain(obj);
2561
2562 /* If we have a partially-valid cache of the object in the CPU,
2563 * finish invalidating it and free the per-page flags.
2564 */
2565 i915_gem_object_set_to_full_cpu_read_domain(obj);
2566
2567 /* Flush the CPU cache if it's still invalid. */
2568 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2569 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002570
2571 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2572 }
2573
2574 /* It should now be out of any other write domains, and we can update
2575 * the domain values for our changes.
2576 */
2577 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2578
2579 /* If we're writing through the CPU, then the GPU read domains will
2580 * need to be invalidated at next use.
2581 */
2582 if (write) {
2583 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2584 obj->write_domain = I915_GEM_DOMAIN_CPU;
2585 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002586
2587 return 0;
2588}
2589
Eric Anholt673a3942008-07-30 12:06:12 -07002590/*
2591 * Set the next domain for the specified object. This
2592 * may not actually perform the necessary flushing/invaliding though,
2593 * as that may want to be batched with other set_domain operations
2594 *
2595 * This is (we hope) the only really tricky part of gem. The goal
2596 * is fairly simple -- track which caches hold bits of the object
2597 * and make sure they remain coherent. A few concrete examples may
2598 * help to explain how it works. For shorthand, we use the notation
2599 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2600 * a pair of read and write domain masks.
2601 *
2602 * Case 1: the batch buffer
2603 *
2604 * 1. Allocated
2605 * 2. Written by CPU
2606 * 3. Mapped to GTT
2607 * 4. Read by GPU
2608 * 5. Unmapped from GTT
2609 * 6. Freed
2610 *
2611 * Let's take these a step at a time
2612 *
2613 * 1. Allocated
2614 * Pages allocated from the kernel may still have
2615 * cache contents, so we set them to (CPU, CPU) always.
2616 * 2. Written by CPU (using pwrite)
2617 * The pwrite function calls set_domain (CPU, CPU) and
2618 * this function does nothing (as nothing changes)
2619 * 3. Mapped by GTT
2620 * This function asserts that the object is not
2621 * currently in any GPU-based read or write domains
2622 * 4. Read by GPU
2623 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2624 * As write_domain is zero, this function adds in the
2625 * current read domains (CPU+COMMAND, 0).
2626 * flush_domains is set to CPU.
2627 * invalidate_domains is set to COMMAND
2628 * clflush is run to get data out of the CPU caches
2629 * then i915_dev_set_domain calls i915_gem_flush to
2630 * emit an MI_FLUSH and drm_agp_chipset_flush
2631 * 5. Unmapped from GTT
2632 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2633 * flush_domains and invalidate_domains end up both zero
2634 * so no flushing/invalidating happens
2635 * 6. Freed
2636 * yay, done
2637 *
2638 * Case 2: The shared render buffer
2639 *
2640 * 1. Allocated
2641 * 2. Mapped to GTT
2642 * 3. Read/written by GPU
2643 * 4. set_domain to (CPU,CPU)
2644 * 5. Read/written by CPU
2645 * 6. Read/written by GPU
2646 *
2647 * 1. Allocated
2648 * Same as last example, (CPU, CPU)
2649 * 2. Mapped to GTT
2650 * Nothing changes (assertions find that it is not in the GPU)
2651 * 3. Read/written by GPU
2652 * execbuffer calls set_domain (RENDER, RENDER)
2653 * flush_domains gets CPU
2654 * invalidate_domains gets GPU
2655 * clflush (obj)
2656 * MI_FLUSH and drm_agp_chipset_flush
2657 * 4. set_domain (CPU, CPU)
2658 * flush_domains gets GPU
2659 * invalidate_domains gets CPU
2660 * wait_rendering (obj) to make sure all drawing is complete.
2661 * This will include an MI_FLUSH to get the data from GPU
2662 * to memory
2663 * clflush (obj) to invalidate the CPU cache
2664 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2665 * 5. Read/written by CPU
2666 * cache lines are loaded and dirtied
2667 * 6. Read written by GPU
2668 * Same as last GPU access
2669 *
2670 * Case 3: The constant buffer
2671 *
2672 * 1. Allocated
2673 * 2. Written by CPU
2674 * 3. Read by GPU
2675 * 4. Updated (written) by CPU again
2676 * 5. Read by GPU
2677 *
2678 * 1. Allocated
2679 * (CPU, CPU)
2680 * 2. Written by CPU
2681 * (CPU, CPU)
2682 * 3. Read by GPU
2683 * (CPU+RENDER, 0)
2684 * flush_domains = CPU
2685 * invalidate_domains = RENDER
2686 * clflush (obj)
2687 * MI_FLUSH
2688 * drm_agp_chipset_flush
2689 * 4. Updated (written) by CPU again
2690 * (CPU, CPU)
2691 * flush_domains = 0 (no previous write domain)
2692 * invalidate_domains = 0 (no new read domains)
2693 * 5. Read by GPU
2694 * (CPU+RENDER, 0)
2695 * flush_domains = CPU
2696 * invalidate_domains = RENDER
2697 * clflush (obj)
2698 * MI_FLUSH
2699 * drm_agp_chipset_flush
2700 */
Keith Packardc0d90822008-11-20 23:11:08 -08002701static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002702i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002703{
2704 struct drm_device *dev = obj->dev;
2705 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2706 uint32_t invalidate_domains = 0;
2707 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002708
Eric Anholt8b0e3782009-02-19 14:40:50 -08002709 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2710 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002711
2712#if WATCH_BUF
2713 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2714 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002715 obj->read_domains, obj->pending_read_domains,
2716 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002717#endif
2718 /*
2719 * If the object isn't moving to a new write domain,
2720 * let the object stay in multiple read domains
2721 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002722 if (obj->pending_write_domain == 0)
2723 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002724 else
2725 obj_priv->dirty = 1;
2726
2727 /*
2728 * Flush the current write domain if
2729 * the new read domains don't match. Invalidate
2730 * any read domains which differ from the old
2731 * write domain
2732 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002733 if (obj->write_domain &&
2734 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002735 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002736 invalidate_domains |=
2737 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002738 }
2739 /*
2740 * Invalidate any read caches which may have
2741 * stale data. That is, any new read domains.
2742 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002743 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002744 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2745#if WATCH_BUF
2746 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2747 __func__, flush_domains, invalidate_domains);
2748#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002749 i915_gem_clflush_object(obj);
2750 }
2751
Eric Anholtefbeed92009-02-19 14:54:51 -08002752 /* The actual obj->write_domain will be updated with
2753 * pending_write_domain after we emit the accumulated flush for all
2754 * of our domain changes in execbuffers (which clears objects'
2755 * write_domains). So if we have a current write domain that we
2756 * aren't changing, set pending_write_domain to that.
2757 */
2758 if (flush_domains == 0 && obj->pending_write_domain == 0)
2759 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002760 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002761
2762 dev->invalidate_domains |= invalidate_domains;
2763 dev->flush_domains |= flush_domains;
2764#if WATCH_BUF
2765 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2766 __func__,
2767 obj->read_domains, obj->write_domain,
2768 dev->invalidate_domains, dev->flush_domains);
2769#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002770}
2771
2772/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002773 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002774 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002775 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2776 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2777 */
2778static void
2779i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2780{
Eric Anholte47c68e2008-11-14 13:35:19 -08002781 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2782
2783 if (!obj_priv->page_cpu_valid)
2784 return;
2785
2786 /* If we're partially in the CPU read domain, finish moving it in.
2787 */
2788 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2789 int i;
2790
2791 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2792 if (obj_priv->page_cpu_valid[i])
2793 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07002794 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08002795 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002796 }
2797
2798 /* Free the page_cpu_valid mappings which are now stale, whether
2799 * or not we've got I915_GEM_DOMAIN_CPU.
2800 */
2801 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
2802 DRM_MEM_DRIVER);
2803 obj_priv->page_cpu_valid = NULL;
2804}
2805
2806/**
2807 * Set the CPU read domain on a range of the object.
2808 *
2809 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2810 * not entirely valid. The page_cpu_valid member of the object flags which
2811 * pages have been flushed, and will be respected by
2812 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2813 * of the whole object.
2814 *
2815 * This function returns when the move is complete, including waiting on
2816 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002817 */
2818static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002819i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2820 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002821{
2822 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002823 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002824
Eric Anholte47c68e2008-11-14 13:35:19 -08002825 if (offset == 0 && size == obj->size)
2826 return i915_gem_object_set_to_cpu_domain(obj, 0);
2827
2828 i915_gem_object_flush_gpu_write_domain(obj);
2829 /* Wait on any GPU rendering and flushing to occur. */
2830 ret = i915_gem_object_wait_rendering(obj);
2831 if (ret != 0)
2832 return ret;
2833 i915_gem_object_flush_gtt_write_domain(obj);
2834
2835 /* If we're already fully in the CPU read domain, we're done. */
2836 if (obj_priv->page_cpu_valid == NULL &&
2837 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002838 return 0;
2839
Eric Anholte47c68e2008-11-14 13:35:19 -08002840 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2841 * newly adding I915_GEM_DOMAIN_CPU
2842 */
Eric Anholt673a3942008-07-30 12:06:12 -07002843 if (obj_priv->page_cpu_valid == NULL) {
2844 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2845 DRM_MEM_DRIVER);
Eric Anholte47c68e2008-11-14 13:35:19 -08002846 if (obj_priv->page_cpu_valid == NULL)
2847 return -ENOMEM;
2848 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2849 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002850
2851 /* Flush the cache on any pages that are still invalid from the CPU's
2852 * perspective.
2853 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002854 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2855 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002856 if (obj_priv->page_cpu_valid[i])
2857 continue;
2858
Eric Anholt856fa192009-03-19 14:10:50 -07002859 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002860
2861 obj_priv->page_cpu_valid[i] = 1;
2862 }
2863
Eric Anholte47c68e2008-11-14 13:35:19 -08002864 /* It should now be out of any other write domains, and we can update
2865 * the domain values for our changes.
2866 */
2867 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2868
2869 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2870
Eric Anholt673a3942008-07-30 12:06:12 -07002871 return 0;
2872}
2873
2874/**
Eric Anholt673a3942008-07-30 12:06:12 -07002875 * Pin an object to the GTT and evaluate the relocations landing in it.
2876 */
2877static int
2878i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2879 struct drm_file *file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002880 struct drm_i915_gem_exec_object *entry,
2881 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07002882{
2883 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002884 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002885 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2886 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002887 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002888
2889 /* Choose the GTT offset for our buffer and put it there. */
2890 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2891 if (ret)
2892 return ret;
2893
2894 entry->offset = obj_priv->gtt_offset;
2895
Eric Anholt673a3942008-07-30 12:06:12 -07002896 /* Apply the relocations, using the GTT aperture to avoid cache
2897 * flushing requirements.
2898 */
2899 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002900 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002901 struct drm_gem_object *target_obj;
2902 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002903 uint32_t reloc_val, reloc_offset;
2904 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002905
Eric Anholt673a3942008-07-30 12:06:12 -07002906 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002907 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002908 if (target_obj == NULL) {
2909 i915_gem_object_unpin(obj);
2910 return -EBADF;
2911 }
2912 target_obj_priv = target_obj->driver_private;
2913
2914 /* The target buffer should have appeared before us in the
2915 * exec_object list, so it should have a GTT space bound by now.
2916 */
2917 if (target_obj_priv->gtt_space == NULL) {
2918 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002919 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002920 drm_gem_object_unreference(target_obj);
2921 i915_gem_object_unpin(obj);
2922 return -EINVAL;
2923 }
2924
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002925 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07002926 DRM_ERROR("Relocation beyond object bounds: "
2927 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002928 obj, reloc->target_handle,
2929 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07002930 drm_gem_object_unreference(target_obj);
2931 i915_gem_object_unpin(obj);
2932 return -EINVAL;
2933 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002934 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07002935 DRM_ERROR("Relocation not 4-byte aligned: "
2936 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002937 obj, reloc->target_handle,
2938 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07002939 drm_gem_object_unreference(target_obj);
2940 i915_gem_object_unpin(obj);
2941 return -EINVAL;
2942 }
2943
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002944 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
2945 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08002946 DRM_ERROR("reloc with read/write CPU domains: "
2947 "obj %p target %d offset %d "
2948 "read %08x write %08x",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002949 obj, reloc->target_handle,
2950 (int) reloc->offset,
2951 reloc->read_domains,
2952 reloc->write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00002953 drm_gem_object_unreference(target_obj);
2954 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002955 return -EINVAL;
2956 }
2957
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002958 if (reloc->write_domain && target_obj->pending_write_domain &&
2959 reloc->write_domain != target_obj->pending_write_domain) {
Eric Anholt673a3942008-07-30 12:06:12 -07002960 DRM_ERROR("Write domain conflict: "
2961 "obj %p target %d offset %d "
2962 "new %08x old %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002963 obj, reloc->target_handle,
2964 (int) reloc->offset,
2965 reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07002966 target_obj->pending_write_domain);
2967 drm_gem_object_unreference(target_obj);
2968 i915_gem_object_unpin(obj);
2969 return -EINVAL;
2970 }
2971
2972#if WATCH_RELOC
2973 DRM_INFO("%s: obj %p offset %08x target %d "
2974 "read %08x write %08x gtt %08x "
2975 "presumed %08x delta %08x\n",
2976 __func__,
2977 obj,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002978 (int) reloc->offset,
2979 (int) reloc->target_handle,
2980 (int) reloc->read_domains,
2981 (int) reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07002982 (int) target_obj_priv->gtt_offset,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002983 (int) reloc->presumed_offset,
2984 reloc->delta);
Eric Anholt673a3942008-07-30 12:06:12 -07002985#endif
2986
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002987 target_obj->pending_read_domains |= reloc->read_domains;
2988 target_obj->pending_write_domain |= reloc->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002989
2990 /* If the relocation already has the right value in it, no
2991 * more work needs to be done.
2992 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002993 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
Eric Anholt673a3942008-07-30 12:06:12 -07002994 drm_gem_object_unreference(target_obj);
2995 continue;
2996 }
2997
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002998 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
2999 if (ret != 0) {
3000 drm_gem_object_unreference(target_obj);
3001 i915_gem_object_unpin(obj);
3002 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003003 }
3004
3005 /* Map the page containing the relocation we're going to
3006 * perform.
3007 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003008 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003009 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3010 (reloc_offset &
3011 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003012 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003013 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003014 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003015
3016#if WATCH_BUF
3017 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003018 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003019 readl(reloc_entry), reloc_val);
3020#endif
3021 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003022 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003023
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003024 /* The updated presumed offset for this entry will be
3025 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003026 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003027 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003028
3029 drm_gem_object_unreference(target_obj);
3030 }
3031
Eric Anholt673a3942008-07-30 12:06:12 -07003032#if WATCH_BUF
3033 if (0)
3034 i915_gem_dump_object(obj, 128, __func__, ~0);
3035#endif
3036 return 0;
3037}
3038
3039/** Dispatch a batchbuffer to the ring
3040 */
3041static int
3042i915_dispatch_gem_execbuffer(struct drm_device *dev,
3043 struct drm_i915_gem_execbuffer *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003044 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003045 uint64_t exec_offset)
3046{
3047 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003048 int nbox = exec->num_cliprects;
3049 int i = 0, count;
3050 uint32_t exec_start, exec_len;
3051 RING_LOCALS;
3052
3053 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3054 exec_len = (uint32_t) exec->batch_len;
3055
3056 if ((exec_start | exec_len) & 0x7) {
3057 DRM_ERROR("alignment\n");
3058 return -EINVAL;
3059 }
3060
3061 if (!exec_start)
3062 return -EINVAL;
3063
3064 count = nbox ? nbox : 1;
3065
3066 for (i = 0; i < count; i++) {
3067 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003068 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003069 exec->DR1, exec->DR4);
3070 if (ret)
3071 return ret;
3072 }
3073
3074 if (IS_I830(dev) || IS_845G(dev)) {
3075 BEGIN_LP_RING(4);
3076 OUT_RING(MI_BATCH_BUFFER);
3077 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3078 OUT_RING(exec_start + exec_len - 4);
3079 OUT_RING(0);
3080 ADVANCE_LP_RING();
3081 } else {
3082 BEGIN_LP_RING(2);
3083 if (IS_I965G(dev)) {
3084 OUT_RING(MI_BATCH_BUFFER_START |
3085 (2 << 6) |
3086 MI_BATCH_NON_SECURE_I965);
3087 OUT_RING(exec_start);
3088 } else {
3089 OUT_RING(MI_BATCH_BUFFER_START |
3090 (2 << 6));
3091 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3092 }
3093 ADVANCE_LP_RING();
3094 }
3095 }
3096
3097 /* XXX breadcrumb */
3098 return 0;
3099}
3100
3101/* Throttle our rendering by waiting until the ring has completed our requests
3102 * emitted over 20 msec ago.
3103 *
Eric Anholtb9624422009-06-03 07:27:35 +00003104 * Note that if we were to use the current jiffies each time around the loop,
3105 * we wouldn't escape the function with any frames outstanding if the time to
3106 * render a frame was over 20ms.
3107 *
Eric Anholt673a3942008-07-30 12:06:12 -07003108 * This should get us reasonable parallelism between CPU and GPU but also
3109 * relatively low latency when blocking on a particular request to finish.
3110 */
3111static int
3112i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3113{
3114 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3115 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003116 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003117
3118 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003119 while (!list_empty(&i915_file_priv->mm.request_list)) {
3120 struct drm_i915_gem_request *request;
3121
3122 request = list_first_entry(&i915_file_priv->mm.request_list,
3123 struct drm_i915_gem_request,
3124 client_list);
3125
3126 if (time_after_eq(request->emitted_jiffies, recent_enough))
3127 break;
3128
3129 ret = i915_wait_request(dev, request->seqno);
3130 if (ret != 0)
3131 break;
3132 }
Eric Anholt673a3942008-07-30 12:06:12 -07003133 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003134
Eric Anholt673a3942008-07-30 12:06:12 -07003135 return ret;
3136}
3137
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003138static int
3139i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3140 uint32_t buffer_count,
3141 struct drm_i915_gem_relocation_entry **relocs)
3142{
3143 uint32_t reloc_count = 0, reloc_index = 0, i;
3144 int ret;
3145
3146 *relocs = NULL;
3147 for (i = 0; i < buffer_count; i++) {
3148 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3149 return -EINVAL;
3150 reloc_count += exec_list[i].relocation_count;
3151 }
3152
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003153 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003154 if (*relocs == NULL)
3155 return -ENOMEM;
3156
3157 for (i = 0; i < buffer_count; i++) {
3158 struct drm_i915_gem_relocation_entry __user *user_relocs;
3159
3160 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3161
3162 ret = copy_from_user(&(*relocs)[reloc_index],
3163 user_relocs,
3164 exec_list[i].relocation_count *
3165 sizeof(**relocs));
3166 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003167 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003168 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003169 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003170 }
3171
3172 reloc_index += exec_list[i].relocation_count;
3173 }
3174
Florian Mickler2bc43b52009-04-06 22:55:41 +02003175 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003176}
3177
3178static int
3179i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3180 uint32_t buffer_count,
3181 struct drm_i915_gem_relocation_entry *relocs)
3182{
3183 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003184 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003185
3186 for (i = 0; i < buffer_count; i++) {
3187 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003188 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003189
3190 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3191
Florian Mickler2bc43b52009-04-06 22:55:41 +02003192 unwritten = copy_to_user(user_relocs,
3193 &relocs[reloc_count],
3194 exec_list[i].relocation_count *
3195 sizeof(*relocs));
3196
3197 if (unwritten) {
3198 ret = -EFAULT;
3199 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003200 }
3201
3202 reloc_count += exec_list[i].relocation_count;
3203 }
3204
Florian Mickler2bc43b52009-04-06 22:55:41 +02003205err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003206 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003207
3208 return ret;
3209}
3210
Eric Anholt673a3942008-07-30 12:06:12 -07003211int
3212i915_gem_execbuffer(struct drm_device *dev, void *data,
3213 struct drm_file *file_priv)
3214{
3215 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003216 struct drm_i915_gem_execbuffer *args = data;
3217 struct drm_i915_gem_exec_object *exec_list = NULL;
3218 struct drm_gem_object **object_list = NULL;
3219 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003220 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003221 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003222 struct drm_i915_gem_relocation_entry *relocs;
3223 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003224 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003225 uint32_t seqno, flush_domains, reloc_index;
Keith Packardac94a962008-11-20 23:30:27 -08003226 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07003227
3228#if WATCH_EXEC
3229 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3230 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3231#endif
3232
Eric Anholt4f481ed2008-09-10 14:22:49 -07003233 if (args->buffer_count < 1) {
3234 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3235 return -EINVAL;
3236 }
Eric Anholt673a3942008-07-30 12:06:12 -07003237 /* Copy in the exec list from userland */
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003238 exec_list = drm_calloc_large(sizeof(*exec_list), args->buffer_count);
3239 object_list = drm_calloc_large(sizeof(*object_list), args->buffer_count);
Eric Anholt673a3942008-07-30 12:06:12 -07003240 if (exec_list == NULL || object_list == NULL) {
3241 DRM_ERROR("Failed to allocate exec or object list "
3242 "for %d buffers\n",
3243 args->buffer_count);
3244 ret = -ENOMEM;
3245 goto pre_mutex_err;
3246 }
3247 ret = copy_from_user(exec_list,
3248 (struct drm_i915_relocation_entry __user *)
3249 (uintptr_t) args->buffers_ptr,
3250 sizeof(*exec_list) * args->buffer_count);
3251 if (ret != 0) {
3252 DRM_ERROR("copy %d exec entries failed %d\n",
3253 args->buffer_count, ret);
3254 goto pre_mutex_err;
3255 }
3256
Eric Anholt201361a2009-03-11 12:30:04 -07003257 if (args->num_cliprects != 0) {
3258 cliprects = drm_calloc(args->num_cliprects, sizeof(*cliprects),
3259 DRM_MEM_DRIVER);
3260 if (cliprects == NULL)
3261 goto pre_mutex_err;
3262
3263 ret = copy_from_user(cliprects,
3264 (struct drm_clip_rect __user *)
3265 (uintptr_t) args->cliprects_ptr,
3266 sizeof(*cliprects) * args->num_cliprects);
3267 if (ret != 0) {
3268 DRM_ERROR("copy %d cliprects failed: %d\n",
3269 args->num_cliprects, ret);
3270 goto pre_mutex_err;
3271 }
3272 }
3273
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003274 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3275 &relocs);
3276 if (ret != 0)
3277 goto pre_mutex_err;
3278
Eric Anholt673a3942008-07-30 12:06:12 -07003279 mutex_lock(&dev->struct_mutex);
3280
3281 i915_verify_inactive(dev, __FILE__, __LINE__);
3282
3283 if (dev_priv->mm.wedged) {
3284 DRM_ERROR("Execbuf while wedged\n");
3285 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003286 ret = -EIO;
3287 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003288 }
3289
3290 if (dev_priv->mm.suspended) {
3291 DRM_ERROR("Execbuf while VT-switched.\n");
3292 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003293 ret = -EBUSY;
3294 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003295 }
3296
Keith Packardac94a962008-11-20 23:30:27 -08003297 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003298 for (i = 0; i < args->buffer_count; i++) {
3299 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3300 exec_list[i].handle);
3301 if (object_list[i] == NULL) {
3302 DRM_ERROR("Invalid object handle %d at index %d\n",
3303 exec_list[i].handle, i);
3304 ret = -EBADF;
3305 goto err;
3306 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003307
3308 obj_priv = object_list[i]->driver_private;
3309 if (obj_priv->in_execbuffer) {
3310 DRM_ERROR("Object %p appears more than once in object list\n",
3311 object_list[i]);
3312 ret = -EBADF;
3313 goto err;
3314 }
3315 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003316 }
Eric Anholt673a3942008-07-30 12:06:12 -07003317
Keith Packardac94a962008-11-20 23:30:27 -08003318 /* Pin and relocate */
3319 for (pin_tries = 0; ; pin_tries++) {
3320 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003321 reloc_index = 0;
3322
Keith Packardac94a962008-11-20 23:30:27 -08003323 for (i = 0; i < args->buffer_count; i++) {
3324 object_list[i]->pending_read_domains = 0;
3325 object_list[i]->pending_write_domain = 0;
3326 ret = i915_gem_object_pin_and_relocate(object_list[i],
3327 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003328 &exec_list[i],
3329 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003330 if (ret)
3331 break;
3332 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003333 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003334 }
3335 /* success */
3336 if (ret == 0)
3337 break;
3338
3339 /* error other than GTT full, or we've already tried again */
3340 if (ret != -ENOMEM || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08003341 if (ret != -ERESTARTSYS)
3342 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003343 goto err;
3344 }
Keith Packardac94a962008-11-20 23:30:27 -08003345
3346 /* unpin all of our buffers */
3347 for (i = 0; i < pinned; i++)
3348 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003349 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003350
3351 /* evict everyone we can from the aperture */
3352 ret = i915_gem_evict_everything(dev);
3353 if (ret)
3354 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003355 }
3356
3357 /* Set the pending read domains for the batch buffer to COMMAND */
3358 batch_obj = object_list[args->buffer_count-1];
3359 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
3360 batch_obj->pending_write_domain = 0;
3361
3362 i915_verify_inactive(dev, __FILE__, __LINE__);
3363
Keith Packard646f0f62008-11-20 23:23:03 -08003364 /* Zero the global flush/invalidate flags. These
3365 * will be modified as new domains are computed
3366 * for each object
3367 */
3368 dev->invalidate_domains = 0;
3369 dev->flush_domains = 0;
3370
Eric Anholt673a3942008-07-30 12:06:12 -07003371 for (i = 0; i < args->buffer_count; i++) {
3372 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003373
Keith Packard646f0f62008-11-20 23:23:03 -08003374 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003375 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003376 }
3377
3378 i915_verify_inactive(dev, __FILE__, __LINE__);
3379
Keith Packard646f0f62008-11-20 23:23:03 -08003380 if (dev->invalidate_domains | dev->flush_domains) {
3381#if WATCH_EXEC
3382 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3383 __func__,
3384 dev->invalidate_domains,
3385 dev->flush_domains);
3386#endif
3387 i915_gem_flush(dev,
3388 dev->invalidate_domains,
3389 dev->flush_domains);
3390 if (dev->flush_domains)
Eric Anholtb9624422009-06-03 07:27:35 +00003391 (void)i915_add_request(dev, file_priv,
3392 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003393 }
Eric Anholt673a3942008-07-30 12:06:12 -07003394
Eric Anholtefbeed92009-02-19 14:54:51 -08003395 for (i = 0; i < args->buffer_count; i++) {
3396 struct drm_gem_object *obj = object_list[i];
3397
3398 obj->write_domain = obj->pending_write_domain;
3399 }
3400
Eric Anholt673a3942008-07-30 12:06:12 -07003401 i915_verify_inactive(dev, __FILE__, __LINE__);
3402
3403#if WATCH_COHERENCY
3404 for (i = 0; i < args->buffer_count; i++) {
3405 i915_gem_object_check_coherency(object_list[i],
3406 exec_list[i].handle);
3407 }
3408#endif
3409
3410 exec_offset = exec_list[args->buffer_count - 1].offset;
3411
3412#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003413 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003414 args->batch_len,
3415 __func__,
3416 ~0);
3417#endif
3418
Eric Anholt673a3942008-07-30 12:06:12 -07003419 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003420 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003421 if (ret) {
3422 DRM_ERROR("dispatch failed %d\n", ret);
3423 goto err;
3424 }
3425
3426 /*
3427 * Ensure that the commands in the batch buffer are
3428 * finished before the interrupt fires
3429 */
3430 flush_domains = i915_retire_commands(dev);
3431
3432 i915_verify_inactive(dev, __FILE__, __LINE__);
3433
3434 /*
3435 * Get a seqno representing the execution of the current buffer,
3436 * which we can wait on. We would like to mitigate these interrupts,
3437 * likely by only creating seqnos occasionally (so that we have
3438 * *some* interrupts representing completion of buffers that we can
3439 * wait on when trying to clear up gtt space).
3440 */
Eric Anholtb9624422009-06-03 07:27:35 +00003441 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003442 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003443 for (i = 0; i < args->buffer_count; i++) {
3444 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003445
Eric Anholtce44b0e2008-11-06 16:00:31 -08003446 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003447#if WATCH_LRU
3448 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3449#endif
3450 }
3451#if WATCH_LRU
3452 i915_dump_lru(dev, __func__);
3453#endif
3454
3455 i915_verify_inactive(dev, __FILE__, __LINE__);
3456
Eric Anholt673a3942008-07-30 12:06:12 -07003457err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003458 for (i = 0; i < pinned; i++)
3459 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003460
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003461 for (i = 0; i < args->buffer_count; i++) {
3462 if (object_list[i]) {
3463 obj_priv = object_list[i]->driver_private;
3464 obj_priv->in_execbuffer = false;
3465 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003466 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003467 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003468
Eric Anholt673a3942008-07-30 12:06:12 -07003469 mutex_unlock(&dev->struct_mutex);
3470
Roland Dreiera35f2e22009-02-06 17:48:09 -08003471 if (!ret) {
3472 /* Copy the new buffer offsets back to the user's exec list. */
3473 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3474 (uintptr_t) args->buffers_ptr,
3475 exec_list,
3476 sizeof(*exec_list) * args->buffer_count);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003477 if (ret) {
3478 ret = -EFAULT;
Roland Dreiera35f2e22009-02-06 17:48:09 -08003479 DRM_ERROR("failed to copy %d exec entries "
3480 "back to user (%d)\n",
3481 args->buffer_count, ret);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003482 }
Roland Dreiera35f2e22009-02-06 17:48:09 -08003483 }
3484
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003485 /* Copy the updated relocations out regardless of current error
3486 * state. Failure to update the relocs would mean that the next
3487 * time userland calls execbuf, it would do so with presumed offset
3488 * state that didn't match the actual object state.
3489 */
3490 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3491 relocs);
3492 if (ret2 != 0) {
3493 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3494
3495 if (ret == 0)
3496 ret = ret2;
3497 }
3498
Eric Anholt673a3942008-07-30 12:06:12 -07003499pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003500 drm_free_large(object_list);
3501 drm_free_large(exec_list);
Eric Anholt201361a2009-03-11 12:30:04 -07003502 drm_free(cliprects, sizeof(*cliprects) * args->num_cliprects,
3503 DRM_MEM_DRIVER);
Eric Anholt673a3942008-07-30 12:06:12 -07003504
3505 return ret;
3506}
3507
3508int
3509i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3510{
3511 struct drm_device *dev = obj->dev;
3512 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3513 int ret;
3514
3515 i915_verify_inactive(dev, __FILE__, __LINE__);
3516 if (obj_priv->gtt_space == NULL) {
3517 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3518 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003519 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05003520 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003521 return ret;
3522 }
Chris Wilson22c344e2009-02-11 14:26:45 +00003523 }
3524 /*
3525 * Pre-965 chips need a fence register set up in order to
3526 * properly handle tiled surfaces.
3527 */
3528 if (!IS_I965G(dev) &&
3529 obj_priv->fence_reg == I915_FENCE_REG_NONE &&
3530 obj_priv->tiling_mode != I915_TILING_NONE) {
3531 ret = i915_gem_object_get_fence_reg(obj, true);
3532 if (ret != 0) {
3533 if (ret != -EBUSY && ret != -ERESTARTSYS)
3534 DRM_ERROR("Failure to install fence: %d\n",
3535 ret);
3536 return ret;
3537 }
Eric Anholt673a3942008-07-30 12:06:12 -07003538 }
3539 obj_priv->pin_count++;
3540
3541 /* If the object is not active and not pending a flush,
3542 * remove it from the inactive list
3543 */
3544 if (obj_priv->pin_count == 1) {
3545 atomic_inc(&dev->pin_count);
3546 atomic_add(obj->size, &dev->pin_memory);
3547 if (!obj_priv->active &&
3548 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
3549 I915_GEM_DOMAIN_GTT)) == 0 &&
3550 !list_empty(&obj_priv->list))
3551 list_del_init(&obj_priv->list);
3552 }
3553 i915_verify_inactive(dev, __FILE__, __LINE__);
3554
3555 return 0;
3556}
3557
3558void
3559i915_gem_object_unpin(struct drm_gem_object *obj)
3560{
3561 struct drm_device *dev = obj->dev;
3562 drm_i915_private_t *dev_priv = dev->dev_private;
3563 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3564
3565 i915_verify_inactive(dev, __FILE__, __LINE__);
3566 obj_priv->pin_count--;
3567 BUG_ON(obj_priv->pin_count < 0);
3568 BUG_ON(obj_priv->gtt_space == NULL);
3569
3570 /* If the object is no longer pinned, and is
3571 * neither active nor being flushed, then stick it on
3572 * the inactive list
3573 */
3574 if (obj_priv->pin_count == 0) {
3575 if (!obj_priv->active &&
3576 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
3577 I915_GEM_DOMAIN_GTT)) == 0)
3578 list_move_tail(&obj_priv->list,
3579 &dev_priv->mm.inactive_list);
3580 atomic_dec(&dev->pin_count);
3581 atomic_sub(obj->size, &dev->pin_memory);
3582 }
3583 i915_verify_inactive(dev, __FILE__, __LINE__);
3584}
3585
3586int
3587i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3588 struct drm_file *file_priv)
3589{
3590 struct drm_i915_gem_pin *args = data;
3591 struct drm_gem_object *obj;
3592 struct drm_i915_gem_object *obj_priv;
3593 int ret;
3594
3595 mutex_lock(&dev->struct_mutex);
3596
3597 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3598 if (obj == NULL) {
3599 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3600 args->handle);
3601 mutex_unlock(&dev->struct_mutex);
3602 return -EBADF;
3603 }
3604 obj_priv = obj->driver_private;
3605
Jesse Barnes79e53942008-11-07 14:24:08 -08003606 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3607 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3608 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00003609 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003610 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003611 return -EINVAL;
3612 }
3613
3614 obj_priv->user_pin_count++;
3615 obj_priv->pin_filp = file_priv;
3616 if (obj_priv->user_pin_count == 1) {
3617 ret = i915_gem_object_pin(obj, args->alignment);
3618 if (ret != 0) {
3619 drm_gem_object_unreference(obj);
3620 mutex_unlock(&dev->struct_mutex);
3621 return ret;
3622 }
Eric Anholt673a3942008-07-30 12:06:12 -07003623 }
3624
3625 /* XXX - flush the CPU caches for pinned objects
3626 * as the X server doesn't manage domains yet
3627 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003628 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003629 args->offset = obj_priv->gtt_offset;
3630 drm_gem_object_unreference(obj);
3631 mutex_unlock(&dev->struct_mutex);
3632
3633 return 0;
3634}
3635
3636int
3637i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3638 struct drm_file *file_priv)
3639{
3640 struct drm_i915_gem_pin *args = data;
3641 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08003642 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003643
3644 mutex_lock(&dev->struct_mutex);
3645
3646 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3647 if (obj == NULL) {
3648 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3649 args->handle);
3650 mutex_unlock(&dev->struct_mutex);
3651 return -EBADF;
3652 }
3653
Jesse Barnes79e53942008-11-07 14:24:08 -08003654 obj_priv = obj->driver_private;
3655 if (obj_priv->pin_filp != file_priv) {
3656 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3657 args->handle);
3658 drm_gem_object_unreference(obj);
3659 mutex_unlock(&dev->struct_mutex);
3660 return -EINVAL;
3661 }
3662 obj_priv->user_pin_count--;
3663 if (obj_priv->user_pin_count == 0) {
3664 obj_priv->pin_filp = NULL;
3665 i915_gem_object_unpin(obj);
3666 }
Eric Anholt673a3942008-07-30 12:06:12 -07003667
3668 drm_gem_object_unreference(obj);
3669 mutex_unlock(&dev->struct_mutex);
3670 return 0;
3671}
3672
3673int
3674i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3675 struct drm_file *file_priv)
3676{
3677 struct drm_i915_gem_busy *args = data;
3678 struct drm_gem_object *obj;
3679 struct drm_i915_gem_object *obj_priv;
3680
3681 mutex_lock(&dev->struct_mutex);
3682 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3683 if (obj == NULL) {
3684 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3685 args->handle);
3686 mutex_unlock(&dev->struct_mutex);
3687 return -EBADF;
3688 }
3689
Eric Anholtf21289b2009-02-18 09:44:56 -08003690 /* Update the active list for the hardware's current position.
3691 * Otherwise this only updates on a delayed timer or when irqs are
3692 * actually unmasked, and our working set ends up being larger than
3693 * required.
3694 */
3695 i915_gem_retire_requests(dev);
3696
Eric Anholt673a3942008-07-30 12:06:12 -07003697 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08003698 /* Don't count being on the flushing list against the object being
3699 * done. Otherwise, a buffer left on the flushing list but not getting
3700 * flushed (because nobody's flushing that domain) won't ever return
3701 * unbusy and get reused by libdrm's bo cache. The other expected
3702 * consumer of this interface, OpenGL's occlusion queries, also specs
3703 * that the objects get unbusy "eventually" without any interference.
3704 */
3705 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003706
3707 drm_gem_object_unreference(obj);
3708 mutex_unlock(&dev->struct_mutex);
3709 return 0;
3710}
3711
3712int
3713i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3714 struct drm_file *file_priv)
3715{
3716 return i915_gem_ring_throttle(dev, file_priv);
3717}
3718
3719int i915_gem_init_object(struct drm_gem_object *obj)
3720{
3721 struct drm_i915_gem_object *obj_priv;
3722
3723 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
3724 if (obj_priv == NULL)
3725 return -ENOMEM;
3726
3727 /*
3728 * We've just allocated pages from the kernel,
3729 * so they've just been written by the CPU with
3730 * zeros. They'll need to be clflushed before we
3731 * use them with the GPU.
3732 */
3733 obj->write_domain = I915_GEM_DOMAIN_CPU;
3734 obj->read_domains = I915_GEM_DOMAIN_CPU;
3735
Keith Packardba1eb1d2008-10-14 19:55:10 -07003736 obj_priv->agp_type = AGP_USER_MEMORY;
3737
Eric Anholt673a3942008-07-30 12:06:12 -07003738 obj->driver_private = obj_priv;
3739 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003740 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07003741 INIT_LIST_HEAD(&obj_priv->list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003742
Eric Anholt673a3942008-07-30 12:06:12 -07003743 return 0;
3744}
3745
3746void i915_gem_free_object(struct drm_gem_object *obj)
3747{
Jesse Barnesde151cf2008-11-12 10:03:55 -08003748 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003749 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3750
3751 while (obj_priv->pin_count > 0)
3752 i915_gem_object_unpin(obj);
3753
Dave Airlie71acb5e2008-12-30 20:31:46 +10003754 if (obj_priv->phys_obj)
3755 i915_gem_detach_phys_object(dev, obj);
3756
Eric Anholt673a3942008-07-30 12:06:12 -07003757 i915_gem_object_unbind(obj);
3758
Jesse Barnesab00b3e2009-02-11 14:01:46 -08003759 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003760
Eric Anholt673a3942008-07-30 12:06:12 -07003761 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
Eric Anholt280b7132009-03-12 16:56:27 -07003762 kfree(obj_priv->bit_17);
Eric Anholt673a3942008-07-30 12:06:12 -07003763 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
3764}
3765
Eric Anholt673a3942008-07-30 12:06:12 -07003766/** Unbinds all objects that are on the given buffer list. */
3767static int
3768i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3769{
3770 struct drm_gem_object *obj;
3771 struct drm_i915_gem_object *obj_priv;
3772 int ret;
3773
3774 while (!list_empty(head)) {
3775 obj_priv = list_first_entry(head,
3776 struct drm_i915_gem_object,
3777 list);
3778 obj = obj_priv->obj;
3779
3780 if (obj_priv->pin_count != 0) {
3781 DRM_ERROR("Pinned object in unbind list\n");
3782 mutex_unlock(&dev->struct_mutex);
3783 return -EINVAL;
3784 }
3785
3786 ret = i915_gem_object_unbind(obj);
3787 if (ret != 0) {
3788 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3789 ret);
3790 mutex_unlock(&dev->struct_mutex);
3791 return ret;
3792 }
3793 }
3794
3795
3796 return 0;
3797}
3798
Jesse Barnes5669fca2009-02-17 15:13:31 -08003799int
Eric Anholt673a3942008-07-30 12:06:12 -07003800i915_gem_idle(struct drm_device *dev)
3801{
3802 drm_i915_private_t *dev_priv = dev->dev_private;
3803 uint32_t seqno, cur_seqno, last_seqno;
3804 int stuck, ret;
3805
Keith Packard6dbe2772008-10-14 21:41:13 -07003806 mutex_lock(&dev->struct_mutex);
3807
3808 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3809 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003810 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003811 }
Eric Anholt673a3942008-07-30 12:06:12 -07003812
3813 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3814 * We need to replace this with a semaphore, or something.
3815 */
3816 dev_priv->mm.suspended = 1;
3817
Keith Packard6dbe2772008-10-14 21:41:13 -07003818 /* Cancel the retire work handler, wait for it to finish if running
3819 */
3820 mutex_unlock(&dev->struct_mutex);
3821 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3822 mutex_lock(&dev->struct_mutex);
3823
Eric Anholt673a3942008-07-30 12:06:12 -07003824 i915_kernel_lost_context(dev);
3825
3826 /* Flush the GPU along with all non-CPU write domains
3827 */
3828 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
3829 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
Eric Anholtb9624422009-06-03 07:27:35 +00003830 seqno = i915_add_request(dev, NULL, ~I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003831
3832 if (seqno == 0) {
3833 mutex_unlock(&dev->struct_mutex);
3834 return -ENOMEM;
3835 }
3836
3837 dev_priv->mm.waiting_gem_seqno = seqno;
3838 last_seqno = 0;
3839 stuck = 0;
3840 for (;;) {
3841 cur_seqno = i915_get_gem_seqno(dev);
3842 if (i915_seqno_passed(cur_seqno, seqno))
3843 break;
3844 if (last_seqno == cur_seqno) {
3845 if (stuck++ > 100) {
3846 DRM_ERROR("hardware wedged\n");
3847 dev_priv->mm.wedged = 1;
3848 DRM_WAKEUP(&dev_priv->irq_queue);
3849 break;
3850 }
3851 }
3852 msleep(10);
3853 last_seqno = cur_seqno;
3854 }
3855 dev_priv->mm.waiting_gem_seqno = 0;
3856
3857 i915_gem_retire_requests(dev);
3858
Carl Worth5e118f42009-03-20 11:54:25 -07003859 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003860 if (!dev_priv->mm.wedged) {
3861 /* Active and flushing should now be empty as we've
3862 * waited for a sequence higher than any pending execbuffer
3863 */
3864 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3865 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3866 /* Request should now be empty as we've also waited
3867 * for the last request in the list
3868 */
3869 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3870 }
Eric Anholt673a3942008-07-30 12:06:12 -07003871
Eric Anholt28dfe522008-11-13 15:00:55 -08003872 /* Empty the active and flushing lists to inactive. If there's
3873 * anything left at this point, it means that we're wedged and
3874 * nothing good's going to happen by leaving them there. So strip
3875 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003876 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003877 while (!list_empty(&dev_priv->mm.active_list)) {
3878 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003879
Eric Anholt28dfe522008-11-13 15:00:55 -08003880 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3881 struct drm_i915_gem_object,
3882 list);
3883 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3884 i915_gem_object_move_to_inactive(obj_priv->obj);
3885 }
Carl Worth5e118f42009-03-20 11:54:25 -07003886 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003887
3888 while (!list_empty(&dev_priv->mm.flushing_list)) {
3889 struct drm_i915_gem_object *obj_priv;
3890
Eric Anholt151903d2008-12-01 10:23:21 +10003891 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003892 struct drm_i915_gem_object,
3893 list);
3894 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3895 i915_gem_object_move_to_inactive(obj_priv->obj);
3896 }
3897
3898
3899 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003900 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003901 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003902 if (ret) {
3903 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003904 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003905 }
Eric Anholt673a3942008-07-30 12:06:12 -07003906
Keith Packard6dbe2772008-10-14 21:41:13 -07003907 i915_gem_cleanup_ringbuffer(dev);
3908 mutex_unlock(&dev->struct_mutex);
3909
Eric Anholt673a3942008-07-30 12:06:12 -07003910 return 0;
3911}
3912
3913static int
3914i915_gem_init_hws(struct drm_device *dev)
3915{
3916 drm_i915_private_t *dev_priv = dev->dev_private;
3917 struct drm_gem_object *obj;
3918 struct drm_i915_gem_object *obj_priv;
3919 int ret;
3920
3921 /* If we need a physical address for the status page, it's already
3922 * initialized at driver load time.
3923 */
3924 if (!I915_NEED_GFX_HWS(dev))
3925 return 0;
3926
3927 obj = drm_gem_object_alloc(dev, 4096);
3928 if (obj == NULL) {
3929 DRM_ERROR("Failed to allocate status page\n");
3930 return -ENOMEM;
3931 }
3932 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07003933 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07003934
3935 ret = i915_gem_object_pin(obj, 4096);
3936 if (ret != 0) {
3937 drm_gem_object_unreference(obj);
3938 return ret;
3939 }
3940
3941 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003942
Eric Anholt856fa192009-03-19 14:10:50 -07003943 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07003944 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003945 DRM_ERROR("Failed to map status page.\n");
3946 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00003947 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003948 drm_gem_object_unreference(obj);
3949 return -EINVAL;
3950 }
3951 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003952 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
3953 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07003954 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07003955 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
3956
3957 return 0;
3958}
3959
Chris Wilson85a7bb92009-02-11 14:52:44 +00003960static void
3961i915_gem_cleanup_hws(struct drm_device *dev)
3962{
3963 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003964 struct drm_gem_object *obj;
3965 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00003966
3967 if (dev_priv->hws_obj == NULL)
3968 return;
3969
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003970 obj = dev_priv->hws_obj;
3971 obj_priv = obj->driver_private;
3972
Eric Anholt856fa192009-03-19 14:10:50 -07003973 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00003974 i915_gem_object_unpin(obj);
3975 drm_gem_object_unreference(obj);
3976 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003977
Chris Wilson85a7bb92009-02-11 14:52:44 +00003978 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3979 dev_priv->hw_status_page = NULL;
3980
3981 /* Write high address into HWS_PGA when disabling. */
3982 I915_WRITE(HWS_PGA, 0x1ffff000);
3983}
3984
Jesse Barnes79e53942008-11-07 14:24:08 -08003985int
Eric Anholt673a3942008-07-30 12:06:12 -07003986i915_gem_init_ringbuffer(struct drm_device *dev)
3987{
3988 drm_i915_private_t *dev_priv = dev->dev_private;
3989 struct drm_gem_object *obj;
3990 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08003991 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07003992 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07003993 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07003994
3995 ret = i915_gem_init_hws(dev);
3996 if (ret != 0)
3997 return ret;
3998
3999 obj = drm_gem_object_alloc(dev, 128 * 1024);
4000 if (obj == NULL) {
4001 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004002 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004003 return -ENOMEM;
4004 }
4005 obj_priv = obj->driver_private;
4006
4007 ret = i915_gem_object_pin(obj, 4096);
4008 if (ret != 0) {
4009 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004010 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004011 return ret;
4012 }
4013
4014 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004015 ring->Size = obj->size;
4016 ring->tail_mask = obj->size - 1;
Eric Anholt673a3942008-07-30 12:06:12 -07004017
Jesse Barnes79e53942008-11-07 14:24:08 -08004018 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4019 ring->map.size = obj->size;
4020 ring->map.type = 0;
4021 ring->map.flags = 0;
4022 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004023
Jesse Barnes79e53942008-11-07 14:24:08 -08004024 drm_core_ioremap_wc(&ring->map, dev);
4025 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004026 DRM_ERROR("Failed to map ringbuffer.\n");
4027 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004028 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004029 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004030 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004031 return -EINVAL;
4032 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004033 ring->ring_obj = obj;
4034 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004035
4036 /* Stop the ring if it's running. */
4037 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004038 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07004039 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004040
4041 /* Initialize the ring. */
4042 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07004043 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4044
4045 /* G45 ring initialization fails to reset head to zero */
4046 if (head != 0) {
4047 DRM_ERROR("Ring head not reset to zero "
4048 "ctl %08x head %08x tail %08x start %08x\n",
4049 I915_READ(PRB0_CTL),
4050 I915_READ(PRB0_HEAD),
4051 I915_READ(PRB0_TAIL),
4052 I915_READ(PRB0_START));
4053 I915_WRITE(PRB0_HEAD, 0);
4054
4055 DRM_ERROR("Ring head forced to zero "
4056 "ctl %08x head %08x tail %08x start %08x\n",
4057 I915_READ(PRB0_CTL),
4058 I915_READ(PRB0_HEAD),
4059 I915_READ(PRB0_TAIL),
4060 I915_READ(PRB0_START));
4061 }
4062
Eric Anholt673a3942008-07-30 12:06:12 -07004063 I915_WRITE(PRB0_CTL,
4064 ((obj->size - 4096) & RING_NR_PAGES) |
4065 RING_NO_REPORT |
4066 RING_VALID);
4067
Keith Packard50aa253d2008-10-14 17:20:35 -07004068 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4069
4070 /* If the head is still not zero, the ring is dead */
4071 if (head != 0) {
4072 DRM_ERROR("Ring initialization failed "
4073 "ctl %08x head %08x tail %08x start %08x\n",
4074 I915_READ(PRB0_CTL),
4075 I915_READ(PRB0_HEAD),
4076 I915_READ(PRB0_TAIL),
4077 I915_READ(PRB0_START));
4078 return -EIO;
4079 }
4080
Eric Anholt673a3942008-07-30 12:06:12 -07004081 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004082 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4083 i915_kernel_lost_context(dev);
4084 else {
4085 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4086 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4087 ring->space = ring->head - (ring->tail + 8);
4088 if (ring->space < 0)
4089 ring->space += ring->Size;
4090 }
Eric Anholt673a3942008-07-30 12:06:12 -07004091
4092 return 0;
4093}
4094
Jesse Barnes79e53942008-11-07 14:24:08 -08004095void
Eric Anholt673a3942008-07-30 12:06:12 -07004096i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4097{
4098 drm_i915_private_t *dev_priv = dev->dev_private;
4099
4100 if (dev_priv->ring.ring_obj == NULL)
4101 return;
4102
4103 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4104
4105 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4106 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4107 dev_priv->ring.ring_obj = NULL;
4108 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4109
Chris Wilson85a7bb92009-02-11 14:52:44 +00004110 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004111}
4112
4113int
4114i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4115 struct drm_file *file_priv)
4116{
4117 drm_i915_private_t *dev_priv = dev->dev_private;
4118 int ret;
4119
Jesse Barnes79e53942008-11-07 14:24:08 -08004120 if (drm_core_check_feature(dev, DRIVER_MODESET))
4121 return 0;
4122
Eric Anholt673a3942008-07-30 12:06:12 -07004123 if (dev_priv->mm.wedged) {
4124 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4125 dev_priv->mm.wedged = 0;
4126 }
4127
Eric Anholt673a3942008-07-30 12:06:12 -07004128 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004129 dev_priv->mm.suspended = 0;
4130
4131 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004132 if (ret != 0) {
4133 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004134 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004135 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004136
Carl Worth5e118f42009-03-20 11:54:25 -07004137 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004138 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004139 spin_unlock(&dev_priv->mm.active_list_lock);
4140
Eric Anholt673a3942008-07-30 12:06:12 -07004141 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4142 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4143 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004144 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004145
4146 drm_irq_install(dev);
4147
Eric Anholt673a3942008-07-30 12:06:12 -07004148 return 0;
4149}
4150
4151int
4152i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4153 struct drm_file *file_priv)
4154{
4155 int ret;
4156
Jesse Barnes79e53942008-11-07 14:24:08 -08004157 if (drm_core_check_feature(dev, DRIVER_MODESET))
4158 return 0;
4159
Eric Anholt673a3942008-07-30 12:06:12 -07004160 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004161 drm_irq_uninstall(dev);
4162
Keith Packard6dbe2772008-10-14 21:41:13 -07004163 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004164}
4165
4166void
4167i915_gem_lastclose(struct drm_device *dev)
4168{
4169 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004170
Eric Anholte806b492009-01-22 09:56:58 -08004171 if (drm_core_check_feature(dev, DRIVER_MODESET))
4172 return;
4173
Keith Packard6dbe2772008-10-14 21:41:13 -07004174 ret = i915_gem_idle(dev);
4175 if (ret)
4176 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004177}
4178
4179void
4180i915_gem_load(struct drm_device *dev)
4181{
4182 drm_i915_private_t *dev_priv = dev->dev_private;
4183
Carl Worth5e118f42009-03-20 11:54:25 -07004184 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004185 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4186 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4187 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4188 INIT_LIST_HEAD(&dev_priv->mm.request_list);
4189 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4190 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004191 dev_priv->mm.next_gem_seqno = 1;
4192
Jesse Barnesde151cf2008-11-12 10:03:55 -08004193 /* Old X drivers will take 0-2 for front, back, depth buffers */
4194 dev_priv->fence_reg_start = 3;
4195
Jesse Barnes0f973f22009-01-26 17:10:45 -08004196 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004197 dev_priv->num_fence_regs = 16;
4198 else
4199 dev_priv->num_fence_regs = 8;
4200
Eric Anholt673a3942008-07-30 12:06:12 -07004201 i915_gem_detect_bit_6_swizzle(dev);
4202}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004203
4204/*
4205 * Create a physically contiguous memory object for this object
4206 * e.g. for cursor + overlay regs
4207 */
4208int i915_gem_init_phys_object(struct drm_device *dev,
4209 int id, int size)
4210{
4211 drm_i915_private_t *dev_priv = dev->dev_private;
4212 struct drm_i915_gem_phys_object *phys_obj;
4213 int ret;
4214
4215 if (dev_priv->mm.phys_objs[id - 1] || !size)
4216 return 0;
4217
4218 phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4219 if (!phys_obj)
4220 return -ENOMEM;
4221
4222 phys_obj->id = id;
4223
4224 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4225 if (!phys_obj->handle) {
4226 ret = -ENOMEM;
4227 goto kfree_obj;
4228 }
4229#ifdef CONFIG_X86
4230 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4231#endif
4232
4233 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4234
4235 return 0;
4236kfree_obj:
4237 drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4238 return ret;
4239}
4240
4241void i915_gem_free_phys_object(struct drm_device *dev, int id)
4242{
4243 drm_i915_private_t *dev_priv = dev->dev_private;
4244 struct drm_i915_gem_phys_object *phys_obj;
4245
4246 if (!dev_priv->mm.phys_objs[id - 1])
4247 return;
4248
4249 phys_obj = dev_priv->mm.phys_objs[id - 1];
4250 if (phys_obj->cur_obj) {
4251 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4252 }
4253
4254#ifdef CONFIG_X86
4255 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4256#endif
4257 drm_pci_free(dev, phys_obj->handle);
4258 kfree(phys_obj);
4259 dev_priv->mm.phys_objs[id - 1] = NULL;
4260}
4261
4262void i915_gem_free_all_phys_object(struct drm_device *dev)
4263{
4264 int i;
4265
Dave Airlie260883c2009-01-22 17:58:49 +10004266 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004267 i915_gem_free_phys_object(dev, i);
4268}
4269
4270void i915_gem_detach_phys_object(struct drm_device *dev,
4271 struct drm_gem_object *obj)
4272{
4273 struct drm_i915_gem_object *obj_priv;
4274 int i;
4275 int ret;
4276 int page_count;
4277
4278 obj_priv = obj->driver_private;
4279 if (!obj_priv->phys_obj)
4280 return;
4281
Eric Anholt856fa192009-03-19 14:10:50 -07004282 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004283 if (ret)
4284 goto out;
4285
4286 page_count = obj->size / PAGE_SIZE;
4287
4288 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004289 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004290 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4291
4292 memcpy(dst, src, PAGE_SIZE);
4293 kunmap_atomic(dst, KM_USER0);
4294 }
Eric Anholt856fa192009-03-19 14:10:50 -07004295 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004296 drm_agp_chipset_flush(dev);
4297out:
4298 obj_priv->phys_obj->cur_obj = NULL;
4299 obj_priv->phys_obj = NULL;
4300}
4301
4302int
4303i915_gem_attach_phys_object(struct drm_device *dev,
4304 struct drm_gem_object *obj, int id)
4305{
4306 drm_i915_private_t *dev_priv = dev->dev_private;
4307 struct drm_i915_gem_object *obj_priv;
4308 int ret = 0;
4309 int page_count;
4310 int i;
4311
4312 if (id > I915_MAX_PHYS_OBJECT)
4313 return -EINVAL;
4314
4315 obj_priv = obj->driver_private;
4316
4317 if (obj_priv->phys_obj) {
4318 if (obj_priv->phys_obj->id == id)
4319 return 0;
4320 i915_gem_detach_phys_object(dev, obj);
4321 }
4322
4323
4324 /* create a new object */
4325 if (!dev_priv->mm.phys_objs[id - 1]) {
4326 ret = i915_gem_init_phys_object(dev, id,
4327 obj->size);
4328 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004329 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004330 goto out;
4331 }
4332 }
4333
4334 /* bind to the object */
4335 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4336 obj_priv->phys_obj->cur_obj = obj;
4337
Eric Anholt856fa192009-03-19 14:10:50 -07004338 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004339 if (ret) {
4340 DRM_ERROR("failed to get page list\n");
4341 goto out;
4342 }
4343
4344 page_count = obj->size / PAGE_SIZE;
4345
4346 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004347 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004348 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4349
4350 memcpy(dst, src, PAGE_SIZE);
4351 kunmap_atomic(src, KM_USER0);
4352 }
4353
4354 return 0;
4355out:
4356 return ret;
4357}
4358
4359static int
4360i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4361 struct drm_i915_gem_pwrite *args,
4362 struct drm_file *file_priv)
4363{
4364 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4365 void *obj_addr;
4366 int ret;
4367 char __user *user_data;
4368
4369 user_data = (char __user *) (uintptr_t) args->data_ptr;
4370 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4371
Dave Airliee08fb4f2009-02-25 14:52:30 +10004372 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004373 ret = copy_from_user(obj_addr, user_data, args->size);
4374 if (ret)
4375 return -EFAULT;
4376
4377 drm_agp_chipset_flush(dev);
4378 return 0;
4379}
Eric Anholtb9624422009-06-03 07:27:35 +00004380
4381void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4382{
4383 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4384
4385 /* Clean up our request list when the client is going away, so that
4386 * later retire_requests won't dereference our soon-to-be-gone
4387 * file_priv.
4388 */
4389 mutex_lock(&dev->struct_mutex);
4390 while (!list_empty(&i915_file_priv->mm.request_list))
4391 list_del_init(i915_file_priv->mm.request_list.next);
4392 mutex_unlock(&dev->struct_mutex);
4393}