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