]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/gpu/drm/i915/i915_gem.c
drm/i915: Remove IMR masking during interrupt handler, and restart it if needed.
[net-next-2.6.git] / drivers / gpu / drm / i915 / i915_gem.c
CommitLineData
673a394b
EA
1/*
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>
33
34static int
35i915_gem_object_set_domain(struct drm_gem_object *obj,
36 uint32_t read_domains,
37 uint32_t write_domain);
38static int
39i915_gem_object_set_domain_range(struct drm_gem_object *obj,
40 uint64_t offset,
41 uint64_t size,
42 uint32_t read_domains,
43 uint32_t write_domain);
44static int
45i915_gem_set_domain(struct drm_gem_object *obj,
46 struct drm_file *file_priv,
47 uint32_t read_domains,
48 uint32_t write_domain);
49static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
50static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
51static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
52
6dbe2772
KP
53static void
54i915_gem_cleanup_ringbuffer(struct drm_device *dev);
55
673a394b
EA
56int
57i915_gem_init_ioctl(struct drm_device *dev, void *data,
58 struct drm_file *file_priv)
59{
60 drm_i915_private_t *dev_priv = dev->dev_private;
61 struct drm_i915_gem_init *args = data;
62
63 mutex_lock(&dev->struct_mutex);
64
65 if (args->gtt_start >= args->gtt_end ||
66 (args->gtt_start & (PAGE_SIZE - 1)) != 0 ||
67 (args->gtt_end & (PAGE_SIZE - 1)) != 0) {
68 mutex_unlock(&dev->struct_mutex);
69 return -EINVAL;
70 }
71
72 drm_mm_init(&dev_priv->mm.gtt_space, args->gtt_start,
73 args->gtt_end - args->gtt_start);
74
75 dev->gtt_total = (uint32_t) (args->gtt_end - args->gtt_start);
76
77 mutex_unlock(&dev->struct_mutex);
78
79 return 0;
80}
81
5a125c3c
EA
82int
83i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
84 struct drm_file *file_priv)
85{
86 drm_i915_private_t *dev_priv = dev->dev_private;
87 struct drm_i915_gem_get_aperture *args = data;
88 struct drm_i915_gem_object *obj_priv;
89
90 if (!(dev->driver->driver_features & DRIVER_GEM))
91 return -ENODEV;
92
93 args->aper_size = dev->gtt_total;
94 args->aper_available_size = args->aper_size;
95
96 list_for_each_entry(obj_priv, &dev_priv->mm.active_list, list) {
97 if (obj_priv->pin_count > 0)
98 args->aper_available_size -= obj_priv->obj->size;
99 }
100
101 return 0;
102}
103
673a394b
EA
104
105/**
106 * Creates a new mm object and returns a handle to it.
107 */
108int
109i915_gem_create_ioctl(struct drm_device *dev, void *data,
110 struct drm_file *file_priv)
111{
112 struct drm_i915_gem_create *args = data;
113 struct drm_gem_object *obj;
114 int handle, ret;
115
116 args->size = roundup(args->size, PAGE_SIZE);
117
118 /* Allocate the new object */
119 obj = drm_gem_object_alloc(dev, args->size);
120 if (obj == NULL)
121 return -ENOMEM;
122
123 ret = drm_gem_handle_create(file_priv, obj, &handle);
124 mutex_lock(&dev->struct_mutex);
125 drm_gem_object_handle_unreference(obj);
126 mutex_unlock(&dev->struct_mutex);
127
128 if (ret)
129 return ret;
130
131 args->handle = handle;
132
133 return 0;
134}
135
136/**
137 * Reads data from the object referenced by handle.
138 *
139 * On error, the contents of *data are undefined.
140 */
141int
142i915_gem_pread_ioctl(struct drm_device *dev, void *data,
143 struct drm_file *file_priv)
144{
145 struct drm_i915_gem_pread *args = data;
146 struct drm_gem_object *obj;
147 struct drm_i915_gem_object *obj_priv;
148 ssize_t read;
149 loff_t offset;
150 int ret;
151
152 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
153 if (obj == NULL)
154 return -EBADF;
155 obj_priv = obj->driver_private;
156
157 /* Bounds check source.
158 *
159 * XXX: This could use review for overflow issues...
160 */
161 if (args->offset > obj->size || args->size > obj->size ||
162 args->offset + args->size > obj->size) {
163 drm_gem_object_unreference(obj);
164 return -EINVAL;
165 }
166
167 mutex_lock(&dev->struct_mutex);
168
169 ret = i915_gem_object_set_domain_range(obj, args->offset, args->size,
170 I915_GEM_DOMAIN_CPU, 0);
171 if (ret != 0) {
172 drm_gem_object_unreference(obj);
173 mutex_unlock(&dev->struct_mutex);
e7d22bc3 174 return ret;
673a394b
EA
175 }
176
177 offset = args->offset;
178
179 read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
180 args->size, &offset);
181 if (read != args->size) {
182 drm_gem_object_unreference(obj);
183 mutex_unlock(&dev->struct_mutex);
184 if (read < 0)
185 return read;
186 else
187 return -EINVAL;
188 }
189
190 drm_gem_object_unreference(obj);
191 mutex_unlock(&dev->struct_mutex);
192
193 return 0;
194}
195
0839ccb8
KP
196/* This is the fast write path which cannot handle
197 * page faults in the source data
9b7530cc 198 */
0839ccb8
KP
199
200static inline int
201fast_user_write(struct io_mapping *mapping,
202 loff_t page_base, int page_offset,
203 char __user *user_data,
204 int length)
9b7530cc 205{
9b7530cc 206 char *vaddr_atomic;
0839ccb8 207 unsigned long unwritten;
9b7530cc 208
0839ccb8
KP
209 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
210 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
211 user_data, length);
212 io_mapping_unmap_atomic(vaddr_atomic);
213 if (unwritten)
214 return -EFAULT;
215 return 0;
216}
217
218/* Here's the write path which can sleep for
219 * page faults
220 */
221
222static inline int
223slow_user_write(struct io_mapping *mapping,
224 loff_t page_base, int page_offset,
225 char __user *user_data,
226 int length)
227{
228 char __iomem *vaddr;
229 unsigned long unwritten;
230
231 vaddr = io_mapping_map_wc(mapping, page_base);
232 if (vaddr == NULL)
233 return -EFAULT;
234 unwritten = __copy_from_user(vaddr + page_offset,
235 user_data, length);
236 io_mapping_unmap(vaddr);
237 if (unwritten)
238 return -EFAULT;
9b7530cc 239 return 0;
9b7530cc
LT
240}
241
673a394b
EA
242static int
243i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
244 struct drm_i915_gem_pwrite *args,
245 struct drm_file *file_priv)
246{
247 struct drm_i915_gem_object *obj_priv = obj->driver_private;
0839ccb8 248 drm_i915_private_t *dev_priv = dev->dev_private;
673a394b 249 ssize_t remain;
0839ccb8 250 loff_t offset, page_base;
673a394b 251 char __user *user_data;
0839ccb8
KP
252 int page_offset, page_length;
253 int ret;
673a394b
EA
254
255 user_data = (char __user *) (uintptr_t) args->data_ptr;
256 remain = args->size;
257 if (!access_ok(VERIFY_READ, user_data, remain))
258 return -EFAULT;
259
260
261 mutex_lock(&dev->struct_mutex);
262 ret = i915_gem_object_pin(obj, 0);
263 if (ret) {
264 mutex_unlock(&dev->struct_mutex);
265 return ret;
266 }
267 ret = i915_gem_set_domain(obj, file_priv,
268 I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
269 if (ret)
270 goto fail;
271
272 obj_priv = obj->driver_private;
273 offset = obj_priv->gtt_offset + args->offset;
274 obj_priv->dirty = 1;
275
276 while (remain > 0) {
277 /* Operation in this page
278 *
0839ccb8
KP
279 * page_base = page offset within aperture
280 * page_offset = offset within page
281 * page_length = bytes to copy for this page
673a394b 282 */
0839ccb8
KP
283 page_base = (offset & ~(PAGE_SIZE-1));
284 page_offset = offset & (PAGE_SIZE-1);
285 page_length = remain;
286 if ((page_offset + remain) > PAGE_SIZE)
287 page_length = PAGE_SIZE - page_offset;
288
289 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
290 page_offset, user_data, page_length);
291
292 /* If we get a fault while copying data, then (presumably) our
293 * source page isn't available. In this case, use the
294 * non-atomic function
295 */
296 if (ret) {
297 ret = slow_user_write (dev_priv->mm.gtt_mapping,
298 page_base, page_offset,
299 user_data, page_length);
300 if (ret)
673a394b 301 goto fail;
673a394b
EA
302 }
303
0839ccb8
KP
304 remain -= page_length;
305 user_data += page_length;
306 offset += page_length;
673a394b 307 }
673a394b
EA
308
309fail:
310 i915_gem_object_unpin(obj);
311 mutex_unlock(&dev->struct_mutex);
312
313 return ret;
314}
315
3043c60c 316static int
673a394b
EA
317i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
318 struct drm_i915_gem_pwrite *args,
319 struct drm_file *file_priv)
320{
321 int ret;
322 loff_t offset;
323 ssize_t written;
324
325 mutex_lock(&dev->struct_mutex);
326
327 ret = i915_gem_set_domain(obj, file_priv,
328 I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
329 if (ret) {
330 mutex_unlock(&dev->struct_mutex);
331 return ret;
332 }
333
334 offset = args->offset;
335
336 written = vfs_write(obj->filp,
337 (char __user *)(uintptr_t) args->data_ptr,
338 args->size, &offset);
339 if (written != args->size) {
340 mutex_unlock(&dev->struct_mutex);
341 if (written < 0)
342 return written;
343 else
344 return -EINVAL;
345 }
346
347 mutex_unlock(&dev->struct_mutex);
348
349 return 0;
350}
351
352/**
353 * Writes data to the object referenced by handle.
354 *
355 * On error, the contents of the buffer that were to be modified are undefined.
356 */
357int
358i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
359 struct drm_file *file_priv)
360{
361 struct drm_i915_gem_pwrite *args = data;
362 struct drm_gem_object *obj;
363 struct drm_i915_gem_object *obj_priv;
364 int ret = 0;
365
366 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
367 if (obj == NULL)
368 return -EBADF;
369 obj_priv = obj->driver_private;
370
371 /* Bounds check destination.
372 *
373 * XXX: This could use review for overflow issues...
374 */
375 if (args->offset > obj->size || args->size > obj->size ||
376 args->offset + args->size > obj->size) {
377 drm_gem_object_unreference(obj);
378 return -EINVAL;
379 }
380
381 /* We can only do the GTT pwrite on untiled buffers, as otherwise
382 * it would end up going through the fenced access, and we'll get
383 * different detiling behavior between reading and writing.
384 * pread/pwrite currently are reading and writing from the CPU
385 * perspective, requiring manual detiling by the client.
386 */
387 if (obj_priv->tiling_mode == I915_TILING_NONE &&
388 dev->gtt_total != 0)
389 ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
390 else
391 ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
392
393#if WATCH_PWRITE
394 if (ret)
395 DRM_INFO("pwrite failed %d\n", ret);
396#endif
397
398 drm_gem_object_unreference(obj);
399
400 return ret;
401}
402
403/**
404 * Called when user space prepares to use an object
405 */
406int
407i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
408 struct drm_file *file_priv)
409{
410 struct drm_i915_gem_set_domain *args = data;
411 struct drm_gem_object *obj;
412 int ret;
413
414 if (!(dev->driver->driver_features & DRIVER_GEM))
415 return -ENODEV;
416
417 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
418 if (obj == NULL)
419 return -EBADF;
420
421 mutex_lock(&dev->struct_mutex);
422#if WATCH_BUF
423 DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
424 obj, obj->size, args->read_domains, args->write_domain);
425#endif
426 ret = i915_gem_set_domain(obj, file_priv,
427 args->read_domains, args->write_domain);
428 drm_gem_object_unreference(obj);
429 mutex_unlock(&dev->struct_mutex);
430 return ret;
431}
432
433/**
434 * Called when user space has done writes to this buffer
435 */
436int
437i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
438 struct drm_file *file_priv)
439{
440 struct drm_i915_gem_sw_finish *args = data;
441 struct drm_gem_object *obj;
442 struct drm_i915_gem_object *obj_priv;
443 int ret = 0;
444
445 if (!(dev->driver->driver_features & DRIVER_GEM))
446 return -ENODEV;
447
448 mutex_lock(&dev->struct_mutex);
449 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
450 if (obj == NULL) {
451 mutex_unlock(&dev->struct_mutex);
452 return -EBADF;
453 }
454
455#if WATCH_BUF
456 DRM_INFO("%s: sw_finish %d (%p %d)\n",
457 __func__, args->handle, obj, obj->size);
458#endif
459 obj_priv = obj->driver_private;
460
461 /* Pinned buffers may be scanout, so flush the cache */
462 if ((obj->write_domain & I915_GEM_DOMAIN_CPU) && obj_priv->pin_count) {
463 i915_gem_clflush_object(obj);
464 drm_agp_chipset_flush(dev);
465 }
466 drm_gem_object_unreference(obj);
467 mutex_unlock(&dev->struct_mutex);
468 return ret;
469}
470
471/**
472 * Maps the contents of an object, returning the address it is mapped
473 * into.
474 *
475 * While the mapping holds a reference on the contents of the object, it doesn't
476 * imply a ref on the object itself.
477 */
478int
479i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
480 struct drm_file *file_priv)
481{
482 struct drm_i915_gem_mmap *args = data;
483 struct drm_gem_object *obj;
484 loff_t offset;
485 unsigned long addr;
486
487 if (!(dev->driver->driver_features & DRIVER_GEM))
488 return -ENODEV;
489
490 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
491 if (obj == NULL)
492 return -EBADF;
493
494 offset = args->offset;
495
496 down_write(&current->mm->mmap_sem);
497 addr = do_mmap(obj->filp, 0, args->size,
498 PROT_READ | PROT_WRITE, MAP_SHARED,
499 args->offset);
500 up_write(&current->mm->mmap_sem);
501 mutex_lock(&dev->struct_mutex);
502 drm_gem_object_unreference(obj);
503 mutex_unlock(&dev->struct_mutex);
504 if (IS_ERR((void *)addr))
505 return addr;
506
507 args->addr_ptr = (uint64_t) addr;
508
509 return 0;
510}
511
512static void
513i915_gem_object_free_page_list(struct drm_gem_object *obj)
514{
515 struct drm_i915_gem_object *obj_priv = obj->driver_private;
516 int page_count = obj->size / PAGE_SIZE;
517 int i;
518
519 if (obj_priv->page_list == NULL)
520 return;
521
522
523 for (i = 0; i < page_count; i++)
524 if (obj_priv->page_list[i] != NULL) {
525 if (obj_priv->dirty)
526 set_page_dirty(obj_priv->page_list[i]);
527 mark_page_accessed(obj_priv->page_list[i]);
528 page_cache_release(obj_priv->page_list[i]);
529 }
530 obj_priv->dirty = 0;
531
532 drm_free(obj_priv->page_list,
533 page_count * sizeof(struct page *),
534 DRM_MEM_DRIVER);
535 obj_priv->page_list = NULL;
536}
537
538static void
539i915_gem_object_move_to_active(struct drm_gem_object *obj)
540{
541 struct drm_device *dev = obj->dev;
542 drm_i915_private_t *dev_priv = dev->dev_private;
543 struct drm_i915_gem_object *obj_priv = obj->driver_private;
544
545 /* Add a reference if we're newly entering the active list. */
546 if (!obj_priv->active) {
547 drm_gem_object_reference(obj);
548 obj_priv->active = 1;
549 }
550 /* Move from whatever list we were on to the tail of execution. */
551 list_move_tail(&obj_priv->list,
552 &dev_priv->mm.active_list);
553}
554
555
556static void
557i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
558{
559 struct drm_device *dev = obj->dev;
560 drm_i915_private_t *dev_priv = dev->dev_private;
561 struct drm_i915_gem_object *obj_priv = obj->driver_private;
562
563 i915_verify_inactive(dev, __FILE__, __LINE__);
564 if (obj_priv->pin_count != 0)
565 list_del_init(&obj_priv->list);
566 else
567 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
568
569 if (obj_priv->active) {
570 obj_priv->active = 0;
571 drm_gem_object_unreference(obj);
572 }
573 i915_verify_inactive(dev, __FILE__, __LINE__);
574}
575
576/**
577 * Creates a new sequence number, emitting a write of it to the status page
578 * plus an interrupt, which will trigger i915_user_interrupt_handler.
579 *
580 * Must be called with struct_lock held.
581 *
582 * Returned sequence numbers are nonzero on success.
583 */
584static uint32_t
585i915_add_request(struct drm_device *dev, uint32_t flush_domains)
586{
587 drm_i915_private_t *dev_priv = dev->dev_private;
588 struct drm_i915_gem_request *request;
589 uint32_t seqno;
590 int was_empty;
591 RING_LOCALS;
592
593 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
594 if (request == NULL)
595 return 0;
596
597 /* Grab the seqno we're going to make this request be, and bump the
598 * next (skipping 0 so it can be the reserved no-seqno value).
599 */
600 seqno = dev_priv->mm.next_gem_seqno;
601 dev_priv->mm.next_gem_seqno++;
602 if (dev_priv->mm.next_gem_seqno == 0)
603 dev_priv->mm.next_gem_seqno++;
604
605 BEGIN_LP_RING(4);
606 OUT_RING(MI_STORE_DWORD_INDEX);
607 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
608 OUT_RING(seqno);
609
610 OUT_RING(MI_USER_INTERRUPT);
611 ADVANCE_LP_RING();
612
613 DRM_DEBUG("%d\n", seqno);
614
615 request->seqno = seqno;
616 request->emitted_jiffies = jiffies;
617 request->flush_domains = flush_domains;
618 was_empty = list_empty(&dev_priv->mm.request_list);
619 list_add_tail(&request->list, &dev_priv->mm.request_list);
620
6dbe2772 621 if (was_empty && !dev_priv->mm.suspended)
673a394b
EA
622 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
623 return seqno;
624}
625
626/**
627 * Command execution barrier
628 *
629 * Ensures that all commands in the ring are finished
630 * before signalling the CPU
631 */
3043c60c 632static uint32_t
673a394b
EA
633i915_retire_commands(struct drm_device *dev)
634{
635 drm_i915_private_t *dev_priv = dev->dev_private;
636 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
637 uint32_t flush_domains = 0;
638 RING_LOCALS;
639
640 /* The sampler always gets flushed on i965 (sigh) */
641 if (IS_I965G(dev))
642 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
643 BEGIN_LP_RING(2);
644 OUT_RING(cmd);
645 OUT_RING(0); /* noop */
646 ADVANCE_LP_RING();
647 return flush_domains;
648}
649
650/**
651 * Moves buffers associated only with the given active seqno from the active
652 * to inactive list, potentially freeing them.
653 */
654static void
655i915_gem_retire_request(struct drm_device *dev,
656 struct drm_i915_gem_request *request)
657{
658 drm_i915_private_t *dev_priv = dev->dev_private;
659
660 /* Move any buffers on the active list that are no longer referenced
661 * by the ringbuffer to the flushing/inactive lists as appropriate.
662 */
663 while (!list_empty(&dev_priv->mm.active_list)) {
664 struct drm_gem_object *obj;
665 struct drm_i915_gem_object *obj_priv;
666
667 obj_priv = list_first_entry(&dev_priv->mm.active_list,
668 struct drm_i915_gem_object,
669 list);
670 obj = obj_priv->obj;
671
672 /* If the seqno being retired doesn't match the oldest in the
673 * list, then the oldest in the list must still be newer than
674 * this seqno.
675 */
676 if (obj_priv->last_rendering_seqno != request->seqno)
677 return;
678#if WATCH_LRU
679 DRM_INFO("%s: retire %d moves to inactive list %p\n",
680 __func__, request->seqno, obj);
681#endif
682
683 if (obj->write_domain != 0) {
684 list_move_tail(&obj_priv->list,
685 &dev_priv->mm.flushing_list);
686 } else {
687 i915_gem_object_move_to_inactive(obj);
688 }
689 }
690
691 if (request->flush_domains != 0) {
692 struct drm_i915_gem_object *obj_priv, *next;
693
694 /* Clear the write domain and activity from any buffers
695 * that are just waiting for a flush matching the one retired.
696 */
697 list_for_each_entry_safe(obj_priv, next,
698 &dev_priv->mm.flushing_list, list) {
699 struct drm_gem_object *obj = obj_priv->obj;
700
701 if (obj->write_domain & request->flush_domains) {
702 obj->write_domain = 0;
703 i915_gem_object_move_to_inactive(obj);
704 }
705 }
706
707 }
708}
709
710/**
711 * Returns true if seq1 is later than seq2.
712 */
713static int
714i915_seqno_passed(uint32_t seq1, uint32_t seq2)
715{
716 return (int32_t)(seq1 - seq2) >= 0;
717}
718
719uint32_t
720i915_get_gem_seqno(struct drm_device *dev)
721{
722 drm_i915_private_t *dev_priv = dev->dev_private;
723
724 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
725}
726
727/**
728 * This function clears the request list as sequence numbers are passed.
729 */
730void
731i915_gem_retire_requests(struct drm_device *dev)
732{
733 drm_i915_private_t *dev_priv = dev->dev_private;
734 uint32_t seqno;
735
736 seqno = i915_get_gem_seqno(dev);
737
738 while (!list_empty(&dev_priv->mm.request_list)) {
739 struct drm_i915_gem_request *request;
740 uint32_t retiring_seqno;
741
742 request = list_first_entry(&dev_priv->mm.request_list,
743 struct drm_i915_gem_request,
744 list);
745 retiring_seqno = request->seqno;
746
747 if (i915_seqno_passed(seqno, retiring_seqno) ||
748 dev_priv->mm.wedged) {
749 i915_gem_retire_request(dev, request);
750
751 list_del(&request->list);
752 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
753 } else
754 break;
755 }
756}
757
758void
759i915_gem_retire_work_handler(struct work_struct *work)
760{
761 drm_i915_private_t *dev_priv;
762 struct drm_device *dev;
763
764 dev_priv = container_of(work, drm_i915_private_t,
765 mm.retire_work.work);
766 dev = dev_priv->dev;
767
768 mutex_lock(&dev->struct_mutex);
769 i915_gem_retire_requests(dev);
6dbe2772
KP
770 if (!dev_priv->mm.suspended &&
771 !list_empty(&dev_priv->mm.request_list))
673a394b
EA
772 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
773 mutex_unlock(&dev->struct_mutex);
774}
775
776/**
777 * Waits for a sequence number to be signaled, and cleans up the
778 * request and object lists appropriately for that event.
779 */
3043c60c 780static int
673a394b
EA
781i915_wait_request(struct drm_device *dev, uint32_t seqno)
782{
783 drm_i915_private_t *dev_priv = dev->dev_private;
784 int ret = 0;
785
786 BUG_ON(seqno == 0);
787
788 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
789 dev_priv->mm.waiting_gem_seqno = seqno;
790 i915_user_irq_get(dev);
791 ret = wait_event_interruptible(dev_priv->irq_queue,
792 i915_seqno_passed(i915_get_gem_seqno(dev),
793 seqno) ||
794 dev_priv->mm.wedged);
795 i915_user_irq_put(dev);
796 dev_priv->mm.waiting_gem_seqno = 0;
797 }
798 if (dev_priv->mm.wedged)
799 ret = -EIO;
800
801 if (ret && ret != -ERESTARTSYS)
802 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
803 __func__, ret, seqno, i915_get_gem_seqno(dev));
804
805 /* Directly dispatch request retiring. While we have the work queue
806 * to handle this, the waiter on a request often wants an associated
807 * buffer to have made it to the inactive list, and we would need
808 * a separate wait queue to handle that.
809 */
810 if (ret == 0)
811 i915_gem_retire_requests(dev);
812
813 return ret;
814}
815
816static void
817i915_gem_flush(struct drm_device *dev,
818 uint32_t invalidate_domains,
819 uint32_t flush_domains)
820{
821 drm_i915_private_t *dev_priv = dev->dev_private;
822 uint32_t cmd;
823 RING_LOCALS;
824
825#if WATCH_EXEC
826 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
827 invalidate_domains, flush_domains);
828#endif
829
830 if (flush_domains & I915_GEM_DOMAIN_CPU)
831 drm_agp_chipset_flush(dev);
832
833 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
834 I915_GEM_DOMAIN_GTT)) {
835 /*
836 * read/write caches:
837 *
838 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
839 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
840 * also flushed at 2d versus 3d pipeline switches.
841 *
842 * read-only caches:
843 *
844 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
845 * MI_READ_FLUSH is set, and is always flushed on 965.
846 *
847 * I915_GEM_DOMAIN_COMMAND may not exist?
848 *
849 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
850 * invalidated when MI_EXE_FLUSH is set.
851 *
852 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
853 * invalidated with every MI_FLUSH.
854 *
855 * TLBs:
856 *
857 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
858 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
859 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
860 * are flushed at any MI_FLUSH.
861 */
862
863 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
864 if ((invalidate_domains|flush_domains) &
865 I915_GEM_DOMAIN_RENDER)
866 cmd &= ~MI_NO_WRITE_FLUSH;
867 if (!IS_I965G(dev)) {
868 /*
869 * On the 965, the sampler cache always gets flushed
870 * and this bit is reserved.
871 */
872 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
873 cmd |= MI_READ_FLUSH;
874 }
875 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
876 cmd |= MI_EXE_FLUSH;
877
878#if WATCH_EXEC
879 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
880#endif
881 BEGIN_LP_RING(2);
882 OUT_RING(cmd);
883 OUT_RING(0); /* noop */
884 ADVANCE_LP_RING();
885 }
886}
887
888/**
889 * Ensures that all rendering to the object has completed and the object is
890 * safe to unbind from the GTT or access from the CPU.
891 */
892static int
893i915_gem_object_wait_rendering(struct drm_gem_object *obj)
894{
895 struct drm_device *dev = obj->dev;
896 struct drm_i915_gem_object *obj_priv = obj->driver_private;
897 int ret;
898
899 /* If there are writes queued to the buffer, flush and
900 * create a new seqno to wait for.
901 */
902 if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) {
903 uint32_t write_domain = obj->write_domain;
904#if WATCH_BUF
905 DRM_INFO("%s: flushing object %p from write domain %08x\n",
906 __func__, obj, write_domain);
907#endif
908 i915_gem_flush(dev, 0, write_domain);
909
910 i915_gem_object_move_to_active(obj);
911 obj_priv->last_rendering_seqno = i915_add_request(dev,
912 write_domain);
913 BUG_ON(obj_priv->last_rendering_seqno == 0);
914#if WATCH_LRU
915 DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj);
916#endif
917 }
918
919 /* If there is rendering queued on the buffer being evicted, wait for
920 * it.
921 */
922 if (obj_priv->active) {
923#if WATCH_BUF
924 DRM_INFO("%s: object %p wait for seqno %08x\n",
925 __func__, obj, obj_priv->last_rendering_seqno);
926#endif
927 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
928 if (ret != 0)
929 return ret;
930 }
931
932 return 0;
933}
934
935/**
936 * Unbinds an object from the GTT aperture.
937 */
938static int
939i915_gem_object_unbind(struct drm_gem_object *obj)
940{
941 struct drm_device *dev = obj->dev;
942 struct drm_i915_gem_object *obj_priv = obj->driver_private;
943 int ret = 0;
944
945#if WATCH_BUF
946 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
947 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
948#endif
949 if (obj_priv->gtt_space == NULL)
950 return 0;
951
952 if (obj_priv->pin_count != 0) {
953 DRM_ERROR("Attempting to unbind pinned buffer\n");
954 return -EINVAL;
955 }
956
957 /* Wait for any rendering to complete
958 */
959 ret = i915_gem_object_wait_rendering(obj);
960 if (ret) {
961 DRM_ERROR("wait_rendering failed: %d\n", ret);
962 return ret;
963 }
964
965 /* Move the object to the CPU domain to ensure that
966 * any possible CPU writes while it's not in the GTT
967 * are flushed when we go to remap it. This will
968 * also ensure that all pending GPU writes are finished
969 * before we unbind.
970 */
971 ret = i915_gem_object_set_domain(obj, I915_GEM_DOMAIN_CPU,
972 I915_GEM_DOMAIN_CPU);
973 if (ret) {
974 DRM_ERROR("set_domain failed: %d\n", ret);
975 return ret;
976 }
977
978 if (obj_priv->agp_mem != NULL) {
979 drm_unbind_agp(obj_priv->agp_mem);
980 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
981 obj_priv->agp_mem = NULL;
982 }
983
984 BUG_ON(obj_priv->active);
985
986 i915_gem_object_free_page_list(obj);
987
988 if (obj_priv->gtt_space) {
989 atomic_dec(&dev->gtt_count);
990 atomic_sub(obj->size, &dev->gtt_memory);
991
992 drm_mm_put_block(obj_priv->gtt_space);
993 obj_priv->gtt_space = NULL;
994 }
995
996 /* Remove ourselves from the LRU list if present. */
997 if (!list_empty(&obj_priv->list))
998 list_del_init(&obj_priv->list);
999
1000 return 0;
1001}
1002
1003static int
1004i915_gem_evict_something(struct drm_device *dev)
1005{
1006 drm_i915_private_t *dev_priv = dev->dev_private;
1007 struct drm_gem_object *obj;
1008 struct drm_i915_gem_object *obj_priv;
1009 int ret = 0;
1010
1011 for (;;) {
1012 /* If there's an inactive buffer available now, grab it
1013 * and be done.
1014 */
1015 if (!list_empty(&dev_priv->mm.inactive_list)) {
1016 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1017 struct drm_i915_gem_object,
1018 list);
1019 obj = obj_priv->obj;
1020 BUG_ON(obj_priv->pin_count != 0);
1021#if WATCH_LRU
1022 DRM_INFO("%s: evicting %p\n", __func__, obj);
1023#endif
1024 BUG_ON(obj_priv->active);
1025
1026 /* Wait on the rendering and unbind the buffer. */
1027 ret = i915_gem_object_unbind(obj);
1028 break;
1029 }
1030
1031 /* If we didn't get anything, but the ring is still processing
1032 * things, wait for one of those things to finish and hopefully
1033 * leave us a buffer to evict.
1034 */
1035 if (!list_empty(&dev_priv->mm.request_list)) {
1036 struct drm_i915_gem_request *request;
1037
1038 request = list_first_entry(&dev_priv->mm.request_list,
1039 struct drm_i915_gem_request,
1040 list);
1041
1042 ret = i915_wait_request(dev, request->seqno);
1043 if (ret)
1044 break;
1045
1046 /* if waiting caused an object to become inactive,
1047 * then loop around and wait for it. Otherwise, we
1048 * assume that waiting freed and unbound something,
1049 * so there should now be some space in the GTT
1050 */
1051 if (!list_empty(&dev_priv->mm.inactive_list))
1052 continue;
1053 break;
1054 }
1055
1056 /* If we didn't have anything on the request list but there
1057 * are buffers awaiting a flush, emit one and try again.
1058 * When we wait on it, those buffers waiting for that flush
1059 * will get moved to inactive.
1060 */
1061 if (!list_empty(&dev_priv->mm.flushing_list)) {
1062 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1063 struct drm_i915_gem_object,
1064 list);
1065 obj = obj_priv->obj;
1066
1067 i915_gem_flush(dev,
1068 obj->write_domain,
1069 obj->write_domain);
1070 i915_add_request(dev, obj->write_domain);
1071
1072 obj = NULL;
1073 continue;
1074 }
1075
1076 DRM_ERROR("inactive empty %d request empty %d "
1077 "flushing empty %d\n",
1078 list_empty(&dev_priv->mm.inactive_list),
1079 list_empty(&dev_priv->mm.request_list),
1080 list_empty(&dev_priv->mm.flushing_list));
1081 /* If we didn't do any of the above, there's nothing to be done
1082 * and we just can't fit it in.
1083 */
1084 return -ENOMEM;
1085 }
1086 return ret;
1087}
1088
1089static int
1090i915_gem_object_get_page_list(struct drm_gem_object *obj)
1091{
1092 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1093 int page_count, i;
1094 struct address_space *mapping;
1095 struct inode *inode;
1096 struct page *page;
1097 int ret;
1098
1099 if (obj_priv->page_list)
1100 return 0;
1101
1102 /* Get the list of pages out of our struct file. They'll be pinned
1103 * at this point until we release them.
1104 */
1105 page_count = obj->size / PAGE_SIZE;
1106 BUG_ON(obj_priv->page_list != NULL);
1107 obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
1108 DRM_MEM_DRIVER);
1109 if (obj_priv->page_list == NULL) {
1110 DRM_ERROR("Faled to allocate page list\n");
1111 return -ENOMEM;
1112 }
1113
1114 inode = obj->filp->f_path.dentry->d_inode;
1115 mapping = inode->i_mapping;
1116 for (i = 0; i < page_count; i++) {
1117 page = read_mapping_page(mapping, i, NULL);
1118 if (IS_ERR(page)) {
1119 ret = PTR_ERR(page);
1120 DRM_ERROR("read_mapping_page failed: %d\n", ret);
1121 i915_gem_object_free_page_list(obj);
1122 return ret;
1123 }
1124 obj_priv->page_list[i] = page;
1125 }
1126 return 0;
1127}
1128
1129/**
1130 * Finds free space in the GTT aperture and binds the object there.
1131 */
1132static int
1133i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
1134{
1135 struct drm_device *dev = obj->dev;
1136 drm_i915_private_t *dev_priv = dev->dev_private;
1137 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1138 struct drm_mm_node *free_space;
1139 int page_count, ret;
1140
1141 if (alignment == 0)
1142 alignment = PAGE_SIZE;
1143 if (alignment & (PAGE_SIZE - 1)) {
1144 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
1145 return -EINVAL;
1146 }
1147
1148 search_free:
1149 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
1150 obj->size, alignment, 0);
1151 if (free_space != NULL) {
1152 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
1153 alignment);
1154 if (obj_priv->gtt_space != NULL) {
1155 obj_priv->gtt_space->private = obj;
1156 obj_priv->gtt_offset = obj_priv->gtt_space->start;
1157 }
1158 }
1159 if (obj_priv->gtt_space == NULL) {
1160 /* If the gtt is empty and we're still having trouble
1161 * fitting our object in, we're out of memory.
1162 */
1163#if WATCH_LRU
1164 DRM_INFO("%s: GTT full, evicting something\n", __func__);
1165#endif
1166 if (list_empty(&dev_priv->mm.inactive_list) &&
1167 list_empty(&dev_priv->mm.flushing_list) &&
1168 list_empty(&dev_priv->mm.active_list)) {
1169 DRM_ERROR("GTT full, but LRU list empty\n");
1170 return -ENOMEM;
1171 }
1172
1173 ret = i915_gem_evict_something(dev);
1174 if (ret != 0) {
1175 DRM_ERROR("Failed to evict a buffer %d\n", ret);
1176 return ret;
1177 }
1178 goto search_free;
1179 }
1180
1181#if WATCH_BUF
1182 DRM_INFO("Binding object of size %d at 0x%08x\n",
1183 obj->size, obj_priv->gtt_offset);
1184#endif
1185 ret = i915_gem_object_get_page_list(obj);
1186 if (ret) {
1187 drm_mm_put_block(obj_priv->gtt_space);
1188 obj_priv->gtt_space = NULL;
1189 return ret;
1190 }
1191
1192 page_count = obj->size / PAGE_SIZE;
1193 /* Create an AGP memory structure pointing at our pages, and bind it
1194 * into the GTT.
1195 */
1196 obj_priv->agp_mem = drm_agp_bind_pages(dev,
1197 obj_priv->page_list,
1198 page_count,
ba1eb1d8
KP
1199 obj_priv->gtt_offset,
1200 obj_priv->agp_type);
673a394b
EA
1201 if (obj_priv->agp_mem == NULL) {
1202 i915_gem_object_free_page_list(obj);
1203 drm_mm_put_block(obj_priv->gtt_space);
1204 obj_priv->gtt_space = NULL;
1205 return -ENOMEM;
1206 }
1207 atomic_inc(&dev->gtt_count);
1208 atomic_add(obj->size, &dev->gtt_memory);
1209
1210 /* Assert that the object is not currently in any GPU domain. As it
1211 * wasn't in the GTT, there shouldn't be any way it could have been in
1212 * a GPU cache
1213 */
1214 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1215 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1216
1217 return 0;
1218}
1219
1220void
1221i915_gem_clflush_object(struct drm_gem_object *obj)
1222{
1223 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1224
1225 /* If we don't have a page list set up, then we're not pinned
1226 * to GPU, and we can ignore the cache flush because it'll happen
1227 * again at bind time.
1228 */
1229 if (obj_priv->page_list == NULL)
1230 return;
1231
1232 drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
1233}
1234
1235/*
1236 * Set the next domain for the specified object. This
1237 * may not actually perform the necessary flushing/invaliding though,
1238 * as that may want to be batched with other set_domain operations
1239 *
1240 * This is (we hope) the only really tricky part of gem. The goal
1241 * is fairly simple -- track which caches hold bits of the object
1242 * and make sure they remain coherent. A few concrete examples may
1243 * help to explain how it works. For shorthand, we use the notation
1244 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
1245 * a pair of read and write domain masks.
1246 *
1247 * Case 1: the batch buffer
1248 *
1249 * 1. Allocated
1250 * 2. Written by CPU
1251 * 3. Mapped to GTT
1252 * 4. Read by GPU
1253 * 5. Unmapped from GTT
1254 * 6. Freed
1255 *
1256 * Let's take these a step at a time
1257 *
1258 * 1. Allocated
1259 * Pages allocated from the kernel may still have
1260 * cache contents, so we set them to (CPU, CPU) always.
1261 * 2. Written by CPU (using pwrite)
1262 * The pwrite function calls set_domain (CPU, CPU) and
1263 * this function does nothing (as nothing changes)
1264 * 3. Mapped by GTT
1265 * This function asserts that the object is not
1266 * currently in any GPU-based read or write domains
1267 * 4. Read by GPU
1268 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
1269 * As write_domain is zero, this function adds in the
1270 * current read domains (CPU+COMMAND, 0).
1271 * flush_domains is set to CPU.
1272 * invalidate_domains is set to COMMAND
1273 * clflush is run to get data out of the CPU caches
1274 * then i915_dev_set_domain calls i915_gem_flush to
1275 * emit an MI_FLUSH and drm_agp_chipset_flush
1276 * 5. Unmapped from GTT
1277 * i915_gem_object_unbind calls set_domain (CPU, CPU)
1278 * flush_domains and invalidate_domains end up both zero
1279 * so no flushing/invalidating happens
1280 * 6. Freed
1281 * yay, done
1282 *
1283 * Case 2: The shared render buffer
1284 *
1285 * 1. Allocated
1286 * 2. Mapped to GTT
1287 * 3. Read/written by GPU
1288 * 4. set_domain to (CPU,CPU)
1289 * 5. Read/written by CPU
1290 * 6. Read/written by GPU
1291 *
1292 * 1. Allocated
1293 * Same as last example, (CPU, CPU)
1294 * 2. Mapped to GTT
1295 * Nothing changes (assertions find that it is not in the GPU)
1296 * 3. Read/written by GPU
1297 * execbuffer calls set_domain (RENDER, RENDER)
1298 * flush_domains gets CPU
1299 * invalidate_domains gets GPU
1300 * clflush (obj)
1301 * MI_FLUSH and drm_agp_chipset_flush
1302 * 4. set_domain (CPU, CPU)
1303 * flush_domains gets GPU
1304 * invalidate_domains gets CPU
1305 * wait_rendering (obj) to make sure all drawing is complete.
1306 * This will include an MI_FLUSH to get the data from GPU
1307 * to memory
1308 * clflush (obj) to invalidate the CPU cache
1309 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
1310 * 5. Read/written by CPU
1311 * cache lines are loaded and dirtied
1312 * 6. Read written by GPU
1313 * Same as last GPU access
1314 *
1315 * Case 3: The constant buffer
1316 *
1317 * 1. Allocated
1318 * 2. Written by CPU
1319 * 3. Read by GPU
1320 * 4. Updated (written) by CPU again
1321 * 5. Read by GPU
1322 *
1323 * 1. Allocated
1324 * (CPU, CPU)
1325 * 2. Written by CPU
1326 * (CPU, CPU)
1327 * 3. Read by GPU
1328 * (CPU+RENDER, 0)
1329 * flush_domains = CPU
1330 * invalidate_domains = RENDER
1331 * clflush (obj)
1332 * MI_FLUSH
1333 * drm_agp_chipset_flush
1334 * 4. Updated (written) by CPU again
1335 * (CPU, CPU)
1336 * flush_domains = 0 (no previous write domain)
1337 * invalidate_domains = 0 (no new read domains)
1338 * 5. Read by GPU
1339 * (CPU+RENDER, 0)
1340 * flush_domains = CPU
1341 * invalidate_domains = RENDER
1342 * clflush (obj)
1343 * MI_FLUSH
1344 * drm_agp_chipset_flush
1345 */
1346static int
1347i915_gem_object_set_domain(struct drm_gem_object *obj,
1348 uint32_t read_domains,
1349 uint32_t write_domain)
1350{
1351 struct drm_device *dev = obj->dev;
1352 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1353 uint32_t invalidate_domains = 0;
1354 uint32_t flush_domains = 0;
1355 int ret;
1356
1357#if WATCH_BUF
1358 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
1359 __func__, obj,
1360 obj->read_domains, read_domains,
1361 obj->write_domain, write_domain);
1362#endif
1363 /*
1364 * If the object isn't moving to a new write domain,
1365 * let the object stay in multiple read domains
1366 */
1367 if (write_domain == 0)
1368 read_domains |= obj->read_domains;
1369 else
1370 obj_priv->dirty = 1;
1371
1372 /*
1373 * Flush the current write domain if
1374 * the new read domains don't match. Invalidate
1375 * any read domains which differ from the old
1376 * write domain
1377 */
1378 if (obj->write_domain && obj->write_domain != read_domains) {
1379 flush_domains |= obj->write_domain;
1380 invalidate_domains |= read_domains & ~obj->write_domain;
1381 }
1382 /*
1383 * Invalidate any read caches which may have
1384 * stale data. That is, any new read domains.
1385 */
1386 invalidate_domains |= read_domains & ~obj->read_domains;
1387 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
1388#if WATCH_BUF
1389 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
1390 __func__, flush_domains, invalidate_domains);
1391#endif
1392 /*
1393 * If we're invaliding the CPU cache and flushing a GPU cache,
1394 * then pause for rendering so that the GPU caches will be
1395 * flushed before the cpu cache is invalidated
1396 */
1397 if ((invalidate_domains & I915_GEM_DOMAIN_CPU) &&
1398 (flush_domains & ~(I915_GEM_DOMAIN_CPU |
1399 I915_GEM_DOMAIN_GTT))) {
1400 ret = i915_gem_object_wait_rendering(obj);
1401 if (ret)
1402 return ret;
1403 }
1404 i915_gem_clflush_object(obj);
1405 }
1406
1407 if ((write_domain | flush_domains) != 0)
1408 obj->write_domain = write_domain;
1409
1410 /* If we're invalidating the CPU domain, clear the per-page CPU
1411 * domain list as well.
1412 */
1413 if (obj_priv->page_cpu_valid != NULL &&
1414 (write_domain != 0 ||
1415 read_domains & I915_GEM_DOMAIN_CPU)) {
1416 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
1417 DRM_MEM_DRIVER);
1418 obj_priv->page_cpu_valid = NULL;
1419 }
1420 obj->read_domains = read_domains;
1421
1422 dev->invalidate_domains |= invalidate_domains;
1423 dev->flush_domains |= flush_domains;
1424#if WATCH_BUF
1425 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
1426 __func__,
1427 obj->read_domains, obj->write_domain,
1428 dev->invalidate_domains, dev->flush_domains);
1429#endif
1430 return 0;
1431}
1432
1433/**
1434 * Set the read/write domain on a range of the object.
1435 *
1436 * Currently only implemented for CPU reads, otherwise drops to normal
1437 * i915_gem_object_set_domain().
1438 */
1439static int
1440i915_gem_object_set_domain_range(struct drm_gem_object *obj,
1441 uint64_t offset,
1442 uint64_t size,
1443 uint32_t read_domains,
1444 uint32_t write_domain)
1445{
1446 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1447 int ret, i;
1448
1449 if (obj->read_domains & I915_GEM_DOMAIN_CPU)
1450 return 0;
1451
1452 if (read_domains != I915_GEM_DOMAIN_CPU ||
1453 write_domain != 0)
1454 return i915_gem_object_set_domain(obj,
1455 read_domains, write_domain);
1456
1457 /* Wait on any GPU rendering to the object to be flushed. */
6a47baa6
OT
1458 ret = i915_gem_object_wait_rendering(obj);
1459 if (ret)
1460 return ret;
673a394b
EA
1461
1462 if (obj_priv->page_cpu_valid == NULL) {
1463 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
1464 DRM_MEM_DRIVER);
1465 }
1466
1467 /* Flush the cache on any pages that are still invalid from the CPU's
1468 * perspective.
1469 */
1470 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE; i++) {
1471 if (obj_priv->page_cpu_valid[i])
1472 continue;
1473
1474 drm_clflush_pages(obj_priv->page_list + i, 1);
1475
1476 obj_priv->page_cpu_valid[i] = 1;
1477 }
1478
1479 return 0;
1480}
1481
1482/**
1483 * Once all of the objects have been set in the proper domain,
1484 * perform the necessary flush and invalidate operations.
1485 *
1486 * Returns the write domains flushed, for use in flush tracking.
1487 */
1488static uint32_t
1489i915_gem_dev_set_domain(struct drm_device *dev)
1490{
1491 uint32_t flush_domains = dev->flush_domains;
1492
1493 /*
1494 * Now that all the buffers are synced to the proper domains,
1495 * flush and invalidate the collected domains
1496 */
1497 if (dev->invalidate_domains | dev->flush_domains) {
1498#if WATCH_EXEC
1499 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
1500 __func__,
1501 dev->invalidate_domains,
1502 dev->flush_domains);
1503#endif
1504 i915_gem_flush(dev,
1505 dev->invalidate_domains,
1506 dev->flush_domains);
1507 dev->invalidate_domains = 0;
1508 dev->flush_domains = 0;
1509 }
1510
1511 return flush_domains;
1512}
1513
1514/**
1515 * Pin an object to the GTT and evaluate the relocations landing in it.
1516 */
1517static int
1518i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
1519 struct drm_file *file_priv,
1520 struct drm_i915_gem_exec_object *entry)
1521{
1522 struct drm_device *dev = obj->dev;
0839ccb8 1523 drm_i915_private_t *dev_priv = dev->dev_private;
673a394b
EA
1524 struct drm_i915_gem_relocation_entry reloc;
1525 struct drm_i915_gem_relocation_entry __user *relocs;
1526 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1527 int i, ret;
0839ccb8 1528 void __iomem *reloc_page;
673a394b
EA
1529
1530 /* Choose the GTT offset for our buffer and put it there. */
1531 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
1532 if (ret)
1533 return ret;
1534
1535 entry->offset = obj_priv->gtt_offset;
1536
1537 relocs = (struct drm_i915_gem_relocation_entry __user *)
1538 (uintptr_t) entry->relocs_ptr;
1539 /* Apply the relocations, using the GTT aperture to avoid cache
1540 * flushing requirements.
1541 */
1542 for (i = 0; i < entry->relocation_count; i++) {
1543 struct drm_gem_object *target_obj;
1544 struct drm_i915_gem_object *target_obj_priv;
3043c60c
EA
1545 uint32_t reloc_val, reloc_offset;
1546 uint32_t __iomem *reloc_entry;
673a394b
EA
1547
1548 ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
1549 if (ret != 0) {
1550 i915_gem_object_unpin(obj);
1551 return ret;
1552 }
1553
1554 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
1555 reloc.target_handle);
1556 if (target_obj == NULL) {
1557 i915_gem_object_unpin(obj);
1558 return -EBADF;
1559 }
1560 target_obj_priv = target_obj->driver_private;
1561
1562 /* The target buffer should have appeared before us in the
1563 * exec_object list, so it should have a GTT space bound by now.
1564 */
1565 if (target_obj_priv->gtt_space == NULL) {
1566 DRM_ERROR("No GTT space found for object %d\n",
1567 reloc.target_handle);
1568 drm_gem_object_unreference(target_obj);
1569 i915_gem_object_unpin(obj);
1570 return -EINVAL;
1571 }
1572
1573 if (reloc.offset > obj->size - 4) {
1574 DRM_ERROR("Relocation beyond object bounds: "
1575 "obj %p target %d offset %d size %d.\n",
1576 obj, reloc.target_handle,
1577 (int) reloc.offset, (int) obj->size);
1578 drm_gem_object_unreference(target_obj);
1579 i915_gem_object_unpin(obj);
1580 return -EINVAL;
1581 }
1582 if (reloc.offset & 3) {
1583 DRM_ERROR("Relocation not 4-byte aligned: "
1584 "obj %p target %d offset %d.\n",
1585 obj, reloc.target_handle,
1586 (int) reloc.offset);
1587 drm_gem_object_unreference(target_obj);
1588 i915_gem_object_unpin(obj);
1589 return -EINVAL;
1590 }
1591
1592 if (reloc.write_domain && target_obj->pending_write_domain &&
1593 reloc.write_domain != target_obj->pending_write_domain) {
1594 DRM_ERROR("Write domain conflict: "
1595 "obj %p target %d offset %d "
1596 "new %08x old %08x\n",
1597 obj, reloc.target_handle,
1598 (int) reloc.offset,
1599 reloc.write_domain,
1600 target_obj->pending_write_domain);
1601 drm_gem_object_unreference(target_obj);
1602 i915_gem_object_unpin(obj);
1603 return -EINVAL;
1604 }
1605
1606#if WATCH_RELOC
1607 DRM_INFO("%s: obj %p offset %08x target %d "
1608 "read %08x write %08x gtt %08x "
1609 "presumed %08x delta %08x\n",
1610 __func__,
1611 obj,
1612 (int) reloc.offset,
1613 (int) reloc.target_handle,
1614 (int) reloc.read_domains,
1615 (int) reloc.write_domain,
1616 (int) target_obj_priv->gtt_offset,
1617 (int) reloc.presumed_offset,
1618 reloc.delta);
1619#endif
1620
1621 target_obj->pending_read_domains |= reloc.read_domains;
1622 target_obj->pending_write_domain |= reloc.write_domain;
1623
1624 /* If the relocation already has the right value in it, no
1625 * more work needs to be done.
1626 */
1627 if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
1628 drm_gem_object_unreference(target_obj);
1629 continue;
1630 }
1631
1632 /* Now that we're going to actually write some data in,
1633 * make sure that any rendering using this buffer's contents
1634 * is completed.
1635 */
1636 i915_gem_object_wait_rendering(obj);
1637
1638 /* As we're writing through the gtt, flush
1639 * any CPU writes before we write the relocations
1640 */
1641 if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
1642 i915_gem_clflush_object(obj);
1643 drm_agp_chipset_flush(dev);
1644 obj->write_domain = 0;
1645 }
1646
1647 /* Map the page containing the relocation we're going to
1648 * perform.
1649 */
1650 reloc_offset = obj_priv->gtt_offset + reloc.offset;
0839ccb8
KP
1651 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
1652 (reloc_offset &
1653 ~(PAGE_SIZE - 1)));
3043c60c 1654 reloc_entry = (uint32_t __iomem *)(reloc_page +
0839ccb8 1655 (reloc_offset & (PAGE_SIZE - 1)));
673a394b
EA
1656 reloc_val = target_obj_priv->gtt_offset + reloc.delta;
1657
1658#if WATCH_BUF
1659 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
1660 obj, (unsigned int) reloc.offset,
1661 readl(reloc_entry), reloc_val);
1662#endif
1663 writel(reloc_val, reloc_entry);
0839ccb8 1664 io_mapping_unmap_atomic(reloc_page);
673a394b
EA
1665
1666 /* Write the updated presumed offset for this entry back out
1667 * to the user.
1668 */
1669 reloc.presumed_offset = target_obj_priv->gtt_offset;
1670 ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
1671 if (ret != 0) {
1672 drm_gem_object_unreference(target_obj);
1673 i915_gem_object_unpin(obj);
1674 return ret;
1675 }
1676
1677 drm_gem_object_unreference(target_obj);
1678 }
1679
673a394b
EA
1680#if WATCH_BUF
1681 if (0)
1682 i915_gem_dump_object(obj, 128, __func__, ~0);
1683#endif
1684 return 0;
1685}
1686
1687/** Dispatch a batchbuffer to the ring
1688 */
1689static int
1690i915_dispatch_gem_execbuffer(struct drm_device *dev,
1691 struct drm_i915_gem_execbuffer *exec,
1692 uint64_t exec_offset)
1693{
1694 drm_i915_private_t *dev_priv = dev->dev_private;
1695 struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
1696 (uintptr_t) exec->cliprects_ptr;
1697 int nbox = exec->num_cliprects;
1698 int i = 0, count;
1699 uint32_t exec_start, exec_len;
1700 RING_LOCALS;
1701
1702 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
1703 exec_len = (uint32_t) exec->batch_len;
1704
1705 if ((exec_start | exec_len) & 0x7) {
1706 DRM_ERROR("alignment\n");
1707 return -EINVAL;
1708 }
1709
1710 if (!exec_start)
1711 return -EINVAL;
1712
1713 count = nbox ? nbox : 1;
1714
1715 for (i = 0; i < count; i++) {
1716 if (i < nbox) {
1717 int ret = i915_emit_box(dev, boxes, i,
1718 exec->DR1, exec->DR4);
1719 if (ret)
1720 return ret;
1721 }
1722
1723 if (IS_I830(dev) || IS_845G(dev)) {
1724 BEGIN_LP_RING(4);
1725 OUT_RING(MI_BATCH_BUFFER);
1726 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
1727 OUT_RING(exec_start + exec_len - 4);
1728 OUT_RING(0);
1729 ADVANCE_LP_RING();
1730 } else {
1731 BEGIN_LP_RING(2);
1732 if (IS_I965G(dev)) {
1733 OUT_RING(MI_BATCH_BUFFER_START |
1734 (2 << 6) |
1735 MI_BATCH_NON_SECURE_I965);
1736 OUT_RING(exec_start);
1737 } else {
1738 OUT_RING(MI_BATCH_BUFFER_START |
1739 (2 << 6));
1740 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
1741 }
1742 ADVANCE_LP_RING();
1743 }
1744 }
1745
1746 /* XXX breadcrumb */
1747 return 0;
1748}
1749
1750/* Throttle our rendering by waiting until the ring has completed our requests
1751 * emitted over 20 msec ago.
1752 *
1753 * This should get us reasonable parallelism between CPU and GPU but also
1754 * relatively low latency when blocking on a particular request to finish.
1755 */
1756static int
1757i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
1758{
1759 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1760 int ret = 0;
1761 uint32_t seqno;
1762
1763 mutex_lock(&dev->struct_mutex);
1764 seqno = i915_file_priv->mm.last_gem_throttle_seqno;
1765 i915_file_priv->mm.last_gem_throttle_seqno =
1766 i915_file_priv->mm.last_gem_seqno;
1767 if (seqno)
1768 ret = i915_wait_request(dev, seqno);
1769 mutex_unlock(&dev->struct_mutex);
1770 return ret;
1771}
1772
1773int
1774i915_gem_execbuffer(struct drm_device *dev, void *data,
1775 struct drm_file *file_priv)
1776{
1777 drm_i915_private_t *dev_priv = dev->dev_private;
1778 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1779 struct drm_i915_gem_execbuffer *args = data;
1780 struct drm_i915_gem_exec_object *exec_list = NULL;
1781 struct drm_gem_object **object_list = NULL;
1782 struct drm_gem_object *batch_obj;
1783 int ret, i, pinned = 0;
1784 uint64_t exec_offset;
1785 uint32_t seqno, flush_domains;
1786
1787#if WATCH_EXEC
1788 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
1789 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
1790#endif
1791
4f481ed2
EA
1792 if (args->buffer_count < 1) {
1793 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
1794 return -EINVAL;
1795 }
673a394b
EA
1796 /* Copy in the exec list from userland */
1797 exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
1798 DRM_MEM_DRIVER);
1799 object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
1800 DRM_MEM_DRIVER);
1801 if (exec_list == NULL || object_list == NULL) {
1802 DRM_ERROR("Failed to allocate exec or object list "
1803 "for %d buffers\n",
1804 args->buffer_count);
1805 ret = -ENOMEM;
1806 goto pre_mutex_err;
1807 }
1808 ret = copy_from_user(exec_list,
1809 (struct drm_i915_relocation_entry __user *)
1810 (uintptr_t) args->buffers_ptr,
1811 sizeof(*exec_list) * args->buffer_count);
1812 if (ret != 0) {
1813 DRM_ERROR("copy %d exec entries failed %d\n",
1814 args->buffer_count, ret);
1815 goto pre_mutex_err;
1816 }
1817
1818 mutex_lock(&dev->struct_mutex);
1819
1820 i915_verify_inactive(dev, __FILE__, __LINE__);
1821
1822 if (dev_priv->mm.wedged) {
1823 DRM_ERROR("Execbuf while wedged\n");
1824 mutex_unlock(&dev->struct_mutex);
1825 return -EIO;
1826 }
1827
1828 if (dev_priv->mm.suspended) {
1829 DRM_ERROR("Execbuf while VT-switched.\n");
1830 mutex_unlock(&dev->struct_mutex);
1831 return -EBUSY;
1832 }
1833
1834 /* Zero the gloabl flush/invalidate flags. These
1835 * will be modified as each object is bound to the
1836 * gtt
1837 */
1838 dev->invalidate_domains = 0;
1839 dev->flush_domains = 0;
1840
1841 /* Look up object handles and perform the relocations */
1842 for (i = 0; i < args->buffer_count; i++) {
1843 object_list[i] = drm_gem_object_lookup(dev, file_priv,
1844 exec_list[i].handle);
1845 if (object_list[i] == NULL) {
1846 DRM_ERROR("Invalid object handle %d at index %d\n",
1847 exec_list[i].handle, i);
1848 ret = -EBADF;
1849 goto err;
1850 }
1851
1852 object_list[i]->pending_read_domains = 0;
1853 object_list[i]->pending_write_domain = 0;
1854 ret = i915_gem_object_pin_and_relocate(object_list[i],
1855 file_priv,
1856 &exec_list[i]);
1857 if (ret) {
1858 DRM_ERROR("object bind and relocate failed %d\n", ret);
1859 goto err;
1860 }
1861 pinned = i + 1;
1862 }
1863
1864 /* Set the pending read domains for the batch buffer to COMMAND */
1865 batch_obj = object_list[args->buffer_count-1];
1866 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
1867 batch_obj->pending_write_domain = 0;
1868
1869 i915_verify_inactive(dev, __FILE__, __LINE__);
1870
1871 for (i = 0; i < args->buffer_count; i++) {
1872 struct drm_gem_object *obj = object_list[i];
1873 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1874
1875 if (obj_priv->gtt_space == NULL) {
1876 /* We evicted the buffer in the process of validating
1877 * our set of buffers in. We could try to recover by
1878 * kicking them everything out and trying again from
1879 * the start.
1880 */
1881 ret = -ENOMEM;
1882 goto err;
1883 }
1884
1885 /* make sure all previous memory operations have passed */
1886 ret = i915_gem_object_set_domain(obj,
1887 obj->pending_read_domains,
1888 obj->pending_write_domain);
1889 if (ret)
1890 goto err;
1891 }
1892
1893 i915_verify_inactive(dev, __FILE__, __LINE__);
1894
1895 /* Flush/invalidate caches and chipset buffer */
1896 flush_domains = i915_gem_dev_set_domain(dev);
1897
1898 i915_verify_inactive(dev, __FILE__, __LINE__);
1899
1900#if WATCH_COHERENCY
1901 for (i = 0; i < args->buffer_count; i++) {
1902 i915_gem_object_check_coherency(object_list[i],
1903 exec_list[i].handle);
1904 }
1905#endif
1906
1907 exec_offset = exec_list[args->buffer_count - 1].offset;
1908
1909#if WATCH_EXEC
1910 i915_gem_dump_object(object_list[args->buffer_count - 1],
1911 args->batch_len,
1912 __func__,
1913 ~0);
1914#endif
1915
1916 (void)i915_add_request(dev, flush_domains);
1917
1918 /* Exec the batchbuffer */
1919 ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
1920 if (ret) {
1921 DRM_ERROR("dispatch failed %d\n", ret);
1922 goto err;
1923 }
1924
1925 /*
1926 * Ensure that the commands in the batch buffer are
1927 * finished before the interrupt fires
1928 */
1929 flush_domains = i915_retire_commands(dev);
1930
1931 i915_verify_inactive(dev, __FILE__, __LINE__);
1932
1933 /*
1934 * Get a seqno representing the execution of the current buffer,
1935 * which we can wait on. We would like to mitigate these interrupts,
1936 * likely by only creating seqnos occasionally (so that we have
1937 * *some* interrupts representing completion of buffers that we can
1938 * wait on when trying to clear up gtt space).
1939 */
1940 seqno = i915_add_request(dev, flush_domains);
1941 BUG_ON(seqno == 0);
1942 i915_file_priv->mm.last_gem_seqno = seqno;
1943 for (i = 0; i < args->buffer_count; i++) {
1944 struct drm_gem_object *obj = object_list[i];
1945 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1946
1947 i915_gem_object_move_to_active(obj);
1948 obj_priv->last_rendering_seqno = seqno;
1949#if WATCH_LRU
1950 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
1951#endif
1952 }
1953#if WATCH_LRU
1954 i915_dump_lru(dev, __func__);
1955#endif
1956
1957 i915_verify_inactive(dev, __FILE__, __LINE__);
1958
1959 /* Copy the new buffer offsets back to the user's exec list. */
1960 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1961 (uintptr_t) args->buffers_ptr,
1962 exec_list,
1963 sizeof(*exec_list) * args->buffer_count);
1964 if (ret)
1965 DRM_ERROR("failed to copy %d exec entries "
1966 "back to user (%d)\n",
1967 args->buffer_count, ret);
1968err:
1969 if (object_list != NULL) {
1970 for (i = 0; i < pinned; i++)
1971 i915_gem_object_unpin(object_list[i]);
1972
1973 for (i = 0; i < args->buffer_count; i++)
1974 drm_gem_object_unreference(object_list[i]);
1975 }
1976 mutex_unlock(&dev->struct_mutex);
1977
1978pre_mutex_err:
1979 drm_free(object_list, sizeof(*object_list) * args->buffer_count,
1980 DRM_MEM_DRIVER);
1981 drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
1982 DRM_MEM_DRIVER);
1983
1984 return ret;
1985}
1986
1987int
1988i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
1989{
1990 struct drm_device *dev = obj->dev;
1991 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1992 int ret;
1993
1994 i915_verify_inactive(dev, __FILE__, __LINE__);
1995 if (obj_priv->gtt_space == NULL) {
1996 ret = i915_gem_object_bind_to_gtt(obj, alignment);
1997 if (ret != 0) {
1998 DRM_ERROR("Failure to bind: %d", ret);
1999 return ret;
2000 }
2001 }
2002 obj_priv->pin_count++;
2003
2004 /* If the object is not active and not pending a flush,
2005 * remove it from the inactive list
2006 */
2007 if (obj_priv->pin_count == 1) {
2008 atomic_inc(&dev->pin_count);
2009 atomic_add(obj->size, &dev->pin_memory);
2010 if (!obj_priv->active &&
2011 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2012 I915_GEM_DOMAIN_GTT)) == 0 &&
2013 !list_empty(&obj_priv->list))
2014 list_del_init(&obj_priv->list);
2015 }
2016 i915_verify_inactive(dev, __FILE__, __LINE__);
2017
2018 return 0;
2019}
2020
2021void
2022i915_gem_object_unpin(struct drm_gem_object *obj)
2023{
2024 struct drm_device *dev = obj->dev;
2025 drm_i915_private_t *dev_priv = dev->dev_private;
2026 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2027
2028 i915_verify_inactive(dev, __FILE__, __LINE__);
2029 obj_priv->pin_count--;
2030 BUG_ON(obj_priv->pin_count < 0);
2031 BUG_ON(obj_priv->gtt_space == NULL);
2032
2033 /* If the object is no longer pinned, and is
2034 * neither active nor being flushed, then stick it on
2035 * the inactive list
2036 */
2037 if (obj_priv->pin_count == 0) {
2038 if (!obj_priv->active &&
2039 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2040 I915_GEM_DOMAIN_GTT)) == 0)
2041 list_move_tail(&obj_priv->list,
2042 &dev_priv->mm.inactive_list);
2043 atomic_dec(&dev->pin_count);
2044 atomic_sub(obj->size, &dev->pin_memory);
2045 }
2046 i915_verify_inactive(dev, __FILE__, __LINE__);
2047}
2048
2049int
2050i915_gem_pin_ioctl(struct drm_device *dev, void *data,
2051 struct drm_file *file_priv)
2052{
2053 struct drm_i915_gem_pin *args = data;
2054 struct drm_gem_object *obj;
2055 struct drm_i915_gem_object *obj_priv;
2056 int ret;
2057
2058 mutex_lock(&dev->struct_mutex);
2059
2060 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2061 if (obj == NULL) {
2062 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
2063 args->handle);
2064 mutex_unlock(&dev->struct_mutex);
2065 return -EBADF;
2066 }
2067 obj_priv = obj->driver_private;
2068
2069 ret = i915_gem_object_pin(obj, args->alignment);
2070 if (ret != 0) {
2071 drm_gem_object_unreference(obj);
2072 mutex_unlock(&dev->struct_mutex);
2073 return ret;
2074 }
2075
2076 /* XXX - flush the CPU caches for pinned objects
2077 * as the X server doesn't manage domains yet
2078 */
2079 if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
2080 i915_gem_clflush_object(obj);
2081 drm_agp_chipset_flush(dev);
2082 obj->write_domain = 0;
2083 }
2084 args->offset = obj_priv->gtt_offset;
2085 drm_gem_object_unreference(obj);
2086 mutex_unlock(&dev->struct_mutex);
2087
2088 return 0;
2089}
2090
2091int
2092i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
2093 struct drm_file *file_priv)
2094{
2095 struct drm_i915_gem_pin *args = data;
2096 struct drm_gem_object *obj;
2097
2098 mutex_lock(&dev->struct_mutex);
2099
2100 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2101 if (obj == NULL) {
2102 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
2103 args->handle);
2104 mutex_unlock(&dev->struct_mutex);
2105 return -EBADF;
2106 }
2107
2108 i915_gem_object_unpin(obj);
2109
2110 drm_gem_object_unreference(obj);
2111 mutex_unlock(&dev->struct_mutex);
2112 return 0;
2113}
2114
2115int
2116i915_gem_busy_ioctl(struct drm_device *dev, void *data,
2117 struct drm_file *file_priv)
2118{
2119 struct drm_i915_gem_busy *args = data;
2120 struct drm_gem_object *obj;
2121 struct drm_i915_gem_object *obj_priv;
2122
2123 mutex_lock(&dev->struct_mutex);
2124 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2125 if (obj == NULL) {
2126 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
2127 args->handle);
2128 mutex_unlock(&dev->struct_mutex);
2129 return -EBADF;
2130 }
2131
2132 obj_priv = obj->driver_private;
2133 args->busy = obj_priv->active;
2134
2135 drm_gem_object_unreference(obj);
2136 mutex_unlock(&dev->struct_mutex);
2137 return 0;
2138}
2139
2140int
2141i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
2142 struct drm_file *file_priv)
2143{
2144 return i915_gem_ring_throttle(dev, file_priv);
2145}
2146
2147int i915_gem_init_object(struct drm_gem_object *obj)
2148{
2149 struct drm_i915_gem_object *obj_priv;
2150
2151 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
2152 if (obj_priv == NULL)
2153 return -ENOMEM;
2154
2155 /*
2156 * We've just allocated pages from the kernel,
2157 * so they've just been written by the CPU with
2158 * zeros. They'll need to be clflushed before we
2159 * use them with the GPU.
2160 */
2161 obj->write_domain = I915_GEM_DOMAIN_CPU;
2162 obj->read_domains = I915_GEM_DOMAIN_CPU;
2163
ba1eb1d8
KP
2164 obj_priv->agp_type = AGP_USER_MEMORY;
2165
673a394b
EA
2166 obj->driver_private = obj_priv;
2167 obj_priv->obj = obj;
2168 INIT_LIST_HEAD(&obj_priv->list);
2169 return 0;
2170}
2171
2172void i915_gem_free_object(struct drm_gem_object *obj)
2173{
2174 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2175
2176 while (obj_priv->pin_count > 0)
2177 i915_gem_object_unpin(obj);
2178
2179 i915_gem_object_unbind(obj);
2180
2181 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
2182 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
2183}
2184
2185static int
2186i915_gem_set_domain(struct drm_gem_object *obj,
2187 struct drm_file *file_priv,
2188 uint32_t read_domains,
2189 uint32_t write_domain)
2190{
2191 struct drm_device *dev = obj->dev;
2192 int ret;
2193 uint32_t flush_domains;
2194
2195 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
2196
2197 ret = i915_gem_object_set_domain(obj, read_domains, write_domain);
2198 if (ret)
2199 return ret;
2200 flush_domains = i915_gem_dev_set_domain(obj->dev);
2201
2202 if (flush_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT))
2203 (void) i915_add_request(dev, flush_domains);
2204
2205 return 0;
2206}
2207
2208/** Unbinds all objects that are on the given buffer list. */
2209static int
2210i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
2211{
2212 struct drm_gem_object *obj;
2213 struct drm_i915_gem_object *obj_priv;
2214 int ret;
2215
2216 while (!list_empty(head)) {
2217 obj_priv = list_first_entry(head,
2218 struct drm_i915_gem_object,
2219 list);
2220 obj = obj_priv->obj;
2221
2222 if (obj_priv->pin_count != 0) {
2223 DRM_ERROR("Pinned object in unbind list\n");
2224 mutex_unlock(&dev->struct_mutex);
2225 return -EINVAL;
2226 }
2227
2228 ret = i915_gem_object_unbind(obj);
2229 if (ret != 0) {
2230 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
2231 ret);
2232 mutex_unlock(&dev->struct_mutex);
2233 return ret;
2234 }
2235 }
2236
2237
2238 return 0;
2239}
2240
2241static int
2242i915_gem_idle(struct drm_device *dev)
2243{
2244 drm_i915_private_t *dev_priv = dev->dev_private;
2245 uint32_t seqno, cur_seqno, last_seqno;
2246 int stuck, ret;
2247
6dbe2772
KP
2248 mutex_lock(&dev->struct_mutex);
2249
2250 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
2251 mutex_unlock(&dev->struct_mutex);
673a394b 2252 return 0;
6dbe2772 2253 }
673a394b
EA
2254
2255 /* Hack! Don't let anybody do execbuf while we don't control the chip.
2256 * We need to replace this with a semaphore, or something.
2257 */
2258 dev_priv->mm.suspended = 1;
2259
6dbe2772
KP
2260 /* Cancel the retire work handler, wait for it to finish if running
2261 */
2262 mutex_unlock(&dev->struct_mutex);
2263 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
2264 mutex_lock(&dev->struct_mutex);
2265
673a394b
EA
2266 i915_kernel_lost_context(dev);
2267
2268 /* Flush the GPU along with all non-CPU write domains
2269 */
2270 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
2271 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2272 seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU |
2273 I915_GEM_DOMAIN_GTT));
2274
2275 if (seqno == 0) {
2276 mutex_unlock(&dev->struct_mutex);
2277 return -ENOMEM;
2278 }
2279
2280 dev_priv->mm.waiting_gem_seqno = seqno;
2281 last_seqno = 0;
2282 stuck = 0;
2283 for (;;) {
2284 cur_seqno = i915_get_gem_seqno(dev);
2285 if (i915_seqno_passed(cur_seqno, seqno))
2286 break;
2287 if (last_seqno == cur_seqno) {
2288 if (stuck++ > 100) {
2289 DRM_ERROR("hardware wedged\n");
2290 dev_priv->mm.wedged = 1;
2291 DRM_WAKEUP(&dev_priv->irq_queue);
2292 break;
2293 }
2294 }
2295 msleep(10);
2296 last_seqno = cur_seqno;
2297 }
2298 dev_priv->mm.waiting_gem_seqno = 0;
2299
2300 i915_gem_retire_requests(dev);
2301
2302 /* Active and flushing should now be empty as we've
2303 * waited for a sequence higher than any pending execbuffer
2304 */
2305 BUG_ON(!list_empty(&dev_priv->mm.active_list));
2306 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2307
2308 /* Request should now be empty as we've also waited
2309 * for the last request in the list
2310 */
2311 BUG_ON(!list_empty(&dev_priv->mm.request_list));
2312
2313 /* Move all buffers out of the GTT. */
2314 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
6dbe2772
KP
2315 if (ret) {
2316 mutex_unlock(&dev->struct_mutex);
673a394b 2317 return ret;
6dbe2772 2318 }
673a394b
EA
2319
2320 BUG_ON(!list_empty(&dev_priv->mm.active_list));
2321 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2322 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
2323 BUG_ON(!list_empty(&dev_priv->mm.request_list));
6dbe2772
KP
2324
2325 i915_gem_cleanup_ringbuffer(dev);
2326 mutex_unlock(&dev->struct_mutex);
2327
673a394b
EA
2328 return 0;
2329}
2330
2331static int
2332i915_gem_init_hws(struct drm_device *dev)
2333{
2334 drm_i915_private_t *dev_priv = dev->dev_private;
2335 struct drm_gem_object *obj;
2336 struct drm_i915_gem_object *obj_priv;
2337 int ret;
2338
2339 /* If we need a physical address for the status page, it's already
2340 * initialized at driver load time.
2341 */
2342 if (!I915_NEED_GFX_HWS(dev))
2343 return 0;
2344
2345 obj = drm_gem_object_alloc(dev, 4096);
2346 if (obj == NULL) {
2347 DRM_ERROR("Failed to allocate status page\n");
2348 return -ENOMEM;
2349 }
2350 obj_priv = obj->driver_private;
ba1eb1d8 2351 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
673a394b
EA
2352
2353 ret = i915_gem_object_pin(obj, 4096);
2354 if (ret != 0) {
2355 drm_gem_object_unreference(obj);
2356 return ret;
2357 }
2358
2359 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
673a394b 2360
ba1eb1d8
KP
2361 dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
2362 if (dev_priv->hw_status_page == NULL) {
673a394b
EA
2363 DRM_ERROR("Failed to map status page.\n");
2364 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
2365 drm_gem_object_unreference(obj);
2366 return -EINVAL;
2367 }
2368 dev_priv->hws_obj = obj;
673a394b
EA
2369 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
2370 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
ba1eb1d8 2371 I915_READ(HWS_PGA); /* posting read */
673a394b
EA
2372 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
2373
2374 return 0;
2375}
2376
2377static int
2378i915_gem_init_ringbuffer(struct drm_device *dev)
2379{
2380 drm_i915_private_t *dev_priv = dev->dev_private;
2381 struct drm_gem_object *obj;
2382 struct drm_i915_gem_object *obj_priv;
2383 int ret;
50aa253d 2384 u32 head;
673a394b
EA
2385
2386 ret = i915_gem_init_hws(dev);
2387 if (ret != 0)
2388 return ret;
2389
2390 obj = drm_gem_object_alloc(dev, 128 * 1024);
2391 if (obj == NULL) {
2392 DRM_ERROR("Failed to allocate ringbuffer\n");
2393 return -ENOMEM;
2394 }
2395 obj_priv = obj->driver_private;
2396
2397 ret = i915_gem_object_pin(obj, 4096);
2398 if (ret != 0) {
2399 drm_gem_object_unreference(obj);
2400 return ret;
2401 }
2402
2403 /* Set up the kernel mapping for the ring. */
2404 dev_priv->ring.Size = obj->size;
2405 dev_priv->ring.tail_mask = obj->size - 1;
2406
2407 dev_priv->ring.map.offset = dev->agp->base + obj_priv->gtt_offset;
2408 dev_priv->ring.map.size = obj->size;
2409 dev_priv->ring.map.type = 0;
2410 dev_priv->ring.map.flags = 0;
2411 dev_priv->ring.map.mtrr = 0;
2412
bd88ee4c 2413 drm_core_ioremap_wc(&dev_priv->ring.map, dev);
673a394b
EA
2414 if (dev_priv->ring.map.handle == NULL) {
2415 DRM_ERROR("Failed to map ringbuffer.\n");
2416 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
2417 drm_gem_object_unreference(obj);
2418 return -EINVAL;
2419 }
2420 dev_priv->ring.ring_obj = obj;
2421 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
2422
2423 /* Stop the ring if it's running. */
2424 I915_WRITE(PRB0_CTL, 0);
673a394b 2425 I915_WRITE(PRB0_TAIL, 0);
50aa253d 2426 I915_WRITE(PRB0_HEAD, 0);
673a394b
EA
2427
2428 /* Initialize the ring. */
2429 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
50aa253d
KP
2430 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
2431
2432 /* G45 ring initialization fails to reset head to zero */
2433 if (head != 0) {
2434 DRM_ERROR("Ring head not reset to zero "
2435 "ctl %08x head %08x tail %08x start %08x\n",
2436 I915_READ(PRB0_CTL),
2437 I915_READ(PRB0_HEAD),
2438 I915_READ(PRB0_TAIL),
2439 I915_READ(PRB0_START));
2440 I915_WRITE(PRB0_HEAD, 0);
2441
2442 DRM_ERROR("Ring head forced to zero "
2443 "ctl %08x head %08x tail %08x start %08x\n",
2444 I915_READ(PRB0_CTL),
2445 I915_READ(PRB0_HEAD),
2446 I915_READ(PRB0_TAIL),
2447 I915_READ(PRB0_START));
2448 }
2449
673a394b
EA
2450 I915_WRITE(PRB0_CTL,
2451 ((obj->size - 4096) & RING_NR_PAGES) |
2452 RING_NO_REPORT |
2453 RING_VALID);
2454
50aa253d
KP
2455 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
2456
2457 /* If the head is still not zero, the ring is dead */
2458 if (head != 0) {
2459 DRM_ERROR("Ring initialization failed "
2460 "ctl %08x head %08x tail %08x start %08x\n",
2461 I915_READ(PRB0_CTL),
2462 I915_READ(PRB0_HEAD),
2463 I915_READ(PRB0_TAIL),
2464 I915_READ(PRB0_START));
2465 return -EIO;
2466 }
2467
673a394b
EA
2468 /* Update our cache of the ring state */
2469 i915_kernel_lost_context(dev);
2470
2471 return 0;
2472}
2473
2474static void
2475i915_gem_cleanup_ringbuffer(struct drm_device *dev)
2476{
2477 drm_i915_private_t *dev_priv = dev->dev_private;
2478
2479 if (dev_priv->ring.ring_obj == NULL)
2480 return;
2481
2482 drm_core_ioremapfree(&dev_priv->ring.map, dev);
2483
2484 i915_gem_object_unpin(dev_priv->ring.ring_obj);
2485 drm_gem_object_unreference(dev_priv->ring.ring_obj);
2486 dev_priv->ring.ring_obj = NULL;
2487 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
2488
2489 if (dev_priv->hws_obj != NULL) {
ba1eb1d8
KP
2490 struct drm_gem_object *obj = dev_priv->hws_obj;
2491 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2492
2493 kunmap(obj_priv->page_list[0]);
2494 i915_gem_object_unpin(obj);
2495 drm_gem_object_unreference(obj);
673a394b
EA
2496 dev_priv->hws_obj = NULL;
2497 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
ba1eb1d8 2498 dev_priv->hw_status_page = NULL;
673a394b
EA
2499
2500 /* Write high address into HWS_PGA when disabling. */
2501 I915_WRITE(HWS_PGA, 0x1ffff000);
2502 }
2503}
2504
2505int
2506i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
2507 struct drm_file *file_priv)
2508{
2509 drm_i915_private_t *dev_priv = dev->dev_private;
2510 int ret;
2511
2512 if (dev_priv->mm.wedged) {
2513 DRM_ERROR("Reenabling wedged hardware, good luck\n");
2514 dev_priv->mm.wedged = 0;
2515 }
2516
2517 ret = i915_gem_init_ringbuffer(dev);
2518 if (ret != 0)
2519 return ret;
2520
0839ccb8
KP
2521 dev_priv->mm.gtt_mapping = io_mapping_create_wc(dev->agp->base,
2522 dev->agp->agp_info.aper_size
2523 * 1024 * 1024);
2524
673a394b
EA
2525 mutex_lock(&dev->struct_mutex);
2526 BUG_ON(!list_empty(&dev_priv->mm.active_list));
2527 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2528 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
2529 BUG_ON(!list_empty(&dev_priv->mm.request_list));
2530 dev_priv->mm.suspended = 0;
2531 mutex_unlock(&dev->struct_mutex);
dbb19d30
KH
2532
2533 drm_irq_install(dev);
2534
673a394b
EA
2535 return 0;
2536}
2537
2538int
2539i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
2540 struct drm_file *file_priv)
2541{
0839ccb8 2542 drm_i915_private_t *dev_priv = dev->dev_private;
673a394b
EA
2543 int ret;
2544
673a394b 2545 ret = i915_gem_idle(dev);
dbb19d30
KH
2546 drm_irq_uninstall(dev);
2547
0839ccb8 2548 io_mapping_free(dev_priv->mm.gtt_mapping);
6dbe2772 2549 return ret;
673a394b
EA
2550}
2551
2552void
2553i915_gem_lastclose(struct drm_device *dev)
2554{
2555 int ret;
673a394b 2556
6dbe2772
KP
2557 ret = i915_gem_idle(dev);
2558 if (ret)
2559 DRM_ERROR("failed to idle hardware: %d\n", ret);
673a394b
EA
2560}
2561
2562void
2563i915_gem_load(struct drm_device *dev)
2564{
2565 drm_i915_private_t *dev_priv = dev->dev_private;
2566
2567 INIT_LIST_HEAD(&dev_priv->mm.active_list);
2568 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
2569 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
2570 INIT_LIST_HEAD(&dev_priv->mm.request_list);
2571 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
2572 i915_gem_retire_work_handler);
2573 dev_priv->mm.next_gem_seqno = 1;
2574
2575 i915_gem_detect_bit_6_swizzle(dev);
2576}