blob: 14afc23a0e240301744151f10de5dd464d8f24cc [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
Keith Packardc0d90822008-11-20 23:11:08 -080037static void
38i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
39 uint32_t read_domains,
40 uint32_t write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -080041static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
42static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
43static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080044static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
45 int write);
46static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
47 uint64_t offset,
48 uint64_t size);
49static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070050static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
51static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
52static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080053static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
54 unsigned alignment);
55static void i915_gem_object_get_fence_reg(struct drm_gem_object *obj);
56static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
57static int i915_gem_evict_something(struct drm_device *dev);
Eric Anholt673a3942008-07-30 12:06:12 -070058
Jesse Barnes79e53942008-11-07 14:24:08 -080059int i915_gem_do_init(struct drm_device *dev, unsigned long start,
60 unsigned long end)
61{
62 drm_i915_private_t *dev_priv = dev->dev_private;
63
64 if (start >= end ||
65 (start & (PAGE_SIZE - 1)) != 0 ||
66 (end & (PAGE_SIZE - 1)) != 0) {
67 return -EINVAL;
68 }
69
70 drm_mm_init(&dev_priv->mm.gtt_space, start,
71 end - start);
72
73 dev->gtt_total = (uint32_t) (end - start);
74
75 return 0;
76}
Keith Packard6dbe2772008-10-14 21:41:13 -070077
Eric Anholt673a3942008-07-30 12:06:12 -070078int
79i915_gem_init_ioctl(struct drm_device *dev, void *data,
80 struct drm_file *file_priv)
81{
Eric Anholt673a3942008-07-30 12:06:12 -070082 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080083 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070084
85 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080086 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070087 mutex_unlock(&dev->struct_mutex);
88
Jesse Barnes79e53942008-11-07 14:24:08 -080089 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070090}
91
Eric Anholt5a125c32008-10-22 21:40:13 -070092int
93i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
94 struct drm_file *file_priv)
95{
Eric Anholt5a125c32008-10-22 21:40:13 -070096 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -070097
98 if (!(dev->driver->driver_features & DRIVER_GEM))
99 return -ENODEV;
100
101 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800102 args->aper_available_size = (args->aper_size -
103 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700104
105 return 0;
106}
107
Eric Anholt673a3942008-07-30 12:06:12 -0700108
109/**
110 * Creates a new mm object and returns a handle to it.
111 */
112int
113i915_gem_create_ioctl(struct drm_device *dev, void *data,
114 struct drm_file *file_priv)
115{
116 struct drm_i915_gem_create *args = data;
117 struct drm_gem_object *obj;
118 int handle, ret;
119
120 args->size = roundup(args->size, PAGE_SIZE);
121
122 /* Allocate the new object */
123 obj = drm_gem_object_alloc(dev, args->size);
124 if (obj == NULL)
125 return -ENOMEM;
126
127 ret = drm_gem_handle_create(file_priv, obj, &handle);
128 mutex_lock(&dev->struct_mutex);
129 drm_gem_object_handle_unreference(obj);
130 mutex_unlock(&dev->struct_mutex);
131
132 if (ret)
133 return ret;
134
135 args->handle = handle;
136
137 return 0;
138}
139
140/**
141 * Reads data from the object referenced by handle.
142 *
143 * On error, the contents of *data are undefined.
144 */
145int
146i915_gem_pread_ioctl(struct drm_device *dev, void *data,
147 struct drm_file *file_priv)
148{
149 struct drm_i915_gem_pread *args = data;
150 struct drm_gem_object *obj;
151 struct drm_i915_gem_object *obj_priv;
152 ssize_t read;
153 loff_t offset;
154 int ret;
155
156 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
157 if (obj == NULL)
158 return -EBADF;
159 obj_priv = obj->driver_private;
160
161 /* Bounds check source.
162 *
163 * XXX: This could use review for overflow issues...
164 */
165 if (args->offset > obj->size || args->size > obj->size ||
166 args->offset + args->size > obj->size) {
167 drm_gem_object_unreference(obj);
168 return -EINVAL;
169 }
170
171 mutex_lock(&dev->struct_mutex);
172
Eric Anholte47c68e2008-11-14 13:35:19 -0800173 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
174 args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700175 if (ret != 0) {
176 drm_gem_object_unreference(obj);
177 mutex_unlock(&dev->struct_mutex);
Dave Airliee7d22bc2008-10-07 13:40:36 +1000178 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700179 }
180
181 offset = args->offset;
182
183 read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
184 args->size, &offset);
185 if (read != args->size) {
186 drm_gem_object_unreference(obj);
187 mutex_unlock(&dev->struct_mutex);
188 if (read < 0)
189 return read;
190 else
191 return -EINVAL;
192 }
193
194 drm_gem_object_unreference(obj);
195 mutex_unlock(&dev->struct_mutex);
196
197 return 0;
198}
199
Keith Packard0839ccb2008-10-30 19:38:48 -0700200/* This is the fast write path which cannot handle
201 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700202 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700203
Keith Packard0839ccb2008-10-30 19:38:48 -0700204static inline int
205fast_user_write(struct io_mapping *mapping,
206 loff_t page_base, int page_offset,
207 char __user *user_data,
208 int length)
209{
210 char *vaddr_atomic;
211 unsigned long unwritten;
212
213 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
214 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
215 user_data, length);
216 io_mapping_unmap_atomic(vaddr_atomic);
217 if (unwritten)
218 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700219 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700220}
221
222/* Here's the write path which can sleep for
223 * page faults
224 */
225
226static inline int
227slow_user_write(struct io_mapping *mapping,
228 loff_t page_base, int page_offset,
229 char __user *user_data,
230 int length)
231{
232 char __iomem *vaddr;
233 unsigned long unwritten;
234
235 vaddr = io_mapping_map_wc(mapping, page_base);
236 if (vaddr == NULL)
237 return -EFAULT;
238 unwritten = __copy_from_user(vaddr + page_offset,
239 user_data, length);
240 io_mapping_unmap(vaddr);
241 if (unwritten)
242 return -EFAULT;
243 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700244}
245
Eric Anholt673a3942008-07-30 12:06:12 -0700246static int
247i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
248 struct drm_i915_gem_pwrite *args,
249 struct drm_file *file_priv)
250{
251 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Keith Packard0839ccb2008-10-30 19:38:48 -0700252 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700253 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700254 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700255 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700256 int page_offset, page_length;
257 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700258
259 user_data = (char __user *) (uintptr_t) args->data_ptr;
260 remain = args->size;
261 if (!access_ok(VERIFY_READ, user_data, remain))
262 return -EFAULT;
263
264
265 mutex_lock(&dev->struct_mutex);
266 ret = i915_gem_object_pin(obj, 0);
267 if (ret) {
268 mutex_unlock(&dev->struct_mutex);
269 return ret;
270 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800271 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700272 if (ret)
273 goto fail;
274
275 obj_priv = obj->driver_private;
276 offset = obj_priv->gtt_offset + args->offset;
277 obj_priv->dirty = 1;
278
279 while (remain > 0) {
280 /* Operation in this page
281 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700282 * page_base = page offset within aperture
283 * page_offset = offset within page
284 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700285 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700286 page_base = (offset & ~(PAGE_SIZE-1));
287 page_offset = offset & (PAGE_SIZE-1);
288 page_length = remain;
289 if ((page_offset + remain) > PAGE_SIZE)
290 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700291
Keith Packard0839ccb2008-10-30 19:38:48 -0700292 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
293 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700294
Keith Packard0839ccb2008-10-30 19:38:48 -0700295 /* If we get a fault while copying data, then (presumably) our
296 * source page isn't available. In this case, use the
297 * non-atomic function
298 */
299 if (ret) {
300 ret = slow_user_write (dev_priv->mm.gtt_mapping,
301 page_base, page_offset,
302 user_data, page_length);
303 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -0700304 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700305 }
306
Keith Packard0839ccb2008-10-30 19:38:48 -0700307 remain -= page_length;
308 user_data += page_length;
309 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700310 }
Eric Anholt673a3942008-07-30 12:06:12 -0700311
312fail:
313 i915_gem_object_unpin(obj);
314 mutex_unlock(&dev->struct_mutex);
315
316 return ret;
317}
318
Eric Anholt3043c602008-10-02 12:24:47 -0700319static int
Eric Anholt673a3942008-07-30 12:06:12 -0700320i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
321 struct drm_i915_gem_pwrite *args,
322 struct drm_file *file_priv)
323{
324 int ret;
325 loff_t offset;
326 ssize_t written;
327
328 mutex_lock(&dev->struct_mutex);
329
Eric Anholte47c68e2008-11-14 13:35:19 -0800330 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700331 if (ret) {
332 mutex_unlock(&dev->struct_mutex);
333 return ret;
334 }
335
336 offset = args->offset;
337
338 written = vfs_write(obj->filp,
339 (char __user *)(uintptr_t) args->data_ptr,
340 args->size, &offset);
341 if (written != args->size) {
342 mutex_unlock(&dev->struct_mutex);
343 if (written < 0)
344 return written;
345 else
346 return -EINVAL;
347 }
348
349 mutex_unlock(&dev->struct_mutex);
350
351 return 0;
352}
353
354/**
355 * Writes data to the object referenced by handle.
356 *
357 * On error, the contents of the buffer that were to be modified are undefined.
358 */
359int
360i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
361 struct drm_file *file_priv)
362{
363 struct drm_i915_gem_pwrite *args = data;
364 struct drm_gem_object *obj;
365 struct drm_i915_gem_object *obj_priv;
366 int ret = 0;
367
368 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
369 if (obj == NULL)
370 return -EBADF;
371 obj_priv = obj->driver_private;
372
373 /* Bounds check destination.
374 *
375 * XXX: This could use review for overflow issues...
376 */
377 if (args->offset > obj->size || args->size > obj->size ||
378 args->offset + args->size > obj->size) {
379 drm_gem_object_unreference(obj);
380 return -EINVAL;
381 }
382
383 /* We can only do the GTT pwrite on untiled buffers, as otherwise
384 * it would end up going through the fenced access, and we'll get
385 * different detiling behavior between reading and writing.
386 * pread/pwrite currently are reading and writing from the CPU
387 * perspective, requiring manual detiling by the client.
388 */
389 if (obj_priv->tiling_mode == I915_TILING_NONE &&
390 dev->gtt_total != 0)
391 ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
392 else
393 ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
394
395#if WATCH_PWRITE
396 if (ret)
397 DRM_INFO("pwrite failed %d\n", ret);
398#endif
399
400 drm_gem_object_unreference(obj);
401
402 return ret;
403}
404
405/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800406 * Called when user space prepares to use an object with the CPU, either
407 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700408 */
409int
410i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
411 struct drm_file *file_priv)
412{
413 struct drm_i915_gem_set_domain *args = data;
414 struct drm_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800415 uint32_t read_domains = args->read_domains;
416 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700417 int ret;
418
419 if (!(dev->driver->driver_features & DRIVER_GEM))
420 return -ENODEV;
421
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800422 /* Only handle setting domains to types used by the CPU. */
423 if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
424 return -EINVAL;
425
426 if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
427 return -EINVAL;
428
429 /* Having something in the write domain implies it's in the read
430 * domain, and only that read domain. Enforce that in the request.
431 */
432 if (write_domain != 0 && read_domains != write_domain)
433 return -EINVAL;
434
Eric Anholt673a3942008-07-30 12:06:12 -0700435 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
436 if (obj == NULL)
437 return -EBADF;
438
439 mutex_lock(&dev->struct_mutex);
440#if WATCH_BUF
441 DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800442 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -0700443#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800444 if (read_domains & I915_GEM_DOMAIN_GTT) {
445 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -0800446
447 /* Silently promote "you're not bound, there was nothing to do"
448 * to success, since the client was just asking us to
449 * make sure everything was done.
450 */
451 if (ret == -EINVAL)
452 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800453 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -0800454 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800455 }
456
Eric Anholt673a3942008-07-30 12:06:12 -0700457 drm_gem_object_unreference(obj);
458 mutex_unlock(&dev->struct_mutex);
459 return ret;
460}
461
462/**
463 * Called when user space has done writes to this buffer
464 */
465int
466i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
467 struct drm_file *file_priv)
468{
469 struct drm_i915_gem_sw_finish *args = data;
470 struct drm_gem_object *obj;
471 struct drm_i915_gem_object *obj_priv;
472 int ret = 0;
473
474 if (!(dev->driver->driver_features & DRIVER_GEM))
475 return -ENODEV;
476
477 mutex_lock(&dev->struct_mutex);
478 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
479 if (obj == NULL) {
480 mutex_unlock(&dev->struct_mutex);
481 return -EBADF;
482 }
483
484#if WATCH_BUF
485 DRM_INFO("%s: sw_finish %d (%p %d)\n",
486 __func__, args->handle, obj, obj->size);
487#endif
488 obj_priv = obj->driver_private;
489
490 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -0800491 if (obj_priv->pin_count)
492 i915_gem_object_flush_cpu_write_domain(obj);
493
Eric Anholt673a3942008-07-30 12:06:12 -0700494 drm_gem_object_unreference(obj);
495 mutex_unlock(&dev->struct_mutex);
496 return ret;
497}
498
499/**
500 * Maps the contents of an object, returning the address it is mapped
501 * into.
502 *
503 * While the mapping holds a reference on the contents of the object, it doesn't
504 * imply a ref on the object itself.
505 */
506int
507i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
508 struct drm_file *file_priv)
509{
510 struct drm_i915_gem_mmap *args = data;
511 struct drm_gem_object *obj;
512 loff_t offset;
513 unsigned long addr;
514
515 if (!(dev->driver->driver_features & DRIVER_GEM))
516 return -ENODEV;
517
518 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
519 if (obj == NULL)
520 return -EBADF;
521
522 offset = args->offset;
523
524 down_write(&current->mm->mmap_sem);
525 addr = do_mmap(obj->filp, 0, args->size,
526 PROT_READ | PROT_WRITE, MAP_SHARED,
527 args->offset);
528 up_write(&current->mm->mmap_sem);
529 mutex_lock(&dev->struct_mutex);
530 drm_gem_object_unreference(obj);
531 mutex_unlock(&dev->struct_mutex);
532 if (IS_ERR((void *)addr))
533 return addr;
534
535 args->addr_ptr = (uint64_t) addr;
536
537 return 0;
538}
539
Jesse Barnesde151cf2008-11-12 10:03:55 -0800540/**
541 * i915_gem_fault - fault a page into the GTT
542 * vma: VMA in question
543 * vmf: fault info
544 *
545 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
546 * from userspace. The fault handler takes care of binding the object to
547 * the GTT (if needed), allocating and programming a fence register (again,
548 * only if needed based on whether the old reg is still valid or the object
549 * is tiled) and inserting a new PTE into the faulting process.
550 *
551 * Note that the faulting process may involve evicting existing objects
552 * from the GTT and/or fence registers to make room. So performance may
553 * suffer if the GTT working set is large or there are few fence registers
554 * left.
555 */
556int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
557{
558 struct drm_gem_object *obj = vma->vm_private_data;
559 struct drm_device *dev = obj->dev;
560 struct drm_i915_private *dev_priv = dev->dev_private;
561 struct drm_i915_gem_object *obj_priv = obj->driver_private;
562 pgoff_t page_offset;
563 unsigned long pfn;
564 int ret = 0;
565
566 /* We don't use vmf->pgoff since that has the fake offset */
567 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
568 PAGE_SHIFT;
569
570 /* Now bind it into the GTT if needed */
571 mutex_lock(&dev->struct_mutex);
572 if (!obj_priv->gtt_space) {
573 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
574 if (ret) {
575 mutex_unlock(&dev->struct_mutex);
576 return VM_FAULT_SIGBUS;
577 }
578 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
579 }
580
581 /* Need a new fence register? */
582 if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
583 obj_priv->tiling_mode != I915_TILING_NONE)
584 i915_gem_object_get_fence_reg(obj);
585
586 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
587 page_offset;
588
589 /* Finally, remap it using the new GTT offset */
590 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
591
592 mutex_unlock(&dev->struct_mutex);
593
594 switch (ret) {
595 case -ENOMEM:
596 case -EAGAIN:
597 return VM_FAULT_OOM;
598 case -EFAULT:
599 case -EBUSY:
600 DRM_ERROR("can't insert pfn?? fault or busy...\n");
601 return VM_FAULT_SIGBUS;
602 default:
603 return VM_FAULT_NOPAGE;
604 }
605}
606
607/**
608 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
609 * @obj: obj in question
610 *
611 * GEM memory mapping works by handing back to userspace a fake mmap offset
612 * it can use in a subsequent mmap(2) call. The DRM core code then looks
613 * up the object based on the offset and sets up the various memory mapping
614 * structures.
615 *
616 * This routine allocates and attaches a fake offset for @obj.
617 */
618static int
619i915_gem_create_mmap_offset(struct drm_gem_object *obj)
620{
621 struct drm_device *dev = obj->dev;
622 struct drm_gem_mm *mm = dev->mm_private;
623 struct drm_i915_gem_object *obj_priv = obj->driver_private;
624 struct drm_map_list *list;
625 struct drm_map *map;
626 int ret = 0;
627
628 /* Set the object up for mmap'ing */
629 list = &obj->map_list;
630 list->map = drm_calloc(1, sizeof(struct drm_map_list),
631 DRM_MEM_DRIVER);
632 if (!list->map)
633 return -ENOMEM;
634
635 map = list->map;
636 map->type = _DRM_GEM;
637 map->size = obj->size;
638 map->handle = obj;
639
640 /* Get a DRM GEM mmap offset allocated... */
641 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
642 obj->size / PAGE_SIZE, 0, 0);
643 if (!list->file_offset_node) {
644 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
645 ret = -ENOMEM;
646 goto out_free_list;
647 }
648
649 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
650 obj->size / PAGE_SIZE, 0);
651 if (!list->file_offset_node) {
652 ret = -ENOMEM;
653 goto out_free_list;
654 }
655
656 list->hash.key = list->file_offset_node->start;
657 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
658 DRM_ERROR("failed to add to map hash\n");
659 goto out_free_mm;
660 }
661
662 /* By now we should be all set, any drm_mmap request on the offset
663 * below will get to our mmap & fault handler */
664 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
665
666 return 0;
667
668out_free_mm:
669 drm_mm_put_block(list->file_offset_node);
670out_free_list:
671 drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
672
673 return ret;
674}
675
676/**
677 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
678 * @obj: object to check
679 *
680 * Return the required GTT alignment for an object, taking into account
681 * potential fence register mapping if needed.
682 */
683static uint32_t
684i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
685{
686 struct drm_device *dev = obj->dev;
687 struct drm_i915_gem_object *obj_priv = obj->driver_private;
688 int start, i;
689
690 /*
691 * Minimum alignment is 4k (GTT page size), but might be greater
692 * if a fence register is needed for the object.
693 */
694 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
695 return 4096;
696
697 /*
698 * Previous chips need to be aligned to the size of the smallest
699 * fence register that can contain the object.
700 */
701 if (IS_I9XX(dev))
702 start = 1024*1024;
703 else
704 start = 512*1024;
705
706 for (i = start; i < obj->size; i <<= 1)
707 ;
708
709 return i;
710}
711
712/**
713 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
714 * @dev: DRM device
715 * @data: GTT mapping ioctl data
716 * @file_priv: GEM object info
717 *
718 * Simply returns the fake offset to userspace so it can mmap it.
719 * The mmap call will end up in drm_gem_mmap(), which will set things
720 * up so we can get faults in the handler above.
721 *
722 * The fault handler will take care of binding the object into the GTT
723 * (since it may have been evicted to make room for something), allocating
724 * a fence register, and mapping the appropriate aperture address into
725 * userspace.
726 */
727int
728i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
729 struct drm_file *file_priv)
730{
731 struct drm_i915_gem_mmap_gtt *args = data;
732 struct drm_i915_private *dev_priv = dev->dev_private;
733 struct drm_gem_object *obj;
734 struct drm_i915_gem_object *obj_priv;
735 int ret;
736
737 if (!(dev->driver->driver_features & DRIVER_GEM))
738 return -ENODEV;
739
740 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
741 if (obj == NULL)
742 return -EBADF;
743
744 mutex_lock(&dev->struct_mutex);
745
746 obj_priv = obj->driver_private;
747
748 if (!obj_priv->mmap_offset) {
749 ret = i915_gem_create_mmap_offset(obj);
750 if (ret)
751 return ret;
752 }
753
754 args->offset = obj_priv->mmap_offset;
755
756 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
757
758 /* Make sure the alignment is correct for fence regs etc */
759 if (obj_priv->agp_mem &&
760 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
761 drm_gem_object_unreference(obj);
762 mutex_unlock(&dev->struct_mutex);
763 return -EINVAL;
764 }
765
766 /*
767 * Pull it into the GTT so that we have a page list (makes the
768 * initial fault faster and any subsequent flushing possible).
769 */
770 if (!obj_priv->agp_mem) {
771 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
772 if (ret) {
773 drm_gem_object_unreference(obj);
774 mutex_unlock(&dev->struct_mutex);
775 return ret;
776 }
777 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
778 }
779
780 drm_gem_object_unreference(obj);
781 mutex_unlock(&dev->struct_mutex);
782
783 return 0;
784}
785
Eric Anholt673a3942008-07-30 12:06:12 -0700786static void
787i915_gem_object_free_page_list(struct drm_gem_object *obj)
788{
789 struct drm_i915_gem_object *obj_priv = obj->driver_private;
790 int page_count = obj->size / PAGE_SIZE;
791 int i;
792
793 if (obj_priv->page_list == NULL)
794 return;
795
796
797 for (i = 0; i < page_count; i++)
798 if (obj_priv->page_list[i] != NULL) {
799 if (obj_priv->dirty)
800 set_page_dirty(obj_priv->page_list[i]);
801 mark_page_accessed(obj_priv->page_list[i]);
802 page_cache_release(obj_priv->page_list[i]);
803 }
804 obj_priv->dirty = 0;
805
806 drm_free(obj_priv->page_list,
807 page_count * sizeof(struct page *),
808 DRM_MEM_DRIVER);
809 obj_priv->page_list = NULL;
810}
811
812static void
Eric Anholtce44b0e2008-11-06 16:00:31 -0800813i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -0700814{
815 struct drm_device *dev = obj->dev;
816 drm_i915_private_t *dev_priv = dev->dev_private;
817 struct drm_i915_gem_object *obj_priv = obj->driver_private;
818
819 /* Add a reference if we're newly entering the active list. */
820 if (!obj_priv->active) {
821 drm_gem_object_reference(obj);
822 obj_priv->active = 1;
823 }
824 /* Move from whatever list we were on to the tail of execution. */
825 list_move_tail(&obj_priv->list,
826 &dev_priv->mm.active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -0800827 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -0700828}
829
Eric Anholtce44b0e2008-11-06 16:00:31 -0800830static void
831i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
832{
833 struct drm_device *dev = obj->dev;
834 drm_i915_private_t *dev_priv = dev->dev_private;
835 struct drm_i915_gem_object *obj_priv = obj->driver_private;
836
837 BUG_ON(!obj_priv->active);
838 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
839 obj_priv->last_rendering_seqno = 0;
840}
Eric Anholt673a3942008-07-30 12:06:12 -0700841
842static void
843i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
844{
845 struct drm_device *dev = obj->dev;
846 drm_i915_private_t *dev_priv = dev->dev_private;
847 struct drm_i915_gem_object *obj_priv = obj->driver_private;
848
849 i915_verify_inactive(dev, __FILE__, __LINE__);
850 if (obj_priv->pin_count != 0)
851 list_del_init(&obj_priv->list);
852 else
853 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
854
Eric Anholtce44b0e2008-11-06 16:00:31 -0800855 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700856 if (obj_priv->active) {
857 obj_priv->active = 0;
858 drm_gem_object_unreference(obj);
859 }
860 i915_verify_inactive(dev, __FILE__, __LINE__);
861}
862
863/**
864 * Creates a new sequence number, emitting a write of it to the status page
865 * plus an interrupt, which will trigger i915_user_interrupt_handler.
866 *
867 * Must be called with struct_lock held.
868 *
869 * Returned sequence numbers are nonzero on success.
870 */
871static uint32_t
872i915_add_request(struct drm_device *dev, uint32_t flush_domains)
873{
874 drm_i915_private_t *dev_priv = dev->dev_private;
875 struct drm_i915_gem_request *request;
876 uint32_t seqno;
877 int was_empty;
878 RING_LOCALS;
879
880 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
881 if (request == NULL)
882 return 0;
883
884 /* Grab the seqno we're going to make this request be, and bump the
885 * next (skipping 0 so it can be the reserved no-seqno value).
886 */
887 seqno = dev_priv->mm.next_gem_seqno;
888 dev_priv->mm.next_gem_seqno++;
889 if (dev_priv->mm.next_gem_seqno == 0)
890 dev_priv->mm.next_gem_seqno++;
891
892 BEGIN_LP_RING(4);
893 OUT_RING(MI_STORE_DWORD_INDEX);
894 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
895 OUT_RING(seqno);
896
897 OUT_RING(MI_USER_INTERRUPT);
898 ADVANCE_LP_RING();
899
900 DRM_DEBUG("%d\n", seqno);
901
902 request->seqno = seqno;
903 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -0700904 was_empty = list_empty(&dev_priv->mm.request_list);
905 list_add_tail(&request->list, &dev_priv->mm.request_list);
906
Eric Anholtce44b0e2008-11-06 16:00:31 -0800907 /* Associate any objects on the flushing list matching the write
908 * domain we're flushing with our flush.
909 */
910 if (flush_domains != 0) {
911 struct drm_i915_gem_object *obj_priv, *next;
912
913 list_for_each_entry_safe(obj_priv, next,
914 &dev_priv->mm.flushing_list, list) {
915 struct drm_gem_object *obj = obj_priv->obj;
916
917 if ((obj->write_domain & flush_domains) ==
918 obj->write_domain) {
919 obj->write_domain = 0;
920 i915_gem_object_move_to_active(obj, seqno);
921 }
922 }
923
924 }
925
Keith Packard6dbe2772008-10-14 21:41:13 -0700926 if (was_empty && !dev_priv->mm.suspended)
Eric Anholt673a3942008-07-30 12:06:12 -0700927 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
928 return seqno;
929}
930
931/**
932 * Command execution barrier
933 *
934 * Ensures that all commands in the ring are finished
935 * before signalling the CPU
936 */
Eric Anholt3043c602008-10-02 12:24:47 -0700937static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -0700938i915_retire_commands(struct drm_device *dev)
939{
940 drm_i915_private_t *dev_priv = dev->dev_private;
941 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
942 uint32_t flush_domains = 0;
943 RING_LOCALS;
944
945 /* The sampler always gets flushed on i965 (sigh) */
946 if (IS_I965G(dev))
947 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
948 BEGIN_LP_RING(2);
949 OUT_RING(cmd);
950 OUT_RING(0); /* noop */
951 ADVANCE_LP_RING();
952 return flush_domains;
953}
954
955/**
956 * Moves buffers associated only with the given active seqno from the active
957 * to inactive list, potentially freeing them.
958 */
959static void
960i915_gem_retire_request(struct drm_device *dev,
961 struct drm_i915_gem_request *request)
962{
963 drm_i915_private_t *dev_priv = dev->dev_private;
964
965 /* Move any buffers on the active list that are no longer referenced
966 * by the ringbuffer to the flushing/inactive lists as appropriate.
967 */
968 while (!list_empty(&dev_priv->mm.active_list)) {
969 struct drm_gem_object *obj;
970 struct drm_i915_gem_object *obj_priv;
971
972 obj_priv = list_first_entry(&dev_priv->mm.active_list,
973 struct drm_i915_gem_object,
974 list);
975 obj = obj_priv->obj;
976
977 /* If the seqno being retired doesn't match the oldest in the
978 * list, then the oldest in the list must still be newer than
979 * this seqno.
980 */
981 if (obj_priv->last_rendering_seqno != request->seqno)
982 return;
Jesse Barnesde151cf2008-11-12 10:03:55 -0800983
Eric Anholt673a3942008-07-30 12:06:12 -0700984#if WATCH_LRU
985 DRM_INFO("%s: retire %d moves to inactive list %p\n",
986 __func__, request->seqno, obj);
987#endif
988
Eric Anholtce44b0e2008-11-06 16:00:31 -0800989 if (obj->write_domain != 0)
990 i915_gem_object_move_to_flushing(obj);
991 else
Eric Anholt673a3942008-07-30 12:06:12 -0700992 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700993 }
994}
995
996/**
997 * Returns true if seq1 is later than seq2.
998 */
999static int
1000i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1001{
1002 return (int32_t)(seq1 - seq2) >= 0;
1003}
1004
1005uint32_t
1006i915_get_gem_seqno(struct drm_device *dev)
1007{
1008 drm_i915_private_t *dev_priv = dev->dev_private;
1009
1010 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1011}
1012
1013/**
1014 * This function clears the request list as sequence numbers are passed.
1015 */
1016void
1017i915_gem_retire_requests(struct drm_device *dev)
1018{
1019 drm_i915_private_t *dev_priv = dev->dev_private;
1020 uint32_t seqno;
1021
1022 seqno = i915_get_gem_seqno(dev);
1023
1024 while (!list_empty(&dev_priv->mm.request_list)) {
1025 struct drm_i915_gem_request *request;
1026 uint32_t retiring_seqno;
1027
1028 request = list_first_entry(&dev_priv->mm.request_list,
1029 struct drm_i915_gem_request,
1030 list);
1031 retiring_seqno = request->seqno;
1032
1033 if (i915_seqno_passed(seqno, retiring_seqno) ||
1034 dev_priv->mm.wedged) {
1035 i915_gem_retire_request(dev, request);
1036
1037 list_del(&request->list);
1038 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
1039 } else
1040 break;
1041 }
1042}
1043
1044void
1045i915_gem_retire_work_handler(struct work_struct *work)
1046{
1047 drm_i915_private_t *dev_priv;
1048 struct drm_device *dev;
1049
1050 dev_priv = container_of(work, drm_i915_private_t,
1051 mm.retire_work.work);
1052 dev = dev_priv->dev;
1053
1054 mutex_lock(&dev->struct_mutex);
1055 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001056 if (!dev_priv->mm.suspended &&
1057 !list_empty(&dev_priv->mm.request_list))
Eric Anholt673a3942008-07-30 12:06:12 -07001058 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1059 mutex_unlock(&dev->struct_mutex);
1060}
1061
1062/**
1063 * Waits for a sequence number to be signaled, and cleans up the
1064 * request and object lists appropriately for that event.
1065 */
Eric Anholt3043c602008-10-02 12:24:47 -07001066static int
Eric Anholt673a3942008-07-30 12:06:12 -07001067i915_wait_request(struct drm_device *dev, uint32_t seqno)
1068{
1069 drm_i915_private_t *dev_priv = dev->dev_private;
1070 int ret = 0;
1071
1072 BUG_ON(seqno == 0);
1073
1074 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
1075 dev_priv->mm.waiting_gem_seqno = seqno;
1076 i915_user_irq_get(dev);
1077 ret = wait_event_interruptible(dev_priv->irq_queue,
1078 i915_seqno_passed(i915_get_gem_seqno(dev),
1079 seqno) ||
1080 dev_priv->mm.wedged);
1081 i915_user_irq_put(dev);
1082 dev_priv->mm.waiting_gem_seqno = 0;
1083 }
1084 if (dev_priv->mm.wedged)
1085 ret = -EIO;
1086
1087 if (ret && ret != -ERESTARTSYS)
1088 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1089 __func__, ret, seqno, i915_get_gem_seqno(dev));
1090
1091 /* Directly dispatch request retiring. While we have the work queue
1092 * to handle this, the waiter on a request often wants an associated
1093 * buffer to have made it to the inactive list, and we would need
1094 * a separate wait queue to handle that.
1095 */
1096 if (ret == 0)
1097 i915_gem_retire_requests(dev);
1098
1099 return ret;
1100}
1101
1102static void
1103i915_gem_flush(struct drm_device *dev,
1104 uint32_t invalidate_domains,
1105 uint32_t flush_domains)
1106{
1107 drm_i915_private_t *dev_priv = dev->dev_private;
1108 uint32_t cmd;
1109 RING_LOCALS;
1110
1111#if WATCH_EXEC
1112 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1113 invalidate_domains, flush_domains);
1114#endif
1115
1116 if (flush_domains & I915_GEM_DOMAIN_CPU)
1117 drm_agp_chipset_flush(dev);
1118
1119 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
1120 I915_GEM_DOMAIN_GTT)) {
1121 /*
1122 * read/write caches:
1123 *
1124 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1125 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1126 * also flushed at 2d versus 3d pipeline switches.
1127 *
1128 * read-only caches:
1129 *
1130 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1131 * MI_READ_FLUSH is set, and is always flushed on 965.
1132 *
1133 * I915_GEM_DOMAIN_COMMAND may not exist?
1134 *
1135 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1136 * invalidated when MI_EXE_FLUSH is set.
1137 *
1138 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1139 * invalidated with every MI_FLUSH.
1140 *
1141 * TLBs:
1142 *
1143 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1144 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1145 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1146 * are flushed at any MI_FLUSH.
1147 */
1148
1149 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1150 if ((invalidate_domains|flush_domains) &
1151 I915_GEM_DOMAIN_RENDER)
1152 cmd &= ~MI_NO_WRITE_FLUSH;
1153 if (!IS_I965G(dev)) {
1154 /*
1155 * On the 965, the sampler cache always gets flushed
1156 * and this bit is reserved.
1157 */
1158 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1159 cmd |= MI_READ_FLUSH;
1160 }
1161 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1162 cmd |= MI_EXE_FLUSH;
1163
1164#if WATCH_EXEC
1165 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1166#endif
1167 BEGIN_LP_RING(2);
1168 OUT_RING(cmd);
1169 OUT_RING(0); /* noop */
1170 ADVANCE_LP_RING();
1171 }
1172}
1173
1174/**
1175 * Ensures that all rendering to the object has completed and the object is
1176 * safe to unbind from the GTT or access from the CPU.
1177 */
1178static int
1179i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1180{
1181 struct drm_device *dev = obj->dev;
1182 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1183 int ret;
1184
Eric Anholte47c68e2008-11-14 13:35:19 -08001185 /* This function only exists to support waiting for existing rendering,
1186 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001187 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001188 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001189
1190 /* If there is rendering queued on the buffer being evicted, wait for
1191 * it.
1192 */
1193 if (obj_priv->active) {
1194#if WATCH_BUF
1195 DRM_INFO("%s: object %p wait for seqno %08x\n",
1196 __func__, obj, obj_priv->last_rendering_seqno);
1197#endif
1198 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1199 if (ret != 0)
1200 return ret;
1201 }
1202
1203 return 0;
1204}
1205
1206/**
1207 * Unbinds an object from the GTT aperture.
1208 */
1209static int
1210i915_gem_object_unbind(struct drm_gem_object *obj)
1211{
1212 struct drm_device *dev = obj->dev;
1213 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001214 loff_t offset;
Eric Anholt673a3942008-07-30 12:06:12 -07001215 int ret = 0;
1216
1217#if WATCH_BUF
1218 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1219 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1220#endif
1221 if (obj_priv->gtt_space == NULL)
1222 return 0;
1223
1224 if (obj_priv->pin_count != 0) {
1225 DRM_ERROR("Attempting to unbind pinned buffer\n");
1226 return -EINVAL;
1227 }
1228
Eric Anholt673a3942008-07-30 12:06:12 -07001229 /* Move the object to the CPU domain to ensure that
1230 * any possible CPU writes while it's not in the GTT
1231 * are flushed when we go to remap it. This will
1232 * also ensure that all pending GPU writes are finished
1233 * before we unbind.
1234 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001235 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001236 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001237 if (ret != -ERESTARTSYS)
1238 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001239 return ret;
1240 }
1241
1242 if (obj_priv->agp_mem != NULL) {
1243 drm_unbind_agp(obj_priv->agp_mem);
1244 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1245 obj_priv->agp_mem = NULL;
1246 }
1247
1248 BUG_ON(obj_priv->active);
1249
Jesse Barnesde151cf2008-11-12 10:03:55 -08001250 /* blow away mappings if mapped through GTT */
1251 offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08001252 if (dev->dev_mapping)
1253 unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001254
1255 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1256 i915_gem_clear_fence_reg(obj);
1257
Eric Anholt673a3942008-07-30 12:06:12 -07001258 i915_gem_object_free_page_list(obj);
1259
1260 if (obj_priv->gtt_space) {
1261 atomic_dec(&dev->gtt_count);
1262 atomic_sub(obj->size, &dev->gtt_memory);
1263
1264 drm_mm_put_block(obj_priv->gtt_space);
1265 obj_priv->gtt_space = NULL;
1266 }
1267
1268 /* Remove ourselves from the LRU list if present. */
1269 if (!list_empty(&obj_priv->list))
1270 list_del_init(&obj_priv->list);
1271
1272 return 0;
1273}
1274
1275static int
1276i915_gem_evict_something(struct drm_device *dev)
1277{
1278 drm_i915_private_t *dev_priv = dev->dev_private;
1279 struct drm_gem_object *obj;
1280 struct drm_i915_gem_object *obj_priv;
1281 int ret = 0;
1282
1283 for (;;) {
1284 /* If there's an inactive buffer available now, grab it
1285 * and be done.
1286 */
1287 if (!list_empty(&dev_priv->mm.inactive_list)) {
1288 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1289 struct drm_i915_gem_object,
1290 list);
1291 obj = obj_priv->obj;
1292 BUG_ON(obj_priv->pin_count != 0);
1293#if WATCH_LRU
1294 DRM_INFO("%s: evicting %p\n", __func__, obj);
1295#endif
1296 BUG_ON(obj_priv->active);
1297
1298 /* Wait on the rendering and unbind the buffer. */
1299 ret = i915_gem_object_unbind(obj);
1300 break;
1301 }
1302
1303 /* If we didn't get anything, but the ring is still processing
1304 * things, wait for one of those things to finish and hopefully
1305 * leave us a buffer to evict.
1306 */
1307 if (!list_empty(&dev_priv->mm.request_list)) {
1308 struct drm_i915_gem_request *request;
1309
1310 request = list_first_entry(&dev_priv->mm.request_list,
1311 struct drm_i915_gem_request,
1312 list);
1313
1314 ret = i915_wait_request(dev, request->seqno);
1315 if (ret)
1316 break;
1317
1318 /* if waiting caused an object to become inactive,
1319 * then loop around and wait for it. Otherwise, we
1320 * assume that waiting freed and unbound something,
1321 * so there should now be some space in the GTT
1322 */
1323 if (!list_empty(&dev_priv->mm.inactive_list))
1324 continue;
1325 break;
1326 }
1327
1328 /* If we didn't have anything on the request list but there
1329 * are buffers awaiting a flush, emit one and try again.
1330 * When we wait on it, those buffers waiting for that flush
1331 * will get moved to inactive.
1332 */
1333 if (!list_empty(&dev_priv->mm.flushing_list)) {
1334 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1335 struct drm_i915_gem_object,
1336 list);
1337 obj = obj_priv->obj;
1338
1339 i915_gem_flush(dev,
1340 obj->write_domain,
1341 obj->write_domain);
1342 i915_add_request(dev, obj->write_domain);
1343
1344 obj = NULL;
1345 continue;
1346 }
1347
1348 DRM_ERROR("inactive empty %d request empty %d "
1349 "flushing empty %d\n",
1350 list_empty(&dev_priv->mm.inactive_list),
1351 list_empty(&dev_priv->mm.request_list),
1352 list_empty(&dev_priv->mm.flushing_list));
1353 /* If we didn't do any of the above, there's nothing to be done
1354 * and we just can't fit it in.
1355 */
1356 return -ENOMEM;
1357 }
1358 return ret;
1359}
1360
1361static int
Keith Packardac94a962008-11-20 23:30:27 -08001362i915_gem_evict_everything(struct drm_device *dev)
1363{
1364 int ret;
1365
1366 for (;;) {
1367 ret = i915_gem_evict_something(dev);
1368 if (ret != 0)
1369 break;
1370 }
Owain Ainsworth15c35332008-12-06 20:42:20 -08001371 if (ret == -ENOMEM)
1372 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08001373 return ret;
1374}
1375
1376static int
Eric Anholt673a3942008-07-30 12:06:12 -07001377i915_gem_object_get_page_list(struct drm_gem_object *obj)
1378{
1379 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1380 int page_count, i;
1381 struct address_space *mapping;
1382 struct inode *inode;
1383 struct page *page;
1384 int ret;
1385
1386 if (obj_priv->page_list)
1387 return 0;
1388
1389 /* Get the list of pages out of our struct file. They'll be pinned
1390 * at this point until we release them.
1391 */
1392 page_count = obj->size / PAGE_SIZE;
1393 BUG_ON(obj_priv->page_list != NULL);
1394 obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
1395 DRM_MEM_DRIVER);
1396 if (obj_priv->page_list == NULL) {
1397 DRM_ERROR("Faled to allocate page list\n");
1398 return -ENOMEM;
1399 }
1400
1401 inode = obj->filp->f_path.dentry->d_inode;
1402 mapping = inode->i_mapping;
1403 for (i = 0; i < page_count; i++) {
1404 page = read_mapping_page(mapping, i, NULL);
1405 if (IS_ERR(page)) {
1406 ret = PTR_ERR(page);
1407 DRM_ERROR("read_mapping_page failed: %d\n", ret);
1408 i915_gem_object_free_page_list(obj);
1409 return ret;
1410 }
1411 obj_priv->page_list[i] = page;
1412 }
1413 return 0;
1414}
1415
Jesse Barnesde151cf2008-11-12 10:03:55 -08001416static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
1417{
1418 struct drm_gem_object *obj = reg->obj;
1419 struct drm_device *dev = obj->dev;
1420 drm_i915_private_t *dev_priv = dev->dev_private;
1421 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1422 int regnum = obj_priv->fence_reg;
1423 uint64_t val;
1424
1425 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
1426 0xfffff000) << 32;
1427 val |= obj_priv->gtt_offset & 0xfffff000;
1428 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
1429 if (obj_priv->tiling_mode == I915_TILING_Y)
1430 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
1431 val |= I965_FENCE_REG_VALID;
1432
1433 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
1434}
1435
1436static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
1437{
1438 struct drm_gem_object *obj = reg->obj;
1439 struct drm_device *dev = obj->dev;
1440 drm_i915_private_t *dev_priv = dev->dev_private;
1441 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1442 int regnum = obj_priv->fence_reg;
1443 uint32_t val;
1444 uint32_t pitch_val;
1445
1446 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1447 (obj_priv->gtt_offset & (obj->size - 1))) {
1448 WARN(1, "%s: object not 1M or size aligned\n", __FUNCTION__);
1449 return;
1450 }
1451
1452 if (obj_priv->tiling_mode == I915_TILING_Y && (IS_I945G(dev) ||
1453 IS_I945GM(dev) ||
1454 IS_G33(dev)))
1455 pitch_val = (obj_priv->stride / 128) - 1;
1456 else
1457 pitch_val = (obj_priv->stride / 512) - 1;
1458
1459 val = obj_priv->gtt_offset;
1460 if (obj_priv->tiling_mode == I915_TILING_Y)
1461 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1462 val |= I915_FENCE_SIZE_BITS(obj->size);
1463 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1464 val |= I830_FENCE_REG_VALID;
1465
1466 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
1467}
1468
1469static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
1470{
1471 struct drm_gem_object *obj = reg->obj;
1472 struct drm_device *dev = obj->dev;
1473 drm_i915_private_t *dev_priv = dev->dev_private;
1474 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1475 int regnum = obj_priv->fence_reg;
1476 uint32_t val;
1477 uint32_t pitch_val;
1478
1479 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1480 (obj_priv->gtt_offset & (obj->size - 1))) {
1481 WARN(1, "%s: object not 1M or size aligned\n", __FUNCTION__);
1482 return;
1483 }
1484
1485 pitch_val = (obj_priv->stride / 128) - 1;
1486
1487 val = obj_priv->gtt_offset;
1488 if (obj_priv->tiling_mode == I915_TILING_Y)
1489 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1490 val |= I830_FENCE_SIZE_BITS(obj->size);
1491 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1492 val |= I830_FENCE_REG_VALID;
1493
1494 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
1495
1496}
1497
1498/**
1499 * i915_gem_object_get_fence_reg - set up a fence reg for an object
1500 * @obj: object to map through a fence reg
1501 *
1502 * When mapping objects through the GTT, userspace wants to be able to write
1503 * to them without having to worry about swizzling if the object is tiled.
1504 *
1505 * This function walks the fence regs looking for a free one for @obj,
1506 * stealing one if it can't find any.
1507 *
1508 * It then sets up the reg based on the object's properties: address, pitch
1509 * and tiling format.
1510 */
1511static void
1512i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
1513{
1514 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001515 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001516 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1517 struct drm_i915_fence_reg *reg = NULL;
1518 int i, ret;
1519
1520 switch (obj_priv->tiling_mode) {
1521 case I915_TILING_NONE:
1522 WARN(1, "allocating a fence for non-tiled object?\n");
1523 break;
1524 case I915_TILING_X:
1525 WARN(obj_priv->stride & (512 - 1),
1526 "object is X tiled but has non-512B pitch\n");
1527 break;
1528 case I915_TILING_Y:
1529 WARN(obj_priv->stride & (128 - 1),
1530 "object is Y tiled but has non-128B pitch\n");
1531 break;
1532 }
1533
1534 /* First try to find a free reg */
1535 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
1536 reg = &dev_priv->fence_regs[i];
1537 if (!reg->obj)
1538 break;
1539 }
1540
1541 /* None available, try to steal one or wait for a user to finish */
1542 if (i == dev_priv->num_fence_regs) {
1543 struct drm_i915_gem_object *old_obj_priv = NULL;
1544 loff_t offset;
1545
1546try_again:
1547 /* Could try to use LRU here instead... */
1548 for (i = dev_priv->fence_reg_start;
1549 i < dev_priv->num_fence_regs; i++) {
1550 reg = &dev_priv->fence_regs[i];
1551 old_obj_priv = reg->obj->driver_private;
1552 if (!old_obj_priv->pin_count)
1553 break;
1554 }
1555
1556 /*
1557 * Now things get ugly... we have to wait for one of the
1558 * objects to finish before trying again.
1559 */
1560 if (i == dev_priv->num_fence_regs) {
1561 ret = i915_gem_object_wait_rendering(reg->obj);
1562 if (ret) {
1563 WARN(ret, "wait_rendering failed: %d\n", ret);
1564 return;
1565 }
1566 goto try_again;
1567 }
1568
1569 /*
1570 * Zap this virtual mapping so we can set up a fence again
1571 * for this object next time we need it.
1572 */
1573 offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08001574 if (dev->dev_mapping)
1575 unmap_mapping_range(dev->dev_mapping, offset,
1576 reg->obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001577 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
1578 }
1579
1580 obj_priv->fence_reg = i;
1581 reg->obj = obj;
1582
1583 if (IS_I965G(dev))
1584 i965_write_fence_reg(reg);
1585 else if (IS_I9XX(dev))
1586 i915_write_fence_reg(reg);
1587 else
1588 i830_write_fence_reg(reg);
1589}
1590
1591/**
1592 * i915_gem_clear_fence_reg - clear out fence register info
1593 * @obj: object to clear
1594 *
1595 * Zeroes out the fence register itself and clears out the associated
1596 * data structures in dev_priv and obj_priv.
1597 */
1598static void
1599i915_gem_clear_fence_reg(struct drm_gem_object *obj)
1600{
1601 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001602 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001603 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1604
1605 if (IS_I965G(dev))
1606 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
1607 else
1608 I915_WRITE(FENCE_REG_830_0 + (obj_priv->fence_reg * 4), 0);
1609
1610 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
1611 obj_priv->fence_reg = I915_FENCE_REG_NONE;
1612}
1613
Eric Anholt673a3942008-07-30 12:06:12 -07001614/**
1615 * Finds free space in the GTT aperture and binds the object there.
1616 */
1617static int
1618i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
1619{
1620 struct drm_device *dev = obj->dev;
1621 drm_i915_private_t *dev_priv = dev->dev_private;
1622 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1623 struct drm_mm_node *free_space;
1624 int page_count, ret;
1625
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08001626 if (dev_priv->mm.suspended)
1627 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07001628 if (alignment == 0)
1629 alignment = PAGE_SIZE;
1630 if (alignment & (PAGE_SIZE - 1)) {
1631 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
1632 return -EINVAL;
1633 }
1634
1635 search_free:
1636 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
1637 obj->size, alignment, 0);
1638 if (free_space != NULL) {
1639 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
1640 alignment);
1641 if (obj_priv->gtt_space != NULL) {
1642 obj_priv->gtt_space->private = obj;
1643 obj_priv->gtt_offset = obj_priv->gtt_space->start;
1644 }
1645 }
1646 if (obj_priv->gtt_space == NULL) {
1647 /* If the gtt is empty and we're still having trouble
1648 * fitting our object in, we're out of memory.
1649 */
1650#if WATCH_LRU
1651 DRM_INFO("%s: GTT full, evicting something\n", __func__);
1652#endif
1653 if (list_empty(&dev_priv->mm.inactive_list) &&
1654 list_empty(&dev_priv->mm.flushing_list) &&
1655 list_empty(&dev_priv->mm.active_list)) {
1656 DRM_ERROR("GTT full, but LRU list empty\n");
1657 return -ENOMEM;
1658 }
1659
1660 ret = i915_gem_evict_something(dev);
1661 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08001662 if (ret != -ERESTARTSYS)
1663 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001664 return ret;
1665 }
1666 goto search_free;
1667 }
1668
1669#if WATCH_BUF
1670 DRM_INFO("Binding object of size %d at 0x%08x\n",
1671 obj->size, obj_priv->gtt_offset);
1672#endif
1673 ret = i915_gem_object_get_page_list(obj);
1674 if (ret) {
1675 drm_mm_put_block(obj_priv->gtt_space);
1676 obj_priv->gtt_space = NULL;
1677 return ret;
1678 }
1679
1680 page_count = obj->size / PAGE_SIZE;
1681 /* Create an AGP memory structure pointing at our pages, and bind it
1682 * into the GTT.
1683 */
1684 obj_priv->agp_mem = drm_agp_bind_pages(dev,
1685 obj_priv->page_list,
1686 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07001687 obj_priv->gtt_offset,
1688 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07001689 if (obj_priv->agp_mem == NULL) {
1690 i915_gem_object_free_page_list(obj);
1691 drm_mm_put_block(obj_priv->gtt_space);
1692 obj_priv->gtt_space = NULL;
1693 return -ENOMEM;
1694 }
1695 atomic_inc(&dev->gtt_count);
1696 atomic_add(obj->size, &dev->gtt_memory);
1697
1698 /* Assert that the object is not currently in any GPU domain. As it
1699 * wasn't in the GTT, there shouldn't be any way it could have been in
1700 * a GPU cache
1701 */
1702 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1703 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1704
1705 return 0;
1706}
1707
1708void
1709i915_gem_clflush_object(struct drm_gem_object *obj)
1710{
1711 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1712
1713 /* If we don't have a page list set up, then we're not pinned
1714 * to GPU, and we can ignore the cache flush because it'll happen
1715 * again at bind time.
1716 */
1717 if (obj_priv->page_list == NULL)
1718 return;
1719
1720 drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
1721}
1722
Eric Anholte47c68e2008-11-14 13:35:19 -08001723/** Flushes any GPU write domain for the object if it's dirty. */
1724static void
1725i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
1726{
1727 struct drm_device *dev = obj->dev;
1728 uint32_t seqno;
1729
1730 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
1731 return;
1732
1733 /* Queue the GPU write cache flushing we need. */
1734 i915_gem_flush(dev, 0, obj->write_domain);
1735 seqno = i915_add_request(dev, obj->write_domain);
1736 obj->write_domain = 0;
1737 i915_gem_object_move_to_active(obj, seqno);
1738}
1739
1740/** Flushes the GTT write domain for the object if it's dirty. */
1741static void
1742i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
1743{
1744 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
1745 return;
1746
1747 /* No actual flushing is required for the GTT write domain. Writes
1748 * to it immediately go to main memory as far as we know, so there's
1749 * no chipset flush. It also doesn't land in render cache.
1750 */
1751 obj->write_domain = 0;
1752}
1753
1754/** Flushes the CPU write domain for the object if it's dirty. */
1755static void
1756i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
1757{
1758 struct drm_device *dev = obj->dev;
1759
1760 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
1761 return;
1762
1763 i915_gem_clflush_object(obj);
1764 drm_agp_chipset_flush(dev);
1765 obj->write_domain = 0;
1766}
1767
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001768/**
1769 * Moves a single object to the GTT read, and possibly write domain.
1770 *
1771 * This function returns when the move is complete, including waiting on
1772 * flushes to occur.
1773 */
Jesse Barnes79e53942008-11-07 14:24:08 -08001774int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001775i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
1776{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001777 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08001778 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001779
Eric Anholt02354392008-11-26 13:58:13 -08001780 /* Not valid to be called on unbound objects. */
1781 if (obj_priv->gtt_space == NULL)
1782 return -EINVAL;
1783
Eric Anholte47c68e2008-11-14 13:35:19 -08001784 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001785 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08001786 ret = i915_gem_object_wait_rendering(obj);
1787 if (ret != 0)
1788 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001789
1790 /* If we're writing through the GTT domain, then CPU and GPU caches
1791 * will need to be invalidated at next use.
1792 */
1793 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08001794 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001795
Eric Anholte47c68e2008-11-14 13:35:19 -08001796 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001797
1798 /* It should now be out of any other write domains, and we can update
1799 * the domain values for our changes.
1800 */
1801 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
1802 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08001803 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001804 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08001805 obj_priv->dirty = 1;
1806 }
1807
1808 return 0;
1809}
1810
1811/**
1812 * Moves a single object to the CPU read, and possibly write domain.
1813 *
1814 * This function returns when the move is complete, including waiting on
1815 * flushes to occur.
1816 */
1817static int
1818i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
1819{
1820 struct drm_device *dev = obj->dev;
1821 int ret;
1822
1823 i915_gem_object_flush_gpu_write_domain(obj);
1824 /* Wait on any GPU rendering and flushing to occur. */
1825 ret = i915_gem_object_wait_rendering(obj);
1826 if (ret != 0)
1827 return ret;
1828
1829 i915_gem_object_flush_gtt_write_domain(obj);
1830
1831 /* If we have a partially-valid cache of the object in the CPU,
1832 * finish invalidating it and free the per-page flags.
1833 */
1834 i915_gem_object_set_to_full_cpu_read_domain(obj);
1835
1836 /* Flush the CPU cache if it's still invalid. */
1837 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
1838 i915_gem_clflush_object(obj);
1839 drm_agp_chipset_flush(dev);
1840
1841 obj->read_domains |= I915_GEM_DOMAIN_CPU;
1842 }
1843
1844 /* It should now be out of any other write domains, and we can update
1845 * the domain values for our changes.
1846 */
1847 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
1848
1849 /* If we're writing through the CPU, then the GPU read domains will
1850 * need to be invalidated at next use.
1851 */
1852 if (write) {
1853 obj->read_domains &= I915_GEM_DOMAIN_CPU;
1854 obj->write_domain = I915_GEM_DOMAIN_CPU;
1855 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001856
1857 return 0;
1858}
1859
Eric Anholt673a3942008-07-30 12:06:12 -07001860/*
1861 * Set the next domain for the specified object. This
1862 * may not actually perform the necessary flushing/invaliding though,
1863 * as that may want to be batched with other set_domain operations
1864 *
1865 * This is (we hope) the only really tricky part of gem. The goal
1866 * is fairly simple -- track which caches hold bits of the object
1867 * and make sure they remain coherent. A few concrete examples may
1868 * help to explain how it works. For shorthand, we use the notation
1869 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
1870 * a pair of read and write domain masks.
1871 *
1872 * Case 1: the batch buffer
1873 *
1874 * 1. Allocated
1875 * 2. Written by CPU
1876 * 3. Mapped to GTT
1877 * 4. Read by GPU
1878 * 5. Unmapped from GTT
1879 * 6. Freed
1880 *
1881 * Let's take these a step at a time
1882 *
1883 * 1. Allocated
1884 * Pages allocated from the kernel may still have
1885 * cache contents, so we set them to (CPU, CPU) always.
1886 * 2. Written by CPU (using pwrite)
1887 * The pwrite function calls set_domain (CPU, CPU) and
1888 * this function does nothing (as nothing changes)
1889 * 3. Mapped by GTT
1890 * This function asserts that the object is not
1891 * currently in any GPU-based read or write domains
1892 * 4. Read by GPU
1893 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
1894 * As write_domain is zero, this function adds in the
1895 * current read domains (CPU+COMMAND, 0).
1896 * flush_domains is set to CPU.
1897 * invalidate_domains is set to COMMAND
1898 * clflush is run to get data out of the CPU caches
1899 * then i915_dev_set_domain calls i915_gem_flush to
1900 * emit an MI_FLUSH and drm_agp_chipset_flush
1901 * 5. Unmapped from GTT
1902 * i915_gem_object_unbind calls set_domain (CPU, CPU)
1903 * flush_domains and invalidate_domains end up both zero
1904 * so no flushing/invalidating happens
1905 * 6. Freed
1906 * yay, done
1907 *
1908 * Case 2: The shared render buffer
1909 *
1910 * 1. Allocated
1911 * 2. Mapped to GTT
1912 * 3. Read/written by GPU
1913 * 4. set_domain to (CPU,CPU)
1914 * 5. Read/written by CPU
1915 * 6. Read/written by GPU
1916 *
1917 * 1. Allocated
1918 * Same as last example, (CPU, CPU)
1919 * 2. Mapped to GTT
1920 * Nothing changes (assertions find that it is not in the GPU)
1921 * 3. Read/written by GPU
1922 * execbuffer calls set_domain (RENDER, RENDER)
1923 * flush_domains gets CPU
1924 * invalidate_domains gets GPU
1925 * clflush (obj)
1926 * MI_FLUSH and drm_agp_chipset_flush
1927 * 4. set_domain (CPU, CPU)
1928 * flush_domains gets GPU
1929 * invalidate_domains gets CPU
1930 * wait_rendering (obj) to make sure all drawing is complete.
1931 * This will include an MI_FLUSH to get the data from GPU
1932 * to memory
1933 * clflush (obj) to invalidate the CPU cache
1934 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
1935 * 5. Read/written by CPU
1936 * cache lines are loaded and dirtied
1937 * 6. Read written by GPU
1938 * Same as last GPU access
1939 *
1940 * Case 3: The constant buffer
1941 *
1942 * 1. Allocated
1943 * 2. Written by CPU
1944 * 3. Read by GPU
1945 * 4. Updated (written) by CPU again
1946 * 5. Read by GPU
1947 *
1948 * 1. Allocated
1949 * (CPU, CPU)
1950 * 2. Written by CPU
1951 * (CPU, CPU)
1952 * 3. Read by GPU
1953 * (CPU+RENDER, 0)
1954 * flush_domains = CPU
1955 * invalidate_domains = RENDER
1956 * clflush (obj)
1957 * MI_FLUSH
1958 * drm_agp_chipset_flush
1959 * 4. Updated (written) by CPU again
1960 * (CPU, CPU)
1961 * flush_domains = 0 (no previous write domain)
1962 * invalidate_domains = 0 (no new read domains)
1963 * 5. Read by GPU
1964 * (CPU+RENDER, 0)
1965 * flush_domains = CPU
1966 * invalidate_domains = RENDER
1967 * clflush (obj)
1968 * MI_FLUSH
1969 * drm_agp_chipset_flush
1970 */
Keith Packardc0d90822008-11-20 23:11:08 -08001971static void
1972i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
1973 uint32_t read_domains,
1974 uint32_t write_domain)
Eric Anholt673a3942008-07-30 12:06:12 -07001975{
1976 struct drm_device *dev = obj->dev;
1977 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1978 uint32_t invalidate_domains = 0;
1979 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08001980
1981 BUG_ON(read_domains & I915_GEM_DOMAIN_CPU);
1982 BUG_ON(write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07001983
1984#if WATCH_BUF
1985 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
1986 __func__, obj,
1987 obj->read_domains, read_domains,
1988 obj->write_domain, write_domain);
1989#endif
1990 /*
1991 * If the object isn't moving to a new write domain,
1992 * let the object stay in multiple read domains
1993 */
1994 if (write_domain == 0)
1995 read_domains |= obj->read_domains;
1996 else
1997 obj_priv->dirty = 1;
1998
1999 /*
2000 * Flush the current write domain if
2001 * the new read domains don't match. Invalidate
2002 * any read domains which differ from the old
2003 * write domain
2004 */
2005 if (obj->write_domain && obj->write_domain != read_domains) {
2006 flush_domains |= obj->write_domain;
2007 invalidate_domains |= read_domains & ~obj->write_domain;
2008 }
2009 /*
2010 * Invalidate any read caches which may have
2011 * stale data. That is, any new read domains.
2012 */
2013 invalidate_domains |= read_domains & ~obj->read_domains;
2014 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2015#if WATCH_BUF
2016 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2017 __func__, flush_domains, invalidate_domains);
2018#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002019 i915_gem_clflush_object(obj);
2020 }
2021
2022 if ((write_domain | flush_domains) != 0)
2023 obj->write_domain = write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002024 obj->read_domains = read_domains;
2025
2026 dev->invalidate_domains |= invalidate_domains;
2027 dev->flush_domains |= flush_domains;
2028#if WATCH_BUF
2029 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2030 __func__,
2031 obj->read_domains, obj->write_domain,
2032 dev->invalidate_domains, dev->flush_domains);
2033#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002034}
2035
2036/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002037 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002038 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002039 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2040 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2041 */
2042static void
2043i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2044{
2045 struct drm_device *dev = obj->dev;
2046 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2047
2048 if (!obj_priv->page_cpu_valid)
2049 return;
2050
2051 /* If we're partially in the CPU read domain, finish moving it in.
2052 */
2053 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2054 int i;
2055
2056 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2057 if (obj_priv->page_cpu_valid[i])
2058 continue;
2059 drm_clflush_pages(obj_priv->page_list + i, 1);
2060 }
2061 drm_agp_chipset_flush(dev);
2062 }
2063
2064 /* Free the page_cpu_valid mappings which are now stale, whether
2065 * or not we've got I915_GEM_DOMAIN_CPU.
2066 */
2067 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
2068 DRM_MEM_DRIVER);
2069 obj_priv->page_cpu_valid = NULL;
2070}
2071
2072/**
2073 * Set the CPU read domain on a range of the object.
2074 *
2075 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2076 * not entirely valid. The page_cpu_valid member of the object flags which
2077 * pages have been flushed, and will be respected by
2078 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2079 * of the whole object.
2080 *
2081 * This function returns when the move is complete, including waiting on
2082 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002083 */
2084static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002085i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2086 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002087{
2088 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002089 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002090
Eric Anholte47c68e2008-11-14 13:35:19 -08002091 if (offset == 0 && size == obj->size)
2092 return i915_gem_object_set_to_cpu_domain(obj, 0);
2093
2094 i915_gem_object_flush_gpu_write_domain(obj);
2095 /* Wait on any GPU rendering and flushing to occur. */
2096 ret = i915_gem_object_wait_rendering(obj);
2097 if (ret != 0)
2098 return ret;
2099 i915_gem_object_flush_gtt_write_domain(obj);
2100
2101 /* If we're already fully in the CPU read domain, we're done. */
2102 if (obj_priv->page_cpu_valid == NULL &&
2103 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002104 return 0;
2105
Eric Anholte47c68e2008-11-14 13:35:19 -08002106 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2107 * newly adding I915_GEM_DOMAIN_CPU
2108 */
Eric Anholt673a3942008-07-30 12:06:12 -07002109 if (obj_priv->page_cpu_valid == NULL) {
2110 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2111 DRM_MEM_DRIVER);
Eric Anholte47c68e2008-11-14 13:35:19 -08002112 if (obj_priv->page_cpu_valid == NULL)
2113 return -ENOMEM;
2114 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2115 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002116
2117 /* Flush the cache on any pages that are still invalid from the CPU's
2118 * perspective.
2119 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002120 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2121 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002122 if (obj_priv->page_cpu_valid[i])
2123 continue;
2124
2125 drm_clflush_pages(obj_priv->page_list + i, 1);
2126
2127 obj_priv->page_cpu_valid[i] = 1;
2128 }
2129
Eric Anholte47c68e2008-11-14 13:35:19 -08002130 /* It should now be out of any other write domains, and we can update
2131 * the domain values for our changes.
2132 */
2133 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2134
2135 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2136
Eric Anholt673a3942008-07-30 12:06:12 -07002137 return 0;
2138}
2139
2140/**
Eric Anholt673a3942008-07-30 12:06:12 -07002141 * Pin an object to the GTT and evaluate the relocations landing in it.
2142 */
2143static int
2144i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2145 struct drm_file *file_priv,
2146 struct drm_i915_gem_exec_object *entry)
2147{
2148 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002149 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002150 struct drm_i915_gem_relocation_entry reloc;
2151 struct drm_i915_gem_relocation_entry __user *relocs;
2152 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2153 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002154 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002155
2156 /* Choose the GTT offset for our buffer and put it there. */
2157 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2158 if (ret)
2159 return ret;
2160
2161 entry->offset = obj_priv->gtt_offset;
2162
2163 relocs = (struct drm_i915_gem_relocation_entry __user *)
2164 (uintptr_t) entry->relocs_ptr;
2165 /* Apply the relocations, using the GTT aperture to avoid cache
2166 * flushing requirements.
2167 */
2168 for (i = 0; i < entry->relocation_count; i++) {
2169 struct drm_gem_object *target_obj;
2170 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002171 uint32_t reloc_val, reloc_offset;
2172 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002173
2174 ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
2175 if (ret != 0) {
2176 i915_gem_object_unpin(obj);
2177 return ret;
2178 }
2179
2180 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
2181 reloc.target_handle);
2182 if (target_obj == NULL) {
2183 i915_gem_object_unpin(obj);
2184 return -EBADF;
2185 }
2186 target_obj_priv = target_obj->driver_private;
2187
2188 /* The target buffer should have appeared before us in the
2189 * exec_object list, so it should have a GTT space bound by now.
2190 */
2191 if (target_obj_priv->gtt_space == NULL) {
2192 DRM_ERROR("No GTT space found for object %d\n",
2193 reloc.target_handle);
2194 drm_gem_object_unreference(target_obj);
2195 i915_gem_object_unpin(obj);
2196 return -EINVAL;
2197 }
2198
2199 if (reloc.offset > obj->size - 4) {
2200 DRM_ERROR("Relocation beyond object bounds: "
2201 "obj %p target %d offset %d size %d.\n",
2202 obj, reloc.target_handle,
2203 (int) reloc.offset, (int) obj->size);
2204 drm_gem_object_unreference(target_obj);
2205 i915_gem_object_unpin(obj);
2206 return -EINVAL;
2207 }
2208 if (reloc.offset & 3) {
2209 DRM_ERROR("Relocation not 4-byte aligned: "
2210 "obj %p target %d offset %d.\n",
2211 obj, reloc.target_handle,
2212 (int) reloc.offset);
2213 drm_gem_object_unreference(target_obj);
2214 i915_gem_object_unpin(obj);
2215 return -EINVAL;
2216 }
2217
Eric Anholte47c68e2008-11-14 13:35:19 -08002218 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
2219 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
2220 DRM_ERROR("reloc with read/write CPU domains: "
2221 "obj %p target %d offset %d "
2222 "read %08x write %08x",
2223 obj, reloc.target_handle,
2224 (int) reloc.offset,
2225 reloc.read_domains,
2226 reloc.write_domain);
2227 return -EINVAL;
2228 }
2229
Eric Anholt673a3942008-07-30 12:06:12 -07002230 if (reloc.write_domain && target_obj->pending_write_domain &&
2231 reloc.write_domain != target_obj->pending_write_domain) {
2232 DRM_ERROR("Write domain conflict: "
2233 "obj %p target %d offset %d "
2234 "new %08x old %08x\n",
2235 obj, reloc.target_handle,
2236 (int) reloc.offset,
2237 reloc.write_domain,
2238 target_obj->pending_write_domain);
2239 drm_gem_object_unreference(target_obj);
2240 i915_gem_object_unpin(obj);
2241 return -EINVAL;
2242 }
2243
2244#if WATCH_RELOC
2245 DRM_INFO("%s: obj %p offset %08x target %d "
2246 "read %08x write %08x gtt %08x "
2247 "presumed %08x delta %08x\n",
2248 __func__,
2249 obj,
2250 (int) reloc.offset,
2251 (int) reloc.target_handle,
2252 (int) reloc.read_domains,
2253 (int) reloc.write_domain,
2254 (int) target_obj_priv->gtt_offset,
2255 (int) reloc.presumed_offset,
2256 reloc.delta);
2257#endif
2258
2259 target_obj->pending_read_domains |= reloc.read_domains;
2260 target_obj->pending_write_domain |= reloc.write_domain;
2261
2262 /* If the relocation already has the right value in it, no
2263 * more work needs to be done.
2264 */
2265 if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
2266 drm_gem_object_unreference(target_obj);
2267 continue;
2268 }
2269
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002270 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
2271 if (ret != 0) {
2272 drm_gem_object_unreference(target_obj);
2273 i915_gem_object_unpin(obj);
2274 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07002275 }
2276
2277 /* Map the page containing the relocation we're going to
2278 * perform.
2279 */
2280 reloc_offset = obj_priv->gtt_offset + reloc.offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07002281 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
2282 (reloc_offset &
2283 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07002284 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07002285 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt673a3942008-07-30 12:06:12 -07002286 reloc_val = target_obj_priv->gtt_offset + reloc.delta;
2287
2288#if WATCH_BUF
2289 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
2290 obj, (unsigned int) reloc.offset,
2291 readl(reloc_entry), reloc_val);
2292#endif
2293 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07002294 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07002295
2296 /* Write the updated presumed offset for this entry back out
2297 * to the user.
2298 */
2299 reloc.presumed_offset = target_obj_priv->gtt_offset;
2300 ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
2301 if (ret != 0) {
2302 drm_gem_object_unreference(target_obj);
2303 i915_gem_object_unpin(obj);
2304 return ret;
2305 }
2306
2307 drm_gem_object_unreference(target_obj);
2308 }
2309
Eric Anholt673a3942008-07-30 12:06:12 -07002310#if WATCH_BUF
2311 if (0)
2312 i915_gem_dump_object(obj, 128, __func__, ~0);
2313#endif
2314 return 0;
2315}
2316
2317/** Dispatch a batchbuffer to the ring
2318 */
2319static int
2320i915_dispatch_gem_execbuffer(struct drm_device *dev,
2321 struct drm_i915_gem_execbuffer *exec,
2322 uint64_t exec_offset)
2323{
2324 drm_i915_private_t *dev_priv = dev->dev_private;
2325 struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
2326 (uintptr_t) exec->cliprects_ptr;
2327 int nbox = exec->num_cliprects;
2328 int i = 0, count;
2329 uint32_t exec_start, exec_len;
2330 RING_LOCALS;
2331
2332 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
2333 exec_len = (uint32_t) exec->batch_len;
2334
2335 if ((exec_start | exec_len) & 0x7) {
2336 DRM_ERROR("alignment\n");
2337 return -EINVAL;
2338 }
2339
2340 if (!exec_start)
2341 return -EINVAL;
2342
2343 count = nbox ? nbox : 1;
2344
2345 for (i = 0; i < count; i++) {
2346 if (i < nbox) {
2347 int ret = i915_emit_box(dev, boxes, i,
2348 exec->DR1, exec->DR4);
2349 if (ret)
2350 return ret;
2351 }
2352
2353 if (IS_I830(dev) || IS_845G(dev)) {
2354 BEGIN_LP_RING(4);
2355 OUT_RING(MI_BATCH_BUFFER);
2356 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2357 OUT_RING(exec_start + exec_len - 4);
2358 OUT_RING(0);
2359 ADVANCE_LP_RING();
2360 } else {
2361 BEGIN_LP_RING(2);
2362 if (IS_I965G(dev)) {
2363 OUT_RING(MI_BATCH_BUFFER_START |
2364 (2 << 6) |
2365 MI_BATCH_NON_SECURE_I965);
2366 OUT_RING(exec_start);
2367 } else {
2368 OUT_RING(MI_BATCH_BUFFER_START |
2369 (2 << 6));
2370 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2371 }
2372 ADVANCE_LP_RING();
2373 }
2374 }
2375
2376 /* XXX breadcrumb */
2377 return 0;
2378}
2379
2380/* Throttle our rendering by waiting until the ring has completed our requests
2381 * emitted over 20 msec ago.
2382 *
2383 * This should get us reasonable parallelism between CPU and GPU but also
2384 * relatively low latency when blocking on a particular request to finish.
2385 */
2386static int
2387i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
2388{
2389 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2390 int ret = 0;
2391 uint32_t seqno;
2392
2393 mutex_lock(&dev->struct_mutex);
2394 seqno = i915_file_priv->mm.last_gem_throttle_seqno;
2395 i915_file_priv->mm.last_gem_throttle_seqno =
2396 i915_file_priv->mm.last_gem_seqno;
2397 if (seqno)
2398 ret = i915_wait_request(dev, seqno);
2399 mutex_unlock(&dev->struct_mutex);
2400 return ret;
2401}
2402
2403int
2404i915_gem_execbuffer(struct drm_device *dev, void *data,
2405 struct drm_file *file_priv)
2406{
2407 drm_i915_private_t *dev_priv = dev->dev_private;
2408 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2409 struct drm_i915_gem_execbuffer *args = data;
2410 struct drm_i915_gem_exec_object *exec_list = NULL;
2411 struct drm_gem_object **object_list = NULL;
2412 struct drm_gem_object *batch_obj;
2413 int ret, i, pinned = 0;
2414 uint64_t exec_offset;
2415 uint32_t seqno, flush_domains;
Keith Packardac94a962008-11-20 23:30:27 -08002416 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07002417
2418#if WATCH_EXEC
2419 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
2420 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
2421#endif
2422
Eric Anholt4f481ed2008-09-10 14:22:49 -07002423 if (args->buffer_count < 1) {
2424 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
2425 return -EINVAL;
2426 }
Eric Anholt673a3942008-07-30 12:06:12 -07002427 /* Copy in the exec list from userland */
2428 exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
2429 DRM_MEM_DRIVER);
2430 object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
2431 DRM_MEM_DRIVER);
2432 if (exec_list == NULL || object_list == NULL) {
2433 DRM_ERROR("Failed to allocate exec or object list "
2434 "for %d buffers\n",
2435 args->buffer_count);
2436 ret = -ENOMEM;
2437 goto pre_mutex_err;
2438 }
2439 ret = copy_from_user(exec_list,
2440 (struct drm_i915_relocation_entry __user *)
2441 (uintptr_t) args->buffers_ptr,
2442 sizeof(*exec_list) * args->buffer_count);
2443 if (ret != 0) {
2444 DRM_ERROR("copy %d exec entries failed %d\n",
2445 args->buffer_count, ret);
2446 goto pre_mutex_err;
2447 }
2448
2449 mutex_lock(&dev->struct_mutex);
2450
2451 i915_verify_inactive(dev, __FILE__, __LINE__);
2452
2453 if (dev_priv->mm.wedged) {
2454 DRM_ERROR("Execbuf while wedged\n");
2455 mutex_unlock(&dev->struct_mutex);
2456 return -EIO;
2457 }
2458
2459 if (dev_priv->mm.suspended) {
2460 DRM_ERROR("Execbuf while VT-switched.\n");
2461 mutex_unlock(&dev->struct_mutex);
2462 return -EBUSY;
2463 }
2464
Keith Packardac94a962008-11-20 23:30:27 -08002465 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07002466 for (i = 0; i < args->buffer_count; i++) {
2467 object_list[i] = drm_gem_object_lookup(dev, file_priv,
2468 exec_list[i].handle);
2469 if (object_list[i] == NULL) {
2470 DRM_ERROR("Invalid object handle %d at index %d\n",
2471 exec_list[i].handle, i);
2472 ret = -EBADF;
2473 goto err;
2474 }
Keith Packardac94a962008-11-20 23:30:27 -08002475 }
Eric Anholt673a3942008-07-30 12:06:12 -07002476
Keith Packardac94a962008-11-20 23:30:27 -08002477 /* Pin and relocate */
2478 for (pin_tries = 0; ; pin_tries++) {
2479 ret = 0;
2480 for (i = 0; i < args->buffer_count; i++) {
2481 object_list[i]->pending_read_domains = 0;
2482 object_list[i]->pending_write_domain = 0;
2483 ret = i915_gem_object_pin_and_relocate(object_list[i],
2484 file_priv,
2485 &exec_list[i]);
2486 if (ret)
2487 break;
2488 pinned = i + 1;
2489 }
2490 /* success */
2491 if (ret == 0)
2492 break;
2493
2494 /* error other than GTT full, or we've already tried again */
2495 if (ret != -ENOMEM || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08002496 if (ret != -ERESTARTSYS)
2497 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002498 goto err;
2499 }
Keith Packardac94a962008-11-20 23:30:27 -08002500
2501 /* unpin all of our buffers */
2502 for (i = 0; i < pinned; i++)
2503 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08002504 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08002505
2506 /* evict everyone we can from the aperture */
2507 ret = i915_gem_evict_everything(dev);
2508 if (ret)
2509 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07002510 }
2511
2512 /* Set the pending read domains for the batch buffer to COMMAND */
2513 batch_obj = object_list[args->buffer_count-1];
2514 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
2515 batch_obj->pending_write_domain = 0;
2516
2517 i915_verify_inactive(dev, __FILE__, __LINE__);
2518
Keith Packard646f0f62008-11-20 23:23:03 -08002519 /* Zero the global flush/invalidate flags. These
2520 * will be modified as new domains are computed
2521 * for each object
2522 */
2523 dev->invalidate_domains = 0;
2524 dev->flush_domains = 0;
2525
Eric Anholt673a3942008-07-30 12:06:12 -07002526 for (i = 0; i < args->buffer_count; i++) {
2527 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002528
Keith Packard646f0f62008-11-20 23:23:03 -08002529 /* Compute new gpu domains and update invalidate/flush */
Keith Packardc0d90822008-11-20 23:11:08 -08002530 i915_gem_object_set_to_gpu_domain(obj,
2531 obj->pending_read_domains,
2532 obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002533 }
2534
2535 i915_verify_inactive(dev, __FILE__, __LINE__);
2536
Keith Packard646f0f62008-11-20 23:23:03 -08002537 if (dev->invalidate_domains | dev->flush_domains) {
2538#if WATCH_EXEC
2539 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
2540 __func__,
2541 dev->invalidate_domains,
2542 dev->flush_domains);
2543#endif
2544 i915_gem_flush(dev,
2545 dev->invalidate_domains,
2546 dev->flush_domains);
2547 if (dev->flush_domains)
2548 (void)i915_add_request(dev, dev->flush_domains);
2549 }
Eric Anholt673a3942008-07-30 12:06:12 -07002550
2551 i915_verify_inactive(dev, __FILE__, __LINE__);
2552
2553#if WATCH_COHERENCY
2554 for (i = 0; i < args->buffer_count; i++) {
2555 i915_gem_object_check_coherency(object_list[i],
2556 exec_list[i].handle);
2557 }
2558#endif
2559
2560 exec_offset = exec_list[args->buffer_count - 1].offset;
2561
2562#if WATCH_EXEC
2563 i915_gem_dump_object(object_list[args->buffer_count - 1],
2564 args->batch_len,
2565 __func__,
2566 ~0);
2567#endif
2568
Eric Anholt673a3942008-07-30 12:06:12 -07002569 /* Exec the batchbuffer */
2570 ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
2571 if (ret) {
2572 DRM_ERROR("dispatch failed %d\n", ret);
2573 goto err;
2574 }
2575
2576 /*
2577 * Ensure that the commands in the batch buffer are
2578 * finished before the interrupt fires
2579 */
2580 flush_domains = i915_retire_commands(dev);
2581
2582 i915_verify_inactive(dev, __FILE__, __LINE__);
2583
2584 /*
2585 * Get a seqno representing the execution of the current buffer,
2586 * which we can wait on. We would like to mitigate these interrupts,
2587 * likely by only creating seqnos occasionally (so that we have
2588 * *some* interrupts representing completion of buffers that we can
2589 * wait on when trying to clear up gtt space).
2590 */
2591 seqno = i915_add_request(dev, flush_domains);
2592 BUG_ON(seqno == 0);
2593 i915_file_priv->mm.last_gem_seqno = seqno;
2594 for (i = 0; i < args->buffer_count; i++) {
2595 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002596
Eric Anholtce44b0e2008-11-06 16:00:31 -08002597 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002598#if WATCH_LRU
2599 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
2600#endif
2601 }
2602#if WATCH_LRU
2603 i915_dump_lru(dev, __func__);
2604#endif
2605
2606 i915_verify_inactive(dev, __FILE__, __LINE__);
2607
2608 /* Copy the new buffer offsets back to the user's exec list. */
2609 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
2610 (uintptr_t) args->buffers_ptr,
2611 exec_list,
2612 sizeof(*exec_list) * args->buffer_count);
2613 if (ret)
2614 DRM_ERROR("failed to copy %d exec entries "
2615 "back to user (%d)\n",
2616 args->buffer_count, ret);
2617err:
Julia Lawallaad87df2008-12-21 16:28:47 +01002618 for (i = 0; i < pinned; i++)
2619 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07002620
Julia Lawallaad87df2008-12-21 16:28:47 +01002621 for (i = 0; i < args->buffer_count; i++)
2622 drm_gem_object_unreference(object_list[i]);
2623
Eric Anholt673a3942008-07-30 12:06:12 -07002624 mutex_unlock(&dev->struct_mutex);
2625
2626pre_mutex_err:
2627 drm_free(object_list, sizeof(*object_list) * args->buffer_count,
2628 DRM_MEM_DRIVER);
2629 drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
2630 DRM_MEM_DRIVER);
2631
2632 return ret;
2633}
2634
2635int
2636i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
2637{
2638 struct drm_device *dev = obj->dev;
2639 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2640 int ret;
2641
2642 i915_verify_inactive(dev, __FILE__, __LINE__);
2643 if (obj_priv->gtt_space == NULL) {
2644 ret = i915_gem_object_bind_to_gtt(obj, alignment);
2645 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002646 if (ret != -EBUSY && ret != -ERESTARTSYS)
Eric Anholtf1acec92008-12-19 14:47:48 -08002647 DRM_ERROR("Failure to bind: %d", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002648 return ret;
2649 }
2650 }
2651 obj_priv->pin_count++;
2652
2653 /* If the object is not active and not pending a flush,
2654 * remove it from the inactive list
2655 */
2656 if (obj_priv->pin_count == 1) {
2657 atomic_inc(&dev->pin_count);
2658 atomic_add(obj->size, &dev->pin_memory);
2659 if (!obj_priv->active &&
2660 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2661 I915_GEM_DOMAIN_GTT)) == 0 &&
2662 !list_empty(&obj_priv->list))
2663 list_del_init(&obj_priv->list);
2664 }
2665 i915_verify_inactive(dev, __FILE__, __LINE__);
2666
2667 return 0;
2668}
2669
2670void
2671i915_gem_object_unpin(struct drm_gem_object *obj)
2672{
2673 struct drm_device *dev = obj->dev;
2674 drm_i915_private_t *dev_priv = dev->dev_private;
2675 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2676
2677 i915_verify_inactive(dev, __FILE__, __LINE__);
2678 obj_priv->pin_count--;
2679 BUG_ON(obj_priv->pin_count < 0);
2680 BUG_ON(obj_priv->gtt_space == NULL);
2681
2682 /* If the object is no longer pinned, and is
2683 * neither active nor being flushed, then stick it on
2684 * the inactive list
2685 */
2686 if (obj_priv->pin_count == 0) {
2687 if (!obj_priv->active &&
2688 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2689 I915_GEM_DOMAIN_GTT)) == 0)
2690 list_move_tail(&obj_priv->list,
2691 &dev_priv->mm.inactive_list);
2692 atomic_dec(&dev->pin_count);
2693 atomic_sub(obj->size, &dev->pin_memory);
2694 }
2695 i915_verify_inactive(dev, __FILE__, __LINE__);
2696}
2697
2698int
2699i915_gem_pin_ioctl(struct drm_device *dev, void *data,
2700 struct drm_file *file_priv)
2701{
2702 struct drm_i915_gem_pin *args = data;
2703 struct drm_gem_object *obj;
2704 struct drm_i915_gem_object *obj_priv;
2705 int ret;
2706
2707 mutex_lock(&dev->struct_mutex);
2708
2709 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2710 if (obj == NULL) {
2711 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
2712 args->handle);
2713 mutex_unlock(&dev->struct_mutex);
2714 return -EBADF;
2715 }
2716 obj_priv = obj->driver_private;
2717
Jesse Barnes79e53942008-11-07 14:24:08 -08002718 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
2719 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
2720 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002721 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002722 return -EINVAL;
2723 }
2724
2725 obj_priv->user_pin_count++;
2726 obj_priv->pin_filp = file_priv;
2727 if (obj_priv->user_pin_count == 1) {
2728 ret = i915_gem_object_pin(obj, args->alignment);
2729 if (ret != 0) {
2730 drm_gem_object_unreference(obj);
2731 mutex_unlock(&dev->struct_mutex);
2732 return ret;
2733 }
Eric Anholt673a3942008-07-30 12:06:12 -07002734 }
2735
2736 /* XXX - flush the CPU caches for pinned objects
2737 * as the X server doesn't manage domains yet
2738 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002739 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002740 args->offset = obj_priv->gtt_offset;
2741 drm_gem_object_unreference(obj);
2742 mutex_unlock(&dev->struct_mutex);
2743
2744 return 0;
2745}
2746
2747int
2748i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
2749 struct drm_file *file_priv)
2750{
2751 struct drm_i915_gem_pin *args = data;
2752 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08002753 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002754
2755 mutex_lock(&dev->struct_mutex);
2756
2757 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2758 if (obj == NULL) {
2759 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
2760 args->handle);
2761 mutex_unlock(&dev->struct_mutex);
2762 return -EBADF;
2763 }
2764
Jesse Barnes79e53942008-11-07 14:24:08 -08002765 obj_priv = obj->driver_private;
2766 if (obj_priv->pin_filp != file_priv) {
2767 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
2768 args->handle);
2769 drm_gem_object_unreference(obj);
2770 mutex_unlock(&dev->struct_mutex);
2771 return -EINVAL;
2772 }
2773 obj_priv->user_pin_count--;
2774 if (obj_priv->user_pin_count == 0) {
2775 obj_priv->pin_filp = NULL;
2776 i915_gem_object_unpin(obj);
2777 }
Eric Anholt673a3942008-07-30 12:06:12 -07002778
2779 drm_gem_object_unreference(obj);
2780 mutex_unlock(&dev->struct_mutex);
2781 return 0;
2782}
2783
2784int
2785i915_gem_busy_ioctl(struct drm_device *dev, void *data,
2786 struct drm_file *file_priv)
2787{
2788 struct drm_i915_gem_busy *args = data;
2789 struct drm_gem_object *obj;
2790 struct drm_i915_gem_object *obj_priv;
2791
2792 mutex_lock(&dev->struct_mutex);
2793 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2794 if (obj == NULL) {
2795 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
2796 args->handle);
2797 mutex_unlock(&dev->struct_mutex);
2798 return -EBADF;
2799 }
2800
2801 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08002802 /* Don't count being on the flushing list against the object being
2803 * done. Otherwise, a buffer left on the flushing list but not getting
2804 * flushed (because nobody's flushing that domain) won't ever return
2805 * unbusy and get reused by libdrm's bo cache. The other expected
2806 * consumer of this interface, OpenGL's occlusion queries, also specs
2807 * that the objects get unbusy "eventually" without any interference.
2808 */
2809 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002810
2811 drm_gem_object_unreference(obj);
2812 mutex_unlock(&dev->struct_mutex);
2813 return 0;
2814}
2815
2816int
2817i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
2818 struct drm_file *file_priv)
2819{
2820 return i915_gem_ring_throttle(dev, file_priv);
2821}
2822
2823int i915_gem_init_object(struct drm_gem_object *obj)
2824{
2825 struct drm_i915_gem_object *obj_priv;
2826
2827 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
2828 if (obj_priv == NULL)
2829 return -ENOMEM;
2830
2831 /*
2832 * We've just allocated pages from the kernel,
2833 * so they've just been written by the CPU with
2834 * zeros. They'll need to be clflushed before we
2835 * use them with the GPU.
2836 */
2837 obj->write_domain = I915_GEM_DOMAIN_CPU;
2838 obj->read_domains = I915_GEM_DOMAIN_CPU;
2839
Keith Packardba1eb1d2008-10-14 19:55:10 -07002840 obj_priv->agp_type = AGP_USER_MEMORY;
2841
Eric Anholt673a3942008-07-30 12:06:12 -07002842 obj->driver_private = obj_priv;
2843 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002844 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07002845 INIT_LIST_HEAD(&obj_priv->list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002846
Eric Anholt673a3942008-07-30 12:06:12 -07002847 return 0;
2848}
2849
2850void i915_gem_free_object(struct drm_gem_object *obj)
2851{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002852 struct drm_device *dev = obj->dev;
2853 struct drm_gem_mm *mm = dev->mm_private;
2854 struct drm_map_list *list;
2855 struct drm_map *map;
Eric Anholt673a3942008-07-30 12:06:12 -07002856 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2857
2858 while (obj_priv->pin_count > 0)
2859 i915_gem_object_unpin(obj);
2860
2861 i915_gem_object_unbind(obj);
2862
Jesse Barnesde151cf2008-11-12 10:03:55 -08002863 list = &obj->map_list;
2864 drm_ht_remove_item(&mm->offset_hash, &list->hash);
2865
2866 if (list->file_offset_node) {
2867 drm_mm_put_block(list->file_offset_node);
2868 list->file_offset_node = NULL;
2869 }
2870
2871 map = list->map;
2872 if (map) {
2873 drm_free(map, sizeof(*map), DRM_MEM_DRIVER);
2874 list->map = NULL;
2875 }
2876
Eric Anholt673a3942008-07-30 12:06:12 -07002877 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
2878 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
2879}
2880
Eric Anholt673a3942008-07-30 12:06:12 -07002881/** Unbinds all objects that are on the given buffer list. */
2882static int
2883i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
2884{
2885 struct drm_gem_object *obj;
2886 struct drm_i915_gem_object *obj_priv;
2887 int ret;
2888
2889 while (!list_empty(head)) {
2890 obj_priv = list_first_entry(head,
2891 struct drm_i915_gem_object,
2892 list);
2893 obj = obj_priv->obj;
2894
2895 if (obj_priv->pin_count != 0) {
2896 DRM_ERROR("Pinned object in unbind list\n");
2897 mutex_unlock(&dev->struct_mutex);
2898 return -EINVAL;
2899 }
2900
2901 ret = i915_gem_object_unbind(obj);
2902 if (ret != 0) {
2903 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
2904 ret);
2905 mutex_unlock(&dev->struct_mutex);
2906 return ret;
2907 }
2908 }
2909
2910
2911 return 0;
2912}
2913
2914static int
2915i915_gem_idle(struct drm_device *dev)
2916{
2917 drm_i915_private_t *dev_priv = dev->dev_private;
2918 uint32_t seqno, cur_seqno, last_seqno;
2919 int stuck, ret;
2920
Keith Packard6dbe2772008-10-14 21:41:13 -07002921 mutex_lock(&dev->struct_mutex);
2922
2923 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
2924 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07002925 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07002926 }
Eric Anholt673a3942008-07-30 12:06:12 -07002927
2928 /* Hack! Don't let anybody do execbuf while we don't control the chip.
2929 * We need to replace this with a semaphore, or something.
2930 */
2931 dev_priv->mm.suspended = 1;
2932
Keith Packard6dbe2772008-10-14 21:41:13 -07002933 /* Cancel the retire work handler, wait for it to finish if running
2934 */
2935 mutex_unlock(&dev->struct_mutex);
2936 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
2937 mutex_lock(&dev->struct_mutex);
2938
Eric Anholt673a3942008-07-30 12:06:12 -07002939 i915_kernel_lost_context(dev);
2940
2941 /* Flush the GPU along with all non-CPU write domains
2942 */
2943 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
2944 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
Jesse Barnesde151cf2008-11-12 10:03:55 -08002945 seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002946
2947 if (seqno == 0) {
2948 mutex_unlock(&dev->struct_mutex);
2949 return -ENOMEM;
2950 }
2951
2952 dev_priv->mm.waiting_gem_seqno = seqno;
2953 last_seqno = 0;
2954 stuck = 0;
2955 for (;;) {
2956 cur_seqno = i915_get_gem_seqno(dev);
2957 if (i915_seqno_passed(cur_seqno, seqno))
2958 break;
2959 if (last_seqno == cur_seqno) {
2960 if (stuck++ > 100) {
2961 DRM_ERROR("hardware wedged\n");
2962 dev_priv->mm.wedged = 1;
2963 DRM_WAKEUP(&dev_priv->irq_queue);
2964 break;
2965 }
2966 }
2967 msleep(10);
2968 last_seqno = cur_seqno;
2969 }
2970 dev_priv->mm.waiting_gem_seqno = 0;
2971
2972 i915_gem_retire_requests(dev);
2973
Eric Anholt28dfe522008-11-13 15:00:55 -08002974 if (!dev_priv->mm.wedged) {
2975 /* Active and flushing should now be empty as we've
2976 * waited for a sequence higher than any pending execbuffer
2977 */
2978 WARN_ON(!list_empty(&dev_priv->mm.active_list));
2979 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
2980 /* Request should now be empty as we've also waited
2981 * for the last request in the list
2982 */
2983 WARN_ON(!list_empty(&dev_priv->mm.request_list));
2984 }
Eric Anholt673a3942008-07-30 12:06:12 -07002985
Eric Anholt28dfe522008-11-13 15:00:55 -08002986 /* Empty the active and flushing lists to inactive. If there's
2987 * anything left at this point, it means that we're wedged and
2988 * nothing good's going to happen by leaving them there. So strip
2989 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07002990 */
Eric Anholt28dfe522008-11-13 15:00:55 -08002991 while (!list_empty(&dev_priv->mm.active_list)) {
2992 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002993
Eric Anholt28dfe522008-11-13 15:00:55 -08002994 obj_priv = list_first_entry(&dev_priv->mm.active_list,
2995 struct drm_i915_gem_object,
2996 list);
2997 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
2998 i915_gem_object_move_to_inactive(obj_priv->obj);
2999 }
3000
3001 while (!list_empty(&dev_priv->mm.flushing_list)) {
3002 struct drm_i915_gem_object *obj_priv;
3003
Eric Anholt151903d2008-12-01 10:23:21 +10003004 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003005 struct drm_i915_gem_object,
3006 list);
3007 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3008 i915_gem_object_move_to_inactive(obj_priv->obj);
3009 }
3010
3011
3012 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003013 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003014 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003015 if (ret) {
3016 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003017 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003018 }
Eric Anholt673a3942008-07-30 12:06:12 -07003019
Keith Packard6dbe2772008-10-14 21:41:13 -07003020 i915_gem_cleanup_ringbuffer(dev);
3021 mutex_unlock(&dev->struct_mutex);
3022
Eric Anholt673a3942008-07-30 12:06:12 -07003023 return 0;
3024}
3025
3026static int
3027i915_gem_init_hws(struct drm_device *dev)
3028{
3029 drm_i915_private_t *dev_priv = dev->dev_private;
3030 struct drm_gem_object *obj;
3031 struct drm_i915_gem_object *obj_priv;
3032 int ret;
3033
3034 /* If we need a physical address for the status page, it's already
3035 * initialized at driver load time.
3036 */
3037 if (!I915_NEED_GFX_HWS(dev))
3038 return 0;
3039
3040 obj = drm_gem_object_alloc(dev, 4096);
3041 if (obj == NULL) {
3042 DRM_ERROR("Failed to allocate status page\n");
3043 return -ENOMEM;
3044 }
3045 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07003046 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07003047
3048 ret = i915_gem_object_pin(obj, 4096);
3049 if (ret != 0) {
3050 drm_gem_object_unreference(obj);
3051 return ret;
3052 }
3053
3054 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003055
Keith Packardba1eb1d2008-10-14 19:55:10 -07003056 dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
3057 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003058 DRM_ERROR("Failed to map status page.\n");
3059 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3060 drm_gem_object_unreference(obj);
3061 return -EINVAL;
3062 }
3063 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003064 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
3065 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07003066 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07003067 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
3068
3069 return 0;
3070}
3071
Jesse Barnes79e53942008-11-07 14:24:08 -08003072int
Eric Anholt673a3942008-07-30 12:06:12 -07003073i915_gem_init_ringbuffer(struct drm_device *dev)
3074{
3075 drm_i915_private_t *dev_priv = dev->dev_private;
3076 struct drm_gem_object *obj;
3077 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08003078 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07003079 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07003080 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07003081
3082 ret = i915_gem_init_hws(dev);
3083 if (ret != 0)
3084 return ret;
3085
3086 obj = drm_gem_object_alloc(dev, 128 * 1024);
3087 if (obj == NULL) {
3088 DRM_ERROR("Failed to allocate ringbuffer\n");
3089 return -ENOMEM;
3090 }
3091 obj_priv = obj->driver_private;
3092
3093 ret = i915_gem_object_pin(obj, 4096);
3094 if (ret != 0) {
3095 drm_gem_object_unreference(obj);
3096 return ret;
3097 }
3098
3099 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08003100 ring->Size = obj->size;
3101 ring->tail_mask = obj->size - 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003102
Jesse Barnes79e53942008-11-07 14:24:08 -08003103 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
3104 ring->map.size = obj->size;
3105 ring->map.type = 0;
3106 ring->map.flags = 0;
3107 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003108
Jesse Barnes79e53942008-11-07 14:24:08 -08003109 drm_core_ioremap_wc(&ring->map, dev);
3110 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003111 DRM_ERROR("Failed to map ringbuffer.\n");
3112 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3113 drm_gem_object_unreference(obj);
3114 return -EINVAL;
3115 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003116 ring->ring_obj = obj;
3117 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07003118
3119 /* Stop the ring if it's running. */
3120 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003121 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07003122 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003123
3124 /* Initialize the ring. */
3125 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07003126 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3127
3128 /* G45 ring initialization fails to reset head to zero */
3129 if (head != 0) {
3130 DRM_ERROR("Ring head not reset to zero "
3131 "ctl %08x head %08x tail %08x start %08x\n",
3132 I915_READ(PRB0_CTL),
3133 I915_READ(PRB0_HEAD),
3134 I915_READ(PRB0_TAIL),
3135 I915_READ(PRB0_START));
3136 I915_WRITE(PRB0_HEAD, 0);
3137
3138 DRM_ERROR("Ring head forced to zero "
3139 "ctl %08x head %08x tail %08x start %08x\n",
3140 I915_READ(PRB0_CTL),
3141 I915_READ(PRB0_HEAD),
3142 I915_READ(PRB0_TAIL),
3143 I915_READ(PRB0_START));
3144 }
3145
Eric Anholt673a3942008-07-30 12:06:12 -07003146 I915_WRITE(PRB0_CTL,
3147 ((obj->size - 4096) & RING_NR_PAGES) |
3148 RING_NO_REPORT |
3149 RING_VALID);
3150
Keith Packard50aa253d2008-10-14 17:20:35 -07003151 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3152
3153 /* If the head is still not zero, the ring is dead */
3154 if (head != 0) {
3155 DRM_ERROR("Ring initialization failed "
3156 "ctl %08x head %08x tail %08x start %08x\n",
3157 I915_READ(PRB0_CTL),
3158 I915_READ(PRB0_HEAD),
3159 I915_READ(PRB0_TAIL),
3160 I915_READ(PRB0_START));
3161 return -EIO;
3162 }
3163
Eric Anholt673a3942008-07-30 12:06:12 -07003164 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08003165 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3166 i915_kernel_lost_context(dev);
3167 else {
3168 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3169 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
3170 ring->space = ring->head - (ring->tail + 8);
3171 if (ring->space < 0)
3172 ring->space += ring->Size;
3173 }
Eric Anholt673a3942008-07-30 12:06:12 -07003174
3175 return 0;
3176}
3177
Jesse Barnes79e53942008-11-07 14:24:08 -08003178void
Eric Anholt673a3942008-07-30 12:06:12 -07003179i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3180{
3181 drm_i915_private_t *dev_priv = dev->dev_private;
3182
3183 if (dev_priv->ring.ring_obj == NULL)
3184 return;
3185
3186 drm_core_ioremapfree(&dev_priv->ring.map, dev);
3187
3188 i915_gem_object_unpin(dev_priv->ring.ring_obj);
3189 drm_gem_object_unreference(dev_priv->ring.ring_obj);
3190 dev_priv->ring.ring_obj = NULL;
3191 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3192
3193 if (dev_priv->hws_obj != NULL) {
Keith Packardba1eb1d2008-10-14 19:55:10 -07003194 struct drm_gem_object *obj = dev_priv->hws_obj;
3195 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3196
3197 kunmap(obj_priv->page_list[0]);
3198 i915_gem_object_unpin(obj);
3199 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003200 dev_priv->hws_obj = NULL;
3201 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Keith Packardba1eb1d2008-10-14 19:55:10 -07003202 dev_priv->hw_status_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003203
3204 /* Write high address into HWS_PGA when disabling. */
3205 I915_WRITE(HWS_PGA, 0x1ffff000);
3206 }
3207}
3208
3209int
3210i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3211 struct drm_file *file_priv)
3212{
3213 drm_i915_private_t *dev_priv = dev->dev_private;
3214 int ret;
3215
Jesse Barnes79e53942008-11-07 14:24:08 -08003216 if (drm_core_check_feature(dev, DRIVER_MODESET))
3217 return 0;
3218
Eric Anholt673a3942008-07-30 12:06:12 -07003219 if (dev_priv->mm.wedged) {
3220 DRM_ERROR("Reenabling wedged hardware, good luck\n");
3221 dev_priv->mm.wedged = 0;
3222 }
3223
Keith Packard0839ccb2008-10-30 19:38:48 -07003224 dev_priv->mm.gtt_mapping = io_mapping_create_wc(dev->agp->base,
3225 dev->agp->agp_info.aper_size
3226 * 1024 * 1024);
3227
Eric Anholt673a3942008-07-30 12:06:12 -07003228 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003229 dev_priv->mm.suspended = 0;
3230
3231 ret = i915_gem_init_ringbuffer(dev);
3232 if (ret != 0)
3233 return ret;
3234
Eric Anholt673a3942008-07-30 12:06:12 -07003235 BUG_ON(!list_empty(&dev_priv->mm.active_list));
3236 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3237 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3238 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003239 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003240
3241 drm_irq_install(dev);
3242
Eric Anholt673a3942008-07-30 12:06:12 -07003243 return 0;
3244}
3245
3246int
3247i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3248 struct drm_file *file_priv)
3249{
Keith Packard0839ccb2008-10-30 19:38:48 -07003250 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003251 int ret;
3252
Jesse Barnes79e53942008-11-07 14:24:08 -08003253 if (drm_core_check_feature(dev, DRIVER_MODESET))
3254 return 0;
3255
Eric Anholt673a3942008-07-30 12:06:12 -07003256 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003257 drm_irq_uninstall(dev);
3258
Keith Packard0839ccb2008-10-30 19:38:48 -07003259 io_mapping_free(dev_priv->mm.gtt_mapping);
Keith Packard6dbe2772008-10-14 21:41:13 -07003260 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003261}
3262
3263void
3264i915_gem_lastclose(struct drm_device *dev)
3265{
3266 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003267
Keith Packard6dbe2772008-10-14 21:41:13 -07003268 ret = i915_gem_idle(dev);
3269 if (ret)
3270 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003271}
3272
3273void
3274i915_gem_load(struct drm_device *dev)
3275{
3276 drm_i915_private_t *dev_priv = dev->dev_private;
3277
3278 INIT_LIST_HEAD(&dev_priv->mm.active_list);
3279 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3280 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3281 INIT_LIST_HEAD(&dev_priv->mm.request_list);
3282 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3283 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07003284 dev_priv->mm.next_gem_seqno = 1;
3285
Jesse Barnesde151cf2008-11-12 10:03:55 -08003286 /* Old X drivers will take 0-2 for front, back, depth buffers */
3287 dev_priv->fence_reg_start = 3;
3288
3289 if (IS_I965G(dev))
3290 dev_priv->num_fence_regs = 16;
3291 else
3292 dev_priv->num_fence_regs = 8;
3293
Eric Anholt673a3942008-07-30 12:06:12 -07003294 i915_gem_detect_bit_6_swizzle(dev);
3295}