drm/i915: Move the implementation details of PIPE_CONTROL to the ringbuffer
[deliverable/linux.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 "i915_trace.h"
33 #include "intel_drv.h"
34 #include <linux/slab.h>
35 #include <linux/swap.h>
36 #include <linux/pci.h>
37
38 struct change_domains {
39 uint32_t invalidate_domains;
40 uint32_t flush_domains;
41 uint32_t flush_rings;
42 };
43
44 static int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
45 bool pipelined);
46 static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
47 static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
48 static int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
49 int write);
50 static int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
51 uint64_t offset,
52 uint64_t size);
53 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
54 static int i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
55 bool interruptible);
56 static int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
57 unsigned alignment,
58 bool map_and_fenceable);
59 static void i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj);
60 static int i915_gem_phys_pwrite(struct drm_device *dev,
61 struct drm_i915_gem_object *obj,
62 struct drm_i915_gem_pwrite *args,
63 struct drm_file *file);
64 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
65
66 static int i915_gem_inactive_shrink(struct shrinker *shrinker,
67 int nr_to_scan,
68 gfp_t gfp_mask);
69
70
71 /* some bookkeeping */
72 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
73 size_t size)
74 {
75 dev_priv->mm.object_count++;
76 dev_priv->mm.object_memory += size;
77 }
78
79 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
80 size_t size)
81 {
82 dev_priv->mm.object_count--;
83 dev_priv->mm.object_memory -= size;
84 }
85
86 static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
87 struct drm_i915_gem_object *obj)
88 {
89 dev_priv->mm.gtt_count++;
90 dev_priv->mm.gtt_memory += obj->gtt_space->size;
91 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
92 dev_priv->mm.mappable_gtt_used +=
93 min_t(size_t, obj->gtt_space->size,
94 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
95 }
96 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
97 }
98
99 static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
100 struct drm_i915_gem_object *obj)
101 {
102 dev_priv->mm.gtt_count--;
103 dev_priv->mm.gtt_memory -= obj->gtt_space->size;
104 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
105 dev_priv->mm.mappable_gtt_used -=
106 min_t(size_t, obj->gtt_space->size,
107 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
108 }
109 list_del_init(&obj->gtt_list);
110 }
111
112 /**
113 * Update the mappable working set counters. Call _only_ when there is a change
114 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
115 * @mappable: new state the changed mappable flag (either pin_ or fault_).
116 */
117 static void
118 i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
119 struct drm_i915_gem_object *obj,
120 bool mappable)
121 {
122 if (mappable) {
123 if (obj->pin_mappable && obj->fault_mappable)
124 /* Combined state was already mappable. */
125 return;
126 dev_priv->mm.gtt_mappable_count++;
127 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
128 } else {
129 if (obj->pin_mappable || obj->fault_mappable)
130 /* Combined state still mappable. */
131 return;
132 dev_priv->mm.gtt_mappable_count--;
133 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
134 }
135 }
136
137 static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
138 struct drm_i915_gem_object *obj,
139 bool mappable)
140 {
141 dev_priv->mm.pin_count++;
142 dev_priv->mm.pin_memory += obj->gtt_space->size;
143 if (mappable) {
144 obj->pin_mappable = true;
145 i915_gem_info_update_mappable(dev_priv, obj, true);
146 }
147 }
148
149 static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
150 struct drm_i915_gem_object *obj)
151 {
152 dev_priv->mm.pin_count--;
153 dev_priv->mm.pin_memory -= obj->gtt_space->size;
154 if (obj->pin_mappable) {
155 obj->pin_mappable = false;
156 i915_gem_info_update_mappable(dev_priv, obj, false);
157 }
158 }
159
160 int
161 i915_gem_check_is_wedged(struct drm_device *dev)
162 {
163 struct drm_i915_private *dev_priv = dev->dev_private;
164 struct completion *x = &dev_priv->error_completion;
165 unsigned long flags;
166 int ret;
167
168 if (!atomic_read(&dev_priv->mm.wedged))
169 return 0;
170
171 ret = wait_for_completion_interruptible(x);
172 if (ret)
173 return ret;
174
175 /* Success, we reset the GPU! */
176 if (!atomic_read(&dev_priv->mm.wedged))
177 return 0;
178
179 /* GPU is hung, bump the completion count to account for
180 * the token we just consumed so that we never hit zero and
181 * end up waiting upon a subsequent completion event that
182 * will never happen.
183 */
184 spin_lock_irqsave(&x->wait.lock, flags);
185 x->done++;
186 spin_unlock_irqrestore(&x->wait.lock, flags);
187 return -EIO;
188 }
189
190 static int i915_mutex_lock_interruptible(struct drm_device *dev)
191 {
192 struct drm_i915_private *dev_priv = dev->dev_private;
193 int ret;
194
195 ret = i915_gem_check_is_wedged(dev);
196 if (ret)
197 return ret;
198
199 ret = mutex_lock_interruptible(&dev->struct_mutex);
200 if (ret)
201 return ret;
202
203 if (atomic_read(&dev_priv->mm.wedged)) {
204 mutex_unlock(&dev->struct_mutex);
205 return -EAGAIN;
206 }
207
208 WARN_ON(i915_verify_lists(dev));
209 return 0;
210 }
211
212 static inline bool
213 i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
214 {
215 return obj->gtt_space && !obj->active && obj->pin_count == 0;
216 }
217
218 int i915_gem_do_init(struct drm_device *dev,
219 unsigned long start,
220 unsigned long mappable_end,
221 unsigned long end)
222 {
223 drm_i915_private_t *dev_priv = dev->dev_private;
224
225 if (start >= end ||
226 (start & (PAGE_SIZE - 1)) != 0 ||
227 (end & (PAGE_SIZE - 1)) != 0) {
228 return -EINVAL;
229 }
230
231 drm_mm_init(&dev_priv->mm.gtt_space, start,
232 end - start);
233
234 dev_priv->mm.gtt_total = end - start;
235 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
236 dev_priv->mm.gtt_mappable_end = mappable_end;
237
238 return 0;
239 }
240
241 int
242 i915_gem_init_ioctl(struct drm_device *dev, void *data,
243 struct drm_file *file)
244 {
245 struct drm_i915_gem_init *args = data;
246 int ret;
247
248 mutex_lock(&dev->struct_mutex);
249 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
250 mutex_unlock(&dev->struct_mutex);
251
252 return ret;
253 }
254
255 int
256 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
257 struct drm_file *file)
258 {
259 struct drm_i915_private *dev_priv = dev->dev_private;
260 struct drm_i915_gem_get_aperture *args = data;
261
262 if (!(dev->driver->driver_features & DRIVER_GEM))
263 return -ENODEV;
264
265 mutex_lock(&dev->struct_mutex);
266 args->aper_size = dev_priv->mm.gtt_total;
267 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
268 mutex_unlock(&dev->struct_mutex);
269
270 return 0;
271 }
272
273
274 /**
275 * Creates a new mm object and returns a handle to it.
276 */
277 int
278 i915_gem_create_ioctl(struct drm_device *dev, void *data,
279 struct drm_file *file)
280 {
281 struct drm_i915_gem_create *args = data;
282 struct drm_i915_gem_object *obj;
283 int ret;
284 u32 handle;
285
286 args->size = roundup(args->size, PAGE_SIZE);
287
288 /* Allocate the new object */
289 obj = i915_gem_alloc_object(dev, args->size);
290 if (obj == NULL)
291 return -ENOMEM;
292
293 ret = drm_gem_handle_create(file, &obj->base, &handle);
294 if (ret) {
295 drm_gem_object_release(&obj->base);
296 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
297 kfree(obj);
298 return ret;
299 }
300
301 /* drop reference from allocate - handle holds it now */
302 drm_gem_object_unreference(&obj->base);
303 trace_i915_gem_object_create(obj);
304
305 args->handle = handle;
306 return 0;
307 }
308
309 static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
310 {
311 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
312
313 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
314 obj->tiling_mode != I915_TILING_NONE;
315 }
316
317 static inline void
318 slow_shmem_copy(struct page *dst_page,
319 int dst_offset,
320 struct page *src_page,
321 int src_offset,
322 int length)
323 {
324 char *dst_vaddr, *src_vaddr;
325
326 dst_vaddr = kmap(dst_page);
327 src_vaddr = kmap(src_page);
328
329 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
330
331 kunmap(src_page);
332 kunmap(dst_page);
333 }
334
335 static inline void
336 slow_shmem_bit17_copy(struct page *gpu_page,
337 int gpu_offset,
338 struct page *cpu_page,
339 int cpu_offset,
340 int length,
341 int is_read)
342 {
343 char *gpu_vaddr, *cpu_vaddr;
344
345 /* Use the unswizzled path if this page isn't affected. */
346 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
347 if (is_read)
348 return slow_shmem_copy(cpu_page, cpu_offset,
349 gpu_page, gpu_offset, length);
350 else
351 return slow_shmem_copy(gpu_page, gpu_offset,
352 cpu_page, cpu_offset, length);
353 }
354
355 gpu_vaddr = kmap(gpu_page);
356 cpu_vaddr = kmap(cpu_page);
357
358 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
359 * XORing with the other bits (A9 for Y, A9 and A10 for X)
360 */
361 while (length > 0) {
362 int cacheline_end = ALIGN(gpu_offset + 1, 64);
363 int this_length = min(cacheline_end - gpu_offset, length);
364 int swizzled_gpu_offset = gpu_offset ^ 64;
365
366 if (is_read) {
367 memcpy(cpu_vaddr + cpu_offset,
368 gpu_vaddr + swizzled_gpu_offset,
369 this_length);
370 } else {
371 memcpy(gpu_vaddr + swizzled_gpu_offset,
372 cpu_vaddr + cpu_offset,
373 this_length);
374 }
375 cpu_offset += this_length;
376 gpu_offset += this_length;
377 length -= this_length;
378 }
379
380 kunmap(cpu_page);
381 kunmap(gpu_page);
382 }
383
384 /**
385 * This is the fast shmem pread path, which attempts to copy_from_user directly
386 * from the backing pages of the object to the user's address space. On a
387 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
388 */
389 static int
390 i915_gem_shmem_pread_fast(struct drm_device *dev,
391 struct drm_i915_gem_object *obj,
392 struct drm_i915_gem_pread *args,
393 struct drm_file *file)
394 {
395 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
396 ssize_t remain;
397 loff_t offset;
398 char __user *user_data;
399 int page_offset, page_length;
400
401 user_data = (char __user *) (uintptr_t) args->data_ptr;
402 remain = args->size;
403
404 offset = args->offset;
405
406 while (remain > 0) {
407 struct page *page;
408 char *vaddr;
409 int ret;
410
411 /* Operation in this page
412 *
413 * page_offset = offset within page
414 * page_length = bytes to copy for this page
415 */
416 page_offset = offset & (PAGE_SIZE-1);
417 page_length = remain;
418 if ((page_offset + remain) > PAGE_SIZE)
419 page_length = PAGE_SIZE - page_offset;
420
421 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
422 GFP_HIGHUSER | __GFP_RECLAIMABLE);
423 if (IS_ERR(page))
424 return PTR_ERR(page);
425
426 vaddr = kmap_atomic(page);
427 ret = __copy_to_user_inatomic(user_data,
428 vaddr + page_offset,
429 page_length);
430 kunmap_atomic(vaddr);
431
432 mark_page_accessed(page);
433 page_cache_release(page);
434 if (ret)
435 return -EFAULT;
436
437 remain -= page_length;
438 user_data += page_length;
439 offset += page_length;
440 }
441
442 return 0;
443 }
444
445 /**
446 * This is the fallback shmem pread path, which allocates temporary storage
447 * in kernel space to copy_to_user into outside of the struct_mutex, so we
448 * can copy out of the object's backing pages while holding the struct mutex
449 * and not take page faults.
450 */
451 static int
452 i915_gem_shmem_pread_slow(struct drm_device *dev,
453 struct drm_i915_gem_object *obj,
454 struct drm_i915_gem_pread *args,
455 struct drm_file *file)
456 {
457 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
458 struct mm_struct *mm = current->mm;
459 struct page **user_pages;
460 ssize_t remain;
461 loff_t offset, pinned_pages, i;
462 loff_t first_data_page, last_data_page, num_pages;
463 int shmem_page_offset;
464 int data_page_index, data_page_offset;
465 int page_length;
466 int ret;
467 uint64_t data_ptr = args->data_ptr;
468 int do_bit17_swizzling;
469
470 remain = args->size;
471
472 /* Pin the user pages containing the data. We can't fault while
473 * holding the struct mutex, yet we want to hold it while
474 * dereferencing the user data.
475 */
476 first_data_page = data_ptr / PAGE_SIZE;
477 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
478 num_pages = last_data_page - first_data_page + 1;
479
480 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
481 if (user_pages == NULL)
482 return -ENOMEM;
483
484 mutex_unlock(&dev->struct_mutex);
485 down_read(&mm->mmap_sem);
486 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
487 num_pages, 1, 0, user_pages, NULL);
488 up_read(&mm->mmap_sem);
489 mutex_lock(&dev->struct_mutex);
490 if (pinned_pages < num_pages) {
491 ret = -EFAULT;
492 goto out;
493 }
494
495 ret = i915_gem_object_set_cpu_read_domain_range(obj,
496 args->offset,
497 args->size);
498 if (ret)
499 goto out;
500
501 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
502
503 offset = args->offset;
504
505 while (remain > 0) {
506 struct page *page;
507
508 /* Operation in this page
509 *
510 * shmem_page_offset = offset within page in shmem file
511 * data_page_index = page number in get_user_pages return
512 * data_page_offset = offset with data_page_index page.
513 * page_length = bytes to copy for this page
514 */
515 shmem_page_offset = offset & ~PAGE_MASK;
516 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
517 data_page_offset = data_ptr & ~PAGE_MASK;
518
519 page_length = remain;
520 if ((shmem_page_offset + page_length) > PAGE_SIZE)
521 page_length = PAGE_SIZE - shmem_page_offset;
522 if ((data_page_offset + page_length) > PAGE_SIZE)
523 page_length = PAGE_SIZE - data_page_offset;
524
525 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
526 GFP_HIGHUSER | __GFP_RECLAIMABLE);
527 if (IS_ERR(page))
528 return PTR_ERR(page);
529
530 if (do_bit17_swizzling) {
531 slow_shmem_bit17_copy(page,
532 shmem_page_offset,
533 user_pages[data_page_index],
534 data_page_offset,
535 page_length,
536 1);
537 } else {
538 slow_shmem_copy(user_pages[data_page_index],
539 data_page_offset,
540 page,
541 shmem_page_offset,
542 page_length);
543 }
544
545 mark_page_accessed(page);
546 page_cache_release(page);
547
548 remain -= page_length;
549 data_ptr += page_length;
550 offset += page_length;
551 }
552
553 out:
554 for (i = 0; i < pinned_pages; i++) {
555 SetPageDirty(user_pages[i]);
556 mark_page_accessed(user_pages[i]);
557 page_cache_release(user_pages[i]);
558 }
559 drm_free_large(user_pages);
560
561 return ret;
562 }
563
564 /**
565 * Reads data from the object referenced by handle.
566 *
567 * On error, the contents of *data are undefined.
568 */
569 int
570 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
571 struct drm_file *file)
572 {
573 struct drm_i915_gem_pread *args = data;
574 struct drm_i915_gem_object *obj;
575 int ret = 0;
576
577 if (args->size == 0)
578 return 0;
579
580 if (!access_ok(VERIFY_WRITE,
581 (char __user *)(uintptr_t)args->data_ptr,
582 args->size))
583 return -EFAULT;
584
585 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
586 args->size);
587 if (ret)
588 return -EFAULT;
589
590 ret = i915_mutex_lock_interruptible(dev);
591 if (ret)
592 return ret;
593
594 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
595 if (obj == NULL) {
596 ret = -ENOENT;
597 goto unlock;
598 }
599
600 /* Bounds check source. */
601 if (args->offset > obj->base.size ||
602 args->size > obj->base.size - args->offset) {
603 ret = -EINVAL;
604 goto out;
605 }
606
607 ret = i915_gem_object_set_cpu_read_domain_range(obj,
608 args->offset,
609 args->size);
610 if (ret)
611 goto out;
612
613 ret = -EFAULT;
614 if (!i915_gem_object_needs_bit17_swizzle(obj))
615 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
616 if (ret == -EFAULT)
617 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
618
619 out:
620 drm_gem_object_unreference(&obj->base);
621 unlock:
622 mutex_unlock(&dev->struct_mutex);
623 return ret;
624 }
625
626 /* This is the fast write path which cannot handle
627 * page faults in the source data
628 */
629
630 static inline int
631 fast_user_write(struct io_mapping *mapping,
632 loff_t page_base, int page_offset,
633 char __user *user_data,
634 int length)
635 {
636 char *vaddr_atomic;
637 unsigned long unwritten;
638
639 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
640 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
641 user_data, length);
642 io_mapping_unmap_atomic(vaddr_atomic);
643 return unwritten;
644 }
645
646 /* Here's the write path which can sleep for
647 * page faults
648 */
649
650 static inline void
651 slow_kernel_write(struct io_mapping *mapping,
652 loff_t gtt_base, int gtt_offset,
653 struct page *user_page, int user_offset,
654 int length)
655 {
656 char __iomem *dst_vaddr;
657 char *src_vaddr;
658
659 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
660 src_vaddr = kmap(user_page);
661
662 memcpy_toio(dst_vaddr + gtt_offset,
663 src_vaddr + user_offset,
664 length);
665
666 kunmap(user_page);
667 io_mapping_unmap(dst_vaddr);
668 }
669
670 /**
671 * This is the fast pwrite path, where we copy the data directly from the
672 * user into the GTT, uncached.
673 */
674 static int
675 i915_gem_gtt_pwrite_fast(struct drm_device *dev,
676 struct drm_i915_gem_object *obj,
677 struct drm_i915_gem_pwrite *args,
678 struct drm_file *file)
679 {
680 drm_i915_private_t *dev_priv = dev->dev_private;
681 ssize_t remain;
682 loff_t offset, page_base;
683 char __user *user_data;
684 int page_offset, page_length;
685
686 user_data = (char __user *) (uintptr_t) args->data_ptr;
687 remain = args->size;
688
689 offset = obj->gtt_offset + args->offset;
690
691 while (remain > 0) {
692 /* Operation in this page
693 *
694 * page_base = page offset within aperture
695 * page_offset = offset within page
696 * page_length = bytes to copy for this page
697 */
698 page_base = (offset & ~(PAGE_SIZE-1));
699 page_offset = offset & (PAGE_SIZE-1);
700 page_length = remain;
701 if ((page_offset + remain) > PAGE_SIZE)
702 page_length = PAGE_SIZE - page_offset;
703
704 /* If we get a fault while copying data, then (presumably) our
705 * source page isn't available. Return the error and we'll
706 * retry in the slow path.
707 */
708 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
709 page_offset, user_data, page_length))
710
711 return -EFAULT;
712
713 remain -= page_length;
714 user_data += page_length;
715 offset += page_length;
716 }
717
718 return 0;
719 }
720
721 /**
722 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
723 * the memory and maps it using kmap_atomic for copying.
724 *
725 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
726 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
727 */
728 static int
729 i915_gem_gtt_pwrite_slow(struct drm_device *dev,
730 struct drm_i915_gem_object *obj,
731 struct drm_i915_gem_pwrite *args,
732 struct drm_file *file)
733 {
734 drm_i915_private_t *dev_priv = dev->dev_private;
735 ssize_t remain;
736 loff_t gtt_page_base, offset;
737 loff_t first_data_page, last_data_page, num_pages;
738 loff_t pinned_pages, i;
739 struct page **user_pages;
740 struct mm_struct *mm = current->mm;
741 int gtt_page_offset, data_page_offset, data_page_index, page_length;
742 int ret;
743 uint64_t data_ptr = args->data_ptr;
744
745 remain = args->size;
746
747 /* Pin the user pages containing the data. We can't fault while
748 * holding the struct mutex, and all of the pwrite implementations
749 * want to hold it while dereferencing the user data.
750 */
751 first_data_page = data_ptr / PAGE_SIZE;
752 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
753 num_pages = last_data_page - first_data_page + 1;
754
755 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
756 if (user_pages == NULL)
757 return -ENOMEM;
758
759 mutex_unlock(&dev->struct_mutex);
760 down_read(&mm->mmap_sem);
761 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
762 num_pages, 0, 0, user_pages, NULL);
763 up_read(&mm->mmap_sem);
764 mutex_lock(&dev->struct_mutex);
765 if (pinned_pages < num_pages) {
766 ret = -EFAULT;
767 goto out_unpin_pages;
768 }
769
770 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
771 if (ret)
772 goto out_unpin_pages;
773
774 offset = obj->gtt_offset + args->offset;
775
776 while (remain > 0) {
777 /* Operation in this page
778 *
779 * gtt_page_base = page offset within aperture
780 * gtt_page_offset = offset within page in aperture
781 * data_page_index = page number in get_user_pages return
782 * data_page_offset = offset with data_page_index page.
783 * page_length = bytes to copy for this page
784 */
785 gtt_page_base = offset & PAGE_MASK;
786 gtt_page_offset = offset & ~PAGE_MASK;
787 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
788 data_page_offset = data_ptr & ~PAGE_MASK;
789
790 page_length = remain;
791 if ((gtt_page_offset + page_length) > PAGE_SIZE)
792 page_length = PAGE_SIZE - gtt_page_offset;
793 if ((data_page_offset + page_length) > PAGE_SIZE)
794 page_length = PAGE_SIZE - data_page_offset;
795
796 slow_kernel_write(dev_priv->mm.gtt_mapping,
797 gtt_page_base, gtt_page_offset,
798 user_pages[data_page_index],
799 data_page_offset,
800 page_length);
801
802 remain -= page_length;
803 offset += page_length;
804 data_ptr += page_length;
805 }
806
807 out_unpin_pages:
808 for (i = 0; i < pinned_pages; i++)
809 page_cache_release(user_pages[i]);
810 drm_free_large(user_pages);
811
812 return ret;
813 }
814
815 /**
816 * This is the fast shmem pwrite path, which attempts to directly
817 * copy_from_user into the kmapped pages backing the object.
818 */
819 static int
820 i915_gem_shmem_pwrite_fast(struct drm_device *dev,
821 struct drm_i915_gem_object *obj,
822 struct drm_i915_gem_pwrite *args,
823 struct drm_file *file)
824 {
825 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
826 ssize_t remain;
827 loff_t offset;
828 char __user *user_data;
829 int page_offset, page_length;
830
831 user_data = (char __user *) (uintptr_t) args->data_ptr;
832 remain = args->size;
833
834 offset = args->offset;
835 obj->dirty = 1;
836
837 while (remain > 0) {
838 struct page *page;
839 char *vaddr;
840 int ret;
841
842 /* Operation in this page
843 *
844 * page_offset = offset within page
845 * page_length = bytes to copy for this page
846 */
847 page_offset = offset & (PAGE_SIZE-1);
848 page_length = remain;
849 if ((page_offset + remain) > PAGE_SIZE)
850 page_length = PAGE_SIZE - page_offset;
851
852 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
853 GFP_HIGHUSER | __GFP_RECLAIMABLE);
854 if (IS_ERR(page))
855 return PTR_ERR(page);
856
857 vaddr = kmap_atomic(page, KM_USER0);
858 ret = __copy_from_user_inatomic(vaddr + page_offset,
859 user_data,
860 page_length);
861 kunmap_atomic(vaddr, KM_USER0);
862
863 set_page_dirty(page);
864 mark_page_accessed(page);
865 page_cache_release(page);
866
867 /* If we get a fault while copying data, then (presumably) our
868 * source page isn't available. Return the error and we'll
869 * retry in the slow path.
870 */
871 if (ret)
872 return -EFAULT;
873
874 remain -= page_length;
875 user_data += page_length;
876 offset += page_length;
877 }
878
879 return 0;
880 }
881
882 /**
883 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
884 * the memory and maps it using kmap_atomic for copying.
885 *
886 * This avoids taking mmap_sem for faulting on the user's address while the
887 * struct_mutex is held.
888 */
889 static int
890 i915_gem_shmem_pwrite_slow(struct drm_device *dev,
891 struct drm_i915_gem_object *obj,
892 struct drm_i915_gem_pwrite *args,
893 struct drm_file *file)
894 {
895 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
896 struct mm_struct *mm = current->mm;
897 struct page **user_pages;
898 ssize_t remain;
899 loff_t offset, pinned_pages, i;
900 loff_t first_data_page, last_data_page, num_pages;
901 int shmem_page_offset;
902 int data_page_index, data_page_offset;
903 int page_length;
904 int ret;
905 uint64_t data_ptr = args->data_ptr;
906 int do_bit17_swizzling;
907
908 remain = args->size;
909
910 /* Pin the user pages containing the data. We can't fault while
911 * holding the struct mutex, and all of the pwrite implementations
912 * want to hold it while dereferencing the user data.
913 */
914 first_data_page = data_ptr / PAGE_SIZE;
915 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
916 num_pages = last_data_page - first_data_page + 1;
917
918 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
919 if (user_pages == NULL)
920 return -ENOMEM;
921
922 mutex_unlock(&dev->struct_mutex);
923 down_read(&mm->mmap_sem);
924 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
925 num_pages, 0, 0, user_pages, NULL);
926 up_read(&mm->mmap_sem);
927 mutex_lock(&dev->struct_mutex);
928 if (pinned_pages < num_pages) {
929 ret = -EFAULT;
930 goto out;
931 }
932
933 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
934 if (ret)
935 goto out;
936
937 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
938
939 offset = args->offset;
940 obj->dirty = 1;
941
942 while (remain > 0) {
943 struct page *page;
944
945 /* Operation in this page
946 *
947 * shmem_page_offset = offset within page in shmem file
948 * data_page_index = page number in get_user_pages return
949 * data_page_offset = offset with data_page_index page.
950 * page_length = bytes to copy for this page
951 */
952 shmem_page_offset = offset & ~PAGE_MASK;
953 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
954 data_page_offset = data_ptr & ~PAGE_MASK;
955
956 page_length = remain;
957 if ((shmem_page_offset + page_length) > PAGE_SIZE)
958 page_length = PAGE_SIZE - shmem_page_offset;
959 if ((data_page_offset + page_length) > PAGE_SIZE)
960 page_length = PAGE_SIZE - data_page_offset;
961
962 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
963 GFP_HIGHUSER | __GFP_RECLAIMABLE);
964 if (IS_ERR(page)) {
965 ret = PTR_ERR(page);
966 goto out;
967 }
968
969 if (do_bit17_swizzling) {
970 slow_shmem_bit17_copy(page,
971 shmem_page_offset,
972 user_pages[data_page_index],
973 data_page_offset,
974 page_length,
975 0);
976 } else {
977 slow_shmem_copy(page,
978 shmem_page_offset,
979 user_pages[data_page_index],
980 data_page_offset,
981 page_length);
982 }
983
984 set_page_dirty(page);
985 mark_page_accessed(page);
986 page_cache_release(page);
987
988 remain -= page_length;
989 data_ptr += page_length;
990 offset += page_length;
991 }
992
993 out:
994 for (i = 0; i < pinned_pages; i++)
995 page_cache_release(user_pages[i]);
996 drm_free_large(user_pages);
997
998 return ret;
999 }
1000
1001 /**
1002 * Writes data to the object referenced by handle.
1003 *
1004 * On error, the contents of the buffer that were to be modified are undefined.
1005 */
1006 int
1007 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1008 struct drm_file *file)
1009 {
1010 struct drm_i915_gem_pwrite *args = data;
1011 struct drm_i915_gem_object *obj;
1012 int ret;
1013
1014 if (args->size == 0)
1015 return 0;
1016
1017 if (!access_ok(VERIFY_READ,
1018 (char __user *)(uintptr_t)args->data_ptr,
1019 args->size))
1020 return -EFAULT;
1021
1022 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1023 args->size);
1024 if (ret)
1025 return -EFAULT;
1026
1027 ret = i915_mutex_lock_interruptible(dev);
1028 if (ret)
1029 return ret;
1030
1031 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1032 if (obj == NULL) {
1033 ret = -ENOENT;
1034 goto unlock;
1035 }
1036
1037 /* Bounds check destination. */
1038 if (args->offset > obj->base.size ||
1039 args->size > obj->base.size - args->offset) {
1040 ret = -EINVAL;
1041 goto out;
1042 }
1043
1044 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1045 * it would end up going through the fenced access, and we'll get
1046 * different detiling behavior between reading and writing.
1047 * pread/pwrite currently are reading and writing from the CPU
1048 * perspective, requiring manual detiling by the client.
1049 */
1050 if (obj->phys_obj)
1051 ret = i915_gem_phys_pwrite(dev, obj, args, file);
1052 else if (obj->tiling_mode == I915_TILING_NONE &&
1053 obj->gtt_space &&
1054 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
1055 ret = i915_gem_object_pin(obj, 0, true);
1056 if (ret)
1057 goto out;
1058
1059 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1060 if (ret)
1061 goto out_unpin;
1062
1063 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1064 if (ret == -EFAULT)
1065 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1066
1067 out_unpin:
1068 i915_gem_object_unpin(obj);
1069 } else {
1070 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1071 if (ret)
1072 goto out;
1073
1074 ret = -EFAULT;
1075 if (!i915_gem_object_needs_bit17_swizzle(obj))
1076 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1077 if (ret == -EFAULT)
1078 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
1079 }
1080
1081 out:
1082 drm_gem_object_unreference(&obj->base);
1083 unlock:
1084 mutex_unlock(&dev->struct_mutex);
1085 return ret;
1086 }
1087
1088 /**
1089 * Called when user space prepares to use an object with the CPU, either
1090 * through the mmap ioctl's mapping or a GTT mapping.
1091 */
1092 int
1093 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1094 struct drm_file *file)
1095 {
1096 struct drm_i915_private *dev_priv = dev->dev_private;
1097 struct drm_i915_gem_set_domain *args = data;
1098 struct drm_i915_gem_object *obj;
1099 uint32_t read_domains = args->read_domains;
1100 uint32_t write_domain = args->write_domain;
1101 int ret;
1102
1103 if (!(dev->driver->driver_features & DRIVER_GEM))
1104 return -ENODEV;
1105
1106 /* Only handle setting domains to types used by the CPU. */
1107 if (write_domain & I915_GEM_GPU_DOMAINS)
1108 return -EINVAL;
1109
1110 if (read_domains & I915_GEM_GPU_DOMAINS)
1111 return -EINVAL;
1112
1113 /* Having something in the write domain implies it's in the read
1114 * domain, and only that read domain. Enforce that in the request.
1115 */
1116 if (write_domain != 0 && read_domains != write_domain)
1117 return -EINVAL;
1118
1119 ret = i915_mutex_lock_interruptible(dev);
1120 if (ret)
1121 return ret;
1122
1123 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1124 if (obj == NULL) {
1125 ret = -ENOENT;
1126 goto unlock;
1127 }
1128
1129 intel_mark_busy(dev, obj);
1130
1131 if (read_domains & I915_GEM_DOMAIN_GTT) {
1132 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1133
1134 /* Update the LRU on the fence for the CPU access that's
1135 * about to occur.
1136 */
1137 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1138 struct drm_i915_fence_reg *reg =
1139 &dev_priv->fence_regs[obj->fence_reg];
1140 list_move_tail(&reg->lru_list,
1141 &dev_priv->mm.fence_list);
1142 }
1143
1144 /* Silently promote "you're not bound, there was nothing to do"
1145 * to success, since the client was just asking us to
1146 * make sure everything was done.
1147 */
1148 if (ret == -EINVAL)
1149 ret = 0;
1150 } else {
1151 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1152 }
1153
1154 /* Maintain LRU order of "inactive" objects */
1155 if (ret == 0 && i915_gem_object_is_inactive(obj))
1156 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1157
1158 drm_gem_object_unreference(&obj->base);
1159 unlock:
1160 mutex_unlock(&dev->struct_mutex);
1161 return ret;
1162 }
1163
1164 /**
1165 * Called when user space has done writes to this buffer
1166 */
1167 int
1168 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1169 struct drm_file *file)
1170 {
1171 struct drm_i915_gem_sw_finish *args = data;
1172 struct drm_i915_gem_object *obj;
1173 int ret = 0;
1174
1175 if (!(dev->driver->driver_features & DRIVER_GEM))
1176 return -ENODEV;
1177
1178 ret = i915_mutex_lock_interruptible(dev);
1179 if (ret)
1180 return ret;
1181
1182 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1183 if (obj == NULL) {
1184 ret = -ENOENT;
1185 goto unlock;
1186 }
1187
1188 /* Pinned buffers may be scanout, so flush the cache */
1189 if (obj->pin_count)
1190 i915_gem_object_flush_cpu_write_domain(obj);
1191
1192 drm_gem_object_unreference(&obj->base);
1193 unlock:
1194 mutex_unlock(&dev->struct_mutex);
1195 return ret;
1196 }
1197
1198 /**
1199 * Maps the contents of an object, returning the address it is mapped
1200 * into.
1201 *
1202 * While the mapping holds a reference on the contents of the object, it doesn't
1203 * imply a ref on the object itself.
1204 */
1205 int
1206 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1207 struct drm_file *file)
1208 {
1209 struct drm_i915_private *dev_priv = dev->dev_private;
1210 struct drm_i915_gem_mmap *args = data;
1211 struct drm_gem_object *obj;
1212 loff_t offset;
1213 unsigned long addr;
1214
1215 if (!(dev->driver->driver_features & DRIVER_GEM))
1216 return -ENODEV;
1217
1218 obj = drm_gem_object_lookup(dev, file, args->handle);
1219 if (obj == NULL)
1220 return -ENOENT;
1221
1222 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1223 drm_gem_object_unreference_unlocked(obj);
1224 return -E2BIG;
1225 }
1226
1227 offset = args->offset;
1228
1229 down_write(&current->mm->mmap_sem);
1230 addr = do_mmap(obj->filp, 0, args->size,
1231 PROT_READ | PROT_WRITE, MAP_SHARED,
1232 args->offset);
1233 up_write(&current->mm->mmap_sem);
1234 drm_gem_object_unreference_unlocked(obj);
1235 if (IS_ERR((void *)addr))
1236 return addr;
1237
1238 args->addr_ptr = (uint64_t) addr;
1239
1240 return 0;
1241 }
1242
1243 /**
1244 * i915_gem_fault - fault a page into the GTT
1245 * vma: VMA in question
1246 * vmf: fault info
1247 *
1248 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1249 * from userspace. The fault handler takes care of binding the object to
1250 * the GTT (if needed), allocating and programming a fence register (again,
1251 * only if needed based on whether the old reg is still valid or the object
1252 * is tiled) and inserting a new PTE into the faulting process.
1253 *
1254 * Note that the faulting process may involve evicting existing objects
1255 * from the GTT and/or fence registers to make room. So performance may
1256 * suffer if the GTT working set is large or there are few fence registers
1257 * left.
1258 */
1259 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1260 {
1261 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1262 struct drm_device *dev = obj->base.dev;
1263 drm_i915_private_t *dev_priv = dev->dev_private;
1264 pgoff_t page_offset;
1265 unsigned long pfn;
1266 int ret = 0;
1267 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1268
1269 /* We don't use vmf->pgoff since that has the fake offset */
1270 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1271 PAGE_SHIFT;
1272
1273 /* Now bind it into the GTT if needed */
1274 mutex_lock(&dev->struct_mutex);
1275 BUG_ON(obj->pin_count && !obj->pin_mappable);
1276
1277 if (obj->gtt_space) {
1278 if (!obj->map_and_fenceable) {
1279 ret = i915_gem_object_unbind(obj);
1280 if (ret)
1281 goto unlock;
1282 }
1283 }
1284
1285 if (!obj->gtt_space) {
1286 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
1287 if (ret)
1288 goto unlock;
1289 }
1290
1291 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1292 if (ret)
1293 goto unlock;
1294
1295 if (!obj->fault_mappable) {
1296 obj->fault_mappable = true;
1297 i915_gem_info_update_mappable(dev_priv, obj, true);
1298 }
1299
1300 /* Need a new fence register? */
1301 if (obj->tiling_mode != I915_TILING_NONE) {
1302 ret = i915_gem_object_get_fence_reg(obj, true);
1303 if (ret)
1304 goto unlock;
1305 }
1306
1307 if (i915_gem_object_is_inactive(obj))
1308 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1309
1310 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
1311 page_offset;
1312
1313 /* Finally, remap it using the new GTT offset */
1314 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1315 unlock:
1316 mutex_unlock(&dev->struct_mutex);
1317
1318 switch (ret) {
1319 case -EAGAIN:
1320 set_need_resched();
1321 case 0:
1322 case -ERESTARTSYS:
1323 return VM_FAULT_NOPAGE;
1324 case -ENOMEM:
1325 return VM_FAULT_OOM;
1326 default:
1327 return VM_FAULT_SIGBUS;
1328 }
1329 }
1330
1331 /**
1332 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1333 * @obj: obj in question
1334 *
1335 * GEM memory mapping works by handing back to userspace a fake mmap offset
1336 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1337 * up the object based on the offset and sets up the various memory mapping
1338 * structures.
1339 *
1340 * This routine allocates and attaches a fake offset for @obj.
1341 */
1342 static int
1343 i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
1344 {
1345 struct drm_device *dev = obj->base.dev;
1346 struct drm_gem_mm *mm = dev->mm_private;
1347 struct drm_map_list *list;
1348 struct drm_local_map *map;
1349 int ret = 0;
1350
1351 /* Set the object up for mmap'ing */
1352 list = &obj->base.map_list;
1353 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
1354 if (!list->map)
1355 return -ENOMEM;
1356
1357 map = list->map;
1358 map->type = _DRM_GEM;
1359 map->size = obj->base.size;
1360 map->handle = obj;
1361
1362 /* Get a DRM GEM mmap offset allocated... */
1363 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1364 obj->base.size / PAGE_SIZE,
1365 0, 0);
1366 if (!list->file_offset_node) {
1367 DRM_ERROR("failed to allocate offset for bo %d\n",
1368 obj->base.name);
1369 ret = -ENOSPC;
1370 goto out_free_list;
1371 }
1372
1373 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1374 obj->base.size / PAGE_SIZE,
1375 0);
1376 if (!list->file_offset_node) {
1377 ret = -ENOMEM;
1378 goto out_free_list;
1379 }
1380
1381 list->hash.key = list->file_offset_node->start;
1382 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1383 if (ret) {
1384 DRM_ERROR("failed to add to map hash\n");
1385 goto out_free_mm;
1386 }
1387
1388 return 0;
1389
1390 out_free_mm:
1391 drm_mm_put_block(list->file_offset_node);
1392 out_free_list:
1393 kfree(list->map);
1394 list->map = NULL;
1395
1396 return ret;
1397 }
1398
1399 /**
1400 * i915_gem_release_mmap - remove physical page mappings
1401 * @obj: obj in question
1402 *
1403 * Preserve the reservation of the mmapping with the DRM core code, but
1404 * relinquish ownership of the pages back to the system.
1405 *
1406 * It is vital that we remove the page mapping if we have mapped a tiled
1407 * object through the GTT and then lose the fence register due to
1408 * resource pressure. Similarly if the object has been moved out of the
1409 * aperture, than pages mapped into userspace must be revoked. Removing the
1410 * mapping will then trigger a page fault on the next user access, allowing
1411 * fixup by i915_gem_fault().
1412 */
1413 void
1414 i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1415 {
1416 struct drm_device *dev = obj->base.dev;
1417 struct drm_i915_private *dev_priv = dev->dev_private;
1418
1419 if (unlikely(obj->base.map_list.map && dev->dev_mapping))
1420 unmap_mapping_range(dev->dev_mapping,
1421 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1422 obj->base.size, 1);
1423
1424 if (obj->fault_mappable) {
1425 obj->fault_mappable = false;
1426 i915_gem_info_update_mappable(dev_priv, obj, false);
1427 }
1428 }
1429
1430 static void
1431 i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
1432 {
1433 struct drm_device *dev = obj->base.dev;
1434 struct drm_gem_mm *mm = dev->mm_private;
1435 struct drm_map_list *list = &obj->base.map_list;
1436
1437 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1438 drm_mm_put_block(list->file_offset_node);
1439 kfree(list->map);
1440 list->map = NULL;
1441 }
1442
1443 static uint32_t
1444 i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1445 {
1446 struct drm_device *dev = obj->base.dev;
1447 uint32_t size;
1448
1449 if (INTEL_INFO(dev)->gen >= 4 ||
1450 obj->tiling_mode == I915_TILING_NONE)
1451 return obj->base.size;
1452
1453 /* Previous chips need a power-of-two fence region when tiling */
1454 if (INTEL_INFO(dev)->gen == 3)
1455 size = 1024*1024;
1456 else
1457 size = 512*1024;
1458
1459 while (size < obj->base.size)
1460 size <<= 1;
1461
1462 return size;
1463 }
1464
1465 /**
1466 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1467 * @obj: object to check
1468 *
1469 * Return the required GTT alignment for an object, taking into account
1470 * potential fence register mapping.
1471 */
1472 static uint32_t
1473 i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
1474 {
1475 struct drm_device *dev = obj->base.dev;
1476
1477 /*
1478 * Minimum alignment is 4k (GTT page size), but might be greater
1479 * if a fence register is needed for the object.
1480 */
1481 if (INTEL_INFO(dev)->gen >= 4 ||
1482 obj->tiling_mode == I915_TILING_NONE)
1483 return 4096;
1484
1485 /*
1486 * Previous chips need to be aligned to the size of the smallest
1487 * fence register that can contain the object.
1488 */
1489 return i915_gem_get_gtt_size(obj);
1490 }
1491
1492 /**
1493 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1494 * unfenced object
1495 * @obj: object to check
1496 *
1497 * Return the required GTT alignment for an object, only taking into account
1498 * unfenced tiled surface requirements.
1499 */
1500 static uint32_t
1501 i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
1502 {
1503 struct drm_device *dev = obj->base.dev;
1504 int tile_height;
1505
1506 /*
1507 * Minimum alignment is 4k (GTT page size) for sane hw.
1508 */
1509 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1510 obj->tiling_mode == I915_TILING_NONE)
1511 return 4096;
1512
1513 /*
1514 * Older chips need unfenced tiled buffers to be aligned to the left
1515 * edge of an even tile row (where tile rows are counted as if the bo is
1516 * placed in a fenced gtt region).
1517 */
1518 if (IS_GEN2(dev) ||
1519 (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
1520 tile_height = 32;
1521 else
1522 tile_height = 8;
1523
1524 return tile_height * obj->stride * 2;
1525 }
1526
1527 /**
1528 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1529 * @dev: DRM device
1530 * @data: GTT mapping ioctl data
1531 * @file: GEM object info
1532 *
1533 * Simply returns the fake offset to userspace so it can mmap it.
1534 * The mmap call will end up in drm_gem_mmap(), which will set things
1535 * up so we can get faults in the handler above.
1536 *
1537 * The fault handler will take care of binding the object into the GTT
1538 * (since it may have been evicted to make room for something), allocating
1539 * a fence register, and mapping the appropriate aperture address into
1540 * userspace.
1541 */
1542 int
1543 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1544 struct drm_file *file)
1545 {
1546 struct drm_i915_private *dev_priv = dev->dev_private;
1547 struct drm_i915_gem_mmap_gtt *args = data;
1548 struct drm_i915_gem_object *obj;
1549 int ret;
1550
1551 if (!(dev->driver->driver_features & DRIVER_GEM))
1552 return -ENODEV;
1553
1554 ret = i915_mutex_lock_interruptible(dev);
1555 if (ret)
1556 return ret;
1557
1558 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1559 if (obj == NULL) {
1560 ret = -ENOENT;
1561 goto unlock;
1562 }
1563
1564 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
1565 ret = -E2BIG;
1566 goto unlock;
1567 }
1568
1569 if (obj->madv != I915_MADV_WILLNEED) {
1570 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1571 ret = -EINVAL;
1572 goto out;
1573 }
1574
1575 if (!obj->base.map_list.map) {
1576 ret = i915_gem_create_mmap_offset(obj);
1577 if (ret)
1578 goto out;
1579 }
1580
1581 args->offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
1582
1583 out:
1584 drm_gem_object_unreference(&obj->base);
1585 unlock:
1586 mutex_unlock(&dev->struct_mutex);
1587 return ret;
1588 }
1589
1590 static int
1591 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
1592 gfp_t gfpmask)
1593 {
1594 int page_count, i;
1595 struct address_space *mapping;
1596 struct inode *inode;
1597 struct page *page;
1598
1599 /* Get the list of pages out of our struct file. They'll be pinned
1600 * at this point until we release them.
1601 */
1602 page_count = obj->base.size / PAGE_SIZE;
1603 BUG_ON(obj->pages != NULL);
1604 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1605 if (obj->pages == NULL)
1606 return -ENOMEM;
1607
1608 inode = obj->base.filp->f_path.dentry->d_inode;
1609 mapping = inode->i_mapping;
1610 for (i = 0; i < page_count; i++) {
1611 page = read_cache_page_gfp(mapping, i,
1612 GFP_HIGHUSER |
1613 __GFP_COLD |
1614 __GFP_RECLAIMABLE |
1615 gfpmask);
1616 if (IS_ERR(page))
1617 goto err_pages;
1618
1619 obj->pages[i] = page;
1620 }
1621
1622 if (obj->tiling_mode != I915_TILING_NONE)
1623 i915_gem_object_do_bit_17_swizzle(obj);
1624
1625 return 0;
1626
1627 err_pages:
1628 while (i--)
1629 page_cache_release(obj->pages[i]);
1630
1631 drm_free_large(obj->pages);
1632 obj->pages = NULL;
1633 return PTR_ERR(page);
1634 }
1635
1636 static void
1637 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1638 {
1639 int page_count = obj->base.size / PAGE_SIZE;
1640 int i;
1641
1642 BUG_ON(obj->madv == __I915_MADV_PURGED);
1643
1644 if (obj->tiling_mode != I915_TILING_NONE)
1645 i915_gem_object_save_bit_17_swizzle(obj);
1646
1647 if (obj->madv == I915_MADV_DONTNEED)
1648 obj->dirty = 0;
1649
1650 for (i = 0; i < page_count; i++) {
1651 if (obj->dirty)
1652 set_page_dirty(obj->pages[i]);
1653
1654 if (obj->madv == I915_MADV_WILLNEED)
1655 mark_page_accessed(obj->pages[i]);
1656
1657 page_cache_release(obj->pages[i]);
1658 }
1659 obj->dirty = 0;
1660
1661 drm_free_large(obj->pages);
1662 obj->pages = NULL;
1663 }
1664
1665 static uint32_t
1666 i915_gem_next_request_seqno(struct drm_device *dev,
1667 struct intel_ring_buffer *ring)
1668 {
1669 drm_i915_private_t *dev_priv = dev->dev_private;
1670 return ring->outstanding_lazy_request = dev_priv->next_seqno;
1671 }
1672
1673 static void
1674 i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
1675 struct intel_ring_buffer *ring)
1676 {
1677 struct drm_device *dev = obj->base.dev;
1678 struct drm_i915_private *dev_priv = dev->dev_private;
1679 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
1680
1681 BUG_ON(ring == NULL);
1682 obj->ring = ring;
1683
1684 /* Add a reference if we're newly entering the active list. */
1685 if (!obj->active) {
1686 drm_gem_object_reference(&obj->base);
1687 obj->active = 1;
1688 }
1689
1690 /* Move from whatever list we were on to the tail of execution. */
1691 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1692 list_move_tail(&obj->ring_list, &ring->active_list);
1693 obj->last_rendering_seqno = seqno;
1694 }
1695
1696 static void
1697 i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
1698 {
1699 struct drm_device *dev = obj->base.dev;
1700 drm_i915_private_t *dev_priv = dev->dev_private;
1701
1702 BUG_ON(!obj->active);
1703 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
1704 list_del_init(&obj->ring_list);
1705 obj->last_rendering_seqno = 0;
1706 }
1707
1708 /* Immediately discard the backing storage */
1709 static void
1710 i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1711 {
1712 struct inode *inode;
1713
1714 /* Our goal here is to return as much of the memory as
1715 * is possible back to the system as we are called from OOM.
1716 * To do this we must instruct the shmfs to drop all of its
1717 * backing pages, *now*. Here we mirror the actions taken
1718 * when by shmem_delete_inode() to release the backing store.
1719 */
1720 inode = obj->base.filp->f_path.dentry->d_inode;
1721 truncate_inode_pages(inode->i_mapping, 0);
1722 if (inode->i_op->truncate_range)
1723 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
1724
1725 obj->madv = __I915_MADV_PURGED;
1726 }
1727
1728 static inline int
1729 i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1730 {
1731 return obj->madv == I915_MADV_DONTNEED;
1732 }
1733
1734 static void
1735 i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1736 {
1737 struct drm_device *dev = obj->base.dev;
1738 drm_i915_private_t *dev_priv = dev->dev_private;
1739
1740 if (obj->pin_count != 0)
1741 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1742 else
1743 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1744 list_del_init(&obj->ring_list);
1745
1746 BUG_ON(!list_empty(&obj->gpu_write_list));
1747
1748 obj->last_rendering_seqno = 0;
1749 obj->ring = NULL;
1750 if (obj->active) {
1751 obj->active = 0;
1752 drm_gem_object_unreference(&obj->base);
1753 }
1754 WARN_ON(i915_verify_lists(dev));
1755 }
1756
1757 static void
1758 i915_gem_process_flushing_list(struct drm_device *dev,
1759 uint32_t flush_domains,
1760 struct intel_ring_buffer *ring)
1761 {
1762 drm_i915_private_t *dev_priv = dev->dev_private;
1763 struct drm_i915_gem_object *obj, *next;
1764
1765 list_for_each_entry_safe(obj, next,
1766 &ring->gpu_write_list,
1767 gpu_write_list) {
1768 if (obj->base.write_domain & flush_domains) {
1769 uint32_t old_write_domain = obj->base.write_domain;
1770
1771 obj->base.write_domain = 0;
1772 list_del_init(&obj->gpu_write_list);
1773 i915_gem_object_move_to_active(obj, ring);
1774
1775 /* update the fence lru list */
1776 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1777 struct drm_i915_fence_reg *reg =
1778 &dev_priv->fence_regs[obj->fence_reg];
1779 list_move_tail(&reg->lru_list,
1780 &dev_priv->mm.fence_list);
1781 }
1782
1783 trace_i915_gem_object_change_domain(obj,
1784 obj->base.read_domains,
1785 old_write_domain);
1786 }
1787 }
1788 }
1789
1790 int
1791 i915_add_request(struct drm_device *dev,
1792 struct drm_file *file,
1793 struct drm_i915_gem_request *request,
1794 struct intel_ring_buffer *ring)
1795 {
1796 drm_i915_private_t *dev_priv = dev->dev_private;
1797 struct drm_i915_file_private *file_priv = NULL;
1798 uint32_t seqno;
1799 int was_empty;
1800 int ret;
1801
1802 BUG_ON(request == NULL);
1803
1804 if (file != NULL)
1805 file_priv = file->driver_priv;
1806
1807 ret = ring->add_request(ring, &seqno);
1808 if (ret)
1809 return ret;
1810
1811 ring->outstanding_lazy_request = false;
1812
1813 request->seqno = seqno;
1814 request->ring = ring;
1815 request->emitted_jiffies = jiffies;
1816 was_empty = list_empty(&ring->request_list);
1817 list_add_tail(&request->list, &ring->request_list);
1818
1819 if (file_priv) {
1820 spin_lock(&file_priv->mm.lock);
1821 request->file_priv = file_priv;
1822 list_add_tail(&request->client_list,
1823 &file_priv->mm.request_list);
1824 spin_unlock(&file_priv->mm.lock);
1825 }
1826
1827 if (!dev_priv->mm.suspended) {
1828 mod_timer(&dev_priv->hangcheck_timer,
1829 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1830 if (was_empty)
1831 queue_delayed_work(dev_priv->wq,
1832 &dev_priv->mm.retire_work, HZ);
1833 }
1834 return 0;
1835 }
1836
1837 /**
1838 * Command execution barrier
1839 *
1840 * Ensures that all commands in the ring are finished
1841 * before signalling the CPU
1842 */
1843 static void
1844 i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
1845 {
1846 uint32_t flush_domains = 0;
1847
1848 /* The sampler always gets flushed on i965 (sigh) */
1849 if (INTEL_INFO(dev)->gen >= 4)
1850 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1851
1852 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
1853 }
1854
1855 static inline void
1856 i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1857 {
1858 struct drm_i915_file_private *file_priv = request->file_priv;
1859
1860 if (!file_priv)
1861 return;
1862
1863 spin_lock(&file_priv->mm.lock);
1864 list_del(&request->client_list);
1865 request->file_priv = NULL;
1866 spin_unlock(&file_priv->mm.lock);
1867 }
1868
1869 static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1870 struct intel_ring_buffer *ring)
1871 {
1872 while (!list_empty(&ring->request_list)) {
1873 struct drm_i915_gem_request *request;
1874
1875 request = list_first_entry(&ring->request_list,
1876 struct drm_i915_gem_request,
1877 list);
1878
1879 list_del(&request->list);
1880 i915_gem_request_remove_from_client(request);
1881 kfree(request);
1882 }
1883
1884 while (!list_empty(&ring->active_list)) {
1885 struct drm_i915_gem_object *obj;
1886
1887 obj = list_first_entry(&ring->active_list,
1888 struct drm_i915_gem_object,
1889 ring_list);
1890
1891 obj->base.write_domain = 0;
1892 list_del_init(&obj->gpu_write_list);
1893 i915_gem_object_move_to_inactive(obj);
1894 }
1895 }
1896
1897 void i915_gem_reset(struct drm_device *dev)
1898 {
1899 struct drm_i915_private *dev_priv = dev->dev_private;
1900 struct drm_i915_gem_object *obj;
1901 int i;
1902
1903 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
1904 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
1905 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
1906
1907 /* Remove anything from the flushing lists. The GPU cache is likely
1908 * to be lost on reset along with the data, so simply move the
1909 * lost bo to the inactive list.
1910 */
1911 while (!list_empty(&dev_priv->mm.flushing_list)) {
1912 obj= list_first_entry(&dev_priv->mm.flushing_list,
1913 struct drm_i915_gem_object,
1914 mm_list);
1915
1916 obj->base.write_domain = 0;
1917 list_del_init(&obj->gpu_write_list);
1918 i915_gem_object_move_to_inactive(obj);
1919 }
1920
1921 /* Move everything out of the GPU domains to ensure we do any
1922 * necessary invalidation upon reuse.
1923 */
1924 list_for_each_entry(obj,
1925 &dev_priv->mm.inactive_list,
1926 mm_list)
1927 {
1928 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1929 }
1930
1931 /* The fence registers are invalidated so clear them out */
1932 for (i = 0; i < 16; i++) {
1933 struct drm_i915_fence_reg *reg;
1934
1935 reg = &dev_priv->fence_regs[i];
1936 if (!reg->obj)
1937 continue;
1938
1939 i915_gem_clear_fence_reg(reg->obj);
1940 }
1941 }
1942
1943 /**
1944 * This function clears the request list as sequence numbers are passed.
1945 */
1946 static void
1947 i915_gem_retire_requests_ring(struct drm_device *dev,
1948 struct intel_ring_buffer *ring)
1949 {
1950 drm_i915_private_t *dev_priv = dev->dev_private;
1951 uint32_t seqno;
1952
1953 if (!ring->status_page.page_addr ||
1954 list_empty(&ring->request_list))
1955 return;
1956
1957 WARN_ON(i915_verify_lists(dev));
1958
1959 seqno = ring->get_seqno(ring);
1960 while (!list_empty(&ring->request_list)) {
1961 struct drm_i915_gem_request *request;
1962
1963 request = list_first_entry(&ring->request_list,
1964 struct drm_i915_gem_request,
1965 list);
1966
1967 if (!i915_seqno_passed(seqno, request->seqno))
1968 break;
1969
1970 trace_i915_gem_request_retire(dev, request->seqno);
1971
1972 list_del(&request->list);
1973 i915_gem_request_remove_from_client(request);
1974 kfree(request);
1975 }
1976
1977 /* Move any buffers on the active list that are no longer referenced
1978 * by the ringbuffer to the flushing/inactive lists as appropriate.
1979 */
1980 while (!list_empty(&ring->active_list)) {
1981 struct drm_i915_gem_object *obj;
1982
1983 obj= list_first_entry(&ring->active_list,
1984 struct drm_i915_gem_object,
1985 ring_list);
1986
1987 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
1988 break;
1989
1990 if (obj->base.write_domain != 0)
1991 i915_gem_object_move_to_flushing(obj);
1992 else
1993 i915_gem_object_move_to_inactive(obj);
1994 }
1995
1996 if (unlikely (dev_priv->trace_irq_seqno &&
1997 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
1998 ring->user_irq_put(ring);
1999 dev_priv->trace_irq_seqno = 0;
2000 }
2001
2002 WARN_ON(i915_verify_lists(dev));
2003 }
2004
2005 void
2006 i915_gem_retire_requests(struct drm_device *dev)
2007 {
2008 drm_i915_private_t *dev_priv = dev->dev_private;
2009
2010 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
2011 struct drm_i915_gem_object *obj, *next;
2012
2013 /* We must be careful that during unbind() we do not
2014 * accidentally infinitely recurse into retire requests.
2015 * Currently:
2016 * retire -> free -> unbind -> wait -> retire_ring
2017 */
2018 list_for_each_entry_safe(obj, next,
2019 &dev_priv->mm.deferred_free_list,
2020 mm_list)
2021 i915_gem_free_object_tail(obj);
2022 }
2023
2024 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
2025 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
2026 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
2027 }
2028
2029 static void
2030 i915_gem_retire_work_handler(struct work_struct *work)
2031 {
2032 drm_i915_private_t *dev_priv;
2033 struct drm_device *dev;
2034
2035 dev_priv = container_of(work, drm_i915_private_t,
2036 mm.retire_work.work);
2037 dev = dev_priv->dev;
2038
2039 /* Come back later if the device is busy... */
2040 if (!mutex_trylock(&dev->struct_mutex)) {
2041 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2042 return;
2043 }
2044
2045 i915_gem_retire_requests(dev);
2046
2047 if (!dev_priv->mm.suspended &&
2048 (!list_empty(&dev_priv->render_ring.request_list) ||
2049 !list_empty(&dev_priv->bsd_ring.request_list) ||
2050 !list_empty(&dev_priv->blt_ring.request_list)))
2051 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2052 mutex_unlock(&dev->struct_mutex);
2053 }
2054
2055 int
2056 i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
2057 bool interruptible, struct intel_ring_buffer *ring)
2058 {
2059 drm_i915_private_t *dev_priv = dev->dev_private;
2060 u32 ier;
2061 int ret = 0;
2062
2063 BUG_ON(seqno == 0);
2064
2065 if (atomic_read(&dev_priv->mm.wedged))
2066 return -EAGAIN;
2067
2068 if (seqno == ring->outstanding_lazy_request) {
2069 struct drm_i915_gem_request *request;
2070
2071 request = kzalloc(sizeof(*request), GFP_KERNEL);
2072 if (request == NULL)
2073 return -ENOMEM;
2074
2075 ret = i915_add_request(dev, NULL, request, ring);
2076 if (ret) {
2077 kfree(request);
2078 return ret;
2079 }
2080
2081 seqno = request->seqno;
2082 }
2083
2084 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
2085 if (HAS_PCH_SPLIT(dev))
2086 ier = I915_READ(DEIER) | I915_READ(GTIER);
2087 else
2088 ier = I915_READ(IER);
2089 if (!ier) {
2090 DRM_ERROR("something (likely vbetool) disabled "
2091 "interrupts, re-enabling\n");
2092 i915_driver_irq_preinstall(dev);
2093 i915_driver_irq_postinstall(dev);
2094 }
2095
2096 trace_i915_gem_request_wait_begin(dev, seqno);
2097
2098 ring->waiting_seqno = seqno;
2099 ring->user_irq_get(ring);
2100 if (interruptible)
2101 ret = wait_event_interruptible(ring->irq_queue,
2102 i915_seqno_passed(ring->get_seqno(ring), seqno)
2103 || atomic_read(&dev_priv->mm.wedged));
2104 else
2105 wait_event(ring->irq_queue,
2106 i915_seqno_passed(ring->get_seqno(ring), seqno)
2107 || atomic_read(&dev_priv->mm.wedged));
2108
2109 ring->user_irq_put(ring);
2110 ring->waiting_seqno = 0;
2111
2112 trace_i915_gem_request_wait_end(dev, seqno);
2113 }
2114 if (atomic_read(&dev_priv->mm.wedged))
2115 ret = -EAGAIN;
2116
2117 if (ret && ret != -ERESTARTSYS)
2118 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
2119 __func__, ret, seqno, ring->get_seqno(ring),
2120 dev_priv->next_seqno);
2121
2122 /* Directly dispatch request retiring. While we have the work queue
2123 * to handle this, the waiter on a request often wants an associated
2124 * buffer to have made it to the inactive list, and we would need
2125 * a separate wait queue to handle that.
2126 */
2127 if (ret == 0)
2128 i915_gem_retire_requests_ring(dev, ring);
2129
2130 return ret;
2131 }
2132
2133 /**
2134 * Waits for a sequence number to be signaled, and cleans up the
2135 * request and object lists appropriately for that event.
2136 */
2137 static int
2138 i915_wait_request(struct drm_device *dev, uint32_t seqno,
2139 struct intel_ring_buffer *ring)
2140 {
2141 return i915_do_wait_request(dev, seqno, 1, ring);
2142 }
2143
2144 static void
2145 i915_gem_flush_ring(struct drm_device *dev,
2146 struct intel_ring_buffer *ring,
2147 uint32_t invalidate_domains,
2148 uint32_t flush_domains)
2149 {
2150 ring->flush(ring, invalidate_domains, flush_domains);
2151 i915_gem_process_flushing_list(dev, flush_domains, ring);
2152 }
2153
2154 static void
2155 i915_gem_flush(struct drm_device *dev,
2156 uint32_t invalidate_domains,
2157 uint32_t flush_domains,
2158 uint32_t flush_rings)
2159 {
2160 drm_i915_private_t *dev_priv = dev->dev_private;
2161
2162 if (flush_domains & I915_GEM_DOMAIN_CPU)
2163 intel_gtt_chipset_flush();
2164
2165 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2166 if (flush_rings & RING_RENDER)
2167 i915_gem_flush_ring(dev, &dev_priv->render_ring,
2168 invalidate_domains, flush_domains);
2169 if (flush_rings & RING_BSD)
2170 i915_gem_flush_ring(dev, &dev_priv->bsd_ring,
2171 invalidate_domains, flush_domains);
2172 if (flush_rings & RING_BLT)
2173 i915_gem_flush_ring(dev, &dev_priv->blt_ring,
2174 invalidate_domains, flush_domains);
2175 }
2176 }
2177
2178 /**
2179 * Ensures that all rendering to the object has completed and the object is
2180 * safe to unbind from the GTT or access from the CPU.
2181 */
2182 static int
2183 i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
2184 bool interruptible)
2185 {
2186 struct drm_device *dev = obj->base.dev;
2187 int ret;
2188
2189 /* This function only exists to support waiting for existing rendering,
2190 * not for emitting required flushes.
2191 */
2192 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
2193
2194 /* If there is rendering queued on the buffer being evicted, wait for
2195 * it.
2196 */
2197 if (obj->active) {
2198 ret = i915_do_wait_request(dev,
2199 obj->last_rendering_seqno,
2200 interruptible,
2201 obj->ring);
2202 if (ret)
2203 return ret;
2204 }
2205
2206 return 0;
2207 }
2208
2209 /**
2210 * Unbinds an object from the GTT aperture.
2211 */
2212 int
2213 i915_gem_object_unbind(struct drm_i915_gem_object *obj)
2214 {
2215 struct drm_device *dev = obj->base.dev;
2216 struct drm_i915_private *dev_priv = dev->dev_private;
2217 int ret = 0;
2218
2219 if (obj->gtt_space == NULL)
2220 return 0;
2221
2222 if (obj->pin_count != 0) {
2223 DRM_ERROR("Attempting to unbind pinned buffer\n");
2224 return -EINVAL;
2225 }
2226
2227 /* blow away mappings if mapped through GTT */
2228 i915_gem_release_mmap(obj);
2229
2230 /* Move the object to the CPU domain to ensure that
2231 * any possible CPU writes while it's not in the GTT
2232 * are flushed when we go to remap it. This will
2233 * also ensure that all pending GPU writes are finished
2234 * before we unbind.
2235 */
2236 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2237 if (ret == -ERESTARTSYS)
2238 return ret;
2239 /* Continue on if we fail due to EIO, the GPU is hung so we
2240 * should be safe and we need to cleanup or else we might
2241 * cause memory corruption through use-after-free.
2242 */
2243 if (ret) {
2244 i915_gem_clflush_object(obj);
2245 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2246 }
2247
2248 /* release the fence reg _after_ flushing */
2249 if (obj->fence_reg != I915_FENCE_REG_NONE)
2250 i915_gem_clear_fence_reg(obj);
2251
2252 i915_gem_gtt_unbind_object(obj);
2253
2254 i915_gem_object_put_pages_gtt(obj);
2255
2256 i915_gem_info_remove_gtt(dev_priv, obj);
2257 list_del_init(&obj->mm_list);
2258 /* Avoid an unnecessary call to unbind on rebind. */
2259 obj->map_and_fenceable = true;
2260
2261 drm_mm_put_block(obj->gtt_space);
2262 obj->gtt_space = NULL;
2263 obj->gtt_offset = 0;
2264
2265 if (i915_gem_object_is_purgeable(obj))
2266 i915_gem_object_truncate(obj);
2267
2268 trace_i915_gem_object_unbind(obj);
2269
2270 return ret;
2271 }
2272
2273 static int i915_ring_idle(struct drm_device *dev,
2274 struct intel_ring_buffer *ring)
2275 {
2276 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
2277 return 0;
2278
2279 i915_gem_flush_ring(dev, ring,
2280 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2281 return i915_wait_request(dev,
2282 i915_gem_next_request_seqno(dev, ring),
2283 ring);
2284 }
2285
2286 int
2287 i915_gpu_idle(struct drm_device *dev)
2288 {
2289 drm_i915_private_t *dev_priv = dev->dev_private;
2290 bool lists_empty;
2291 int ret;
2292
2293 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2294 list_empty(&dev_priv->mm.active_list));
2295 if (lists_empty)
2296 return 0;
2297
2298 /* Flush everything onto the inactive list. */
2299 ret = i915_ring_idle(dev, &dev_priv->render_ring);
2300 if (ret)
2301 return ret;
2302
2303 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2304 if (ret)
2305 return ret;
2306
2307 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2308 if (ret)
2309 return ret;
2310
2311 return 0;
2312 }
2313
2314 static void sandybridge_write_fence_reg(struct drm_i915_gem_object *obj)
2315 {
2316 struct drm_device *dev = obj->base.dev;
2317 drm_i915_private_t *dev_priv = dev->dev_private;
2318 u32 size = obj->gtt_space->size;
2319 int regnum = obj->fence_reg;
2320 uint64_t val;
2321
2322 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2323 0xfffff000) << 32;
2324 val |= obj->gtt_offset & 0xfffff000;
2325 val |= (uint64_t)((obj->stride / 128) - 1) <<
2326 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2327
2328 if (obj->tiling_mode == I915_TILING_Y)
2329 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2330 val |= I965_FENCE_REG_VALID;
2331
2332 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2333 }
2334
2335 static void i965_write_fence_reg(struct drm_i915_gem_object *obj)
2336 {
2337 struct drm_device *dev = obj->base.dev;
2338 drm_i915_private_t *dev_priv = dev->dev_private;
2339 u32 size = obj->gtt_space->size;
2340 int regnum = obj->fence_reg;
2341 uint64_t val;
2342
2343 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2344 0xfffff000) << 32;
2345 val |= obj->gtt_offset & 0xfffff000;
2346 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2347 if (obj->tiling_mode == I915_TILING_Y)
2348 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2349 val |= I965_FENCE_REG_VALID;
2350
2351 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2352 }
2353
2354 static void i915_write_fence_reg(struct drm_i915_gem_object *obj)
2355 {
2356 struct drm_device *dev = obj->base.dev;
2357 drm_i915_private_t *dev_priv = dev->dev_private;
2358 u32 size = obj->gtt_space->size;
2359 uint32_t fence_reg, val, pitch_val;
2360 int tile_width;
2361
2362 if ((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2363 (obj->gtt_offset & (size - 1))) {
2364 WARN(1, "%s: object 0x%08x [fenceable? %d] not 1M or size (0x%08x) aligned [gtt_space offset=%lx, size=%lx]\n",
2365 __func__, obj->gtt_offset, obj->map_and_fenceable, size,
2366 obj->gtt_space->start, obj->gtt_space->size);
2367 return;
2368 }
2369
2370 if (obj->tiling_mode == I915_TILING_Y &&
2371 HAS_128_BYTE_Y_TILING(dev))
2372 tile_width = 128;
2373 else
2374 tile_width = 512;
2375
2376 /* Note: pitch better be a power of two tile widths */
2377 pitch_val = obj->stride / tile_width;
2378 pitch_val = ffs(pitch_val) - 1;
2379
2380 if (obj->tiling_mode == I915_TILING_Y &&
2381 HAS_128_BYTE_Y_TILING(dev))
2382 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2383 else
2384 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2385
2386 val = obj->gtt_offset;
2387 if (obj->tiling_mode == I915_TILING_Y)
2388 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2389 val |= I915_FENCE_SIZE_BITS(size);
2390 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2391 val |= I830_FENCE_REG_VALID;
2392
2393 fence_reg = obj->fence_reg;
2394 if (fence_reg < 8)
2395 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2396 else
2397 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2398 I915_WRITE(fence_reg, val);
2399 }
2400
2401 static void i830_write_fence_reg(struct drm_i915_gem_object *obj)
2402 {
2403 struct drm_device *dev = obj->base.dev;
2404 drm_i915_private_t *dev_priv = dev->dev_private;
2405 u32 size = obj->gtt_space->size;
2406 int regnum = obj->fence_reg;
2407 uint32_t val;
2408 uint32_t pitch_val;
2409 uint32_t fence_size_bits;
2410
2411 if ((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2412 (obj->gtt_offset & (obj->base.size - 1))) {
2413 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
2414 __func__, obj->gtt_offset);
2415 return;
2416 }
2417
2418 pitch_val = obj->stride / 128;
2419 pitch_val = ffs(pitch_val) - 1;
2420 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2421
2422 val = obj->gtt_offset;
2423 if (obj->tiling_mode == I915_TILING_Y)
2424 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2425 fence_size_bits = I830_FENCE_SIZE_BITS(size);
2426 WARN_ON(fence_size_bits & ~0x00000f00);
2427 val |= fence_size_bits;
2428 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2429 val |= I830_FENCE_REG_VALID;
2430
2431 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
2432 }
2433
2434 static int i915_find_fence_reg(struct drm_device *dev,
2435 bool interruptible)
2436 {
2437 struct drm_i915_private *dev_priv = dev->dev_private;
2438 struct drm_i915_fence_reg *reg;
2439 struct drm_i915_gem_object *obj = NULL;
2440 int i, avail, ret;
2441
2442 /* First try to find a free reg */
2443 avail = 0;
2444 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2445 reg = &dev_priv->fence_regs[i];
2446 if (!reg->obj)
2447 return i;
2448
2449 if (!reg->obj->pin_count)
2450 avail++;
2451 }
2452
2453 if (avail == 0)
2454 return -ENOSPC;
2455
2456 /* None available, try to steal one or wait for a user to finish */
2457 avail = I915_FENCE_REG_NONE;
2458 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2459 lru_list) {
2460 obj = reg->obj;
2461 if (obj->pin_count)
2462 continue;
2463
2464 /* found one! */
2465 avail = obj->fence_reg;
2466 break;
2467 }
2468
2469 BUG_ON(avail == I915_FENCE_REG_NONE);
2470
2471 /* We only have a reference on obj from the active list. put_fence_reg
2472 * might drop that one, causing a use-after-free in it. So hold a
2473 * private reference to obj like the other callers of put_fence_reg
2474 * (set_tiling ioctl) do. */
2475 drm_gem_object_reference(&obj->base);
2476 ret = i915_gem_object_put_fence_reg(obj, interruptible);
2477 drm_gem_object_unreference(&obj->base);
2478 if (ret != 0)
2479 return ret;
2480
2481 return avail;
2482 }
2483
2484 /**
2485 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2486 * @obj: object to map through a fence reg
2487 *
2488 * When mapping objects through the GTT, userspace wants to be able to write
2489 * to them without having to worry about swizzling if the object is tiled.
2490 *
2491 * This function walks the fence regs looking for a free one for @obj,
2492 * stealing one if it can't find any.
2493 *
2494 * It then sets up the reg based on the object's properties: address, pitch
2495 * and tiling format.
2496 */
2497 int
2498 i915_gem_object_get_fence_reg(struct drm_i915_gem_object *obj,
2499 bool interruptible)
2500 {
2501 struct drm_device *dev = obj->base.dev;
2502 struct drm_i915_private *dev_priv = dev->dev_private;
2503 struct drm_i915_fence_reg *reg = NULL;
2504 int ret;
2505
2506 /* Just update our place in the LRU if our fence is getting used. */
2507 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2508 reg = &dev_priv->fence_regs[obj->fence_reg];
2509 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2510 return 0;
2511 }
2512
2513 switch (obj->tiling_mode) {
2514 case I915_TILING_NONE:
2515 WARN(1, "allocating a fence for non-tiled object?\n");
2516 break;
2517 case I915_TILING_X:
2518 if (!obj->stride)
2519 return -EINVAL;
2520 WARN((obj->stride & (512 - 1)),
2521 "object 0x%08x is X tiled but has non-512B pitch\n",
2522 obj->gtt_offset);
2523 break;
2524 case I915_TILING_Y:
2525 if (!obj->stride)
2526 return -EINVAL;
2527 WARN((obj->stride & (128 - 1)),
2528 "object 0x%08x is Y tiled but has non-128B pitch\n",
2529 obj->gtt_offset);
2530 break;
2531 }
2532
2533 ret = i915_find_fence_reg(dev, interruptible);
2534 if (ret < 0)
2535 return ret;
2536
2537 obj->fence_reg = ret;
2538 reg = &dev_priv->fence_regs[obj->fence_reg];
2539 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2540
2541 reg->obj = obj;
2542
2543 switch (INTEL_INFO(dev)->gen) {
2544 case 6:
2545 sandybridge_write_fence_reg(obj);
2546 break;
2547 case 5:
2548 case 4:
2549 i965_write_fence_reg(obj);
2550 break;
2551 case 3:
2552 i915_write_fence_reg(obj);
2553 break;
2554 case 2:
2555 i830_write_fence_reg(obj);
2556 break;
2557 }
2558
2559 trace_i915_gem_object_get_fence(obj,
2560 obj->fence_reg,
2561 obj->tiling_mode);
2562
2563 return 0;
2564 }
2565
2566 /**
2567 * i915_gem_clear_fence_reg - clear out fence register info
2568 * @obj: object to clear
2569 *
2570 * Zeroes out the fence register itself and clears out the associated
2571 * data structures in dev_priv and obj.
2572 */
2573 static void
2574 i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj)
2575 {
2576 struct drm_device *dev = obj->base.dev;
2577 drm_i915_private_t *dev_priv = dev->dev_private;
2578 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[obj->fence_reg];
2579 uint32_t fence_reg;
2580
2581 switch (INTEL_INFO(dev)->gen) {
2582 case 6:
2583 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2584 (obj->fence_reg * 8), 0);
2585 break;
2586 case 5:
2587 case 4:
2588 I915_WRITE64(FENCE_REG_965_0 + (obj->fence_reg * 8), 0);
2589 break;
2590 case 3:
2591 if (obj->fence_reg >= 8)
2592 fence_reg = FENCE_REG_945_8 + (obj->fence_reg - 8) * 4;
2593 else
2594 case 2:
2595 fence_reg = FENCE_REG_830_0 + obj->fence_reg * 4;
2596
2597 I915_WRITE(fence_reg, 0);
2598 break;
2599 }
2600
2601 reg->obj = NULL;
2602 obj->fence_reg = I915_FENCE_REG_NONE;
2603 list_del_init(&reg->lru_list);
2604 }
2605
2606 /**
2607 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2608 * to the buffer to finish, and then resets the fence register.
2609 * @obj: tiled object holding a fence register.
2610 * @bool: whether the wait upon the fence is interruptible
2611 *
2612 * Zeroes out the fence register itself and clears out the associated
2613 * data structures in dev_priv and obj.
2614 */
2615 int
2616 i915_gem_object_put_fence_reg(struct drm_i915_gem_object *obj,
2617 bool interruptible)
2618 {
2619 struct drm_device *dev = obj->base.dev;
2620 struct drm_i915_private *dev_priv = dev->dev_private;
2621 struct drm_i915_fence_reg *reg;
2622
2623 if (obj->fence_reg == I915_FENCE_REG_NONE)
2624 return 0;
2625
2626 /* If we've changed tiling, GTT-mappings of the object
2627 * need to re-fault to ensure that the correct fence register
2628 * setup is in place.
2629 */
2630 i915_gem_release_mmap(obj);
2631
2632 /* On the i915, GPU access to tiled buffers is via a fence,
2633 * therefore we must wait for any outstanding access to complete
2634 * before clearing the fence.
2635 */
2636 reg = &dev_priv->fence_regs[obj->fence_reg];
2637 if (reg->gpu) {
2638 int ret;
2639
2640 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
2641 if (ret)
2642 return ret;
2643
2644 ret = i915_gem_object_wait_rendering(obj, interruptible);
2645 if (ret)
2646 return ret;
2647
2648 reg->gpu = false;
2649 }
2650
2651 i915_gem_object_flush_gtt_write_domain(obj);
2652 i915_gem_clear_fence_reg(obj);
2653
2654 return 0;
2655 }
2656
2657 /**
2658 * Finds free space in the GTT aperture and binds the object there.
2659 */
2660 static int
2661 i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
2662 unsigned alignment,
2663 bool map_and_fenceable)
2664 {
2665 struct drm_device *dev = obj->base.dev;
2666 drm_i915_private_t *dev_priv = dev->dev_private;
2667 struct drm_mm_node *free_space;
2668 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2669 u32 size, fence_size, fence_alignment, unfenced_alignment;
2670 bool mappable, fenceable;
2671 int ret;
2672
2673 if (obj->madv != I915_MADV_WILLNEED) {
2674 DRM_ERROR("Attempting to bind a purgeable object\n");
2675 return -EINVAL;
2676 }
2677
2678 fence_size = i915_gem_get_gtt_size(obj);
2679 fence_alignment = i915_gem_get_gtt_alignment(obj);
2680 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
2681
2682 if (alignment == 0)
2683 alignment = map_and_fenceable ? fence_alignment :
2684 unfenced_alignment;
2685 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
2686 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2687 return -EINVAL;
2688 }
2689
2690 size = map_and_fenceable ? fence_size : obj->base.size;
2691
2692 /* If the object is bigger than the entire aperture, reject it early
2693 * before evicting everything in a vain attempt to find space.
2694 */
2695 if (obj->base.size >
2696 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
2697 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2698 return -E2BIG;
2699 }
2700
2701 search_free:
2702 if (map_and_fenceable)
2703 free_space =
2704 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
2705 size, alignment, 0,
2706 dev_priv->mm.gtt_mappable_end,
2707 0);
2708 else
2709 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2710 size, alignment, 0);
2711
2712 if (free_space != NULL) {
2713 if (map_and_fenceable)
2714 obj->gtt_space =
2715 drm_mm_get_block_range_generic(free_space,
2716 size, alignment, 0,
2717 dev_priv->mm.gtt_mappable_end,
2718 0);
2719 else
2720 obj->gtt_space =
2721 drm_mm_get_block(free_space, size, alignment);
2722 }
2723 if (obj->gtt_space == NULL) {
2724 /* If the gtt is empty and we're still having trouble
2725 * fitting our object in, we're out of memory.
2726 */
2727 ret = i915_gem_evict_something(dev, size, alignment,
2728 map_and_fenceable);
2729 if (ret)
2730 return ret;
2731
2732 goto search_free;
2733 }
2734
2735 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
2736 if (ret) {
2737 drm_mm_put_block(obj->gtt_space);
2738 obj->gtt_space = NULL;
2739
2740 if (ret == -ENOMEM) {
2741 /* first try to clear up some space from the GTT */
2742 ret = i915_gem_evict_something(dev, size,
2743 alignment,
2744 map_and_fenceable);
2745 if (ret) {
2746 /* now try to shrink everyone else */
2747 if (gfpmask) {
2748 gfpmask = 0;
2749 goto search_free;
2750 }
2751
2752 return ret;
2753 }
2754
2755 goto search_free;
2756 }
2757
2758 return ret;
2759 }
2760
2761 ret = i915_gem_gtt_bind_object(obj);
2762 if (ret) {
2763 i915_gem_object_put_pages_gtt(obj);
2764 drm_mm_put_block(obj->gtt_space);
2765 obj->gtt_space = NULL;
2766
2767 ret = i915_gem_evict_something(dev, size,
2768 alignment, map_and_fenceable);
2769 if (ret)
2770 return ret;
2771
2772 goto search_free;
2773 }
2774
2775 obj->gtt_offset = obj->gtt_space->start;
2776
2777 /* keep track of bounds object by adding it to the inactive list */
2778 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2779 i915_gem_info_add_gtt(dev_priv, obj);
2780
2781 /* Assert that the object is not currently in any GPU domain. As it
2782 * wasn't in the GTT, there shouldn't be any way it could have been in
2783 * a GPU cache
2784 */
2785 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2786 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2787
2788 trace_i915_gem_object_bind(obj, obj->gtt_offset, map_and_fenceable);
2789
2790 fenceable =
2791 obj->gtt_space->size == fence_size &&
2792 (obj->gtt_space->start & (fence_alignment -1)) == 0;
2793
2794 mappable =
2795 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
2796
2797 obj->map_and_fenceable = mappable && fenceable;
2798
2799 return 0;
2800 }
2801
2802 void
2803 i915_gem_clflush_object(struct drm_i915_gem_object *obj)
2804 {
2805 /* If we don't have a page list set up, then we're not pinned
2806 * to GPU, and we can ignore the cache flush because it'll happen
2807 * again at bind time.
2808 */
2809 if (obj->pages == NULL)
2810 return;
2811
2812 trace_i915_gem_object_clflush(obj);
2813
2814 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
2815 }
2816
2817 /** Flushes any GPU write domain for the object if it's dirty. */
2818 static int
2819 i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
2820 bool pipelined)
2821 {
2822 struct drm_device *dev = obj->base.dev;
2823
2824 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
2825 return 0;
2826
2827 /* Queue the GPU write cache flushing we need. */
2828 i915_gem_flush_ring(dev, obj->ring, 0, obj->base.write_domain);
2829 BUG_ON(obj->base.write_domain);
2830
2831 if (pipelined)
2832 return 0;
2833
2834 return i915_gem_object_wait_rendering(obj, true);
2835 }
2836
2837 /** Flushes the GTT write domain for the object if it's dirty. */
2838 static void
2839 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
2840 {
2841 uint32_t old_write_domain;
2842
2843 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
2844 return;
2845
2846 /* No actual flushing is required for the GTT write domain. Writes
2847 * to it immediately go to main memory as far as we know, so there's
2848 * no chipset flush. It also doesn't land in render cache.
2849 */
2850 i915_gem_release_mmap(obj);
2851
2852 old_write_domain = obj->base.write_domain;
2853 obj->base.write_domain = 0;
2854
2855 trace_i915_gem_object_change_domain(obj,
2856 obj->base.read_domains,
2857 old_write_domain);
2858 }
2859
2860 /** Flushes the CPU write domain for the object if it's dirty. */
2861 static void
2862 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
2863 {
2864 uint32_t old_write_domain;
2865
2866 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
2867 return;
2868
2869 i915_gem_clflush_object(obj);
2870 intel_gtt_chipset_flush();
2871 old_write_domain = obj->base.write_domain;
2872 obj->base.write_domain = 0;
2873
2874 trace_i915_gem_object_change_domain(obj,
2875 obj->base.read_domains,
2876 old_write_domain);
2877 }
2878
2879 /**
2880 * Moves a single object to the GTT read, and possibly write domain.
2881 *
2882 * This function returns when the move is complete, including waiting on
2883 * flushes to occur.
2884 */
2885 int
2886 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, int write)
2887 {
2888 uint32_t old_write_domain, old_read_domains;
2889 int ret;
2890
2891 /* Not valid to be called on unbound objects. */
2892 if (obj->gtt_space == NULL)
2893 return -EINVAL;
2894
2895 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
2896 if (ret != 0)
2897 return ret;
2898
2899 i915_gem_object_flush_cpu_write_domain(obj);
2900
2901 if (write) {
2902 ret = i915_gem_object_wait_rendering(obj, true);
2903 if (ret)
2904 return ret;
2905 }
2906
2907 old_write_domain = obj->base.write_domain;
2908 old_read_domains = obj->base.read_domains;
2909
2910 /* It should now be out of any other write domains, and we can update
2911 * the domain values for our changes.
2912 */
2913 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2914 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
2915 if (write) {
2916 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2917 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2918 obj->dirty = 1;
2919 }
2920
2921 trace_i915_gem_object_change_domain(obj,
2922 old_read_domains,
2923 old_write_domain);
2924
2925 return 0;
2926 }
2927
2928 /*
2929 * Prepare buffer for display plane. Use uninterruptible for possible flush
2930 * wait, as in modesetting process we're not supposed to be interrupted.
2931 */
2932 int
2933 i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj,
2934 bool pipelined)
2935 {
2936 uint32_t old_read_domains;
2937 int ret;
2938
2939 /* Not valid to be called on unbound objects. */
2940 if (obj->gtt_space == NULL)
2941 return -EINVAL;
2942
2943 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
2944 if (ret)
2945 return ret;
2946
2947 /* Currently, we are always called from an non-interruptible context. */
2948 if (!pipelined) {
2949 ret = i915_gem_object_wait_rendering(obj, false);
2950 if (ret)
2951 return ret;
2952 }
2953
2954 i915_gem_object_flush_cpu_write_domain(obj);
2955
2956 old_read_domains = obj->base.read_domains;
2957 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
2958
2959 trace_i915_gem_object_change_domain(obj,
2960 old_read_domains,
2961 obj->base.write_domain);
2962
2963 return 0;
2964 }
2965
2966 int
2967 i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
2968 bool interruptible)
2969 {
2970 if (!obj->active)
2971 return 0;
2972
2973 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
2974 i915_gem_flush_ring(obj->base.dev, obj->ring,
2975 0, obj->base.write_domain);
2976
2977 return i915_gem_object_wait_rendering(obj, interruptible);
2978 }
2979
2980 /**
2981 * Moves a single object to the CPU read, and possibly write domain.
2982 *
2983 * This function returns when the move is complete, including waiting on
2984 * flushes to occur.
2985 */
2986 static int
2987 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, int write)
2988 {
2989 uint32_t old_write_domain, old_read_domains;
2990 int ret;
2991
2992 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
2993 if (ret != 0)
2994 return ret;
2995
2996 i915_gem_object_flush_gtt_write_domain(obj);
2997
2998 /* If we have a partially-valid cache of the object in the CPU,
2999 * finish invalidating it and free the per-page flags.
3000 */
3001 i915_gem_object_set_to_full_cpu_read_domain(obj);
3002
3003 if (write) {
3004 ret = i915_gem_object_wait_rendering(obj, true);
3005 if (ret)
3006 return ret;
3007 }
3008
3009 old_write_domain = obj->base.write_domain;
3010 old_read_domains = obj->base.read_domains;
3011
3012 /* Flush the CPU cache if it's still invalid. */
3013 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3014 i915_gem_clflush_object(obj);
3015
3016 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3017 }
3018
3019 /* It should now be out of any other write domains, and we can update
3020 * the domain values for our changes.
3021 */
3022 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3023
3024 /* If we're writing through the CPU, then the GPU read domains will
3025 * need to be invalidated at next use.
3026 */
3027 if (write) {
3028 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3029 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3030 }
3031
3032 trace_i915_gem_object_change_domain(obj,
3033 old_read_domains,
3034 old_write_domain);
3035
3036 return 0;
3037 }
3038
3039 /*
3040 * Set the next domain for the specified object. This
3041 * may not actually perform the necessary flushing/invaliding though,
3042 * as that may want to be batched with other set_domain operations
3043 *
3044 * This is (we hope) the only really tricky part of gem. The goal
3045 * is fairly simple -- track which caches hold bits of the object
3046 * and make sure they remain coherent. A few concrete examples may
3047 * help to explain how it works. For shorthand, we use the notation
3048 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3049 * a pair of read and write domain masks.
3050 *
3051 * Case 1: the batch buffer
3052 *
3053 * 1. Allocated
3054 * 2. Written by CPU
3055 * 3. Mapped to GTT
3056 * 4. Read by GPU
3057 * 5. Unmapped from GTT
3058 * 6. Freed
3059 *
3060 * Let's take these a step at a time
3061 *
3062 * 1. Allocated
3063 * Pages allocated from the kernel may still have
3064 * cache contents, so we set them to (CPU, CPU) always.
3065 * 2. Written by CPU (using pwrite)
3066 * The pwrite function calls set_domain (CPU, CPU) and
3067 * this function does nothing (as nothing changes)
3068 * 3. Mapped by GTT
3069 * This function asserts that the object is not
3070 * currently in any GPU-based read or write domains
3071 * 4. Read by GPU
3072 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3073 * As write_domain is zero, this function adds in the
3074 * current read domains (CPU+COMMAND, 0).
3075 * flush_domains is set to CPU.
3076 * invalidate_domains is set to COMMAND
3077 * clflush is run to get data out of the CPU caches
3078 * then i915_dev_set_domain calls i915_gem_flush to
3079 * emit an MI_FLUSH and drm_agp_chipset_flush
3080 * 5. Unmapped from GTT
3081 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3082 * flush_domains and invalidate_domains end up both zero
3083 * so no flushing/invalidating happens
3084 * 6. Freed
3085 * yay, done
3086 *
3087 * Case 2: The shared render buffer
3088 *
3089 * 1. Allocated
3090 * 2. Mapped to GTT
3091 * 3. Read/written by GPU
3092 * 4. set_domain to (CPU,CPU)
3093 * 5. Read/written by CPU
3094 * 6. Read/written by GPU
3095 *
3096 * 1. Allocated
3097 * Same as last example, (CPU, CPU)
3098 * 2. Mapped to GTT
3099 * Nothing changes (assertions find that it is not in the GPU)
3100 * 3. Read/written by GPU
3101 * execbuffer calls set_domain (RENDER, RENDER)
3102 * flush_domains gets CPU
3103 * invalidate_domains gets GPU
3104 * clflush (obj)
3105 * MI_FLUSH and drm_agp_chipset_flush
3106 * 4. set_domain (CPU, CPU)
3107 * flush_domains gets GPU
3108 * invalidate_domains gets CPU
3109 * wait_rendering (obj) to make sure all drawing is complete.
3110 * This will include an MI_FLUSH to get the data from GPU
3111 * to memory
3112 * clflush (obj) to invalidate the CPU cache
3113 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3114 * 5. Read/written by CPU
3115 * cache lines are loaded and dirtied
3116 * 6. Read written by GPU
3117 * Same as last GPU access
3118 *
3119 * Case 3: The constant buffer
3120 *
3121 * 1. Allocated
3122 * 2. Written by CPU
3123 * 3. Read by GPU
3124 * 4. Updated (written) by CPU again
3125 * 5. Read by GPU
3126 *
3127 * 1. Allocated
3128 * (CPU, CPU)
3129 * 2. Written by CPU
3130 * (CPU, CPU)
3131 * 3. Read by GPU
3132 * (CPU+RENDER, 0)
3133 * flush_domains = CPU
3134 * invalidate_domains = RENDER
3135 * clflush (obj)
3136 * MI_FLUSH
3137 * drm_agp_chipset_flush
3138 * 4. Updated (written) by CPU again
3139 * (CPU, CPU)
3140 * flush_domains = 0 (no previous write domain)
3141 * invalidate_domains = 0 (no new read domains)
3142 * 5. Read by GPU
3143 * (CPU+RENDER, 0)
3144 * flush_domains = CPU
3145 * invalidate_domains = RENDER
3146 * clflush (obj)
3147 * MI_FLUSH
3148 * drm_agp_chipset_flush
3149 */
3150 static void
3151 i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
3152 struct intel_ring_buffer *ring,
3153 struct change_domains *cd)
3154 {
3155 uint32_t invalidate_domains = 0, flush_domains = 0;
3156
3157 /*
3158 * If the object isn't moving to a new write domain,
3159 * let the object stay in multiple read domains
3160 */
3161 if (obj->base.pending_write_domain == 0)
3162 obj->base.pending_read_domains |= obj->base.read_domains;
3163
3164 /*
3165 * Flush the current write domain if
3166 * the new read domains don't match. Invalidate
3167 * any read domains which differ from the old
3168 * write domain
3169 */
3170 if (obj->base.write_domain &&
3171 (obj->base.write_domain != obj->base.pending_read_domains ||
3172 obj->ring != ring)) {
3173 flush_domains |= obj->base.write_domain;
3174 invalidate_domains |=
3175 obj->base.pending_read_domains & ~obj->base.write_domain;
3176 }
3177 /*
3178 * Invalidate any read caches which may have
3179 * stale data. That is, any new read domains.
3180 */
3181 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
3182 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
3183 i915_gem_clflush_object(obj);
3184
3185 /* blow away mappings if mapped through GTT */
3186 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3187 i915_gem_release_mmap(obj);
3188
3189 /* The actual obj->write_domain will be updated with
3190 * pending_write_domain after we emit the accumulated flush for all
3191 * of our domain changes in execbuffers (which clears objects'
3192 * write_domains). So if we have a current write domain that we
3193 * aren't changing, set pending_write_domain to that.
3194 */
3195 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
3196 obj->base.pending_write_domain = obj->base.write_domain;
3197
3198 cd->invalidate_domains |= invalidate_domains;
3199 cd->flush_domains |= flush_domains;
3200 if (flush_domains & I915_GEM_GPU_DOMAINS)
3201 cd->flush_rings |= obj->ring->id;
3202 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
3203 cd->flush_rings |= ring->id;
3204 }
3205
3206 /**
3207 * Moves the object from a partially CPU read to a full one.
3208 *
3209 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3210 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3211 */
3212 static void
3213 i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
3214 {
3215 if (!obj->page_cpu_valid)
3216 return;
3217
3218 /* If we're partially in the CPU read domain, finish moving it in.
3219 */
3220 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
3221 int i;
3222
3223 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3224 if (obj->page_cpu_valid[i])
3225 continue;
3226 drm_clflush_pages(obj->pages + i, 1);
3227 }
3228 }
3229
3230 /* Free the page_cpu_valid mappings which are now stale, whether
3231 * or not we've got I915_GEM_DOMAIN_CPU.
3232 */
3233 kfree(obj->page_cpu_valid);
3234 obj->page_cpu_valid = NULL;
3235 }
3236
3237 /**
3238 * Set the CPU read domain on a range of the object.
3239 *
3240 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3241 * not entirely valid. The page_cpu_valid member of the object flags which
3242 * pages have been flushed, and will be respected by
3243 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3244 * of the whole object.
3245 *
3246 * This function returns when the move is complete, including waiting on
3247 * flushes to occur.
3248 */
3249 static int
3250 i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
3251 uint64_t offset, uint64_t size)
3252 {
3253 uint32_t old_read_domains;
3254 int i, ret;
3255
3256 if (offset == 0 && size == obj->base.size)
3257 return i915_gem_object_set_to_cpu_domain(obj, 0);
3258
3259 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
3260 if (ret != 0)
3261 return ret;
3262 i915_gem_object_flush_gtt_write_domain(obj);
3263
3264 /* If we're already fully in the CPU read domain, we're done. */
3265 if (obj->page_cpu_valid == NULL &&
3266 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
3267 return 0;
3268
3269 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3270 * newly adding I915_GEM_DOMAIN_CPU
3271 */
3272 if (obj->page_cpu_valid == NULL) {
3273 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3274 GFP_KERNEL);
3275 if (obj->page_cpu_valid == NULL)
3276 return -ENOMEM;
3277 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3278 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
3279
3280 /* Flush the cache on any pages that are still invalid from the CPU's
3281 * perspective.
3282 */
3283 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3284 i++) {
3285 if (obj->page_cpu_valid[i])
3286 continue;
3287
3288 drm_clflush_pages(obj->pages + i, 1);
3289
3290 obj->page_cpu_valid[i] = 1;
3291 }
3292
3293 /* It should now be out of any other write domains, and we can update
3294 * the domain values for our changes.
3295 */
3296 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3297
3298 old_read_domains = obj->base.read_domains;
3299 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3300
3301 trace_i915_gem_object_change_domain(obj,
3302 old_read_domains,
3303 obj->base.write_domain);
3304
3305 return 0;
3306 }
3307
3308 static int
3309 i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
3310 struct drm_file *file_priv,
3311 struct drm_i915_gem_exec_object2 *entry,
3312 struct drm_i915_gem_relocation_entry *reloc)
3313 {
3314 struct drm_device *dev = obj->base.dev;
3315 struct drm_gem_object *target_obj;
3316 uint32_t target_offset;
3317 int ret = -EINVAL;
3318
3319 target_obj = drm_gem_object_lookup(dev, file_priv,
3320 reloc->target_handle);
3321 if (target_obj == NULL)
3322 return -ENOENT;
3323
3324 target_offset = to_intel_bo(target_obj)->gtt_offset;
3325
3326 #if WATCH_RELOC
3327 DRM_INFO("%s: obj %p offset %08x target %d "
3328 "read %08x write %08x gtt %08x "
3329 "presumed %08x delta %08x\n",
3330 __func__,
3331 obj,
3332 (int) reloc->offset,
3333 (int) reloc->target_handle,
3334 (int) reloc->read_domains,
3335 (int) reloc->write_domain,
3336 (int) target_offset,
3337 (int) reloc->presumed_offset,
3338 reloc->delta);
3339 #endif
3340
3341 /* The target buffer should have appeared before us in the
3342 * exec_object list, so it should have a GTT space bound by now.
3343 */
3344 if (target_offset == 0) {
3345 DRM_ERROR("No GTT space found for object %d\n",
3346 reloc->target_handle);
3347 goto err;
3348 }
3349
3350 /* Validate that the target is in a valid r/w GPU domain */
3351 if (reloc->write_domain & (reloc->write_domain - 1)) {
3352 DRM_ERROR("reloc with multiple write domains: "
3353 "obj %p target %d offset %d "
3354 "read %08x write %08x",
3355 obj, reloc->target_handle,
3356 (int) reloc->offset,
3357 reloc->read_domains,
3358 reloc->write_domain);
3359 goto err;
3360 }
3361 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3362 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3363 DRM_ERROR("reloc with read/write CPU domains: "
3364 "obj %p target %d offset %d "
3365 "read %08x write %08x",
3366 obj, reloc->target_handle,
3367 (int) reloc->offset,
3368 reloc->read_domains,
3369 reloc->write_domain);
3370 goto err;
3371 }
3372 if (reloc->write_domain && target_obj->pending_write_domain &&
3373 reloc->write_domain != target_obj->pending_write_domain) {
3374 DRM_ERROR("Write domain conflict: "
3375 "obj %p target %d offset %d "
3376 "new %08x old %08x\n",
3377 obj, reloc->target_handle,
3378 (int) reloc->offset,
3379 reloc->write_domain,
3380 target_obj->pending_write_domain);
3381 goto err;
3382 }
3383
3384 target_obj->pending_read_domains |= reloc->read_domains;
3385 target_obj->pending_write_domain |= reloc->write_domain;
3386
3387 /* If the relocation already has the right value in it, no
3388 * more work needs to be done.
3389 */
3390 if (target_offset == reloc->presumed_offset)
3391 goto out;
3392
3393 /* Check that the relocation address is valid... */
3394 if (reloc->offset > obj->base.size - 4) {
3395 DRM_ERROR("Relocation beyond object bounds: "
3396 "obj %p target %d offset %d size %d.\n",
3397 obj, reloc->target_handle,
3398 (int) reloc->offset,
3399 (int) obj->base.size);
3400 goto err;
3401 }
3402 if (reloc->offset & 3) {
3403 DRM_ERROR("Relocation not 4-byte aligned: "
3404 "obj %p target %d offset %d.\n",
3405 obj, reloc->target_handle,
3406 (int) reloc->offset);
3407 goto err;
3408 }
3409
3410 /* and points to somewhere within the target object. */
3411 if (reloc->delta >= target_obj->size) {
3412 DRM_ERROR("Relocation beyond target object bounds: "
3413 "obj %p target %d delta %d size %d.\n",
3414 obj, reloc->target_handle,
3415 (int) reloc->delta,
3416 (int) target_obj->size);
3417 goto err;
3418 }
3419
3420 reloc->delta += target_offset;
3421 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3422 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
3423 char *vaddr;
3424
3425 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
3426 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
3427 kunmap_atomic(vaddr);
3428 } else {
3429 struct drm_i915_private *dev_priv = dev->dev_private;
3430 uint32_t __iomem *reloc_entry;
3431 void __iomem *reloc_page;
3432
3433 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3434 if (ret)
3435 goto err;
3436
3437 /* Map the page containing the relocation we're going to perform. */
3438 reloc->offset += obj->gtt_offset;
3439 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3440 reloc->offset & PAGE_MASK);
3441 reloc_entry = (uint32_t __iomem *)
3442 (reloc_page + (reloc->offset & ~PAGE_MASK));
3443 iowrite32(reloc->delta, reloc_entry);
3444 io_mapping_unmap_atomic(reloc_page);
3445 }
3446
3447 /* and update the user's relocation entry */
3448 reloc->presumed_offset = target_offset;
3449
3450 out:
3451 ret = 0;
3452 err:
3453 drm_gem_object_unreference(target_obj);
3454 return ret;
3455 }
3456
3457 static int
3458 i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
3459 struct drm_file *file_priv,
3460 struct drm_i915_gem_exec_object2 *entry)
3461 {
3462 struct drm_i915_gem_relocation_entry __user *user_relocs;
3463 int i, ret;
3464
3465 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3466 for (i = 0; i < entry->relocation_count; i++) {
3467 struct drm_i915_gem_relocation_entry reloc;
3468
3469 if (__copy_from_user_inatomic(&reloc,
3470 user_relocs+i,
3471 sizeof(reloc)))
3472 return -EFAULT;
3473
3474 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
3475 if (ret)
3476 return ret;
3477
3478 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3479 &reloc.presumed_offset,
3480 sizeof(reloc.presumed_offset)))
3481 return -EFAULT;
3482 }
3483
3484 return 0;
3485 }
3486
3487 static int
3488 i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
3489 struct drm_file *file_priv,
3490 struct drm_i915_gem_exec_object2 *entry,
3491 struct drm_i915_gem_relocation_entry *relocs)
3492 {
3493 int i, ret;
3494
3495 for (i = 0; i < entry->relocation_count; i++) {
3496 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
3497 if (ret)
3498 return ret;
3499 }
3500
3501 return 0;
3502 }
3503
3504 static int
3505 i915_gem_execbuffer_relocate(struct drm_device *dev,
3506 struct drm_file *file,
3507 struct drm_i915_gem_object **object_list,
3508 struct drm_i915_gem_exec_object2 *exec_list,
3509 int count)
3510 {
3511 int i, ret;
3512
3513 for (i = 0; i < count; i++) {
3514 struct drm_i915_gem_object *obj = object_list[i];
3515 obj->base.pending_read_domains = 0;
3516 obj->base.pending_write_domain = 0;
3517 ret = i915_gem_execbuffer_relocate_object(obj, file,
3518 &exec_list[i]);
3519 if (ret)
3520 return ret;
3521 }
3522
3523 return 0;
3524 }
3525
3526 static int
3527 i915_gem_execbuffer_reserve(struct drm_device *dev,
3528 struct drm_file *file,
3529 struct drm_i915_gem_object **object_list,
3530 struct drm_i915_gem_exec_object2 *exec_list,
3531 int count)
3532 {
3533 struct drm_i915_private *dev_priv = dev->dev_private;
3534 int ret, i, retry;
3535
3536 /* attempt to pin all of the buffers into the GTT */
3537 retry = 0;
3538 do {
3539 ret = 0;
3540 for (i = 0; i < count; i++) {
3541 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
3542 struct drm_i915_gem_object *obj = object_list[i];
3543 bool need_fence =
3544 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3545 obj->tiling_mode != I915_TILING_NONE;
3546
3547 /* g33/pnv can't fence buffers in the unmappable part */
3548 bool need_mappable =
3549 entry->relocation_count ? true : need_fence;
3550
3551 /* Check fence reg constraints and rebind if necessary */
3552 if (need_mappable && !obj->map_and_fenceable) {
3553 ret = i915_gem_object_unbind(obj);
3554 if (ret)
3555 break;
3556 }
3557
3558 ret = i915_gem_object_pin(obj,
3559 entry->alignment,
3560 need_mappable);
3561 if (ret)
3562 break;
3563
3564 /*
3565 * Pre-965 chips need a fence register set up in order
3566 * to properly handle blits to/from tiled surfaces.
3567 */
3568 if (need_fence) {
3569 ret = i915_gem_object_get_fence_reg(obj, true);
3570 if (ret) {
3571 i915_gem_object_unpin(obj);
3572 break;
3573 }
3574
3575 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3576 }
3577
3578 entry->offset = obj->gtt_offset;
3579 }
3580
3581 while (i--)
3582 i915_gem_object_unpin(object_list[i]);
3583
3584 if (ret != -ENOSPC || retry > 1)
3585 return ret;
3586
3587 /* First attempt, just clear anything that is purgeable.
3588 * Second attempt, clear the entire GTT.
3589 */
3590 ret = i915_gem_evict_everything(dev, retry == 0);
3591 if (ret)
3592 return ret;
3593
3594 retry++;
3595 } while (1);
3596 }
3597
3598 static int
3599 i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
3600 struct drm_file *file,
3601 struct drm_i915_gem_object **object_list,
3602 struct drm_i915_gem_exec_object2 *exec_list,
3603 int count)
3604 {
3605 struct drm_i915_gem_relocation_entry *reloc;
3606 int i, total, ret;
3607
3608 for (i = 0; i < count; i++)
3609 object_list[i]->in_execbuffer = false;
3610
3611 mutex_unlock(&dev->struct_mutex);
3612
3613 total = 0;
3614 for (i = 0; i < count; i++)
3615 total += exec_list[i].relocation_count;
3616
3617 reloc = drm_malloc_ab(total, sizeof(*reloc));
3618 if (reloc == NULL) {
3619 mutex_lock(&dev->struct_mutex);
3620 return -ENOMEM;
3621 }
3622
3623 total = 0;
3624 for (i = 0; i < count; i++) {
3625 struct drm_i915_gem_relocation_entry __user *user_relocs;
3626
3627 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3628
3629 if (copy_from_user(reloc+total, user_relocs,
3630 exec_list[i].relocation_count *
3631 sizeof(*reloc))) {
3632 ret = -EFAULT;
3633 mutex_lock(&dev->struct_mutex);
3634 goto err;
3635 }
3636
3637 total += exec_list[i].relocation_count;
3638 }
3639
3640 ret = i915_mutex_lock_interruptible(dev);
3641 if (ret) {
3642 mutex_lock(&dev->struct_mutex);
3643 goto err;
3644 }
3645
3646 ret = i915_gem_execbuffer_reserve(dev, file,
3647 object_list, exec_list,
3648 count);
3649 if (ret)
3650 goto err;
3651
3652 total = 0;
3653 for (i = 0; i < count; i++) {
3654 struct drm_i915_gem_object *obj = object_list[i];
3655 obj->base.pending_read_domains = 0;
3656 obj->base.pending_write_domain = 0;
3657 ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
3658 &exec_list[i],
3659 reloc + total);
3660 if (ret)
3661 goto err;
3662
3663 total += exec_list[i].relocation_count;
3664 }
3665
3666 /* Leave the user relocations as are, this is the painfully slow path,
3667 * and we want to avoid the complication of dropping the lock whilst
3668 * having buffers reserved in the aperture and so causing spurious
3669 * ENOSPC for random operations.
3670 */
3671
3672 err:
3673 drm_free_large(reloc);
3674 return ret;
3675 }
3676
3677 static int
3678 i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3679 struct drm_file *file,
3680 struct intel_ring_buffer *ring,
3681 struct drm_i915_gem_object **objects,
3682 int count)
3683 {
3684 struct change_domains cd;
3685 int ret, i;
3686
3687 cd.invalidate_domains = 0;
3688 cd.flush_domains = 0;
3689 cd.flush_rings = 0;
3690 for (i = 0; i < count; i++)
3691 i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
3692
3693 if (cd.invalidate_domains | cd.flush_domains) {
3694 #if WATCH_EXEC
3695 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3696 __func__,
3697 cd.invalidate_domains,
3698 cd.flush_domains);
3699 #endif
3700 i915_gem_flush(dev,
3701 cd.invalidate_domains,
3702 cd.flush_domains,
3703 cd.flush_rings);
3704 }
3705
3706 for (i = 0; i < count; i++) {
3707 struct drm_i915_gem_object *obj = objects[i];
3708 /* XXX replace with semaphores */
3709 if (obj->ring && ring != obj->ring) {
3710 ret = i915_gem_object_wait_rendering(obj, true);
3711 if (ret)
3712 return ret;
3713 }
3714 }
3715
3716 return 0;
3717 }
3718
3719 /* Throttle our rendering by waiting until the ring has completed our requests
3720 * emitted over 20 msec ago.
3721 *
3722 * Note that if we were to use the current jiffies each time around the loop,
3723 * we wouldn't escape the function with any frames outstanding if the time to
3724 * render a frame was over 20ms.
3725 *
3726 * This should get us reasonable parallelism between CPU and GPU but also
3727 * relatively low latency when blocking on a particular request to finish.
3728 */
3729 static int
3730 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3731 {
3732 struct drm_i915_private *dev_priv = dev->dev_private;
3733 struct drm_i915_file_private *file_priv = file->driver_priv;
3734 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3735 struct drm_i915_gem_request *request;
3736 struct intel_ring_buffer *ring = NULL;
3737 u32 seqno = 0;
3738 int ret;
3739
3740 spin_lock(&file_priv->mm.lock);
3741 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3742 if (time_after_eq(request->emitted_jiffies, recent_enough))
3743 break;
3744
3745 ring = request->ring;
3746 seqno = request->seqno;
3747 }
3748 spin_unlock(&file_priv->mm.lock);
3749
3750 if (seqno == 0)
3751 return 0;
3752
3753 ret = 0;
3754 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
3755 /* And wait for the seqno passing without holding any locks and
3756 * causing extra latency for others. This is safe as the irq
3757 * generation is designed to be run atomically and so is
3758 * lockless.
3759 */
3760 ring->user_irq_get(ring);
3761 ret = wait_event_interruptible(ring->irq_queue,
3762 i915_seqno_passed(ring->get_seqno(ring), seqno)
3763 || atomic_read(&dev_priv->mm.wedged));
3764 ring->user_irq_put(ring);
3765
3766 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3767 ret = -EIO;
3768 }
3769
3770 if (ret == 0)
3771 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3772
3773 return ret;
3774 }
3775
3776 static int
3777 i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3778 uint64_t exec_offset)
3779 {
3780 uint32_t exec_start, exec_len;
3781
3782 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3783 exec_len = (uint32_t) exec->batch_len;
3784
3785 if ((exec_start | exec_len) & 0x7)
3786 return -EINVAL;
3787
3788 if (!exec_start)
3789 return -EINVAL;
3790
3791 return 0;
3792 }
3793
3794 static int
3795 validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3796 int count)
3797 {
3798 int i;
3799
3800 for (i = 0; i < count; i++) {
3801 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3802 int length; /* limited by fault_in_pages_readable() */
3803
3804 /* First check for malicious input causing overflow */
3805 if (exec[i].relocation_count >
3806 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
3807 return -EINVAL;
3808
3809 length = exec[i].relocation_count *
3810 sizeof(struct drm_i915_gem_relocation_entry);
3811 if (!access_ok(VERIFY_READ, ptr, length))
3812 return -EFAULT;
3813
3814 /* we may also need to update the presumed offsets */
3815 if (!access_ok(VERIFY_WRITE, ptr, length))
3816 return -EFAULT;
3817
3818 if (fault_in_pages_readable(ptr, length))
3819 return -EFAULT;
3820 }
3821
3822 return 0;
3823 }
3824
3825 static int
3826 i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3827 struct drm_file *file,
3828 struct drm_i915_gem_execbuffer2 *args,
3829 struct drm_i915_gem_exec_object2 *exec_list)
3830 {
3831 drm_i915_private_t *dev_priv = dev->dev_private;
3832 struct drm_i915_gem_object **object_list = NULL;
3833 struct drm_i915_gem_object *batch_obj;
3834 struct drm_clip_rect *cliprects = NULL;
3835 struct drm_i915_gem_request *request = NULL;
3836 int ret, i, flips;
3837 uint64_t exec_offset;
3838
3839 struct intel_ring_buffer *ring = NULL;
3840
3841 ret = i915_gem_check_is_wedged(dev);
3842 if (ret)
3843 return ret;
3844
3845 ret = validate_exec_list(exec_list, args->buffer_count);
3846 if (ret)
3847 return ret;
3848
3849 #if WATCH_EXEC
3850 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3851 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3852 #endif
3853 switch (args->flags & I915_EXEC_RING_MASK) {
3854 case I915_EXEC_DEFAULT:
3855 case I915_EXEC_RENDER:
3856 ring = &dev_priv->render_ring;
3857 break;
3858 case I915_EXEC_BSD:
3859 if (!HAS_BSD(dev)) {
3860 DRM_ERROR("execbuf with invalid ring (BSD)\n");
3861 return -EINVAL;
3862 }
3863 ring = &dev_priv->bsd_ring;
3864 break;
3865 case I915_EXEC_BLT:
3866 if (!HAS_BLT(dev)) {
3867 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3868 return -EINVAL;
3869 }
3870 ring = &dev_priv->blt_ring;
3871 break;
3872 default:
3873 DRM_ERROR("execbuf with unknown ring: %d\n",
3874 (int)(args->flags & I915_EXEC_RING_MASK));
3875 return -EINVAL;
3876 }
3877
3878 if (args->buffer_count < 1) {
3879 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3880 return -EINVAL;
3881 }
3882 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
3883 if (object_list == NULL) {
3884 DRM_ERROR("Failed to allocate object list for %d buffers\n",
3885 args->buffer_count);
3886 ret = -ENOMEM;
3887 goto pre_mutex_err;
3888 }
3889
3890 if (args->num_cliprects != 0) {
3891 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3892 GFP_KERNEL);
3893 if (cliprects == NULL) {
3894 ret = -ENOMEM;
3895 goto pre_mutex_err;
3896 }
3897
3898 ret = copy_from_user(cliprects,
3899 (struct drm_clip_rect __user *)
3900 (uintptr_t) args->cliprects_ptr,
3901 sizeof(*cliprects) * args->num_cliprects);
3902 if (ret != 0) {
3903 DRM_ERROR("copy %d cliprects failed: %d\n",
3904 args->num_cliprects, ret);
3905 ret = -EFAULT;
3906 goto pre_mutex_err;
3907 }
3908 }
3909
3910 request = kzalloc(sizeof(*request), GFP_KERNEL);
3911 if (request == NULL) {
3912 ret = -ENOMEM;
3913 goto pre_mutex_err;
3914 }
3915
3916 ret = i915_mutex_lock_interruptible(dev);
3917 if (ret)
3918 goto pre_mutex_err;
3919
3920 if (dev_priv->mm.suspended) {
3921 mutex_unlock(&dev->struct_mutex);
3922 ret = -EBUSY;
3923 goto pre_mutex_err;
3924 }
3925
3926 /* Look up object handles */
3927 for (i = 0; i < args->buffer_count; i++) {
3928 struct drm_i915_gem_object *obj;
3929
3930 obj = to_intel_bo (drm_gem_object_lookup(dev, file,
3931 exec_list[i].handle));
3932 if (obj == NULL) {
3933 DRM_ERROR("Invalid object handle %d at index %d\n",
3934 exec_list[i].handle, i);
3935 /* prevent error path from reading uninitialized data */
3936 args->buffer_count = i;
3937 ret = -ENOENT;
3938 goto err;
3939 }
3940 object_list[i] = obj;
3941
3942 if (obj->in_execbuffer) {
3943 DRM_ERROR("Object %p appears more than once in object list\n",
3944 obj);
3945 /* prevent error path from reading uninitialized data */
3946 args->buffer_count = i + 1;
3947 ret = -EINVAL;
3948 goto err;
3949 }
3950 obj->in_execbuffer = true;
3951 }
3952
3953 /* Move the objects en-masse into the GTT, evicting if necessary. */
3954 ret = i915_gem_execbuffer_reserve(dev, file,
3955 object_list, exec_list,
3956 args->buffer_count);
3957 if (ret)
3958 goto err;
3959
3960 /* The objects are in their final locations, apply the relocations. */
3961 ret = i915_gem_execbuffer_relocate(dev, file,
3962 object_list, exec_list,
3963 args->buffer_count);
3964 if (ret) {
3965 if (ret == -EFAULT) {
3966 ret = i915_gem_execbuffer_relocate_slow(dev, file,
3967 object_list,
3968 exec_list,
3969 args->buffer_count);
3970 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
3971 }
3972 if (ret)
3973 goto err;
3974 }
3975
3976 /* Set the pending read domains for the batch buffer to COMMAND */
3977 batch_obj = object_list[args->buffer_count-1];
3978 if (batch_obj->base.pending_write_domain) {
3979 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3980 ret = -EINVAL;
3981 goto err;
3982 }
3983 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
3984
3985 /* Sanity check the batch buffer */
3986 exec_offset = batch_obj->gtt_offset;
3987 ret = i915_gem_check_execbuffer(args, exec_offset);
3988 if (ret != 0) {
3989 DRM_ERROR("execbuf with invalid offset/length\n");
3990 goto err;
3991 }
3992
3993 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
3994 object_list, args->buffer_count);
3995 if (ret)
3996 goto err;
3997
3998 #if WATCH_COHERENCY
3999 for (i = 0; i < args->buffer_count; i++) {
4000 i915_gem_object_check_coherency(object_list[i],
4001 exec_list[i].handle);
4002 }
4003 #endif
4004
4005 #if WATCH_EXEC
4006 i915_gem_dump_object(batch_obj,
4007 args->batch_len,
4008 __func__,
4009 ~0);
4010 #endif
4011
4012 /* Check for any pending flips. As we only maintain a flip queue depth
4013 * of 1, we can simply insert a WAIT for the next display flip prior
4014 * to executing the batch and avoid stalling the CPU.
4015 */
4016 flips = 0;
4017 for (i = 0; i < args->buffer_count; i++) {
4018 if (object_list[i]->base.write_domain)
4019 flips |= atomic_read(&object_list[i]->pending_flip);
4020 }
4021 if (flips) {
4022 int plane, flip_mask;
4023
4024 for (plane = 0; flips >> plane; plane++) {
4025 if (((flips >> plane) & 1) == 0)
4026 continue;
4027
4028 if (plane)
4029 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
4030 else
4031 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
4032
4033 ret = intel_ring_begin(ring, 2);
4034 if (ret)
4035 goto err;
4036
4037 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
4038 intel_ring_emit(ring, MI_NOOP);
4039 intel_ring_advance(ring);
4040 }
4041 }
4042
4043 /* Exec the batchbuffer */
4044 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
4045 if (ret) {
4046 DRM_ERROR("dispatch failed %d\n", ret);
4047 goto err;
4048 }
4049
4050 for (i = 0; i < args->buffer_count; i++) {
4051 struct drm_i915_gem_object *obj = object_list[i];
4052
4053 obj->base.read_domains = obj->base.pending_read_domains;
4054 obj->base.write_domain = obj->base.pending_write_domain;
4055
4056 i915_gem_object_move_to_active(obj, ring);
4057 if (obj->base.write_domain) {
4058 obj->dirty = 1;
4059 list_move_tail(&obj->gpu_write_list,
4060 &ring->gpu_write_list);
4061 intel_mark_busy(dev, obj);
4062 }
4063
4064 trace_i915_gem_object_change_domain(obj,
4065 obj->base.read_domains,
4066 obj->base.write_domain);
4067 }
4068
4069 /*
4070 * Ensure that the commands in the batch buffer are
4071 * finished before the interrupt fires
4072 */
4073 i915_retire_commands(dev, ring);
4074
4075 if (i915_add_request(dev, file, request, ring))
4076 i915_gem_next_request_seqno(dev, ring);
4077 else
4078 request = NULL;
4079
4080 err:
4081 for (i = 0; i < args->buffer_count; i++) {
4082 object_list[i]->in_execbuffer = false;
4083 drm_gem_object_unreference(&object_list[i]->base);
4084 }
4085
4086 mutex_unlock(&dev->struct_mutex);
4087
4088 pre_mutex_err:
4089 drm_free_large(object_list);
4090 kfree(cliprects);
4091 kfree(request);
4092
4093 return ret;
4094 }
4095
4096 /*
4097 * Legacy execbuffer just creates an exec2 list from the original exec object
4098 * list array and passes it to the real function.
4099 */
4100 int
4101 i915_gem_execbuffer(struct drm_device *dev, void *data,
4102 struct drm_file *file)
4103 {
4104 struct drm_i915_gem_execbuffer *args = data;
4105 struct drm_i915_gem_execbuffer2 exec2;
4106 struct drm_i915_gem_exec_object *exec_list = NULL;
4107 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4108 int ret, i;
4109
4110 #if WATCH_EXEC
4111 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4112 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4113 #endif
4114
4115 if (args->buffer_count < 1) {
4116 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4117 return -EINVAL;
4118 }
4119
4120 /* Copy in the exec list from userland */
4121 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4122 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4123 if (exec_list == NULL || exec2_list == NULL) {
4124 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4125 args->buffer_count);
4126 drm_free_large(exec_list);
4127 drm_free_large(exec2_list);
4128 return -ENOMEM;
4129 }
4130 ret = copy_from_user(exec_list,
4131 (struct drm_i915_relocation_entry __user *)
4132 (uintptr_t) args->buffers_ptr,
4133 sizeof(*exec_list) * args->buffer_count);
4134 if (ret != 0) {
4135 DRM_ERROR("copy %d exec entries failed %d\n",
4136 args->buffer_count, ret);
4137 drm_free_large(exec_list);
4138 drm_free_large(exec2_list);
4139 return -EFAULT;
4140 }
4141
4142 for (i = 0; i < args->buffer_count; i++) {
4143 exec2_list[i].handle = exec_list[i].handle;
4144 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4145 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4146 exec2_list[i].alignment = exec_list[i].alignment;
4147 exec2_list[i].offset = exec_list[i].offset;
4148 if (INTEL_INFO(dev)->gen < 4)
4149 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4150 else
4151 exec2_list[i].flags = 0;
4152 }
4153
4154 exec2.buffers_ptr = args->buffers_ptr;
4155 exec2.buffer_count = args->buffer_count;
4156 exec2.batch_start_offset = args->batch_start_offset;
4157 exec2.batch_len = args->batch_len;
4158 exec2.DR1 = args->DR1;
4159 exec2.DR4 = args->DR4;
4160 exec2.num_cliprects = args->num_cliprects;
4161 exec2.cliprects_ptr = args->cliprects_ptr;
4162 exec2.flags = I915_EXEC_RENDER;
4163
4164 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
4165 if (!ret) {
4166 /* Copy the new buffer offsets back to the user's exec list. */
4167 for (i = 0; i < args->buffer_count; i++)
4168 exec_list[i].offset = exec2_list[i].offset;
4169 /* ... and back out to userspace */
4170 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4171 (uintptr_t) args->buffers_ptr,
4172 exec_list,
4173 sizeof(*exec_list) * args->buffer_count);
4174 if (ret) {
4175 ret = -EFAULT;
4176 DRM_ERROR("failed to copy %d exec entries "
4177 "back to user (%d)\n",
4178 args->buffer_count, ret);
4179 }
4180 }
4181
4182 drm_free_large(exec_list);
4183 drm_free_large(exec2_list);
4184 return ret;
4185 }
4186
4187 int
4188 i915_gem_execbuffer2(struct drm_device *dev, void *data,
4189 struct drm_file *file)
4190 {
4191 struct drm_i915_gem_execbuffer2 *args = data;
4192 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4193 int ret;
4194
4195 #if WATCH_EXEC
4196 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4197 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4198 #endif
4199
4200 if (args->buffer_count < 1) {
4201 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4202 return -EINVAL;
4203 }
4204
4205 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4206 if (exec2_list == NULL) {
4207 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4208 args->buffer_count);
4209 return -ENOMEM;
4210 }
4211 ret = copy_from_user(exec2_list,
4212 (struct drm_i915_relocation_entry __user *)
4213 (uintptr_t) args->buffers_ptr,
4214 sizeof(*exec2_list) * args->buffer_count);
4215 if (ret != 0) {
4216 DRM_ERROR("copy %d exec entries failed %d\n",
4217 args->buffer_count, ret);
4218 drm_free_large(exec2_list);
4219 return -EFAULT;
4220 }
4221
4222 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
4223 if (!ret) {
4224 /* Copy the new buffer offsets back to the user's exec list. */
4225 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4226 (uintptr_t) args->buffers_ptr,
4227 exec2_list,
4228 sizeof(*exec2_list) * args->buffer_count);
4229 if (ret) {
4230 ret = -EFAULT;
4231 DRM_ERROR("failed to copy %d exec entries "
4232 "back to user (%d)\n",
4233 args->buffer_count, ret);
4234 }
4235 }
4236
4237 drm_free_large(exec2_list);
4238 return ret;
4239 }
4240
4241 int
4242 i915_gem_object_pin(struct drm_i915_gem_object *obj,
4243 uint32_t alignment,
4244 bool map_and_fenceable)
4245 {
4246 struct drm_device *dev = obj->base.dev;
4247 struct drm_i915_private *dev_priv = dev->dev_private;
4248 int ret;
4249
4250 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4251 BUG_ON(map_and_fenceable && !map_and_fenceable);
4252 WARN_ON(i915_verify_lists(dev));
4253
4254 if (obj->gtt_space != NULL) {
4255 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
4256 (map_and_fenceable && !obj->map_and_fenceable)) {
4257 WARN(obj->pin_count,
4258 "bo is already pinned with incorrect alignment:"
4259 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
4260 " obj->map_and_fenceable=%d\n",
4261 obj->gtt_offset, alignment,
4262 map_and_fenceable,
4263 obj->map_and_fenceable);
4264 ret = i915_gem_object_unbind(obj);
4265 if (ret)
4266 return ret;
4267 }
4268 }
4269
4270 if (obj->gtt_space == NULL) {
4271 ret = i915_gem_object_bind_to_gtt(obj, alignment,
4272 map_and_fenceable);
4273 if (ret)
4274 return ret;
4275 }
4276
4277 if (obj->pin_count++ == 0) {
4278 i915_gem_info_add_pin(dev_priv, obj, map_and_fenceable);
4279 if (!obj->active)
4280 list_move_tail(&obj->mm_list,
4281 &dev_priv->mm.pinned_list);
4282 }
4283 BUG_ON(!obj->pin_mappable && map_and_fenceable);
4284
4285 WARN_ON(i915_verify_lists(dev));
4286 return 0;
4287 }
4288
4289 void
4290 i915_gem_object_unpin(struct drm_i915_gem_object *obj)
4291 {
4292 struct drm_device *dev = obj->base.dev;
4293 drm_i915_private_t *dev_priv = dev->dev_private;
4294
4295 WARN_ON(i915_verify_lists(dev));
4296 BUG_ON(obj->pin_count == 0);
4297 BUG_ON(obj->gtt_space == NULL);
4298
4299 if (--obj->pin_count == 0) {
4300 if (!obj->active)
4301 list_move_tail(&obj->mm_list,
4302 &dev_priv->mm.inactive_list);
4303 i915_gem_info_remove_pin(dev_priv, obj);
4304 }
4305 WARN_ON(i915_verify_lists(dev));
4306 }
4307
4308 int
4309 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4310 struct drm_file *file)
4311 {
4312 struct drm_i915_gem_pin *args = data;
4313 struct drm_i915_gem_object *obj;
4314 int ret;
4315
4316 ret = i915_mutex_lock_interruptible(dev);
4317 if (ret)
4318 return ret;
4319
4320 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
4321 if (obj == NULL) {
4322 ret = -ENOENT;
4323 goto unlock;
4324 }
4325
4326 if (obj->madv != I915_MADV_WILLNEED) {
4327 DRM_ERROR("Attempting to pin a purgeable buffer\n");
4328 ret = -EINVAL;
4329 goto out;
4330 }
4331
4332 if (obj->pin_filp != NULL && obj->pin_filp != file) {
4333 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4334 args->handle);
4335 ret = -EINVAL;
4336 goto out;
4337 }
4338
4339 obj->user_pin_count++;
4340 obj->pin_filp = file;
4341 if (obj->user_pin_count == 1) {
4342 ret = i915_gem_object_pin(obj, args->alignment, true);
4343 if (ret)
4344 goto out;
4345 }
4346
4347 /* XXX - flush the CPU caches for pinned objects
4348 * as the X server doesn't manage domains yet
4349 */
4350 i915_gem_object_flush_cpu_write_domain(obj);
4351 args->offset = obj->gtt_offset;
4352 out:
4353 drm_gem_object_unreference(&obj->base);
4354 unlock:
4355 mutex_unlock(&dev->struct_mutex);
4356 return ret;
4357 }
4358
4359 int
4360 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4361 struct drm_file *file)
4362 {
4363 struct drm_i915_gem_pin *args = data;
4364 struct drm_i915_gem_object *obj;
4365 int ret;
4366
4367 ret = i915_mutex_lock_interruptible(dev);
4368 if (ret)
4369 return ret;
4370
4371 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
4372 if (obj == NULL) {
4373 ret = -ENOENT;
4374 goto unlock;
4375 }
4376
4377 if (obj->pin_filp != file) {
4378 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4379 args->handle);
4380 ret = -EINVAL;
4381 goto out;
4382 }
4383 obj->user_pin_count--;
4384 if (obj->user_pin_count == 0) {
4385 obj->pin_filp = NULL;
4386 i915_gem_object_unpin(obj);
4387 }
4388
4389 out:
4390 drm_gem_object_unreference(&obj->base);
4391 unlock:
4392 mutex_unlock(&dev->struct_mutex);
4393 return ret;
4394 }
4395
4396 int
4397 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4398 struct drm_file *file)
4399 {
4400 struct drm_i915_gem_busy *args = data;
4401 struct drm_i915_gem_object *obj;
4402 int ret;
4403
4404 ret = i915_mutex_lock_interruptible(dev);
4405 if (ret)
4406 return ret;
4407
4408 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
4409 if (obj == NULL) {
4410 ret = -ENOENT;
4411 goto unlock;
4412 }
4413
4414 /* Count all active objects as busy, even if they are currently not used
4415 * by the gpu. Users of this interface expect objects to eventually
4416 * become non-busy without any further actions, therefore emit any
4417 * necessary flushes here.
4418 */
4419 args->busy = obj->active;
4420 if (args->busy) {
4421 /* Unconditionally flush objects, even when the gpu still uses this
4422 * object. Userspace calling this function indicates that it wants to
4423 * use this buffer rather sooner than later, so issuing the required
4424 * flush earlier is beneficial.
4425 */
4426 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
4427 i915_gem_flush_ring(dev, obj->ring,
4428 0, obj->base.write_domain);
4429
4430 /* Update the active list for the hardware's current position.
4431 * Otherwise this only updates on a delayed timer or when irqs
4432 * are actually unmasked, and our working set ends up being
4433 * larger than required.
4434 */
4435 i915_gem_retire_requests_ring(dev, obj->ring);
4436
4437 args->busy = obj->active;
4438 }
4439
4440 drm_gem_object_unreference(&obj->base);
4441 unlock:
4442 mutex_unlock(&dev->struct_mutex);
4443 return ret;
4444 }
4445
4446 int
4447 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4448 struct drm_file *file_priv)
4449 {
4450 return i915_gem_ring_throttle(dev, file_priv);
4451 }
4452
4453 int
4454 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4455 struct drm_file *file_priv)
4456 {
4457 struct drm_i915_gem_madvise *args = data;
4458 struct drm_i915_gem_object *obj;
4459 int ret;
4460
4461 switch (args->madv) {
4462 case I915_MADV_DONTNEED:
4463 case I915_MADV_WILLNEED:
4464 break;
4465 default:
4466 return -EINVAL;
4467 }
4468
4469 ret = i915_mutex_lock_interruptible(dev);
4470 if (ret)
4471 return ret;
4472
4473 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
4474 if (obj == NULL) {
4475 ret = -ENOENT;
4476 goto unlock;
4477 }
4478
4479 if (obj->pin_count) {
4480 ret = -EINVAL;
4481 goto out;
4482 }
4483
4484 if (obj->madv != __I915_MADV_PURGED)
4485 obj->madv = args->madv;
4486
4487 /* if the object is no longer bound, discard its backing storage */
4488 if (i915_gem_object_is_purgeable(obj) &&
4489 obj->gtt_space == NULL)
4490 i915_gem_object_truncate(obj);
4491
4492 args->retained = obj->madv != __I915_MADV_PURGED;
4493
4494 out:
4495 drm_gem_object_unreference(&obj->base);
4496 unlock:
4497 mutex_unlock(&dev->struct_mutex);
4498 return ret;
4499 }
4500
4501 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
4502 size_t size)
4503 {
4504 struct drm_i915_private *dev_priv = dev->dev_private;
4505 struct drm_i915_gem_object *obj;
4506
4507 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4508 if (obj == NULL)
4509 return NULL;
4510
4511 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4512 kfree(obj);
4513 return NULL;
4514 }
4515
4516 i915_gem_info_add_obj(dev_priv, size);
4517
4518 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4519 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4520
4521 obj->agp_type = AGP_USER_MEMORY;
4522 obj->base.driver_private = NULL;
4523 obj->fence_reg = I915_FENCE_REG_NONE;
4524 INIT_LIST_HEAD(&obj->mm_list);
4525 INIT_LIST_HEAD(&obj->gtt_list);
4526 INIT_LIST_HEAD(&obj->ring_list);
4527 INIT_LIST_HEAD(&obj->gpu_write_list);
4528 obj->madv = I915_MADV_WILLNEED;
4529 /* Avoid an unnecessary call to unbind on the first bind. */
4530 obj->map_and_fenceable = true;
4531
4532 return obj;
4533 }
4534
4535 int i915_gem_init_object(struct drm_gem_object *obj)
4536 {
4537 BUG();
4538
4539 return 0;
4540 }
4541
4542 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
4543 {
4544 struct drm_device *dev = obj->base.dev;
4545 drm_i915_private_t *dev_priv = dev->dev_private;
4546 int ret;
4547
4548 ret = i915_gem_object_unbind(obj);
4549 if (ret == -ERESTARTSYS) {
4550 list_move(&obj->mm_list,
4551 &dev_priv->mm.deferred_free_list);
4552 return;
4553 }
4554
4555 if (obj->base.map_list.map)
4556 i915_gem_free_mmap_offset(obj);
4557
4558 drm_gem_object_release(&obj->base);
4559 i915_gem_info_remove_obj(dev_priv, obj->base.size);
4560
4561 kfree(obj->page_cpu_valid);
4562 kfree(obj->bit_17);
4563 kfree(obj);
4564 }
4565
4566 void i915_gem_free_object(struct drm_gem_object *gem_obj)
4567 {
4568 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4569 struct drm_device *dev = obj->base.dev;
4570
4571 trace_i915_gem_object_destroy(obj);
4572
4573 while (obj->pin_count > 0)
4574 i915_gem_object_unpin(obj);
4575
4576 if (obj->phys_obj)
4577 i915_gem_detach_phys_object(dev, obj);
4578
4579 i915_gem_free_object_tail(obj);
4580 }
4581
4582 int
4583 i915_gem_idle(struct drm_device *dev)
4584 {
4585 drm_i915_private_t *dev_priv = dev->dev_private;
4586 int ret;
4587
4588 mutex_lock(&dev->struct_mutex);
4589
4590 if (dev_priv->mm.suspended) {
4591 mutex_unlock(&dev->struct_mutex);
4592 return 0;
4593 }
4594
4595 ret = i915_gpu_idle(dev);
4596 if (ret) {
4597 mutex_unlock(&dev->struct_mutex);
4598 return ret;
4599 }
4600
4601 /* Under UMS, be paranoid and evict. */
4602 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
4603 ret = i915_gem_evict_inactive(dev, false);
4604 if (ret) {
4605 mutex_unlock(&dev->struct_mutex);
4606 return ret;
4607 }
4608 }
4609
4610 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4611 * We need to replace this with a semaphore, or something.
4612 * And not confound mm.suspended!
4613 */
4614 dev_priv->mm.suspended = 1;
4615 del_timer_sync(&dev_priv->hangcheck_timer);
4616
4617 i915_kernel_lost_context(dev);
4618 i915_gem_cleanup_ringbuffer(dev);
4619
4620 mutex_unlock(&dev->struct_mutex);
4621
4622 /* Cancel the retire work handler, which should be idle now. */
4623 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4624
4625 return 0;
4626 }
4627
4628 int
4629 i915_gem_init_ringbuffer(struct drm_device *dev)
4630 {
4631 drm_i915_private_t *dev_priv = dev->dev_private;
4632 int ret;
4633
4634 ret = intel_init_render_ring_buffer(dev);
4635 if (ret)
4636 return ret;
4637
4638 if (HAS_BSD(dev)) {
4639 ret = intel_init_bsd_ring_buffer(dev);
4640 if (ret)
4641 goto cleanup_render_ring;
4642 }
4643
4644 if (HAS_BLT(dev)) {
4645 ret = intel_init_blt_ring_buffer(dev);
4646 if (ret)
4647 goto cleanup_bsd_ring;
4648 }
4649
4650 dev_priv->next_seqno = 1;
4651
4652 return 0;
4653
4654 cleanup_bsd_ring:
4655 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4656 cleanup_render_ring:
4657 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4658 return ret;
4659 }
4660
4661 void
4662 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4663 {
4664 drm_i915_private_t *dev_priv = dev->dev_private;
4665
4666 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4667 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4668 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
4669 }
4670
4671 int
4672 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4673 struct drm_file *file_priv)
4674 {
4675 drm_i915_private_t *dev_priv = dev->dev_private;
4676 int ret;
4677
4678 if (drm_core_check_feature(dev, DRIVER_MODESET))
4679 return 0;
4680
4681 if (atomic_read(&dev_priv->mm.wedged)) {
4682 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4683 atomic_set(&dev_priv->mm.wedged, 0);
4684 }
4685
4686 mutex_lock(&dev->struct_mutex);
4687 dev_priv->mm.suspended = 0;
4688
4689 ret = i915_gem_init_ringbuffer(dev);
4690 if (ret != 0) {
4691 mutex_unlock(&dev->struct_mutex);
4692 return ret;
4693 }
4694
4695 BUG_ON(!list_empty(&dev_priv->mm.active_list));
4696 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
4697 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
4698 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
4699 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4700 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4701 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
4702 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
4703 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
4704 mutex_unlock(&dev->struct_mutex);
4705
4706 ret = drm_irq_install(dev);
4707 if (ret)
4708 goto cleanup_ringbuffer;
4709
4710 return 0;
4711
4712 cleanup_ringbuffer:
4713 mutex_lock(&dev->struct_mutex);
4714 i915_gem_cleanup_ringbuffer(dev);
4715 dev_priv->mm.suspended = 1;
4716 mutex_unlock(&dev->struct_mutex);
4717
4718 return ret;
4719 }
4720
4721 int
4722 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4723 struct drm_file *file_priv)
4724 {
4725 if (drm_core_check_feature(dev, DRIVER_MODESET))
4726 return 0;
4727
4728 drm_irq_uninstall(dev);
4729 return i915_gem_idle(dev);
4730 }
4731
4732 void
4733 i915_gem_lastclose(struct drm_device *dev)
4734 {
4735 int ret;
4736
4737 if (drm_core_check_feature(dev, DRIVER_MODESET))
4738 return;
4739
4740 ret = i915_gem_idle(dev);
4741 if (ret)
4742 DRM_ERROR("failed to idle hardware: %d\n", ret);
4743 }
4744
4745 static void
4746 init_ring_lists(struct intel_ring_buffer *ring)
4747 {
4748 INIT_LIST_HEAD(&ring->active_list);
4749 INIT_LIST_HEAD(&ring->request_list);
4750 INIT_LIST_HEAD(&ring->gpu_write_list);
4751 }
4752
4753 void
4754 i915_gem_load(struct drm_device *dev)
4755 {
4756 int i;
4757 drm_i915_private_t *dev_priv = dev->dev_private;
4758
4759 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4760 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4761 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4762 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
4763 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4764 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
4765 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
4766 init_ring_lists(&dev_priv->render_ring);
4767 init_ring_lists(&dev_priv->bsd_ring);
4768 init_ring_lists(&dev_priv->blt_ring);
4769 for (i = 0; i < 16; i++)
4770 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
4771 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4772 i915_gem_retire_work_handler);
4773 init_completion(&dev_priv->error_completion);
4774
4775 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4776 if (IS_GEN3(dev)) {
4777 u32 tmp = I915_READ(MI_ARB_STATE);
4778 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4779 /* arb state is a masked write, so set bit + bit in mask */
4780 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4781 I915_WRITE(MI_ARB_STATE, tmp);
4782 }
4783 }
4784
4785 /* Old X drivers will take 0-2 for front, back, depth buffers */
4786 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4787 dev_priv->fence_reg_start = 3;
4788
4789 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4790 dev_priv->num_fence_regs = 16;
4791 else
4792 dev_priv->num_fence_regs = 8;
4793
4794 /* Initialize fence registers to zero */
4795 switch (INTEL_INFO(dev)->gen) {
4796 case 6:
4797 for (i = 0; i < 16; i++)
4798 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4799 break;
4800 case 5:
4801 case 4:
4802 for (i = 0; i < 16; i++)
4803 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4804 break;
4805 case 3:
4806 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4807 for (i = 0; i < 8; i++)
4808 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4809 case 2:
4810 for (i = 0; i < 8; i++)
4811 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4812 break;
4813 }
4814 i915_gem_detect_bit_6_swizzle(dev);
4815 init_waitqueue_head(&dev_priv->pending_flip_queue);
4816
4817 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4818 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4819 register_shrinker(&dev_priv->mm.inactive_shrinker);
4820 }
4821
4822 /*
4823 * Create a physically contiguous memory object for this object
4824 * e.g. for cursor + overlay regs
4825 */
4826 static int i915_gem_init_phys_object(struct drm_device *dev,
4827 int id, int size, int align)
4828 {
4829 drm_i915_private_t *dev_priv = dev->dev_private;
4830 struct drm_i915_gem_phys_object *phys_obj;
4831 int ret;
4832
4833 if (dev_priv->mm.phys_objs[id - 1] || !size)
4834 return 0;
4835
4836 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
4837 if (!phys_obj)
4838 return -ENOMEM;
4839
4840 phys_obj->id = id;
4841
4842 phys_obj->handle = drm_pci_alloc(dev, size, align);
4843 if (!phys_obj->handle) {
4844 ret = -ENOMEM;
4845 goto kfree_obj;
4846 }
4847 #ifdef CONFIG_X86
4848 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4849 #endif
4850
4851 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4852
4853 return 0;
4854 kfree_obj:
4855 kfree(phys_obj);
4856 return ret;
4857 }
4858
4859 static void i915_gem_free_phys_object(struct drm_device *dev, int id)
4860 {
4861 drm_i915_private_t *dev_priv = dev->dev_private;
4862 struct drm_i915_gem_phys_object *phys_obj;
4863
4864 if (!dev_priv->mm.phys_objs[id - 1])
4865 return;
4866
4867 phys_obj = dev_priv->mm.phys_objs[id - 1];
4868 if (phys_obj->cur_obj) {
4869 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4870 }
4871
4872 #ifdef CONFIG_X86
4873 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4874 #endif
4875 drm_pci_free(dev, phys_obj->handle);
4876 kfree(phys_obj);
4877 dev_priv->mm.phys_objs[id - 1] = NULL;
4878 }
4879
4880 void i915_gem_free_all_phys_object(struct drm_device *dev)
4881 {
4882 int i;
4883
4884 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4885 i915_gem_free_phys_object(dev, i);
4886 }
4887
4888 void i915_gem_detach_phys_object(struct drm_device *dev,
4889 struct drm_i915_gem_object *obj)
4890 {
4891 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4892 char *vaddr;
4893 int i;
4894 int page_count;
4895
4896 if (!obj->phys_obj)
4897 return;
4898 vaddr = obj->phys_obj->handle->vaddr;
4899
4900 page_count = obj->base.size / PAGE_SIZE;
4901 for (i = 0; i < page_count; i++) {
4902 struct page *page = read_cache_page_gfp(mapping, i,
4903 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4904 if (!IS_ERR(page)) {
4905 char *dst = kmap_atomic(page);
4906 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4907 kunmap_atomic(dst);
4908
4909 drm_clflush_pages(&page, 1);
4910
4911 set_page_dirty(page);
4912 mark_page_accessed(page);
4913 page_cache_release(page);
4914 }
4915 }
4916 intel_gtt_chipset_flush();
4917
4918 obj->phys_obj->cur_obj = NULL;
4919 obj->phys_obj = NULL;
4920 }
4921
4922 int
4923 i915_gem_attach_phys_object(struct drm_device *dev,
4924 struct drm_i915_gem_object *obj,
4925 int id,
4926 int align)
4927 {
4928 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4929 drm_i915_private_t *dev_priv = dev->dev_private;
4930 int ret = 0;
4931 int page_count;
4932 int i;
4933
4934 if (id > I915_MAX_PHYS_OBJECT)
4935 return -EINVAL;
4936
4937 if (obj->phys_obj) {
4938 if (obj->phys_obj->id == id)
4939 return 0;
4940 i915_gem_detach_phys_object(dev, obj);
4941 }
4942
4943 /* create a new object */
4944 if (!dev_priv->mm.phys_objs[id - 1]) {
4945 ret = i915_gem_init_phys_object(dev, id,
4946 obj->base.size, align);
4947 if (ret) {
4948 DRM_ERROR("failed to init phys object %d size: %zu\n",
4949 id, obj->base.size);
4950 return ret;
4951 }
4952 }
4953
4954 /* bind to the object */
4955 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4956 obj->phys_obj->cur_obj = obj;
4957
4958 page_count = obj->base.size / PAGE_SIZE;
4959
4960 for (i = 0; i < page_count; i++) {
4961 struct page *page;
4962 char *dst, *src;
4963
4964 page = read_cache_page_gfp(mapping, i,
4965 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4966 if (IS_ERR(page))
4967 return PTR_ERR(page);
4968
4969 src = kmap_atomic(page);
4970 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4971 memcpy(dst, src, PAGE_SIZE);
4972 kunmap_atomic(src);
4973
4974 mark_page_accessed(page);
4975 page_cache_release(page);
4976 }
4977
4978 return 0;
4979 }
4980
4981 static int
4982 i915_gem_phys_pwrite(struct drm_device *dev,
4983 struct drm_i915_gem_object *obj,
4984 struct drm_i915_gem_pwrite *args,
4985 struct drm_file *file_priv)
4986 {
4987 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
4988 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
4989
4990 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4991 unsigned long unwritten;
4992
4993 /* The physical object once assigned is fixed for the lifetime
4994 * of the obj, so we can safely drop the lock and continue
4995 * to access vaddr.
4996 */
4997 mutex_unlock(&dev->struct_mutex);
4998 unwritten = copy_from_user(vaddr, user_data, args->size);
4999 mutex_lock(&dev->struct_mutex);
5000 if (unwritten)
5001 return -EFAULT;
5002 }
5003
5004 intel_gtt_chipset_flush();
5005 return 0;
5006 }
5007
5008 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
5009 {
5010 struct drm_i915_file_private *file_priv = file->driver_priv;
5011
5012 /* Clean up our request list when the client is going away, so that
5013 * later retire_requests won't dereference our soon-to-be-gone
5014 * file_priv.
5015 */
5016 spin_lock(&file_priv->mm.lock);
5017 while (!list_empty(&file_priv->mm.request_list)) {
5018 struct drm_i915_gem_request *request;
5019
5020 request = list_first_entry(&file_priv->mm.request_list,
5021 struct drm_i915_gem_request,
5022 client_list);
5023 list_del(&request->client_list);
5024 request->file_priv = NULL;
5025 }
5026 spin_unlock(&file_priv->mm.lock);
5027 }
5028
5029 static int
5030 i915_gpu_is_active(struct drm_device *dev)
5031 {
5032 drm_i915_private_t *dev_priv = dev->dev_private;
5033 int lists_empty;
5034
5035 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
5036 list_empty(&dev_priv->mm.active_list);
5037
5038 return !lists_empty;
5039 }
5040
5041 static int
5042 i915_gem_inactive_shrink(struct shrinker *shrinker,
5043 int nr_to_scan,
5044 gfp_t gfp_mask)
5045 {
5046 struct drm_i915_private *dev_priv =
5047 container_of(shrinker,
5048 struct drm_i915_private,
5049 mm.inactive_shrinker);
5050 struct drm_device *dev = dev_priv->dev;
5051 struct drm_i915_gem_object *obj, *next;
5052 int cnt;
5053
5054 if (!mutex_trylock(&dev->struct_mutex))
5055 return 0;
5056
5057 /* "fast-path" to count number of available objects */
5058 if (nr_to_scan == 0) {
5059 cnt = 0;
5060 list_for_each_entry(obj,
5061 &dev_priv->mm.inactive_list,
5062 mm_list)
5063 cnt++;
5064 mutex_unlock(&dev->struct_mutex);
5065 return cnt / 100 * sysctl_vfs_cache_pressure;
5066 }
5067
5068 rescan:
5069 /* first scan for clean buffers */
5070 i915_gem_retire_requests(dev);
5071
5072 list_for_each_entry_safe(obj, next,
5073 &dev_priv->mm.inactive_list,
5074 mm_list) {
5075 if (i915_gem_object_is_purgeable(obj)) {
5076 i915_gem_object_unbind(obj);
5077 if (--nr_to_scan == 0)
5078 break;
5079 }
5080 }
5081
5082 /* second pass, evict/count anything still on the inactive list */
5083 cnt = 0;
5084 list_for_each_entry_safe(obj, next,
5085 &dev_priv->mm.inactive_list,
5086 mm_list) {
5087 if (nr_to_scan) {
5088 i915_gem_object_unbind(obj);
5089 nr_to_scan--;
5090 } else
5091 cnt++;
5092 }
5093
5094 if (nr_to_scan && i915_gpu_is_active(dev)) {
5095 /*
5096 * We are desperate for pages, so as a last resort, wait
5097 * for the GPU to finish and discard whatever we can.
5098 * This has a dramatic impact to reduce the number of
5099 * OOM-killer events whilst running the GPU aggressively.
5100 */
5101 if (i915_gpu_idle(dev) == 0)
5102 goto rescan;
5103 }
5104 mutex_unlock(&dev->struct_mutex);
5105 return cnt / 100 * sysctl_vfs_cache_pressure;
5106 }
This page took 0.133654 seconds and 6 git commands to generate.