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