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