blob: 2b5c7ad9e64a89d9492cb7ef173588429880a191 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
32#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080033#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070034
Eric Anholt28dfe522008-11-13 15:00:55 -080035#define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
36
Eric Anholte47c68e2008-11-14 13:35:19 -080037static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
38static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
39static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080040static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
41 int write);
42static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
43 uint64_t offset,
44 uint64_t size);
45static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070046static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
47static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
48static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080049static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
50 unsigned alignment);
Jesse Barnes0f973f22009-01-26 17:10:45 -080051static int i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write);
Jesse Barnesde151cf2008-11-12 10:03:55 -080052static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
53static int i915_gem_evict_something(struct drm_device *dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +100054static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
55 struct drm_i915_gem_pwrite *args,
56 struct drm_file *file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -070057
Jesse Barnes79e53942008-11-07 14:24:08 -080058int i915_gem_do_init(struct drm_device *dev, unsigned long start,
59 unsigned long end)
60{
61 drm_i915_private_t *dev_priv = dev->dev_private;
62
63 if (start >= end ||
64 (start & (PAGE_SIZE - 1)) != 0 ||
65 (end & (PAGE_SIZE - 1)) != 0) {
66 return -EINVAL;
67 }
68
69 drm_mm_init(&dev_priv->mm.gtt_space, start,
70 end - start);
71
72 dev->gtt_total = (uint32_t) (end - start);
73
74 return 0;
75}
Keith Packard6dbe2772008-10-14 21:41:13 -070076
Eric Anholt673a3942008-07-30 12:06:12 -070077int
78i915_gem_init_ioctl(struct drm_device *dev, void *data,
79 struct drm_file *file_priv)
80{
Eric Anholt673a3942008-07-30 12:06:12 -070081 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080082 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070083
84 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080085 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070086 mutex_unlock(&dev->struct_mutex);
87
Jesse Barnes79e53942008-11-07 14:24:08 -080088 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070089}
90
Eric Anholt5a125c32008-10-22 21:40:13 -070091int
92i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
93 struct drm_file *file_priv)
94{
Eric Anholt5a125c32008-10-22 21:40:13 -070095 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -070096
97 if (!(dev->driver->driver_features & DRIVER_GEM))
98 return -ENODEV;
99
100 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800101 args->aper_available_size = (args->aper_size -
102 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700103
104 return 0;
105}
106
Eric Anholt673a3942008-07-30 12:06:12 -0700107
108/**
109 * Creates a new mm object and returns a handle to it.
110 */
111int
112i915_gem_create_ioctl(struct drm_device *dev, void *data,
113 struct drm_file *file_priv)
114{
115 struct drm_i915_gem_create *args = data;
116 struct drm_gem_object *obj;
117 int handle, ret;
118
119 args->size = roundup(args->size, PAGE_SIZE);
120
121 /* Allocate the new object */
122 obj = drm_gem_object_alloc(dev, args->size);
123 if (obj == NULL)
124 return -ENOMEM;
125
126 ret = drm_gem_handle_create(file_priv, obj, &handle);
127 mutex_lock(&dev->struct_mutex);
128 drm_gem_object_handle_unreference(obj);
129 mutex_unlock(&dev->struct_mutex);
130
131 if (ret)
132 return ret;
133
134 args->handle = handle;
135
136 return 0;
137}
138
139/**
140 * Reads data from the object referenced by handle.
141 *
142 * On error, the contents of *data are undefined.
143 */
144int
145i915_gem_pread_ioctl(struct drm_device *dev, void *data,
146 struct drm_file *file_priv)
147{
148 struct drm_i915_gem_pread *args = data;
149 struct drm_gem_object *obj;
150 struct drm_i915_gem_object *obj_priv;
151 ssize_t read;
152 loff_t offset;
153 int ret;
154
155 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
156 if (obj == NULL)
157 return -EBADF;
158 obj_priv = obj->driver_private;
159
160 /* Bounds check source.
161 *
162 * XXX: This could use review for overflow issues...
163 */
164 if (args->offset > obj->size || args->size > obj->size ||
165 args->offset + args->size > obj->size) {
166 drm_gem_object_unreference(obj);
167 return -EINVAL;
168 }
169
170 mutex_lock(&dev->struct_mutex);
171
Eric Anholte47c68e2008-11-14 13:35:19 -0800172 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
173 args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700174 if (ret != 0) {
175 drm_gem_object_unreference(obj);
176 mutex_unlock(&dev->struct_mutex);
Dave Airliee7d22bc2008-10-07 13:40:36 +1000177 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700178 }
179
180 offset = args->offset;
181
182 read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
183 args->size, &offset);
184 if (read != args->size) {
185 drm_gem_object_unreference(obj);
186 mutex_unlock(&dev->struct_mutex);
187 if (read < 0)
188 return read;
189 else
190 return -EINVAL;
191 }
192
193 drm_gem_object_unreference(obj);
194 mutex_unlock(&dev->struct_mutex);
195
196 return 0;
197}
198
Keith Packard0839ccb2008-10-30 19:38:48 -0700199/* This is the fast write path which cannot handle
200 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700201 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700202
Keith Packard0839ccb2008-10-30 19:38:48 -0700203static inline int
204fast_user_write(struct io_mapping *mapping,
205 loff_t page_base, int page_offset,
206 char __user *user_data,
207 int length)
208{
209 char *vaddr_atomic;
210 unsigned long unwritten;
211
212 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
213 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
214 user_data, length);
215 io_mapping_unmap_atomic(vaddr_atomic);
216 if (unwritten)
217 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700218 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700219}
220
221/* Here's the write path which can sleep for
222 * page faults
223 */
224
225static inline int
226slow_user_write(struct io_mapping *mapping,
227 loff_t page_base, int page_offset,
228 char __user *user_data,
229 int length)
230{
231 char __iomem *vaddr;
232 unsigned long unwritten;
233
234 vaddr = io_mapping_map_wc(mapping, page_base);
235 if (vaddr == NULL)
236 return -EFAULT;
237 unwritten = __copy_from_user(vaddr + page_offset,
238 user_data, length);
239 io_mapping_unmap(vaddr);
240 if (unwritten)
241 return -EFAULT;
242 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700243}
244
Eric Anholt673a3942008-07-30 12:06:12 -0700245static int
246i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
247 struct drm_i915_gem_pwrite *args,
248 struct drm_file *file_priv)
249{
250 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Keith Packard0839ccb2008-10-30 19:38:48 -0700251 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700252 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700253 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700254 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700255 int page_offset, page_length;
256 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700257
258 user_data = (char __user *) (uintptr_t) args->data_ptr;
259 remain = args->size;
260 if (!access_ok(VERIFY_READ, user_data, remain))
261 return -EFAULT;
262
263
264 mutex_lock(&dev->struct_mutex);
265 ret = i915_gem_object_pin(obj, 0);
266 if (ret) {
267 mutex_unlock(&dev->struct_mutex);
268 return ret;
269 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800270 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700271 if (ret)
272 goto fail;
273
274 obj_priv = obj->driver_private;
275 offset = obj_priv->gtt_offset + args->offset;
276 obj_priv->dirty = 1;
277
278 while (remain > 0) {
279 /* Operation in this page
280 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700281 * page_base = page offset within aperture
282 * page_offset = offset within page
283 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700284 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700285 page_base = (offset & ~(PAGE_SIZE-1));
286 page_offset = offset & (PAGE_SIZE-1);
287 page_length = remain;
288 if ((page_offset + remain) > PAGE_SIZE)
289 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700290
Keith Packard0839ccb2008-10-30 19:38:48 -0700291 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
292 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700293
Keith Packard0839ccb2008-10-30 19:38:48 -0700294 /* If we get a fault while copying data, then (presumably) our
295 * source page isn't available. In this case, use the
296 * non-atomic function
297 */
298 if (ret) {
299 ret = slow_user_write (dev_priv->mm.gtt_mapping,
300 page_base, page_offset,
301 user_data, page_length);
302 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -0700303 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700304 }
305
Keith Packard0839ccb2008-10-30 19:38:48 -0700306 remain -= page_length;
307 user_data += page_length;
308 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700309 }
Eric Anholt673a3942008-07-30 12:06:12 -0700310
311fail:
312 i915_gem_object_unpin(obj);
313 mutex_unlock(&dev->struct_mutex);
314
315 return ret;
316}
317
Eric Anholt3043c602008-10-02 12:24:47 -0700318static int
Eric Anholt673a3942008-07-30 12:06:12 -0700319i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
320 struct drm_i915_gem_pwrite *args,
321 struct drm_file *file_priv)
322{
323 int ret;
324 loff_t offset;
325 ssize_t written;
326
327 mutex_lock(&dev->struct_mutex);
328
Eric Anholte47c68e2008-11-14 13:35:19 -0800329 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700330 if (ret) {
331 mutex_unlock(&dev->struct_mutex);
332 return ret;
333 }
334
335 offset = args->offset;
336
337 written = vfs_write(obj->filp,
338 (char __user *)(uintptr_t) args->data_ptr,
339 args->size, &offset);
340 if (written != args->size) {
341 mutex_unlock(&dev->struct_mutex);
342 if (written < 0)
343 return written;
344 else
345 return -EINVAL;
346 }
347
348 mutex_unlock(&dev->struct_mutex);
349
350 return 0;
351}
352
353/**
354 * Writes data to the object referenced by handle.
355 *
356 * On error, the contents of the buffer that were to be modified are undefined.
357 */
358int
359i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
360 struct drm_file *file_priv)
361{
362 struct drm_i915_gem_pwrite *args = data;
363 struct drm_gem_object *obj;
364 struct drm_i915_gem_object *obj_priv;
365 int ret = 0;
366
367 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
368 if (obj == NULL)
369 return -EBADF;
370 obj_priv = obj->driver_private;
371
372 /* Bounds check destination.
373 *
374 * XXX: This could use review for overflow issues...
375 */
376 if (args->offset > obj->size || args->size > obj->size ||
377 args->offset + args->size > obj->size) {
378 drm_gem_object_unreference(obj);
379 return -EINVAL;
380 }
381
382 /* We can only do the GTT pwrite on untiled buffers, as otherwise
383 * it would end up going through the fenced access, and we'll get
384 * different detiling behavior between reading and writing.
385 * pread/pwrite currently are reading and writing from the CPU
386 * perspective, requiring manual detiling by the client.
387 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000388 if (obj_priv->phys_obj)
389 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
390 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
391 dev->gtt_total != 0)
Eric Anholt673a3942008-07-30 12:06:12 -0700392 ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
393 else
394 ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
395
396#if WATCH_PWRITE
397 if (ret)
398 DRM_INFO("pwrite failed %d\n", ret);
399#endif
400
401 drm_gem_object_unreference(obj);
402
403 return ret;
404}
405
406/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800407 * Called when user space prepares to use an object with the CPU, either
408 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700409 */
410int
411i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
412 struct drm_file *file_priv)
413{
414 struct drm_i915_gem_set_domain *args = data;
415 struct drm_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800416 uint32_t read_domains = args->read_domains;
417 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700418 int ret;
419
420 if (!(dev->driver->driver_features & DRIVER_GEM))
421 return -ENODEV;
422
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800423 /* Only handle setting domains to types used by the CPU. */
424 if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
425 return -EINVAL;
426
427 if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
428 return -EINVAL;
429
430 /* Having something in the write domain implies it's in the read
431 * domain, and only that read domain. Enforce that in the request.
432 */
433 if (write_domain != 0 && read_domains != write_domain)
434 return -EINVAL;
435
Eric Anholt673a3942008-07-30 12:06:12 -0700436 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
437 if (obj == NULL)
438 return -EBADF;
439
440 mutex_lock(&dev->struct_mutex);
441#if WATCH_BUF
442 DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800443 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -0700444#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800445 if (read_domains & I915_GEM_DOMAIN_GTT) {
446 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -0800447
448 /* Silently promote "you're not bound, there was nothing to do"
449 * to success, since the client was just asking us to
450 * make sure everything was done.
451 */
452 if (ret == -EINVAL)
453 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800454 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -0800455 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800456 }
457
Eric Anholt673a3942008-07-30 12:06:12 -0700458 drm_gem_object_unreference(obj);
459 mutex_unlock(&dev->struct_mutex);
460 return ret;
461}
462
463/**
464 * Called when user space has done writes to this buffer
465 */
466int
467i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
468 struct drm_file *file_priv)
469{
470 struct drm_i915_gem_sw_finish *args = data;
471 struct drm_gem_object *obj;
472 struct drm_i915_gem_object *obj_priv;
473 int ret = 0;
474
475 if (!(dev->driver->driver_features & DRIVER_GEM))
476 return -ENODEV;
477
478 mutex_lock(&dev->struct_mutex);
479 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
480 if (obj == NULL) {
481 mutex_unlock(&dev->struct_mutex);
482 return -EBADF;
483 }
484
485#if WATCH_BUF
486 DRM_INFO("%s: sw_finish %d (%p %d)\n",
487 __func__, args->handle, obj, obj->size);
488#endif
489 obj_priv = obj->driver_private;
490
491 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -0800492 if (obj_priv->pin_count)
493 i915_gem_object_flush_cpu_write_domain(obj);
494
Eric Anholt673a3942008-07-30 12:06:12 -0700495 drm_gem_object_unreference(obj);
496 mutex_unlock(&dev->struct_mutex);
497 return ret;
498}
499
500/**
501 * Maps the contents of an object, returning the address it is mapped
502 * into.
503 *
504 * While the mapping holds a reference on the contents of the object, it doesn't
505 * imply a ref on the object itself.
506 */
507int
508i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
509 struct drm_file *file_priv)
510{
511 struct drm_i915_gem_mmap *args = data;
512 struct drm_gem_object *obj;
513 loff_t offset;
514 unsigned long addr;
515
516 if (!(dev->driver->driver_features & DRIVER_GEM))
517 return -ENODEV;
518
519 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
520 if (obj == NULL)
521 return -EBADF;
522
523 offset = args->offset;
524
525 down_write(&current->mm->mmap_sem);
526 addr = do_mmap(obj->filp, 0, args->size,
527 PROT_READ | PROT_WRITE, MAP_SHARED,
528 args->offset);
529 up_write(&current->mm->mmap_sem);
530 mutex_lock(&dev->struct_mutex);
531 drm_gem_object_unreference(obj);
532 mutex_unlock(&dev->struct_mutex);
533 if (IS_ERR((void *)addr))
534 return addr;
535
536 args->addr_ptr = (uint64_t) addr;
537
538 return 0;
539}
540
Jesse Barnesde151cf2008-11-12 10:03:55 -0800541/**
542 * i915_gem_fault - fault a page into the GTT
543 * vma: VMA in question
544 * vmf: fault info
545 *
546 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
547 * from userspace. The fault handler takes care of binding the object to
548 * the GTT (if needed), allocating and programming a fence register (again,
549 * only if needed based on whether the old reg is still valid or the object
550 * is tiled) and inserting a new PTE into the faulting process.
551 *
552 * Note that the faulting process may involve evicting existing objects
553 * from the GTT and/or fence registers to make room. So performance may
554 * suffer if the GTT working set is large or there are few fence registers
555 * left.
556 */
557int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
558{
559 struct drm_gem_object *obj = vma->vm_private_data;
560 struct drm_device *dev = obj->dev;
561 struct drm_i915_private *dev_priv = dev->dev_private;
562 struct drm_i915_gem_object *obj_priv = obj->driver_private;
563 pgoff_t page_offset;
564 unsigned long pfn;
565 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800566 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -0800567
568 /* We don't use vmf->pgoff since that has the fake offset */
569 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
570 PAGE_SHIFT;
571
572 /* Now bind it into the GTT if needed */
573 mutex_lock(&dev->struct_mutex);
574 if (!obj_priv->gtt_space) {
575 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
576 if (ret) {
577 mutex_unlock(&dev->struct_mutex);
578 return VM_FAULT_SIGBUS;
579 }
580 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
581 }
582
583 /* Need a new fence register? */
584 if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
Eric Anholtd9ddcb92009-01-27 10:33:49 -0800585 obj_priv->tiling_mode != I915_TILING_NONE) {
Jesse Barnes0f973f22009-01-26 17:10:45 -0800586 ret = i915_gem_object_get_fence_reg(obj, write);
Chris Wilson7d8d58b2009-02-04 14:15:10 +0000587 if (ret) {
588 mutex_unlock(&dev->struct_mutex);
Eric Anholtd9ddcb92009-01-27 10:33:49 -0800589 return VM_FAULT_SIGBUS;
Chris Wilson7d8d58b2009-02-04 14:15:10 +0000590 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -0800591 }
Jesse Barnesde151cf2008-11-12 10:03:55 -0800592
593 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
594 page_offset;
595
596 /* Finally, remap it using the new GTT offset */
597 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
598
599 mutex_unlock(&dev->struct_mutex);
600
601 switch (ret) {
602 case -ENOMEM:
603 case -EAGAIN:
604 return VM_FAULT_OOM;
605 case -EFAULT:
Jesse Barnesde151cf2008-11-12 10:03:55 -0800606 return VM_FAULT_SIGBUS;
607 default:
608 return VM_FAULT_NOPAGE;
609 }
610}
611
612/**
613 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
614 * @obj: obj in question
615 *
616 * GEM memory mapping works by handing back to userspace a fake mmap offset
617 * it can use in a subsequent mmap(2) call. The DRM core code then looks
618 * up the object based on the offset and sets up the various memory mapping
619 * structures.
620 *
621 * This routine allocates and attaches a fake offset for @obj.
622 */
623static int
624i915_gem_create_mmap_offset(struct drm_gem_object *obj)
625{
626 struct drm_device *dev = obj->dev;
627 struct drm_gem_mm *mm = dev->mm_private;
628 struct drm_i915_gem_object *obj_priv = obj->driver_private;
629 struct drm_map_list *list;
630 struct drm_map *map;
631 int ret = 0;
632
633 /* Set the object up for mmap'ing */
634 list = &obj->map_list;
635 list->map = drm_calloc(1, sizeof(struct drm_map_list),
636 DRM_MEM_DRIVER);
637 if (!list->map)
638 return -ENOMEM;
639
640 map = list->map;
641 map->type = _DRM_GEM;
642 map->size = obj->size;
643 map->handle = obj;
644
645 /* Get a DRM GEM mmap offset allocated... */
646 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
647 obj->size / PAGE_SIZE, 0, 0);
648 if (!list->file_offset_node) {
649 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
650 ret = -ENOMEM;
651 goto out_free_list;
652 }
653
654 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
655 obj->size / PAGE_SIZE, 0);
656 if (!list->file_offset_node) {
657 ret = -ENOMEM;
658 goto out_free_list;
659 }
660
661 list->hash.key = list->file_offset_node->start;
662 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
663 DRM_ERROR("failed to add to map hash\n");
664 goto out_free_mm;
665 }
666
667 /* By now we should be all set, any drm_mmap request on the offset
668 * below will get to our mmap & fault handler */
669 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
670
671 return 0;
672
673out_free_mm:
674 drm_mm_put_block(list->file_offset_node);
675out_free_list:
676 drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
677
678 return ret;
679}
680
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800681static void
682i915_gem_free_mmap_offset(struct drm_gem_object *obj)
683{
684 struct drm_device *dev = obj->dev;
685 struct drm_i915_gem_object *obj_priv = obj->driver_private;
686 struct drm_gem_mm *mm = dev->mm_private;
687 struct drm_map_list *list;
688
689 list = &obj->map_list;
690 drm_ht_remove_item(&mm->offset_hash, &list->hash);
691
692 if (list->file_offset_node) {
693 drm_mm_put_block(list->file_offset_node);
694 list->file_offset_node = NULL;
695 }
696
697 if (list->map) {
698 drm_free(list->map, sizeof(struct drm_map), DRM_MEM_DRIVER);
699 list->map = NULL;
700 }
701
702 obj_priv->mmap_offset = 0;
703}
704
Jesse Barnesde151cf2008-11-12 10:03:55 -0800705/**
706 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
707 * @obj: object to check
708 *
709 * Return the required GTT alignment for an object, taking into account
710 * potential fence register mapping if needed.
711 */
712static uint32_t
713i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
714{
715 struct drm_device *dev = obj->dev;
716 struct drm_i915_gem_object *obj_priv = obj->driver_private;
717 int start, i;
718
719 /*
720 * Minimum alignment is 4k (GTT page size), but might be greater
721 * if a fence register is needed for the object.
722 */
723 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
724 return 4096;
725
726 /*
727 * Previous chips need to be aligned to the size of the smallest
728 * fence register that can contain the object.
729 */
730 if (IS_I9XX(dev))
731 start = 1024*1024;
732 else
733 start = 512*1024;
734
735 for (i = start; i < obj->size; i <<= 1)
736 ;
737
738 return i;
739}
740
741/**
742 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
743 * @dev: DRM device
744 * @data: GTT mapping ioctl data
745 * @file_priv: GEM object info
746 *
747 * Simply returns the fake offset to userspace so it can mmap it.
748 * The mmap call will end up in drm_gem_mmap(), which will set things
749 * up so we can get faults in the handler above.
750 *
751 * The fault handler will take care of binding the object into the GTT
752 * (since it may have been evicted to make room for something), allocating
753 * a fence register, and mapping the appropriate aperture address into
754 * userspace.
755 */
756int
757i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
758 struct drm_file *file_priv)
759{
760 struct drm_i915_gem_mmap_gtt *args = data;
761 struct drm_i915_private *dev_priv = dev->dev_private;
762 struct drm_gem_object *obj;
763 struct drm_i915_gem_object *obj_priv;
764 int ret;
765
766 if (!(dev->driver->driver_features & DRIVER_GEM))
767 return -ENODEV;
768
769 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
770 if (obj == NULL)
771 return -EBADF;
772
773 mutex_lock(&dev->struct_mutex);
774
775 obj_priv = obj->driver_private;
776
777 if (!obj_priv->mmap_offset) {
778 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +0000779 if (ret) {
780 drm_gem_object_unreference(obj);
781 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -0800782 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +0000783 }
Jesse Barnesde151cf2008-11-12 10:03:55 -0800784 }
785
786 args->offset = obj_priv->mmap_offset;
787
788 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
789
790 /* Make sure the alignment is correct for fence regs etc */
791 if (obj_priv->agp_mem &&
792 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
793 drm_gem_object_unreference(obj);
794 mutex_unlock(&dev->struct_mutex);
795 return -EINVAL;
796 }
797
798 /*
799 * Pull it into the GTT so that we have a page list (makes the
800 * initial fault faster and any subsequent flushing possible).
801 */
802 if (!obj_priv->agp_mem) {
803 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
804 if (ret) {
805 drm_gem_object_unreference(obj);
806 mutex_unlock(&dev->struct_mutex);
807 return ret;
808 }
809 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
810 }
811
812 drm_gem_object_unreference(obj);
813 mutex_unlock(&dev->struct_mutex);
814
815 return 0;
816}
817
Eric Anholt673a3942008-07-30 12:06:12 -0700818static void
819i915_gem_object_free_page_list(struct drm_gem_object *obj)
820{
821 struct drm_i915_gem_object *obj_priv = obj->driver_private;
822 int page_count = obj->size / PAGE_SIZE;
823 int i;
824
825 if (obj_priv->page_list == NULL)
826 return;
827
828
829 for (i = 0; i < page_count; i++)
830 if (obj_priv->page_list[i] != NULL) {
831 if (obj_priv->dirty)
832 set_page_dirty(obj_priv->page_list[i]);
833 mark_page_accessed(obj_priv->page_list[i]);
834 page_cache_release(obj_priv->page_list[i]);
835 }
836 obj_priv->dirty = 0;
837
838 drm_free(obj_priv->page_list,
839 page_count * sizeof(struct page *),
840 DRM_MEM_DRIVER);
841 obj_priv->page_list = NULL;
842}
843
844static void
Eric Anholtce44b0e2008-11-06 16:00:31 -0800845i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -0700846{
847 struct drm_device *dev = obj->dev;
848 drm_i915_private_t *dev_priv = dev->dev_private;
849 struct drm_i915_gem_object *obj_priv = obj->driver_private;
850
851 /* Add a reference if we're newly entering the active list. */
852 if (!obj_priv->active) {
853 drm_gem_object_reference(obj);
854 obj_priv->active = 1;
855 }
856 /* Move from whatever list we were on to the tail of execution. */
857 list_move_tail(&obj_priv->list,
858 &dev_priv->mm.active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -0800859 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -0700860}
861
Eric Anholtce44b0e2008-11-06 16:00:31 -0800862static void
863i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
864{
865 struct drm_device *dev = obj->dev;
866 drm_i915_private_t *dev_priv = dev->dev_private;
867 struct drm_i915_gem_object *obj_priv = obj->driver_private;
868
869 BUG_ON(!obj_priv->active);
870 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
871 obj_priv->last_rendering_seqno = 0;
872}
Eric Anholt673a3942008-07-30 12:06:12 -0700873
874static void
875i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
876{
877 struct drm_device *dev = obj->dev;
878 drm_i915_private_t *dev_priv = dev->dev_private;
879 struct drm_i915_gem_object *obj_priv = obj->driver_private;
880
881 i915_verify_inactive(dev, __FILE__, __LINE__);
882 if (obj_priv->pin_count != 0)
883 list_del_init(&obj_priv->list);
884 else
885 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
886
Eric Anholtce44b0e2008-11-06 16:00:31 -0800887 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700888 if (obj_priv->active) {
889 obj_priv->active = 0;
890 drm_gem_object_unreference(obj);
891 }
892 i915_verify_inactive(dev, __FILE__, __LINE__);
893}
894
895/**
896 * Creates a new sequence number, emitting a write of it to the status page
897 * plus an interrupt, which will trigger i915_user_interrupt_handler.
898 *
899 * Must be called with struct_lock held.
900 *
901 * Returned sequence numbers are nonzero on success.
902 */
903static uint32_t
904i915_add_request(struct drm_device *dev, uint32_t flush_domains)
905{
906 drm_i915_private_t *dev_priv = dev->dev_private;
907 struct drm_i915_gem_request *request;
908 uint32_t seqno;
909 int was_empty;
910 RING_LOCALS;
911
912 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
913 if (request == NULL)
914 return 0;
915
916 /* Grab the seqno we're going to make this request be, and bump the
917 * next (skipping 0 so it can be the reserved no-seqno value).
918 */
919 seqno = dev_priv->mm.next_gem_seqno;
920 dev_priv->mm.next_gem_seqno++;
921 if (dev_priv->mm.next_gem_seqno == 0)
922 dev_priv->mm.next_gem_seqno++;
923
924 BEGIN_LP_RING(4);
925 OUT_RING(MI_STORE_DWORD_INDEX);
926 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
927 OUT_RING(seqno);
928
929 OUT_RING(MI_USER_INTERRUPT);
930 ADVANCE_LP_RING();
931
932 DRM_DEBUG("%d\n", seqno);
933
934 request->seqno = seqno;
935 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -0700936 was_empty = list_empty(&dev_priv->mm.request_list);
937 list_add_tail(&request->list, &dev_priv->mm.request_list);
938
Eric Anholtce44b0e2008-11-06 16:00:31 -0800939 /* Associate any objects on the flushing list matching the write
940 * domain we're flushing with our flush.
941 */
942 if (flush_domains != 0) {
943 struct drm_i915_gem_object *obj_priv, *next;
944
945 list_for_each_entry_safe(obj_priv, next,
946 &dev_priv->mm.flushing_list, list) {
947 struct drm_gem_object *obj = obj_priv->obj;
948
949 if ((obj->write_domain & flush_domains) ==
950 obj->write_domain) {
951 obj->write_domain = 0;
952 i915_gem_object_move_to_active(obj, seqno);
953 }
954 }
955
956 }
957
Keith Packard6dbe2772008-10-14 21:41:13 -0700958 if (was_empty && !dev_priv->mm.suspended)
Eric Anholt673a3942008-07-30 12:06:12 -0700959 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
960 return seqno;
961}
962
963/**
964 * Command execution barrier
965 *
966 * Ensures that all commands in the ring are finished
967 * before signalling the CPU
968 */
Eric Anholt3043c602008-10-02 12:24:47 -0700969static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -0700970i915_retire_commands(struct drm_device *dev)
971{
972 drm_i915_private_t *dev_priv = dev->dev_private;
973 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
974 uint32_t flush_domains = 0;
975 RING_LOCALS;
976
977 /* The sampler always gets flushed on i965 (sigh) */
978 if (IS_I965G(dev))
979 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
980 BEGIN_LP_RING(2);
981 OUT_RING(cmd);
982 OUT_RING(0); /* noop */
983 ADVANCE_LP_RING();
984 return flush_domains;
985}
986
987/**
988 * Moves buffers associated only with the given active seqno from the active
989 * to inactive list, potentially freeing them.
990 */
991static void
992i915_gem_retire_request(struct drm_device *dev,
993 struct drm_i915_gem_request *request)
994{
995 drm_i915_private_t *dev_priv = dev->dev_private;
996
997 /* Move any buffers on the active list that are no longer referenced
998 * by the ringbuffer to the flushing/inactive lists as appropriate.
999 */
1000 while (!list_empty(&dev_priv->mm.active_list)) {
1001 struct drm_gem_object *obj;
1002 struct drm_i915_gem_object *obj_priv;
1003
1004 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1005 struct drm_i915_gem_object,
1006 list);
1007 obj = obj_priv->obj;
1008
1009 /* If the seqno being retired doesn't match the oldest in the
1010 * list, then the oldest in the list must still be newer than
1011 * this seqno.
1012 */
1013 if (obj_priv->last_rendering_seqno != request->seqno)
1014 return;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001015
Eric Anholt673a3942008-07-30 12:06:12 -07001016#if WATCH_LRU
1017 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1018 __func__, request->seqno, obj);
1019#endif
1020
Eric Anholtce44b0e2008-11-06 16:00:31 -08001021 if (obj->write_domain != 0)
1022 i915_gem_object_move_to_flushing(obj);
1023 else
Eric Anholt673a3942008-07-30 12:06:12 -07001024 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001025 }
1026}
1027
1028/**
1029 * Returns true if seq1 is later than seq2.
1030 */
1031static int
1032i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1033{
1034 return (int32_t)(seq1 - seq2) >= 0;
1035}
1036
1037uint32_t
1038i915_get_gem_seqno(struct drm_device *dev)
1039{
1040 drm_i915_private_t *dev_priv = dev->dev_private;
1041
1042 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1043}
1044
1045/**
1046 * This function clears the request list as sequence numbers are passed.
1047 */
1048void
1049i915_gem_retire_requests(struct drm_device *dev)
1050{
1051 drm_i915_private_t *dev_priv = dev->dev_private;
1052 uint32_t seqno;
1053
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001054 if (!dev_priv->hw_status_page)
1055 return;
1056
Eric Anholt673a3942008-07-30 12:06:12 -07001057 seqno = i915_get_gem_seqno(dev);
1058
1059 while (!list_empty(&dev_priv->mm.request_list)) {
1060 struct drm_i915_gem_request *request;
1061 uint32_t retiring_seqno;
1062
1063 request = list_first_entry(&dev_priv->mm.request_list,
1064 struct drm_i915_gem_request,
1065 list);
1066 retiring_seqno = request->seqno;
1067
1068 if (i915_seqno_passed(seqno, retiring_seqno) ||
1069 dev_priv->mm.wedged) {
1070 i915_gem_retire_request(dev, request);
1071
1072 list_del(&request->list);
1073 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
1074 } else
1075 break;
1076 }
1077}
1078
1079void
1080i915_gem_retire_work_handler(struct work_struct *work)
1081{
1082 drm_i915_private_t *dev_priv;
1083 struct drm_device *dev;
1084
1085 dev_priv = container_of(work, drm_i915_private_t,
1086 mm.retire_work.work);
1087 dev = dev_priv->dev;
1088
1089 mutex_lock(&dev->struct_mutex);
1090 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001091 if (!dev_priv->mm.suspended &&
1092 !list_empty(&dev_priv->mm.request_list))
Eric Anholt673a3942008-07-30 12:06:12 -07001093 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1094 mutex_unlock(&dev->struct_mutex);
1095}
1096
1097/**
1098 * Waits for a sequence number to be signaled, and cleans up the
1099 * request and object lists appropriately for that event.
1100 */
Eric Anholt3043c602008-10-02 12:24:47 -07001101static int
Eric Anholt673a3942008-07-30 12:06:12 -07001102i915_wait_request(struct drm_device *dev, uint32_t seqno)
1103{
1104 drm_i915_private_t *dev_priv = dev->dev_private;
1105 int ret = 0;
1106
1107 BUG_ON(seqno == 0);
1108
1109 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
1110 dev_priv->mm.waiting_gem_seqno = seqno;
1111 i915_user_irq_get(dev);
1112 ret = wait_event_interruptible(dev_priv->irq_queue,
1113 i915_seqno_passed(i915_get_gem_seqno(dev),
1114 seqno) ||
1115 dev_priv->mm.wedged);
1116 i915_user_irq_put(dev);
1117 dev_priv->mm.waiting_gem_seqno = 0;
1118 }
1119 if (dev_priv->mm.wedged)
1120 ret = -EIO;
1121
1122 if (ret && ret != -ERESTARTSYS)
1123 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1124 __func__, ret, seqno, i915_get_gem_seqno(dev));
1125
1126 /* Directly dispatch request retiring. While we have the work queue
1127 * to handle this, the waiter on a request often wants an associated
1128 * buffer to have made it to the inactive list, and we would need
1129 * a separate wait queue to handle that.
1130 */
1131 if (ret == 0)
1132 i915_gem_retire_requests(dev);
1133
1134 return ret;
1135}
1136
1137static void
1138i915_gem_flush(struct drm_device *dev,
1139 uint32_t invalidate_domains,
1140 uint32_t flush_domains)
1141{
1142 drm_i915_private_t *dev_priv = dev->dev_private;
1143 uint32_t cmd;
1144 RING_LOCALS;
1145
1146#if WATCH_EXEC
1147 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1148 invalidate_domains, flush_domains);
1149#endif
1150
1151 if (flush_domains & I915_GEM_DOMAIN_CPU)
1152 drm_agp_chipset_flush(dev);
1153
1154 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
1155 I915_GEM_DOMAIN_GTT)) {
1156 /*
1157 * read/write caches:
1158 *
1159 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1160 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1161 * also flushed at 2d versus 3d pipeline switches.
1162 *
1163 * read-only caches:
1164 *
1165 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1166 * MI_READ_FLUSH is set, and is always flushed on 965.
1167 *
1168 * I915_GEM_DOMAIN_COMMAND may not exist?
1169 *
1170 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1171 * invalidated when MI_EXE_FLUSH is set.
1172 *
1173 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1174 * invalidated with every MI_FLUSH.
1175 *
1176 * TLBs:
1177 *
1178 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1179 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1180 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1181 * are flushed at any MI_FLUSH.
1182 */
1183
1184 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1185 if ((invalidate_domains|flush_domains) &
1186 I915_GEM_DOMAIN_RENDER)
1187 cmd &= ~MI_NO_WRITE_FLUSH;
1188 if (!IS_I965G(dev)) {
1189 /*
1190 * On the 965, the sampler cache always gets flushed
1191 * and this bit is reserved.
1192 */
1193 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1194 cmd |= MI_READ_FLUSH;
1195 }
1196 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1197 cmd |= MI_EXE_FLUSH;
1198
1199#if WATCH_EXEC
1200 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1201#endif
1202 BEGIN_LP_RING(2);
1203 OUT_RING(cmd);
1204 OUT_RING(0); /* noop */
1205 ADVANCE_LP_RING();
1206 }
1207}
1208
1209/**
1210 * Ensures that all rendering to the object has completed and the object is
1211 * safe to unbind from the GTT or access from the CPU.
1212 */
1213static int
1214i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1215{
1216 struct drm_device *dev = obj->dev;
1217 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1218 int ret;
1219
Eric Anholte47c68e2008-11-14 13:35:19 -08001220 /* This function only exists to support waiting for existing rendering,
1221 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001222 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001223 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001224
1225 /* If there is rendering queued on the buffer being evicted, wait for
1226 * it.
1227 */
1228 if (obj_priv->active) {
1229#if WATCH_BUF
1230 DRM_INFO("%s: object %p wait for seqno %08x\n",
1231 __func__, obj, obj_priv->last_rendering_seqno);
1232#endif
1233 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1234 if (ret != 0)
1235 return ret;
1236 }
1237
1238 return 0;
1239}
1240
1241/**
1242 * Unbinds an object from the GTT aperture.
1243 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001244int
Eric Anholt673a3942008-07-30 12:06:12 -07001245i915_gem_object_unbind(struct drm_gem_object *obj)
1246{
1247 struct drm_device *dev = obj->dev;
1248 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001249 loff_t offset;
Eric Anholt673a3942008-07-30 12:06:12 -07001250 int ret = 0;
1251
1252#if WATCH_BUF
1253 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1254 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1255#endif
1256 if (obj_priv->gtt_space == NULL)
1257 return 0;
1258
1259 if (obj_priv->pin_count != 0) {
1260 DRM_ERROR("Attempting to unbind pinned buffer\n");
1261 return -EINVAL;
1262 }
1263
Eric Anholt673a3942008-07-30 12:06:12 -07001264 /* Move the object to the CPU domain to ensure that
1265 * any possible CPU writes while it's not in the GTT
1266 * are flushed when we go to remap it. This will
1267 * also ensure that all pending GPU writes are finished
1268 * before we unbind.
1269 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001270 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001271 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001272 if (ret != -ERESTARTSYS)
1273 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001274 return ret;
1275 }
1276
1277 if (obj_priv->agp_mem != NULL) {
1278 drm_unbind_agp(obj_priv->agp_mem);
1279 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1280 obj_priv->agp_mem = NULL;
1281 }
1282
1283 BUG_ON(obj_priv->active);
1284
Jesse Barnesde151cf2008-11-12 10:03:55 -08001285 /* blow away mappings if mapped through GTT */
1286 offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08001287 if (dev->dev_mapping)
1288 unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001289
1290 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1291 i915_gem_clear_fence_reg(obj);
1292
Eric Anholt673a3942008-07-30 12:06:12 -07001293 i915_gem_object_free_page_list(obj);
1294
1295 if (obj_priv->gtt_space) {
1296 atomic_dec(&dev->gtt_count);
1297 atomic_sub(obj->size, &dev->gtt_memory);
1298
1299 drm_mm_put_block(obj_priv->gtt_space);
1300 obj_priv->gtt_space = NULL;
1301 }
1302
1303 /* Remove ourselves from the LRU list if present. */
1304 if (!list_empty(&obj_priv->list))
1305 list_del_init(&obj_priv->list);
1306
1307 return 0;
1308}
1309
1310static int
1311i915_gem_evict_something(struct drm_device *dev)
1312{
1313 drm_i915_private_t *dev_priv = dev->dev_private;
1314 struct drm_gem_object *obj;
1315 struct drm_i915_gem_object *obj_priv;
1316 int ret = 0;
1317
1318 for (;;) {
1319 /* If there's an inactive buffer available now, grab it
1320 * and be done.
1321 */
1322 if (!list_empty(&dev_priv->mm.inactive_list)) {
1323 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1324 struct drm_i915_gem_object,
1325 list);
1326 obj = obj_priv->obj;
1327 BUG_ON(obj_priv->pin_count != 0);
1328#if WATCH_LRU
1329 DRM_INFO("%s: evicting %p\n", __func__, obj);
1330#endif
1331 BUG_ON(obj_priv->active);
1332
1333 /* Wait on the rendering and unbind the buffer. */
1334 ret = i915_gem_object_unbind(obj);
1335 break;
1336 }
1337
1338 /* If we didn't get anything, but the ring is still processing
1339 * things, wait for one of those things to finish and hopefully
1340 * leave us a buffer to evict.
1341 */
1342 if (!list_empty(&dev_priv->mm.request_list)) {
1343 struct drm_i915_gem_request *request;
1344
1345 request = list_first_entry(&dev_priv->mm.request_list,
1346 struct drm_i915_gem_request,
1347 list);
1348
1349 ret = i915_wait_request(dev, request->seqno);
1350 if (ret)
1351 break;
1352
1353 /* if waiting caused an object to become inactive,
1354 * then loop around and wait for it. Otherwise, we
1355 * assume that waiting freed and unbound something,
1356 * so there should now be some space in the GTT
1357 */
1358 if (!list_empty(&dev_priv->mm.inactive_list))
1359 continue;
1360 break;
1361 }
1362
1363 /* If we didn't have anything on the request list but there
1364 * are buffers awaiting a flush, emit one and try again.
1365 * When we wait on it, those buffers waiting for that flush
1366 * will get moved to inactive.
1367 */
1368 if (!list_empty(&dev_priv->mm.flushing_list)) {
1369 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1370 struct drm_i915_gem_object,
1371 list);
1372 obj = obj_priv->obj;
1373
1374 i915_gem_flush(dev,
1375 obj->write_domain,
1376 obj->write_domain);
1377 i915_add_request(dev, obj->write_domain);
1378
1379 obj = NULL;
1380 continue;
1381 }
1382
1383 DRM_ERROR("inactive empty %d request empty %d "
1384 "flushing empty %d\n",
1385 list_empty(&dev_priv->mm.inactive_list),
1386 list_empty(&dev_priv->mm.request_list),
1387 list_empty(&dev_priv->mm.flushing_list));
1388 /* If we didn't do any of the above, there's nothing to be done
1389 * and we just can't fit it in.
1390 */
1391 return -ENOMEM;
1392 }
1393 return ret;
1394}
1395
1396static int
Keith Packardac94a962008-11-20 23:30:27 -08001397i915_gem_evict_everything(struct drm_device *dev)
1398{
1399 int ret;
1400
1401 for (;;) {
1402 ret = i915_gem_evict_something(dev);
1403 if (ret != 0)
1404 break;
1405 }
Owain Ainsworth15c35332008-12-06 20:42:20 -08001406 if (ret == -ENOMEM)
1407 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08001408 return ret;
1409}
1410
1411static int
Eric Anholt673a3942008-07-30 12:06:12 -07001412i915_gem_object_get_page_list(struct drm_gem_object *obj)
1413{
1414 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1415 int page_count, i;
1416 struct address_space *mapping;
1417 struct inode *inode;
1418 struct page *page;
1419 int ret;
1420
1421 if (obj_priv->page_list)
1422 return 0;
1423
1424 /* Get the list of pages out of our struct file. They'll be pinned
1425 * at this point until we release them.
1426 */
1427 page_count = obj->size / PAGE_SIZE;
1428 BUG_ON(obj_priv->page_list != NULL);
1429 obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
1430 DRM_MEM_DRIVER);
1431 if (obj_priv->page_list == NULL) {
1432 DRM_ERROR("Faled to allocate page list\n");
1433 return -ENOMEM;
1434 }
1435
1436 inode = obj->filp->f_path.dentry->d_inode;
1437 mapping = inode->i_mapping;
1438 for (i = 0; i < page_count; i++) {
1439 page = read_mapping_page(mapping, i, NULL);
1440 if (IS_ERR(page)) {
1441 ret = PTR_ERR(page);
1442 DRM_ERROR("read_mapping_page failed: %d\n", ret);
1443 i915_gem_object_free_page_list(obj);
1444 return ret;
1445 }
1446 obj_priv->page_list[i] = page;
1447 }
1448 return 0;
1449}
1450
Jesse Barnesde151cf2008-11-12 10:03:55 -08001451static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
1452{
1453 struct drm_gem_object *obj = reg->obj;
1454 struct drm_device *dev = obj->dev;
1455 drm_i915_private_t *dev_priv = dev->dev_private;
1456 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1457 int regnum = obj_priv->fence_reg;
1458 uint64_t val;
1459
1460 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
1461 0xfffff000) << 32;
1462 val |= obj_priv->gtt_offset & 0xfffff000;
1463 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
1464 if (obj_priv->tiling_mode == I915_TILING_Y)
1465 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
1466 val |= I965_FENCE_REG_VALID;
1467
1468 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
1469}
1470
1471static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
1472{
1473 struct drm_gem_object *obj = reg->obj;
1474 struct drm_device *dev = obj->dev;
1475 drm_i915_private_t *dev_priv = dev->dev_private;
1476 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1477 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001478 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001479 uint32_t val;
1480 uint32_t pitch_val;
1481
1482 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1483 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08001484 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08001485 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001486 return;
1487 }
1488
Jesse Barnes0f973f22009-01-26 17:10:45 -08001489 if (obj_priv->tiling_mode == I915_TILING_Y &&
1490 HAS_128_BYTE_Y_TILING(dev))
1491 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001492 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08001493 tile_width = 512;
1494
1495 /* Note: pitch better be a power of two tile widths */
1496 pitch_val = obj_priv->stride / tile_width;
1497 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001498
1499 val = obj_priv->gtt_offset;
1500 if (obj_priv->tiling_mode == I915_TILING_Y)
1501 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1502 val |= I915_FENCE_SIZE_BITS(obj->size);
1503 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1504 val |= I830_FENCE_REG_VALID;
1505
1506 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
1507}
1508
1509static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
1510{
1511 struct drm_gem_object *obj = reg->obj;
1512 struct drm_device *dev = obj->dev;
1513 drm_i915_private_t *dev_priv = dev->dev_private;
1514 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1515 int regnum = obj_priv->fence_reg;
1516 uint32_t val;
1517 uint32_t pitch_val;
1518
1519 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1520 (obj_priv->gtt_offset & (obj->size - 1))) {
Jesse Barnes0f973f22009-01-26 17:10:45 -08001521 WARN(1, "%s: object 0x%08x not 1M or size aligned\n",
1522 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001523 return;
1524 }
1525
1526 pitch_val = (obj_priv->stride / 128) - 1;
1527
1528 val = obj_priv->gtt_offset;
1529 if (obj_priv->tiling_mode == I915_TILING_Y)
1530 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1531 val |= I830_FENCE_SIZE_BITS(obj->size);
1532 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1533 val |= I830_FENCE_REG_VALID;
1534
1535 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
1536
1537}
1538
1539/**
1540 * i915_gem_object_get_fence_reg - set up a fence reg for an object
1541 * @obj: object to map through a fence reg
Jesse Barnes0f973f22009-01-26 17:10:45 -08001542 * @write: object is about to be written
Jesse Barnesde151cf2008-11-12 10:03:55 -08001543 *
1544 * When mapping objects through the GTT, userspace wants to be able to write
1545 * to them without having to worry about swizzling if the object is tiled.
1546 *
1547 * This function walks the fence regs looking for a free one for @obj,
1548 * stealing one if it can't find any.
1549 *
1550 * It then sets up the reg based on the object's properties: address, pitch
1551 * and tiling format.
1552 */
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001553static int
Jesse Barnes0f973f22009-01-26 17:10:45 -08001554i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001555{
1556 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001557 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001558 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1559 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00001560 struct drm_i915_gem_object *old_obj_priv = NULL;
1561 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001562
1563 switch (obj_priv->tiling_mode) {
1564 case I915_TILING_NONE:
1565 WARN(1, "allocating a fence for non-tiled object?\n");
1566 break;
1567 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08001568 if (!obj_priv->stride)
1569 return -EINVAL;
1570 WARN((obj_priv->stride & (512 - 1)),
1571 "object 0x%08x is X tiled but has non-512B pitch\n",
1572 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001573 break;
1574 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08001575 if (!obj_priv->stride)
1576 return -EINVAL;
1577 WARN((obj_priv->stride & (128 - 1)),
1578 "object 0x%08x is Y tiled but has non-128B pitch\n",
1579 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001580 break;
1581 }
1582
1583 /* First try to find a free reg */
Chris Wilson9b2412f2009-02-11 14:26:44 +00001584try_again:
Chris Wilsonfc7170b2009-02-11 14:26:46 +00001585 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001586 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
1587 reg = &dev_priv->fence_regs[i];
1588 if (!reg->obj)
1589 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00001590
1591 old_obj_priv = reg->obj->driver_private;
1592 if (!old_obj_priv->pin_count)
1593 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001594 }
1595
1596 /* None available, try to steal one or wait for a user to finish */
1597 if (i == dev_priv->num_fence_regs) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001598 loff_t offset;
1599
Chris Wilsonfc7170b2009-02-11 14:26:46 +00001600 if (avail == 0)
1601 return -ENOMEM;
1602
Jesse Barnesde151cf2008-11-12 10:03:55 -08001603 /* Could try to use LRU here instead... */
1604 for (i = dev_priv->fence_reg_start;
1605 i < dev_priv->num_fence_regs; i++) {
1606 reg = &dev_priv->fence_regs[i];
1607 old_obj_priv = reg->obj->driver_private;
1608 if (!old_obj_priv->pin_count)
1609 break;
1610 }
1611
1612 /*
1613 * Now things get ugly... we have to wait for one of the
1614 * objects to finish before trying again.
1615 */
1616 if (i == dev_priv->num_fence_regs) {
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001617 ret = i915_gem_object_set_to_gtt_domain(reg->obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001618 if (ret) {
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001619 WARN(ret != -ERESTARTSYS,
1620 "switch to GTT domain failed: %d\n", ret);
1621 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001622 }
1623 goto try_again;
1624 }
1625
1626 /*
1627 * Zap this virtual mapping so we can set up a fence again
1628 * for this object next time we need it.
1629 */
1630 offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08001631 if (dev->dev_mapping)
1632 unmap_mapping_range(dev->dev_mapping, offset,
1633 reg->obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001634 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
1635 }
1636
1637 obj_priv->fence_reg = i;
1638 reg->obj = obj;
1639
1640 if (IS_I965G(dev))
1641 i965_write_fence_reg(reg);
1642 else if (IS_I9XX(dev))
1643 i915_write_fence_reg(reg);
1644 else
1645 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001646
1647 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001648}
1649
1650/**
1651 * i915_gem_clear_fence_reg - clear out fence register info
1652 * @obj: object to clear
1653 *
1654 * Zeroes out the fence register itself and clears out the associated
1655 * data structures in dev_priv and obj_priv.
1656 */
1657static void
1658i915_gem_clear_fence_reg(struct drm_gem_object *obj)
1659{
1660 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001661 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001662 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1663
1664 if (IS_I965G(dev))
1665 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
1666 else
1667 I915_WRITE(FENCE_REG_830_0 + (obj_priv->fence_reg * 4), 0);
1668
1669 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
1670 obj_priv->fence_reg = I915_FENCE_REG_NONE;
1671}
1672
Eric Anholt673a3942008-07-30 12:06:12 -07001673/**
1674 * Finds free space in the GTT aperture and binds the object there.
1675 */
1676static int
1677i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
1678{
1679 struct drm_device *dev = obj->dev;
1680 drm_i915_private_t *dev_priv = dev->dev_private;
1681 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1682 struct drm_mm_node *free_space;
1683 int page_count, ret;
1684
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08001685 if (dev_priv->mm.suspended)
1686 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07001687 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08001688 alignment = i915_gem_get_gtt_alignment(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001689 if (alignment & (PAGE_SIZE - 1)) {
1690 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
1691 return -EINVAL;
1692 }
1693
1694 search_free:
1695 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
1696 obj->size, alignment, 0);
1697 if (free_space != NULL) {
1698 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
1699 alignment);
1700 if (obj_priv->gtt_space != NULL) {
1701 obj_priv->gtt_space->private = obj;
1702 obj_priv->gtt_offset = obj_priv->gtt_space->start;
1703 }
1704 }
1705 if (obj_priv->gtt_space == NULL) {
1706 /* If the gtt is empty and we're still having trouble
1707 * fitting our object in, we're out of memory.
1708 */
1709#if WATCH_LRU
1710 DRM_INFO("%s: GTT full, evicting something\n", __func__);
1711#endif
1712 if (list_empty(&dev_priv->mm.inactive_list) &&
1713 list_empty(&dev_priv->mm.flushing_list) &&
1714 list_empty(&dev_priv->mm.active_list)) {
1715 DRM_ERROR("GTT full, but LRU list empty\n");
1716 return -ENOMEM;
1717 }
1718
1719 ret = i915_gem_evict_something(dev);
1720 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08001721 if (ret != -ERESTARTSYS)
1722 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001723 return ret;
1724 }
1725 goto search_free;
1726 }
1727
1728#if WATCH_BUF
1729 DRM_INFO("Binding object of size %d at 0x%08x\n",
1730 obj->size, obj_priv->gtt_offset);
1731#endif
1732 ret = i915_gem_object_get_page_list(obj);
1733 if (ret) {
1734 drm_mm_put_block(obj_priv->gtt_space);
1735 obj_priv->gtt_space = NULL;
1736 return ret;
1737 }
1738
1739 page_count = obj->size / PAGE_SIZE;
1740 /* Create an AGP memory structure pointing at our pages, and bind it
1741 * into the GTT.
1742 */
1743 obj_priv->agp_mem = drm_agp_bind_pages(dev,
1744 obj_priv->page_list,
1745 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07001746 obj_priv->gtt_offset,
1747 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07001748 if (obj_priv->agp_mem == NULL) {
1749 i915_gem_object_free_page_list(obj);
1750 drm_mm_put_block(obj_priv->gtt_space);
1751 obj_priv->gtt_space = NULL;
1752 return -ENOMEM;
1753 }
1754 atomic_inc(&dev->gtt_count);
1755 atomic_add(obj->size, &dev->gtt_memory);
1756
1757 /* Assert that the object is not currently in any GPU domain. As it
1758 * wasn't in the GTT, there shouldn't be any way it could have been in
1759 * a GPU cache
1760 */
1761 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1762 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1763
1764 return 0;
1765}
1766
1767void
1768i915_gem_clflush_object(struct drm_gem_object *obj)
1769{
1770 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1771
1772 /* If we don't have a page list set up, then we're not pinned
1773 * to GPU, and we can ignore the cache flush because it'll happen
1774 * again at bind time.
1775 */
1776 if (obj_priv->page_list == NULL)
1777 return;
1778
1779 drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
1780}
1781
Eric Anholte47c68e2008-11-14 13:35:19 -08001782/** Flushes any GPU write domain for the object if it's dirty. */
1783static void
1784i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
1785{
1786 struct drm_device *dev = obj->dev;
1787 uint32_t seqno;
1788
1789 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
1790 return;
1791
1792 /* Queue the GPU write cache flushing we need. */
1793 i915_gem_flush(dev, 0, obj->write_domain);
1794 seqno = i915_add_request(dev, obj->write_domain);
1795 obj->write_domain = 0;
1796 i915_gem_object_move_to_active(obj, seqno);
1797}
1798
1799/** Flushes the GTT write domain for the object if it's dirty. */
1800static void
1801i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
1802{
1803 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
1804 return;
1805
1806 /* No actual flushing is required for the GTT write domain. Writes
1807 * to it immediately go to main memory as far as we know, so there's
1808 * no chipset flush. It also doesn't land in render cache.
1809 */
1810 obj->write_domain = 0;
1811}
1812
1813/** Flushes the CPU write domain for the object if it's dirty. */
1814static void
1815i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
1816{
1817 struct drm_device *dev = obj->dev;
1818
1819 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
1820 return;
1821
1822 i915_gem_clflush_object(obj);
1823 drm_agp_chipset_flush(dev);
1824 obj->write_domain = 0;
1825}
1826
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001827/**
1828 * Moves a single object to the GTT read, and possibly write domain.
1829 *
1830 * This function returns when the move is complete, including waiting on
1831 * flushes to occur.
1832 */
Jesse Barnes79e53942008-11-07 14:24:08 -08001833int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001834i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
1835{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001836 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08001837 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001838
Eric Anholt02354392008-11-26 13:58:13 -08001839 /* Not valid to be called on unbound objects. */
1840 if (obj_priv->gtt_space == NULL)
1841 return -EINVAL;
1842
Eric Anholte47c68e2008-11-14 13:35:19 -08001843 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001844 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08001845 ret = i915_gem_object_wait_rendering(obj);
1846 if (ret != 0)
1847 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001848
1849 /* If we're writing through the GTT domain, then CPU and GPU caches
1850 * will need to be invalidated at next use.
1851 */
1852 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08001853 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001854
Eric Anholte47c68e2008-11-14 13:35:19 -08001855 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001856
1857 /* It should now be out of any other write domains, and we can update
1858 * the domain values for our changes.
1859 */
1860 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
1861 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08001862 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001863 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08001864 obj_priv->dirty = 1;
1865 }
1866
1867 return 0;
1868}
1869
1870/**
1871 * Moves a single object to the CPU read, and possibly write domain.
1872 *
1873 * This function returns when the move is complete, including waiting on
1874 * flushes to occur.
1875 */
1876static int
1877i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
1878{
1879 struct drm_device *dev = obj->dev;
1880 int ret;
1881
1882 i915_gem_object_flush_gpu_write_domain(obj);
1883 /* Wait on any GPU rendering and flushing to occur. */
1884 ret = i915_gem_object_wait_rendering(obj);
1885 if (ret != 0)
1886 return ret;
1887
1888 i915_gem_object_flush_gtt_write_domain(obj);
1889
1890 /* If we have a partially-valid cache of the object in the CPU,
1891 * finish invalidating it and free the per-page flags.
1892 */
1893 i915_gem_object_set_to_full_cpu_read_domain(obj);
1894
1895 /* Flush the CPU cache if it's still invalid. */
1896 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
1897 i915_gem_clflush_object(obj);
1898 drm_agp_chipset_flush(dev);
1899
1900 obj->read_domains |= I915_GEM_DOMAIN_CPU;
1901 }
1902
1903 /* It should now be out of any other write domains, and we can update
1904 * the domain values for our changes.
1905 */
1906 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
1907
1908 /* If we're writing through the CPU, then the GPU read domains will
1909 * need to be invalidated at next use.
1910 */
1911 if (write) {
1912 obj->read_domains &= I915_GEM_DOMAIN_CPU;
1913 obj->write_domain = I915_GEM_DOMAIN_CPU;
1914 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001915
1916 return 0;
1917}
1918
Eric Anholt673a3942008-07-30 12:06:12 -07001919/*
1920 * Set the next domain for the specified object. This
1921 * may not actually perform the necessary flushing/invaliding though,
1922 * as that may want to be batched with other set_domain operations
1923 *
1924 * This is (we hope) the only really tricky part of gem. The goal
1925 * is fairly simple -- track which caches hold bits of the object
1926 * and make sure they remain coherent. A few concrete examples may
1927 * help to explain how it works. For shorthand, we use the notation
1928 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
1929 * a pair of read and write domain masks.
1930 *
1931 * Case 1: the batch buffer
1932 *
1933 * 1. Allocated
1934 * 2. Written by CPU
1935 * 3. Mapped to GTT
1936 * 4. Read by GPU
1937 * 5. Unmapped from GTT
1938 * 6. Freed
1939 *
1940 * Let's take these a step at a time
1941 *
1942 * 1. Allocated
1943 * Pages allocated from the kernel may still have
1944 * cache contents, so we set them to (CPU, CPU) always.
1945 * 2. Written by CPU (using pwrite)
1946 * The pwrite function calls set_domain (CPU, CPU) and
1947 * this function does nothing (as nothing changes)
1948 * 3. Mapped by GTT
1949 * This function asserts that the object is not
1950 * currently in any GPU-based read or write domains
1951 * 4. Read by GPU
1952 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
1953 * As write_domain is zero, this function adds in the
1954 * current read domains (CPU+COMMAND, 0).
1955 * flush_domains is set to CPU.
1956 * invalidate_domains is set to COMMAND
1957 * clflush is run to get data out of the CPU caches
1958 * then i915_dev_set_domain calls i915_gem_flush to
1959 * emit an MI_FLUSH and drm_agp_chipset_flush
1960 * 5. Unmapped from GTT
1961 * i915_gem_object_unbind calls set_domain (CPU, CPU)
1962 * flush_domains and invalidate_domains end up both zero
1963 * so no flushing/invalidating happens
1964 * 6. Freed
1965 * yay, done
1966 *
1967 * Case 2: The shared render buffer
1968 *
1969 * 1. Allocated
1970 * 2. Mapped to GTT
1971 * 3. Read/written by GPU
1972 * 4. set_domain to (CPU,CPU)
1973 * 5. Read/written by CPU
1974 * 6. Read/written by GPU
1975 *
1976 * 1. Allocated
1977 * Same as last example, (CPU, CPU)
1978 * 2. Mapped to GTT
1979 * Nothing changes (assertions find that it is not in the GPU)
1980 * 3. Read/written by GPU
1981 * execbuffer calls set_domain (RENDER, RENDER)
1982 * flush_domains gets CPU
1983 * invalidate_domains gets GPU
1984 * clflush (obj)
1985 * MI_FLUSH and drm_agp_chipset_flush
1986 * 4. set_domain (CPU, CPU)
1987 * flush_domains gets GPU
1988 * invalidate_domains gets CPU
1989 * wait_rendering (obj) to make sure all drawing is complete.
1990 * This will include an MI_FLUSH to get the data from GPU
1991 * to memory
1992 * clflush (obj) to invalidate the CPU cache
1993 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
1994 * 5. Read/written by CPU
1995 * cache lines are loaded and dirtied
1996 * 6. Read written by GPU
1997 * Same as last GPU access
1998 *
1999 * Case 3: The constant buffer
2000 *
2001 * 1. Allocated
2002 * 2. Written by CPU
2003 * 3. Read by GPU
2004 * 4. Updated (written) by CPU again
2005 * 5. Read by GPU
2006 *
2007 * 1. Allocated
2008 * (CPU, CPU)
2009 * 2. Written by CPU
2010 * (CPU, CPU)
2011 * 3. Read by GPU
2012 * (CPU+RENDER, 0)
2013 * flush_domains = CPU
2014 * invalidate_domains = RENDER
2015 * clflush (obj)
2016 * MI_FLUSH
2017 * drm_agp_chipset_flush
2018 * 4. Updated (written) by CPU again
2019 * (CPU, CPU)
2020 * flush_domains = 0 (no previous write domain)
2021 * invalidate_domains = 0 (no new read domains)
2022 * 5. Read by GPU
2023 * (CPU+RENDER, 0)
2024 * flush_domains = CPU
2025 * invalidate_domains = RENDER
2026 * clflush (obj)
2027 * MI_FLUSH
2028 * drm_agp_chipset_flush
2029 */
Keith Packardc0d90822008-11-20 23:11:08 -08002030static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002031i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002032{
2033 struct drm_device *dev = obj->dev;
2034 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2035 uint32_t invalidate_domains = 0;
2036 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002037
Eric Anholt8b0e3782009-02-19 14:40:50 -08002038 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2039 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002040
2041#if WATCH_BUF
2042 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2043 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002044 obj->read_domains, obj->pending_read_domains,
2045 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002046#endif
2047 /*
2048 * If the object isn't moving to a new write domain,
2049 * let the object stay in multiple read domains
2050 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002051 if (obj->pending_write_domain == 0)
2052 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002053 else
2054 obj_priv->dirty = 1;
2055
2056 /*
2057 * Flush the current write domain if
2058 * the new read domains don't match. Invalidate
2059 * any read domains which differ from the old
2060 * write domain
2061 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002062 if (obj->write_domain &&
2063 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002064 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002065 invalidate_domains |=
2066 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002067 }
2068 /*
2069 * Invalidate any read caches which may have
2070 * stale data. That is, any new read domains.
2071 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002072 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002073 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2074#if WATCH_BUF
2075 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2076 __func__, flush_domains, invalidate_domains);
2077#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002078 i915_gem_clflush_object(obj);
2079 }
2080
Eric Anholtefbeed92009-02-19 14:54:51 -08002081 /* The actual obj->write_domain will be updated with
2082 * pending_write_domain after we emit the accumulated flush for all
2083 * of our domain changes in execbuffers (which clears objects'
2084 * write_domains). So if we have a current write domain that we
2085 * aren't changing, set pending_write_domain to that.
2086 */
2087 if (flush_domains == 0 && obj->pending_write_domain == 0)
2088 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002089 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002090
2091 dev->invalidate_domains |= invalidate_domains;
2092 dev->flush_domains |= flush_domains;
2093#if WATCH_BUF
2094 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2095 __func__,
2096 obj->read_domains, obj->write_domain,
2097 dev->invalidate_domains, dev->flush_domains);
2098#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002099}
2100
2101/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002102 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002103 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002104 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2105 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2106 */
2107static void
2108i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2109{
2110 struct drm_device *dev = obj->dev;
2111 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2112
2113 if (!obj_priv->page_cpu_valid)
2114 return;
2115
2116 /* If we're partially in the CPU read domain, finish moving it in.
2117 */
2118 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2119 int i;
2120
2121 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2122 if (obj_priv->page_cpu_valid[i])
2123 continue;
2124 drm_clflush_pages(obj_priv->page_list + i, 1);
2125 }
2126 drm_agp_chipset_flush(dev);
2127 }
2128
2129 /* Free the page_cpu_valid mappings which are now stale, whether
2130 * or not we've got I915_GEM_DOMAIN_CPU.
2131 */
2132 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
2133 DRM_MEM_DRIVER);
2134 obj_priv->page_cpu_valid = NULL;
2135}
2136
2137/**
2138 * Set the CPU read domain on a range of the object.
2139 *
2140 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2141 * not entirely valid. The page_cpu_valid member of the object flags which
2142 * pages have been flushed, and will be respected by
2143 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2144 * of the whole object.
2145 *
2146 * This function returns when the move is complete, including waiting on
2147 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002148 */
2149static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002150i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2151 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002152{
2153 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002154 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002155
Eric Anholte47c68e2008-11-14 13:35:19 -08002156 if (offset == 0 && size == obj->size)
2157 return i915_gem_object_set_to_cpu_domain(obj, 0);
2158
2159 i915_gem_object_flush_gpu_write_domain(obj);
2160 /* Wait on any GPU rendering and flushing to occur. */
2161 ret = i915_gem_object_wait_rendering(obj);
2162 if (ret != 0)
2163 return ret;
2164 i915_gem_object_flush_gtt_write_domain(obj);
2165
2166 /* If we're already fully in the CPU read domain, we're done. */
2167 if (obj_priv->page_cpu_valid == NULL &&
2168 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002169 return 0;
2170
Eric Anholte47c68e2008-11-14 13:35:19 -08002171 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2172 * newly adding I915_GEM_DOMAIN_CPU
2173 */
Eric Anholt673a3942008-07-30 12:06:12 -07002174 if (obj_priv->page_cpu_valid == NULL) {
2175 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2176 DRM_MEM_DRIVER);
Eric Anholte47c68e2008-11-14 13:35:19 -08002177 if (obj_priv->page_cpu_valid == NULL)
2178 return -ENOMEM;
2179 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2180 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002181
2182 /* Flush the cache on any pages that are still invalid from the CPU's
2183 * perspective.
2184 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002185 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2186 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002187 if (obj_priv->page_cpu_valid[i])
2188 continue;
2189
2190 drm_clflush_pages(obj_priv->page_list + i, 1);
2191
2192 obj_priv->page_cpu_valid[i] = 1;
2193 }
2194
Eric Anholte47c68e2008-11-14 13:35:19 -08002195 /* It should now be out of any other write domains, and we can update
2196 * the domain values for our changes.
2197 */
2198 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2199
2200 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2201
Eric Anholt673a3942008-07-30 12:06:12 -07002202 return 0;
2203}
2204
2205/**
Eric Anholt673a3942008-07-30 12:06:12 -07002206 * Pin an object to the GTT and evaluate the relocations landing in it.
2207 */
2208static int
2209i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2210 struct drm_file *file_priv,
2211 struct drm_i915_gem_exec_object *entry)
2212{
2213 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002214 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002215 struct drm_i915_gem_relocation_entry reloc;
2216 struct drm_i915_gem_relocation_entry __user *relocs;
2217 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2218 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002219 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002220
2221 /* Choose the GTT offset for our buffer and put it there. */
2222 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2223 if (ret)
2224 return ret;
2225
2226 entry->offset = obj_priv->gtt_offset;
2227
2228 relocs = (struct drm_i915_gem_relocation_entry __user *)
2229 (uintptr_t) entry->relocs_ptr;
2230 /* Apply the relocations, using the GTT aperture to avoid cache
2231 * flushing requirements.
2232 */
2233 for (i = 0; i < entry->relocation_count; i++) {
2234 struct drm_gem_object *target_obj;
2235 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002236 uint32_t reloc_val, reloc_offset;
2237 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002238
2239 ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
2240 if (ret != 0) {
2241 i915_gem_object_unpin(obj);
2242 return ret;
2243 }
2244
2245 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
2246 reloc.target_handle);
2247 if (target_obj == NULL) {
2248 i915_gem_object_unpin(obj);
2249 return -EBADF;
2250 }
2251 target_obj_priv = target_obj->driver_private;
2252
2253 /* The target buffer should have appeared before us in the
2254 * exec_object list, so it should have a GTT space bound by now.
2255 */
2256 if (target_obj_priv->gtt_space == NULL) {
2257 DRM_ERROR("No GTT space found for object %d\n",
2258 reloc.target_handle);
2259 drm_gem_object_unreference(target_obj);
2260 i915_gem_object_unpin(obj);
2261 return -EINVAL;
2262 }
2263
2264 if (reloc.offset > obj->size - 4) {
2265 DRM_ERROR("Relocation beyond object bounds: "
2266 "obj %p target %d offset %d size %d.\n",
2267 obj, reloc.target_handle,
2268 (int) reloc.offset, (int) obj->size);
2269 drm_gem_object_unreference(target_obj);
2270 i915_gem_object_unpin(obj);
2271 return -EINVAL;
2272 }
2273 if (reloc.offset & 3) {
2274 DRM_ERROR("Relocation not 4-byte aligned: "
2275 "obj %p target %d offset %d.\n",
2276 obj, reloc.target_handle,
2277 (int) reloc.offset);
2278 drm_gem_object_unreference(target_obj);
2279 i915_gem_object_unpin(obj);
2280 return -EINVAL;
2281 }
2282
Eric Anholte47c68e2008-11-14 13:35:19 -08002283 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
2284 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
2285 DRM_ERROR("reloc with read/write CPU domains: "
2286 "obj %p target %d offset %d "
2287 "read %08x write %08x",
2288 obj, reloc.target_handle,
2289 (int) reloc.offset,
2290 reloc.read_domains,
2291 reloc.write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00002292 drm_gem_object_unreference(target_obj);
2293 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002294 return -EINVAL;
2295 }
2296
Eric Anholt673a3942008-07-30 12:06:12 -07002297 if (reloc.write_domain && target_obj->pending_write_domain &&
2298 reloc.write_domain != target_obj->pending_write_domain) {
2299 DRM_ERROR("Write domain conflict: "
2300 "obj %p target %d offset %d "
2301 "new %08x old %08x\n",
2302 obj, reloc.target_handle,
2303 (int) reloc.offset,
2304 reloc.write_domain,
2305 target_obj->pending_write_domain);
2306 drm_gem_object_unreference(target_obj);
2307 i915_gem_object_unpin(obj);
2308 return -EINVAL;
2309 }
2310
2311#if WATCH_RELOC
2312 DRM_INFO("%s: obj %p offset %08x target %d "
2313 "read %08x write %08x gtt %08x "
2314 "presumed %08x delta %08x\n",
2315 __func__,
2316 obj,
2317 (int) reloc.offset,
2318 (int) reloc.target_handle,
2319 (int) reloc.read_domains,
2320 (int) reloc.write_domain,
2321 (int) target_obj_priv->gtt_offset,
2322 (int) reloc.presumed_offset,
2323 reloc.delta);
2324#endif
2325
2326 target_obj->pending_read_domains |= reloc.read_domains;
2327 target_obj->pending_write_domain |= reloc.write_domain;
2328
2329 /* If the relocation already has the right value in it, no
2330 * more work needs to be done.
2331 */
2332 if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
2333 drm_gem_object_unreference(target_obj);
2334 continue;
2335 }
2336
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002337 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
2338 if (ret != 0) {
2339 drm_gem_object_unreference(target_obj);
2340 i915_gem_object_unpin(obj);
2341 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07002342 }
2343
2344 /* Map the page containing the relocation we're going to
2345 * perform.
2346 */
2347 reloc_offset = obj_priv->gtt_offset + reloc.offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07002348 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
2349 (reloc_offset &
2350 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07002351 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07002352 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt673a3942008-07-30 12:06:12 -07002353 reloc_val = target_obj_priv->gtt_offset + reloc.delta;
2354
2355#if WATCH_BUF
2356 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
2357 obj, (unsigned int) reloc.offset,
2358 readl(reloc_entry), reloc_val);
2359#endif
2360 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07002361 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07002362
2363 /* Write the updated presumed offset for this entry back out
2364 * to the user.
2365 */
2366 reloc.presumed_offset = target_obj_priv->gtt_offset;
2367 ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
2368 if (ret != 0) {
2369 drm_gem_object_unreference(target_obj);
2370 i915_gem_object_unpin(obj);
2371 return ret;
2372 }
2373
2374 drm_gem_object_unreference(target_obj);
2375 }
2376
Eric Anholt673a3942008-07-30 12:06:12 -07002377#if WATCH_BUF
2378 if (0)
2379 i915_gem_dump_object(obj, 128, __func__, ~0);
2380#endif
2381 return 0;
2382}
2383
2384/** Dispatch a batchbuffer to the ring
2385 */
2386static int
2387i915_dispatch_gem_execbuffer(struct drm_device *dev,
2388 struct drm_i915_gem_execbuffer *exec,
2389 uint64_t exec_offset)
2390{
2391 drm_i915_private_t *dev_priv = dev->dev_private;
2392 struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
2393 (uintptr_t) exec->cliprects_ptr;
2394 int nbox = exec->num_cliprects;
2395 int i = 0, count;
2396 uint32_t exec_start, exec_len;
2397 RING_LOCALS;
2398
2399 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
2400 exec_len = (uint32_t) exec->batch_len;
2401
2402 if ((exec_start | exec_len) & 0x7) {
2403 DRM_ERROR("alignment\n");
2404 return -EINVAL;
2405 }
2406
2407 if (!exec_start)
2408 return -EINVAL;
2409
2410 count = nbox ? nbox : 1;
2411
2412 for (i = 0; i < count; i++) {
2413 if (i < nbox) {
2414 int ret = i915_emit_box(dev, boxes, i,
2415 exec->DR1, exec->DR4);
2416 if (ret)
2417 return ret;
2418 }
2419
2420 if (IS_I830(dev) || IS_845G(dev)) {
2421 BEGIN_LP_RING(4);
2422 OUT_RING(MI_BATCH_BUFFER);
2423 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2424 OUT_RING(exec_start + exec_len - 4);
2425 OUT_RING(0);
2426 ADVANCE_LP_RING();
2427 } else {
2428 BEGIN_LP_RING(2);
2429 if (IS_I965G(dev)) {
2430 OUT_RING(MI_BATCH_BUFFER_START |
2431 (2 << 6) |
2432 MI_BATCH_NON_SECURE_I965);
2433 OUT_RING(exec_start);
2434 } else {
2435 OUT_RING(MI_BATCH_BUFFER_START |
2436 (2 << 6));
2437 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2438 }
2439 ADVANCE_LP_RING();
2440 }
2441 }
2442
2443 /* XXX breadcrumb */
2444 return 0;
2445}
2446
2447/* Throttle our rendering by waiting until the ring has completed our requests
2448 * emitted over 20 msec ago.
2449 *
2450 * This should get us reasonable parallelism between CPU and GPU but also
2451 * relatively low latency when blocking on a particular request to finish.
2452 */
2453static int
2454i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
2455{
2456 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2457 int ret = 0;
2458 uint32_t seqno;
2459
2460 mutex_lock(&dev->struct_mutex);
2461 seqno = i915_file_priv->mm.last_gem_throttle_seqno;
2462 i915_file_priv->mm.last_gem_throttle_seqno =
2463 i915_file_priv->mm.last_gem_seqno;
2464 if (seqno)
2465 ret = i915_wait_request(dev, seqno);
2466 mutex_unlock(&dev->struct_mutex);
2467 return ret;
2468}
2469
2470int
2471i915_gem_execbuffer(struct drm_device *dev, void *data,
2472 struct drm_file *file_priv)
2473{
2474 drm_i915_private_t *dev_priv = dev->dev_private;
2475 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2476 struct drm_i915_gem_execbuffer *args = data;
2477 struct drm_i915_gem_exec_object *exec_list = NULL;
2478 struct drm_gem_object **object_list = NULL;
2479 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05002480 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002481 int ret, i, pinned = 0;
2482 uint64_t exec_offset;
2483 uint32_t seqno, flush_domains;
Keith Packardac94a962008-11-20 23:30:27 -08002484 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07002485
2486#if WATCH_EXEC
2487 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
2488 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
2489#endif
2490
Eric Anholt4f481ed2008-09-10 14:22:49 -07002491 if (args->buffer_count < 1) {
2492 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
2493 return -EINVAL;
2494 }
Eric Anholt673a3942008-07-30 12:06:12 -07002495 /* Copy in the exec list from userland */
2496 exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
2497 DRM_MEM_DRIVER);
2498 object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
2499 DRM_MEM_DRIVER);
2500 if (exec_list == NULL || object_list == NULL) {
2501 DRM_ERROR("Failed to allocate exec or object list "
2502 "for %d buffers\n",
2503 args->buffer_count);
2504 ret = -ENOMEM;
2505 goto pre_mutex_err;
2506 }
2507 ret = copy_from_user(exec_list,
2508 (struct drm_i915_relocation_entry __user *)
2509 (uintptr_t) args->buffers_ptr,
2510 sizeof(*exec_list) * args->buffer_count);
2511 if (ret != 0) {
2512 DRM_ERROR("copy %d exec entries failed %d\n",
2513 args->buffer_count, ret);
2514 goto pre_mutex_err;
2515 }
2516
2517 mutex_lock(&dev->struct_mutex);
2518
2519 i915_verify_inactive(dev, __FILE__, __LINE__);
2520
2521 if (dev_priv->mm.wedged) {
2522 DRM_ERROR("Execbuf while wedged\n");
2523 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00002524 ret = -EIO;
2525 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07002526 }
2527
2528 if (dev_priv->mm.suspended) {
2529 DRM_ERROR("Execbuf while VT-switched.\n");
2530 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00002531 ret = -EBUSY;
2532 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07002533 }
2534
Keith Packardac94a962008-11-20 23:30:27 -08002535 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07002536 for (i = 0; i < args->buffer_count; i++) {
2537 object_list[i] = drm_gem_object_lookup(dev, file_priv,
2538 exec_list[i].handle);
2539 if (object_list[i] == NULL) {
2540 DRM_ERROR("Invalid object handle %d at index %d\n",
2541 exec_list[i].handle, i);
2542 ret = -EBADF;
2543 goto err;
2544 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05002545
2546 obj_priv = object_list[i]->driver_private;
2547 if (obj_priv->in_execbuffer) {
2548 DRM_ERROR("Object %p appears more than once in object list\n",
2549 object_list[i]);
2550 ret = -EBADF;
2551 goto err;
2552 }
2553 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08002554 }
Eric Anholt673a3942008-07-30 12:06:12 -07002555
Keith Packardac94a962008-11-20 23:30:27 -08002556 /* Pin and relocate */
2557 for (pin_tries = 0; ; pin_tries++) {
2558 ret = 0;
2559 for (i = 0; i < args->buffer_count; i++) {
2560 object_list[i]->pending_read_domains = 0;
2561 object_list[i]->pending_write_domain = 0;
2562 ret = i915_gem_object_pin_and_relocate(object_list[i],
2563 file_priv,
2564 &exec_list[i]);
2565 if (ret)
2566 break;
2567 pinned = i + 1;
2568 }
2569 /* success */
2570 if (ret == 0)
2571 break;
2572
2573 /* error other than GTT full, or we've already tried again */
2574 if (ret != -ENOMEM || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08002575 if (ret != -ERESTARTSYS)
2576 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002577 goto err;
2578 }
Keith Packardac94a962008-11-20 23:30:27 -08002579
2580 /* unpin all of our buffers */
2581 for (i = 0; i < pinned; i++)
2582 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08002583 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08002584
2585 /* evict everyone we can from the aperture */
2586 ret = i915_gem_evict_everything(dev);
2587 if (ret)
2588 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07002589 }
2590
2591 /* Set the pending read domains for the batch buffer to COMMAND */
2592 batch_obj = object_list[args->buffer_count-1];
2593 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
2594 batch_obj->pending_write_domain = 0;
2595
2596 i915_verify_inactive(dev, __FILE__, __LINE__);
2597
Keith Packard646f0f62008-11-20 23:23:03 -08002598 /* Zero the global flush/invalidate flags. These
2599 * will be modified as new domains are computed
2600 * for each object
2601 */
2602 dev->invalidate_domains = 0;
2603 dev->flush_domains = 0;
2604
Eric Anholt673a3942008-07-30 12:06:12 -07002605 for (i = 0; i < args->buffer_count; i++) {
2606 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002607
Keith Packard646f0f62008-11-20 23:23:03 -08002608 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002609 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002610 }
2611
2612 i915_verify_inactive(dev, __FILE__, __LINE__);
2613
Keith Packard646f0f62008-11-20 23:23:03 -08002614 if (dev->invalidate_domains | dev->flush_domains) {
2615#if WATCH_EXEC
2616 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
2617 __func__,
2618 dev->invalidate_domains,
2619 dev->flush_domains);
2620#endif
2621 i915_gem_flush(dev,
2622 dev->invalidate_domains,
2623 dev->flush_domains);
2624 if (dev->flush_domains)
2625 (void)i915_add_request(dev, dev->flush_domains);
2626 }
Eric Anholt673a3942008-07-30 12:06:12 -07002627
Eric Anholtefbeed92009-02-19 14:54:51 -08002628 for (i = 0; i < args->buffer_count; i++) {
2629 struct drm_gem_object *obj = object_list[i];
2630
2631 obj->write_domain = obj->pending_write_domain;
2632 }
2633
Eric Anholt673a3942008-07-30 12:06:12 -07002634 i915_verify_inactive(dev, __FILE__, __LINE__);
2635
2636#if WATCH_COHERENCY
2637 for (i = 0; i < args->buffer_count; i++) {
2638 i915_gem_object_check_coherency(object_list[i],
2639 exec_list[i].handle);
2640 }
2641#endif
2642
2643 exec_offset = exec_list[args->buffer_count - 1].offset;
2644
2645#if WATCH_EXEC
2646 i915_gem_dump_object(object_list[args->buffer_count - 1],
2647 args->batch_len,
2648 __func__,
2649 ~0);
2650#endif
2651
Eric Anholt673a3942008-07-30 12:06:12 -07002652 /* Exec the batchbuffer */
2653 ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
2654 if (ret) {
2655 DRM_ERROR("dispatch failed %d\n", ret);
2656 goto err;
2657 }
2658
2659 /*
2660 * Ensure that the commands in the batch buffer are
2661 * finished before the interrupt fires
2662 */
2663 flush_domains = i915_retire_commands(dev);
2664
2665 i915_verify_inactive(dev, __FILE__, __LINE__);
2666
2667 /*
2668 * Get a seqno representing the execution of the current buffer,
2669 * which we can wait on. We would like to mitigate these interrupts,
2670 * likely by only creating seqnos occasionally (so that we have
2671 * *some* interrupts representing completion of buffers that we can
2672 * wait on when trying to clear up gtt space).
2673 */
2674 seqno = i915_add_request(dev, flush_domains);
2675 BUG_ON(seqno == 0);
2676 i915_file_priv->mm.last_gem_seqno = seqno;
2677 for (i = 0; i < args->buffer_count; i++) {
2678 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002679
Eric Anholtce44b0e2008-11-06 16:00:31 -08002680 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002681#if WATCH_LRU
2682 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
2683#endif
2684 }
2685#if WATCH_LRU
2686 i915_dump_lru(dev, __func__);
2687#endif
2688
2689 i915_verify_inactive(dev, __FILE__, __LINE__);
2690
Eric Anholt673a3942008-07-30 12:06:12 -07002691err:
Julia Lawallaad87df2008-12-21 16:28:47 +01002692 for (i = 0; i < pinned; i++)
2693 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07002694
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05002695 for (i = 0; i < args->buffer_count; i++) {
2696 if (object_list[i]) {
2697 obj_priv = object_list[i]->driver_private;
2698 obj_priv->in_execbuffer = false;
2699 }
Julia Lawallaad87df2008-12-21 16:28:47 +01002700 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05002701 }
Julia Lawallaad87df2008-12-21 16:28:47 +01002702
Eric Anholt673a3942008-07-30 12:06:12 -07002703 mutex_unlock(&dev->struct_mutex);
2704
Roland Dreiera35f2e22009-02-06 17:48:09 -08002705 if (!ret) {
2706 /* Copy the new buffer offsets back to the user's exec list. */
2707 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
2708 (uintptr_t) args->buffers_ptr,
2709 exec_list,
2710 sizeof(*exec_list) * args->buffer_count);
2711 if (ret)
2712 DRM_ERROR("failed to copy %d exec entries "
2713 "back to user (%d)\n",
2714 args->buffer_count, ret);
2715 }
2716
Eric Anholt673a3942008-07-30 12:06:12 -07002717pre_mutex_err:
2718 drm_free(object_list, sizeof(*object_list) * args->buffer_count,
2719 DRM_MEM_DRIVER);
2720 drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
2721 DRM_MEM_DRIVER);
2722
2723 return ret;
2724}
2725
2726int
2727i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
2728{
2729 struct drm_device *dev = obj->dev;
2730 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2731 int ret;
2732
2733 i915_verify_inactive(dev, __FILE__, __LINE__);
2734 if (obj_priv->gtt_space == NULL) {
2735 ret = i915_gem_object_bind_to_gtt(obj, alignment);
2736 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002737 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05002738 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002739 return ret;
2740 }
Chris Wilson22c344e2009-02-11 14:26:45 +00002741 }
2742 /*
2743 * Pre-965 chips need a fence register set up in order to
2744 * properly handle tiled surfaces.
2745 */
2746 if (!IS_I965G(dev) &&
2747 obj_priv->fence_reg == I915_FENCE_REG_NONE &&
2748 obj_priv->tiling_mode != I915_TILING_NONE) {
2749 ret = i915_gem_object_get_fence_reg(obj, true);
2750 if (ret != 0) {
2751 if (ret != -EBUSY && ret != -ERESTARTSYS)
2752 DRM_ERROR("Failure to install fence: %d\n",
2753 ret);
2754 return ret;
2755 }
Eric Anholt673a3942008-07-30 12:06:12 -07002756 }
2757 obj_priv->pin_count++;
2758
2759 /* If the object is not active and not pending a flush,
2760 * remove it from the inactive list
2761 */
2762 if (obj_priv->pin_count == 1) {
2763 atomic_inc(&dev->pin_count);
2764 atomic_add(obj->size, &dev->pin_memory);
2765 if (!obj_priv->active &&
2766 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2767 I915_GEM_DOMAIN_GTT)) == 0 &&
2768 !list_empty(&obj_priv->list))
2769 list_del_init(&obj_priv->list);
2770 }
2771 i915_verify_inactive(dev, __FILE__, __LINE__);
2772
2773 return 0;
2774}
2775
2776void
2777i915_gem_object_unpin(struct drm_gem_object *obj)
2778{
2779 struct drm_device *dev = obj->dev;
2780 drm_i915_private_t *dev_priv = dev->dev_private;
2781 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2782
2783 i915_verify_inactive(dev, __FILE__, __LINE__);
2784 obj_priv->pin_count--;
2785 BUG_ON(obj_priv->pin_count < 0);
2786 BUG_ON(obj_priv->gtt_space == NULL);
2787
2788 /* If the object is no longer pinned, and is
2789 * neither active nor being flushed, then stick it on
2790 * the inactive list
2791 */
2792 if (obj_priv->pin_count == 0) {
2793 if (!obj_priv->active &&
2794 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2795 I915_GEM_DOMAIN_GTT)) == 0)
2796 list_move_tail(&obj_priv->list,
2797 &dev_priv->mm.inactive_list);
2798 atomic_dec(&dev->pin_count);
2799 atomic_sub(obj->size, &dev->pin_memory);
2800 }
2801 i915_verify_inactive(dev, __FILE__, __LINE__);
2802}
2803
2804int
2805i915_gem_pin_ioctl(struct drm_device *dev, void *data,
2806 struct drm_file *file_priv)
2807{
2808 struct drm_i915_gem_pin *args = data;
2809 struct drm_gem_object *obj;
2810 struct drm_i915_gem_object *obj_priv;
2811 int ret;
2812
2813 mutex_lock(&dev->struct_mutex);
2814
2815 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2816 if (obj == NULL) {
2817 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
2818 args->handle);
2819 mutex_unlock(&dev->struct_mutex);
2820 return -EBADF;
2821 }
2822 obj_priv = obj->driver_private;
2823
Jesse Barnes79e53942008-11-07 14:24:08 -08002824 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
2825 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
2826 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00002827 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002828 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002829 return -EINVAL;
2830 }
2831
2832 obj_priv->user_pin_count++;
2833 obj_priv->pin_filp = file_priv;
2834 if (obj_priv->user_pin_count == 1) {
2835 ret = i915_gem_object_pin(obj, args->alignment);
2836 if (ret != 0) {
2837 drm_gem_object_unreference(obj);
2838 mutex_unlock(&dev->struct_mutex);
2839 return ret;
2840 }
Eric Anholt673a3942008-07-30 12:06:12 -07002841 }
2842
2843 /* XXX - flush the CPU caches for pinned objects
2844 * as the X server doesn't manage domains yet
2845 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002846 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002847 args->offset = obj_priv->gtt_offset;
2848 drm_gem_object_unreference(obj);
2849 mutex_unlock(&dev->struct_mutex);
2850
2851 return 0;
2852}
2853
2854int
2855i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
2856 struct drm_file *file_priv)
2857{
2858 struct drm_i915_gem_pin *args = data;
2859 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08002860 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002861
2862 mutex_lock(&dev->struct_mutex);
2863
2864 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2865 if (obj == NULL) {
2866 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
2867 args->handle);
2868 mutex_unlock(&dev->struct_mutex);
2869 return -EBADF;
2870 }
2871
Jesse Barnes79e53942008-11-07 14:24:08 -08002872 obj_priv = obj->driver_private;
2873 if (obj_priv->pin_filp != file_priv) {
2874 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
2875 args->handle);
2876 drm_gem_object_unreference(obj);
2877 mutex_unlock(&dev->struct_mutex);
2878 return -EINVAL;
2879 }
2880 obj_priv->user_pin_count--;
2881 if (obj_priv->user_pin_count == 0) {
2882 obj_priv->pin_filp = NULL;
2883 i915_gem_object_unpin(obj);
2884 }
Eric Anholt673a3942008-07-30 12:06:12 -07002885
2886 drm_gem_object_unreference(obj);
2887 mutex_unlock(&dev->struct_mutex);
2888 return 0;
2889}
2890
2891int
2892i915_gem_busy_ioctl(struct drm_device *dev, void *data,
2893 struct drm_file *file_priv)
2894{
2895 struct drm_i915_gem_busy *args = data;
2896 struct drm_gem_object *obj;
2897 struct drm_i915_gem_object *obj_priv;
2898
2899 mutex_lock(&dev->struct_mutex);
2900 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2901 if (obj == NULL) {
2902 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
2903 args->handle);
2904 mutex_unlock(&dev->struct_mutex);
2905 return -EBADF;
2906 }
2907
Eric Anholtf21289b2009-02-18 09:44:56 -08002908 /* Update the active list for the hardware's current position.
2909 * Otherwise this only updates on a delayed timer or when irqs are
2910 * actually unmasked, and our working set ends up being larger than
2911 * required.
2912 */
2913 i915_gem_retire_requests(dev);
2914
Eric Anholt673a3942008-07-30 12:06:12 -07002915 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08002916 /* Don't count being on the flushing list against the object being
2917 * done. Otherwise, a buffer left on the flushing list but not getting
2918 * flushed (because nobody's flushing that domain) won't ever return
2919 * unbusy and get reused by libdrm's bo cache. The other expected
2920 * consumer of this interface, OpenGL's occlusion queries, also specs
2921 * that the objects get unbusy "eventually" without any interference.
2922 */
2923 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002924
2925 drm_gem_object_unreference(obj);
2926 mutex_unlock(&dev->struct_mutex);
2927 return 0;
2928}
2929
2930int
2931i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
2932 struct drm_file *file_priv)
2933{
2934 return i915_gem_ring_throttle(dev, file_priv);
2935}
2936
2937int i915_gem_init_object(struct drm_gem_object *obj)
2938{
2939 struct drm_i915_gem_object *obj_priv;
2940
2941 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
2942 if (obj_priv == NULL)
2943 return -ENOMEM;
2944
2945 /*
2946 * We've just allocated pages from the kernel,
2947 * so they've just been written by the CPU with
2948 * zeros. They'll need to be clflushed before we
2949 * use them with the GPU.
2950 */
2951 obj->write_domain = I915_GEM_DOMAIN_CPU;
2952 obj->read_domains = I915_GEM_DOMAIN_CPU;
2953
Keith Packardba1eb1d2008-10-14 19:55:10 -07002954 obj_priv->agp_type = AGP_USER_MEMORY;
2955
Eric Anholt673a3942008-07-30 12:06:12 -07002956 obj->driver_private = obj_priv;
2957 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002958 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07002959 INIT_LIST_HEAD(&obj_priv->list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002960
Eric Anholt673a3942008-07-30 12:06:12 -07002961 return 0;
2962}
2963
2964void i915_gem_free_object(struct drm_gem_object *obj)
2965{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002966 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002967 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2968
2969 while (obj_priv->pin_count > 0)
2970 i915_gem_object_unpin(obj);
2971
Dave Airlie71acb5e2008-12-30 20:31:46 +10002972 if (obj_priv->phys_obj)
2973 i915_gem_detach_phys_object(dev, obj);
2974
Eric Anholt673a3942008-07-30 12:06:12 -07002975 i915_gem_object_unbind(obj);
2976
Jesse Barnesab00b3e2009-02-11 14:01:46 -08002977 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002978
Eric Anholt673a3942008-07-30 12:06:12 -07002979 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
2980 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
2981}
2982
Eric Anholt673a3942008-07-30 12:06:12 -07002983/** Unbinds all objects that are on the given buffer list. */
2984static int
2985i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
2986{
2987 struct drm_gem_object *obj;
2988 struct drm_i915_gem_object *obj_priv;
2989 int ret;
2990
2991 while (!list_empty(head)) {
2992 obj_priv = list_first_entry(head,
2993 struct drm_i915_gem_object,
2994 list);
2995 obj = obj_priv->obj;
2996
2997 if (obj_priv->pin_count != 0) {
2998 DRM_ERROR("Pinned object in unbind list\n");
2999 mutex_unlock(&dev->struct_mutex);
3000 return -EINVAL;
3001 }
3002
3003 ret = i915_gem_object_unbind(obj);
3004 if (ret != 0) {
3005 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3006 ret);
3007 mutex_unlock(&dev->struct_mutex);
3008 return ret;
3009 }
3010 }
3011
3012
3013 return 0;
3014}
3015
Jesse Barnes5669fca2009-02-17 15:13:31 -08003016int
Eric Anholt673a3942008-07-30 12:06:12 -07003017i915_gem_idle(struct drm_device *dev)
3018{
3019 drm_i915_private_t *dev_priv = dev->dev_private;
3020 uint32_t seqno, cur_seqno, last_seqno;
3021 int stuck, ret;
3022
Keith Packard6dbe2772008-10-14 21:41:13 -07003023 mutex_lock(&dev->struct_mutex);
3024
3025 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3026 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003027 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003028 }
Eric Anholt673a3942008-07-30 12:06:12 -07003029
3030 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3031 * We need to replace this with a semaphore, or something.
3032 */
3033 dev_priv->mm.suspended = 1;
3034
Keith Packard6dbe2772008-10-14 21:41:13 -07003035 /* Cancel the retire work handler, wait for it to finish if running
3036 */
3037 mutex_unlock(&dev->struct_mutex);
3038 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3039 mutex_lock(&dev->struct_mutex);
3040
Eric Anholt673a3942008-07-30 12:06:12 -07003041 i915_kernel_lost_context(dev);
3042
3043 /* Flush the GPU along with all non-CPU write domains
3044 */
3045 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
3046 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
Jesse Barnesde151cf2008-11-12 10:03:55 -08003047 seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003048
3049 if (seqno == 0) {
3050 mutex_unlock(&dev->struct_mutex);
3051 return -ENOMEM;
3052 }
3053
3054 dev_priv->mm.waiting_gem_seqno = seqno;
3055 last_seqno = 0;
3056 stuck = 0;
3057 for (;;) {
3058 cur_seqno = i915_get_gem_seqno(dev);
3059 if (i915_seqno_passed(cur_seqno, seqno))
3060 break;
3061 if (last_seqno == cur_seqno) {
3062 if (stuck++ > 100) {
3063 DRM_ERROR("hardware wedged\n");
3064 dev_priv->mm.wedged = 1;
3065 DRM_WAKEUP(&dev_priv->irq_queue);
3066 break;
3067 }
3068 }
3069 msleep(10);
3070 last_seqno = cur_seqno;
3071 }
3072 dev_priv->mm.waiting_gem_seqno = 0;
3073
3074 i915_gem_retire_requests(dev);
3075
Eric Anholt28dfe522008-11-13 15:00:55 -08003076 if (!dev_priv->mm.wedged) {
3077 /* Active and flushing should now be empty as we've
3078 * waited for a sequence higher than any pending execbuffer
3079 */
3080 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3081 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3082 /* Request should now be empty as we've also waited
3083 * for the last request in the list
3084 */
3085 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3086 }
Eric Anholt673a3942008-07-30 12:06:12 -07003087
Eric Anholt28dfe522008-11-13 15:00:55 -08003088 /* Empty the active and flushing lists to inactive. If there's
3089 * anything left at this point, it means that we're wedged and
3090 * nothing good's going to happen by leaving them there. So strip
3091 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003092 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003093 while (!list_empty(&dev_priv->mm.active_list)) {
3094 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003095
Eric Anholt28dfe522008-11-13 15:00:55 -08003096 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3097 struct drm_i915_gem_object,
3098 list);
3099 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3100 i915_gem_object_move_to_inactive(obj_priv->obj);
3101 }
3102
3103 while (!list_empty(&dev_priv->mm.flushing_list)) {
3104 struct drm_i915_gem_object *obj_priv;
3105
Eric Anholt151903d2008-12-01 10:23:21 +10003106 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003107 struct drm_i915_gem_object,
3108 list);
3109 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3110 i915_gem_object_move_to_inactive(obj_priv->obj);
3111 }
3112
3113
3114 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003115 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003116 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003117 if (ret) {
3118 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003119 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003120 }
Eric Anholt673a3942008-07-30 12:06:12 -07003121
Keith Packard6dbe2772008-10-14 21:41:13 -07003122 i915_gem_cleanup_ringbuffer(dev);
3123 mutex_unlock(&dev->struct_mutex);
3124
Eric Anholt673a3942008-07-30 12:06:12 -07003125 return 0;
3126}
3127
3128static int
3129i915_gem_init_hws(struct drm_device *dev)
3130{
3131 drm_i915_private_t *dev_priv = dev->dev_private;
3132 struct drm_gem_object *obj;
3133 struct drm_i915_gem_object *obj_priv;
3134 int ret;
3135
3136 /* If we need a physical address for the status page, it's already
3137 * initialized at driver load time.
3138 */
3139 if (!I915_NEED_GFX_HWS(dev))
3140 return 0;
3141
3142 obj = drm_gem_object_alloc(dev, 4096);
3143 if (obj == NULL) {
3144 DRM_ERROR("Failed to allocate status page\n");
3145 return -ENOMEM;
3146 }
3147 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07003148 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07003149
3150 ret = i915_gem_object_pin(obj, 4096);
3151 if (ret != 0) {
3152 drm_gem_object_unreference(obj);
3153 return ret;
3154 }
3155
3156 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003157
Keith Packardba1eb1d2008-10-14 19:55:10 -07003158 dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
3159 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003160 DRM_ERROR("Failed to map status page.\n");
3161 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00003162 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003163 drm_gem_object_unreference(obj);
3164 return -EINVAL;
3165 }
3166 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003167 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
3168 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07003169 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07003170 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
3171
3172 return 0;
3173}
3174
Chris Wilson85a7bb92009-02-11 14:52:44 +00003175static void
3176i915_gem_cleanup_hws(struct drm_device *dev)
3177{
3178 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003179 struct drm_gem_object *obj;
3180 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00003181
3182 if (dev_priv->hws_obj == NULL)
3183 return;
3184
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003185 obj = dev_priv->hws_obj;
3186 obj_priv = obj->driver_private;
3187
Chris Wilson85a7bb92009-02-11 14:52:44 +00003188 kunmap(obj_priv->page_list[0]);
3189 i915_gem_object_unpin(obj);
3190 drm_gem_object_unreference(obj);
3191 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003192
Chris Wilson85a7bb92009-02-11 14:52:44 +00003193 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3194 dev_priv->hw_status_page = NULL;
3195
3196 /* Write high address into HWS_PGA when disabling. */
3197 I915_WRITE(HWS_PGA, 0x1ffff000);
3198}
3199
Jesse Barnes79e53942008-11-07 14:24:08 -08003200int
Eric Anholt673a3942008-07-30 12:06:12 -07003201i915_gem_init_ringbuffer(struct drm_device *dev)
3202{
3203 drm_i915_private_t *dev_priv = dev->dev_private;
3204 struct drm_gem_object *obj;
3205 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08003206 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07003207 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07003208 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07003209
3210 ret = i915_gem_init_hws(dev);
3211 if (ret != 0)
3212 return ret;
3213
3214 obj = drm_gem_object_alloc(dev, 128 * 1024);
3215 if (obj == NULL) {
3216 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00003217 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003218 return -ENOMEM;
3219 }
3220 obj_priv = obj->driver_private;
3221
3222 ret = i915_gem_object_pin(obj, 4096);
3223 if (ret != 0) {
3224 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00003225 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003226 return ret;
3227 }
3228
3229 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08003230 ring->Size = obj->size;
3231 ring->tail_mask = obj->size - 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003232
Jesse Barnes79e53942008-11-07 14:24:08 -08003233 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
3234 ring->map.size = obj->size;
3235 ring->map.type = 0;
3236 ring->map.flags = 0;
3237 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003238
Jesse Barnes79e53942008-11-07 14:24:08 -08003239 drm_core_ioremap_wc(&ring->map, dev);
3240 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003241 DRM_ERROR("Failed to map ringbuffer.\n");
3242 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00003243 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003244 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00003245 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003246 return -EINVAL;
3247 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003248 ring->ring_obj = obj;
3249 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07003250
3251 /* Stop the ring if it's running. */
3252 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003253 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07003254 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003255
3256 /* Initialize the ring. */
3257 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07003258 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3259
3260 /* G45 ring initialization fails to reset head to zero */
3261 if (head != 0) {
3262 DRM_ERROR("Ring head not reset to zero "
3263 "ctl %08x head %08x tail %08x start %08x\n",
3264 I915_READ(PRB0_CTL),
3265 I915_READ(PRB0_HEAD),
3266 I915_READ(PRB0_TAIL),
3267 I915_READ(PRB0_START));
3268 I915_WRITE(PRB0_HEAD, 0);
3269
3270 DRM_ERROR("Ring head forced to zero "
3271 "ctl %08x head %08x tail %08x start %08x\n",
3272 I915_READ(PRB0_CTL),
3273 I915_READ(PRB0_HEAD),
3274 I915_READ(PRB0_TAIL),
3275 I915_READ(PRB0_START));
3276 }
3277
Eric Anholt673a3942008-07-30 12:06:12 -07003278 I915_WRITE(PRB0_CTL,
3279 ((obj->size - 4096) & RING_NR_PAGES) |
3280 RING_NO_REPORT |
3281 RING_VALID);
3282
Keith Packard50aa253d2008-10-14 17:20:35 -07003283 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3284
3285 /* If the head is still not zero, the ring is dead */
3286 if (head != 0) {
3287 DRM_ERROR("Ring initialization failed "
3288 "ctl %08x head %08x tail %08x start %08x\n",
3289 I915_READ(PRB0_CTL),
3290 I915_READ(PRB0_HEAD),
3291 I915_READ(PRB0_TAIL),
3292 I915_READ(PRB0_START));
3293 return -EIO;
3294 }
3295
Eric Anholt673a3942008-07-30 12:06:12 -07003296 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08003297 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3298 i915_kernel_lost_context(dev);
3299 else {
3300 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3301 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
3302 ring->space = ring->head - (ring->tail + 8);
3303 if (ring->space < 0)
3304 ring->space += ring->Size;
3305 }
Eric Anholt673a3942008-07-30 12:06:12 -07003306
3307 return 0;
3308}
3309
Jesse Barnes79e53942008-11-07 14:24:08 -08003310void
Eric Anholt673a3942008-07-30 12:06:12 -07003311i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3312{
3313 drm_i915_private_t *dev_priv = dev->dev_private;
3314
3315 if (dev_priv->ring.ring_obj == NULL)
3316 return;
3317
3318 drm_core_ioremapfree(&dev_priv->ring.map, dev);
3319
3320 i915_gem_object_unpin(dev_priv->ring.ring_obj);
3321 drm_gem_object_unreference(dev_priv->ring.ring_obj);
3322 dev_priv->ring.ring_obj = NULL;
3323 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3324
Chris Wilson85a7bb92009-02-11 14:52:44 +00003325 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003326}
3327
3328int
3329i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3330 struct drm_file *file_priv)
3331{
3332 drm_i915_private_t *dev_priv = dev->dev_private;
3333 int ret;
3334
Jesse Barnes79e53942008-11-07 14:24:08 -08003335 if (drm_core_check_feature(dev, DRIVER_MODESET))
3336 return 0;
3337
Eric Anholt673a3942008-07-30 12:06:12 -07003338 if (dev_priv->mm.wedged) {
3339 DRM_ERROR("Reenabling wedged hardware, good luck\n");
3340 dev_priv->mm.wedged = 0;
3341 }
3342
Eric Anholt673a3942008-07-30 12:06:12 -07003343 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003344 dev_priv->mm.suspended = 0;
3345
3346 ret = i915_gem_init_ringbuffer(dev);
3347 if (ret != 0)
3348 return ret;
3349
Eric Anholt673a3942008-07-30 12:06:12 -07003350 BUG_ON(!list_empty(&dev_priv->mm.active_list));
3351 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3352 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3353 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003354 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003355
3356 drm_irq_install(dev);
3357
Eric Anholt673a3942008-07-30 12:06:12 -07003358 return 0;
3359}
3360
3361int
3362i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3363 struct drm_file *file_priv)
3364{
3365 int ret;
3366
Jesse Barnes79e53942008-11-07 14:24:08 -08003367 if (drm_core_check_feature(dev, DRIVER_MODESET))
3368 return 0;
3369
Eric Anholt673a3942008-07-30 12:06:12 -07003370 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003371 drm_irq_uninstall(dev);
3372
Keith Packard6dbe2772008-10-14 21:41:13 -07003373 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003374}
3375
3376void
3377i915_gem_lastclose(struct drm_device *dev)
3378{
3379 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003380
Eric Anholte806b492009-01-22 09:56:58 -08003381 if (drm_core_check_feature(dev, DRIVER_MODESET))
3382 return;
3383
Keith Packard6dbe2772008-10-14 21:41:13 -07003384 ret = i915_gem_idle(dev);
3385 if (ret)
3386 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003387}
3388
3389void
3390i915_gem_load(struct drm_device *dev)
3391{
3392 drm_i915_private_t *dev_priv = dev->dev_private;
3393
3394 INIT_LIST_HEAD(&dev_priv->mm.active_list);
3395 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3396 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3397 INIT_LIST_HEAD(&dev_priv->mm.request_list);
3398 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3399 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07003400 dev_priv->mm.next_gem_seqno = 1;
3401
Jesse Barnesde151cf2008-11-12 10:03:55 -08003402 /* Old X drivers will take 0-2 for front, back, depth buffers */
3403 dev_priv->fence_reg_start = 3;
3404
Jesse Barnes0f973f22009-01-26 17:10:45 -08003405 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003406 dev_priv->num_fence_regs = 16;
3407 else
3408 dev_priv->num_fence_regs = 8;
3409
Eric Anholt673a3942008-07-30 12:06:12 -07003410 i915_gem_detect_bit_6_swizzle(dev);
3411}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003412
3413/*
3414 * Create a physically contiguous memory object for this object
3415 * e.g. for cursor + overlay regs
3416 */
3417int i915_gem_init_phys_object(struct drm_device *dev,
3418 int id, int size)
3419{
3420 drm_i915_private_t *dev_priv = dev->dev_private;
3421 struct drm_i915_gem_phys_object *phys_obj;
3422 int ret;
3423
3424 if (dev_priv->mm.phys_objs[id - 1] || !size)
3425 return 0;
3426
3427 phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
3428 if (!phys_obj)
3429 return -ENOMEM;
3430
3431 phys_obj->id = id;
3432
3433 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
3434 if (!phys_obj->handle) {
3435 ret = -ENOMEM;
3436 goto kfree_obj;
3437 }
3438#ifdef CONFIG_X86
3439 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3440#endif
3441
3442 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3443
3444 return 0;
3445kfree_obj:
3446 drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
3447 return ret;
3448}
3449
3450void i915_gem_free_phys_object(struct drm_device *dev, int id)
3451{
3452 drm_i915_private_t *dev_priv = dev->dev_private;
3453 struct drm_i915_gem_phys_object *phys_obj;
3454
3455 if (!dev_priv->mm.phys_objs[id - 1])
3456 return;
3457
3458 phys_obj = dev_priv->mm.phys_objs[id - 1];
3459 if (phys_obj->cur_obj) {
3460 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3461 }
3462
3463#ifdef CONFIG_X86
3464 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3465#endif
3466 drm_pci_free(dev, phys_obj->handle);
3467 kfree(phys_obj);
3468 dev_priv->mm.phys_objs[id - 1] = NULL;
3469}
3470
3471void i915_gem_free_all_phys_object(struct drm_device *dev)
3472{
3473 int i;
3474
Dave Airlie260883c2009-01-22 17:58:49 +10003475 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003476 i915_gem_free_phys_object(dev, i);
3477}
3478
3479void i915_gem_detach_phys_object(struct drm_device *dev,
3480 struct drm_gem_object *obj)
3481{
3482 struct drm_i915_gem_object *obj_priv;
3483 int i;
3484 int ret;
3485 int page_count;
3486
3487 obj_priv = obj->driver_private;
3488 if (!obj_priv->phys_obj)
3489 return;
3490
3491 ret = i915_gem_object_get_page_list(obj);
3492 if (ret)
3493 goto out;
3494
3495 page_count = obj->size / PAGE_SIZE;
3496
3497 for (i = 0; i < page_count; i++) {
3498 char *dst = kmap_atomic(obj_priv->page_list[i], KM_USER0);
3499 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
3500
3501 memcpy(dst, src, PAGE_SIZE);
3502 kunmap_atomic(dst, KM_USER0);
3503 }
3504 drm_clflush_pages(obj_priv->page_list, page_count);
3505 drm_agp_chipset_flush(dev);
3506out:
3507 obj_priv->phys_obj->cur_obj = NULL;
3508 obj_priv->phys_obj = NULL;
3509}
3510
3511int
3512i915_gem_attach_phys_object(struct drm_device *dev,
3513 struct drm_gem_object *obj, int id)
3514{
3515 drm_i915_private_t *dev_priv = dev->dev_private;
3516 struct drm_i915_gem_object *obj_priv;
3517 int ret = 0;
3518 int page_count;
3519 int i;
3520
3521 if (id > I915_MAX_PHYS_OBJECT)
3522 return -EINVAL;
3523
3524 obj_priv = obj->driver_private;
3525
3526 if (obj_priv->phys_obj) {
3527 if (obj_priv->phys_obj->id == id)
3528 return 0;
3529 i915_gem_detach_phys_object(dev, obj);
3530 }
3531
3532
3533 /* create a new object */
3534 if (!dev_priv->mm.phys_objs[id - 1]) {
3535 ret = i915_gem_init_phys_object(dev, id,
3536 obj->size);
3537 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08003538 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003539 goto out;
3540 }
3541 }
3542
3543 /* bind to the object */
3544 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
3545 obj_priv->phys_obj->cur_obj = obj;
3546
3547 ret = i915_gem_object_get_page_list(obj);
3548 if (ret) {
3549 DRM_ERROR("failed to get page list\n");
3550 goto out;
3551 }
3552
3553 page_count = obj->size / PAGE_SIZE;
3554
3555 for (i = 0; i < page_count; i++) {
3556 char *src = kmap_atomic(obj_priv->page_list[i], KM_USER0);
3557 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
3558
3559 memcpy(dst, src, PAGE_SIZE);
3560 kunmap_atomic(src, KM_USER0);
3561 }
3562
3563 return 0;
3564out:
3565 return ret;
3566}
3567
3568static int
3569i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
3570 struct drm_i915_gem_pwrite *args,
3571 struct drm_file *file_priv)
3572{
3573 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3574 void *obj_addr;
3575 int ret;
3576 char __user *user_data;
3577
3578 user_data = (char __user *) (uintptr_t) args->data_ptr;
3579 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
3580
Dave Airliee08fb4f2009-02-25 14:52:30 +10003581 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003582 ret = copy_from_user(obj_addr, user_data, args->size);
3583 if (ret)
3584 return -EFAULT;
3585
3586 drm_agp_chipset_flush(dev);
3587 return 0;
3588}