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/shmem_fs.h>
35 #include <linux/slab.h>
36 #include <linux/swap.h>
37 #include <linux/pci.h>
38
39 static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
40 static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
41 static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
42 static __must_check int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
43 bool write);
44 static __must_check int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
45 uint64_t offset,
46 uint64_t size);
47 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
48 static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
49 unsigned alignment,
50 bool map_and_fenceable);
51 static void i915_gem_clear_fence_reg(struct drm_device *dev,
52 struct drm_i915_fence_reg *reg);
53 static int i915_gem_phys_pwrite(struct drm_device *dev,
54 struct drm_i915_gem_object *obj,
55 struct drm_i915_gem_pwrite *args,
56 struct drm_file *file);
57 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
58
59 static int i915_gem_inactive_shrink(struct shrinker *shrinker,
60 struct shrink_control *sc);
61 static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
62
63 /* some bookkeeping */
i915_gem_info_add_obj(struct drm_i915_private * dev_priv,size_t size)64 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
65 size_t size)
66 {
67 dev_priv->mm.object_count++;
68 dev_priv->mm.object_memory += size;
69 }
70
i915_gem_info_remove_obj(struct drm_i915_private * dev_priv,size_t size)71 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
72 size_t size)
73 {
74 dev_priv->mm.object_count--;
75 dev_priv->mm.object_memory -= size;
76 }
77
78 static int
i915_gem_wait_for_error(struct drm_device * dev)79 i915_gem_wait_for_error(struct drm_device *dev)
80 {
81 struct drm_i915_private *dev_priv = dev->dev_private;
82 struct completion *x = &dev_priv->error_completion;
83 unsigned long flags;
84 int ret;
85
86 if (!atomic_read(&dev_priv->mm.wedged))
87 return 0;
88
89 ret = wait_for_completion_interruptible(x);
90 if (ret)
91 return ret;
92
93 if (atomic_read(&dev_priv->mm.wedged)) {
94 /* GPU is hung, bump the completion count to account for
95 * the token we just consumed so that we never hit zero and
96 * end up waiting upon a subsequent completion event that
97 * will never happen.
98 */
99 spin_lock_irqsave(&x->wait.lock, flags);
100 x->done++;
101 spin_unlock_irqrestore(&x->wait.lock, flags);
102 }
103 return 0;
104 }
105
i915_mutex_lock_interruptible(struct drm_device * dev)106 int i915_mutex_lock_interruptible(struct drm_device *dev)
107 {
108 int ret;
109
110 ret = i915_gem_wait_for_error(dev);
111 if (ret)
112 return ret;
113
114 ret = mutex_lock_interruptible(&dev->struct_mutex);
115 if (ret)
116 return ret;
117
118 WARN_ON(i915_verify_lists(dev));
119 return 0;
120 }
121
122 static inline bool
i915_gem_object_is_inactive(struct drm_i915_gem_object * obj)123 i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
124 {
125 return obj->gtt_space && !obj->active && obj->pin_count == 0;
126 }
127
i915_gem_do_init(struct drm_device * dev,unsigned long start,unsigned long mappable_end,unsigned long end)128 void i915_gem_do_init(struct drm_device *dev,
129 unsigned long start,
130 unsigned long mappable_end,
131 unsigned long end)
132 {
133 drm_i915_private_t *dev_priv = dev->dev_private;
134
135 drm_mm_init(&dev_priv->mm.gtt_space, start, end - start);
136
137 dev_priv->mm.gtt_start = start;
138 dev_priv->mm.gtt_mappable_end = mappable_end;
139 dev_priv->mm.gtt_end = end;
140 dev_priv->mm.gtt_total = end - start;
141 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
142
143 /* Take over this portion of the GTT */
144 intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
145 }
146
147 int
i915_gem_init_ioctl(struct drm_device * dev,void * data,struct drm_file * file)148 i915_gem_init_ioctl(struct drm_device *dev, void *data,
149 struct drm_file *file)
150 {
151 struct drm_i915_gem_init *args = data;
152
153 if (args->gtt_start >= args->gtt_end ||
154 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
155 return -EINVAL;
156
157 mutex_lock(&dev->struct_mutex);
158 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
159 mutex_unlock(&dev->struct_mutex);
160
161 return 0;
162 }
163
164 int
i915_gem_get_aperture_ioctl(struct drm_device * dev,void * data,struct drm_file * file)165 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
166 struct drm_file *file)
167 {
168 struct drm_i915_private *dev_priv = dev->dev_private;
169 struct drm_i915_gem_get_aperture *args = data;
170 struct drm_i915_gem_object *obj;
171 size_t pinned;
172
173 if (!(dev->driver->driver_features & DRIVER_GEM))
174 return -ENODEV;
175
176 pinned = 0;
177 mutex_lock(&dev->struct_mutex);
178 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
179 pinned += obj->gtt_space->size;
180 mutex_unlock(&dev->struct_mutex);
181
182 args->aper_size = dev_priv->mm.gtt_total;
183 args->aper_available_size = args->aper_size - pinned;
184
185 return 0;
186 }
187
188 static int
i915_gem_create(struct drm_file * file,struct drm_device * dev,uint64_t size,uint32_t * handle_p)189 i915_gem_create(struct drm_file *file,
190 struct drm_device *dev,
191 uint64_t size,
192 uint32_t *handle_p)
193 {
194 struct drm_i915_gem_object *obj;
195 int ret;
196 u32 handle;
197
198 size = roundup(size, PAGE_SIZE);
199 if (size == 0)
200 return -EINVAL;
201
202 /* Allocate the new object */
203 obj = i915_gem_alloc_object(dev, size);
204 if (obj == NULL)
205 return -ENOMEM;
206
207 ret = drm_gem_handle_create(file, &obj->base, &handle);
208 if (ret) {
209 drm_gem_object_release(&obj->base);
210 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
211 kfree(obj);
212 return ret;
213 }
214
215 /* drop reference from allocate - handle holds it now */
216 drm_gem_object_unreference(&obj->base);
217 trace_i915_gem_object_create(obj);
218
219 *handle_p = handle;
220 return 0;
221 }
222
223 int
i915_gem_dumb_create(struct drm_file * file,struct drm_device * dev,struct drm_mode_create_dumb * args)224 i915_gem_dumb_create(struct drm_file *file,
225 struct drm_device *dev,
226 struct drm_mode_create_dumb *args)
227 {
228 /* have to work out size/pitch and return them */
229 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
230 args->size = args->pitch * args->height;
231 return i915_gem_create(file, dev,
232 args->size, &args->handle);
233 }
234
i915_gem_dumb_destroy(struct drm_file * file,struct drm_device * dev,uint32_t handle)235 int i915_gem_dumb_destroy(struct drm_file *file,
236 struct drm_device *dev,
237 uint32_t handle)
238 {
239 return drm_gem_handle_delete(file, handle);
240 }
241
242 /**
243 * Creates a new mm object and returns a handle to it.
244 */
245 int
i915_gem_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file)246 i915_gem_create_ioctl(struct drm_device *dev, void *data,
247 struct drm_file *file)
248 {
249 struct drm_i915_gem_create *args = data;
250 return i915_gem_create(file, dev,
251 args->size, &args->handle);
252 }
253
i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object * obj)254 static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
255 {
256 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
257
258 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
259 obj->tiling_mode != I915_TILING_NONE;
260 }
261
262 /**
263 * This is the fast shmem pread path, which attempts to copy_from_user directly
264 * from the backing pages of the object to the user's address space. On a
265 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
266 */
267 static int
i915_gem_shmem_pread_fast(struct drm_device * dev,struct drm_i915_gem_object * obj,struct drm_i915_gem_pread * args,struct drm_file * file)268 i915_gem_shmem_pread_fast(struct drm_device *dev,
269 struct drm_i915_gem_object *obj,
270 struct drm_i915_gem_pread *args,
271 struct drm_file *file)
272 {
273 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
274 ssize_t remain;
275 loff_t offset;
276 char __user *user_data;
277 int page_offset, page_length;
278
279 user_data = (char __user *) (uintptr_t) args->data_ptr;
280 remain = args->size;
281
282 offset = args->offset;
283
284 while (remain > 0) {
285 struct page *page;
286 char *vaddr;
287 int ret;
288
289 /* Operation in this page
290 *
291 * page_offset = offset within page
292 * page_length = bytes to copy for this page
293 */
294 page_offset = offset_in_page(offset);
295 page_length = remain;
296 if ((page_offset + remain) > PAGE_SIZE)
297 page_length = PAGE_SIZE - page_offset;
298
299 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
300 if (IS_ERR(page))
301 return PTR_ERR(page);
302
303 vaddr = kmap_atomic(page);
304 ret = __copy_to_user_inatomic(user_data,
305 vaddr + page_offset,
306 page_length);
307 kunmap_atomic(vaddr);
308
309 mark_page_accessed(page);
310 page_cache_release(page);
311 if (ret)
312 return -EFAULT;
313
314 remain -= page_length;
315 user_data += page_length;
316 offset += page_length;
317 }
318
319 return 0;
320 }
321
322 static inline int
__copy_to_user_swizzled(char __user * cpu_vaddr,const char * gpu_vaddr,int gpu_offset,int length)323 __copy_to_user_swizzled(char __user *cpu_vaddr,
324 const char *gpu_vaddr, int gpu_offset,
325 int length)
326 {
327 int ret, cpu_offset = 0;
328
329 while (length > 0) {
330 int cacheline_end = ALIGN(gpu_offset + 1, 64);
331 int this_length = min(cacheline_end - gpu_offset, length);
332 int swizzled_gpu_offset = gpu_offset ^ 64;
333
334 ret = __copy_to_user(cpu_vaddr + cpu_offset,
335 gpu_vaddr + swizzled_gpu_offset,
336 this_length);
337 if (ret)
338 return ret + length;
339
340 cpu_offset += this_length;
341 gpu_offset += this_length;
342 length -= this_length;
343 }
344
345 return 0;
346 }
347
348 static inline int
__copy_from_user_swizzled(char __user * gpu_vaddr,int gpu_offset,const char * cpu_vaddr,int length)349 __copy_from_user_swizzled(char __user *gpu_vaddr, int gpu_offset,
350 const char *cpu_vaddr,
351 int length)
352 {
353 int ret, cpu_offset = 0;
354
355 while (length > 0) {
356 int cacheline_end = ALIGN(gpu_offset + 1, 64);
357 int this_length = min(cacheline_end - gpu_offset, length);
358 int swizzled_gpu_offset = gpu_offset ^ 64;
359
360 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
361 cpu_vaddr + cpu_offset,
362 this_length);
363 if (ret)
364 return ret + length;
365
366 cpu_offset += this_length;
367 gpu_offset += this_length;
368 length -= this_length;
369 }
370
371 return 0;
372 }
373
374 /**
375 * This is the fallback shmem pread path, which allocates temporary storage
376 * in kernel space to copy_to_user into outside of the struct_mutex, so we
377 * can copy out of the object's backing pages while holding the struct mutex
378 * and not take page faults.
379 */
380 static int
i915_gem_shmem_pread_slow(struct drm_device * dev,struct drm_i915_gem_object * obj,struct drm_i915_gem_pread * args,struct drm_file * file)381 i915_gem_shmem_pread_slow(struct drm_device *dev,
382 struct drm_i915_gem_object *obj,
383 struct drm_i915_gem_pread *args,
384 struct drm_file *file)
385 {
386 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
387 char __user *user_data;
388 ssize_t remain;
389 loff_t offset;
390 int shmem_page_offset, page_length, ret;
391 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
392
393 user_data = (char __user *) (uintptr_t) args->data_ptr;
394 remain = args->size;
395
396 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
397
398 offset = args->offset;
399
400 mutex_unlock(&dev->struct_mutex);
401
402 while (remain > 0) {
403 struct page *page;
404 char *vaddr;
405
406 /* Operation in this page
407 *
408 * shmem_page_offset = offset within page in shmem file
409 * page_length = bytes to copy for this page
410 */
411 shmem_page_offset = offset_in_page(offset);
412 page_length = remain;
413 if ((shmem_page_offset + page_length) > PAGE_SIZE)
414 page_length = PAGE_SIZE - shmem_page_offset;
415
416 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
417 if (IS_ERR(page)) {
418 ret = PTR_ERR(page);
419 goto out;
420 }
421
422 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
423 (page_to_phys(page) & (1 << 17)) != 0;
424
425 vaddr = kmap(page);
426 if (page_do_bit17_swizzling)
427 ret = __copy_to_user_swizzled(user_data,
428 vaddr, shmem_page_offset,
429 page_length);
430 else
431 ret = __copy_to_user(user_data,
432 vaddr + shmem_page_offset,
433 page_length);
434 kunmap(page);
435
436 mark_page_accessed(page);
437 page_cache_release(page);
438
439 if (ret) {
440 ret = -EFAULT;
441 goto out;
442 }
443
444 remain -= page_length;
445 user_data += page_length;
446 offset += page_length;
447 }
448
449 out:
450 mutex_lock(&dev->struct_mutex);
451 /* Fixup: Kill any reinstated backing storage pages */
452 if (obj->madv == __I915_MADV_PURGED)
453 i915_gem_object_truncate(obj);
454
455 return ret;
456 }
457
458 /**
459 * Reads data from the object referenced by handle.
460 *
461 * On error, the contents of *data are undefined.
462 */
463 int
i915_gem_pread_ioctl(struct drm_device * dev,void * data,struct drm_file * file)464 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
465 struct drm_file *file)
466 {
467 struct drm_i915_gem_pread *args = data;
468 struct drm_i915_gem_object *obj;
469 int ret = 0;
470
471 if (args->size == 0)
472 return 0;
473
474 if (!access_ok(VERIFY_WRITE,
475 (char __user *)(uintptr_t)args->data_ptr,
476 args->size))
477 return -EFAULT;
478
479 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
480 args->size);
481 if (ret)
482 return -EFAULT;
483
484 ret = i915_mutex_lock_interruptible(dev);
485 if (ret)
486 return ret;
487
488 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
489 if (&obj->base == NULL) {
490 ret = -ENOENT;
491 goto unlock;
492 }
493
494 /* Bounds check source. */
495 if (args->offset > obj->base.size ||
496 args->size > obj->base.size - args->offset) {
497 ret = -EINVAL;
498 goto out;
499 }
500
501 trace_i915_gem_object_pread(obj, args->offset, args->size);
502
503 ret = i915_gem_object_set_cpu_read_domain_range(obj,
504 args->offset,
505 args->size);
506 if (ret)
507 goto out;
508
509 ret = -EFAULT;
510 if (!i915_gem_object_needs_bit17_swizzle(obj))
511 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
512 if (ret == -EFAULT)
513 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
514
515 out:
516 drm_gem_object_unreference(&obj->base);
517 unlock:
518 mutex_unlock(&dev->struct_mutex);
519 return ret;
520 }
521
522 /* This is the fast write path which cannot handle
523 * page faults in the source data
524 */
525
526 static inline int
fast_user_write(struct io_mapping * mapping,loff_t page_base,int page_offset,char __user * user_data,int length)527 fast_user_write(struct io_mapping *mapping,
528 loff_t page_base, int page_offset,
529 char __user *user_data,
530 int length)
531 {
532 char *vaddr_atomic;
533 unsigned long unwritten;
534
535 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
536 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
537 user_data, length);
538 io_mapping_unmap_atomic(vaddr_atomic);
539 return unwritten;
540 }
541
542 /* Here's the write path which can sleep for
543 * page faults
544 */
545
546 static inline void
slow_kernel_write(struct io_mapping * mapping,loff_t gtt_base,int gtt_offset,struct page * user_page,int user_offset,int length)547 slow_kernel_write(struct io_mapping *mapping,
548 loff_t gtt_base, int gtt_offset,
549 struct page *user_page, int user_offset,
550 int length)
551 {
552 char __iomem *dst_vaddr;
553 char *src_vaddr;
554
555 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
556 src_vaddr = kmap(user_page);
557
558 memcpy_toio(dst_vaddr + gtt_offset,
559 src_vaddr + user_offset,
560 length);
561
562 kunmap(user_page);
563 io_mapping_unmap(dst_vaddr);
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
i915_gem_gtt_pwrite_fast(struct drm_device * dev,struct drm_i915_gem_object * obj,struct drm_i915_gem_pwrite * args,struct drm_file * file)571 i915_gem_gtt_pwrite_fast(struct drm_device *dev,
572 struct drm_i915_gem_object *obj,
573 struct drm_i915_gem_pwrite *args,
574 struct drm_file *file)
575 {
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
582 user_data = (char __user *) (uintptr_t) args->data_ptr;
583 remain = args->size;
584
585 offset = obj->gtt_offset + args->offset;
586
587 while (remain > 0) {
588 /* Operation in this page
589 *
590 * page_base = page offset within aperture
591 * page_offset = offset within page
592 * page_length = bytes to copy for this page
593 */
594 page_base = offset & PAGE_MASK;
595 page_offset = offset_in_page(offset);
596 page_length = remain;
597 if ((page_offset + remain) > PAGE_SIZE)
598 page_length = PAGE_SIZE - page_offset;
599
600 /* If we get a fault while copying data, then (presumably) our
601 * source page isn't available. Return the error and we'll
602 * retry in the slow path.
603 */
604 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
605 page_offset, user_data, page_length))
606 return -EFAULT;
607
608 remain -= page_length;
609 user_data += page_length;
610 offset += page_length;
611 }
612
613 return 0;
614 }
615
616 /**
617 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
618 * the memory and maps it using kmap_atomic for copying.
619 *
620 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
621 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
622 */
623 static int
i915_gem_gtt_pwrite_slow(struct drm_device * dev,struct drm_i915_gem_object * obj,struct drm_i915_gem_pwrite * args,struct drm_file * file)624 i915_gem_gtt_pwrite_slow(struct drm_device *dev,
625 struct drm_i915_gem_object *obj,
626 struct drm_i915_gem_pwrite *args,
627 struct drm_file *file)
628 {
629 drm_i915_private_t *dev_priv = dev->dev_private;
630 ssize_t remain;
631 loff_t gtt_page_base, offset;
632 loff_t first_data_page, last_data_page, num_pages;
633 loff_t pinned_pages, i;
634 struct page **user_pages;
635 struct mm_struct *mm = current->mm;
636 int gtt_page_offset, data_page_offset, data_page_index, page_length;
637 int ret;
638 uint64_t data_ptr = args->data_ptr;
639
640 remain = args->size;
641
642 /* Pin the user pages containing the data. We can't fault while
643 * holding the struct mutex, and all of the pwrite implementations
644 * want to hold it while dereferencing the user data.
645 */
646 first_data_page = data_ptr / PAGE_SIZE;
647 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
648 num_pages = last_data_page - first_data_page + 1;
649
650 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
651 if (user_pages == NULL)
652 return -ENOMEM;
653
654 mutex_unlock(&dev->struct_mutex);
655 down_read(&mm->mmap_sem);
656 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
657 num_pages, 0, 0, user_pages, NULL);
658 up_read(&mm->mmap_sem);
659 mutex_lock(&dev->struct_mutex);
660 if (pinned_pages < num_pages) {
661 ret = -EFAULT;
662 goto out_unpin_pages;
663 }
664
665 ret = i915_gem_object_set_to_gtt_domain(obj, true);
666 if (ret)
667 goto out_unpin_pages;
668
669 ret = i915_gem_object_put_fence(obj);
670 if (ret)
671 goto out_unpin_pages;
672
673 offset = obj->gtt_offset + args->offset;
674
675 while (remain > 0) {
676 /* Operation in this page
677 *
678 * gtt_page_base = page offset within aperture
679 * gtt_page_offset = offset within page in aperture
680 * data_page_index = page number in get_user_pages return
681 * data_page_offset = offset with data_page_index page.
682 * page_length = bytes to copy for this page
683 */
684 gtt_page_base = offset & PAGE_MASK;
685 gtt_page_offset = offset_in_page(offset);
686 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
687 data_page_offset = offset_in_page(data_ptr);
688
689 page_length = remain;
690 if ((gtt_page_offset + page_length) > PAGE_SIZE)
691 page_length = PAGE_SIZE - gtt_page_offset;
692 if ((data_page_offset + page_length) > PAGE_SIZE)
693 page_length = PAGE_SIZE - data_page_offset;
694
695 slow_kernel_write(dev_priv->mm.gtt_mapping,
696 gtt_page_base, gtt_page_offset,
697 user_pages[data_page_index],
698 data_page_offset,
699 page_length);
700
701 remain -= page_length;
702 offset += page_length;
703 data_ptr += page_length;
704 }
705
706 out_unpin_pages:
707 for (i = 0; i < pinned_pages; i++)
708 page_cache_release(user_pages[i]);
709 drm_free_large(user_pages);
710
711 return ret;
712 }
713
714 /**
715 * This is the fast shmem pwrite path, which attempts to directly
716 * copy_from_user into the kmapped pages backing the object.
717 */
718 static int
i915_gem_shmem_pwrite_fast(struct drm_device * dev,struct drm_i915_gem_object * obj,struct drm_i915_gem_pwrite * args,struct drm_file * file)719 i915_gem_shmem_pwrite_fast(struct drm_device *dev,
720 struct drm_i915_gem_object *obj,
721 struct drm_i915_gem_pwrite *args,
722 struct drm_file *file)
723 {
724 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
725 ssize_t remain;
726 loff_t offset;
727 char __user *user_data;
728 int page_offset, page_length;
729
730 user_data = (char __user *) (uintptr_t) args->data_ptr;
731 remain = args->size;
732
733 offset = args->offset;
734 obj->dirty = 1;
735
736 while (remain > 0) {
737 struct page *page;
738 char *vaddr;
739 int ret;
740
741 /* Operation in this page
742 *
743 * page_offset = offset within page
744 * page_length = bytes to copy for this page
745 */
746 page_offset = offset_in_page(offset);
747 page_length = remain;
748 if ((page_offset + remain) > PAGE_SIZE)
749 page_length = PAGE_SIZE - page_offset;
750
751 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
752 if (IS_ERR(page))
753 return PTR_ERR(page);
754
755 vaddr = kmap_atomic(page);
756 ret = __copy_from_user_inatomic(vaddr + page_offset,
757 user_data,
758 page_length);
759 kunmap_atomic(vaddr);
760
761 set_page_dirty(page);
762 mark_page_accessed(page);
763 page_cache_release(page);
764
765 /* If we get a fault while copying data, then (presumably) our
766 * source page isn't available. Return the error and we'll
767 * retry in the slow path.
768 */
769 if (ret)
770 return -EFAULT;
771
772 remain -= page_length;
773 user_data += page_length;
774 offset += page_length;
775 }
776
777 return 0;
778 }
779
780 /**
781 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
782 * the memory and maps it using kmap_atomic for copying.
783 *
784 * This avoids taking mmap_sem for faulting on the user's address while the
785 * struct_mutex is held.
786 */
787 static int
i915_gem_shmem_pwrite_slow(struct drm_device * dev,struct drm_i915_gem_object * obj,struct drm_i915_gem_pwrite * args,struct drm_file * file)788 i915_gem_shmem_pwrite_slow(struct drm_device *dev,
789 struct drm_i915_gem_object *obj,
790 struct drm_i915_gem_pwrite *args,
791 struct drm_file *file)
792 {
793 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
794 ssize_t remain;
795 loff_t offset;
796 char __user *user_data;
797 int shmem_page_offset, page_length, ret;
798 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
799
800 user_data = (char __user *) (uintptr_t) args->data_ptr;
801 remain = args->size;
802
803 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
804
805 offset = args->offset;
806 obj->dirty = 1;
807
808 mutex_unlock(&dev->struct_mutex);
809
810 while (remain > 0) {
811 struct page *page;
812 char *vaddr;
813
814 /* Operation in this page
815 *
816 * shmem_page_offset = offset within page in shmem file
817 * page_length = bytes to copy for this page
818 */
819 shmem_page_offset = offset_in_page(offset);
820
821 page_length = remain;
822 if ((shmem_page_offset + page_length) > PAGE_SIZE)
823 page_length = PAGE_SIZE - shmem_page_offset;
824
825 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
826 if (IS_ERR(page)) {
827 ret = PTR_ERR(page);
828 goto out;
829 }
830
831 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
832 (page_to_phys(page) & (1 << 17)) != 0;
833
834 vaddr = kmap(page);
835 if (page_do_bit17_swizzling)
836 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
837 user_data,
838 page_length);
839 else
840 ret = __copy_from_user(vaddr + shmem_page_offset,
841 user_data,
842 page_length);
843 kunmap(page);
844
845 set_page_dirty(page);
846 mark_page_accessed(page);
847 page_cache_release(page);
848
849 if (ret) {
850 ret = -EFAULT;
851 goto out;
852 }
853
854 remain -= page_length;
855 user_data += page_length;
856 offset += page_length;
857 }
858
859 out:
860 mutex_lock(&dev->struct_mutex);
861 /* Fixup: Kill any reinstated backing storage pages */
862 if (obj->madv == __I915_MADV_PURGED)
863 i915_gem_object_truncate(obj);
864 /* and flush dirty cachelines in case the object isn't in the cpu write
865 * domain anymore. */
866 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
867 i915_gem_clflush_object(obj);
868 intel_gtt_chipset_flush();
869 }
870
871 return ret;
872 }
873
874 /**
875 * Writes data to the object referenced by handle.
876 *
877 * On error, the contents of the buffer that were to be modified are undefined.
878 */
879 int
i915_gem_pwrite_ioctl(struct drm_device * dev,void * data,struct drm_file * file)880 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
881 struct drm_file *file)
882 {
883 struct drm_i915_gem_pwrite *args = data;
884 struct drm_i915_gem_object *obj;
885 int ret;
886
887 if (args->size == 0)
888 return 0;
889
890 if (!access_ok(VERIFY_READ,
891 (char __user *)(uintptr_t)args->data_ptr,
892 args->size))
893 return -EFAULT;
894
895 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
896 args->size);
897 if (ret)
898 return -EFAULT;
899
900 ret = i915_mutex_lock_interruptible(dev);
901 if (ret)
902 return ret;
903
904 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
905 if (&obj->base == NULL) {
906 ret = -ENOENT;
907 goto unlock;
908 }
909
910 /* Bounds check destination. */
911 if (args->offset > obj->base.size ||
912 args->size > obj->base.size - args->offset) {
913 ret = -EINVAL;
914 goto out;
915 }
916
917 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
918
919 /* We can only do the GTT pwrite on untiled buffers, as otherwise
920 * it would end up going through the fenced access, and we'll get
921 * different detiling behavior between reading and writing.
922 * pread/pwrite currently are reading and writing from the CPU
923 * perspective, requiring manual detiling by the client.
924 */
925 if (obj->phys_obj) {
926 ret = i915_gem_phys_pwrite(dev, obj, args, file);
927 goto out;
928 }
929
930 if (obj->gtt_space &&
931 obj->tiling_mode == I915_TILING_NONE &&
932 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
933 ret = i915_gem_object_pin(obj, 0, true);
934 if (ret)
935 goto out;
936
937 ret = i915_gem_object_set_to_gtt_domain(obj, true);
938 if (ret)
939 goto out_unpin;
940
941 ret = i915_gem_object_put_fence(obj);
942 if (ret)
943 goto out_unpin;
944
945 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
946 if (ret == -EFAULT)
947 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
948
949 out_unpin:
950 i915_gem_object_unpin(obj);
951
952 if (ret != -EFAULT)
953 goto out;
954 /* Fall through to the shmfs paths because the gtt paths might
955 * fail with non-page-backed user pointers (e.g. gtt mappings
956 * when moving data between textures). */
957 }
958
959 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
960 if (ret)
961 goto out;
962
963 ret = -EFAULT;
964 if (!i915_gem_object_needs_bit17_swizzle(obj))
965 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
966 if (ret == -EFAULT)
967 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
968
969 out:
970 drm_gem_object_unreference(&obj->base);
971 unlock:
972 mutex_unlock(&dev->struct_mutex);
973 return ret;
974 }
975
976 /**
977 * Called when user space prepares to use an object with the CPU, either
978 * through the mmap ioctl's mapping or a GTT mapping.
979 */
980 int
i915_gem_set_domain_ioctl(struct drm_device * dev,void * data,struct drm_file * file)981 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
982 struct drm_file *file)
983 {
984 struct drm_i915_gem_set_domain *args = data;
985 struct drm_i915_gem_object *obj;
986 uint32_t read_domains = args->read_domains;
987 uint32_t write_domain = args->write_domain;
988 int ret;
989
990 if (!(dev->driver->driver_features & DRIVER_GEM))
991 return -ENODEV;
992
993 /* Only handle setting domains to types used by the CPU. */
994 if (write_domain & I915_GEM_GPU_DOMAINS)
995 return -EINVAL;
996
997 if (read_domains & I915_GEM_GPU_DOMAINS)
998 return -EINVAL;
999
1000 /* Having something in the write domain implies it's in the read
1001 * domain, and only that read domain. Enforce that in the request.
1002 */
1003 if (write_domain != 0 && read_domains != write_domain)
1004 return -EINVAL;
1005
1006 ret = i915_mutex_lock_interruptible(dev);
1007 if (ret)
1008 return ret;
1009
1010 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1011 if (&obj->base == NULL) {
1012 ret = -ENOENT;
1013 goto unlock;
1014 }
1015
1016 if (read_domains & I915_GEM_DOMAIN_GTT) {
1017 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1018
1019 /* Silently promote "you're not bound, there was nothing to do"
1020 * to success, since the client was just asking us to
1021 * make sure everything was done.
1022 */
1023 if (ret == -EINVAL)
1024 ret = 0;
1025 } else {
1026 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1027 }
1028
1029 drm_gem_object_unreference(&obj->base);
1030 unlock:
1031 mutex_unlock(&dev->struct_mutex);
1032 return ret;
1033 }
1034
1035 /**
1036 * Called when user space has done writes to this buffer
1037 */
1038 int
i915_gem_sw_finish_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1039 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1040 struct drm_file *file)
1041 {
1042 struct drm_i915_gem_sw_finish *args = data;
1043 struct drm_i915_gem_object *obj;
1044 int ret = 0;
1045
1046 if (!(dev->driver->driver_features & DRIVER_GEM))
1047 return -ENODEV;
1048
1049 ret = i915_mutex_lock_interruptible(dev);
1050 if (ret)
1051 return ret;
1052
1053 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1054 if (&obj->base == NULL) {
1055 ret = -ENOENT;
1056 goto unlock;
1057 }
1058
1059 /* Pinned buffers may be scanout, so flush the cache */
1060 if (obj->pin_count)
1061 i915_gem_object_flush_cpu_write_domain(obj);
1062
1063 drm_gem_object_unreference(&obj->base);
1064 unlock:
1065 mutex_unlock(&dev->struct_mutex);
1066 return ret;
1067 }
1068
1069 /**
1070 * Maps the contents of an object, returning the address it is mapped
1071 * into.
1072 *
1073 * While the mapping holds a reference on the contents of the object, it doesn't
1074 * imply a ref on the object itself.
1075 */
1076 int
i915_gem_mmap_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1077 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1078 struct drm_file *file)
1079 {
1080 struct drm_i915_gem_mmap *args = data;
1081 struct drm_gem_object *obj;
1082 unsigned long addr;
1083
1084 if (!(dev->driver->driver_features & DRIVER_GEM))
1085 return -ENODEV;
1086
1087 obj = drm_gem_object_lookup(dev, file, args->handle);
1088 if (obj == NULL)
1089 return -ENOENT;
1090
1091 addr = vm_mmap(obj->filp, 0, args->size,
1092 PROT_READ | PROT_WRITE, MAP_SHARED,
1093 args->offset);
1094 drm_gem_object_unreference_unlocked(obj);
1095 if (IS_ERR((void *)addr))
1096 return addr;
1097
1098 args->addr_ptr = (uint64_t) addr;
1099
1100 return 0;
1101 }
1102
1103 /**
1104 * i915_gem_fault - fault a page into the GTT
1105 * vma: VMA in question
1106 * vmf: fault info
1107 *
1108 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1109 * from userspace. The fault handler takes care of binding the object to
1110 * the GTT (if needed), allocating and programming a fence register (again,
1111 * only if needed based on whether the old reg is still valid or the object
1112 * is tiled) and inserting a new PTE into the faulting process.
1113 *
1114 * Note that the faulting process may involve evicting existing objects
1115 * from the GTT and/or fence registers to make room. So performance may
1116 * suffer if the GTT working set is large or there are few fence registers
1117 * left.
1118 */
i915_gem_fault(struct vm_area_struct * vma,struct vm_fault * vmf)1119 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1120 {
1121 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1122 struct drm_device *dev = obj->base.dev;
1123 drm_i915_private_t *dev_priv = dev->dev_private;
1124 pgoff_t page_offset;
1125 unsigned long pfn;
1126 int ret = 0;
1127 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1128
1129 /* We don't use vmf->pgoff since that has the fake offset */
1130 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1131 PAGE_SHIFT;
1132
1133 ret = i915_mutex_lock_interruptible(dev);
1134 if (ret)
1135 goto out;
1136
1137 trace_i915_gem_object_fault(obj, page_offset, true, write);
1138
1139 /* Now bind it into the GTT if needed */
1140 if (!obj->map_and_fenceable) {
1141 ret = i915_gem_object_unbind(obj);
1142 if (ret)
1143 goto unlock;
1144 }
1145 if (!obj->gtt_space) {
1146 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
1147 if (ret)
1148 goto unlock;
1149
1150 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1151 if (ret)
1152 goto unlock;
1153 }
1154
1155 if (obj->tiling_mode == I915_TILING_NONE)
1156 ret = i915_gem_object_put_fence(obj);
1157 else
1158 ret = i915_gem_object_get_fence(obj, NULL);
1159 if (ret)
1160 goto unlock;
1161
1162 if (i915_gem_object_is_inactive(obj))
1163 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1164
1165 obj->fault_mappable = true;
1166
1167 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
1168 page_offset;
1169
1170 /* Finally, remap it using the new GTT offset */
1171 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1172 unlock:
1173 mutex_unlock(&dev->struct_mutex);
1174 out:
1175 switch (ret) {
1176 case -EIO:
1177 case -EAGAIN:
1178 /* Give the error handler a chance to run and move the
1179 * objects off the GPU active list. Next time we service the
1180 * fault, we should be able to transition the page into the
1181 * GTT without touching the GPU (and so avoid further
1182 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1183 * with coherency, just lost writes.
1184 */
1185 set_need_resched();
1186 case 0:
1187 case -ERESTARTSYS:
1188 case -EINTR:
1189 case -EBUSY:
1190 /*
1191 * EBUSY is ok: this just means that another thread
1192 * already did the job.
1193 */
1194 return VM_FAULT_NOPAGE;
1195 case -ENOMEM:
1196 return VM_FAULT_OOM;
1197 default:
1198 return VM_FAULT_SIGBUS;
1199 }
1200 }
1201
1202 /**
1203 * i915_gem_release_mmap - remove physical page mappings
1204 * @obj: obj in question
1205 *
1206 * Preserve the reservation of the mmapping with the DRM core code, but
1207 * relinquish ownership of the pages back to the system.
1208 *
1209 * It is vital that we remove the page mapping if we have mapped a tiled
1210 * object through the GTT and then lose the fence register due to
1211 * resource pressure. Similarly if the object has been moved out of the
1212 * aperture, than pages mapped into userspace must be revoked. Removing the
1213 * mapping will then trigger a page fault on the next user access, allowing
1214 * fixup by i915_gem_fault().
1215 */
1216 void
i915_gem_release_mmap(struct drm_i915_gem_object * obj)1217 i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1218 {
1219 if (!obj->fault_mappable)
1220 return;
1221
1222 if (obj->base.dev->dev_mapping)
1223 unmap_mapping_range(obj->base.dev->dev_mapping,
1224 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1225 obj->base.size, 1);
1226
1227 obj->fault_mappable = false;
1228 }
1229
1230 static uint32_t
i915_gem_get_gtt_size(struct drm_device * dev,uint32_t size,int tiling_mode)1231 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
1232 {
1233 uint32_t gtt_size;
1234
1235 if (INTEL_INFO(dev)->gen >= 4 ||
1236 tiling_mode == I915_TILING_NONE)
1237 return size;
1238
1239 /* Previous chips need a power-of-two fence region when tiling */
1240 if (INTEL_INFO(dev)->gen == 3)
1241 gtt_size = 1024*1024;
1242 else
1243 gtt_size = 512*1024;
1244
1245 while (gtt_size < size)
1246 gtt_size <<= 1;
1247
1248 return gtt_size;
1249 }
1250
1251 /**
1252 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1253 * @obj: object to check
1254 *
1255 * Return the required GTT alignment for an object, taking into account
1256 * potential fence register mapping.
1257 */
1258 static uint32_t
i915_gem_get_gtt_alignment(struct drm_device * dev,uint32_t size,int tiling_mode)1259 i915_gem_get_gtt_alignment(struct drm_device *dev,
1260 uint32_t size,
1261 int tiling_mode)
1262 {
1263 /*
1264 * Minimum alignment is 4k (GTT page size), but might be greater
1265 * if a fence register is needed for the object.
1266 */
1267 if (INTEL_INFO(dev)->gen >= 4 ||
1268 tiling_mode == I915_TILING_NONE)
1269 return 4096;
1270
1271 /*
1272 * Previous chips need to be aligned to the size of the smallest
1273 * fence register that can contain the object.
1274 */
1275 return i915_gem_get_gtt_size(dev, size, tiling_mode);
1276 }
1277
1278 /**
1279 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1280 * unfenced object
1281 * @dev: the device
1282 * @size: size of the object
1283 * @tiling_mode: tiling mode of the object
1284 *
1285 * Return the required GTT alignment for an object, only taking into account
1286 * unfenced tiled surface requirements.
1287 */
1288 uint32_t
i915_gem_get_unfenced_gtt_alignment(struct drm_device * dev,uint32_t size,int tiling_mode)1289 i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1290 uint32_t size,
1291 int tiling_mode)
1292 {
1293 /*
1294 * Minimum alignment is 4k (GTT page size) for sane hw.
1295 */
1296 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1297 tiling_mode == I915_TILING_NONE)
1298 return 4096;
1299
1300 /* Previous hardware however needs to be aligned to a power-of-two
1301 * tile height. The simplest method for determining this is to reuse
1302 * the power-of-tile object size.
1303 */
1304 return i915_gem_get_gtt_size(dev, size, tiling_mode);
1305 }
1306
1307 int
i915_gem_mmap_gtt(struct drm_file * file,struct drm_device * dev,uint32_t handle,uint64_t * offset)1308 i915_gem_mmap_gtt(struct drm_file *file,
1309 struct drm_device *dev,
1310 uint32_t handle,
1311 uint64_t *offset)
1312 {
1313 struct drm_i915_private *dev_priv = dev->dev_private;
1314 struct drm_i915_gem_object *obj;
1315 int ret;
1316
1317 if (!(dev->driver->driver_features & DRIVER_GEM))
1318 return -ENODEV;
1319
1320 ret = i915_mutex_lock_interruptible(dev);
1321 if (ret)
1322 return ret;
1323
1324 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
1325 if (&obj->base == NULL) {
1326 ret = -ENOENT;
1327 goto unlock;
1328 }
1329
1330 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
1331 ret = -E2BIG;
1332 goto out;
1333 }
1334
1335 if (obj->madv != I915_MADV_WILLNEED) {
1336 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1337 ret = -EINVAL;
1338 goto out;
1339 }
1340
1341 if (!obj->base.map_list.map) {
1342 ret = drm_gem_create_mmap_offset(&obj->base);
1343 if (ret)
1344 goto out;
1345 }
1346
1347 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
1348
1349 out:
1350 drm_gem_object_unreference(&obj->base);
1351 unlock:
1352 mutex_unlock(&dev->struct_mutex);
1353 return ret;
1354 }
1355
1356 /**
1357 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1358 * @dev: DRM device
1359 * @data: GTT mapping ioctl data
1360 * @file: GEM object info
1361 *
1362 * Simply returns the fake offset to userspace so it can mmap it.
1363 * The mmap call will end up in drm_gem_mmap(), which will set things
1364 * up so we can get faults in the handler above.
1365 *
1366 * The fault handler will take care of binding the object into the GTT
1367 * (since it may have been evicted to make room for something), allocating
1368 * a fence register, and mapping the appropriate aperture address into
1369 * userspace.
1370 */
1371 int
i915_gem_mmap_gtt_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1372 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1373 struct drm_file *file)
1374 {
1375 struct drm_i915_gem_mmap_gtt *args = data;
1376
1377 if (!(dev->driver->driver_features & DRIVER_GEM))
1378 return -ENODEV;
1379
1380 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1381 }
1382
1383
1384 static int
i915_gem_object_get_pages_gtt(struct drm_i915_gem_object * obj,gfp_t gfpmask)1385 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
1386 gfp_t gfpmask)
1387 {
1388 int page_count, i;
1389 struct address_space *mapping;
1390 struct inode *inode;
1391 struct page *page;
1392
1393 /* Get the list of pages out of our struct file. They'll be pinned
1394 * at this point until we release them.
1395 */
1396 page_count = obj->base.size / PAGE_SIZE;
1397 BUG_ON(obj->pages != NULL);
1398 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1399 if (obj->pages == NULL)
1400 return -ENOMEM;
1401
1402 inode = obj->base.filp->f_path.dentry->d_inode;
1403 mapping = inode->i_mapping;
1404 gfpmask |= mapping_gfp_mask(mapping);
1405
1406 for (i = 0; i < page_count; i++) {
1407 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
1408 if (IS_ERR(page))
1409 goto err_pages;
1410
1411 obj->pages[i] = page;
1412 }
1413
1414 if (i915_gem_object_needs_bit17_swizzle(obj))
1415 i915_gem_object_do_bit_17_swizzle(obj);
1416
1417 return 0;
1418
1419 err_pages:
1420 while (i--)
1421 page_cache_release(obj->pages[i]);
1422
1423 drm_free_large(obj->pages);
1424 obj->pages = NULL;
1425 return PTR_ERR(page);
1426 }
1427
1428 static void
i915_gem_object_put_pages_gtt(struct drm_i915_gem_object * obj)1429 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1430 {
1431 int page_count = obj->base.size / PAGE_SIZE;
1432 int i;
1433
1434 BUG_ON(obj->madv == __I915_MADV_PURGED);
1435
1436 if (i915_gem_object_needs_bit17_swizzle(obj))
1437 i915_gem_object_save_bit_17_swizzle(obj);
1438
1439 if (obj->madv == I915_MADV_DONTNEED)
1440 obj->dirty = 0;
1441
1442 for (i = 0; i < page_count; i++) {
1443 if (obj->dirty)
1444 set_page_dirty(obj->pages[i]);
1445
1446 if (obj->madv == I915_MADV_WILLNEED)
1447 mark_page_accessed(obj->pages[i]);
1448
1449 page_cache_release(obj->pages[i]);
1450 }
1451 obj->dirty = 0;
1452
1453 drm_free_large(obj->pages);
1454 obj->pages = NULL;
1455 }
1456
1457 void
i915_gem_object_move_to_active(struct drm_i915_gem_object * obj,struct intel_ring_buffer * ring,u32 seqno)1458 i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
1459 struct intel_ring_buffer *ring,
1460 u32 seqno)
1461 {
1462 struct drm_device *dev = obj->base.dev;
1463 struct drm_i915_private *dev_priv = dev->dev_private;
1464
1465 BUG_ON(ring == NULL);
1466 obj->ring = ring;
1467
1468 /* Add a reference if we're newly entering the active list. */
1469 if (!obj->active) {
1470 drm_gem_object_reference(&obj->base);
1471 obj->active = 1;
1472 }
1473
1474 /* Move from whatever list we were on to the tail of execution. */
1475 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1476 list_move_tail(&obj->ring_list, &ring->active_list);
1477
1478 obj->last_rendering_seqno = seqno;
1479
1480 if (obj->fenced_gpu_access) {
1481 obj->last_fenced_seqno = seqno;
1482 obj->last_fenced_ring = ring;
1483
1484 /* Bump MRU to take account of the delayed flush */
1485 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1486 struct drm_i915_fence_reg *reg;
1487
1488 reg = &dev_priv->fence_regs[obj->fence_reg];
1489 list_move_tail(®->lru_list,
1490 &dev_priv->mm.fence_list);
1491 }
1492 }
1493 }
1494
1495 static void
i915_gem_object_move_off_active(struct drm_i915_gem_object * obj)1496 i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1497 {
1498 list_del_init(&obj->ring_list);
1499 obj->last_rendering_seqno = 0;
1500 obj->last_fenced_seqno = 0;
1501 }
1502
1503 static void
i915_gem_object_move_to_flushing(struct drm_i915_gem_object * obj)1504 i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
1505 {
1506 struct drm_device *dev = obj->base.dev;
1507 drm_i915_private_t *dev_priv = dev->dev_private;
1508
1509 BUG_ON(!obj->active);
1510 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
1511
1512 i915_gem_object_move_off_active(obj);
1513 }
1514
1515 static void
i915_gem_object_move_to_inactive(struct drm_i915_gem_object * obj)1516 i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1517 {
1518 struct drm_device *dev = obj->base.dev;
1519 struct drm_i915_private *dev_priv = dev->dev_private;
1520
1521 if (obj->pin_count != 0)
1522 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1523 else
1524 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1525
1526 BUG_ON(!list_empty(&obj->gpu_write_list));
1527 BUG_ON(!obj->active);
1528 obj->ring = NULL;
1529 obj->last_fenced_ring = NULL;
1530
1531 i915_gem_object_move_off_active(obj);
1532 obj->fenced_gpu_access = false;
1533
1534 obj->active = 0;
1535 obj->pending_gpu_write = false;
1536 drm_gem_object_unreference(&obj->base);
1537
1538 WARN_ON(i915_verify_lists(dev));
1539 }
1540
1541 /* Immediately discard the backing storage */
1542 static void
i915_gem_object_truncate(struct drm_i915_gem_object * obj)1543 i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1544 {
1545 struct inode *inode;
1546
1547 /* Our goal here is to return as much of the memory as
1548 * is possible back to the system as we are called from OOM.
1549 * To do this we must instruct the shmfs to drop all of its
1550 * backing pages, *now*.
1551 */
1552 inode = obj->base.filp->f_path.dentry->d_inode;
1553 shmem_truncate_range(inode, 0, (loff_t)-1);
1554
1555 obj->madv = __I915_MADV_PURGED;
1556 }
1557
1558 static inline int
i915_gem_object_is_purgeable(struct drm_i915_gem_object * obj)1559 i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1560 {
1561 return obj->madv == I915_MADV_DONTNEED;
1562 }
1563
1564 static void
i915_gem_process_flushing_list(struct intel_ring_buffer * ring,uint32_t flush_domains)1565 i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1566 uint32_t flush_domains)
1567 {
1568 struct drm_i915_gem_object *obj, *next;
1569
1570 list_for_each_entry_safe(obj, next,
1571 &ring->gpu_write_list,
1572 gpu_write_list) {
1573 if (obj->base.write_domain & flush_domains) {
1574 uint32_t old_write_domain = obj->base.write_domain;
1575
1576 obj->base.write_domain = 0;
1577 list_del_init(&obj->gpu_write_list);
1578 i915_gem_object_move_to_active(obj, ring,
1579 i915_gem_next_request_seqno(ring));
1580
1581 trace_i915_gem_object_change_domain(obj,
1582 obj->base.read_domains,
1583 old_write_domain);
1584 }
1585 }
1586 }
1587
1588 static u32
i915_gem_get_seqno(struct drm_device * dev)1589 i915_gem_get_seqno(struct drm_device *dev)
1590 {
1591 drm_i915_private_t *dev_priv = dev->dev_private;
1592 u32 seqno = dev_priv->next_seqno;
1593
1594 /* reserve 0 for non-seqno */
1595 if (++dev_priv->next_seqno == 0)
1596 dev_priv->next_seqno = 1;
1597
1598 return seqno;
1599 }
1600
1601 u32
i915_gem_next_request_seqno(struct intel_ring_buffer * ring)1602 i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
1603 {
1604 if (ring->outstanding_lazy_request == 0)
1605 ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
1606
1607 return ring->outstanding_lazy_request;
1608 }
1609
1610 int
i915_add_request(struct intel_ring_buffer * ring,struct drm_file * file,struct drm_i915_gem_request * request)1611 i915_add_request(struct intel_ring_buffer *ring,
1612 struct drm_file *file,
1613 struct drm_i915_gem_request *request)
1614 {
1615 drm_i915_private_t *dev_priv = ring->dev->dev_private;
1616 uint32_t seqno;
1617 u32 request_ring_position;
1618 int was_empty;
1619 int ret;
1620
1621 BUG_ON(request == NULL);
1622 seqno = i915_gem_next_request_seqno(ring);
1623
1624 /* Record the position of the start of the request so that
1625 * should we detect the updated seqno part-way through the
1626 * GPU processing the request, we never over-estimate the
1627 * position of the head.
1628 */
1629 request_ring_position = intel_ring_get_tail(ring);
1630
1631 ret = ring->add_request(ring, &seqno);
1632 if (ret)
1633 return ret;
1634
1635 trace_i915_gem_request_add(ring, seqno);
1636
1637 request->seqno = seqno;
1638 request->ring = ring;
1639 request->tail = request_ring_position;
1640 request->emitted_jiffies = jiffies;
1641 was_empty = list_empty(&ring->request_list);
1642 list_add_tail(&request->list, &ring->request_list);
1643
1644 if (file) {
1645 struct drm_i915_file_private *file_priv = file->driver_priv;
1646
1647 spin_lock(&file_priv->mm.lock);
1648 request->file_priv = file_priv;
1649 list_add_tail(&request->client_list,
1650 &file_priv->mm.request_list);
1651 spin_unlock(&file_priv->mm.lock);
1652 }
1653
1654 ring->outstanding_lazy_request = 0;
1655
1656 if (!dev_priv->mm.suspended) {
1657 if (i915_enable_hangcheck) {
1658 mod_timer(&dev_priv->hangcheck_timer,
1659 jiffies +
1660 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1661 }
1662 if (was_empty)
1663 queue_delayed_work(dev_priv->wq,
1664 &dev_priv->mm.retire_work, HZ);
1665 }
1666 return 0;
1667 }
1668
1669 static inline void
i915_gem_request_remove_from_client(struct drm_i915_gem_request * request)1670 i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1671 {
1672 struct drm_i915_file_private *file_priv = request->file_priv;
1673
1674 if (!file_priv)
1675 return;
1676
1677 spin_lock(&file_priv->mm.lock);
1678 if (request->file_priv) {
1679 list_del(&request->client_list);
1680 request->file_priv = NULL;
1681 }
1682 spin_unlock(&file_priv->mm.lock);
1683 }
1684
i915_gem_reset_ring_lists(struct drm_i915_private * dev_priv,struct intel_ring_buffer * ring)1685 static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1686 struct intel_ring_buffer *ring)
1687 {
1688 while (!list_empty(&ring->request_list)) {
1689 struct drm_i915_gem_request *request;
1690
1691 request = list_first_entry(&ring->request_list,
1692 struct drm_i915_gem_request,
1693 list);
1694
1695 list_del(&request->list);
1696 i915_gem_request_remove_from_client(request);
1697 kfree(request);
1698 }
1699
1700 while (!list_empty(&ring->active_list)) {
1701 struct drm_i915_gem_object *obj;
1702
1703 obj = list_first_entry(&ring->active_list,
1704 struct drm_i915_gem_object,
1705 ring_list);
1706
1707 obj->base.write_domain = 0;
1708 list_del_init(&obj->gpu_write_list);
1709 i915_gem_object_move_to_inactive(obj);
1710 }
1711 }
1712
i915_gem_reset_fences(struct drm_device * dev)1713 static void i915_gem_reset_fences(struct drm_device *dev)
1714 {
1715 struct drm_i915_private *dev_priv = dev->dev_private;
1716 int i;
1717
1718 for (i = 0; i < dev_priv->num_fence_regs; i++) {
1719 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
1720 struct drm_i915_gem_object *obj = reg->obj;
1721
1722 if (!obj)
1723 continue;
1724
1725 if (obj->tiling_mode)
1726 i915_gem_release_mmap(obj);
1727
1728 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1729 reg->obj->fenced_gpu_access = false;
1730 reg->obj->last_fenced_seqno = 0;
1731 reg->obj->last_fenced_ring = NULL;
1732 i915_gem_clear_fence_reg(dev, reg);
1733 }
1734 }
1735
i915_gem_reset(struct drm_device * dev)1736 void i915_gem_reset(struct drm_device *dev)
1737 {
1738 struct drm_i915_private *dev_priv = dev->dev_private;
1739 struct drm_i915_gem_object *obj;
1740 int i;
1741
1742 for (i = 0; i < I915_NUM_RINGS; i++)
1743 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
1744
1745 /* Remove anything from the flushing lists. The GPU cache is likely
1746 * to be lost on reset along with the data, so simply move the
1747 * lost bo to the inactive list.
1748 */
1749 while (!list_empty(&dev_priv->mm.flushing_list)) {
1750 obj = list_first_entry(&dev_priv->mm.flushing_list,
1751 struct drm_i915_gem_object,
1752 mm_list);
1753
1754 obj->base.write_domain = 0;
1755 list_del_init(&obj->gpu_write_list);
1756 i915_gem_object_move_to_inactive(obj);
1757 }
1758
1759 /* Move everything out of the GPU domains to ensure we do any
1760 * necessary invalidation upon reuse.
1761 */
1762 list_for_each_entry(obj,
1763 &dev_priv->mm.inactive_list,
1764 mm_list)
1765 {
1766 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1767 }
1768
1769 /* The fence registers are invalidated so clear them out */
1770 i915_gem_reset_fences(dev);
1771 }
1772
1773 /**
1774 * This function clears the request list as sequence numbers are passed.
1775 */
1776 void
i915_gem_retire_requests_ring(struct intel_ring_buffer * ring)1777 i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
1778 {
1779 uint32_t seqno;
1780 int i;
1781
1782 if (list_empty(&ring->request_list))
1783 return;
1784
1785 WARN_ON(i915_verify_lists(ring->dev));
1786
1787 seqno = ring->get_seqno(ring);
1788
1789 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
1790 if (seqno >= ring->sync_seqno[i])
1791 ring->sync_seqno[i] = 0;
1792
1793 while (!list_empty(&ring->request_list)) {
1794 struct drm_i915_gem_request *request;
1795
1796 request = list_first_entry(&ring->request_list,
1797 struct drm_i915_gem_request,
1798 list);
1799
1800 if (!i915_seqno_passed(seqno, request->seqno))
1801 break;
1802
1803 trace_i915_gem_request_retire(ring, request->seqno);
1804 /* We know the GPU must have read the request to have
1805 * sent us the seqno + interrupt, so use the position
1806 * of tail of the request to update the last known position
1807 * of the GPU head.
1808 */
1809 ring->last_retired_head = request->tail;
1810
1811 list_del(&request->list);
1812 i915_gem_request_remove_from_client(request);
1813 kfree(request);
1814 }
1815
1816 /* Move any buffers on the active list that are no longer referenced
1817 * by the ringbuffer to the flushing/inactive lists as appropriate.
1818 */
1819 while (!list_empty(&ring->active_list)) {
1820 struct drm_i915_gem_object *obj;
1821
1822 obj = list_first_entry(&ring->active_list,
1823 struct drm_i915_gem_object,
1824 ring_list);
1825
1826 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
1827 break;
1828
1829 if (obj->base.write_domain != 0)
1830 i915_gem_object_move_to_flushing(obj);
1831 else
1832 i915_gem_object_move_to_inactive(obj);
1833 }
1834
1835 if (unlikely(ring->trace_irq_seqno &&
1836 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
1837 ring->irq_put(ring);
1838 ring->trace_irq_seqno = 0;
1839 }
1840
1841 WARN_ON(i915_verify_lists(ring->dev));
1842 }
1843
1844 void
i915_gem_retire_requests(struct drm_device * dev)1845 i915_gem_retire_requests(struct drm_device *dev)
1846 {
1847 drm_i915_private_t *dev_priv = dev->dev_private;
1848 int i;
1849
1850 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1851 struct drm_i915_gem_object *obj, *next;
1852
1853 /* We must be careful that during unbind() we do not
1854 * accidentally infinitely recurse into retire requests.
1855 * Currently:
1856 * retire -> free -> unbind -> wait -> retire_ring
1857 */
1858 list_for_each_entry_safe(obj, next,
1859 &dev_priv->mm.deferred_free_list,
1860 mm_list)
1861 i915_gem_free_object_tail(obj);
1862 }
1863
1864 for (i = 0; i < I915_NUM_RINGS; i++)
1865 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
1866 }
1867
1868 static void
i915_gem_retire_work_handler(struct work_struct * work)1869 i915_gem_retire_work_handler(struct work_struct *work)
1870 {
1871 drm_i915_private_t *dev_priv;
1872 struct drm_device *dev;
1873 bool idle;
1874 int i;
1875
1876 dev_priv = container_of(work, drm_i915_private_t,
1877 mm.retire_work.work);
1878 dev = dev_priv->dev;
1879
1880 /* Come back later if the device is busy... */
1881 if (!mutex_trylock(&dev->struct_mutex)) {
1882 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1883 return;
1884 }
1885
1886 i915_gem_retire_requests(dev);
1887
1888 /* Send a periodic flush down the ring so we don't hold onto GEM
1889 * objects indefinitely.
1890 */
1891 idle = true;
1892 for (i = 0; i < I915_NUM_RINGS; i++) {
1893 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1894
1895 if (!list_empty(&ring->gpu_write_list)) {
1896 struct drm_i915_gem_request *request;
1897 int ret;
1898
1899 ret = i915_gem_flush_ring(ring,
1900 0, I915_GEM_GPU_DOMAINS);
1901 request = kzalloc(sizeof(*request), GFP_KERNEL);
1902 if (ret || request == NULL ||
1903 i915_add_request(ring, NULL, request))
1904 kfree(request);
1905 }
1906
1907 idle &= list_empty(&ring->request_list);
1908 }
1909
1910 if (!dev_priv->mm.suspended && !idle)
1911 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1912
1913 mutex_unlock(&dev->struct_mutex);
1914 }
1915
1916 /**
1917 * Waits for a sequence number to be signaled, and cleans up the
1918 * request and object lists appropriately for that event.
1919 */
1920 int
i915_wait_request(struct intel_ring_buffer * ring,uint32_t seqno,bool do_retire)1921 i915_wait_request(struct intel_ring_buffer *ring,
1922 uint32_t seqno,
1923 bool do_retire)
1924 {
1925 drm_i915_private_t *dev_priv = ring->dev->dev_private;
1926 u32 ier;
1927 int ret = 0;
1928
1929 BUG_ON(seqno == 0);
1930
1931 if (atomic_read(&dev_priv->mm.wedged)) {
1932 struct completion *x = &dev_priv->error_completion;
1933 bool recovery_complete;
1934 unsigned long flags;
1935
1936 /* Give the error handler a chance to run. */
1937 spin_lock_irqsave(&x->wait.lock, flags);
1938 recovery_complete = x->done > 0;
1939 spin_unlock_irqrestore(&x->wait.lock, flags);
1940
1941 return recovery_complete ? -EIO : -EAGAIN;
1942 }
1943
1944 if (seqno == ring->outstanding_lazy_request) {
1945 struct drm_i915_gem_request *request;
1946
1947 request = kzalloc(sizeof(*request), GFP_KERNEL);
1948 if (request == NULL)
1949 return -ENOMEM;
1950
1951 ret = i915_add_request(ring, NULL, request);
1952 if (ret) {
1953 kfree(request);
1954 return ret;
1955 }
1956
1957 seqno = request->seqno;
1958 }
1959
1960 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
1961 if (HAS_PCH_SPLIT(ring->dev))
1962 ier = I915_READ(DEIER) | I915_READ(GTIER);
1963 else
1964 ier = I915_READ(IER);
1965 if (!ier) {
1966 DRM_ERROR("something (likely vbetool) disabled "
1967 "interrupts, re-enabling\n");
1968 ring->dev->driver->irq_preinstall(ring->dev);
1969 ring->dev->driver->irq_postinstall(ring->dev);
1970 }
1971
1972 trace_i915_gem_request_wait_begin(ring, seqno);
1973
1974 ring->waiting_seqno = seqno;
1975 if (ring->irq_get(ring)) {
1976 if (dev_priv->mm.interruptible)
1977 ret = wait_event_interruptible(ring->irq_queue,
1978 i915_seqno_passed(ring->get_seqno(ring), seqno)
1979 || atomic_read(&dev_priv->mm.wedged));
1980 else
1981 wait_event(ring->irq_queue,
1982 i915_seqno_passed(ring->get_seqno(ring), seqno)
1983 || atomic_read(&dev_priv->mm.wedged));
1984
1985 ring->irq_put(ring);
1986 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
1987 seqno) ||
1988 atomic_read(&dev_priv->mm.wedged), 3000))
1989 ret = -EBUSY;
1990 ring->waiting_seqno = 0;
1991
1992 trace_i915_gem_request_wait_end(ring, seqno);
1993 }
1994 if (atomic_read(&dev_priv->mm.wedged))
1995 ret = -EAGAIN;
1996
1997 /* Directly dispatch request retiring. While we have the work queue
1998 * to handle this, the waiter on a request often wants an associated
1999 * buffer to have made it to the inactive list, and we would need
2000 * a separate wait queue to handle that.
2001 */
2002 if (ret == 0 && do_retire)
2003 i915_gem_retire_requests_ring(ring);
2004
2005 return ret;
2006 }
2007
2008 /**
2009 * Ensures that all rendering to the object has completed and the object is
2010 * safe to unbind from the GTT or access from the CPU.
2011 */
2012 int
i915_gem_object_wait_rendering(struct drm_i915_gem_object * obj)2013 i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
2014 {
2015 int ret;
2016
2017 /* This function only exists to support waiting for existing rendering,
2018 * not for emitting required flushes.
2019 */
2020 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
2021
2022 /* If there is rendering queued on the buffer being evicted, wait for
2023 * it.
2024 */
2025 if (obj->active) {
2026 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno,
2027 true);
2028 if (ret)
2029 return ret;
2030 }
2031
2032 return 0;
2033 }
2034
i915_gem_object_finish_gtt(struct drm_i915_gem_object * obj)2035 static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2036 {
2037 u32 old_write_domain, old_read_domains;
2038
2039 /* Act a barrier for all accesses through the GTT */
2040 mb();
2041
2042 /* Force a pagefault for domain tracking on next user access */
2043 i915_gem_release_mmap(obj);
2044
2045 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2046 return;
2047
2048 old_read_domains = obj->base.read_domains;
2049 old_write_domain = obj->base.write_domain;
2050
2051 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2052 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2053
2054 trace_i915_gem_object_change_domain(obj,
2055 old_read_domains,
2056 old_write_domain);
2057 }
2058
2059 /**
2060 * Unbinds an object from the GTT aperture.
2061 */
2062 int
i915_gem_object_unbind(struct drm_i915_gem_object * obj)2063 i915_gem_object_unbind(struct drm_i915_gem_object *obj)
2064 {
2065 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
2066 int ret = 0;
2067
2068 if (obj->gtt_space == NULL)
2069 return 0;
2070
2071 if (obj->pin_count != 0) {
2072 DRM_ERROR("Attempting to unbind pinned buffer\n");
2073 return -EINVAL;
2074 }
2075
2076 ret = i915_gem_object_finish_gpu(obj);
2077 if (ret == -ERESTARTSYS)
2078 return ret;
2079 /* Continue on if we fail due to EIO, the GPU is hung so we
2080 * should be safe and we need to cleanup or else we might
2081 * cause memory corruption through use-after-free.
2082 */
2083
2084 i915_gem_object_finish_gtt(obj);
2085
2086 /* Move the object to the CPU domain to ensure that
2087 * any possible CPU writes while it's not in the GTT
2088 * are flushed when we go to remap it.
2089 */
2090 if (ret == 0)
2091 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2092 if (ret == -ERESTARTSYS)
2093 return ret;
2094 if (ret) {
2095 /* In the event of a disaster, abandon all caches and
2096 * hope for the best.
2097 */
2098 i915_gem_clflush_object(obj);
2099 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2100 }
2101
2102 /* release the fence reg _after_ flushing */
2103 ret = i915_gem_object_put_fence(obj);
2104 if (ret == -ERESTARTSYS)
2105 return ret;
2106
2107 trace_i915_gem_object_unbind(obj);
2108
2109 i915_gem_gtt_unbind_object(obj);
2110 if (obj->has_aliasing_ppgtt_mapping) {
2111 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2112 obj->has_aliasing_ppgtt_mapping = 0;
2113 }
2114
2115 i915_gem_object_put_pages_gtt(obj);
2116
2117 list_del_init(&obj->gtt_list);
2118 list_del_init(&obj->mm_list);
2119 /* Avoid an unnecessary call to unbind on rebind. */
2120 obj->map_and_fenceable = true;
2121
2122 drm_mm_put_block(obj->gtt_space);
2123 obj->gtt_space = NULL;
2124 obj->gtt_offset = 0;
2125
2126 if (i915_gem_object_is_purgeable(obj))
2127 i915_gem_object_truncate(obj);
2128
2129 return ret;
2130 }
2131
2132 int
i915_gem_flush_ring(struct intel_ring_buffer * ring,uint32_t invalidate_domains,uint32_t flush_domains)2133 i915_gem_flush_ring(struct intel_ring_buffer *ring,
2134 uint32_t invalidate_domains,
2135 uint32_t flush_domains)
2136 {
2137 int ret;
2138
2139 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2140 return 0;
2141
2142 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2143
2144 ret = ring->flush(ring, invalidate_domains, flush_domains);
2145 if (ret)
2146 return ret;
2147
2148 if (flush_domains & I915_GEM_GPU_DOMAINS)
2149 i915_gem_process_flushing_list(ring, flush_domains);
2150
2151 return 0;
2152 }
2153
i915_ring_idle(struct intel_ring_buffer * ring,bool do_retire)2154 static int i915_ring_idle(struct intel_ring_buffer *ring, bool do_retire)
2155 {
2156 int ret;
2157
2158 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
2159 return 0;
2160
2161 if (!list_empty(&ring->gpu_write_list)) {
2162 ret = i915_gem_flush_ring(ring,
2163 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2164 if (ret)
2165 return ret;
2166 }
2167
2168 return i915_wait_request(ring, i915_gem_next_request_seqno(ring),
2169 do_retire);
2170 }
2171
i915_gpu_idle(struct drm_device * dev,bool do_retire)2172 int i915_gpu_idle(struct drm_device *dev, bool do_retire)
2173 {
2174 drm_i915_private_t *dev_priv = dev->dev_private;
2175 int ret, i;
2176
2177 /* Flush everything onto the inactive list. */
2178 for (i = 0; i < I915_NUM_RINGS; i++) {
2179 ret = i915_ring_idle(&dev_priv->ring[i], do_retire);
2180 if (ret)
2181 return ret;
2182 }
2183
2184 return 0;
2185 }
2186
sandybridge_write_fence_reg(struct drm_i915_gem_object * obj,struct intel_ring_buffer * pipelined)2187 static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2188 struct intel_ring_buffer *pipelined)
2189 {
2190 struct drm_device *dev = obj->base.dev;
2191 drm_i915_private_t *dev_priv = dev->dev_private;
2192 u32 size = obj->gtt_space->size;
2193 int regnum = obj->fence_reg;
2194 uint64_t val;
2195
2196 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2197 0xfffff000) << 32;
2198 val |= obj->gtt_offset & 0xfffff000;
2199 val |= (uint64_t)((obj->stride / 128) - 1) <<
2200 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2201
2202 if (obj->tiling_mode == I915_TILING_Y)
2203 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2204 val |= I965_FENCE_REG_VALID;
2205
2206 if (pipelined) {
2207 int ret = intel_ring_begin(pipelined, 6);
2208 if (ret)
2209 return ret;
2210
2211 intel_ring_emit(pipelined, MI_NOOP);
2212 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2213 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2214 intel_ring_emit(pipelined, (u32)val);
2215 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2216 intel_ring_emit(pipelined, (u32)(val >> 32));
2217 intel_ring_advance(pipelined);
2218 } else
2219 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2220
2221 return 0;
2222 }
2223
i965_write_fence_reg(struct drm_i915_gem_object * obj,struct intel_ring_buffer * pipelined)2224 static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2225 struct intel_ring_buffer *pipelined)
2226 {
2227 struct drm_device *dev = obj->base.dev;
2228 drm_i915_private_t *dev_priv = dev->dev_private;
2229 u32 size = obj->gtt_space->size;
2230 int regnum = obj->fence_reg;
2231 uint64_t val;
2232
2233 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2234 0xfffff000) << 32;
2235 val |= obj->gtt_offset & 0xfffff000;
2236 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2237 if (obj->tiling_mode == I915_TILING_Y)
2238 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2239 val |= I965_FENCE_REG_VALID;
2240
2241 if (pipelined) {
2242 int ret = intel_ring_begin(pipelined, 6);
2243 if (ret)
2244 return ret;
2245
2246 intel_ring_emit(pipelined, MI_NOOP);
2247 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2248 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2249 intel_ring_emit(pipelined, (u32)val);
2250 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2251 intel_ring_emit(pipelined, (u32)(val >> 32));
2252 intel_ring_advance(pipelined);
2253 } else
2254 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2255
2256 return 0;
2257 }
2258
i915_write_fence_reg(struct drm_i915_gem_object * obj,struct intel_ring_buffer * pipelined)2259 static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2260 struct intel_ring_buffer *pipelined)
2261 {
2262 struct drm_device *dev = obj->base.dev;
2263 drm_i915_private_t *dev_priv = dev->dev_private;
2264 u32 size = obj->gtt_space->size;
2265 u32 fence_reg, val, pitch_val;
2266 int tile_width;
2267
2268 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2269 (size & -size) != size ||
2270 (obj->gtt_offset & (size - 1)),
2271 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2272 obj->gtt_offset, obj->map_and_fenceable, size))
2273 return -EINVAL;
2274
2275 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2276 tile_width = 128;
2277 else
2278 tile_width = 512;
2279
2280 /* Note: pitch better be a power of two tile widths */
2281 pitch_val = obj->stride / tile_width;
2282 pitch_val = ffs(pitch_val) - 1;
2283
2284 val = obj->gtt_offset;
2285 if (obj->tiling_mode == I915_TILING_Y)
2286 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2287 val |= I915_FENCE_SIZE_BITS(size);
2288 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2289 val |= I830_FENCE_REG_VALID;
2290
2291 fence_reg = obj->fence_reg;
2292 if (fence_reg < 8)
2293 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2294 else
2295 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2296
2297 if (pipelined) {
2298 int ret = intel_ring_begin(pipelined, 4);
2299 if (ret)
2300 return ret;
2301
2302 intel_ring_emit(pipelined, MI_NOOP);
2303 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2304 intel_ring_emit(pipelined, fence_reg);
2305 intel_ring_emit(pipelined, val);
2306 intel_ring_advance(pipelined);
2307 } else
2308 I915_WRITE(fence_reg, val);
2309
2310 return 0;
2311 }
2312
i830_write_fence_reg(struct drm_i915_gem_object * obj,struct intel_ring_buffer * pipelined)2313 static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2314 struct intel_ring_buffer *pipelined)
2315 {
2316 struct drm_device *dev = obj->base.dev;
2317 drm_i915_private_t *dev_priv = dev->dev_private;
2318 u32 size = obj->gtt_space->size;
2319 int regnum = obj->fence_reg;
2320 uint32_t val;
2321 uint32_t pitch_val;
2322
2323 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2324 (size & -size) != size ||
2325 (obj->gtt_offset & (size - 1)),
2326 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2327 obj->gtt_offset, size))
2328 return -EINVAL;
2329
2330 pitch_val = obj->stride / 128;
2331 pitch_val = ffs(pitch_val) - 1;
2332
2333 val = obj->gtt_offset;
2334 if (obj->tiling_mode == I915_TILING_Y)
2335 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2336 val |= I830_FENCE_SIZE_BITS(size);
2337 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2338 val |= I830_FENCE_REG_VALID;
2339
2340 if (pipelined) {
2341 int ret = intel_ring_begin(pipelined, 4);
2342 if (ret)
2343 return ret;
2344
2345 intel_ring_emit(pipelined, MI_NOOP);
2346 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2347 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2348 intel_ring_emit(pipelined, val);
2349 intel_ring_advance(pipelined);
2350 } else
2351 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2352
2353 return 0;
2354 }
2355
ring_passed_seqno(struct intel_ring_buffer * ring,u32 seqno)2356 static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2357 {
2358 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2359 }
2360
2361 static int
i915_gem_object_flush_fence(struct drm_i915_gem_object * obj,struct intel_ring_buffer * pipelined)2362 i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
2363 struct intel_ring_buffer *pipelined)
2364 {
2365 int ret;
2366
2367 if (obj->fenced_gpu_access) {
2368 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
2369 ret = i915_gem_flush_ring(obj->last_fenced_ring,
2370 0, obj->base.write_domain);
2371 if (ret)
2372 return ret;
2373 }
2374
2375 obj->fenced_gpu_access = false;
2376 }
2377
2378 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2379 if (!ring_passed_seqno(obj->last_fenced_ring,
2380 obj->last_fenced_seqno)) {
2381 ret = i915_wait_request(obj->last_fenced_ring,
2382 obj->last_fenced_seqno,
2383 true);
2384 if (ret)
2385 return ret;
2386 }
2387
2388 obj->last_fenced_seqno = 0;
2389 obj->last_fenced_ring = NULL;
2390 }
2391
2392 /* Ensure that all CPU reads are completed before installing a fence
2393 * and all writes before removing the fence.
2394 */
2395 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2396 mb();
2397
2398 return 0;
2399 }
2400
2401 int
i915_gem_object_put_fence(struct drm_i915_gem_object * obj)2402 i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2403 {
2404 int ret;
2405
2406 if (obj->tiling_mode)
2407 i915_gem_release_mmap(obj);
2408
2409 ret = i915_gem_object_flush_fence(obj, NULL);
2410 if (ret)
2411 return ret;
2412
2413 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2414 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2415
2416 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count);
2417 i915_gem_clear_fence_reg(obj->base.dev,
2418 &dev_priv->fence_regs[obj->fence_reg]);
2419
2420 obj->fence_reg = I915_FENCE_REG_NONE;
2421 }
2422
2423 return 0;
2424 }
2425
2426 static struct drm_i915_fence_reg *
i915_find_fence_reg(struct drm_device * dev,struct intel_ring_buffer * pipelined)2427 i915_find_fence_reg(struct drm_device *dev,
2428 struct intel_ring_buffer *pipelined)
2429 {
2430 struct drm_i915_private *dev_priv = dev->dev_private;
2431 struct drm_i915_fence_reg *reg, *first, *avail;
2432 int i;
2433
2434 /* First try to find a free reg */
2435 avail = NULL;
2436 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2437 reg = &dev_priv->fence_regs[i];
2438 if (!reg->obj)
2439 return reg;
2440
2441 if (!reg->pin_count)
2442 avail = reg;
2443 }
2444
2445 if (avail == NULL)
2446 return NULL;
2447
2448 /* None available, try to steal one or wait for a user to finish */
2449 avail = first = NULL;
2450 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2451 if (reg->pin_count)
2452 continue;
2453
2454 if (first == NULL)
2455 first = reg;
2456
2457 if (!pipelined ||
2458 !reg->obj->last_fenced_ring ||
2459 reg->obj->last_fenced_ring == pipelined) {
2460 avail = reg;
2461 break;
2462 }
2463 }
2464
2465 if (avail == NULL)
2466 avail = first;
2467
2468 return avail;
2469 }
2470
i915_gem_write_fence__ipi(void * data)2471 static void i915_gem_write_fence__ipi(void *data)
2472 {
2473 wbinvd();
2474 }
2475
2476 /**
2477 * i915_gem_object_get_fence - set up a fence reg for an object
2478 * @obj: object to map through a fence reg
2479 * @pipelined: ring on which to queue the change, or NULL for CPU access
2480 * @interruptible: must we wait uninterruptibly for the register to retire?
2481 *
2482 * When mapping objects through the GTT, userspace wants to be able to write
2483 * to them without having to worry about swizzling if the object is tiled.
2484 *
2485 * This function walks the fence regs looking for a free one for @obj,
2486 * stealing one if it can't find any.
2487 *
2488 * It then sets up the reg based on the object's properties: address, pitch
2489 * and tiling format.
2490 */
2491 int
i915_gem_object_get_fence(struct drm_i915_gem_object * obj,struct intel_ring_buffer * pipelined)2492 i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
2493 struct intel_ring_buffer *pipelined)
2494 {
2495 struct drm_device *dev = obj->base.dev;
2496 struct drm_i915_private *dev_priv = dev->dev_private;
2497 struct drm_i915_fence_reg *reg;
2498 int ret;
2499
2500 /* XXX disable pipelining. There are bugs. Shocking. */
2501 pipelined = NULL;
2502
2503 /* Just update our place in the LRU if our fence is getting reused. */
2504 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2505 reg = &dev_priv->fence_regs[obj->fence_reg];
2506 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
2507
2508 if (obj->tiling_changed) {
2509 ret = i915_gem_object_flush_fence(obj, pipelined);
2510 if (ret)
2511 return ret;
2512
2513 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2514 pipelined = NULL;
2515
2516 if (pipelined) {
2517 reg->setup_seqno =
2518 i915_gem_next_request_seqno(pipelined);
2519 obj->last_fenced_seqno = reg->setup_seqno;
2520 obj->last_fenced_ring = pipelined;
2521 }
2522
2523 goto update;
2524 }
2525
2526 if (!pipelined) {
2527 if (reg->setup_seqno) {
2528 if (!ring_passed_seqno(obj->last_fenced_ring,
2529 reg->setup_seqno)) {
2530 ret = i915_wait_request(obj->last_fenced_ring,
2531 reg->setup_seqno,
2532 true);
2533 if (ret)
2534 return ret;
2535 }
2536
2537 reg->setup_seqno = 0;
2538 }
2539 } else if (obj->last_fenced_ring &&
2540 obj->last_fenced_ring != pipelined) {
2541 ret = i915_gem_object_flush_fence(obj, pipelined);
2542 if (ret)
2543 return ret;
2544 }
2545
2546 return 0;
2547 }
2548
2549 reg = i915_find_fence_reg(dev, pipelined);
2550 if (reg == NULL)
2551 return -EDEADLK;
2552
2553 ret = i915_gem_object_flush_fence(obj, pipelined);
2554 if (ret)
2555 return ret;
2556
2557 if (reg->obj) {
2558 struct drm_i915_gem_object *old = reg->obj;
2559
2560 drm_gem_object_reference(&old->base);
2561
2562 if (old->tiling_mode)
2563 i915_gem_release_mmap(old);
2564
2565 ret = i915_gem_object_flush_fence(old, pipelined);
2566 if (ret) {
2567 drm_gem_object_unreference(&old->base);
2568 return ret;
2569 }
2570
2571 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2572 pipelined = NULL;
2573
2574 old->fence_reg = I915_FENCE_REG_NONE;
2575 old->last_fenced_ring = pipelined;
2576 old->last_fenced_seqno =
2577 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
2578
2579 drm_gem_object_unreference(&old->base);
2580 } else if (obj->last_fenced_seqno == 0)
2581 pipelined = NULL;
2582
2583 reg->obj = obj;
2584 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
2585 obj->fence_reg = reg - dev_priv->fence_regs;
2586 obj->last_fenced_ring = pipelined;
2587
2588 reg->setup_seqno =
2589 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
2590 obj->last_fenced_seqno = reg->setup_seqno;
2591
2592 update:
2593 obj->tiling_changed = false;
2594 switch (INTEL_INFO(dev)->gen) {
2595 case 7:
2596 case 6:
2597 /* In order to fully serialize access to the fenced region and
2598 * the update to the fence register we need to take extreme
2599 * measures on SNB+. In theory, the write to the fence register
2600 * flushes all memory transactions before, and coupled with the
2601 * mb() placed around the register write we serialise all memory
2602 * operations with respect to the changes in the tiler. Yet, on
2603 * SNB+ we need to take a step further and emit an explicit wbinvd()
2604 * on each processor in order to manually flush all memory
2605 * transactions before updating the fence register.
2606 */
2607 on_each_cpu(i915_gem_write_fence__ipi, NULL, 1);
2608 ret = sandybridge_write_fence_reg(obj, pipelined);
2609 break;
2610 case 5:
2611 case 4:
2612 ret = i965_write_fence_reg(obj, pipelined);
2613 break;
2614 case 3:
2615 ret = i915_write_fence_reg(obj, pipelined);
2616 break;
2617 case 2:
2618 ret = i830_write_fence_reg(obj, pipelined);
2619 break;
2620 }
2621
2622 return ret;
2623 }
2624
2625 /**
2626 * i915_gem_clear_fence_reg - clear out fence register info
2627 * @obj: object to clear
2628 *
2629 * Zeroes out the fence register itself and clears out the associated
2630 * data structures in dev_priv and obj.
2631 */
2632 static void
i915_gem_clear_fence_reg(struct drm_device * dev,struct drm_i915_fence_reg * reg)2633 i915_gem_clear_fence_reg(struct drm_device *dev,
2634 struct drm_i915_fence_reg *reg)
2635 {
2636 drm_i915_private_t *dev_priv = dev->dev_private;
2637 uint32_t fence_reg = reg - dev_priv->fence_regs;
2638
2639 switch (INTEL_INFO(dev)->gen) {
2640 case 7:
2641 case 6:
2642 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
2643 break;
2644 case 5:
2645 case 4:
2646 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
2647 break;
2648 case 3:
2649 if (fence_reg >= 8)
2650 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2651 else
2652 case 2:
2653 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2654
2655 I915_WRITE(fence_reg, 0);
2656 break;
2657 }
2658
2659 list_del_init(®->lru_list);
2660 reg->obj = NULL;
2661 reg->setup_seqno = 0;
2662 reg->pin_count = 0;
2663 }
2664
2665 /**
2666 * Finds free space in the GTT aperture and binds the object there.
2667 */
2668 static int
i915_gem_object_bind_to_gtt(struct drm_i915_gem_object * obj,unsigned alignment,bool map_and_fenceable)2669 i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
2670 unsigned alignment,
2671 bool map_and_fenceable)
2672 {
2673 struct drm_device *dev = obj->base.dev;
2674 drm_i915_private_t *dev_priv = dev->dev_private;
2675 struct drm_mm_node *free_space;
2676 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2677 u32 size, fence_size, fence_alignment, unfenced_alignment;
2678 bool mappable, fenceable;
2679 int ret;
2680
2681 if (obj->madv != I915_MADV_WILLNEED) {
2682 DRM_ERROR("Attempting to bind a purgeable object\n");
2683 return -EINVAL;
2684 }
2685
2686 fence_size = i915_gem_get_gtt_size(dev,
2687 obj->base.size,
2688 obj->tiling_mode);
2689 fence_alignment = i915_gem_get_gtt_alignment(dev,
2690 obj->base.size,
2691 obj->tiling_mode);
2692 unfenced_alignment =
2693 i915_gem_get_unfenced_gtt_alignment(dev,
2694 obj->base.size,
2695 obj->tiling_mode);
2696
2697 if (alignment == 0)
2698 alignment = map_and_fenceable ? fence_alignment :
2699 unfenced_alignment;
2700 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
2701 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2702 return -EINVAL;
2703 }
2704
2705 size = map_and_fenceable ? fence_size : obj->base.size;
2706
2707 /* If the object is bigger than the entire aperture, reject it early
2708 * before evicting everything in a vain attempt to find space.
2709 */
2710 if (obj->base.size >
2711 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
2712 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2713 return -E2BIG;
2714 }
2715
2716 search_free:
2717 if (map_and_fenceable)
2718 free_space =
2719 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
2720 size, alignment, 0,
2721 dev_priv->mm.gtt_mappable_end,
2722 0);
2723 else
2724 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2725 size, alignment, 0);
2726
2727 if (free_space != NULL) {
2728 if (map_and_fenceable)
2729 obj->gtt_space =
2730 drm_mm_get_block_range_generic(free_space,
2731 size, alignment, 0,
2732 dev_priv->mm.gtt_mappable_end,
2733 0);
2734 else
2735 obj->gtt_space =
2736 drm_mm_get_block(free_space, size, alignment);
2737 }
2738 if (obj->gtt_space == NULL) {
2739 /* If the gtt is empty and we're still having trouble
2740 * fitting our object in, we're out of memory.
2741 */
2742 ret = i915_gem_evict_something(dev, size, alignment,
2743 map_and_fenceable);
2744 if (ret)
2745 return ret;
2746
2747 goto search_free;
2748 }
2749
2750 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
2751 if (ret) {
2752 drm_mm_put_block(obj->gtt_space);
2753 obj->gtt_space = NULL;
2754
2755 if (ret == -ENOMEM) {
2756 /* first try to reclaim some memory by clearing the GTT */
2757 ret = i915_gem_evict_everything(dev, false);
2758 if (ret) {
2759 /* now try to shrink everyone else */
2760 if (gfpmask) {
2761 gfpmask = 0;
2762 goto search_free;
2763 }
2764
2765 return -ENOMEM;
2766 }
2767
2768 goto search_free;
2769 }
2770
2771 return ret;
2772 }
2773
2774 ret = i915_gem_gtt_bind_object(obj);
2775 if (ret) {
2776 i915_gem_object_put_pages_gtt(obj);
2777 drm_mm_put_block(obj->gtt_space);
2778 obj->gtt_space = NULL;
2779
2780 if (i915_gem_evict_everything(dev, false))
2781 return ret;
2782
2783 goto search_free;
2784 }
2785
2786 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
2787 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2788
2789 /* Assert that the object is not currently in any GPU domain. As it
2790 * wasn't in the GTT, there shouldn't be any way it could have been in
2791 * a GPU cache
2792 */
2793 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2794 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2795
2796 obj->gtt_offset = obj->gtt_space->start;
2797
2798 fenceable =
2799 obj->gtt_space->size == fence_size &&
2800 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
2801
2802 mappable =
2803 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
2804
2805 obj->map_and_fenceable = mappable && fenceable;
2806
2807 trace_i915_gem_object_bind(obj, map_and_fenceable);
2808 return 0;
2809 }
2810
2811 void
i915_gem_clflush_object(struct drm_i915_gem_object * obj)2812 i915_gem_clflush_object(struct drm_i915_gem_object *obj)
2813 {
2814 /* If we don't have a page list set up, then we're not pinned
2815 * to GPU, and we can ignore the cache flush because it'll happen
2816 * again at bind time.
2817 */
2818 if (obj->pages == NULL)
2819 return;
2820
2821 /* If the GPU is snooping the contents of the CPU cache,
2822 * we do not need to manually clear the CPU cache lines. However,
2823 * the caches are only snooped when the render cache is
2824 * flushed/invalidated. As we always have to emit invalidations
2825 * and flushes when moving into and out of the RENDER domain, correct
2826 * snooping behaviour occurs naturally as the result of our domain
2827 * tracking.
2828 */
2829 if (obj->cache_level != I915_CACHE_NONE)
2830 return;
2831
2832 trace_i915_gem_object_clflush(obj);
2833
2834 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
2835 }
2836
2837 /** Flushes any GPU write domain for the object if it's dirty. */
2838 static int
i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object * obj)2839 i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
2840 {
2841 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
2842 return 0;
2843
2844 /* Queue the GPU write cache flushing we need. */
2845 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
2846 }
2847
2848 /** Flushes the GTT write domain for the object if it's dirty. */
2849 static void
i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object * obj)2850 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
2851 {
2852 uint32_t old_write_domain;
2853
2854 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
2855 return;
2856
2857 /* No actual flushing is required for the GTT write domain. Writes
2858 * to it immediately go to main memory as far as we know, so there's
2859 * no chipset flush. It also doesn't land in render cache.
2860 *
2861 * However, we do have to enforce the order so that all writes through
2862 * the GTT land before any writes to the device, such as updates to
2863 * the GATT itself.
2864 */
2865 wmb();
2866
2867 old_write_domain = obj->base.write_domain;
2868 obj->base.write_domain = 0;
2869
2870 trace_i915_gem_object_change_domain(obj,
2871 obj->base.read_domains,
2872 old_write_domain);
2873 }
2874
2875 /** Flushes the CPU write domain for the object if it's dirty. */
2876 static void
i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object * obj)2877 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
2878 {
2879 uint32_t old_write_domain;
2880
2881 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
2882 return;
2883
2884 i915_gem_clflush_object(obj);
2885 intel_gtt_chipset_flush();
2886 old_write_domain = obj->base.write_domain;
2887 obj->base.write_domain = 0;
2888
2889 trace_i915_gem_object_change_domain(obj,
2890 obj->base.read_domains,
2891 old_write_domain);
2892 }
2893
2894 /**
2895 * Moves a single object to the GTT read, and possibly write domain.
2896 *
2897 * This function returns when the move is complete, including waiting on
2898 * flushes to occur.
2899 */
2900 int
i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object * obj,bool write)2901 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
2902 {
2903 uint32_t old_write_domain, old_read_domains;
2904 int ret;
2905
2906 /* Not valid to be called on unbound objects. */
2907 if (obj->gtt_space == NULL)
2908 return -EINVAL;
2909
2910 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2911 return 0;
2912
2913 ret = i915_gem_object_flush_gpu_write_domain(obj);
2914 if (ret)
2915 return ret;
2916
2917 if (obj->pending_gpu_write || write) {
2918 ret = i915_gem_object_wait_rendering(obj);
2919 if (ret)
2920 return ret;
2921 }
2922
2923 i915_gem_object_flush_cpu_write_domain(obj);
2924
2925 old_write_domain = obj->base.write_domain;
2926 old_read_domains = obj->base.read_domains;
2927
2928 /* It should now be out of any other write domains, and we can update
2929 * the domain values for our changes.
2930 */
2931 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2932 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
2933 if (write) {
2934 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2935 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2936 obj->dirty = 1;
2937 }
2938
2939 trace_i915_gem_object_change_domain(obj,
2940 old_read_domains,
2941 old_write_domain);
2942
2943 return 0;
2944 }
2945
i915_gem_object_set_cache_level(struct drm_i915_gem_object * obj,enum i915_cache_level cache_level)2946 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2947 enum i915_cache_level cache_level)
2948 {
2949 struct drm_device *dev = obj->base.dev;
2950 drm_i915_private_t *dev_priv = dev->dev_private;
2951 int ret;
2952
2953 if (obj->cache_level == cache_level)
2954 return 0;
2955
2956 if (obj->pin_count) {
2957 DRM_DEBUG("can not change the cache level of pinned objects\n");
2958 return -EBUSY;
2959 }
2960
2961 if (obj->gtt_space) {
2962 ret = i915_gem_object_finish_gpu(obj);
2963 if (ret)
2964 return ret;
2965
2966 i915_gem_object_finish_gtt(obj);
2967
2968 /* Before SandyBridge, you could not use tiling or fence
2969 * registers with snooped memory, so relinquish any fences
2970 * currently pointing to our region in the aperture.
2971 */
2972 if (INTEL_INFO(obj->base.dev)->gen < 6) {
2973 ret = i915_gem_object_put_fence(obj);
2974 if (ret)
2975 return ret;
2976 }
2977
2978 i915_gem_gtt_rebind_object(obj, cache_level);
2979 if (obj->has_aliasing_ppgtt_mapping)
2980 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
2981 obj, cache_level);
2982 }
2983
2984 if (cache_level == I915_CACHE_NONE) {
2985 u32 old_read_domains, old_write_domain;
2986
2987 /* If we're coming from LLC cached, then we haven't
2988 * actually been tracking whether the data is in the
2989 * CPU cache or not, since we only allow one bit set
2990 * in obj->write_domain and have been skipping the clflushes.
2991 * Just set it to the CPU cache for now.
2992 */
2993 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
2994 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
2995
2996 old_read_domains = obj->base.read_domains;
2997 old_write_domain = obj->base.write_domain;
2998
2999 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3000 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3001
3002 trace_i915_gem_object_change_domain(obj,
3003 old_read_domains,
3004 old_write_domain);
3005 }
3006
3007 obj->cache_level = cache_level;
3008 return 0;
3009 }
3010
3011 /*
3012 * Prepare buffer for display plane (scanout, cursors, etc).
3013 * Can be called from an uninterruptible phase (modesetting) and allows
3014 * any flushes to be pipelined (for pageflips).
3015 *
3016 * For the display plane, we want to be in the GTT but out of any write
3017 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
3018 * ability to pipeline the waits, pinning and any additional subtleties
3019 * that may differentiate the display plane from ordinary buffers.
3020 */
3021 int
i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object * obj,u32 alignment,struct intel_ring_buffer * pipelined)3022 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3023 u32 alignment,
3024 struct intel_ring_buffer *pipelined)
3025 {
3026 u32 old_read_domains, old_write_domain;
3027 int ret;
3028
3029 ret = i915_gem_object_flush_gpu_write_domain(obj);
3030 if (ret)
3031 return ret;
3032
3033 if (pipelined != obj->ring) {
3034 ret = i915_gem_object_wait_rendering(obj);
3035 if (ret == -ERESTARTSYS)
3036 return ret;
3037 }
3038
3039 /* The display engine is not coherent with the LLC cache on gen6. As
3040 * a result, we make sure that the pinning that is about to occur is
3041 * done with uncached PTEs. This is lowest common denominator for all
3042 * chipsets.
3043 *
3044 * However for gen6+, we could do better by using the GFDT bit instead
3045 * of uncaching, which would allow us to flush all the LLC-cached data
3046 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3047 */
3048 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3049 if (ret)
3050 return ret;
3051
3052 /* As the user may map the buffer once pinned in the display plane
3053 * (e.g. libkms for the bootup splash), we have to ensure that we
3054 * always use map_and_fenceable for all scanout buffers.
3055 */
3056 ret = i915_gem_object_pin(obj, alignment, true);
3057 if (ret)
3058 return ret;
3059
3060 i915_gem_object_flush_cpu_write_domain(obj);
3061
3062 old_write_domain = obj->base.write_domain;
3063 old_read_domains = obj->base.read_domains;
3064
3065 /* It should now be out of any other write domains, and we can update
3066 * the domain values for our changes.
3067 */
3068 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3069 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3070
3071 trace_i915_gem_object_change_domain(obj,
3072 old_read_domains,
3073 old_write_domain);
3074
3075 return 0;
3076 }
3077
3078 int
i915_gem_object_finish_gpu(struct drm_i915_gem_object * obj)3079 i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
3080 {
3081 int ret;
3082
3083 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
3084 return 0;
3085
3086 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
3087 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
3088 if (ret)
3089 return ret;
3090 }
3091
3092 ret = i915_gem_object_wait_rendering(obj);
3093 if (ret)
3094 return ret;
3095
3096 /* Ensure that we invalidate the GPU's caches and TLBs. */
3097 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3098 return 0;
3099 }
3100
3101 /**
3102 * Moves a single object to the CPU read, and possibly write domain.
3103 *
3104 * This function returns when the move is complete, including waiting on
3105 * flushes to occur.
3106 */
3107 static int
i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object * obj,bool write)3108 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
3109 {
3110 uint32_t old_write_domain, old_read_domains;
3111 int ret;
3112
3113 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3114 return 0;
3115
3116 ret = i915_gem_object_flush_gpu_write_domain(obj);
3117 if (ret)
3118 return ret;
3119
3120 ret = i915_gem_object_wait_rendering(obj);
3121 if (ret)
3122 return ret;
3123
3124 i915_gem_object_flush_gtt_write_domain(obj);
3125
3126 /* If we have a partially-valid cache of the object in the CPU,
3127 * finish invalidating it and free the per-page flags.
3128 */
3129 i915_gem_object_set_to_full_cpu_read_domain(obj);
3130
3131 old_write_domain = obj->base.write_domain;
3132 old_read_domains = obj->base.read_domains;
3133
3134 /* Flush the CPU cache if it's still invalid. */
3135 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3136 i915_gem_clflush_object(obj);
3137
3138 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3139 }
3140
3141 /* It should now be out of any other write domains, and we can update
3142 * the domain values for our changes.
3143 */
3144 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3145
3146 /* If we're writing through the CPU, then the GPU read domains will
3147 * need to be invalidated at next use.
3148 */
3149 if (write) {
3150 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3151 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3152 }
3153
3154 trace_i915_gem_object_change_domain(obj,
3155 old_read_domains,
3156 old_write_domain);
3157
3158 return 0;
3159 }
3160
3161 /**
3162 * Moves the object from a partially CPU read to a full one.
3163 *
3164 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3165 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3166 */
3167 static void
i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object * obj)3168 i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
3169 {
3170 if (!obj->page_cpu_valid)
3171 return;
3172
3173 /* If we're partially in the CPU read domain, finish moving it in.
3174 */
3175 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
3176 int i;
3177
3178 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3179 if (obj->page_cpu_valid[i])
3180 continue;
3181 drm_clflush_pages(obj->pages + i, 1);
3182 }
3183 }
3184
3185 /* Free the page_cpu_valid mappings which are now stale, whether
3186 * or not we've got I915_GEM_DOMAIN_CPU.
3187 */
3188 kfree(obj->page_cpu_valid);
3189 obj->page_cpu_valid = NULL;
3190 }
3191
3192 /**
3193 * Set the CPU read domain on a range of the object.
3194 *
3195 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3196 * not entirely valid. The page_cpu_valid member of the object flags which
3197 * pages have been flushed, and will be respected by
3198 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3199 * of the whole object.
3200 *
3201 * This function returns when the move is complete, including waiting on
3202 * flushes to occur.
3203 */
3204 static int
i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object * obj,uint64_t offset,uint64_t size)3205 i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
3206 uint64_t offset, uint64_t size)
3207 {
3208 uint32_t old_read_domains;
3209 int i, ret;
3210
3211 if (offset == 0 && size == obj->base.size)
3212 return i915_gem_object_set_to_cpu_domain(obj, 0);
3213
3214 ret = i915_gem_object_flush_gpu_write_domain(obj);
3215 if (ret)
3216 return ret;
3217
3218 ret = i915_gem_object_wait_rendering(obj);
3219 if (ret)
3220 return ret;
3221
3222 i915_gem_object_flush_gtt_write_domain(obj);
3223
3224 /* If we're already fully in the CPU read domain, we're done. */
3225 if (obj->page_cpu_valid == NULL &&
3226 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
3227 return 0;
3228
3229 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3230 * newly adding I915_GEM_DOMAIN_CPU
3231 */
3232 if (obj->page_cpu_valid == NULL) {
3233 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3234 GFP_KERNEL);
3235 if (obj->page_cpu_valid == NULL)
3236 return -ENOMEM;
3237 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3238 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
3239
3240 /* Flush the cache on any pages that are still invalid from the CPU's
3241 * perspective.
3242 */
3243 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3244 i++) {
3245 if (obj->page_cpu_valid[i])
3246 continue;
3247
3248 drm_clflush_pages(obj->pages + i, 1);
3249
3250 obj->page_cpu_valid[i] = 1;
3251 }
3252
3253 /* It should now be out of any other write domains, and we can update
3254 * the domain values for our changes.
3255 */
3256 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3257
3258 old_read_domains = obj->base.read_domains;
3259 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3260
3261 trace_i915_gem_object_change_domain(obj,
3262 old_read_domains,
3263 obj->base.write_domain);
3264
3265 return 0;
3266 }
3267
3268 /* Throttle our rendering by waiting until the ring has completed our requests
3269 * emitted over 20 msec ago.
3270 *
3271 * Note that if we were to use the current jiffies each time around the loop,
3272 * we wouldn't escape the function with any frames outstanding if the time to
3273 * render a frame was over 20ms.
3274 *
3275 * This should get us reasonable parallelism between CPU and GPU but also
3276 * relatively low latency when blocking on a particular request to finish.
3277 */
3278 static int
i915_gem_ring_throttle(struct drm_device * dev,struct drm_file * file)3279 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3280 {
3281 struct drm_i915_private *dev_priv = dev->dev_private;
3282 struct drm_i915_file_private *file_priv = file->driver_priv;
3283 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3284 struct drm_i915_gem_request *request;
3285 struct intel_ring_buffer *ring = NULL;
3286 u32 seqno = 0;
3287 int ret;
3288
3289 if (atomic_read(&dev_priv->mm.wedged))
3290 return -EIO;
3291
3292 spin_lock(&file_priv->mm.lock);
3293 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3294 if (time_after_eq(request->emitted_jiffies, recent_enough))
3295 break;
3296
3297 ring = request->ring;
3298 seqno = request->seqno;
3299 }
3300 spin_unlock(&file_priv->mm.lock);
3301
3302 if (seqno == 0)
3303 return 0;
3304
3305 ret = 0;
3306 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
3307 /* And wait for the seqno passing without holding any locks and
3308 * causing extra latency for others. This is safe as the irq
3309 * generation is designed to be run atomically and so is
3310 * lockless.
3311 */
3312 if (ring->irq_get(ring)) {
3313 ret = wait_event_interruptible(ring->irq_queue,
3314 i915_seqno_passed(ring->get_seqno(ring), seqno)
3315 || atomic_read(&dev_priv->mm.wedged));
3316 ring->irq_put(ring);
3317
3318 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3319 ret = -EIO;
3320 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
3321 seqno) ||
3322 atomic_read(&dev_priv->mm.wedged), 3000)) {
3323 ret = -EBUSY;
3324 }
3325 }
3326
3327 if (ret == 0)
3328 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3329
3330 return ret;
3331 }
3332
3333 int
i915_gem_object_pin(struct drm_i915_gem_object * obj,uint32_t alignment,bool map_and_fenceable)3334 i915_gem_object_pin(struct drm_i915_gem_object *obj,
3335 uint32_t alignment,
3336 bool map_and_fenceable)
3337 {
3338 struct drm_device *dev = obj->base.dev;
3339 struct drm_i915_private *dev_priv = dev->dev_private;
3340 int ret;
3341
3342 if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3343 return -EBUSY;
3344 WARN_ON(i915_verify_lists(dev));
3345
3346 if (obj->gtt_space != NULL) {
3347 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3348 (map_and_fenceable && !obj->map_and_fenceable)) {
3349 WARN(obj->pin_count,
3350 "bo is already pinned with incorrect alignment:"
3351 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3352 " obj->map_and_fenceable=%d\n",
3353 obj->gtt_offset, alignment,
3354 map_and_fenceable,
3355 obj->map_and_fenceable);
3356 ret = i915_gem_object_unbind(obj);
3357 if (ret)
3358 return ret;
3359 }
3360 }
3361
3362 if (obj->gtt_space == NULL) {
3363 ret = i915_gem_object_bind_to_gtt(obj, alignment,
3364 map_and_fenceable);
3365 if (ret)
3366 return ret;
3367 }
3368
3369 if (obj->pin_count++ == 0) {
3370 if (!obj->active)
3371 list_move_tail(&obj->mm_list,
3372 &dev_priv->mm.pinned_list);
3373 }
3374 obj->pin_mappable |= map_and_fenceable;
3375
3376 WARN_ON(i915_verify_lists(dev));
3377 return 0;
3378 }
3379
3380 void
i915_gem_object_unpin(struct drm_i915_gem_object * obj)3381 i915_gem_object_unpin(struct drm_i915_gem_object *obj)
3382 {
3383 struct drm_device *dev = obj->base.dev;
3384 drm_i915_private_t *dev_priv = dev->dev_private;
3385
3386 WARN_ON(i915_verify_lists(dev));
3387 BUG_ON(obj->pin_count == 0);
3388 BUG_ON(obj->gtt_space == NULL);
3389
3390 if (--obj->pin_count == 0) {
3391 if (!obj->active)
3392 list_move_tail(&obj->mm_list,
3393 &dev_priv->mm.inactive_list);
3394 obj->pin_mappable = false;
3395 }
3396 WARN_ON(i915_verify_lists(dev));
3397 }
3398
3399 int
i915_gem_pin_ioctl(struct drm_device * dev,void * data,struct drm_file * file)3400 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3401 struct drm_file *file)
3402 {
3403 struct drm_i915_gem_pin *args = data;
3404 struct drm_i915_gem_object *obj;
3405 int ret;
3406
3407 ret = i915_mutex_lock_interruptible(dev);
3408 if (ret)
3409 return ret;
3410
3411 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3412 if (&obj->base == NULL) {
3413 ret = -ENOENT;
3414 goto unlock;
3415 }
3416
3417 if (obj->madv != I915_MADV_WILLNEED) {
3418 DRM_ERROR("Attempting to pin a purgeable buffer\n");
3419 ret = -EINVAL;
3420 goto out;
3421 }
3422
3423 if (obj->pin_filp != NULL && obj->pin_filp != file) {
3424 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3425 args->handle);
3426 ret = -EINVAL;
3427 goto out;
3428 }
3429
3430 if (obj->user_pin_count == 0) {
3431 ret = i915_gem_object_pin(obj, args->alignment, true);
3432 if (ret)
3433 goto out;
3434 }
3435
3436 obj->user_pin_count++;
3437 obj->pin_filp = file;
3438
3439 /* XXX - flush the CPU caches for pinned objects
3440 * as the X server doesn't manage domains yet
3441 */
3442 i915_gem_object_flush_cpu_write_domain(obj);
3443 args->offset = obj->gtt_offset;
3444 out:
3445 drm_gem_object_unreference(&obj->base);
3446 unlock:
3447 mutex_unlock(&dev->struct_mutex);
3448 return ret;
3449 }
3450
3451 int
i915_gem_unpin_ioctl(struct drm_device * dev,void * data,struct drm_file * file)3452 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3453 struct drm_file *file)
3454 {
3455 struct drm_i915_gem_pin *args = data;
3456 struct drm_i915_gem_object *obj;
3457 int ret;
3458
3459 ret = i915_mutex_lock_interruptible(dev);
3460 if (ret)
3461 return ret;
3462
3463 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3464 if (&obj->base == NULL) {
3465 ret = -ENOENT;
3466 goto unlock;
3467 }
3468
3469 if (obj->pin_filp != file) {
3470 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3471 args->handle);
3472 ret = -EINVAL;
3473 goto out;
3474 }
3475 obj->user_pin_count--;
3476 if (obj->user_pin_count == 0) {
3477 obj->pin_filp = NULL;
3478 i915_gem_object_unpin(obj);
3479 }
3480
3481 out:
3482 drm_gem_object_unreference(&obj->base);
3483 unlock:
3484 mutex_unlock(&dev->struct_mutex);
3485 return ret;
3486 }
3487
3488 int
i915_gem_busy_ioctl(struct drm_device * dev,void * data,struct drm_file * file)3489 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3490 struct drm_file *file)
3491 {
3492 struct drm_i915_gem_busy *args = data;
3493 struct drm_i915_gem_object *obj;
3494 int ret;
3495
3496 ret = i915_mutex_lock_interruptible(dev);
3497 if (ret)
3498 return ret;
3499
3500 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3501 if (&obj->base == NULL) {
3502 ret = -ENOENT;
3503 goto unlock;
3504 }
3505
3506 /* Count all active objects as busy, even if they are currently not used
3507 * by the gpu. Users of this interface expect objects to eventually
3508 * become non-busy without any further actions, therefore emit any
3509 * necessary flushes here.
3510 */
3511 args->busy = obj->active;
3512 if (args->busy) {
3513 /* Unconditionally flush objects, even when the gpu still uses this
3514 * object. Userspace calling this function indicates that it wants to
3515 * use this buffer rather sooner than later, so issuing the required
3516 * flush earlier is beneficial.
3517 */
3518 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
3519 ret = i915_gem_flush_ring(obj->ring,
3520 0, obj->base.write_domain);
3521 } else if (obj->ring->outstanding_lazy_request ==
3522 obj->last_rendering_seqno) {
3523 struct drm_i915_gem_request *request;
3524
3525 /* This ring is not being cleared by active usage,
3526 * so emit a request to do so.
3527 */
3528 request = kzalloc(sizeof(*request), GFP_KERNEL);
3529 if (request) {
3530 ret = i915_add_request(obj->ring, NULL, request);
3531 if (ret)
3532 kfree(request);
3533 } else
3534 ret = -ENOMEM;
3535 }
3536
3537 /* Update the active list for the hardware's current position.
3538 * Otherwise this only updates on a delayed timer or when irqs
3539 * are actually unmasked, and our working set ends up being
3540 * larger than required.
3541 */
3542 i915_gem_retire_requests_ring(obj->ring);
3543
3544 args->busy = obj->active;
3545 }
3546
3547 drm_gem_object_unreference(&obj->base);
3548 unlock:
3549 mutex_unlock(&dev->struct_mutex);
3550 return ret;
3551 }
3552
3553 int
i915_gem_throttle_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)3554 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3555 struct drm_file *file_priv)
3556 {
3557 return i915_gem_ring_throttle(dev, file_priv);
3558 }
3559
3560 int
i915_gem_madvise_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)3561 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3562 struct drm_file *file_priv)
3563 {
3564 struct drm_i915_gem_madvise *args = data;
3565 struct drm_i915_gem_object *obj;
3566 int ret;
3567
3568 switch (args->madv) {
3569 case I915_MADV_DONTNEED:
3570 case I915_MADV_WILLNEED:
3571 break;
3572 default:
3573 return -EINVAL;
3574 }
3575
3576 ret = i915_mutex_lock_interruptible(dev);
3577 if (ret)
3578 return ret;
3579
3580 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
3581 if (&obj->base == NULL) {
3582 ret = -ENOENT;
3583 goto unlock;
3584 }
3585
3586 if (obj->pin_count) {
3587 ret = -EINVAL;
3588 goto out;
3589 }
3590
3591 if (obj->madv != __I915_MADV_PURGED)
3592 obj->madv = args->madv;
3593
3594 /* if the object is no longer bound, discard its backing storage */
3595 if (i915_gem_object_is_purgeable(obj) &&
3596 obj->gtt_space == NULL)
3597 i915_gem_object_truncate(obj);
3598
3599 args->retained = obj->madv != __I915_MADV_PURGED;
3600
3601 out:
3602 drm_gem_object_unreference(&obj->base);
3603 unlock:
3604 mutex_unlock(&dev->struct_mutex);
3605 return ret;
3606 }
3607
i915_gem_alloc_object(struct drm_device * dev,size_t size)3608 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3609 size_t size)
3610 {
3611 struct drm_i915_private *dev_priv = dev->dev_private;
3612 struct drm_i915_gem_object *obj;
3613 struct address_space *mapping;
3614
3615 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3616 if (obj == NULL)
3617 return NULL;
3618
3619 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3620 kfree(obj);
3621 return NULL;
3622 }
3623
3624 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3625 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3626
3627 i915_gem_info_add_obj(dev_priv, size);
3628
3629 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3630 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3631
3632 if (HAS_LLC(dev)) {
3633 /* On some devices, we can have the GPU use the LLC (the CPU
3634 * cache) for about a 10% performance improvement
3635 * compared to uncached. Graphics requests other than
3636 * display scanout are coherent with the CPU in
3637 * accessing this cache. This means in this mode we
3638 * don't need to clflush on the CPU side, and on the
3639 * GPU side we only need to flush internal caches to
3640 * get data visible to the CPU.
3641 *
3642 * However, we maintain the display planes as UC, and so
3643 * need to rebind when first used as such.
3644 */
3645 obj->cache_level = I915_CACHE_LLC;
3646 } else
3647 obj->cache_level = I915_CACHE_NONE;
3648
3649 obj->base.driver_private = NULL;
3650 obj->fence_reg = I915_FENCE_REG_NONE;
3651 INIT_LIST_HEAD(&obj->mm_list);
3652 INIT_LIST_HEAD(&obj->gtt_list);
3653 INIT_LIST_HEAD(&obj->ring_list);
3654 INIT_LIST_HEAD(&obj->exec_list);
3655 INIT_LIST_HEAD(&obj->gpu_write_list);
3656 obj->madv = I915_MADV_WILLNEED;
3657 /* Avoid an unnecessary call to unbind on the first bind. */
3658 obj->map_and_fenceable = true;
3659
3660 return obj;
3661 }
3662
i915_gem_init_object(struct drm_gem_object * obj)3663 int i915_gem_init_object(struct drm_gem_object *obj)
3664 {
3665 BUG();
3666
3667 return 0;
3668 }
3669
i915_gem_free_object_tail(struct drm_i915_gem_object * obj)3670 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
3671 {
3672 struct drm_device *dev = obj->base.dev;
3673 drm_i915_private_t *dev_priv = dev->dev_private;
3674 int ret;
3675
3676 ret = i915_gem_object_unbind(obj);
3677 if (ret == -ERESTARTSYS) {
3678 list_move(&obj->mm_list,
3679 &dev_priv->mm.deferred_free_list);
3680 return;
3681 }
3682
3683 trace_i915_gem_object_destroy(obj);
3684
3685 if (obj->base.map_list.map)
3686 drm_gem_free_mmap_offset(&obj->base);
3687
3688 drm_gem_object_release(&obj->base);
3689 i915_gem_info_remove_obj(dev_priv, obj->base.size);
3690
3691 kfree(obj->page_cpu_valid);
3692 kfree(obj->bit_17);
3693 kfree(obj);
3694 }
3695
i915_gem_free_object(struct drm_gem_object * gem_obj)3696 void i915_gem_free_object(struct drm_gem_object *gem_obj)
3697 {
3698 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3699 struct drm_device *dev = obj->base.dev;
3700
3701 while (obj->pin_count > 0)
3702 i915_gem_object_unpin(obj);
3703
3704 if (obj->phys_obj)
3705 i915_gem_detach_phys_object(dev, obj);
3706
3707 i915_gem_free_object_tail(obj);
3708 }
3709
3710 int
i915_gem_idle(struct drm_device * dev)3711 i915_gem_idle(struct drm_device *dev)
3712 {
3713 drm_i915_private_t *dev_priv = dev->dev_private;
3714 int ret;
3715
3716 mutex_lock(&dev->struct_mutex);
3717
3718 if (dev_priv->mm.suspended) {
3719 mutex_unlock(&dev->struct_mutex);
3720 return 0;
3721 }
3722
3723 ret = i915_gpu_idle(dev, true);
3724 if (ret) {
3725 mutex_unlock(&dev->struct_mutex);
3726 return ret;
3727 }
3728
3729 /* Under UMS, be paranoid and evict. */
3730 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
3731 ret = i915_gem_evict_inactive(dev, false);
3732 if (ret) {
3733 mutex_unlock(&dev->struct_mutex);
3734 return ret;
3735 }
3736 }
3737
3738 i915_gem_reset_fences(dev);
3739
3740 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3741 * We need to replace this with a semaphore, or something.
3742 * And not confound mm.suspended!
3743 */
3744 dev_priv->mm.suspended = 1;
3745 del_timer_sync(&dev_priv->hangcheck_timer);
3746
3747 i915_kernel_lost_context(dev);
3748 i915_gem_cleanup_ringbuffer(dev);
3749
3750 mutex_unlock(&dev->struct_mutex);
3751
3752 /* Cancel the retire work handler, which should be idle now. */
3753 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3754
3755 return 0;
3756 }
3757
i915_gem_init_swizzling(struct drm_device * dev)3758 void i915_gem_init_swizzling(struct drm_device *dev)
3759 {
3760 drm_i915_private_t *dev_priv = dev->dev_private;
3761
3762 if (INTEL_INFO(dev)->gen < 5 ||
3763 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3764 return;
3765
3766 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3767 DISP_TILE_SURFACE_SWIZZLING);
3768
3769 if (IS_GEN5(dev))
3770 return;
3771
3772 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3773 if (IS_GEN6(dev))
3774 I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_SNB));
3775 else
3776 I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_IVB));
3777 }
3778
i915_gem_init_ppgtt(struct drm_device * dev)3779 void i915_gem_init_ppgtt(struct drm_device *dev)
3780 {
3781 drm_i915_private_t *dev_priv = dev->dev_private;
3782 uint32_t pd_offset;
3783 struct intel_ring_buffer *ring;
3784 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
3785 uint32_t __iomem *pd_addr;
3786 uint32_t pd_entry;
3787 int i;
3788
3789 if (!dev_priv->mm.aliasing_ppgtt)
3790 return;
3791
3792
3793 pd_addr = dev_priv->mm.gtt->gtt + ppgtt->pd_offset/sizeof(uint32_t);
3794 for (i = 0; i < ppgtt->num_pd_entries; i++) {
3795 dma_addr_t pt_addr;
3796
3797 if (dev_priv->mm.gtt->needs_dmar)
3798 pt_addr = ppgtt->pt_dma_addr[i];
3799 else
3800 pt_addr = page_to_phys(ppgtt->pt_pages[i]);
3801
3802 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
3803 pd_entry |= GEN6_PDE_VALID;
3804
3805 writel(pd_entry, pd_addr + i);
3806 }
3807 readl(pd_addr);
3808
3809 pd_offset = ppgtt->pd_offset;
3810 pd_offset /= 64; /* in cachelines, */
3811 pd_offset <<= 16;
3812
3813 if (INTEL_INFO(dev)->gen == 6) {
3814 uint32_t ecochk = I915_READ(GAM_ECOCHK);
3815 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
3816 ECOCHK_PPGTT_CACHE64B);
3817 I915_WRITE(GFX_MODE, GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3818 } else if (INTEL_INFO(dev)->gen >= 7) {
3819 I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
3820 /* GFX_MODE is per-ring on gen7+ */
3821 }
3822
3823 for (i = 0; i < I915_NUM_RINGS; i++) {
3824 ring = &dev_priv->ring[i];
3825
3826 if (INTEL_INFO(dev)->gen >= 7)
3827 I915_WRITE(RING_MODE_GEN7(ring),
3828 GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3829
3830 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
3831 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
3832 }
3833 }
3834
3835 int
i915_gem_init_hw(struct drm_device * dev)3836 i915_gem_init_hw(struct drm_device *dev)
3837 {
3838 drm_i915_private_t *dev_priv = dev->dev_private;
3839 int ret;
3840
3841 i915_gem_init_swizzling(dev);
3842
3843 ret = intel_init_render_ring_buffer(dev);
3844 if (ret)
3845 return ret;
3846
3847 if (HAS_BSD(dev)) {
3848 ret = intel_init_bsd_ring_buffer(dev);
3849 if (ret)
3850 goto cleanup_render_ring;
3851 }
3852
3853 if (HAS_BLT(dev)) {
3854 ret = intel_init_blt_ring_buffer(dev);
3855 if (ret)
3856 goto cleanup_bsd_ring;
3857 }
3858
3859 dev_priv->next_seqno = 1;
3860
3861 i915_gem_init_ppgtt(dev);
3862
3863 return 0;
3864
3865 cleanup_bsd_ring:
3866 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
3867 cleanup_render_ring:
3868 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
3869 return ret;
3870 }
3871
3872 void
i915_gem_cleanup_ringbuffer(struct drm_device * dev)3873 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3874 {
3875 drm_i915_private_t *dev_priv = dev->dev_private;
3876 int i;
3877
3878 for (i = 0; i < I915_NUM_RINGS; i++)
3879 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
3880 }
3881
3882 int
i915_gem_entervt_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)3883 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3884 struct drm_file *file_priv)
3885 {
3886 drm_i915_private_t *dev_priv = dev->dev_private;
3887 int ret, i;
3888
3889 if (drm_core_check_feature(dev, DRIVER_MODESET))
3890 return 0;
3891
3892 if (atomic_read(&dev_priv->mm.wedged)) {
3893 DRM_ERROR("Reenabling wedged hardware, good luck\n");
3894 atomic_set(&dev_priv->mm.wedged, 0);
3895 }
3896
3897 mutex_lock(&dev->struct_mutex);
3898 dev_priv->mm.suspended = 0;
3899
3900 ret = i915_gem_init_hw(dev);
3901 if (ret != 0) {
3902 mutex_unlock(&dev->struct_mutex);
3903 return ret;
3904 }
3905
3906 BUG_ON(!list_empty(&dev_priv->mm.active_list));
3907 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3908 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3909 for (i = 0; i < I915_NUM_RINGS; i++) {
3910 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3911 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3912 }
3913 mutex_unlock(&dev->struct_mutex);
3914
3915 ret = drm_irq_install(dev);
3916 if (ret)
3917 goto cleanup_ringbuffer;
3918
3919 return 0;
3920
3921 cleanup_ringbuffer:
3922 mutex_lock(&dev->struct_mutex);
3923 i915_gem_cleanup_ringbuffer(dev);
3924 dev_priv->mm.suspended = 1;
3925 mutex_unlock(&dev->struct_mutex);
3926
3927 return ret;
3928 }
3929
3930 int
i915_gem_leavevt_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)3931 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3932 struct drm_file *file_priv)
3933 {
3934 if (drm_core_check_feature(dev, DRIVER_MODESET))
3935 return 0;
3936
3937 drm_irq_uninstall(dev);
3938 return i915_gem_idle(dev);
3939 }
3940
3941 void
i915_gem_lastclose(struct drm_device * dev)3942 i915_gem_lastclose(struct drm_device *dev)
3943 {
3944 int ret;
3945
3946 if (drm_core_check_feature(dev, DRIVER_MODESET))
3947 return;
3948
3949 ret = i915_gem_idle(dev);
3950 if (ret)
3951 DRM_ERROR("failed to idle hardware: %d\n", ret);
3952 }
3953
3954 static void
init_ring_lists(struct intel_ring_buffer * ring)3955 init_ring_lists(struct intel_ring_buffer *ring)
3956 {
3957 INIT_LIST_HEAD(&ring->active_list);
3958 INIT_LIST_HEAD(&ring->request_list);
3959 INIT_LIST_HEAD(&ring->gpu_write_list);
3960 }
3961
3962 void
i915_gem_load(struct drm_device * dev)3963 i915_gem_load(struct drm_device *dev)
3964 {
3965 int i;
3966 drm_i915_private_t *dev_priv = dev->dev_private;
3967
3968 INIT_LIST_HEAD(&dev_priv->mm.active_list);
3969 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3970 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3971 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
3972 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
3973 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
3974 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
3975 for (i = 0; i < I915_NUM_RINGS; i++)
3976 init_ring_lists(&dev_priv->ring[i]);
3977 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
3978 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
3979 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3980 i915_gem_retire_work_handler);
3981 init_completion(&dev_priv->error_completion);
3982
3983 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3984 if (IS_GEN3(dev)) {
3985 u32 tmp = I915_READ(MI_ARB_STATE);
3986 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3987 /* arb state is a masked write, so set bit + bit in mask */
3988 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3989 I915_WRITE(MI_ARB_STATE, tmp);
3990 }
3991 }
3992
3993 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3994
3995 /* Old X drivers will take 0-2 for front, back, depth buffers */
3996 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3997 dev_priv->fence_reg_start = 3;
3998
3999 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4000 dev_priv->num_fence_regs = 16;
4001 else
4002 dev_priv->num_fence_regs = 8;
4003
4004 /* Initialize fence registers to zero */
4005 for (i = 0; i < dev_priv->num_fence_regs; i++) {
4006 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
4007 }
4008
4009 i915_gem_detect_bit_6_swizzle(dev);
4010 init_waitqueue_head(&dev_priv->pending_flip_queue);
4011
4012 dev_priv->mm.interruptible = true;
4013
4014 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4015 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4016 register_shrinker(&dev_priv->mm.inactive_shrinker);
4017 }
4018
4019 /*
4020 * Create a physically contiguous memory object for this object
4021 * e.g. for cursor + overlay regs
4022 */
i915_gem_init_phys_object(struct drm_device * dev,int id,int size,int align)4023 static int i915_gem_init_phys_object(struct drm_device *dev,
4024 int id, int size, int align)
4025 {
4026 drm_i915_private_t *dev_priv = dev->dev_private;
4027 struct drm_i915_gem_phys_object *phys_obj;
4028 int ret;
4029
4030 if (dev_priv->mm.phys_objs[id - 1] || !size)
4031 return 0;
4032
4033 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
4034 if (!phys_obj)
4035 return -ENOMEM;
4036
4037 phys_obj->id = id;
4038
4039 phys_obj->handle = drm_pci_alloc(dev, size, align);
4040 if (!phys_obj->handle) {
4041 ret = -ENOMEM;
4042 goto kfree_obj;
4043 }
4044 #ifdef CONFIG_X86
4045 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4046 #endif
4047
4048 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4049
4050 return 0;
4051 kfree_obj:
4052 kfree(phys_obj);
4053 return ret;
4054 }
4055
i915_gem_free_phys_object(struct drm_device * dev,int id)4056 static void i915_gem_free_phys_object(struct drm_device *dev, int id)
4057 {
4058 drm_i915_private_t *dev_priv = dev->dev_private;
4059 struct drm_i915_gem_phys_object *phys_obj;
4060
4061 if (!dev_priv->mm.phys_objs[id - 1])
4062 return;
4063
4064 phys_obj = dev_priv->mm.phys_objs[id - 1];
4065 if (phys_obj->cur_obj) {
4066 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4067 }
4068
4069 #ifdef CONFIG_X86
4070 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4071 #endif
4072 drm_pci_free(dev, phys_obj->handle);
4073 kfree(phys_obj);
4074 dev_priv->mm.phys_objs[id - 1] = NULL;
4075 }
4076
i915_gem_free_all_phys_object(struct drm_device * dev)4077 void i915_gem_free_all_phys_object(struct drm_device *dev)
4078 {
4079 int i;
4080
4081 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4082 i915_gem_free_phys_object(dev, i);
4083 }
4084
i915_gem_detach_phys_object(struct drm_device * dev,struct drm_i915_gem_object * obj)4085 void i915_gem_detach_phys_object(struct drm_device *dev,
4086 struct drm_i915_gem_object *obj)
4087 {
4088 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4089 char *vaddr;
4090 int i;
4091 int page_count;
4092
4093 if (!obj->phys_obj)
4094 return;
4095 vaddr = obj->phys_obj->handle->vaddr;
4096
4097 page_count = obj->base.size / PAGE_SIZE;
4098 for (i = 0; i < page_count; i++) {
4099 struct page *page = shmem_read_mapping_page(mapping, i);
4100 if (!IS_ERR(page)) {
4101 char *dst = kmap_atomic(page);
4102 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4103 kunmap_atomic(dst);
4104
4105 drm_clflush_pages(&page, 1);
4106
4107 set_page_dirty(page);
4108 mark_page_accessed(page);
4109 page_cache_release(page);
4110 }
4111 }
4112 intel_gtt_chipset_flush();
4113
4114 obj->phys_obj->cur_obj = NULL;
4115 obj->phys_obj = NULL;
4116 }
4117
4118 int
i915_gem_attach_phys_object(struct drm_device * dev,struct drm_i915_gem_object * obj,int id,int align)4119 i915_gem_attach_phys_object(struct drm_device *dev,
4120 struct drm_i915_gem_object *obj,
4121 int id,
4122 int align)
4123 {
4124 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4125 drm_i915_private_t *dev_priv = dev->dev_private;
4126 int ret = 0;
4127 int page_count;
4128 int i;
4129
4130 if (id > I915_MAX_PHYS_OBJECT)
4131 return -EINVAL;
4132
4133 if (obj->phys_obj) {
4134 if (obj->phys_obj->id == id)
4135 return 0;
4136 i915_gem_detach_phys_object(dev, obj);
4137 }
4138
4139 /* create a new object */
4140 if (!dev_priv->mm.phys_objs[id - 1]) {
4141 ret = i915_gem_init_phys_object(dev, id,
4142 obj->base.size, align);
4143 if (ret) {
4144 DRM_ERROR("failed to init phys object %d size: %zu\n",
4145 id, obj->base.size);
4146 return ret;
4147 }
4148 }
4149
4150 /* bind to the object */
4151 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4152 obj->phys_obj->cur_obj = obj;
4153
4154 page_count = obj->base.size / PAGE_SIZE;
4155
4156 for (i = 0; i < page_count; i++) {
4157 struct page *page;
4158 char *dst, *src;
4159
4160 page = shmem_read_mapping_page(mapping, i);
4161 if (IS_ERR(page))
4162 return PTR_ERR(page);
4163
4164 src = kmap_atomic(page);
4165 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4166 memcpy(dst, src, PAGE_SIZE);
4167 kunmap_atomic(src);
4168
4169 mark_page_accessed(page);
4170 page_cache_release(page);
4171 }
4172
4173 return 0;
4174 }
4175
4176 static int
i915_gem_phys_pwrite(struct drm_device * dev,struct drm_i915_gem_object * obj,struct drm_i915_gem_pwrite * args,struct drm_file * file_priv)4177 i915_gem_phys_pwrite(struct drm_device *dev,
4178 struct drm_i915_gem_object *obj,
4179 struct drm_i915_gem_pwrite *args,
4180 struct drm_file *file_priv)
4181 {
4182 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
4183 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
4184
4185 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4186 unsigned long unwritten;
4187
4188 /* The physical object once assigned is fixed for the lifetime
4189 * of the obj, so we can safely drop the lock and continue
4190 * to access vaddr.
4191 */
4192 mutex_unlock(&dev->struct_mutex);
4193 unwritten = copy_from_user(vaddr, user_data, args->size);
4194 mutex_lock(&dev->struct_mutex);
4195 if (unwritten)
4196 return -EFAULT;
4197 }
4198
4199 intel_gtt_chipset_flush();
4200 return 0;
4201 }
4202
i915_gem_release(struct drm_device * dev,struct drm_file * file)4203 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
4204 {
4205 struct drm_i915_file_private *file_priv = file->driver_priv;
4206
4207 /* Clean up our request list when the client is going away, so that
4208 * later retire_requests won't dereference our soon-to-be-gone
4209 * file_priv.
4210 */
4211 spin_lock(&file_priv->mm.lock);
4212 while (!list_empty(&file_priv->mm.request_list)) {
4213 struct drm_i915_gem_request *request;
4214
4215 request = list_first_entry(&file_priv->mm.request_list,
4216 struct drm_i915_gem_request,
4217 client_list);
4218 list_del(&request->client_list);
4219 request->file_priv = NULL;
4220 }
4221 spin_unlock(&file_priv->mm.lock);
4222 }
4223
4224 static int
i915_gpu_is_active(struct drm_device * dev)4225 i915_gpu_is_active(struct drm_device *dev)
4226 {
4227 drm_i915_private_t *dev_priv = dev->dev_private;
4228 int lists_empty;
4229
4230 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
4231 list_empty(&dev_priv->mm.active_list);
4232
4233 return !lists_empty;
4234 }
4235
4236 static int
i915_gem_inactive_shrink(struct shrinker * shrinker,struct shrink_control * sc)4237 i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
4238 {
4239 struct drm_i915_private *dev_priv =
4240 container_of(shrinker,
4241 struct drm_i915_private,
4242 mm.inactive_shrinker);
4243 struct drm_device *dev = dev_priv->dev;
4244 struct drm_i915_gem_object *obj, *next;
4245 int nr_to_scan = sc->nr_to_scan;
4246 int cnt;
4247
4248 if (!mutex_trylock(&dev->struct_mutex))
4249 return 0;
4250
4251 /* "fast-path" to count number of available objects */
4252 if (nr_to_scan == 0) {
4253 cnt = 0;
4254 list_for_each_entry(obj,
4255 &dev_priv->mm.inactive_list,
4256 mm_list)
4257 cnt++;
4258 mutex_unlock(&dev->struct_mutex);
4259 return cnt / 100 * sysctl_vfs_cache_pressure;
4260 }
4261
4262 rescan:
4263 /* first scan for clean buffers */
4264 i915_gem_retire_requests(dev);
4265
4266 list_for_each_entry_safe(obj, next,
4267 &dev_priv->mm.inactive_list,
4268 mm_list) {
4269 if (i915_gem_object_is_purgeable(obj)) {
4270 if (i915_gem_object_unbind(obj) == 0 &&
4271 --nr_to_scan == 0)
4272 break;
4273 }
4274 }
4275
4276 /* second pass, evict/count anything still on the inactive list */
4277 cnt = 0;
4278 list_for_each_entry_safe(obj, next,
4279 &dev_priv->mm.inactive_list,
4280 mm_list) {
4281 if (nr_to_scan &&
4282 i915_gem_object_unbind(obj) == 0)
4283 nr_to_scan--;
4284 else
4285 cnt++;
4286 }
4287
4288 if (nr_to_scan && i915_gpu_is_active(dev)) {
4289 /*
4290 * We are desperate for pages, so as a last resort, wait
4291 * for the GPU to finish and discard whatever we can.
4292 * This has a dramatic impact to reduce the number of
4293 * OOM-killer events whilst running the GPU aggressively.
4294 */
4295 if (i915_gpu_idle(dev, true) == 0)
4296 goto rescan;
4297 }
4298 mutex_unlock(&dev->struct_mutex);
4299 return cnt / 100 * sysctl_vfs_cache_pressure;
4300 }
4301