• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(&reg->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 
2471 /**
2472  * i915_gem_object_get_fence - set up a fence reg for an object
2473  * @obj: object to map through a fence reg
2474  * @pipelined: ring on which to queue the change, or NULL for CPU access
2475  * @interruptible: must we wait uninterruptibly for the register to retire?
2476  *
2477  * When mapping objects through the GTT, userspace wants to be able to write
2478  * to them without having to worry about swizzling if the object is tiled.
2479  *
2480  * This function walks the fence regs looking for a free one for @obj,
2481  * stealing one if it can't find any.
2482  *
2483  * It then sets up the reg based on the object's properties: address, pitch
2484  * and tiling format.
2485  */
2486 int
i915_gem_object_get_fence(struct drm_i915_gem_object * obj,struct intel_ring_buffer * pipelined)2487 i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
2488 			  struct intel_ring_buffer *pipelined)
2489 {
2490 	struct drm_device *dev = obj->base.dev;
2491 	struct drm_i915_private *dev_priv = dev->dev_private;
2492 	struct drm_i915_fence_reg *reg;
2493 	int ret;
2494 
2495 	/* XXX disable pipelining. There are bugs. Shocking. */
2496 	pipelined = NULL;
2497 
2498 	/* Just update our place in the LRU if our fence is getting reused. */
2499 	if (obj->fence_reg != I915_FENCE_REG_NONE) {
2500 		reg = &dev_priv->fence_regs[obj->fence_reg];
2501 		list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2502 
2503 		if (obj->tiling_changed) {
2504 			ret = i915_gem_object_flush_fence(obj, pipelined);
2505 			if (ret)
2506 				return ret;
2507 
2508 			if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2509 				pipelined = NULL;
2510 
2511 			if (pipelined) {
2512 				reg->setup_seqno =
2513 					i915_gem_next_request_seqno(pipelined);
2514 				obj->last_fenced_seqno = reg->setup_seqno;
2515 				obj->last_fenced_ring = pipelined;
2516 			}
2517 
2518 			goto update;
2519 		}
2520 
2521 		if (!pipelined) {
2522 			if (reg->setup_seqno) {
2523 				if (!ring_passed_seqno(obj->last_fenced_ring,
2524 						       reg->setup_seqno)) {
2525 					ret = i915_wait_request(obj->last_fenced_ring,
2526 								reg->setup_seqno,
2527 								true);
2528 					if (ret)
2529 						return ret;
2530 				}
2531 
2532 				reg->setup_seqno = 0;
2533 			}
2534 		} else if (obj->last_fenced_ring &&
2535 			   obj->last_fenced_ring != pipelined) {
2536 			ret = i915_gem_object_flush_fence(obj, pipelined);
2537 			if (ret)
2538 				return ret;
2539 		}
2540 
2541 		return 0;
2542 	}
2543 
2544 	reg = i915_find_fence_reg(dev, pipelined);
2545 	if (reg == NULL)
2546 		return -EDEADLK;
2547 
2548 	ret = i915_gem_object_flush_fence(obj, pipelined);
2549 	if (ret)
2550 		return ret;
2551 
2552 	if (reg->obj) {
2553 		struct drm_i915_gem_object *old = reg->obj;
2554 
2555 		drm_gem_object_reference(&old->base);
2556 
2557 		if (old->tiling_mode)
2558 			i915_gem_release_mmap(old);
2559 
2560 		ret = i915_gem_object_flush_fence(old, pipelined);
2561 		if (ret) {
2562 			drm_gem_object_unreference(&old->base);
2563 			return ret;
2564 		}
2565 
2566 		if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2567 			pipelined = NULL;
2568 
2569 		old->fence_reg = I915_FENCE_REG_NONE;
2570 		old->last_fenced_ring = pipelined;
2571 		old->last_fenced_seqno =
2572 			pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
2573 
2574 		drm_gem_object_unreference(&old->base);
2575 	} else if (obj->last_fenced_seqno == 0)
2576 		pipelined = NULL;
2577 
2578 	reg->obj = obj;
2579 	list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2580 	obj->fence_reg = reg - dev_priv->fence_regs;
2581 	obj->last_fenced_ring = pipelined;
2582 
2583 	reg->setup_seqno =
2584 		pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
2585 	obj->last_fenced_seqno = reg->setup_seqno;
2586 
2587 update:
2588 	obj->tiling_changed = false;
2589 	switch (INTEL_INFO(dev)->gen) {
2590 	case 7:
2591 	case 6:
2592 		ret = sandybridge_write_fence_reg(obj, pipelined);
2593 		break;
2594 	case 5:
2595 	case 4:
2596 		ret = i965_write_fence_reg(obj, pipelined);
2597 		break;
2598 	case 3:
2599 		ret = i915_write_fence_reg(obj, pipelined);
2600 		break;
2601 	case 2:
2602 		ret = i830_write_fence_reg(obj, pipelined);
2603 		break;
2604 	}
2605 
2606 	return ret;
2607 }
2608 
2609 /**
2610  * i915_gem_clear_fence_reg - clear out fence register info
2611  * @obj: object to clear
2612  *
2613  * Zeroes out the fence register itself and clears out the associated
2614  * data structures in dev_priv and obj.
2615  */
2616 static void
i915_gem_clear_fence_reg(struct drm_device * dev,struct drm_i915_fence_reg * reg)2617 i915_gem_clear_fence_reg(struct drm_device *dev,
2618 			 struct drm_i915_fence_reg *reg)
2619 {
2620 	drm_i915_private_t *dev_priv = dev->dev_private;
2621 	uint32_t fence_reg = reg - dev_priv->fence_regs;
2622 
2623 	switch (INTEL_INFO(dev)->gen) {
2624 	case 7:
2625 	case 6:
2626 		I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
2627 		break;
2628 	case 5:
2629 	case 4:
2630 		I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
2631 		break;
2632 	case 3:
2633 		if (fence_reg >= 8)
2634 			fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2635 		else
2636 	case 2:
2637 			fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2638 
2639 		I915_WRITE(fence_reg, 0);
2640 		break;
2641 	}
2642 
2643 	list_del_init(&reg->lru_list);
2644 	reg->obj = NULL;
2645 	reg->setup_seqno = 0;
2646 	reg->pin_count = 0;
2647 }
2648 
2649 /**
2650  * Finds free space in the GTT aperture and binds the object there.
2651  */
2652 static int
i915_gem_object_bind_to_gtt(struct drm_i915_gem_object * obj,unsigned alignment,bool map_and_fenceable)2653 i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
2654 			    unsigned alignment,
2655 			    bool map_and_fenceable)
2656 {
2657 	struct drm_device *dev = obj->base.dev;
2658 	drm_i915_private_t *dev_priv = dev->dev_private;
2659 	struct drm_mm_node *free_space;
2660 	gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2661 	u32 size, fence_size, fence_alignment, unfenced_alignment;
2662 	bool mappable, fenceable;
2663 	int ret;
2664 
2665 	if (obj->madv != I915_MADV_WILLNEED) {
2666 		DRM_ERROR("Attempting to bind a purgeable object\n");
2667 		return -EINVAL;
2668 	}
2669 
2670 	fence_size = i915_gem_get_gtt_size(dev,
2671 					   obj->base.size,
2672 					   obj->tiling_mode);
2673 	fence_alignment = i915_gem_get_gtt_alignment(dev,
2674 						     obj->base.size,
2675 						     obj->tiling_mode);
2676 	unfenced_alignment =
2677 		i915_gem_get_unfenced_gtt_alignment(dev,
2678 						    obj->base.size,
2679 						    obj->tiling_mode);
2680 
2681 	if (alignment == 0)
2682 		alignment = map_and_fenceable ? fence_alignment :
2683 						unfenced_alignment;
2684 	if (map_and_fenceable && alignment & (fence_alignment - 1)) {
2685 		DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2686 		return -EINVAL;
2687 	}
2688 
2689 	size = map_and_fenceable ? fence_size : obj->base.size;
2690 
2691 	/* If the object is bigger than the entire aperture, reject it early
2692 	 * before evicting everything in a vain attempt to find space.
2693 	 */
2694 	if (obj->base.size >
2695 	    (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
2696 		DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2697 		return -E2BIG;
2698 	}
2699 
2700  search_free:
2701 	if (map_and_fenceable)
2702 		free_space =
2703 			drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
2704 						    size, alignment, 0,
2705 						    dev_priv->mm.gtt_mappable_end,
2706 						    0);
2707 	else
2708 		free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2709 						size, alignment, 0);
2710 
2711 	if (free_space != NULL) {
2712 		if (map_and_fenceable)
2713 			obj->gtt_space =
2714 				drm_mm_get_block_range_generic(free_space,
2715 							       size, alignment, 0,
2716 							       dev_priv->mm.gtt_mappable_end,
2717 							       0);
2718 		else
2719 			obj->gtt_space =
2720 				drm_mm_get_block(free_space, size, alignment);
2721 	}
2722 	if (obj->gtt_space == NULL) {
2723 		/* If the gtt is empty and we're still having trouble
2724 		 * fitting our object in, we're out of memory.
2725 		 */
2726 		ret = i915_gem_evict_something(dev, size, alignment,
2727 					       map_and_fenceable);
2728 		if (ret)
2729 			return ret;
2730 
2731 		goto search_free;
2732 	}
2733 
2734 	ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
2735 	if (ret) {
2736 		drm_mm_put_block(obj->gtt_space);
2737 		obj->gtt_space = NULL;
2738 
2739 		if (ret == -ENOMEM) {
2740 			/* first try to reclaim some memory by clearing the GTT */
2741 			ret = i915_gem_evict_everything(dev, false);
2742 			if (ret) {
2743 				/* now try to shrink everyone else */
2744 				if (gfpmask) {
2745 					gfpmask = 0;
2746 					goto search_free;
2747 				}
2748 
2749 				return -ENOMEM;
2750 			}
2751 
2752 			goto search_free;
2753 		}
2754 
2755 		return ret;
2756 	}
2757 
2758 	ret = i915_gem_gtt_bind_object(obj);
2759 	if (ret) {
2760 		i915_gem_object_put_pages_gtt(obj);
2761 		drm_mm_put_block(obj->gtt_space);
2762 		obj->gtt_space = NULL;
2763 
2764 		if (i915_gem_evict_everything(dev, false))
2765 			return ret;
2766 
2767 		goto search_free;
2768 	}
2769 
2770 	list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
2771 	list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2772 
2773 	/* Assert that the object is not currently in any GPU domain. As it
2774 	 * wasn't in the GTT, there shouldn't be any way it could have been in
2775 	 * a GPU cache
2776 	 */
2777 	BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2778 	BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2779 
2780 	obj->gtt_offset = obj->gtt_space->start;
2781 
2782 	fenceable =
2783 		obj->gtt_space->size == fence_size &&
2784 		(obj->gtt_space->start & (fence_alignment - 1)) == 0;
2785 
2786 	mappable =
2787 		obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
2788 
2789 	obj->map_and_fenceable = mappable && fenceable;
2790 
2791 	trace_i915_gem_object_bind(obj, map_and_fenceable);
2792 	return 0;
2793 }
2794 
2795 void
i915_gem_clflush_object(struct drm_i915_gem_object * obj)2796 i915_gem_clflush_object(struct drm_i915_gem_object *obj)
2797 {
2798 	/* If we don't have a page list set up, then we're not pinned
2799 	 * to GPU, and we can ignore the cache flush because it'll happen
2800 	 * again at bind time.
2801 	 */
2802 	if (obj->pages == NULL)
2803 		return;
2804 
2805 	/* If the GPU is snooping the contents of the CPU cache,
2806 	 * we do not need to manually clear the CPU cache lines.  However,
2807 	 * the caches are only snooped when the render cache is
2808 	 * flushed/invalidated.  As we always have to emit invalidations
2809 	 * and flushes when moving into and out of the RENDER domain, correct
2810 	 * snooping behaviour occurs naturally as the result of our domain
2811 	 * tracking.
2812 	 */
2813 	if (obj->cache_level != I915_CACHE_NONE)
2814 		return;
2815 
2816 	trace_i915_gem_object_clflush(obj);
2817 
2818 	drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
2819 }
2820 
2821 /** Flushes any GPU write domain for the object if it's dirty. */
2822 static int
i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object * obj)2823 i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
2824 {
2825 	if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
2826 		return 0;
2827 
2828 	/* Queue the GPU write cache flushing we need. */
2829 	return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
2830 }
2831 
2832 /** Flushes the GTT write domain for the object if it's dirty. */
2833 static void
i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object * obj)2834 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
2835 {
2836 	uint32_t old_write_domain;
2837 
2838 	if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
2839 		return;
2840 
2841 	/* No actual flushing is required for the GTT write domain.  Writes
2842 	 * to it immediately go to main memory as far as we know, so there's
2843 	 * no chipset flush.  It also doesn't land in render cache.
2844 	 *
2845 	 * However, we do have to enforce the order so that all writes through
2846 	 * the GTT land before any writes to the device, such as updates to
2847 	 * the GATT itself.
2848 	 */
2849 	wmb();
2850 
2851 	old_write_domain = obj->base.write_domain;
2852 	obj->base.write_domain = 0;
2853 
2854 	trace_i915_gem_object_change_domain(obj,
2855 					    obj->base.read_domains,
2856 					    old_write_domain);
2857 }
2858 
2859 /** Flushes the CPU write domain for the object if it's dirty. */
2860 static void
i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object * obj)2861 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
2862 {
2863 	uint32_t old_write_domain;
2864 
2865 	if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
2866 		return;
2867 
2868 	i915_gem_clflush_object(obj);
2869 	intel_gtt_chipset_flush();
2870 	old_write_domain = obj->base.write_domain;
2871 	obj->base.write_domain = 0;
2872 
2873 	trace_i915_gem_object_change_domain(obj,
2874 					    obj->base.read_domains,
2875 					    old_write_domain);
2876 }
2877 
2878 /**
2879  * Moves a single object to the GTT read, and possibly write domain.
2880  *
2881  * This function returns when the move is complete, including waiting on
2882  * flushes to occur.
2883  */
2884 int
i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object * obj,bool write)2885 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
2886 {
2887 	uint32_t old_write_domain, old_read_domains;
2888 	int ret;
2889 
2890 	/* Not valid to be called on unbound objects. */
2891 	if (obj->gtt_space == NULL)
2892 		return -EINVAL;
2893 
2894 	if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2895 		return 0;
2896 
2897 	ret = i915_gem_object_flush_gpu_write_domain(obj);
2898 	if (ret)
2899 		return ret;
2900 
2901 	if (obj->pending_gpu_write || write) {
2902 		ret = i915_gem_object_wait_rendering(obj);
2903 		if (ret)
2904 			return ret;
2905 	}
2906 
2907 	i915_gem_object_flush_cpu_write_domain(obj);
2908 
2909 	old_write_domain = obj->base.write_domain;
2910 	old_read_domains = obj->base.read_domains;
2911 
2912 	/* It should now be out of any other write domains, and we can update
2913 	 * the domain values for our changes.
2914 	 */
2915 	BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2916 	obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
2917 	if (write) {
2918 		obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2919 		obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2920 		obj->dirty = 1;
2921 	}
2922 
2923 	trace_i915_gem_object_change_domain(obj,
2924 					    old_read_domains,
2925 					    old_write_domain);
2926 
2927 	return 0;
2928 }
2929 
i915_gem_object_set_cache_level(struct drm_i915_gem_object * obj,enum i915_cache_level cache_level)2930 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2931 				    enum i915_cache_level cache_level)
2932 {
2933 	struct drm_device *dev = obj->base.dev;
2934 	drm_i915_private_t *dev_priv = dev->dev_private;
2935 	int ret;
2936 
2937 	if (obj->cache_level == cache_level)
2938 		return 0;
2939 
2940 	if (obj->pin_count) {
2941 		DRM_DEBUG("can not change the cache level of pinned objects\n");
2942 		return -EBUSY;
2943 	}
2944 
2945 	if (obj->gtt_space) {
2946 		ret = i915_gem_object_finish_gpu(obj);
2947 		if (ret)
2948 			return ret;
2949 
2950 		i915_gem_object_finish_gtt(obj);
2951 
2952 		/* Before SandyBridge, you could not use tiling or fence
2953 		 * registers with snooped memory, so relinquish any fences
2954 		 * currently pointing to our region in the aperture.
2955 		 */
2956 		if (INTEL_INFO(obj->base.dev)->gen < 6) {
2957 			ret = i915_gem_object_put_fence(obj);
2958 			if (ret)
2959 				return ret;
2960 		}
2961 
2962 		i915_gem_gtt_rebind_object(obj, cache_level);
2963 		if (obj->has_aliasing_ppgtt_mapping)
2964 			i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
2965 					       obj, cache_level);
2966 	}
2967 
2968 	if (cache_level == I915_CACHE_NONE) {
2969 		u32 old_read_domains, old_write_domain;
2970 
2971 		/* If we're coming from LLC cached, then we haven't
2972 		 * actually been tracking whether the data is in the
2973 		 * CPU cache or not, since we only allow one bit set
2974 		 * in obj->write_domain and have been skipping the clflushes.
2975 		 * Just set it to the CPU cache for now.
2976 		 */
2977 		WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
2978 		WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
2979 
2980 		old_read_domains = obj->base.read_domains;
2981 		old_write_domain = obj->base.write_domain;
2982 
2983 		obj->base.read_domains = I915_GEM_DOMAIN_CPU;
2984 		obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2985 
2986 		trace_i915_gem_object_change_domain(obj,
2987 						    old_read_domains,
2988 						    old_write_domain);
2989 	}
2990 
2991 	obj->cache_level = cache_level;
2992 	return 0;
2993 }
2994 
2995 /*
2996  * Prepare buffer for display plane (scanout, cursors, etc).
2997  * Can be called from an uninterruptible phase (modesetting) and allows
2998  * any flushes to be pipelined (for pageflips).
2999  *
3000  * For the display plane, we want to be in the GTT but out of any write
3001  * domains. So in many ways this looks like set_to_gtt_domain() apart from the
3002  * ability to pipeline the waits, pinning and any additional subtleties
3003  * that may differentiate the display plane from ordinary buffers.
3004  */
3005 int
i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object * obj,u32 alignment,struct intel_ring_buffer * pipelined)3006 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3007 				     u32 alignment,
3008 				     struct intel_ring_buffer *pipelined)
3009 {
3010 	u32 old_read_domains, old_write_domain;
3011 	int ret;
3012 
3013 	ret = i915_gem_object_flush_gpu_write_domain(obj);
3014 	if (ret)
3015 		return ret;
3016 
3017 	if (pipelined != obj->ring) {
3018 		ret = i915_gem_object_wait_rendering(obj);
3019 		if (ret == -ERESTARTSYS)
3020 			return ret;
3021 	}
3022 
3023 	/* The display engine is not coherent with the LLC cache on gen6.  As
3024 	 * a result, we make sure that the pinning that is about to occur is
3025 	 * done with uncached PTEs. This is lowest common denominator for all
3026 	 * chipsets.
3027 	 *
3028 	 * However for gen6+, we could do better by using the GFDT bit instead
3029 	 * of uncaching, which would allow us to flush all the LLC-cached data
3030 	 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3031 	 */
3032 	ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3033 	if (ret)
3034 		return ret;
3035 
3036 	/* As the user may map the buffer once pinned in the display plane
3037 	 * (e.g. libkms for the bootup splash), we have to ensure that we
3038 	 * always use map_and_fenceable for all scanout buffers.
3039 	 */
3040 	ret = i915_gem_object_pin(obj, alignment, true);
3041 	if (ret)
3042 		return ret;
3043 
3044 	i915_gem_object_flush_cpu_write_domain(obj);
3045 
3046 	old_write_domain = obj->base.write_domain;
3047 	old_read_domains = obj->base.read_domains;
3048 
3049 	/* It should now be out of any other write domains, and we can update
3050 	 * the domain values for our changes.
3051 	 */
3052 	BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3053 	obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3054 
3055 	trace_i915_gem_object_change_domain(obj,
3056 					    old_read_domains,
3057 					    old_write_domain);
3058 
3059 	return 0;
3060 }
3061 
3062 int
i915_gem_object_finish_gpu(struct drm_i915_gem_object * obj)3063 i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
3064 {
3065 	int ret;
3066 
3067 	if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
3068 		return 0;
3069 
3070 	if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
3071 		ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
3072 		if (ret)
3073 			return ret;
3074 	}
3075 
3076 	ret = i915_gem_object_wait_rendering(obj);
3077 	if (ret)
3078 		return ret;
3079 
3080 	/* Ensure that we invalidate the GPU's caches and TLBs. */
3081 	obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3082 	return 0;
3083 }
3084 
3085 /**
3086  * Moves a single object to the CPU read, and possibly write domain.
3087  *
3088  * This function returns when the move is complete, including waiting on
3089  * flushes to occur.
3090  */
3091 static int
i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object * obj,bool write)3092 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
3093 {
3094 	uint32_t old_write_domain, old_read_domains;
3095 	int ret;
3096 
3097 	if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3098 		return 0;
3099 
3100 	ret = i915_gem_object_flush_gpu_write_domain(obj);
3101 	if (ret)
3102 		return ret;
3103 
3104 	ret = i915_gem_object_wait_rendering(obj);
3105 	if (ret)
3106 		return ret;
3107 
3108 	i915_gem_object_flush_gtt_write_domain(obj);
3109 
3110 	/* If we have a partially-valid cache of the object in the CPU,
3111 	 * finish invalidating it and free the per-page flags.
3112 	 */
3113 	i915_gem_object_set_to_full_cpu_read_domain(obj);
3114 
3115 	old_write_domain = obj->base.write_domain;
3116 	old_read_domains = obj->base.read_domains;
3117 
3118 	/* Flush the CPU cache if it's still invalid. */
3119 	if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3120 		i915_gem_clflush_object(obj);
3121 
3122 		obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3123 	}
3124 
3125 	/* It should now be out of any other write domains, and we can update
3126 	 * the domain values for our changes.
3127 	 */
3128 	BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3129 
3130 	/* If we're writing through the CPU, then the GPU read domains will
3131 	 * need to be invalidated at next use.
3132 	 */
3133 	if (write) {
3134 		obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3135 		obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3136 	}
3137 
3138 	trace_i915_gem_object_change_domain(obj,
3139 					    old_read_domains,
3140 					    old_write_domain);
3141 
3142 	return 0;
3143 }
3144 
3145 /**
3146  * Moves the object from a partially CPU read to a full one.
3147  *
3148  * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3149  * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3150  */
3151 static void
i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object * obj)3152 i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
3153 {
3154 	if (!obj->page_cpu_valid)
3155 		return;
3156 
3157 	/* If we're partially in the CPU read domain, finish moving it in.
3158 	 */
3159 	if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
3160 		int i;
3161 
3162 		for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3163 			if (obj->page_cpu_valid[i])
3164 				continue;
3165 			drm_clflush_pages(obj->pages + i, 1);
3166 		}
3167 	}
3168 
3169 	/* Free the page_cpu_valid mappings which are now stale, whether
3170 	 * or not we've got I915_GEM_DOMAIN_CPU.
3171 	 */
3172 	kfree(obj->page_cpu_valid);
3173 	obj->page_cpu_valid = NULL;
3174 }
3175 
3176 /**
3177  * Set the CPU read domain on a range of the object.
3178  *
3179  * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3180  * not entirely valid.  The page_cpu_valid member of the object flags which
3181  * pages have been flushed, and will be respected by
3182  * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3183  * of the whole object.
3184  *
3185  * This function returns when the move is complete, including waiting on
3186  * flushes to occur.
3187  */
3188 static int
i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object * obj,uint64_t offset,uint64_t size)3189 i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
3190 					  uint64_t offset, uint64_t size)
3191 {
3192 	uint32_t old_read_domains;
3193 	int i, ret;
3194 
3195 	if (offset == 0 && size == obj->base.size)
3196 		return i915_gem_object_set_to_cpu_domain(obj, 0);
3197 
3198 	ret = i915_gem_object_flush_gpu_write_domain(obj);
3199 	if (ret)
3200 		return ret;
3201 
3202 	ret = i915_gem_object_wait_rendering(obj);
3203 	if (ret)
3204 		return ret;
3205 
3206 	i915_gem_object_flush_gtt_write_domain(obj);
3207 
3208 	/* If we're already fully in the CPU read domain, we're done. */
3209 	if (obj->page_cpu_valid == NULL &&
3210 	    (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
3211 		return 0;
3212 
3213 	/* Otherwise, create/clear the per-page CPU read domain flag if we're
3214 	 * newly adding I915_GEM_DOMAIN_CPU
3215 	 */
3216 	if (obj->page_cpu_valid == NULL) {
3217 		obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3218 					      GFP_KERNEL);
3219 		if (obj->page_cpu_valid == NULL)
3220 			return -ENOMEM;
3221 	} else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3222 		memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
3223 
3224 	/* Flush the cache on any pages that are still invalid from the CPU's
3225 	 * perspective.
3226 	 */
3227 	for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3228 	     i++) {
3229 		if (obj->page_cpu_valid[i])
3230 			continue;
3231 
3232 		drm_clflush_pages(obj->pages + i, 1);
3233 
3234 		obj->page_cpu_valid[i] = 1;
3235 	}
3236 
3237 	/* It should now be out of any other write domains, and we can update
3238 	 * the domain values for our changes.
3239 	 */
3240 	BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3241 
3242 	old_read_domains = obj->base.read_domains;
3243 	obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3244 
3245 	trace_i915_gem_object_change_domain(obj,
3246 					    old_read_domains,
3247 					    obj->base.write_domain);
3248 
3249 	return 0;
3250 }
3251 
3252 /* Throttle our rendering by waiting until the ring has completed our requests
3253  * emitted over 20 msec ago.
3254  *
3255  * Note that if we were to use the current jiffies each time around the loop,
3256  * we wouldn't escape the function with any frames outstanding if the time to
3257  * render a frame was over 20ms.
3258  *
3259  * This should get us reasonable parallelism between CPU and GPU but also
3260  * relatively low latency when blocking on a particular request to finish.
3261  */
3262 static int
i915_gem_ring_throttle(struct drm_device * dev,struct drm_file * file)3263 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3264 {
3265 	struct drm_i915_private *dev_priv = dev->dev_private;
3266 	struct drm_i915_file_private *file_priv = file->driver_priv;
3267 	unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3268 	struct drm_i915_gem_request *request;
3269 	struct intel_ring_buffer *ring = NULL;
3270 	u32 seqno = 0;
3271 	int ret;
3272 
3273 	if (atomic_read(&dev_priv->mm.wedged))
3274 		return -EIO;
3275 
3276 	spin_lock(&file_priv->mm.lock);
3277 	list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3278 		if (time_after_eq(request->emitted_jiffies, recent_enough))
3279 			break;
3280 
3281 		ring = request->ring;
3282 		seqno = request->seqno;
3283 	}
3284 	spin_unlock(&file_priv->mm.lock);
3285 
3286 	if (seqno == 0)
3287 		return 0;
3288 
3289 	ret = 0;
3290 	if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
3291 		/* And wait for the seqno passing without holding any locks and
3292 		 * causing extra latency for others. This is safe as the irq
3293 		 * generation is designed to be run atomically and so is
3294 		 * lockless.
3295 		 */
3296 		if (ring->irq_get(ring)) {
3297 			ret = wait_event_interruptible(ring->irq_queue,
3298 						       i915_seqno_passed(ring->get_seqno(ring), seqno)
3299 						       || atomic_read(&dev_priv->mm.wedged));
3300 			ring->irq_put(ring);
3301 
3302 			if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3303 				ret = -EIO;
3304 		} else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
3305 							     seqno) ||
3306 				    atomic_read(&dev_priv->mm.wedged), 3000)) {
3307 			ret = -EBUSY;
3308 		}
3309 	}
3310 
3311 	if (ret == 0)
3312 		queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3313 
3314 	return ret;
3315 }
3316 
3317 int
i915_gem_object_pin(struct drm_i915_gem_object * obj,uint32_t alignment,bool map_and_fenceable)3318 i915_gem_object_pin(struct drm_i915_gem_object *obj,
3319 		    uint32_t alignment,
3320 		    bool map_and_fenceable)
3321 {
3322 	struct drm_device *dev = obj->base.dev;
3323 	struct drm_i915_private *dev_priv = dev->dev_private;
3324 	int ret;
3325 
3326 	if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3327 		return -EBUSY;
3328 	WARN_ON(i915_verify_lists(dev));
3329 
3330 	if (obj->gtt_space != NULL) {
3331 		if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3332 		    (map_and_fenceable && !obj->map_and_fenceable)) {
3333 			WARN(obj->pin_count,
3334 			     "bo is already pinned with incorrect alignment:"
3335 			     " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3336 			     " obj->map_and_fenceable=%d\n",
3337 			     obj->gtt_offset, alignment,
3338 			     map_and_fenceable,
3339 			     obj->map_and_fenceable);
3340 			ret = i915_gem_object_unbind(obj);
3341 			if (ret)
3342 				return ret;
3343 		}
3344 	}
3345 
3346 	if (obj->gtt_space == NULL) {
3347 		ret = i915_gem_object_bind_to_gtt(obj, alignment,
3348 						  map_and_fenceable);
3349 		if (ret)
3350 			return ret;
3351 	}
3352 
3353 	if (obj->pin_count++ == 0) {
3354 		if (!obj->active)
3355 			list_move_tail(&obj->mm_list,
3356 				       &dev_priv->mm.pinned_list);
3357 	}
3358 	obj->pin_mappable |= map_and_fenceable;
3359 
3360 	WARN_ON(i915_verify_lists(dev));
3361 	return 0;
3362 }
3363 
3364 void
i915_gem_object_unpin(struct drm_i915_gem_object * obj)3365 i915_gem_object_unpin(struct drm_i915_gem_object *obj)
3366 {
3367 	struct drm_device *dev = obj->base.dev;
3368 	drm_i915_private_t *dev_priv = dev->dev_private;
3369 
3370 	WARN_ON(i915_verify_lists(dev));
3371 	BUG_ON(obj->pin_count == 0);
3372 	BUG_ON(obj->gtt_space == NULL);
3373 
3374 	if (--obj->pin_count == 0) {
3375 		if (!obj->active)
3376 			list_move_tail(&obj->mm_list,
3377 				       &dev_priv->mm.inactive_list);
3378 		obj->pin_mappable = false;
3379 	}
3380 	WARN_ON(i915_verify_lists(dev));
3381 }
3382 
3383 int
i915_gem_pin_ioctl(struct drm_device * dev,void * data,struct drm_file * file)3384 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3385 		   struct drm_file *file)
3386 {
3387 	struct drm_i915_gem_pin *args = data;
3388 	struct drm_i915_gem_object *obj;
3389 	int ret;
3390 
3391 	ret = i915_mutex_lock_interruptible(dev);
3392 	if (ret)
3393 		return ret;
3394 
3395 	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3396 	if (&obj->base == NULL) {
3397 		ret = -ENOENT;
3398 		goto unlock;
3399 	}
3400 
3401 	if (obj->madv != I915_MADV_WILLNEED) {
3402 		DRM_ERROR("Attempting to pin a purgeable buffer\n");
3403 		ret = -EINVAL;
3404 		goto out;
3405 	}
3406 
3407 	if (obj->pin_filp != NULL && obj->pin_filp != file) {
3408 		DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3409 			  args->handle);
3410 		ret = -EINVAL;
3411 		goto out;
3412 	}
3413 
3414 	obj->user_pin_count++;
3415 	obj->pin_filp = file;
3416 	if (obj->user_pin_count == 1) {
3417 		ret = i915_gem_object_pin(obj, args->alignment, true);
3418 		if (ret)
3419 			goto out;
3420 	}
3421 
3422 	/* XXX - flush the CPU caches for pinned objects
3423 	 * as the X server doesn't manage domains yet
3424 	 */
3425 	i915_gem_object_flush_cpu_write_domain(obj);
3426 	args->offset = obj->gtt_offset;
3427 out:
3428 	drm_gem_object_unreference(&obj->base);
3429 unlock:
3430 	mutex_unlock(&dev->struct_mutex);
3431 	return ret;
3432 }
3433 
3434 int
i915_gem_unpin_ioctl(struct drm_device * dev,void * data,struct drm_file * file)3435 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3436 		     struct drm_file *file)
3437 {
3438 	struct drm_i915_gem_pin *args = data;
3439 	struct drm_i915_gem_object *obj;
3440 	int ret;
3441 
3442 	ret = i915_mutex_lock_interruptible(dev);
3443 	if (ret)
3444 		return ret;
3445 
3446 	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3447 	if (&obj->base == NULL) {
3448 		ret = -ENOENT;
3449 		goto unlock;
3450 	}
3451 
3452 	if (obj->pin_filp != file) {
3453 		DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3454 			  args->handle);
3455 		ret = -EINVAL;
3456 		goto out;
3457 	}
3458 	obj->user_pin_count--;
3459 	if (obj->user_pin_count == 0) {
3460 		obj->pin_filp = NULL;
3461 		i915_gem_object_unpin(obj);
3462 	}
3463 
3464 out:
3465 	drm_gem_object_unreference(&obj->base);
3466 unlock:
3467 	mutex_unlock(&dev->struct_mutex);
3468 	return ret;
3469 }
3470 
3471 int
i915_gem_busy_ioctl(struct drm_device * dev,void * data,struct drm_file * file)3472 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3473 		    struct drm_file *file)
3474 {
3475 	struct drm_i915_gem_busy *args = data;
3476 	struct drm_i915_gem_object *obj;
3477 	int ret;
3478 
3479 	ret = i915_mutex_lock_interruptible(dev);
3480 	if (ret)
3481 		return ret;
3482 
3483 	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3484 	if (&obj->base == NULL) {
3485 		ret = -ENOENT;
3486 		goto unlock;
3487 	}
3488 
3489 	/* Count all active objects as busy, even if they are currently not used
3490 	 * by the gpu. Users of this interface expect objects to eventually
3491 	 * become non-busy without any further actions, therefore emit any
3492 	 * necessary flushes here.
3493 	 */
3494 	args->busy = obj->active;
3495 	if (args->busy) {
3496 		/* Unconditionally flush objects, even when the gpu still uses this
3497 		 * object. Userspace calling this function indicates that it wants to
3498 		 * use this buffer rather sooner than later, so issuing the required
3499 		 * flush earlier is beneficial.
3500 		 */
3501 		if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
3502 			ret = i915_gem_flush_ring(obj->ring,
3503 						  0, obj->base.write_domain);
3504 		} else if (obj->ring->outstanding_lazy_request ==
3505 			   obj->last_rendering_seqno) {
3506 			struct drm_i915_gem_request *request;
3507 
3508 			/* This ring is not being cleared by active usage,
3509 			 * so emit a request to do so.
3510 			 */
3511 			request = kzalloc(sizeof(*request), GFP_KERNEL);
3512 			if (request) {
3513 				ret = i915_add_request(obj->ring, NULL, request);
3514 				if (ret)
3515 					kfree(request);
3516 			} else
3517 				ret = -ENOMEM;
3518 		}
3519 
3520 		/* Update the active list for the hardware's current position.
3521 		 * Otherwise this only updates on a delayed timer or when irqs
3522 		 * are actually unmasked, and our working set ends up being
3523 		 * larger than required.
3524 		 */
3525 		i915_gem_retire_requests_ring(obj->ring);
3526 
3527 		args->busy = obj->active;
3528 	}
3529 
3530 	drm_gem_object_unreference(&obj->base);
3531 unlock:
3532 	mutex_unlock(&dev->struct_mutex);
3533 	return ret;
3534 }
3535 
3536 int
i915_gem_throttle_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)3537 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3538 			struct drm_file *file_priv)
3539 {
3540 	return i915_gem_ring_throttle(dev, file_priv);
3541 }
3542 
3543 int
i915_gem_madvise_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)3544 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3545 		       struct drm_file *file_priv)
3546 {
3547 	struct drm_i915_gem_madvise *args = data;
3548 	struct drm_i915_gem_object *obj;
3549 	int ret;
3550 
3551 	switch (args->madv) {
3552 	case I915_MADV_DONTNEED:
3553 	case I915_MADV_WILLNEED:
3554 	    break;
3555 	default:
3556 	    return -EINVAL;
3557 	}
3558 
3559 	ret = i915_mutex_lock_interruptible(dev);
3560 	if (ret)
3561 		return ret;
3562 
3563 	obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
3564 	if (&obj->base == NULL) {
3565 		ret = -ENOENT;
3566 		goto unlock;
3567 	}
3568 
3569 	if (obj->pin_count) {
3570 		ret = -EINVAL;
3571 		goto out;
3572 	}
3573 
3574 	if (obj->madv != __I915_MADV_PURGED)
3575 		obj->madv = args->madv;
3576 
3577 	/* if the object is no longer bound, discard its backing storage */
3578 	if (i915_gem_object_is_purgeable(obj) &&
3579 	    obj->gtt_space == NULL)
3580 		i915_gem_object_truncate(obj);
3581 
3582 	args->retained = obj->madv != __I915_MADV_PURGED;
3583 
3584 out:
3585 	drm_gem_object_unreference(&obj->base);
3586 unlock:
3587 	mutex_unlock(&dev->struct_mutex);
3588 	return ret;
3589 }
3590 
i915_gem_alloc_object(struct drm_device * dev,size_t size)3591 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3592 						  size_t size)
3593 {
3594 	struct drm_i915_private *dev_priv = dev->dev_private;
3595 	struct drm_i915_gem_object *obj;
3596 	struct address_space *mapping;
3597 
3598 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3599 	if (obj == NULL)
3600 		return NULL;
3601 
3602 	if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3603 		kfree(obj);
3604 		return NULL;
3605 	}
3606 
3607 	mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3608 	mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3609 
3610 	i915_gem_info_add_obj(dev_priv, size);
3611 
3612 	obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3613 	obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3614 
3615 	if (HAS_LLC(dev)) {
3616 		/* On some devices, we can have the GPU use the LLC (the CPU
3617 		 * cache) for about a 10% performance improvement
3618 		 * compared to uncached.  Graphics requests other than
3619 		 * display scanout are coherent with the CPU in
3620 		 * accessing this cache.  This means in this mode we
3621 		 * don't need to clflush on the CPU side, and on the
3622 		 * GPU side we only need to flush internal caches to
3623 		 * get data visible to the CPU.
3624 		 *
3625 		 * However, we maintain the display planes as UC, and so
3626 		 * need to rebind when first used as such.
3627 		 */
3628 		obj->cache_level = I915_CACHE_LLC;
3629 	} else
3630 		obj->cache_level = I915_CACHE_NONE;
3631 
3632 	obj->base.driver_private = NULL;
3633 	obj->fence_reg = I915_FENCE_REG_NONE;
3634 	INIT_LIST_HEAD(&obj->mm_list);
3635 	INIT_LIST_HEAD(&obj->gtt_list);
3636 	INIT_LIST_HEAD(&obj->ring_list);
3637 	INIT_LIST_HEAD(&obj->exec_list);
3638 	INIT_LIST_HEAD(&obj->gpu_write_list);
3639 	obj->madv = I915_MADV_WILLNEED;
3640 	/* Avoid an unnecessary call to unbind on the first bind. */
3641 	obj->map_and_fenceable = true;
3642 
3643 	return obj;
3644 }
3645 
i915_gem_init_object(struct drm_gem_object * obj)3646 int i915_gem_init_object(struct drm_gem_object *obj)
3647 {
3648 	BUG();
3649 
3650 	return 0;
3651 }
3652 
i915_gem_free_object_tail(struct drm_i915_gem_object * obj)3653 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
3654 {
3655 	struct drm_device *dev = obj->base.dev;
3656 	drm_i915_private_t *dev_priv = dev->dev_private;
3657 	int ret;
3658 
3659 	ret = i915_gem_object_unbind(obj);
3660 	if (ret == -ERESTARTSYS) {
3661 		list_move(&obj->mm_list,
3662 			  &dev_priv->mm.deferred_free_list);
3663 		return;
3664 	}
3665 
3666 	trace_i915_gem_object_destroy(obj);
3667 
3668 	if (obj->base.map_list.map)
3669 		drm_gem_free_mmap_offset(&obj->base);
3670 
3671 	drm_gem_object_release(&obj->base);
3672 	i915_gem_info_remove_obj(dev_priv, obj->base.size);
3673 
3674 	kfree(obj->page_cpu_valid);
3675 	kfree(obj->bit_17);
3676 	kfree(obj);
3677 }
3678 
i915_gem_free_object(struct drm_gem_object * gem_obj)3679 void i915_gem_free_object(struct drm_gem_object *gem_obj)
3680 {
3681 	struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3682 	struct drm_device *dev = obj->base.dev;
3683 
3684 	while (obj->pin_count > 0)
3685 		i915_gem_object_unpin(obj);
3686 
3687 	if (obj->phys_obj)
3688 		i915_gem_detach_phys_object(dev, obj);
3689 
3690 	i915_gem_free_object_tail(obj);
3691 }
3692 
3693 int
i915_gem_idle(struct drm_device * dev)3694 i915_gem_idle(struct drm_device *dev)
3695 {
3696 	drm_i915_private_t *dev_priv = dev->dev_private;
3697 	int ret;
3698 
3699 	mutex_lock(&dev->struct_mutex);
3700 
3701 	if (dev_priv->mm.suspended) {
3702 		mutex_unlock(&dev->struct_mutex);
3703 		return 0;
3704 	}
3705 
3706 	ret = i915_gpu_idle(dev, true);
3707 	if (ret) {
3708 		mutex_unlock(&dev->struct_mutex);
3709 		return ret;
3710 	}
3711 
3712 	/* Under UMS, be paranoid and evict. */
3713 	if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
3714 		ret = i915_gem_evict_inactive(dev, false);
3715 		if (ret) {
3716 			mutex_unlock(&dev->struct_mutex);
3717 			return ret;
3718 		}
3719 	}
3720 
3721 	i915_gem_reset_fences(dev);
3722 
3723 	/* Hack!  Don't let anybody do execbuf while we don't control the chip.
3724 	 * We need to replace this with a semaphore, or something.
3725 	 * And not confound mm.suspended!
3726 	 */
3727 	dev_priv->mm.suspended = 1;
3728 	del_timer_sync(&dev_priv->hangcheck_timer);
3729 
3730 	i915_kernel_lost_context(dev);
3731 	i915_gem_cleanup_ringbuffer(dev);
3732 
3733 	mutex_unlock(&dev->struct_mutex);
3734 
3735 	/* Cancel the retire work handler, which should be idle now. */
3736 	cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3737 
3738 	return 0;
3739 }
3740 
i915_gem_init_swizzling(struct drm_device * dev)3741 void i915_gem_init_swizzling(struct drm_device *dev)
3742 {
3743 	drm_i915_private_t *dev_priv = dev->dev_private;
3744 
3745 	if (INTEL_INFO(dev)->gen < 5 ||
3746 	    dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3747 		return;
3748 
3749 	I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3750 				 DISP_TILE_SURFACE_SWIZZLING);
3751 
3752 	if (IS_GEN5(dev))
3753 		return;
3754 
3755 	I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3756 	if (IS_GEN6(dev))
3757 		I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_SNB));
3758 	else
3759 		I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_IVB));
3760 }
3761 
i915_gem_init_ppgtt(struct drm_device * dev)3762 void i915_gem_init_ppgtt(struct drm_device *dev)
3763 {
3764 	drm_i915_private_t *dev_priv = dev->dev_private;
3765 	uint32_t pd_offset;
3766 	struct intel_ring_buffer *ring;
3767 	struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
3768 	uint32_t __iomem *pd_addr;
3769 	uint32_t pd_entry;
3770 	int i;
3771 
3772 	if (!dev_priv->mm.aliasing_ppgtt)
3773 		return;
3774 
3775 
3776 	pd_addr = dev_priv->mm.gtt->gtt + ppgtt->pd_offset/sizeof(uint32_t);
3777 	for (i = 0; i < ppgtt->num_pd_entries; i++) {
3778 		dma_addr_t pt_addr;
3779 
3780 		if (dev_priv->mm.gtt->needs_dmar)
3781 			pt_addr = ppgtt->pt_dma_addr[i];
3782 		else
3783 			pt_addr = page_to_phys(ppgtt->pt_pages[i]);
3784 
3785 		pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
3786 		pd_entry |= GEN6_PDE_VALID;
3787 
3788 		writel(pd_entry, pd_addr + i);
3789 	}
3790 	readl(pd_addr);
3791 
3792 	pd_offset = ppgtt->pd_offset;
3793 	pd_offset /= 64; /* in cachelines, */
3794 	pd_offset <<= 16;
3795 
3796 	if (INTEL_INFO(dev)->gen == 6) {
3797 		uint32_t ecochk = I915_READ(GAM_ECOCHK);
3798 		I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
3799 				       ECOCHK_PPGTT_CACHE64B);
3800 		I915_WRITE(GFX_MODE, GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3801 	} else if (INTEL_INFO(dev)->gen >= 7) {
3802 		I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
3803 		/* GFX_MODE is per-ring on gen7+ */
3804 	}
3805 
3806 	for (i = 0; i < I915_NUM_RINGS; i++) {
3807 		ring = &dev_priv->ring[i];
3808 
3809 		if (INTEL_INFO(dev)->gen >= 7)
3810 			I915_WRITE(RING_MODE_GEN7(ring),
3811 				   GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3812 
3813 		I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
3814 		I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
3815 	}
3816 }
3817 
3818 int
i915_gem_init_hw(struct drm_device * dev)3819 i915_gem_init_hw(struct drm_device *dev)
3820 {
3821 	drm_i915_private_t *dev_priv = dev->dev_private;
3822 	int ret;
3823 
3824 	i915_gem_init_swizzling(dev);
3825 
3826 	ret = intel_init_render_ring_buffer(dev);
3827 	if (ret)
3828 		return ret;
3829 
3830 	if (HAS_BSD(dev)) {
3831 		ret = intel_init_bsd_ring_buffer(dev);
3832 		if (ret)
3833 			goto cleanup_render_ring;
3834 	}
3835 
3836 	if (HAS_BLT(dev)) {
3837 		ret = intel_init_blt_ring_buffer(dev);
3838 		if (ret)
3839 			goto cleanup_bsd_ring;
3840 	}
3841 
3842 	dev_priv->next_seqno = 1;
3843 
3844 	i915_gem_init_ppgtt(dev);
3845 
3846 	return 0;
3847 
3848 cleanup_bsd_ring:
3849 	intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
3850 cleanup_render_ring:
3851 	intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
3852 	return ret;
3853 }
3854 
3855 void
i915_gem_cleanup_ringbuffer(struct drm_device * dev)3856 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3857 {
3858 	drm_i915_private_t *dev_priv = dev->dev_private;
3859 	int i;
3860 
3861 	for (i = 0; i < I915_NUM_RINGS; i++)
3862 		intel_cleanup_ring_buffer(&dev_priv->ring[i]);
3863 }
3864 
3865 int
i915_gem_entervt_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)3866 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3867 		       struct drm_file *file_priv)
3868 {
3869 	drm_i915_private_t *dev_priv = dev->dev_private;
3870 	int ret, i;
3871 
3872 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3873 		return 0;
3874 
3875 	if (atomic_read(&dev_priv->mm.wedged)) {
3876 		DRM_ERROR("Reenabling wedged hardware, good luck\n");
3877 		atomic_set(&dev_priv->mm.wedged, 0);
3878 	}
3879 
3880 	mutex_lock(&dev->struct_mutex);
3881 	dev_priv->mm.suspended = 0;
3882 
3883 	ret = i915_gem_init_hw(dev);
3884 	if (ret != 0) {
3885 		mutex_unlock(&dev->struct_mutex);
3886 		return ret;
3887 	}
3888 
3889 	BUG_ON(!list_empty(&dev_priv->mm.active_list));
3890 	BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3891 	BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3892 	for (i = 0; i < I915_NUM_RINGS; i++) {
3893 		BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3894 		BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3895 	}
3896 	mutex_unlock(&dev->struct_mutex);
3897 
3898 	ret = drm_irq_install(dev);
3899 	if (ret)
3900 		goto cleanup_ringbuffer;
3901 
3902 	return 0;
3903 
3904 cleanup_ringbuffer:
3905 	mutex_lock(&dev->struct_mutex);
3906 	i915_gem_cleanup_ringbuffer(dev);
3907 	dev_priv->mm.suspended = 1;
3908 	mutex_unlock(&dev->struct_mutex);
3909 
3910 	return ret;
3911 }
3912 
3913 int
i915_gem_leavevt_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)3914 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3915 		       struct drm_file *file_priv)
3916 {
3917 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3918 		return 0;
3919 
3920 	drm_irq_uninstall(dev);
3921 	return i915_gem_idle(dev);
3922 }
3923 
3924 void
i915_gem_lastclose(struct drm_device * dev)3925 i915_gem_lastclose(struct drm_device *dev)
3926 {
3927 	int ret;
3928 
3929 	if (drm_core_check_feature(dev, DRIVER_MODESET))
3930 		return;
3931 
3932 	ret = i915_gem_idle(dev);
3933 	if (ret)
3934 		DRM_ERROR("failed to idle hardware: %d\n", ret);
3935 }
3936 
3937 static void
init_ring_lists(struct intel_ring_buffer * ring)3938 init_ring_lists(struct intel_ring_buffer *ring)
3939 {
3940 	INIT_LIST_HEAD(&ring->active_list);
3941 	INIT_LIST_HEAD(&ring->request_list);
3942 	INIT_LIST_HEAD(&ring->gpu_write_list);
3943 }
3944 
3945 void
i915_gem_load(struct drm_device * dev)3946 i915_gem_load(struct drm_device *dev)
3947 {
3948 	int i;
3949 	drm_i915_private_t *dev_priv = dev->dev_private;
3950 
3951 	INIT_LIST_HEAD(&dev_priv->mm.active_list);
3952 	INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3953 	INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3954 	INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
3955 	INIT_LIST_HEAD(&dev_priv->mm.fence_list);
3956 	INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
3957 	INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
3958 	for (i = 0; i < I915_NUM_RINGS; i++)
3959 		init_ring_lists(&dev_priv->ring[i]);
3960 	for (i = 0; i < I915_MAX_NUM_FENCES; i++)
3961 		INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
3962 	INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3963 			  i915_gem_retire_work_handler);
3964 	init_completion(&dev_priv->error_completion);
3965 
3966 	/* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3967 	if (IS_GEN3(dev)) {
3968 		u32 tmp = I915_READ(MI_ARB_STATE);
3969 		if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3970 			/* arb state is a masked write, so set bit + bit in mask */
3971 			tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3972 			I915_WRITE(MI_ARB_STATE, tmp);
3973 		}
3974 	}
3975 
3976 	dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3977 
3978 	/* Old X drivers will take 0-2 for front, back, depth buffers */
3979 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3980 		dev_priv->fence_reg_start = 3;
3981 
3982 	if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3983 		dev_priv->num_fence_regs = 16;
3984 	else
3985 		dev_priv->num_fence_regs = 8;
3986 
3987 	/* Initialize fence registers to zero */
3988 	for (i = 0; i < dev_priv->num_fence_regs; i++) {
3989 		i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
3990 	}
3991 
3992 	i915_gem_detect_bit_6_swizzle(dev);
3993 	init_waitqueue_head(&dev_priv->pending_flip_queue);
3994 
3995 	dev_priv->mm.interruptible = true;
3996 
3997 	dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3998 	dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3999 	register_shrinker(&dev_priv->mm.inactive_shrinker);
4000 }
4001 
4002 /*
4003  * Create a physically contiguous memory object for this object
4004  * e.g. for cursor + overlay regs
4005  */
i915_gem_init_phys_object(struct drm_device * dev,int id,int size,int align)4006 static int i915_gem_init_phys_object(struct drm_device *dev,
4007 				     int id, int size, int align)
4008 {
4009 	drm_i915_private_t *dev_priv = dev->dev_private;
4010 	struct drm_i915_gem_phys_object *phys_obj;
4011 	int ret;
4012 
4013 	if (dev_priv->mm.phys_objs[id - 1] || !size)
4014 		return 0;
4015 
4016 	phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
4017 	if (!phys_obj)
4018 		return -ENOMEM;
4019 
4020 	phys_obj->id = id;
4021 
4022 	phys_obj->handle = drm_pci_alloc(dev, size, align);
4023 	if (!phys_obj->handle) {
4024 		ret = -ENOMEM;
4025 		goto kfree_obj;
4026 	}
4027 #ifdef CONFIG_X86
4028 	set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4029 #endif
4030 
4031 	dev_priv->mm.phys_objs[id - 1] = phys_obj;
4032 
4033 	return 0;
4034 kfree_obj:
4035 	kfree(phys_obj);
4036 	return ret;
4037 }
4038 
i915_gem_free_phys_object(struct drm_device * dev,int id)4039 static void i915_gem_free_phys_object(struct drm_device *dev, int id)
4040 {
4041 	drm_i915_private_t *dev_priv = dev->dev_private;
4042 	struct drm_i915_gem_phys_object *phys_obj;
4043 
4044 	if (!dev_priv->mm.phys_objs[id - 1])
4045 		return;
4046 
4047 	phys_obj = dev_priv->mm.phys_objs[id - 1];
4048 	if (phys_obj->cur_obj) {
4049 		i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4050 	}
4051 
4052 #ifdef CONFIG_X86
4053 	set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4054 #endif
4055 	drm_pci_free(dev, phys_obj->handle);
4056 	kfree(phys_obj);
4057 	dev_priv->mm.phys_objs[id - 1] = NULL;
4058 }
4059 
i915_gem_free_all_phys_object(struct drm_device * dev)4060 void i915_gem_free_all_phys_object(struct drm_device *dev)
4061 {
4062 	int i;
4063 
4064 	for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4065 		i915_gem_free_phys_object(dev, i);
4066 }
4067 
i915_gem_detach_phys_object(struct drm_device * dev,struct drm_i915_gem_object * obj)4068 void i915_gem_detach_phys_object(struct drm_device *dev,
4069 				 struct drm_i915_gem_object *obj)
4070 {
4071 	struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4072 	char *vaddr;
4073 	int i;
4074 	int page_count;
4075 
4076 	if (!obj->phys_obj)
4077 		return;
4078 	vaddr = obj->phys_obj->handle->vaddr;
4079 
4080 	page_count = obj->base.size / PAGE_SIZE;
4081 	for (i = 0; i < page_count; i++) {
4082 		struct page *page = shmem_read_mapping_page(mapping, i);
4083 		if (!IS_ERR(page)) {
4084 			char *dst = kmap_atomic(page);
4085 			memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4086 			kunmap_atomic(dst);
4087 
4088 			drm_clflush_pages(&page, 1);
4089 
4090 			set_page_dirty(page);
4091 			mark_page_accessed(page);
4092 			page_cache_release(page);
4093 		}
4094 	}
4095 	intel_gtt_chipset_flush();
4096 
4097 	obj->phys_obj->cur_obj = NULL;
4098 	obj->phys_obj = NULL;
4099 }
4100 
4101 int
i915_gem_attach_phys_object(struct drm_device * dev,struct drm_i915_gem_object * obj,int id,int align)4102 i915_gem_attach_phys_object(struct drm_device *dev,
4103 			    struct drm_i915_gem_object *obj,
4104 			    int id,
4105 			    int align)
4106 {
4107 	struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4108 	drm_i915_private_t *dev_priv = dev->dev_private;
4109 	int ret = 0;
4110 	int page_count;
4111 	int i;
4112 
4113 	if (id > I915_MAX_PHYS_OBJECT)
4114 		return -EINVAL;
4115 
4116 	if (obj->phys_obj) {
4117 		if (obj->phys_obj->id == id)
4118 			return 0;
4119 		i915_gem_detach_phys_object(dev, obj);
4120 	}
4121 
4122 	/* create a new object */
4123 	if (!dev_priv->mm.phys_objs[id - 1]) {
4124 		ret = i915_gem_init_phys_object(dev, id,
4125 						obj->base.size, align);
4126 		if (ret) {
4127 			DRM_ERROR("failed to init phys object %d size: %zu\n",
4128 				  id, obj->base.size);
4129 			return ret;
4130 		}
4131 	}
4132 
4133 	/* bind to the object */
4134 	obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4135 	obj->phys_obj->cur_obj = obj;
4136 
4137 	page_count = obj->base.size / PAGE_SIZE;
4138 
4139 	for (i = 0; i < page_count; i++) {
4140 		struct page *page;
4141 		char *dst, *src;
4142 
4143 		page = shmem_read_mapping_page(mapping, i);
4144 		if (IS_ERR(page))
4145 			return PTR_ERR(page);
4146 
4147 		src = kmap_atomic(page);
4148 		dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4149 		memcpy(dst, src, PAGE_SIZE);
4150 		kunmap_atomic(src);
4151 
4152 		mark_page_accessed(page);
4153 		page_cache_release(page);
4154 	}
4155 
4156 	return 0;
4157 }
4158 
4159 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)4160 i915_gem_phys_pwrite(struct drm_device *dev,
4161 		     struct drm_i915_gem_object *obj,
4162 		     struct drm_i915_gem_pwrite *args,
4163 		     struct drm_file *file_priv)
4164 {
4165 	void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
4166 	char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
4167 
4168 	if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4169 		unsigned long unwritten;
4170 
4171 		/* The physical object once assigned is fixed for the lifetime
4172 		 * of the obj, so we can safely drop the lock and continue
4173 		 * to access vaddr.
4174 		 */
4175 		mutex_unlock(&dev->struct_mutex);
4176 		unwritten = copy_from_user(vaddr, user_data, args->size);
4177 		mutex_lock(&dev->struct_mutex);
4178 		if (unwritten)
4179 			return -EFAULT;
4180 	}
4181 
4182 	intel_gtt_chipset_flush();
4183 	return 0;
4184 }
4185 
i915_gem_release(struct drm_device * dev,struct drm_file * file)4186 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
4187 {
4188 	struct drm_i915_file_private *file_priv = file->driver_priv;
4189 
4190 	/* Clean up our request list when the client is going away, so that
4191 	 * later retire_requests won't dereference our soon-to-be-gone
4192 	 * file_priv.
4193 	 */
4194 	spin_lock(&file_priv->mm.lock);
4195 	while (!list_empty(&file_priv->mm.request_list)) {
4196 		struct drm_i915_gem_request *request;
4197 
4198 		request = list_first_entry(&file_priv->mm.request_list,
4199 					   struct drm_i915_gem_request,
4200 					   client_list);
4201 		list_del(&request->client_list);
4202 		request->file_priv = NULL;
4203 	}
4204 	spin_unlock(&file_priv->mm.lock);
4205 }
4206 
4207 static int
i915_gpu_is_active(struct drm_device * dev)4208 i915_gpu_is_active(struct drm_device *dev)
4209 {
4210 	drm_i915_private_t *dev_priv = dev->dev_private;
4211 	int lists_empty;
4212 
4213 	lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
4214 		      list_empty(&dev_priv->mm.active_list);
4215 
4216 	return !lists_empty;
4217 }
4218 
4219 static int
i915_gem_inactive_shrink(struct shrinker * shrinker,struct shrink_control * sc)4220 i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
4221 {
4222 	struct drm_i915_private *dev_priv =
4223 		container_of(shrinker,
4224 			     struct drm_i915_private,
4225 			     mm.inactive_shrinker);
4226 	struct drm_device *dev = dev_priv->dev;
4227 	struct drm_i915_gem_object *obj, *next;
4228 	int nr_to_scan = sc->nr_to_scan;
4229 	int cnt;
4230 
4231 	if (!mutex_trylock(&dev->struct_mutex))
4232 		return 0;
4233 
4234 	/* "fast-path" to count number of available objects */
4235 	if (nr_to_scan == 0) {
4236 		cnt = 0;
4237 		list_for_each_entry(obj,
4238 				    &dev_priv->mm.inactive_list,
4239 				    mm_list)
4240 			cnt++;
4241 		mutex_unlock(&dev->struct_mutex);
4242 		return cnt / 100 * sysctl_vfs_cache_pressure;
4243 	}
4244 
4245 rescan:
4246 	/* first scan for clean buffers */
4247 	i915_gem_retire_requests(dev);
4248 
4249 	list_for_each_entry_safe(obj, next,
4250 				 &dev_priv->mm.inactive_list,
4251 				 mm_list) {
4252 		if (i915_gem_object_is_purgeable(obj)) {
4253 			if (i915_gem_object_unbind(obj) == 0 &&
4254 			    --nr_to_scan == 0)
4255 				break;
4256 		}
4257 	}
4258 
4259 	/* second pass, evict/count anything still on the inactive list */
4260 	cnt = 0;
4261 	list_for_each_entry_safe(obj, next,
4262 				 &dev_priv->mm.inactive_list,
4263 				 mm_list) {
4264 		if (nr_to_scan &&
4265 		    i915_gem_object_unbind(obj) == 0)
4266 			nr_to_scan--;
4267 		else
4268 			cnt++;
4269 	}
4270 
4271 	if (nr_to_scan && i915_gpu_is_active(dev)) {
4272 		/*
4273 		 * We are desperate for pages, so as a last resort, wait
4274 		 * for the GPU to finish and discard whatever we can.
4275 		 * This has a dramatic impact to reduce the number of
4276 		 * OOM-killer events whilst running the GPU aggressively.
4277 		 */
4278 		if (i915_gpu_idle(dev, true) == 0)
4279 			goto rescan;
4280 	}
4281 	mutex_unlock(&dev->struct_mutex);
4282 	return cnt / 100 * sysctl_vfs_cache_pressure;
4283 }
4284