1 /*
2  * Copyright © 2010 Daniel Vetter
3  * Copyright © 2011-2014 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  */
25 
26 #include <linux/slab.h> /* fault-inject.h is not standalone! */
27 
28 #include <linux/fault-inject.h>
29 #include <linux/log2.h>
30 #include <linux/random.h>
31 #include <linux/seq_file.h>
32 #include <linux/stop_machine.h>
33 
34 #include <asm/set_memory.h>
35 
36 #include <drm/i915_drm.h>
37 
38 #include "display/intel_frontbuffer.h"
39 #include "gt/intel_gt.h"
40 
41 #include "i915_drv.h"
42 #include "i915_scatterlist.h"
43 #include "i915_trace.h"
44 #include "i915_vgpu.h"
45 #include "intel_drv.h"
46 
47 #define I915_GFP_ALLOW_FAIL (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN)
48 
49 #if IS_ENABLED(CONFIG_DRM_I915_TRACE_GTT)
50 #define DBG(...) trace_printk(__VA_ARGS__)
51 #else
52 #define DBG(...)
53 #endif
54 
55 /**
56  * DOC: Global GTT views
57  *
58  * Background and previous state
59  *
60  * Historically objects could exists (be bound) in global GTT space only as
61  * singular instances with a view representing all of the object's backing pages
62  * in a linear fashion. This view will be called a normal view.
63  *
64  * To support multiple views of the same object, where the number of mapped
65  * pages is not equal to the backing store, or where the layout of the pages
66  * is not linear, concept of a GGTT view was added.
67  *
68  * One example of an alternative view is a stereo display driven by a single
69  * image. In this case we would have a framebuffer looking like this
70  * (2x2 pages):
71  *
72  *    12
73  *    34
74  *
75  * Above would represent a normal GGTT view as normally mapped for GPU or CPU
76  * rendering. In contrast, fed to the display engine would be an alternative
77  * view which could look something like this:
78  *
79  *   1212
80  *   3434
81  *
82  * In this example both the size and layout of pages in the alternative view is
83  * different from the normal view.
84  *
85  * Implementation and usage
86  *
87  * GGTT views are implemented using VMAs and are distinguished via enum
88  * i915_ggtt_view_type and struct i915_ggtt_view.
89  *
90  * A new flavour of core GEM functions which work with GGTT bound objects were
91  * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
92  * renaming  in large amounts of code. They take the struct i915_ggtt_view
93  * parameter encapsulating all metadata required to implement a view.
94  *
95  * As a helper for callers which are only interested in the normal view,
96  * globally const i915_ggtt_view_normal singleton instance exists. All old core
97  * GEM API functions, the ones not taking the view parameter, are operating on,
98  * or with the normal GGTT view.
99  *
100  * Code wanting to add or use a new GGTT view needs to:
101  *
102  * 1. Add a new enum with a suitable name.
103  * 2. Extend the metadata in the i915_ggtt_view structure if required.
104  * 3. Add support to i915_get_vma_pages().
105  *
106  * New views are required to build a scatter-gather table from within the
107  * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
108  * exists for the lifetime of an VMA.
109  *
110  * Core API is designed to have copy semantics which means that passed in
111  * struct i915_ggtt_view does not need to be persistent (left around after
112  * calling the core API functions).
113  *
114  */
115 
116 #define as_pd(x) container_of((x), typeof(struct i915_page_directory), pt)
117 
118 static int
119 i915_get_ggtt_vma_pages(struct i915_vma *vma);
120 
121 static void gen6_ggtt_invalidate(struct i915_ggtt *ggtt)
122 {
123 	struct intel_uncore *uncore = &ggtt->vm.i915->uncore;
124 
125 	/*
126 	 * Note that as an uncached mmio write, this will flush the
127 	 * WCB of the writes into the GGTT before it triggers the invalidate.
128 	 */
129 	intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
130 }
131 
132 static void guc_ggtt_invalidate(struct i915_ggtt *ggtt)
133 {
134 	struct intel_uncore *uncore = &ggtt->vm.i915->uncore;
135 
136 	gen6_ggtt_invalidate(ggtt);
137 	intel_uncore_write_fw(uncore, GEN8_GTCR, GEN8_GTCR_INVALIDATE);
138 }
139 
140 static void gmch_ggtt_invalidate(struct i915_ggtt *ggtt)
141 {
142 	intel_gtt_chipset_flush();
143 }
144 
145 static int ppgtt_bind_vma(struct i915_vma *vma,
146 			  enum i915_cache_level cache_level,
147 			  u32 unused)
148 {
149 	u32 pte_flags;
150 	int err;
151 
152 	if (!(vma->flags & I915_VMA_LOCAL_BIND)) {
153 		err = vma->vm->allocate_va_range(vma->vm,
154 						 vma->node.start, vma->size);
155 		if (err)
156 			return err;
157 	}
158 
159 	/* Applicable to VLV, and gen8+ */
160 	pte_flags = 0;
161 	if (i915_gem_object_is_readonly(vma->obj))
162 		pte_flags |= PTE_READ_ONLY;
163 
164 	vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
165 
166 	return 0;
167 }
168 
169 static void ppgtt_unbind_vma(struct i915_vma *vma)
170 {
171 	vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
172 }
173 
174 static int ppgtt_set_pages(struct i915_vma *vma)
175 {
176 	GEM_BUG_ON(vma->pages);
177 
178 	vma->pages = vma->obj->mm.pages;
179 
180 	vma->page_sizes = vma->obj->mm.page_sizes;
181 
182 	return 0;
183 }
184 
185 static void clear_pages(struct i915_vma *vma)
186 {
187 	GEM_BUG_ON(!vma->pages);
188 
189 	if (vma->pages != vma->obj->mm.pages) {
190 		sg_free_table(vma->pages);
191 		kfree(vma->pages);
192 	}
193 	vma->pages = NULL;
194 
195 	memset(&vma->page_sizes, 0, sizeof(vma->page_sizes));
196 }
197 
198 static u64 gen8_pte_encode(dma_addr_t addr,
199 			   enum i915_cache_level level,
200 			   u32 flags)
201 {
202 	gen8_pte_t pte = addr | _PAGE_PRESENT | _PAGE_RW;
203 
204 	if (unlikely(flags & PTE_READ_ONLY))
205 		pte &= ~_PAGE_RW;
206 
207 	switch (level) {
208 	case I915_CACHE_NONE:
209 		pte |= PPAT_UNCACHED;
210 		break;
211 	case I915_CACHE_WT:
212 		pte |= PPAT_DISPLAY_ELLC;
213 		break;
214 	default:
215 		pte |= PPAT_CACHED;
216 		break;
217 	}
218 
219 	return pte;
220 }
221 
222 static u64 gen8_pde_encode(const dma_addr_t addr,
223 			   const enum i915_cache_level level)
224 {
225 	u64 pde = _PAGE_PRESENT | _PAGE_RW;
226 	pde |= addr;
227 	if (level != I915_CACHE_NONE)
228 		pde |= PPAT_CACHED_PDE;
229 	else
230 		pde |= PPAT_UNCACHED;
231 	return pde;
232 }
233 
234 static u64 snb_pte_encode(dma_addr_t addr,
235 			  enum i915_cache_level level,
236 			  u32 flags)
237 {
238 	gen6_pte_t pte = GEN6_PTE_VALID;
239 	pte |= GEN6_PTE_ADDR_ENCODE(addr);
240 
241 	switch (level) {
242 	case I915_CACHE_L3_LLC:
243 	case I915_CACHE_LLC:
244 		pte |= GEN6_PTE_CACHE_LLC;
245 		break;
246 	case I915_CACHE_NONE:
247 		pte |= GEN6_PTE_UNCACHED;
248 		break;
249 	default:
250 		MISSING_CASE(level);
251 	}
252 
253 	return pte;
254 }
255 
256 static u64 ivb_pte_encode(dma_addr_t addr,
257 			  enum i915_cache_level level,
258 			  u32 flags)
259 {
260 	gen6_pte_t pte = GEN6_PTE_VALID;
261 	pte |= GEN6_PTE_ADDR_ENCODE(addr);
262 
263 	switch (level) {
264 	case I915_CACHE_L3_LLC:
265 		pte |= GEN7_PTE_CACHE_L3_LLC;
266 		break;
267 	case I915_CACHE_LLC:
268 		pte |= GEN6_PTE_CACHE_LLC;
269 		break;
270 	case I915_CACHE_NONE:
271 		pte |= GEN6_PTE_UNCACHED;
272 		break;
273 	default:
274 		MISSING_CASE(level);
275 	}
276 
277 	return pte;
278 }
279 
280 static u64 byt_pte_encode(dma_addr_t addr,
281 			  enum i915_cache_level level,
282 			  u32 flags)
283 {
284 	gen6_pte_t pte = GEN6_PTE_VALID;
285 	pte |= GEN6_PTE_ADDR_ENCODE(addr);
286 
287 	if (!(flags & PTE_READ_ONLY))
288 		pte |= BYT_PTE_WRITEABLE;
289 
290 	if (level != I915_CACHE_NONE)
291 		pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
292 
293 	return pte;
294 }
295 
296 static u64 hsw_pte_encode(dma_addr_t addr,
297 			  enum i915_cache_level level,
298 			  u32 flags)
299 {
300 	gen6_pte_t pte = GEN6_PTE_VALID;
301 	pte |= HSW_PTE_ADDR_ENCODE(addr);
302 
303 	if (level != I915_CACHE_NONE)
304 		pte |= HSW_WB_LLC_AGE3;
305 
306 	return pte;
307 }
308 
309 static u64 iris_pte_encode(dma_addr_t addr,
310 			   enum i915_cache_level level,
311 			   u32 flags)
312 {
313 	gen6_pte_t pte = GEN6_PTE_VALID;
314 	pte |= HSW_PTE_ADDR_ENCODE(addr);
315 
316 	switch (level) {
317 	case I915_CACHE_NONE:
318 		break;
319 	case I915_CACHE_WT:
320 		pte |= HSW_WT_ELLC_LLC_AGE3;
321 		break;
322 	default:
323 		pte |= HSW_WB_ELLC_LLC_AGE3;
324 		break;
325 	}
326 
327 	return pte;
328 }
329 
330 static void stash_init(struct pagestash *stash)
331 {
332 	pagevec_init(&stash->pvec);
333 	spin_lock_init(&stash->lock);
334 }
335 
336 static struct page *stash_pop_page(struct pagestash *stash)
337 {
338 	struct page *page = NULL;
339 
340 	spin_lock(&stash->lock);
341 	if (likely(stash->pvec.nr))
342 		page = stash->pvec.pages[--stash->pvec.nr];
343 	spin_unlock(&stash->lock);
344 
345 	return page;
346 }
347 
348 static void stash_push_pagevec(struct pagestash *stash, struct pagevec *pvec)
349 {
350 	unsigned int nr;
351 
352 	spin_lock_nested(&stash->lock, SINGLE_DEPTH_NESTING);
353 
354 	nr = min_t(typeof(nr), pvec->nr, pagevec_space(&stash->pvec));
355 	memcpy(stash->pvec.pages + stash->pvec.nr,
356 	       pvec->pages + pvec->nr - nr,
357 	       sizeof(pvec->pages[0]) * nr);
358 	stash->pvec.nr += nr;
359 
360 	spin_unlock(&stash->lock);
361 
362 	pvec->nr -= nr;
363 }
364 
365 static struct page *vm_alloc_page(struct i915_address_space *vm, gfp_t gfp)
366 {
367 	struct pagevec stack;
368 	struct page *page;
369 
370 	if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
371 		i915_gem_shrink_all(vm->i915);
372 
373 	page = stash_pop_page(&vm->free_pages);
374 	if (page)
375 		return page;
376 
377 	if (!vm->pt_kmap_wc)
378 		return alloc_page(gfp);
379 
380 	/* Look in our global stash of WC pages... */
381 	page = stash_pop_page(&vm->i915->mm.wc_stash);
382 	if (page)
383 		return page;
384 
385 	/*
386 	 * Otherwise batch allocate pages to amortize cost of set_pages_wc.
387 	 *
388 	 * We have to be careful as page allocation may trigger the shrinker
389 	 * (via direct reclaim) which will fill up the WC stash underneath us.
390 	 * So we add our WB pages into a temporary pvec on the stack and merge
391 	 * them into the WC stash after all the allocations are complete.
392 	 */
393 	pagevec_init(&stack);
394 	do {
395 		struct page *page;
396 
397 		page = alloc_page(gfp);
398 		if (unlikely(!page))
399 			break;
400 
401 		stack.pages[stack.nr++] = page;
402 	} while (pagevec_space(&stack));
403 
404 	if (stack.nr && !set_pages_array_wc(stack.pages, stack.nr)) {
405 		page = stack.pages[--stack.nr];
406 
407 		/* Merge spare WC pages to the global stash */
408 		if (stack.nr)
409 			stash_push_pagevec(&vm->i915->mm.wc_stash, &stack);
410 
411 		/* Push any surplus WC pages onto the local VM stash */
412 		if (stack.nr)
413 			stash_push_pagevec(&vm->free_pages, &stack);
414 	}
415 
416 	/* Return unwanted leftovers */
417 	if (unlikely(stack.nr)) {
418 		WARN_ON_ONCE(set_pages_array_wb(stack.pages, stack.nr));
419 		__pagevec_release(&stack);
420 	}
421 
422 	return page;
423 }
424 
425 static void vm_free_pages_release(struct i915_address_space *vm,
426 				  bool immediate)
427 {
428 	struct pagevec *pvec = &vm->free_pages.pvec;
429 	struct pagevec stack;
430 
431 	lockdep_assert_held(&vm->free_pages.lock);
432 	GEM_BUG_ON(!pagevec_count(pvec));
433 
434 	if (vm->pt_kmap_wc) {
435 		/*
436 		 * When we use WC, first fill up the global stash and then
437 		 * only if full immediately free the overflow.
438 		 */
439 		stash_push_pagevec(&vm->i915->mm.wc_stash, pvec);
440 
441 		/*
442 		 * As we have made some room in the VM's free_pages,
443 		 * we can wait for it to fill again. Unless we are
444 		 * inside i915_address_space_fini() and must
445 		 * immediately release the pages!
446 		 */
447 		if (pvec->nr <= (immediate ? 0 : PAGEVEC_SIZE - 1))
448 			return;
449 
450 		/*
451 		 * We have to drop the lock to allow ourselves to sleep,
452 		 * so take a copy of the pvec and clear the stash for
453 		 * others to use it as we sleep.
454 		 */
455 		stack = *pvec;
456 		pagevec_reinit(pvec);
457 		spin_unlock(&vm->free_pages.lock);
458 
459 		pvec = &stack;
460 		set_pages_array_wb(pvec->pages, pvec->nr);
461 
462 		spin_lock(&vm->free_pages.lock);
463 	}
464 
465 	__pagevec_release(pvec);
466 }
467 
468 static void vm_free_page(struct i915_address_space *vm, struct page *page)
469 {
470 	/*
471 	 * On !llc, we need to change the pages back to WB. We only do so
472 	 * in bulk, so we rarely need to change the page attributes here,
473 	 * but doing so requires a stop_machine() from deep inside arch/x86/mm.
474 	 * To make detection of the possible sleep more likely, use an
475 	 * unconditional might_sleep() for everybody.
476 	 */
477 	might_sleep();
478 	spin_lock(&vm->free_pages.lock);
479 	while (!pagevec_space(&vm->free_pages.pvec))
480 		vm_free_pages_release(vm, false);
481 	GEM_BUG_ON(pagevec_count(&vm->free_pages.pvec) >= PAGEVEC_SIZE);
482 	pagevec_add(&vm->free_pages.pvec, page);
483 	spin_unlock(&vm->free_pages.lock);
484 }
485 
486 static void i915_address_space_fini(struct i915_address_space *vm)
487 {
488 	spin_lock(&vm->free_pages.lock);
489 	if (pagevec_count(&vm->free_pages.pvec))
490 		vm_free_pages_release(vm, true);
491 	GEM_BUG_ON(pagevec_count(&vm->free_pages.pvec));
492 	spin_unlock(&vm->free_pages.lock);
493 
494 	drm_mm_takedown(&vm->mm);
495 
496 	mutex_destroy(&vm->mutex);
497 }
498 
499 static void ppgtt_destroy_vma(struct i915_address_space *vm)
500 {
501 	struct list_head *phases[] = {
502 		&vm->bound_list,
503 		&vm->unbound_list,
504 		NULL,
505 	}, **phase;
506 
507 	mutex_lock(&vm->i915->drm.struct_mutex);
508 	for (phase = phases; *phase; phase++) {
509 		struct i915_vma *vma, *vn;
510 
511 		list_for_each_entry_safe(vma, vn, *phase, vm_link)
512 			i915_vma_destroy(vma);
513 	}
514 	mutex_unlock(&vm->i915->drm.struct_mutex);
515 }
516 
517 static void __i915_vm_release(struct work_struct *work)
518 {
519 	struct i915_address_space *vm =
520 		container_of(work, struct i915_address_space, rcu.work);
521 
522 	ppgtt_destroy_vma(vm);
523 
524 	GEM_BUG_ON(!list_empty(&vm->bound_list));
525 	GEM_BUG_ON(!list_empty(&vm->unbound_list));
526 
527 	vm->cleanup(vm);
528 	i915_address_space_fini(vm);
529 
530 	kfree(vm);
531 }
532 
533 void i915_vm_release(struct kref *kref)
534 {
535 	struct i915_address_space *vm =
536 		container_of(kref, struct i915_address_space, ref);
537 
538 	GEM_BUG_ON(i915_is_ggtt(vm));
539 	trace_i915_ppgtt_release(vm);
540 
541 	vm->closed = true;
542 	queue_rcu_work(vm->i915->wq, &vm->rcu);
543 }
544 
545 static void i915_address_space_init(struct i915_address_space *vm, int subclass)
546 {
547 	kref_init(&vm->ref);
548 	INIT_RCU_WORK(&vm->rcu, __i915_vm_release);
549 
550 	/*
551 	 * The vm->mutex must be reclaim safe (for use in the shrinker).
552 	 * Do a dummy acquire now under fs_reclaim so that any allocation
553 	 * attempt holding the lock is immediately reported by lockdep.
554 	 */
555 	mutex_init(&vm->mutex);
556 	lockdep_set_subclass(&vm->mutex, subclass);
557 	i915_gem_shrinker_taints_mutex(vm->i915, &vm->mutex);
558 
559 	GEM_BUG_ON(!vm->total);
560 	drm_mm_init(&vm->mm, 0, vm->total);
561 	vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
562 
563 	stash_init(&vm->free_pages);
564 
565 	INIT_LIST_HEAD(&vm->unbound_list);
566 	INIT_LIST_HEAD(&vm->bound_list);
567 }
568 
569 static int __setup_page_dma(struct i915_address_space *vm,
570 			    struct i915_page_dma *p,
571 			    gfp_t gfp)
572 {
573 	p->page = vm_alloc_page(vm, gfp | I915_GFP_ALLOW_FAIL);
574 	if (unlikely(!p->page))
575 		return -ENOMEM;
576 
577 	p->daddr = dma_map_page_attrs(vm->dma,
578 				      p->page, 0, PAGE_SIZE,
579 				      PCI_DMA_BIDIRECTIONAL,
580 				      DMA_ATTR_SKIP_CPU_SYNC |
581 				      DMA_ATTR_NO_WARN);
582 	if (unlikely(dma_mapping_error(vm->dma, p->daddr))) {
583 		vm_free_page(vm, p->page);
584 		return -ENOMEM;
585 	}
586 
587 	return 0;
588 }
589 
590 static int setup_page_dma(struct i915_address_space *vm,
591 			  struct i915_page_dma *p)
592 {
593 	return __setup_page_dma(vm, p, __GFP_HIGHMEM);
594 }
595 
596 static void cleanup_page_dma(struct i915_address_space *vm,
597 			     struct i915_page_dma *p)
598 {
599 	dma_unmap_page(vm->dma, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
600 	vm_free_page(vm, p->page);
601 }
602 
603 #define kmap_atomic_px(px) kmap_atomic(px_base(px)->page)
604 
605 static void
606 fill_page_dma(const struct i915_page_dma *p, const u64 val, unsigned int count)
607 {
608 	kunmap_atomic(memset64(kmap_atomic(p->page), val, count));
609 }
610 
611 #define fill_px(px, v) fill_page_dma(px_base(px), (v), PAGE_SIZE / sizeof(u64))
612 #define fill32_px(px, v) do {						\
613 	u64 v__ = lower_32_bits(v);					\
614 	fill_px((px), v__ << 32 | v__);					\
615 } while (0)
616 
617 static int
618 setup_scratch_page(struct i915_address_space *vm, gfp_t gfp)
619 {
620 	unsigned long size;
621 
622 	/*
623 	 * In order to utilize 64K pages for an object with a size < 2M, we will
624 	 * need to support a 64K scratch page, given that every 16th entry for a
625 	 * page-table operating in 64K mode must point to a properly aligned 64K
626 	 * region, including any PTEs which happen to point to scratch.
627 	 *
628 	 * This is only relevant for the 48b PPGTT where we support
629 	 * huge-gtt-pages, see also i915_vma_insert(). However, as we share the
630 	 * scratch (read-only) between all vm, we create one 64k scratch page
631 	 * for all.
632 	 */
633 	size = I915_GTT_PAGE_SIZE_4K;
634 	if (i915_vm_is_4lvl(vm) &&
635 	    HAS_PAGE_SIZES(vm->i915, I915_GTT_PAGE_SIZE_64K)) {
636 		size = I915_GTT_PAGE_SIZE_64K;
637 		gfp |= __GFP_NOWARN;
638 	}
639 	gfp |= __GFP_ZERO | __GFP_RETRY_MAYFAIL;
640 
641 	do {
642 		unsigned int order = get_order(size);
643 		struct page *page;
644 		dma_addr_t addr;
645 
646 		page = alloc_pages(gfp, order);
647 		if (unlikely(!page))
648 			goto skip;
649 
650 		addr = dma_map_page_attrs(vm->dma,
651 					  page, 0, size,
652 					  PCI_DMA_BIDIRECTIONAL,
653 					  DMA_ATTR_SKIP_CPU_SYNC |
654 					  DMA_ATTR_NO_WARN);
655 		if (unlikely(dma_mapping_error(vm->dma, addr)))
656 			goto free_page;
657 
658 		if (unlikely(!IS_ALIGNED(addr, size)))
659 			goto unmap_page;
660 
661 		vm->scratch[0].base.page = page;
662 		vm->scratch[0].base.daddr = addr;
663 		vm->scratch_order = order;
664 		return 0;
665 
666 unmap_page:
667 		dma_unmap_page(vm->dma, addr, size, PCI_DMA_BIDIRECTIONAL);
668 free_page:
669 		__free_pages(page, order);
670 skip:
671 		if (size == I915_GTT_PAGE_SIZE_4K)
672 			return -ENOMEM;
673 
674 		size = I915_GTT_PAGE_SIZE_4K;
675 		gfp &= ~__GFP_NOWARN;
676 	} while (1);
677 }
678 
679 static void cleanup_scratch_page(struct i915_address_space *vm)
680 {
681 	struct i915_page_dma *p = px_base(&vm->scratch[0]);
682 	unsigned int order = vm->scratch_order;
683 
684 	dma_unmap_page(vm->dma, p->daddr, BIT(order) << PAGE_SHIFT,
685 		       PCI_DMA_BIDIRECTIONAL);
686 	__free_pages(p->page, order);
687 }
688 
689 static void free_scratch(struct i915_address_space *vm)
690 {
691 	int i;
692 
693 	if (!px_dma(&vm->scratch[0])) /* set to 0 on clones */
694 		return;
695 
696 	for (i = 1; i <= vm->top; i++) {
697 		if (!px_dma(&vm->scratch[i]))
698 			break;
699 		cleanup_page_dma(vm, px_base(&vm->scratch[i]));
700 	}
701 
702 	cleanup_scratch_page(vm);
703 }
704 
705 static struct i915_page_table *alloc_pt(struct i915_address_space *vm)
706 {
707 	struct i915_page_table *pt;
708 
709 	pt = kmalloc(sizeof(*pt), I915_GFP_ALLOW_FAIL);
710 	if (unlikely(!pt))
711 		return ERR_PTR(-ENOMEM);
712 
713 	if (unlikely(setup_page_dma(vm, &pt->base))) {
714 		kfree(pt);
715 		return ERR_PTR(-ENOMEM);
716 	}
717 
718 	atomic_set(&pt->used, 0);
719 	return pt;
720 }
721 
722 static struct i915_page_directory *__alloc_pd(size_t sz)
723 {
724 	struct i915_page_directory *pd;
725 
726 	pd = kzalloc(sz, I915_GFP_ALLOW_FAIL);
727 	if (unlikely(!pd))
728 		return NULL;
729 
730 	spin_lock_init(&pd->lock);
731 	return pd;
732 }
733 
734 static struct i915_page_directory *alloc_pd(struct i915_address_space *vm)
735 {
736 	struct i915_page_directory *pd;
737 
738 	pd = __alloc_pd(sizeof(*pd));
739 	if (unlikely(!pd))
740 		return ERR_PTR(-ENOMEM);
741 
742 	if (unlikely(setup_page_dma(vm, px_base(pd)))) {
743 		kfree(pd);
744 		return ERR_PTR(-ENOMEM);
745 	}
746 
747 	return pd;
748 }
749 
750 static void free_pd(struct i915_address_space *vm, struct i915_page_dma *pd)
751 {
752 	cleanup_page_dma(vm, pd);
753 	kfree(pd);
754 }
755 
756 #define free_px(vm, px) free_pd(vm, px_base(px))
757 
758 static inline void
759 write_dma_entry(struct i915_page_dma * const pdma,
760 		const unsigned short idx,
761 		const u64 encoded_entry)
762 {
763 	u64 * const vaddr = kmap_atomic(pdma->page);
764 
765 	vaddr[idx] = encoded_entry;
766 	kunmap_atomic(vaddr);
767 }
768 
769 static inline void
770 __set_pd_entry(struct i915_page_directory * const pd,
771 	       const unsigned short idx,
772 	       struct i915_page_dma * const to,
773 	       u64 (*encode)(const dma_addr_t, const enum i915_cache_level))
774 {
775 	GEM_BUG_ON(atomic_read(px_used(pd)) > ARRAY_SIZE(pd->entry));
776 
777 	atomic_inc(px_used(pd));
778 	pd->entry[idx] = to;
779 	write_dma_entry(px_base(pd), idx, encode(to->daddr, I915_CACHE_LLC));
780 }
781 
782 #define set_pd_entry(pd, idx, to) \
783 	__set_pd_entry((pd), (idx), px_base(to), gen8_pde_encode)
784 
785 static inline void
786 clear_pd_entry(struct i915_page_directory * const pd,
787 	       const unsigned short idx,
788 	       const struct i915_page_scratch * const scratch)
789 {
790 	GEM_BUG_ON(atomic_read(px_used(pd)) == 0);
791 
792 	write_dma_entry(px_base(pd), idx, scratch->encode);
793 	pd->entry[idx] = NULL;
794 	atomic_dec(px_used(pd));
795 }
796 
797 static bool
798 release_pd_entry(struct i915_page_directory * const pd,
799 		 const unsigned short idx,
800 		 struct i915_page_table * const pt,
801 		 const struct i915_page_scratch * const scratch)
802 {
803 	bool free = false;
804 
805 	if (atomic_add_unless(&pt->used, -1, 1))
806 		return false;
807 
808 	spin_lock(&pd->lock);
809 	if (atomic_dec_and_test(&pt->used)) {
810 		clear_pd_entry(pd, idx, scratch);
811 		free = true;
812 	}
813 	spin_unlock(&pd->lock);
814 
815 	return free;
816 }
817 
818 /*
819  * PDE TLBs are a pain to invalidate on GEN8+. When we modify
820  * the page table structures, we mark them dirty so that
821  * context switching/execlist queuing code takes extra steps
822  * to ensure that tlbs are flushed.
823  */
824 static void mark_tlbs_dirty(struct i915_ppgtt *ppgtt)
825 {
826 	ppgtt->pd_dirty_engines = ALL_ENGINES;
827 }
828 
829 static int gen8_ppgtt_notify_vgt(struct i915_ppgtt *ppgtt, bool create)
830 {
831 	struct i915_address_space *vm = &ppgtt->vm;
832 	struct drm_i915_private *dev_priv = vm->i915;
833 	enum vgt_g2v_type msg;
834 	int i;
835 
836 	if (create)
837 		atomic_inc(px_used(ppgtt->pd)); /* never remove */
838 	else
839 		atomic_dec(px_used(ppgtt->pd));
840 
841 	if (i915_vm_is_4lvl(vm)) {
842 		const u64 daddr = px_dma(ppgtt->pd);
843 
844 		I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
845 		I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
846 
847 		msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
848 				VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
849 	} else {
850 		for (i = 0; i < GEN8_3LVL_PDPES; i++) {
851 			const u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
852 
853 			I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
854 			I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
855 		}
856 
857 		msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
858 				VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
859 	}
860 
861 	I915_WRITE(vgtif_reg(g2v_notify), msg);
862 
863 	return 0;
864 }
865 
866 /* Index shifts into the pagetable are offset by GEN8_PTE_SHIFT [12] */
867 #define GEN8_PAGE_SIZE (SZ_4K) /* page and page-directory sizes are the same */
868 #define GEN8_PTE_SHIFT (ilog2(GEN8_PAGE_SIZE))
869 #define GEN8_PDES (GEN8_PAGE_SIZE / sizeof(u64))
870 #define gen8_pd_shift(lvl) ((lvl) * ilog2(GEN8_PDES))
871 #define gen8_pd_index(i, lvl) i915_pde_index((i), gen8_pd_shift(lvl))
872 #define __gen8_pte_shift(lvl) (GEN8_PTE_SHIFT + gen8_pd_shift(lvl))
873 #define __gen8_pte_index(a, lvl) i915_pde_index((a), __gen8_pte_shift(lvl))
874 
875 static inline unsigned int
876 gen8_pd_range(u64 start, u64 end, int lvl, unsigned int *idx)
877 {
878 	const int shift = gen8_pd_shift(lvl);
879 	const u64 mask = ~0ull << gen8_pd_shift(lvl + 1);
880 
881 	GEM_BUG_ON(start >= end);
882 	end += ~mask >> gen8_pd_shift(1);
883 
884 	*idx = i915_pde_index(start, shift);
885 	if ((start ^ end) & mask)
886 		return GEN8_PDES - *idx;
887 	else
888 		return i915_pde_index(end, shift) - *idx;
889 }
890 
891 static inline bool gen8_pd_contains(u64 start, u64 end, int lvl)
892 {
893 	const u64 mask = ~0ull << gen8_pd_shift(lvl + 1);
894 
895 	GEM_BUG_ON(start >= end);
896 	return (start ^ end) & mask && (start & ~mask) == 0;
897 }
898 
899 static inline unsigned int gen8_pt_count(u64 start, u64 end)
900 {
901 	GEM_BUG_ON(start >= end);
902 	if ((start ^ end) >> gen8_pd_shift(1))
903 		return GEN8_PDES - (start & (GEN8_PDES - 1));
904 	else
905 		return end - start;
906 }
907 
908 static inline unsigned int gen8_pd_top_count(const struct i915_address_space *vm)
909 {
910 	unsigned int shift = __gen8_pte_shift(vm->top);
911 	return (vm->total + (1ull << shift) - 1) >> shift;
912 }
913 
914 static void __gen8_ppgtt_cleanup(struct i915_address_space *vm,
915 				 struct i915_page_directory *pd,
916 				 int count, int lvl)
917 {
918 	if (lvl) {
919 		void **pde = pd->entry;
920 
921 		do {
922 			if (!*pde)
923 				continue;
924 
925 			__gen8_ppgtt_cleanup(vm, *pde, GEN8_PDES, lvl - 1);
926 		} while (pde++, --count);
927 	}
928 
929 	free_px(vm, pd);
930 }
931 
932 static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
933 {
934 	struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
935 
936 	if (intel_vgpu_active(vm->i915))
937 		gen8_ppgtt_notify_vgt(ppgtt, false);
938 
939 	__gen8_ppgtt_cleanup(vm, ppgtt->pd, gen8_pd_top_count(vm), vm->top);
940 	free_scratch(vm);
941 }
942 
943 static u64 __gen8_ppgtt_clear(struct i915_address_space * const vm,
944 			      struct i915_page_directory * const pd,
945 			      u64 start, const u64 end, int lvl)
946 {
947 	const struct i915_page_scratch * const scratch = &vm->scratch[lvl];
948 	unsigned int idx, len;
949 
950 	len = gen8_pd_range(start, end, lvl--, &idx);
951 	DBG("%s(%p):{ lvl:%d, start:%llx, end:%llx, idx:%d, len:%d, used:%d}\n",
952 	    __func__, vm, lvl + 1, start, end,
953 	    idx, len, atomic_read(px_used(pd)));
954 	GEM_BUG_ON(!len || len >= atomic_read(px_used(pd)));
955 
956 	do {
957 		struct i915_page_table *pt = pd->entry[idx];
958 
959 		if (atomic_fetch_inc(&pt->used) >> gen8_pd_shift(1) &&
960 		    gen8_pd_contains(start, end, lvl)) {
961 			DBG("%s(%p):{ lvl:%d, idx:%d, start:%llx, end:%llx } removing pd\n",
962 			    __func__, vm, lvl + 1, idx, start, end);
963 			clear_pd_entry(pd, idx, scratch);
964 			__gen8_ppgtt_cleanup(vm, as_pd(pt), I915_PDES, lvl);
965 			start += (u64)I915_PDES << gen8_pd_shift(lvl);
966 			continue;
967 		}
968 
969 		if (lvl) {
970 			start = __gen8_ppgtt_clear(vm, as_pd(pt),
971 						   start, end, lvl);
972 		} else {
973 			unsigned int count;
974 			u64 *vaddr;
975 
976 			count = gen8_pt_count(start, end);
977 			DBG("%s(%p):{ lvl:%d, start:%llx, end:%llx, idx:%d, len:%d, used:%d} removing pte\n",
978 			    __func__, vm, lvl, start, end,
979 			    gen8_pd_index(start, 0), count,
980 			    atomic_read(&pt->used));
981 			GEM_BUG_ON(!count || count >= atomic_read(&pt->used));
982 
983 			vaddr = kmap_atomic_px(pt);
984 			memset64(vaddr + gen8_pd_index(start, 0),
985 				 vm->scratch[0].encode,
986 				 count);
987 			kunmap_atomic(vaddr);
988 
989 			atomic_sub(count, &pt->used);
990 			start += count;
991 		}
992 
993 		if (release_pd_entry(pd, idx, pt, scratch))
994 			free_px(vm, pt);
995 	} while (idx++, --len);
996 
997 	return start;
998 }
999 
1000 static void gen8_ppgtt_clear(struct i915_address_space *vm,
1001 			     u64 start, u64 length)
1002 {
1003 	GEM_BUG_ON(!IS_ALIGNED(start, BIT_ULL(GEN8_PTE_SHIFT)));
1004 	GEM_BUG_ON(!IS_ALIGNED(length, BIT_ULL(GEN8_PTE_SHIFT)));
1005 
1006 	start >>= GEN8_PTE_SHIFT;
1007 	length >>= GEN8_PTE_SHIFT;
1008 	GEM_BUG_ON(length == 0);
1009 
1010 	__gen8_ppgtt_clear(vm, i915_vm_to_ppgtt(vm)->pd,
1011 			   start, start + length, vm->top);
1012 }
1013 
1014 static int __gen8_ppgtt_alloc(struct i915_address_space * const vm,
1015 			      struct i915_page_directory * const pd,
1016 			      u64 * const start, u64 end, int lvl)
1017 {
1018 	const struct i915_page_scratch * const scratch = &vm->scratch[lvl];
1019 	struct i915_page_table *alloc = NULL;
1020 	unsigned int idx, len;
1021 	int ret = 0;
1022 
1023 	len = gen8_pd_range(*start, end, lvl--, &idx);
1024 	DBG("%s(%p):{lvl:%d, start:%llx, end:%llx, idx:%d, len:%d, used:%d}\n",
1025 	    __func__, vm, lvl + 1, *start, end,
1026 	    idx, len, atomic_read(px_used(pd)));
1027 	GEM_BUG_ON(!len || (idx + len - 1) >> gen8_pd_shift(1));
1028 
1029 	spin_lock(&pd->lock);
1030 	GEM_BUG_ON(!atomic_read(px_used(pd))); /* Must be pinned! */
1031 	do {
1032 		struct i915_page_table *pt = pd->entry[idx];
1033 
1034 		if (!pt) {
1035 			spin_unlock(&pd->lock);
1036 
1037 			DBG("%s(%p):{ lvl:%d, idx:%d } allocating new tree\n",
1038 			    __func__, vm, lvl + 1, idx);
1039 
1040 			pt = fetch_and_zero(&alloc);
1041 			if (lvl) {
1042 				if (!pt) {
1043 					pt = &alloc_pd(vm)->pt;
1044 					if (IS_ERR(pt)) {
1045 						ret = PTR_ERR(pt);
1046 						goto out;
1047 					}
1048 				}
1049 
1050 				fill_px(pt, vm->scratch[lvl].encode);
1051 			} else {
1052 				if (!pt) {
1053 					pt = alloc_pt(vm);
1054 					if (IS_ERR(pt)) {
1055 						ret = PTR_ERR(pt);
1056 						goto out;
1057 					}
1058 				}
1059 
1060 				if (intel_vgpu_active(vm->i915) ||
1061 				    gen8_pt_count(*start, end) < I915_PDES)
1062 					fill_px(pt, vm->scratch[lvl].encode);
1063 			}
1064 
1065 			spin_lock(&pd->lock);
1066 			if (likely(!pd->entry[idx]))
1067 				set_pd_entry(pd, idx, pt);
1068 			else
1069 				alloc = pt, pt = pd->entry[idx];
1070 		}
1071 
1072 		if (lvl) {
1073 			atomic_inc(&pt->used);
1074 			spin_unlock(&pd->lock);
1075 
1076 			ret = __gen8_ppgtt_alloc(vm, as_pd(pt),
1077 						 start, end, lvl);
1078 			if (unlikely(ret)) {
1079 				if (release_pd_entry(pd, idx, pt, scratch))
1080 					free_px(vm, pt);
1081 				goto out;
1082 			}
1083 
1084 			spin_lock(&pd->lock);
1085 			atomic_dec(&pt->used);
1086 			GEM_BUG_ON(!atomic_read(&pt->used));
1087 		} else {
1088 			unsigned int count = gen8_pt_count(*start, end);
1089 
1090 			DBG("%s(%p):{lvl:%d, start:%llx, end:%llx, idx:%d, len:%d, used:%d} inserting pte\n",
1091 			    __func__, vm, lvl, *start, end,
1092 			    gen8_pd_index(*start, 0), count,
1093 			    atomic_read(&pt->used));
1094 
1095 			atomic_add(count, &pt->used);
1096 			GEM_BUG_ON(atomic_read(&pt->used) > I915_PDES);
1097 			*start += count;
1098 		}
1099 	} while (idx++, --len);
1100 	spin_unlock(&pd->lock);
1101 out:
1102 	if (alloc)
1103 		free_px(vm, alloc);
1104 	return ret;
1105 }
1106 
1107 static int gen8_ppgtt_alloc(struct i915_address_space *vm,
1108 			    u64 start, u64 length)
1109 {
1110 	u64 from;
1111 	int err;
1112 
1113 	GEM_BUG_ON(!IS_ALIGNED(start, BIT_ULL(GEN8_PTE_SHIFT)));
1114 	GEM_BUG_ON(!IS_ALIGNED(length, BIT_ULL(GEN8_PTE_SHIFT)));
1115 
1116 	start >>= GEN8_PTE_SHIFT;
1117 	length >>= GEN8_PTE_SHIFT;
1118 	GEM_BUG_ON(length == 0);
1119 	from = start;
1120 
1121 	err = __gen8_ppgtt_alloc(vm, i915_vm_to_ppgtt(vm)->pd,
1122 				 &start, start + length, vm->top);
1123 	if (unlikely(err && from != start))
1124 		__gen8_ppgtt_clear(vm, i915_vm_to_ppgtt(vm)->pd,
1125 				   from, start, vm->top);
1126 
1127 	return err;
1128 }
1129 
1130 static inline struct sgt_dma {
1131 	struct scatterlist *sg;
1132 	dma_addr_t dma, max;
1133 } sgt_dma(struct i915_vma *vma) {
1134 	struct scatterlist *sg = vma->pages->sgl;
1135 	dma_addr_t addr = sg_dma_address(sg);
1136 	return (struct sgt_dma) { sg, addr, addr + sg->length };
1137 }
1138 
1139 static __always_inline u64
1140 gen8_ppgtt_insert_pte_entries(struct i915_ppgtt *ppgtt,
1141 			      struct i915_page_directory *pdp,
1142 			      struct sgt_dma *iter,
1143 			      u64 idx,
1144 			      enum i915_cache_level cache_level,
1145 			      u32 flags)
1146 {
1147 	struct i915_page_directory *pd;
1148 	const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level, flags);
1149 	gen8_pte_t *vaddr;
1150 
1151 	pd = i915_pd_entry(pdp, gen8_pd_index(idx, 2));
1152 	vaddr = kmap_atomic_px(i915_pt_entry(pd, gen8_pd_index(idx, 1)));
1153 	do {
1154 		vaddr[gen8_pd_index(idx, 0)] = pte_encode | iter->dma;
1155 
1156 		iter->dma += I915_GTT_PAGE_SIZE;
1157 		if (iter->dma >= iter->max) {
1158 			iter->sg = __sg_next(iter->sg);
1159 			if (!iter->sg) {
1160 				idx = 0;
1161 				break;
1162 			}
1163 
1164 			iter->dma = sg_dma_address(iter->sg);
1165 			iter->max = iter->dma + iter->sg->length;
1166 		}
1167 
1168 		if (gen8_pd_index(++idx, 0) == 0) {
1169 			if (gen8_pd_index(idx, 1) == 0) {
1170 				/* Limited by sg length for 3lvl */
1171 				if (gen8_pd_index(idx, 2) == 0)
1172 					break;
1173 
1174 				pd = pdp->entry[gen8_pd_index(idx, 2)];
1175 			}
1176 
1177 			kunmap_atomic(vaddr);
1178 			vaddr = kmap_atomic_px(i915_pt_entry(pd, gen8_pd_index(idx, 1)));
1179 		}
1180 	} while (1);
1181 	kunmap_atomic(vaddr);
1182 
1183 	return idx;
1184 }
1185 
1186 static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
1187 				   struct i915_vma *vma,
1188 				   enum i915_cache_level cache_level,
1189 				   u32 flags)
1190 {
1191 	struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1192 	struct sgt_dma iter = sgt_dma(vma);
1193 
1194 	gen8_ppgtt_insert_pte_entries(ppgtt, ppgtt->pd, &iter,
1195 				      vma->node.start >> GEN8_PTE_SHIFT,
1196 				      cache_level, flags);
1197 
1198 	vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
1199 }
1200 
1201 static void gen8_ppgtt_insert_huge_entries(struct i915_vma *vma,
1202 					   struct i915_page_directory *pml4,
1203 					   struct sgt_dma *iter,
1204 					   enum i915_cache_level cache_level,
1205 					   u32 flags)
1206 {
1207 	const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level, flags);
1208 	u64 start = vma->node.start;
1209 	dma_addr_t rem = iter->sg->length;
1210 
1211 	do {
1212 		struct i915_page_directory *pdp =
1213 			i915_pd_entry(pml4, __gen8_pte_index(start, 3));
1214 		struct i915_page_directory *pd =
1215 			i915_pd_entry(pdp, __gen8_pte_index(start, 2));
1216 		gen8_pte_t encode = pte_encode;
1217 		unsigned int maybe_64K = -1;
1218 		unsigned int page_size;
1219 		gen8_pte_t *vaddr;
1220 		u16 index;
1221 
1222 		if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_2M &&
1223 		    IS_ALIGNED(iter->dma, I915_GTT_PAGE_SIZE_2M) &&
1224 		    rem >= I915_GTT_PAGE_SIZE_2M &&
1225 		    !__gen8_pte_index(start, 0)) {
1226 			index = __gen8_pte_index(start, 1);
1227 			encode |= GEN8_PDE_PS_2M;
1228 			page_size = I915_GTT_PAGE_SIZE_2M;
1229 
1230 			vaddr = kmap_atomic_px(pd);
1231 		} else {
1232 			struct i915_page_table *pt =
1233 				i915_pt_entry(pd, __gen8_pte_index(start, 1));
1234 
1235 			index = __gen8_pte_index(start, 0);
1236 			page_size = I915_GTT_PAGE_SIZE;
1237 
1238 			if (!index &&
1239 			    vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K &&
1240 			    IS_ALIGNED(iter->dma, I915_GTT_PAGE_SIZE_64K) &&
1241 			    (IS_ALIGNED(rem, I915_GTT_PAGE_SIZE_64K) ||
1242 			     rem >= (I915_PDES - index) * I915_GTT_PAGE_SIZE))
1243 				maybe_64K = __gen8_pte_index(start, 1);
1244 
1245 			vaddr = kmap_atomic_px(pt);
1246 		}
1247 
1248 		do {
1249 			GEM_BUG_ON(iter->sg->length < page_size);
1250 			vaddr[index++] = encode | iter->dma;
1251 
1252 			start += page_size;
1253 			iter->dma += page_size;
1254 			rem -= page_size;
1255 			if (iter->dma >= iter->max) {
1256 				iter->sg = __sg_next(iter->sg);
1257 				if (!iter->sg)
1258 					break;
1259 
1260 				rem = iter->sg->length;
1261 				iter->dma = sg_dma_address(iter->sg);
1262 				iter->max = iter->dma + rem;
1263 
1264 				if (maybe_64K != -1 && index < I915_PDES &&
1265 				    !(IS_ALIGNED(iter->dma, I915_GTT_PAGE_SIZE_64K) &&
1266 				      (IS_ALIGNED(rem, I915_GTT_PAGE_SIZE_64K) ||
1267 				       rem >= (I915_PDES - index) * I915_GTT_PAGE_SIZE)))
1268 					maybe_64K = -1;
1269 
1270 				if (unlikely(!IS_ALIGNED(iter->dma, page_size)))
1271 					break;
1272 			}
1273 		} while (rem >= page_size && index < I915_PDES);
1274 
1275 		kunmap_atomic(vaddr);
1276 
1277 		/*
1278 		 * Is it safe to mark the 2M block as 64K? -- Either we have
1279 		 * filled whole page-table with 64K entries, or filled part of
1280 		 * it and have reached the end of the sg table and we have
1281 		 * enough padding.
1282 		 */
1283 		if (maybe_64K != -1 &&
1284 		    (index == I915_PDES ||
1285 		     (i915_vm_has_scratch_64K(vma->vm) &&
1286 		      !iter->sg && IS_ALIGNED(vma->node.start +
1287 					      vma->node.size,
1288 					      I915_GTT_PAGE_SIZE_2M)))) {
1289 			vaddr = kmap_atomic_px(pd);
1290 			vaddr[maybe_64K] |= GEN8_PDE_IPS_64K;
1291 			kunmap_atomic(vaddr);
1292 			page_size = I915_GTT_PAGE_SIZE_64K;
1293 
1294 			/*
1295 			 * We write all 4K page entries, even when using 64K
1296 			 * pages. In order to verify that the HW isn't cheating
1297 			 * by using the 4K PTE instead of the 64K PTE, we want
1298 			 * to remove all the surplus entries. If the HW skipped
1299 			 * the 64K PTE, it will read/write into the scratch page
1300 			 * instead - which we detect as missing results during
1301 			 * selftests.
1302 			 */
1303 			if (I915_SELFTEST_ONLY(vma->vm->scrub_64K)) {
1304 				u16 i;
1305 
1306 				encode = vma->vm->scratch[0].encode;
1307 				vaddr = kmap_atomic_px(i915_pt_entry(pd, maybe_64K));
1308 
1309 				for (i = 1; i < index; i += 16)
1310 					memset64(vaddr + i, encode, 15);
1311 
1312 				kunmap_atomic(vaddr);
1313 			}
1314 		}
1315 
1316 		vma->page_sizes.gtt |= page_size;
1317 	} while (iter->sg);
1318 }
1319 
1320 static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
1321 				   struct i915_vma *vma,
1322 				   enum i915_cache_level cache_level,
1323 				   u32 flags)
1324 {
1325 	struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1326 	struct sgt_dma iter = sgt_dma(vma);
1327 	struct i915_page_directory * const pml4 = ppgtt->pd;
1328 
1329 	if (vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
1330 		gen8_ppgtt_insert_huge_entries(vma, pml4, &iter, cache_level,
1331 					       flags);
1332 	} else {
1333 		u64 idx = vma->node.start >> GEN8_PTE_SHIFT;
1334 
1335 		while ((idx = gen8_ppgtt_insert_pte_entries(ppgtt,
1336 							    i915_pd_entry(pml4, gen8_pd_index(idx, 3)),
1337 							    &iter, idx, cache_level,
1338 							    flags)))
1339 			;
1340 
1341 		vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
1342 	}
1343 }
1344 
1345 static int gen8_init_scratch(struct i915_address_space *vm)
1346 {
1347 	int ret;
1348 	int i;
1349 
1350 	/*
1351 	 * If everybody agrees to not to write into the scratch page,
1352 	 * we can reuse it for all vm, keeping contexts and processes separate.
1353 	 */
1354 	if (vm->has_read_only &&
1355 	    vm->i915->kernel_context &&
1356 	    vm->i915->kernel_context->vm) {
1357 		struct i915_address_space *clone = vm->i915->kernel_context->vm;
1358 
1359 		GEM_BUG_ON(!clone->has_read_only);
1360 
1361 		vm->scratch_order = clone->scratch_order;
1362 		memcpy(vm->scratch, clone->scratch, sizeof(vm->scratch));
1363 		px_dma(&vm->scratch[0]) = 0; /* no xfer of ownership */
1364 		return 0;
1365 	}
1366 
1367 	ret = setup_scratch_page(vm, __GFP_HIGHMEM);
1368 	if (ret)
1369 		return ret;
1370 
1371 	vm->scratch[0].encode =
1372 		gen8_pte_encode(px_dma(&vm->scratch[0]),
1373 				I915_CACHE_LLC, vm->has_read_only);
1374 
1375 	for (i = 1; i <= vm->top; i++) {
1376 		if (unlikely(setup_page_dma(vm, px_base(&vm->scratch[i]))))
1377 			goto free_scratch;
1378 
1379 		fill_px(&vm->scratch[i], vm->scratch[i - 1].encode);
1380 		vm->scratch[i].encode =
1381 			gen8_pde_encode(px_dma(&vm->scratch[i]),
1382 					I915_CACHE_LLC);
1383 	}
1384 
1385 	return 0;
1386 
1387 free_scratch:
1388 	free_scratch(vm);
1389 	return -ENOMEM;
1390 }
1391 
1392 static int gen8_preallocate_top_level_pdp(struct i915_ppgtt *ppgtt)
1393 {
1394 	struct i915_address_space *vm = &ppgtt->vm;
1395 	struct i915_page_directory *pd = ppgtt->pd;
1396 	unsigned int idx;
1397 
1398 	GEM_BUG_ON(vm->top != 2);
1399 	GEM_BUG_ON(gen8_pd_top_count(vm) != GEN8_3LVL_PDPES);
1400 
1401 	for (idx = 0; idx < GEN8_3LVL_PDPES; idx++) {
1402 		struct i915_page_directory *pde;
1403 
1404 		pde = alloc_pd(vm);
1405 		if (IS_ERR(pde))
1406 			return PTR_ERR(pde);
1407 
1408 		fill_px(pde, vm->scratch[1].encode);
1409 		set_pd_entry(pd, idx, pde);
1410 		atomic_inc(px_used(pde)); /* keep pinned */
1411 	}
1412 
1413 	return 0;
1414 }
1415 
1416 static void ppgtt_init(struct i915_ppgtt *ppgtt, struct intel_gt *gt)
1417 {
1418 	struct drm_i915_private *i915 = gt->i915;
1419 
1420 	ppgtt->vm.gt = gt;
1421 	ppgtt->vm.i915 = i915;
1422 	ppgtt->vm.dma = &i915->drm.pdev->dev;
1423 	ppgtt->vm.total = BIT_ULL(INTEL_INFO(i915)->ppgtt_size);
1424 
1425 	i915_address_space_init(&ppgtt->vm, VM_CLASS_PPGTT);
1426 
1427 	ppgtt->vm.vma_ops.bind_vma    = ppgtt_bind_vma;
1428 	ppgtt->vm.vma_ops.unbind_vma  = ppgtt_unbind_vma;
1429 	ppgtt->vm.vma_ops.set_pages   = ppgtt_set_pages;
1430 	ppgtt->vm.vma_ops.clear_pages = clear_pages;
1431 }
1432 
1433 static struct i915_page_directory *
1434 gen8_alloc_top_pd(struct i915_address_space *vm)
1435 {
1436 	const unsigned int count = gen8_pd_top_count(vm);
1437 	struct i915_page_directory *pd;
1438 
1439 	GEM_BUG_ON(count > ARRAY_SIZE(pd->entry));
1440 
1441 	pd = __alloc_pd(offsetof(typeof(*pd), entry[count]));
1442 	if (unlikely(!pd))
1443 		return ERR_PTR(-ENOMEM);
1444 
1445 	if (unlikely(setup_page_dma(vm, px_base(pd)))) {
1446 		kfree(pd);
1447 		return ERR_PTR(-ENOMEM);
1448 	}
1449 
1450 	fill_page_dma(px_base(pd), vm->scratch[vm->top].encode, count);
1451 	atomic_inc(px_used(pd)); /* mark as pinned */
1452 	return pd;
1453 }
1454 
1455 /*
1456  * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1457  * with a net effect resembling a 2-level page table in normal x86 terms. Each
1458  * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1459  * space.
1460  *
1461  */
1462 static struct i915_ppgtt *gen8_ppgtt_create(struct drm_i915_private *i915)
1463 {
1464 	struct i915_ppgtt *ppgtt;
1465 	int err;
1466 
1467 	ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1468 	if (!ppgtt)
1469 		return ERR_PTR(-ENOMEM);
1470 
1471 	ppgtt_init(ppgtt, &i915->gt);
1472 	ppgtt->vm.top = i915_vm_is_4lvl(&ppgtt->vm) ? 3 : 2;
1473 
1474 	/*
1475 	 * From bdw, there is hw support for read-only pages in the PPGTT.
1476 	 *
1477 	 * Gen11 has HSDES#:1807136187 unresolved. Disable ro support
1478 	 * for now.
1479 	 */
1480 	ppgtt->vm.has_read_only = INTEL_GEN(i915) != 11;
1481 
1482 	/* There are only few exceptions for gen >=6. chv and bxt.
1483 	 * And we are not sure about the latter so play safe for now.
1484 	 */
1485 	if (IS_CHERRYVIEW(i915) || IS_BROXTON(i915))
1486 		ppgtt->vm.pt_kmap_wc = true;
1487 
1488 	err = gen8_init_scratch(&ppgtt->vm);
1489 	if (err)
1490 		goto err_free;
1491 
1492 	ppgtt->pd = gen8_alloc_top_pd(&ppgtt->vm);
1493 	if (IS_ERR(ppgtt->pd)) {
1494 		err = PTR_ERR(ppgtt->pd);
1495 		goto err_free_scratch;
1496 	}
1497 
1498 	if (i915_vm_is_4lvl(&ppgtt->vm)) {
1499 		ppgtt->vm.insert_entries = gen8_ppgtt_insert_4lvl;
1500 	} else {
1501 		if (intel_vgpu_active(i915)) {
1502 			err = gen8_preallocate_top_level_pdp(ppgtt);
1503 			if (err)
1504 				goto err_free_pd;
1505 		}
1506 
1507 		ppgtt->vm.insert_entries = gen8_ppgtt_insert_3lvl;
1508 	}
1509 
1510 	ppgtt->vm.allocate_va_range = gen8_ppgtt_alloc;
1511 	ppgtt->vm.clear_range = gen8_ppgtt_clear;
1512 
1513 	if (intel_vgpu_active(i915))
1514 		gen8_ppgtt_notify_vgt(ppgtt, true);
1515 
1516 	ppgtt->vm.cleanup = gen8_ppgtt_cleanup;
1517 
1518 	return ppgtt;
1519 
1520 err_free_pd:
1521 	__gen8_ppgtt_cleanup(&ppgtt->vm, ppgtt->pd,
1522 			     gen8_pd_top_count(&ppgtt->vm), ppgtt->vm.top);
1523 err_free_scratch:
1524 	free_scratch(&ppgtt->vm);
1525 err_free:
1526 	kfree(ppgtt);
1527 	return ERR_PTR(err);
1528 }
1529 
1530 /* Write pde (index) from the page directory @pd to the page table @pt */
1531 static inline void gen6_write_pde(const struct gen6_ppgtt *ppgtt,
1532 				  const unsigned int pde,
1533 				  const struct i915_page_table *pt)
1534 {
1535 	/* Caller needs to make sure the write completes if necessary */
1536 	iowrite32(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
1537 		  ppgtt->pd_addr + pde);
1538 }
1539 
1540 static void gen7_ppgtt_enable(struct intel_gt *gt)
1541 {
1542 	struct drm_i915_private *i915 = gt->i915;
1543 	struct intel_uncore *uncore = gt->uncore;
1544 	struct intel_engine_cs *engine;
1545 	enum intel_engine_id id;
1546 	u32 ecochk;
1547 
1548 	intel_uncore_rmw(uncore, GAC_ECO_BITS, 0, ECOBITS_PPGTT_CACHE64B);
1549 
1550 	ecochk = intel_uncore_read(uncore, GAM_ECOCHK);
1551 	if (IS_HASWELL(i915)) {
1552 		ecochk |= ECOCHK_PPGTT_WB_HSW;
1553 	} else {
1554 		ecochk |= ECOCHK_PPGTT_LLC_IVB;
1555 		ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1556 	}
1557 	intel_uncore_write(uncore, GAM_ECOCHK, ecochk);
1558 
1559 	for_each_engine(engine, i915, id) {
1560 		/* GFX_MODE is per-ring on gen7+ */
1561 		ENGINE_WRITE(engine,
1562 			     RING_MODE_GEN7,
1563 			     _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1564 	}
1565 }
1566 
1567 static void gen6_ppgtt_enable(struct intel_gt *gt)
1568 {
1569 	struct intel_uncore *uncore = gt->uncore;
1570 
1571 	intel_uncore_rmw(uncore,
1572 			 GAC_ECO_BITS,
1573 			 0,
1574 			 ECOBITS_SNB_BIT | ECOBITS_PPGTT_CACHE64B);
1575 
1576 	intel_uncore_rmw(uncore,
1577 			 GAB_CTL,
1578 			 0,
1579 			 GAB_CTL_CONT_AFTER_PAGEFAULT);
1580 
1581 	intel_uncore_rmw(uncore,
1582 			 GAM_ECOCHK,
1583 			 0,
1584 			 ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1585 
1586 	if (HAS_PPGTT(uncore->i915)) /* may be disabled for VT-d */
1587 		intel_uncore_write(uncore,
1588 				   GFX_MODE,
1589 				   _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1590 }
1591 
1592 /* PPGTT support for Sandybdrige/Gen6 and later */
1593 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
1594 				   u64 start, u64 length)
1595 {
1596 	struct gen6_ppgtt * const ppgtt = to_gen6_ppgtt(i915_vm_to_ppgtt(vm));
1597 	const unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
1598 	const gen6_pte_t scratch_pte = vm->scratch[0].encode;
1599 	unsigned int pde = first_entry / GEN6_PTES;
1600 	unsigned int pte = first_entry % GEN6_PTES;
1601 	unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
1602 
1603 	while (num_entries) {
1604 		struct i915_page_table * const pt =
1605 			i915_pt_entry(ppgtt->base.pd, pde++);
1606 		const unsigned int count = min(num_entries, GEN6_PTES - pte);
1607 		gen6_pte_t *vaddr;
1608 
1609 		GEM_BUG_ON(px_base(pt) == px_base(&vm->scratch[1]));
1610 
1611 		num_entries -= count;
1612 
1613 		GEM_BUG_ON(count > atomic_read(&pt->used));
1614 		if (!atomic_sub_return(count, &pt->used))
1615 			ppgtt->scan_for_unused_pt = true;
1616 
1617 		/*
1618 		 * Note that the hw doesn't support removing PDE on the fly
1619 		 * (they are cached inside the context with no means to
1620 		 * invalidate the cache), so we can only reset the PTE
1621 		 * entries back to scratch.
1622 		 */
1623 
1624 		vaddr = kmap_atomic_px(pt);
1625 		memset32(vaddr + pte, scratch_pte, count);
1626 		kunmap_atomic(vaddr);
1627 
1628 		pte = 0;
1629 	}
1630 }
1631 
1632 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
1633 				      struct i915_vma *vma,
1634 				      enum i915_cache_level cache_level,
1635 				      u32 flags)
1636 {
1637 	struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1638 	struct i915_page_directory * const pd = ppgtt->pd;
1639 	unsigned first_entry = vma->node.start / I915_GTT_PAGE_SIZE;
1640 	unsigned act_pt = first_entry / GEN6_PTES;
1641 	unsigned act_pte = first_entry % GEN6_PTES;
1642 	const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1643 	struct sgt_dma iter = sgt_dma(vma);
1644 	gen6_pte_t *vaddr;
1645 
1646 	GEM_BUG_ON(pd->entry[act_pt] == &vm->scratch[1]);
1647 
1648 	vaddr = kmap_atomic_px(i915_pt_entry(pd, act_pt));
1649 	do {
1650 		vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
1651 
1652 		iter.dma += I915_GTT_PAGE_SIZE;
1653 		if (iter.dma == iter.max) {
1654 			iter.sg = __sg_next(iter.sg);
1655 			if (!iter.sg)
1656 				break;
1657 
1658 			iter.dma = sg_dma_address(iter.sg);
1659 			iter.max = iter.dma + iter.sg->length;
1660 		}
1661 
1662 		if (++act_pte == GEN6_PTES) {
1663 			kunmap_atomic(vaddr);
1664 			vaddr = kmap_atomic_px(i915_pt_entry(pd, ++act_pt));
1665 			act_pte = 0;
1666 		}
1667 	} while (1);
1668 	kunmap_atomic(vaddr);
1669 
1670 	vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
1671 }
1672 
1673 static int gen6_alloc_va_range(struct i915_address_space *vm,
1674 			       u64 start, u64 length)
1675 {
1676 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(i915_vm_to_ppgtt(vm));
1677 	struct i915_page_directory * const pd = ppgtt->base.pd;
1678 	struct i915_page_table *pt, *alloc = NULL;
1679 	intel_wakeref_t wakeref;
1680 	u64 from = start;
1681 	unsigned int pde;
1682 	bool flush = false;
1683 	int ret = 0;
1684 
1685 	wakeref = intel_runtime_pm_get(&vm->i915->runtime_pm);
1686 
1687 	spin_lock(&pd->lock);
1688 	gen6_for_each_pde(pt, pd, start, length, pde) {
1689 		const unsigned int count = gen6_pte_count(start, length);
1690 
1691 		if (px_base(pt) == px_base(&vm->scratch[1])) {
1692 			spin_unlock(&pd->lock);
1693 
1694 			pt = fetch_and_zero(&alloc);
1695 			if (!pt)
1696 				pt = alloc_pt(vm);
1697 			if (IS_ERR(pt)) {
1698 				ret = PTR_ERR(pt);
1699 				goto unwind_out;
1700 			}
1701 
1702 			fill32_px(pt, vm->scratch[0].encode);
1703 
1704 			spin_lock(&pd->lock);
1705 			if (pd->entry[pde] == &vm->scratch[1]) {
1706 				pd->entry[pde] = pt;
1707 				if (i915_vma_is_bound(ppgtt->vma,
1708 						      I915_VMA_GLOBAL_BIND)) {
1709 					gen6_write_pde(ppgtt, pde, pt);
1710 					flush = true;
1711 				}
1712 			} else {
1713 				alloc = pt;
1714 				pt = pd->entry[pde];
1715 			}
1716 		}
1717 
1718 		atomic_add(count, &pt->used);
1719 	}
1720 	spin_unlock(&pd->lock);
1721 
1722 	if (flush) {
1723 		mark_tlbs_dirty(&ppgtt->base);
1724 		gen6_ggtt_invalidate(vm->gt->ggtt);
1725 	}
1726 
1727 	goto out;
1728 
1729 unwind_out:
1730 	gen6_ppgtt_clear_range(vm, from, start - from);
1731 out:
1732 	if (alloc)
1733 		free_px(vm, alloc);
1734 	intel_runtime_pm_put(&vm->i915->runtime_pm, wakeref);
1735 	return ret;
1736 }
1737 
1738 static int gen6_ppgtt_init_scratch(struct gen6_ppgtt *ppgtt)
1739 {
1740 	struct i915_address_space * const vm = &ppgtt->base.vm;
1741 	struct i915_page_directory * const pd = ppgtt->base.pd;
1742 	int ret;
1743 
1744 	ret = setup_scratch_page(vm, __GFP_HIGHMEM);
1745 	if (ret)
1746 		return ret;
1747 
1748 	vm->scratch[0].encode =
1749 		vm->pte_encode(px_dma(&vm->scratch[0]),
1750 			       I915_CACHE_NONE, PTE_READ_ONLY);
1751 
1752 	if (unlikely(setup_page_dma(vm, px_base(&vm->scratch[1])))) {
1753 		cleanup_scratch_page(vm);
1754 		return -ENOMEM;
1755 	}
1756 
1757 	fill32_px(&vm->scratch[1], vm->scratch[0].encode);
1758 	memset_p(pd->entry, &vm->scratch[1], I915_PDES);
1759 
1760 	return 0;
1761 }
1762 
1763 static void gen6_ppgtt_free_pd(struct gen6_ppgtt *ppgtt)
1764 {
1765 	struct i915_page_directory * const pd = ppgtt->base.pd;
1766 	struct i915_page_dma * const scratch =
1767 		px_base(&ppgtt->base.vm.scratch[1]);
1768 	struct i915_page_table *pt;
1769 	u32 pde;
1770 
1771 	gen6_for_all_pdes(pt, pd, pde)
1772 		if (px_base(pt) != scratch)
1773 			free_px(&ppgtt->base.vm, pt);
1774 }
1775 
1776 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
1777 {
1778 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(i915_vm_to_ppgtt(vm));
1779 	struct drm_i915_private *i915 = vm->i915;
1780 
1781 	/* FIXME remove the struct_mutex to bring the locking under control */
1782 	mutex_lock(&i915->drm.struct_mutex);
1783 	i915_vma_destroy(ppgtt->vma);
1784 	mutex_unlock(&i915->drm.struct_mutex);
1785 
1786 	gen6_ppgtt_free_pd(ppgtt);
1787 	free_scratch(vm);
1788 	kfree(ppgtt->base.pd);
1789 }
1790 
1791 static int pd_vma_set_pages(struct i915_vma *vma)
1792 {
1793 	vma->pages = ERR_PTR(-ENODEV);
1794 	return 0;
1795 }
1796 
1797 static void pd_vma_clear_pages(struct i915_vma *vma)
1798 {
1799 	GEM_BUG_ON(!vma->pages);
1800 
1801 	vma->pages = NULL;
1802 }
1803 
1804 static int pd_vma_bind(struct i915_vma *vma,
1805 		       enum i915_cache_level cache_level,
1806 		       u32 unused)
1807 {
1808 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vma->vm);
1809 	struct gen6_ppgtt *ppgtt = vma->private;
1810 	u32 ggtt_offset = i915_ggtt_offset(vma) / I915_GTT_PAGE_SIZE;
1811 	struct i915_page_table *pt;
1812 	unsigned int pde;
1813 
1814 	px_base(ppgtt->base.pd)->ggtt_offset = ggtt_offset * sizeof(gen6_pte_t);
1815 	ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm + ggtt_offset;
1816 
1817 	gen6_for_all_pdes(pt, ppgtt->base.pd, pde)
1818 		gen6_write_pde(ppgtt, pde, pt);
1819 
1820 	mark_tlbs_dirty(&ppgtt->base);
1821 	gen6_ggtt_invalidate(ggtt);
1822 
1823 	return 0;
1824 }
1825 
1826 static void pd_vma_unbind(struct i915_vma *vma)
1827 {
1828 	struct gen6_ppgtt *ppgtt = vma->private;
1829 	struct i915_page_directory * const pd = ppgtt->base.pd;
1830 	struct i915_page_dma * const scratch =
1831 		px_base(&ppgtt->base.vm.scratch[1]);
1832 	struct i915_page_table *pt;
1833 	unsigned int pde;
1834 
1835 	if (!ppgtt->scan_for_unused_pt)
1836 		return;
1837 
1838 	/* Free all no longer used page tables */
1839 	gen6_for_all_pdes(pt, ppgtt->base.pd, pde) {
1840 		if (px_base(pt) == scratch || atomic_read(&pt->used))
1841 			continue;
1842 
1843 		free_px(&ppgtt->base.vm, pt);
1844 		pd->entry[pde] = scratch;
1845 	}
1846 
1847 	ppgtt->scan_for_unused_pt = false;
1848 }
1849 
1850 static const struct i915_vma_ops pd_vma_ops = {
1851 	.set_pages = pd_vma_set_pages,
1852 	.clear_pages = pd_vma_clear_pages,
1853 	.bind_vma = pd_vma_bind,
1854 	.unbind_vma = pd_vma_unbind,
1855 };
1856 
1857 static struct i915_vma *pd_vma_create(struct gen6_ppgtt *ppgtt, int size)
1858 {
1859 	struct drm_i915_private *i915 = ppgtt->base.vm.i915;
1860 	struct i915_ggtt *ggtt = ppgtt->base.vm.gt->ggtt;
1861 	struct i915_vma *vma;
1862 
1863 	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
1864 	GEM_BUG_ON(size > ggtt->vm.total);
1865 
1866 	vma = i915_vma_alloc();
1867 	if (!vma)
1868 		return ERR_PTR(-ENOMEM);
1869 
1870 	i915_active_init(i915, &vma->active, NULL, NULL);
1871 	INIT_ACTIVE_REQUEST(&vma->last_fence);
1872 
1873 	vma->vm = &ggtt->vm;
1874 	vma->ops = &pd_vma_ops;
1875 	vma->private = ppgtt;
1876 
1877 	vma->size = size;
1878 	vma->fence_size = size;
1879 	vma->flags = I915_VMA_GGTT;
1880 	vma->ggtt_view.type = I915_GGTT_VIEW_ROTATED; /* prevent fencing */
1881 
1882 	INIT_LIST_HEAD(&vma->obj_link);
1883 	INIT_LIST_HEAD(&vma->closed_link);
1884 
1885 	mutex_lock(&vma->vm->mutex);
1886 	list_add(&vma->vm_link, &vma->vm->unbound_list);
1887 	mutex_unlock(&vma->vm->mutex);
1888 
1889 	return vma;
1890 }
1891 
1892 int gen6_ppgtt_pin(struct i915_ppgtt *base)
1893 {
1894 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(base);
1895 	int err;
1896 
1897 	GEM_BUG_ON(ppgtt->base.vm.closed);
1898 
1899 	/*
1900 	 * Workaround the limited maximum vma->pin_count and the aliasing_ppgtt
1901 	 * which will be pinned into every active context.
1902 	 * (When vma->pin_count becomes atomic, I expect we will naturally
1903 	 * need a larger, unpacked, type and kill this redundancy.)
1904 	 */
1905 	if (ppgtt->pin_count++)
1906 		return 0;
1907 
1908 	/*
1909 	 * PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1910 	 * allocator works in address space sizes, so it's multiplied by page
1911 	 * size. We allocate at the top of the GTT to avoid fragmentation.
1912 	 */
1913 	err = i915_vma_pin(ppgtt->vma,
1914 			   0, GEN6_PD_ALIGN,
1915 			   PIN_GLOBAL | PIN_HIGH);
1916 	if (err)
1917 		goto unpin;
1918 
1919 	return 0;
1920 
1921 unpin:
1922 	ppgtt->pin_count = 0;
1923 	return err;
1924 }
1925 
1926 void gen6_ppgtt_unpin(struct i915_ppgtt *base)
1927 {
1928 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(base);
1929 
1930 	GEM_BUG_ON(!ppgtt->pin_count);
1931 	if (--ppgtt->pin_count)
1932 		return;
1933 
1934 	i915_vma_unpin(ppgtt->vma);
1935 }
1936 
1937 void gen6_ppgtt_unpin_all(struct i915_ppgtt *base)
1938 {
1939 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(base);
1940 
1941 	if (!ppgtt->pin_count)
1942 		return;
1943 
1944 	ppgtt->pin_count = 0;
1945 	i915_vma_unpin(ppgtt->vma);
1946 }
1947 
1948 static struct i915_ppgtt *gen6_ppgtt_create(struct drm_i915_private *i915)
1949 {
1950 	struct i915_ggtt * const ggtt = &i915->ggtt;
1951 	struct gen6_ppgtt *ppgtt;
1952 	int err;
1953 
1954 	ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1955 	if (!ppgtt)
1956 		return ERR_PTR(-ENOMEM);
1957 
1958 	ppgtt_init(&ppgtt->base, &i915->gt);
1959 	ppgtt->base.vm.top = 1;
1960 
1961 	ppgtt->base.vm.allocate_va_range = gen6_alloc_va_range;
1962 	ppgtt->base.vm.clear_range = gen6_ppgtt_clear_range;
1963 	ppgtt->base.vm.insert_entries = gen6_ppgtt_insert_entries;
1964 	ppgtt->base.vm.cleanup = gen6_ppgtt_cleanup;
1965 
1966 	ppgtt->base.vm.pte_encode = ggtt->vm.pte_encode;
1967 
1968 	ppgtt->base.pd = __alloc_pd(sizeof(*ppgtt->base.pd));
1969 	if (!ppgtt->base.pd) {
1970 		err = -ENOMEM;
1971 		goto err_free;
1972 	}
1973 
1974 	err = gen6_ppgtt_init_scratch(ppgtt);
1975 	if (err)
1976 		goto err_pd;
1977 
1978 	ppgtt->vma = pd_vma_create(ppgtt, GEN6_PD_SIZE);
1979 	if (IS_ERR(ppgtt->vma)) {
1980 		err = PTR_ERR(ppgtt->vma);
1981 		goto err_scratch;
1982 	}
1983 
1984 	return &ppgtt->base;
1985 
1986 err_scratch:
1987 	free_scratch(&ppgtt->base.vm);
1988 err_pd:
1989 	kfree(ppgtt->base.pd);
1990 err_free:
1991 	kfree(ppgtt);
1992 	return ERR_PTR(err);
1993 }
1994 
1995 static void gtt_write_workarounds(struct intel_gt *gt)
1996 {
1997 	struct drm_i915_private *i915 = gt->i915;
1998 	struct intel_uncore *uncore = gt->uncore;
1999 
2000 	/* This function is for gtt related workarounds. This function is
2001 	 * called on driver load and after a GPU reset, so you can place
2002 	 * workarounds here even if they get overwritten by GPU reset.
2003 	 */
2004 	/* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk,cfl,cnl,icl */
2005 	if (IS_BROADWELL(i915))
2006 		intel_uncore_write(uncore,
2007 				   GEN8_L3_LRA_1_GPGPU,
2008 				   GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
2009 	else if (IS_CHERRYVIEW(i915))
2010 		intel_uncore_write(uncore,
2011 				   GEN8_L3_LRA_1_GPGPU,
2012 				   GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
2013 	else if (IS_GEN9_LP(i915))
2014 		intel_uncore_write(uncore,
2015 				   GEN8_L3_LRA_1_GPGPU,
2016 				   GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2017 	else if (INTEL_GEN(i915) >= 9)
2018 		intel_uncore_write(uncore,
2019 				   GEN8_L3_LRA_1_GPGPU,
2020 				   GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
2021 
2022 	/*
2023 	 * To support 64K PTEs we need to first enable the use of the
2024 	 * Intermediate-Page-Size(IPS) bit of the PDE field via some magical
2025 	 * mmio, otherwise the page-walker will simply ignore the IPS bit. This
2026 	 * shouldn't be needed after GEN10.
2027 	 *
2028 	 * 64K pages were first introduced from BDW+, although technically they
2029 	 * only *work* from gen9+. For pre-BDW we instead have the option for
2030 	 * 32K pages, but we don't currently have any support for it in our
2031 	 * driver.
2032 	 */
2033 	if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_64K) &&
2034 	    INTEL_GEN(i915) <= 10)
2035 		intel_uncore_rmw(uncore,
2036 				 GEN8_GAMW_ECO_DEV_RW_IA,
2037 				 0,
2038 				 GAMW_ECO_ENABLE_64K_IPS_FIELD);
2039 }
2040 
2041 int i915_ppgtt_init_hw(struct intel_gt *gt)
2042 {
2043 	struct drm_i915_private *i915 = gt->i915;
2044 
2045 	gtt_write_workarounds(gt);
2046 
2047 	if (IS_GEN(i915, 6))
2048 		gen6_ppgtt_enable(gt);
2049 	else if (IS_GEN(i915, 7))
2050 		gen7_ppgtt_enable(gt);
2051 
2052 	return 0;
2053 }
2054 
2055 static struct i915_ppgtt *
2056 __ppgtt_create(struct drm_i915_private *i915)
2057 {
2058 	if (INTEL_GEN(i915) < 8)
2059 		return gen6_ppgtt_create(i915);
2060 	else
2061 		return gen8_ppgtt_create(i915);
2062 }
2063 
2064 struct i915_ppgtt *
2065 i915_ppgtt_create(struct drm_i915_private *i915)
2066 {
2067 	struct i915_ppgtt *ppgtt;
2068 
2069 	ppgtt = __ppgtt_create(i915);
2070 	if (IS_ERR(ppgtt))
2071 		return ppgtt;
2072 
2073 	trace_i915_ppgtt_create(&ppgtt->vm);
2074 
2075 	return ppgtt;
2076 }
2077 
2078 /* Certain Gen5 chipsets require require idling the GPU before
2079  * unmapping anything from the GTT when VT-d is enabled.
2080  */
2081 static bool needs_idle_maps(struct drm_i915_private *dev_priv)
2082 {
2083 	/* Query intel_iommu to see if we need the workaround. Presumably that
2084 	 * was loaded first.
2085 	 */
2086 	return IS_GEN(dev_priv, 5) && IS_MOBILE(dev_priv) && intel_vtd_active();
2087 }
2088 
2089 static void ggtt_suspend_mappings(struct i915_ggtt *ggtt)
2090 {
2091 	struct drm_i915_private *i915 = ggtt->vm.i915;
2092 
2093 	/* Don't bother messing with faults pre GEN6 as we have little
2094 	 * documentation supporting that it's a good idea.
2095 	 */
2096 	if (INTEL_GEN(i915) < 6)
2097 		return;
2098 
2099 	intel_gt_check_and_clear_faults(ggtt->vm.gt);
2100 
2101 	ggtt->vm.clear_range(&ggtt->vm, 0, ggtt->vm.total);
2102 
2103 	ggtt->invalidate(ggtt);
2104 }
2105 
2106 void i915_gem_suspend_gtt_mappings(struct drm_i915_private *i915)
2107 {
2108 	ggtt_suspend_mappings(&i915->ggtt);
2109 }
2110 
2111 int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2112 			       struct sg_table *pages)
2113 {
2114 	do {
2115 		if (dma_map_sg_attrs(&obj->base.dev->pdev->dev,
2116 				     pages->sgl, pages->nents,
2117 				     PCI_DMA_BIDIRECTIONAL,
2118 				     DMA_ATTR_NO_WARN))
2119 			return 0;
2120 
2121 		/*
2122 		 * If the DMA remap fails, one cause can be that we have
2123 		 * too many objects pinned in a small remapping table,
2124 		 * such as swiotlb. Incrementally purge all other objects and
2125 		 * try again - if there are no more pages to remove from
2126 		 * the DMA remapper, i915_gem_shrink will return 0.
2127 		 */
2128 		GEM_BUG_ON(obj->mm.pages == pages);
2129 	} while (i915_gem_shrink(to_i915(obj->base.dev),
2130 				 obj->base.size >> PAGE_SHIFT, NULL,
2131 				 I915_SHRINK_BOUND |
2132 				 I915_SHRINK_UNBOUND));
2133 
2134 	return -ENOSPC;
2135 }
2136 
2137 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
2138 {
2139 	writeq(pte, addr);
2140 }
2141 
2142 static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2143 				  dma_addr_t addr,
2144 				  u64 offset,
2145 				  enum i915_cache_level level,
2146 				  u32 unused)
2147 {
2148 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2149 	gen8_pte_t __iomem *pte =
2150 		(gen8_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
2151 
2152 	gen8_set_pte(pte, gen8_pte_encode(addr, level, 0));
2153 
2154 	ggtt->invalidate(ggtt);
2155 }
2156 
2157 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2158 				     struct i915_vma *vma,
2159 				     enum i915_cache_level level,
2160 				     u32 flags)
2161 {
2162 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2163 	struct sgt_iter sgt_iter;
2164 	gen8_pte_t __iomem *gtt_entries;
2165 	const gen8_pte_t pte_encode = gen8_pte_encode(0, level, 0);
2166 	dma_addr_t addr;
2167 
2168 	/*
2169 	 * Note that we ignore PTE_READ_ONLY here. The caller must be careful
2170 	 * not to allow the user to override access to a read only page.
2171 	 */
2172 
2173 	gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
2174 	gtt_entries += vma->node.start / I915_GTT_PAGE_SIZE;
2175 	for_each_sgt_dma(addr, sgt_iter, vma->pages)
2176 		gen8_set_pte(gtt_entries++, pte_encode | addr);
2177 
2178 	/*
2179 	 * We want to flush the TLBs only after we're certain all the PTE
2180 	 * updates have finished.
2181 	 */
2182 	ggtt->invalidate(ggtt);
2183 }
2184 
2185 static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2186 				  dma_addr_t addr,
2187 				  u64 offset,
2188 				  enum i915_cache_level level,
2189 				  u32 flags)
2190 {
2191 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2192 	gen6_pte_t __iomem *pte =
2193 		(gen6_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
2194 
2195 	iowrite32(vm->pte_encode(addr, level, flags), pte);
2196 
2197 	ggtt->invalidate(ggtt);
2198 }
2199 
2200 /*
2201  * Binds an object into the global gtt with the specified cache level. The object
2202  * will be accessible to the GPU via commands whose operands reference offsets
2203  * within the global GTT as well as accessible by the GPU through the GMADR
2204  * mapped BAR (dev_priv->mm.gtt->gtt).
2205  */
2206 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
2207 				     struct i915_vma *vma,
2208 				     enum i915_cache_level level,
2209 				     u32 flags)
2210 {
2211 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2212 	gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
2213 	unsigned int i = vma->node.start / I915_GTT_PAGE_SIZE;
2214 	struct sgt_iter iter;
2215 	dma_addr_t addr;
2216 	for_each_sgt_dma(addr, iter, vma->pages)
2217 		iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2218 
2219 	/*
2220 	 * We want to flush the TLBs only after we're certain all the PTE
2221 	 * updates have finished.
2222 	 */
2223 	ggtt->invalidate(ggtt);
2224 }
2225 
2226 static void nop_clear_range(struct i915_address_space *vm,
2227 			    u64 start, u64 length)
2228 {
2229 }
2230 
2231 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
2232 				  u64 start, u64 length)
2233 {
2234 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2235 	unsigned first_entry = start / I915_GTT_PAGE_SIZE;
2236 	unsigned num_entries = length / I915_GTT_PAGE_SIZE;
2237 	const gen8_pte_t scratch_pte = vm->scratch[0].encode;
2238 	gen8_pte_t __iomem *gtt_base =
2239 		(gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2240 	const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2241 	int i;
2242 
2243 	if (WARN(num_entries > max_entries,
2244 		 "First entry = %d; Num entries = %d (max=%d)\n",
2245 		 first_entry, num_entries, max_entries))
2246 		num_entries = max_entries;
2247 
2248 	for (i = 0; i < num_entries; i++)
2249 		gen8_set_pte(&gtt_base[i], scratch_pte);
2250 }
2251 
2252 static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
2253 {
2254 	struct drm_i915_private *dev_priv = vm->i915;
2255 
2256 	/*
2257 	 * Make sure the internal GAM fifo has been cleared of all GTT
2258 	 * writes before exiting stop_machine(). This guarantees that
2259 	 * any aperture accesses waiting to start in another process
2260 	 * cannot back up behind the GTT writes causing a hang.
2261 	 * The register can be any arbitrary GAM register.
2262 	 */
2263 	POSTING_READ(GFX_FLSH_CNTL_GEN6);
2264 }
2265 
2266 struct insert_page {
2267 	struct i915_address_space *vm;
2268 	dma_addr_t addr;
2269 	u64 offset;
2270 	enum i915_cache_level level;
2271 };
2272 
2273 static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
2274 {
2275 	struct insert_page *arg = _arg;
2276 
2277 	gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset, arg->level, 0);
2278 	bxt_vtd_ggtt_wa(arg->vm);
2279 
2280 	return 0;
2281 }
2282 
2283 static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
2284 					  dma_addr_t addr,
2285 					  u64 offset,
2286 					  enum i915_cache_level level,
2287 					  u32 unused)
2288 {
2289 	struct insert_page arg = { vm, addr, offset, level };
2290 
2291 	stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
2292 }
2293 
2294 struct insert_entries {
2295 	struct i915_address_space *vm;
2296 	struct i915_vma *vma;
2297 	enum i915_cache_level level;
2298 	u32 flags;
2299 };
2300 
2301 static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
2302 {
2303 	struct insert_entries *arg = _arg;
2304 
2305 	gen8_ggtt_insert_entries(arg->vm, arg->vma, arg->level, arg->flags);
2306 	bxt_vtd_ggtt_wa(arg->vm);
2307 
2308 	return 0;
2309 }
2310 
2311 static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2312 					     struct i915_vma *vma,
2313 					     enum i915_cache_level level,
2314 					     u32 flags)
2315 {
2316 	struct insert_entries arg = { vm, vma, level, flags };
2317 
2318 	stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
2319 }
2320 
2321 struct clear_range {
2322 	struct i915_address_space *vm;
2323 	u64 start;
2324 	u64 length;
2325 };
2326 
2327 static int bxt_vtd_ggtt_clear_range__cb(void *_arg)
2328 {
2329 	struct clear_range *arg = _arg;
2330 
2331 	gen8_ggtt_clear_range(arg->vm, arg->start, arg->length);
2332 	bxt_vtd_ggtt_wa(arg->vm);
2333 
2334 	return 0;
2335 }
2336 
2337 static void bxt_vtd_ggtt_clear_range__BKL(struct i915_address_space *vm,
2338 					  u64 start,
2339 					  u64 length)
2340 {
2341 	struct clear_range arg = { vm, start, length };
2342 
2343 	stop_machine(bxt_vtd_ggtt_clear_range__cb, &arg, NULL);
2344 }
2345 
2346 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
2347 				  u64 start, u64 length)
2348 {
2349 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2350 	unsigned first_entry = start / I915_GTT_PAGE_SIZE;
2351 	unsigned num_entries = length / I915_GTT_PAGE_SIZE;
2352 	gen6_pte_t scratch_pte, __iomem *gtt_base =
2353 		(gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2354 	const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2355 	int i;
2356 
2357 	if (WARN(num_entries > max_entries,
2358 		 "First entry = %d; Num entries = %d (max=%d)\n",
2359 		 first_entry, num_entries, max_entries))
2360 		num_entries = max_entries;
2361 
2362 	scratch_pte = vm->scratch[0].encode;
2363 	for (i = 0; i < num_entries; i++)
2364 		iowrite32(scratch_pte, &gtt_base[i]);
2365 }
2366 
2367 static void i915_ggtt_insert_page(struct i915_address_space *vm,
2368 				  dma_addr_t addr,
2369 				  u64 offset,
2370 				  enum i915_cache_level cache_level,
2371 				  u32 unused)
2372 {
2373 	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2374 		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2375 
2376 	intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
2377 }
2378 
2379 static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2380 				     struct i915_vma *vma,
2381 				     enum i915_cache_level cache_level,
2382 				     u32 unused)
2383 {
2384 	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2385 		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2386 
2387 	intel_gtt_insert_sg_entries(vma->pages, vma->node.start >> PAGE_SHIFT,
2388 				    flags);
2389 }
2390 
2391 static void i915_ggtt_clear_range(struct i915_address_space *vm,
2392 				  u64 start, u64 length)
2393 {
2394 	intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
2395 }
2396 
2397 static int ggtt_bind_vma(struct i915_vma *vma,
2398 			 enum i915_cache_level cache_level,
2399 			 u32 flags)
2400 {
2401 	struct drm_i915_private *i915 = vma->vm->i915;
2402 	struct drm_i915_gem_object *obj = vma->obj;
2403 	intel_wakeref_t wakeref;
2404 	u32 pte_flags;
2405 
2406 	/* Applicable to VLV (gen8+ do not support RO in the GGTT) */
2407 	pte_flags = 0;
2408 	if (i915_gem_object_is_readonly(obj))
2409 		pte_flags |= PTE_READ_ONLY;
2410 
2411 	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
2412 		vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
2413 
2414 	vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
2415 
2416 	/*
2417 	 * Without aliasing PPGTT there's no difference between
2418 	 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2419 	 * upgrade to both bound if we bind either to avoid double-binding.
2420 	 */
2421 	vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
2422 
2423 	return 0;
2424 }
2425 
2426 static void ggtt_unbind_vma(struct i915_vma *vma)
2427 {
2428 	struct drm_i915_private *i915 = vma->vm->i915;
2429 	intel_wakeref_t wakeref;
2430 
2431 	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
2432 		vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2433 }
2434 
2435 static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2436 				 enum i915_cache_level cache_level,
2437 				 u32 flags)
2438 {
2439 	struct drm_i915_private *i915 = vma->vm->i915;
2440 	u32 pte_flags;
2441 	int ret;
2442 
2443 	/* Currently applicable only to VLV */
2444 	pte_flags = 0;
2445 	if (i915_gem_object_is_readonly(vma->obj))
2446 		pte_flags |= PTE_READ_ONLY;
2447 
2448 	if (flags & I915_VMA_LOCAL_BIND) {
2449 		struct i915_ppgtt *alias = i915_vm_to_ggtt(vma->vm)->alias;
2450 
2451 		if (!(vma->flags & I915_VMA_LOCAL_BIND)) {
2452 			ret = alias->vm.allocate_va_range(&alias->vm,
2453 							  vma->node.start,
2454 							  vma->size);
2455 			if (ret)
2456 				return ret;
2457 		}
2458 
2459 		alias->vm.insert_entries(&alias->vm, vma,
2460 					 cache_level, pte_flags);
2461 	}
2462 
2463 	if (flags & I915_VMA_GLOBAL_BIND) {
2464 		intel_wakeref_t wakeref;
2465 
2466 		with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
2467 			vma->vm->insert_entries(vma->vm, vma,
2468 						cache_level, pte_flags);
2469 		}
2470 	}
2471 
2472 	return 0;
2473 }
2474 
2475 static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
2476 {
2477 	struct drm_i915_private *i915 = vma->vm->i915;
2478 
2479 	if (vma->flags & I915_VMA_GLOBAL_BIND) {
2480 		struct i915_address_space *vm = vma->vm;
2481 		intel_wakeref_t wakeref;
2482 
2483 		with_intel_runtime_pm(&i915->runtime_pm, wakeref)
2484 			vm->clear_range(vm, vma->node.start, vma->size);
2485 	}
2486 
2487 	if (vma->flags & I915_VMA_LOCAL_BIND) {
2488 		struct i915_address_space *vm =
2489 			&i915_vm_to_ggtt(vma->vm)->alias->vm;
2490 
2491 		vm->clear_range(vm, vma->node.start, vma->size);
2492 	}
2493 }
2494 
2495 void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2496 			       struct sg_table *pages)
2497 {
2498 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2499 	struct device *kdev = &dev_priv->drm.pdev->dev;
2500 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
2501 
2502 	if (unlikely(ggtt->do_idle_maps)) {
2503 		if (i915_gem_wait_for_idle(dev_priv, 0, MAX_SCHEDULE_TIMEOUT)) {
2504 			DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2505 			/* Wait a bit, in hopes it avoids the hang */
2506 			udelay(10);
2507 		}
2508 	}
2509 
2510 	dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
2511 }
2512 
2513 static int ggtt_set_pages(struct i915_vma *vma)
2514 {
2515 	int ret;
2516 
2517 	GEM_BUG_ON(vma->pages);
2518 
2519 	ret = i915_get_ggtt_vma_pages(vma);
2520 	if (ret)
2521 		return ret;
2522 
2523 	vma->page_sizes = vma->obj->mm.page_sizes;
2524 
2525 	return 0;
2526 }
2527 
2528 static void i915_gtt_color_adjust(const struct drm_mm_node *node,
2529 				  unsigned long color,
2530 				  u64 *start,
2531 				  u64 *end)
2532 {
2533 	if (node->allocated && node->color != color)
2534 		*start += I915_GTT_PAGE_SIZE;
2535 
2536 	/* Also leave a space between the unallocated reserved node after the
2537 	 * GTT and any objects within the GTT, i.e. we use the color adjustment
2538 	 * to insert a guard page to prevent prefetches crossing over the
2539 	 * GTT boundary.
2540 	 */
2541 	node = list_next_entry(node, node_list);
2542 	if (node->color != color)
2543 		*end -= I915_GTT_PAGE_SIZE;
2544 }
2545 
2546 static int init_aliasing_ppgtt(struct i915_ggtt *ggtt)
2547 {
2548 	struct i915_ppgtt *ppgtt;
2549 	int err;
2550 
2551 	ppgtt = i915_ppgtt_create(ggtt->vm.i915);
2552 	if (IS_ERR(ppgtt))
2553 		return PTR_ERR(ppgtt);
2554 
2555 	if (GEM_WARN_ON(ppgtt->vm.total < ggtt->vm.total)) {
2556 		err = -ENODEV;
2557 		goto err_ppgtt;
2558 	}
2559 
2560 	/*
2561 	 * Note we only pre-allocate as far as the end of the global
2562 	 * GTT. On 48b / 4-level page-tables, the difference is very,
2563 	 * very significant! We have to preallocate as GVT/vgpu does
2564 	 * not like the page directory disappearing.
2565 	 */
2566 	err = ppgtt->vm.allocate_va_range(&ppgtt->vm, 0, ggtt->vm.total);
2567 	if (err)
2568 		goto err_ppgtt;
2569 
2570 	ggtt->alias = ppgtt;
2571 
2572 	GEM_BUG_ON(ggtt->vm.vma_ops.bind_vma != ggtt_bind_vma);
2573 	ggtt->vm.vma_ops.bind_vma = aliasing_gtt_bind_vma;
2574 
2575 	GEM_BUG_ON(ggtt->vm.vma_ops.unbind_vma != ggtt_unbind_vma);
2576 	ggtt->vm.vma_ops.unbind_vma = aliasing_gtt_unbind_vma;
2577 
2578 	return 0;
2579 
2580 err_ppgtt:
2581 	i915_vm_put(&ppgtt->vm);
2582 	return err;
2583 }
2584 
2585 static void fini_aliasing_ppgtt(struct i915_ggtt *ggtt)
2586 {
2587 	struct drm_i915_private *i915 = ggtt->vm.i915;
2588 	struct i915_ppgtt *ppgtt;
2589 
2590 	mutex_lock(&i915->drm.struct_mutex);
2591 
2592 	ppgtt = fetch_and_zero(&ggtt->alias);
2593 	if (!ppgtt)
2594 		goto out;
2595 
2596 	i915_vm_put(&ppgtt->vm);
2597 
2598 	ggtt->vm.vma_ops.bind_vma   = ggtt_bind_vma;
2599 	ggtt->vm.vma_ops.unbind_vma = ggtt_unbind_vma;
2600 
2601 out:
2602 	mutex_unlock(&i915->drm.struct_mutex);
2603 }
2604 
2605 static int ggtt_reserve_guc_top(struct i915_ggtt *ggtt)
2606 {
2607 	u64 size;
2608 	int ret;
2609 
2610 	if (!USES_GUC(ggtt->vm.i915))
2611 		return 0;
2612 
2613 	GEM_BUG_ON(ggtt->vm.total <= GUC_GGTT_TOP);
2614 	size = ggtt->vm.total - GUC_GGTT_TOP;
2615 
2616 	ret = i915_gem_gtt_reserve(&ggtt->vm, &ggtt->uc_fw, size,
2617 				   GUC_GGTT_TOP, I915_COLOR_UNEVICTABLE,
2618 				   PIN_NOEVICT);
2619 	if (ret)
2620 		DRM_DEBUG_DRIVER("Failed to reserve top of GGTT for GuC\n");
2621 
2622 	return ret;
2623 }
2624 
2625 static void ggtt_release_guc_top(struct i915_ggtt *ggtt)
2626 {
2627 	if (drm_mm_node_allocated(&ggtt->uc_fw))
2628 		drm_mm_remove_node(&ggtt->uc_fw);
2629 }
2630 
2631 static void cleanup_init_ggtt(struct i915_ggtt *ggtt)
2632 {
2633 	ggtt_release_guc_top(ggtt);
2634 	drm_mm_remove_node(&ggtt->error_capture);
2635 }
2636 
2637 static int init_ggtt(struct i915_ggtt *ggtt)
2638 {
2639 	/* Let GEM Manage all of the aperture.
2640 	 *
2641 	 * However, leave one page at the end still bound to the scratch page.
2642 	 * There are a number of places where the hardware apparently prefetches
2643 	 * past the end of the object, and we've seen multiple hangs with the
2644 	 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2645 	 * aperture.  One page should be enough to keep any prefetching inside
2646 	 * of the aperture.
2647 	 */
2648 	unsigned long hole_start, hole_end;
2649 	struct drm_mm_node *entry;
2650 	int ret;
2651 
2652 	/*
2653 	 * GuC requires all resources that we're sharing with it to be placed in
2654 	 * non-WOPCM memory. If GuC is not present or not in use we still need a
2655 	 * small bias as ring wraparound at offset 0 sometimes hangs. No idea
2656 	 * why.
2657 	 */
2658 	ggtt->pin_bias = max_t(u32, I915_GTT_PAGE_SIZE,
2659 			       intel_wopcm_guc_size(&ggtt->vm.i915->wopcm));
2660 
2661 	ret = intel_vgt_balloon(ggtt);
2662 	if (ret)
2663 		return ret;
2664 
2665 	/* Reserve a mappable slot for our lockless error capture */
2666 	ret = drm_mm_insert_node_in_range(&ggtt->vm.mm, &ggtt->error_capture,
2667 					  PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2668 					  0, ggtt->mappable_end,
2669 					  DRM_MM_INSERT_LOW);
2670 	if (ret)
2671 		return ret;
2672 
2673 	/*
2674 	 * The upper portion of the GuC address space has a sizeable hole
2675 	 * (several MB) that is inaccessible by GuC. Reserve this range within
2676 	 * GGTT as it can comfortably hold GuC/HuC firmware images.
2677 	 */
2678 	ret = ggtt_reserve_guc_top(ggtt);
2679 	if (ret)
2680 		goto err;
2681 
2682 	/* Clear any non-preallocated blocks */
2683 	drm_mm_for_each_hole(entry, &ggtt->vm.mm, hole_start, hole_end) {
2684 		DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2685 			      hole_start, hole_end);
2686 		ggtt->vm.clear_range(&ggtt->vm, hole_start,
2687 				     hole_end - hole_start);
2688 	}
2689 
2690 	/* And finally clear the reserved guard page */
2691 	ggtt->vm.clear_range(&ggtt->vm, ggtt->vm.total - PAGE_SIZE, PAGE_SIZE);
2692 
2693 	return 0;
2694 
2695 err:
2696 	cleanup_init_ggtt(ggtt);
2697 	return ret;
2698 }
2699 
2700 int i915_init_ggtt(struct drm_i915_private *i915)
2701 {
2702 	int ret;
2703 
2704 	ret = init_ggtt(&i915->ggtt);
2705 	if (ret)
2706 		return ret;
2707 
2708 	if (INTEL_PPGTT(i915) == INTEL_PPGTT_ALIASING) {
2709 		ret = init_aliasing_ppgtt(&i915->ggtt);
2710 		if (ret)
2711 			cleanup_init_ggtt(&i915->ggtt);
2712 	}
2713 
2714 	return 0;
2715 }
2716 
2717 static void ggtt_cleanup_hw(struct i915_ggtt *ggtt)
2718 {
2719 	struct drm_i915_private *i915 = ggtt->vm.i915;
2720 	struct i915_vma *vma, *vn;
2721 
2722 	ggtt->vm.closed = true;
2723 
2724 	rcu_barrier(); /* flush the RCU'ed__i915_vm_release */
2725 	flush_workqueue(i915->wq);
2726 
2727 	mutex_lock(&i915->drm.struct_mutex);
2728 
2729 	list_for_each_entry_safe(vma, vn, &ggtt->vm.bound_list, vm_link)
2730 		WARN_ON(i915_vma_unbind(vma));
2731 
2732 	if (drm_mm_node_allocated(&ggtt->error_capture))
2733 		drm_mm_remove_node(&ggtt->error_capture);
2734 
2735 	ggtt_release_guc_top(ggtt);
2736 
2737 	if (drm_mm_initialized(&ggtt->vm.mm)) {
2738 		intel_vgt_deballoon(ggtt);
2739 		i915_address_space_fini(&ggtt->vm);
2740 	}
2741 
2742 	ggtt->vm.cleanup(&ggtt->vm);
2743 
2744 	mutex_unlock(&i915->drm.struct_mutex);
2745 
2746 	arch_phys_wc_del(ggtt->mtrr);
2747 	io_mapping_fini(&ggtt->iomap);
2748 }
2749 
2750 /**
2751  * i915_ggtt_driver_release - Clean up GGTT hardware initialization
2752  * @i915: i915 device
2753  */
2754 void i915_ggtt_driver_release(struct drm_i915_private *i915)
2755 {
2756 	struct pagevec *pvec;
2757 
2758 	fini_aliasing_ppgtt(&i915->ggtt);
2759 
2760 	ggtt_cleanup_hw(&i915->ggtt);
2761 
2762 	pvec = &i915->mm.wc_stash.pvec;
2763 	if (pvec->nr) {
2764 		set_pages_array_wb(pvec->pages, pvec->nr);
2765 		__pagevec_release(pvec);
2766 	}
2767 
2768 	i915_gem_cleanup_stolen(i915);
2769 }
2770 
2771 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2772 {
2773 	snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2774 	snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2775 	return snb_gmch_ctl << 20;
2776 }
2777 
2778 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2779 {
2780 	bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2781 	bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2782 	if (bdw_gmch_ctl)
2783 		bdw_gmch_ctl = 1 << bdw_gmch_ctl;
2784 
2785 #ifdef CONFIG_X86_32
2786 	/* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * I915_GTT_PAGE_SIZE */
2787 	if (bdw_gmch_ctl > 4)
2788 		bdw_gmch_ctl = 4;
2789 #endif
2790 
2791 	return bdw_gmch_ctl << 20;
2792 }
2793 
2794 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2795 {
2796 	gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2797 	gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2798 
2799 	if (gmch_ctrl)
2800 		return 1 << (20 + gmch_ctrl);
2801 
2802 	return 0;
2803 }
2804 
2805 static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
2806 {
2807 	struct drm_i915_private *dev_priv = ggtt->vm.i915;
2808 	struct pci_dev *pdev = dev_priv->drm.pdev;
2809 	phys_addr_t phys_addr;
2810 	int ret;
2811 
2812 	/* For Modern GENs the PTEs and register space are split in the BAR */
2813 	phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
2814 
2815 	/*
2816 	 * On BXT+/CNL+ writes larger than 64 bit to the GTT pagetable range
2817 	 * will be dropped. For WC mappings in general we have 64 byte burst
2818 	 * writes when the WC buffer is flushed, so we can't use it, but have to
2819 	 * resort to an uncached mapping. The WC issue is easily caught by the
2820 	 * readback check when writing GTT PTE entries.
2821 	 */
2822 	if (IS_GEN9_LP(dev_priv) || INTEL_GEN(dev_priv) >= 10)
2823 		ggtt->gsm = ioremap_nocache(phys_addr, size);
2824 	else
2825 		ggtt->gsm = ioremap_wc(phys_addr, size);
2826 	if (!ggtt->gsm) {
2827 		DRM_ERROR("Failed to map the ggtt page table\n");
2828 		return -ENOMEM;
2829 	}
2830 
2831 	ret = setup_scratch_page(&ggtt->vm, GFP_DMA32);
2832 	if (ret) {
2833 		DRM_ERROR("Scratch setup failed\n");
2834 		/* iounmap will also get called at remove, but meh */
2835 		iounmap(ggtt->gsm);
2836 		return ret;
2837 	}
2838 
2839 	ggtt->vm.scratch[0].encode =
2840 		ggtt->vm.pte_encode(px_dma(&ggtt->vm.scratch[0]),
2841 				    I915_CACHE_NONE, 0);
2842 
2843 	return 0;
2844 }
2845 
2846 static void cnl_setup_private_ppat(struct drm_i915_private *dev_priv)
2847 {
2848 	I915_WRITE(GEN10_PAT_INDEX(0), GEN8_PPAT_WB | GEN8_PPAT_LLC);
2849 	I915_WRITE(GEN10_PAT_INDEX(1), GEN8_PPAT_WC | GEN8_PPAT_LLCELLC);
2850 	I915_WRITE(GEN10_PAT_INDEX(2), GEN8_PPAT_WT | GEN8_PPAT_LLCELLC);
2851 	I915_WRITE(GEN10_PAT_INDEX(3), GEN8_PPAT_UC);
2852 	I915_WRITE(GEN10_PAT_INDEX(4), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0));
2853 	I915_WRITE(GEN10_PAT_INDEX(5), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1));
2854 	I915_WRITE(GEN10_PAT_INDEX(6), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2));
2855 	I915_WRITE(GEN10_PAT_INDEX(7), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2856 }
2857 
2858 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2859  * bits. When using advanced contexts each context stores its own PAT, but
2860  * writing this data shouldn't be harmful even in those cases. */
2861 static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
2862 {
2863 	u64 pat;
2864 
2865 	pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) |	/* for normal objects, no eLLC */
2866 	      GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) |	/* for something pointing to ptes? */
2867 	      GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) |	/* for scanout with eLLC */
2868 	      GEN8_PPAT(3, GEN8_PPAT_UC) |			/* Uncached objects, mostly for scanout */
2869 	      GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2870 	      GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2871 	      GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2872 	      GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2873 
2874 	I915_WRITE(GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
2875 	I915_WRITE(GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
2876 }
2877 
2878 static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2879 {
2880 	u64 pat;
2881 
2882 	/*
2883 	 * Map WB on BDW to snooped on CHV.
2884 	 *
2885 	 * Only the snoop bit has meaning for CHV, the rest is
2886 	 * ignored.
2887 	 *
2888 	 * The hardware will never snoop for certain types of accesses:
2889 	 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2890 	 * - PPGTT page tables
2891 	 * - some other special cycles
2892 	 *
2893 	 * As with BDW, we also need to consider the following for GT accesses:
2894 	 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2895 	 * so RTL will always use the value corresponding to
2896 	 * pat_sel = 000".
2897 	 * Which means we must set the snoop bit in PAT entry 0
2898 	 * in order to keep the global status page working.
2899 	 */
2900 
2901 	pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2902 	      GEN8_PPAT(1, 0) |
2903 	      GEN8_PPAT(2, 0) |
2904 	      GEN8_PPAT(3, 0) |
2905 	      GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2906 	      GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2907 	      GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2908 	      GEN8_PPAT(7, CHV_PPAT_SNOOP);
2909 
2910 	I915_WRITE(GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
2911 	I915_WRITE(GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
2912 }
2913 
2914 static void gen6_gmch_remove(struct i915_address_space *vm)
2915 {
2916 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2917 
2918 	iounmap(ggtt->gsm);
2919 	cleanup_scratch_page(vm);
2920 }
2921 
2922 static void setup_private_pat(struct drm_i915_private *dev_priv)
2923 {
2924 	GEM_BUG_ON(INTEL_GEN(dev_priv) < 8);
2925 
2926 	if (INTEL_GEN(dev_priv) >= 10)
2927 		cnl_setup_private_ppat(dev_priv);
2928 	else if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
2929 		chv_setup_private_ppat(dev_priv);
2930 	else
2931 		bdw_setup_private_ppat(dev_priv);
2932 }
2933 
2934 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
2935 {
2936 	struct drm_i915_private *dev_priv = ggtt->vm.i915;
2937 	struct pci_dev *pdev = dev_priv->drm.pdev;
2938 	unsigned int size;
2939 	u16 snb_gmch_ctl;
2940 	int err;
2941 
2942 	/* TODO: We're not aware of mappable constraints on gen8 yet */
2943 	ggtt->gmadr =
2944 		(struct resource) DEFINE_RES_MEM(pci_resource_start(pdev, 2),
2945 						 pci_resource_len(pdev, 2));
2946 	ggtt->mappable_end = resource_size(&ggtt->gmadr);
2947 
2948 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(39));
2949 	if (!err)
2950 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
2951 	if (err)
2952 		DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
2953 
2954 	pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2955 	if (IS_CHERRYVIEW(dev_priv))
2956 		size = chv_get_total_gtt_size(snb_gmch_ctl);
2957 	else
2958 		size = gen8_get_total_gtt_size(snb_gmch_ctl);
2959 
2960 	ggtt->vm.total = (size / sizeof(gen8_pte_t)) * I915_GTT_PAGE_SIZE;
2961 	ggtt->vm.cleanup = gen6_gmch_remove;
2962 	ggtt->vm.insert_page = gen8_ggtt_insert_page;
2963 	ggtt->vm.clear_range = nop_clear_range;
2964 	if (intel_scanout_needs_vtd_wa(dev_priv))
2965 		ggtt->vm.clear_range = gen8_ggtt_clear_range;
2966 
2967 	ggtt->vm.insert_entries = gen8_ggtt_insert_entries;
2968 
2969 	/* Serialize GTT updates with aperture access on BXT if VT-d is on. */
2970 	if (intel_ggtt_update_needs_vtd_wa(dev_priv) ||
2971 	    IS_CHERRYVIEW(dev_priv) /* fails with concurrent use/update */) {
2972 		ggtt->vm.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
2973 		ggtt->vm.insert_page    = bxt_vtd_ggtt_insert_page__BKL;
2974 		if (ggtt->vm.clear_range != nop_clear_range)
2975 			ggtt->vm.clear_range = bxt_vtd_ggtt_clear_range__BKL;
2976 	}
2977 
2978 	ggtt->invalidate = gen6_ggtt_invalidate;
2979 
2980 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
2981 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
2982 	ggtt->vm.vma_ops.set_pages   = ggtt_set_pages;
2983 	ggtt->vm.vma_ops.clear_pages = clear_pages;
2984 
2985 	ggtt->vm.pte_encode = gen8_pte_encode;
2986 
2987 	setup_private_pat(dev_priv);
2988 
2989 	return ggtt_probe_common(ggtt, size);
2990 }
2991 
2992 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
2993 {
2994 	struct drm_i915_private *dev_priv = ggtt->vm.i915;
2995 	struct pci_dev *pdev = dev_priv->drm.pdev;
2996 	unsigned int size;
2997 	u16 snb_gmch_ctl;
2998 	int err;
2999 
3000 	ggtt->gmadr =
3001 		(struct resource) DEFINE_RES_MEM(pci_resource_start(pdev, 2),
3002 						 pci_resource_len(pdev, 2));
3003 	ggtt->mappable_end = resource_size(&ggtt->gmadr);
3004 
3005 	/* 64/512MB is the current min/max we actually know of, but this is just
3006 	 * a coarse sanity check.
3007 	 */
3008 	if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
3009 		DRM_ERROR("Unknown GMADR size (%pa)\n", &ggtt->mappable_end);
3010 		return -ENXIO;
3011 	}
3012 
3013 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
3014 	if (!err)
3015 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3016 	if (err)
3017 		DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
3018 	pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3019 
3020 	size = gen6_get_total_gtt_size(snb_gmch_ctl);
3021 	ggtt->vm.total = (size / sizeof(gen6_pte_t)) * I915_GTT_PAGE_SIZE;
3022 
3023 	ggtt->vm.clear_range = nop_clear_range;
3024 	if (!HAS_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
3025 		ggtt->vm.clear_range = gen6_ggtt_clear_range;
3026 	ggtt->vm.insert_page = gen6_ggtt_insert_page;
3027 	ggtt->vm.insert_entries = gen6_ggtt_insert_entries;
3028 	ggtt->vm.cleanup = gen6_gmch_remove;
3029 
3030 	ggtt->invalidate = gen6_ggtt_invalidate;
3031 
3032 	if (HAS_EDRAM(dev_priv))
3033 		ggtt->vm.pte_encode = iris_pte_encode;
3034 	else if (IS_HASWELL(dev_priv))
3035 		ggtt->vm.pte_encode = hsw_pte_encode;
3036 	else if (IS_VALLEYVIEW(dev_priv))
3037 		ggtt->vm.pte_encode = byt_pte_encode;
3038 	else if (INTEL_GEN(dev_priv) >= 7)
3039 		ggtt->vm.pte_encode = ivb_pte_encode;
3040 	else
3041 		ggtt->vm.pte_encode = snb_pte_encode;
3042 
3043 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
3044 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
3045 	ggtt->vm.vma_ops.set_pages   = ggtt_set_pages;
3046 	ggtt->vm.vma_ops.clear_pages = clear_pages;
3047 
3048 	return ggtt_probe_common(ggtt, size);
3049 }
3050 
3051 static void i915_gmch_remove(struct i915_address_space *vm)
3052 {
3053 	intel_gmch_remove();
3054 }
3055 
3056 static int i915_gmch_probe(struct i915_ggtt *ggtt)
3057 {
3058 	struct drm_i915_private *dev_priv = ggtt->vm.i915;
3059 	phys_addr_t gmadr_base;
3060 	int ret;
3061 
3062 	ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
3063 	if (!ret) {
3064 		DRM_ERROR("failed to set up gmch\n");
3065 		return -EIO;
3066 	}
3067 
3068 	intel_gtt_get(&ggtt->vm.total, &gmadr_base, &ggtt->mappable_end);
3069 
3070 	ggtt->gmadr =
3071 		(struct resource) DEFINE_RES_MEM(gmadr_base,
3072 						 ggtt->mappable_end);
3073 
3074 	ggtt->do_idle_maps = needs_idle_maps(dev_priv);
3075 	ggtt->vm.insert_page = i915_ggtt_insert_page;
3076 	ggtt->vm.insert_entries = i915_ggtt_insert_entries;
3077 	ggtt->vm.clear_range = i915_ggtt_clear_range;
3078 	ggtt->vm.cleanup = i915_gmch_remove;
3079 
3080 	ggtt->invalidate = gmch_ggtt_invalidate;
3081 
3082 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
3083 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
3084 	ggtt->vm.vma_ops.set_pages   = ggtt_set_pages;
3085 	ggtt->vm.vma_ops.clear_pages = clear_pages;
3086 
3087 	if (unlikely(ggtt->do_idle_maps))
3088 		DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3089 
3090 	return 0;
3091 }
3092 
3093 static int ggtt_probe_hw(struct i915_ggtt *ggtt, struct intel_gt *gt)
3094 {
3095 	struct drm_i915_private *i915 = gt->i915;
3096 	int ret;
3097 
3098 	ggtt->vm.gt = gt;
3099 	ggtt->vm.i915 = i915;
3100 	ggtt->vm.dma = &i915->drm.pdev->dev;
3101 
3102 	if (INTEL_GEN(i915) <= 5)
3103 		ret = i915_gmch_probe(ggtt);
3104 	else if (INTEL_GEN(i915) < 8)
3105 		ret = gen6_gmch_probe(ggtt);
3106 	else
3107 		ret = gen8_gmch_probe(ggtt);
3108 	if (ret)
3109 		return ret;
3110 
3111 	if ((ggtt->vm.total - 1) >> 32) {
3112 		DRM_ERROR("We never expected a Global GTT with more than 32bits"
3113 			  " of address space! Found %lldM!\n",
3114 			  ggtt->vm.total >> 20);
3115 		ggtt->vm.total = 1ULL << 32;
3116 		ggtt->mappable_end =
3117 			min_t(u64, ggtt->mappable_end, ggtt->vm.total);
3118 	}
3119 
3120 	if (ggtt->mappable_end > ggtt->vm.total) {
3121 		DRM_ERROR("mappable aperture extends past end of GGTT,"
3122 			  " aperture=%pa, total=%llx\n",
3123 			  &ggtt->mappable_end, ggtt->vm.total);
3124 		ggtt->mappable_end = ggtt->vm.total;
3125 	}
3126 
3127 	/* GMADR is the PCI mmio aperture into the global GTT. */
3128 	DRM_DEBUG_DRIVER("GGTT size = %lluM\n", ggtt->vm.total >> 20);
3129 	DRM_DEBUG_DRIVER("GMADR size = %lluM\n", (u64)ggtt->mappable_end >> 20);
3130 	DRM_DEBUG_DRIVER("DSM size = %lluM\n",
3131 			 (u64)resource_size(&intel_graphics_stolen_res) >> 20);
3132 
3133 	return 0;
3134 }
3135 
3136 /**
3137  * i915_ggtt_probe_hw - Probe GGTT hardware location
3138  * @i915: i915 device
3139  */
3140 int i915_ggtt_probe_hw(struct drm_i915_private *i915)
3141 {
3142 	int ret;
3143 
3144 	ret = ggtt_probe_hw(&i915->ggtt, &i915->gt);
3145 	if (ret)
3146 		return ret;
3147 
3148 	if (intel_vtd_active())
3149 		DRM_INFO("VT-d active for gfx access\n");
3150 
3151 	return 0;
3152 }
3153 
3154 static int ggtt_init_hw(struct i915_ggtt *ggtt)
3155 {
3156 	struct drm_i915_private *i915 = ggtt->vm.i915;
3157 	int ret = 0;
3158 
3159 	mutex_lock(&i915->drm.struct_mutex);
3160 
3161 	i915_address_space_init(&ggtt->vm, VM_CLASS_GGTT);
3162 
3163 	ggtt->vm.is_ggtt = true;
3164 
3165 	/* Only VLV supports read-only GGTT mappings */
3166 	ggtt->vm.has_read_only = IS_VALLEYVIEW(i915);
3167 
3168 	if (!HAS_LLC(i915) && !HAS_PPGTT(i915))
3169 		ggtt->vm.mm.color_adjust = i915_gtt_color_adjust;
3170 
3171 	if (!io_mapping_init_wc(&ggtt->iomap,
3172 				ggtt->gmadr.start,
3173 				ggtt->mappable_end)) {
3174 		ggtt->vm.cleanup(&ggtt->vm);
3175 		ret = -EIO;
3176 		goto out;
3177 	}
3178 
3179 	ggtt->mtrr = arch_phys_wc_add(ggtt->gmadr.start, ggtt->mappable_end);
3180 
3181 	i915_ggtt_init_fences(ggtt);
3182 
3183 out:
3184 	mutex_unlock(&i915->drm.struct_mutex);
3185 
3186 	return ret;
3187 }
3188 
3189 /**
3190  * i915_ggtt_init_hw - Initialize GGTT hardware
3191  * @dev_priv: i915 device
3192  */
3193 int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
3194 {
3195 	int ret;
3196 
3197 	stash_init(&dev_priv->mm.wc_stash);
3198 
3199 	/* Note that we use page colouring to enforce a guard page at the
3200 	 * end of the address space. This is required as the CS may prefetch
3201 	 * beyond the end of the batch buffer, across the page boundary,
3202 	 * and beyond the end of the GTT if we do not provide a guard.
3203 	 */
3204 	ret = ggtt_init_hw(&dev_priv->ggtt);
3205 	if (ret)
3206 		return ret;
3207 
3208 	/*
3209 	 * Initialise stolen early so that we may reserve preallocated
3210 	 * objects for the BIOS to KMS transition.
3211 	 */
3212 	ret = i915_gem_init_stolen(dev_priv);
3213 	if (ret)
3214 		goto out_gtt_cleanup;
3215 
3216 	return 0;
3217 
3218 out_gtt_cleanup:
3219 	dev_priv->ggtt.vm.cleanup(&dev_priv->ggtt.vm);
3220 	return ret;
3221 }
3222 
3223 int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
3224 {
3225 	if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
3226 		return -EIO;
3227 
3228 	return 0;
3229 }
3230 
3231 void i915_ggtt_enable_guc(struct i915_ggtt *ggtt)
3232 {
3233 	GEM_BUG_ON(ggtt->invalidate != gen6_ggtt_invalidate);
3234 
3235 	ggtt->invalidate = guc_ggtt_invalidate;
3236 
3237 	ggtt->invalidate(ggtt);
3238 }
3239 
3240 void i915_ggtt_disable_guc(struct i915_ggtt *ggtt)
3241 {
3242 	/* XXX Temporary pardon for error unload */
3243 	if (ggtt->invalidate == gen6_ggtt_invalidate)
3244 		return;
3245 
3246 	/* We should only be called after i915_ggtt_enable_guc() */
3247 	GEM_BUG_ON(ggtt->invalidate != guc_ggtt_invalidate);
3248 
3249 	ggtt->invalidate = gen6_ggtt_invalidate;
3250 
3251 	ggtt->invalidate(ggtt);
3252 }
3253 
3254 static void ggtt_restore_mappings(struct i915_ggtt *ggtt)
3255 {
3256 	struct i915_vma *vma, *vn;
3257 
3258 	intel_gt_check_and_clear_faults(ggtt->vm.gt);
3259 
3260 	mutex_lock(&ggtt->vm.mutex);
3261 
3262 	/* First fill our portion of the GTT with scratch pages */
3263 	ggtt->vm.clear_range(&ggtt->vm, 0, ggtt->vm.total);
3264 	ggtt->vm.closed = true; /* skip rewriting PTE on VMA unbind */
3265 
3266 	/* clflush objects bound into the GGTT and rebind them. */
3267 	list_for_each_entry_safe(vma, vn, &ggtt->vm.bound_list, vm_link) {
3268 		struct drm_i915_gem_object *obj = vma->obj;
3269 
3270 		if (!(vma->flags & I915_VMA_GLOBAL_BIND))
3271 			continue;
3272 
3273 		mutex_unlock(&ggtt->vm.mutex);
3274 
3275 		if (!i915_vma_unbind(vma))
3276 			goto lock;
3277 
3278 		WARN_ON(i915_vma_bind(vma,
3279 				      obj ? obj->cache_level : 0,
3280 				      PIN_UPDATE));
3281 		if (obj) {
3282 			i915_gem_object_lock(obj);
3283 			WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
3284 			i915_gem_object_unlock(obj);
3285 		}
3286 
3287 lock:
3288 		mutex_lock(&ggtt->vm.mutex);
3289 	}
3290 
3291 	ggtt->vm.closed = false;
3292 	ggtt->invalidate(ggtt);
3293 
3294 	mutex_unlock(&ggtt->vm.mutex);
3295 }
3296 
3297 void i915_gem_restore_gtt_mappings(struct drm_i915_private *i915)
3298 {
3299 	ggtt_restore_mappings(&i915->ggtt);
3300 
3301 	if (INTEL_GEN(i915) >= 8)
3302 		setup_private_pat(i915);
3303 }
3304 
3305 static struct scatterlist *
3306 rotate_pages(struct drm_i915_gem_object *obj, unsigned int offset,
3307 	     unsigned int width, unsigned int height,
3308 	     unsigned int stride,
3309 	     struct sg_table *st, struct scatterlist *sg)
3310 {
3311 	unsigned int column, row;
3312 	unsigned int src_idx;
3313 
3314 	for (column = 0; column < width; column++) {
3315 		src_idx = stride * (height - 1) + column + offset;
3316 		for (row = 0; row < height; row++) {
3317 			st->nents++;
3318 			/* We don't need the pages, but need to initialize
3319 			 * the entries so the sg list can be happily traversed.
3320 			 * The only thing we need are DMA addresses.
3321 			 */
3322 			sg_set_page(sg, NULL, I915_GTT_PAGE_SIZE, 0);
3323 			sg_dma_address(sg) =
3324 				i915_gem_object_get_dma_address(obj, src_idx);
3325 			sg_dma_len(sg) = I915_GTT_PAGE_SIZE;
3326 			sg = sg_next(sg);
3327 			src_idx -= stride;
3328 		}
3329 	}
3330 
3331 	return sg;
3332 }
3333 
3334 static noinline struct sg_table *
3335 intel_rotate_pages(struct intel_rotation_info *rot_info,
3336 		   struct drm_i915_gem_object *obj)
3337 {
3338 	unsigned int size = intel_rotation_info_size(rot_info);
3339 	struct sg_table *st;
3340 	struct scatterlist *sg;
3341 	int ret = -ENOMEM;
3342 	int i;
3343 
3344 	/* Allocate target SG list. */
3345 	st = kmalloc(sizeof(*st), GFP_KERNEL);
3346 	if (!st)
3347 		goto err_st_alloc;
3348 
3349 	ret = sg_alloc_table(st, size, GFP_KERNEL);
3350 	if (ret)
3351 		goto err_sg_alloc;
3352 
3353 	st->nents = 0;
3354 	sg = st->sgl;
3355 
3356 	for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3357 		sg = rotate_pages(obj, rot_info->plane[i].offset,
3358 				  rot_info->plane[i].width, rot_info->plane[i].height,
3359 				  rot_info->plane[i].stride, st, sg);
3360 	}
3361 
3362 	return st;
3363 
3364 err_sg_alloc:
3365 	kfree(st);
3366 err_st_alloc:
3367 
3368 	DRM_DEBUG_DRIVER("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3369 			 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3370 
3371 	return ERR_PTR(ret);
3372 }
3373 
3374 static struct scatterlist *
3375 remap_pages(struct drm_i915_gem_object *obj, unsigned int offset,
3376 	    unsigned int width, unsigned int height,
3377 	    unsigned int stride,
3378 	    struct sg_table *st, struct scatterlist *sg)
3379 {
3380 	unsigned int row;
3381 
3382 	for (row = 0; row < height; row++) {
3383 		unsigned int left = width * I915_GTT_PAGE_SIZE;
3384 
3385 		while (left) {
3386 			dma_addr_t addr;
3387 			unsigned int length;
3388 
3389 			/* We don't need the pages, but need to initialize
3390 			 * the entries so the sg list can be happily traversed.
3391 			 * The only thing we need are DMA addresses.
3392 			 */
3393 
3394 			addr = i915_gem_object_get_dma_address_len(obj, offset, &length);
3395 
3396 			length = min(left, length);
3397 
3398 			st->nents++;
3399 
3400 			sg_set_page(sg, NULL, length, 0);
3401 			sg_dma_address(sg) = addr;
3402 			sg_dma_len(sg) = length;
3403 			sg = sg_next(sg);
3404 
3405 			offset += length / I915_GTT_PAGE_SIZE;
3406 			left -= length;
3407 		}
3408 
3409 		offset += stride - width;
3410 	}
3411 
3412 	return sg;
3413 }
3414 
3415 static noinline struct sg_table *
3416 intel_remap_pages(struct intel_remapped_info *rem_info,
3417 		  struct drm_i915_gem_object *obj)
3418 {
3419 	unsigned int size = intel_remapped_info_size(rem_info);
3420 	struct sg_table *st;
3421 	struct scatterlist *sg;
3422 	int ret = -ENOMEM;
3423 	int i;
3424 
3425 	/* Allocate target SG list. */
3426 	st = kmalloc(sizeof(*st), GFP_KERNEL);
3427 	if (!st)
3428 		goto err_st_alloc;
3429 
3430 	ret = sg_alloc_table(st, size, GFP_KERNEL);
3431 	if (ret)
3432 		goto err_sg_alloc;
3433 
3434 	st->nents = 0;
3435 	sg = st->sgl;
3436 
3437 	for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++) {
3438 		sg = remap_pages(obj, rem_info->plane[i].offset,
3439 				 rem_info->plane[i].width, rem_info->plane[i].height,
3440 				 rem_info->plane[i].stride, st, sg);
3441 	}
3442 
3443 	i915_sg_trim(st);
3444 
3445 	return st;
3446 
3447 err_sg_alloc:
3448 	kfree(st);
3449 err_st_alloc:
3450 
3451 	DRM_DEBUG_DRIVER("Failed to create remapped mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3452 			 obj->base.size, rem_info->plane[0].width, rem_info->plane[0].height, size);
3453 
3454 	return ERR_PTR(ret);
3455 }
3456 
3457 static noinline struct sg_table *
3458 intel_partial_pages(const struct i915_ggtt_view *view,
3459 		    struct drm_i915_gem_object *obj)
3460 {
3461 	struct sg_table *st;
3462 	struct scatterlist *sg, *iter;
3463 	unsigned int count = view->partial.size;
3464 	unsigned int offset;
3465 	int ret = -ENOMEM;
3466 
3467 	st = kmalloc(sizeof(*st), GFP_KERNEL);
3468 	if (!st)
3469 		goto err_st_alloc;
3470 
3471 	ret = sg_alloc_table(st, count, GFP_KERNEL);
3472 	if (ret)
3473 		goto err_sg_alloc;
3474 
3475 	iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
3476 	GEM_BUG_ON(!iter);
3477 
3478 	sg = st->sgl;
3479 	st->nents = 0;
3480 	do {
3481 		unsigned int len;
3482 
3483 		len = min(iter->length - (offset << PAGE_SHIFT),
3484 			  count << PAGE_SHIFT);
3485 		sg_set_page(sg, NULL, len, 0);
3486 		sg_dma_address(sg) =
3487 			sg_dma_address(iter) + (offset << PAGE_SHIFT);
3488 		sg_dma_len(sg) = len;
3489 
3490 		st->nents++;
3491 		count -= len >> PAGE_SHIFT;
3492 		if (count == 0) {
3493 			sg_mark_end(sg);
3494 			i915_sg_trim(st); /* Drop any unused tail entries. */
3495 
3496 			return st;
3497 		}
3498 
3499 		sg = __sg_next(sg);
3500 		iter = __sg_next(iter);
3501 		offset = 0;
3502 	} while (1);
3503 
3504 err_sg_alloc:
3505 	kfree(st);
3506 err_st_alloc:
3507 	return ERR_PTR(ret);
3508 }
3509 
3510 static int
3511 i915_get_ggtt_vma_pages(struct i915_vma *vma)
3512 {
3513 	int ret;
3514 
3515 	/* The vma->pages are only valid within the lifespan of the borrowed
3516 	 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3517 	 * must be the vma->pages. A simple rule is that vma->pages must only
3518 	 * be accessed when the obj->mm.pages are pinned.
3519 	 */
3520 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3521 
3522 	switch (vma->ggtt_view.type) {
3523 	default:
3524 		GEM_BUG_ON(vma->ggtt_view.type);
3525 		/* fall through */
3526 	case I915_GGTT_VIEW_NORMAL:
3527 		vma->pages = vma->obj->mm.pages;
3528 		return 0;
3529 
3530 	case I915_GGTT_VIEW_ROTATED:
3531 		vma->pages =
3532 			intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3533 		break;
3534 
3535 	case I915_GGTT_VIEW_REMAPPED:
3536 		vma->pages =
3537 			intel_remap_pages(&vma->ggtt_view.remapped, vma->obj);
3538 		break;
3539 
3540 	case I915_GGTT_VIEW_PARTIAL:
3541 		vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
3542 		break;
3543 	}
3544 
3545 	ret = 0;
3546 	if (IS_ERR(vma->pages)) {
3547 		ret = PTR_ERR(vma->pages);
3548 		vma->pages = NULL;
3549 		DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3550 			  vma->ggtt_view.type, ret);
3551 	}
3552 	return ret;
3553 }
3554 
3555 /**
3556  * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
3557  * @vm: the &struct i915_address_space
3558  * @node: the &struct drm_mm_node (typically i915_vma.mode)
3559  * @size: how much space to allocate inside the GTT,
3560  *        must be #I915_GTT_PAGE_SIZE aligned
3561  * @offset: where to insert inside the GTT,
3562  *          must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3563  *          (@offset + @size) must fit within the address space
3564  * @color: color to apply to node, if this node is not from a VMA,
3565  *         color must be #I915_COLOR_UNEVICTABLE
3566  * @flags: control search and eviction behaviour
3567  *
3568  * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3569  * the address space (using @size and @color). If the @node does not fit, it
3570  * tries to evict any overlapping nodes from the GTT, including any
3571  * neighbouring nodes if the colors do not match (to ensure guard pages between
3572  * differing domains). See i915_gem_evict_for_node() for the gory details
3573  * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3574  * evicting active overlapping objects, and any overlapping node that is pinned
3575  * or marked as unevictable will also result in failure.
3576  *
3577  * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3578  * asked to wait for eviction and interrupted.
3579  */
3580 int i915_gem_gtt_reserve(struct i915_address_space *vm,
3581 			 struct drm_mm_node *node,
3582 			 u64 size, u64 offset, unsigned long color,
3583 			 unsigned int flags)
3584 {
3585 	int err;
3586 
3587 	GEM_BUG_ON(!size);
3588 	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3589 	GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3590 	GEM_BUG_ON(range_overflows(offset, size, vm->total));
3591 	GEM_BUG_ON(vm == &vm->i915->ggtt.alias->vm);
3592 	GEM_BUG_ON(drm_mm_node_allocated(node));
3593 
3594 	node->size = size;
3595 	node->start = offset;
3596 	node->color = color;
3597 
3598 	err = drm_mm_reserve_node(&vm->mm, node);
3599 	if (err != -ENOSPC)
3600 		return err;
3601 
3602 	if (flags & PIN_NOEVICT)
3603 		return -ENOSPC;
3604 
3605 	err = i915_gem_evict_for_node(vm, node, flags);
3606 	if (err == 0)
3607 		err = drm_mm_reserve_node(&vm->mm, node);
3608 
3609 	return err;
3610 }
3611 
3612 static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3613 {
3614 	u64 range, addr;
3615 
3616 	GEM_BUG_ON(range_overflows(start, len, end));
3617 	GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3618 
3619 	range = round_down(end - len, align) - round_up(start, align);
3620 	if (range) {
3621 		if (sizeof(unsigned long) == sizeof(u64)) {
3622 			addr = get_random_long();
3623 		} else {
3624 			addr = get_random_int();
3625 			if (range > U32_MAX) {
3626 				addr <<= 32;
3627 				addr |= get_random_int();
3628 			}
3629 		}
3630 		div64_u64_rem(addr, range, &addr);
3631 		start += addr;
3632 	}
3633 
3634 	return round_up(start, align);
3635 }
3636 
3637 /**
3638  * i915_gem_gtt_insert - insert a node into an address_space (GTT)
3639  * @vm: the &struct i915_address_space
3640  * @node: the &struct drm_mm_node (typically i915_vma.node)
3641  * @size: how much space to allocate inside the GTT,
3642  *        must be #I915_GTT_PAGE_SIZE aligned
3643  * @alignment: required alignment of starting offset, may be 0 but
3644  *             if specified, this must be a power-of-two and at least
3645  *             #I915_GTT_MIN_ALIGNMENT
3646  * @color: color to apply to node
3647  * @start: start of any range restriction inside GTT (0 for all),
3648  *         must be #I915_GTT_PAGE_SIZE aligned
3649  * @end: end of any range restriction inside GTT (U64_MAX for all),
3650  *       must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3651  * @flags: control search and eviction behaviour
3652  *
3653  * i915_gem_gtt_insert() first searches for an available hole into which
3654  * is can insert the node. The hole address is aligned to @alignment and
3655  * its @size must then fit entirely within the [@start, @end] bounds. The
3656  * nodes on either side of the hole must match @color, or else a guard page
3657  * will be inserted between the two nodes (or the node evicted). If no
3658  * suitable hole is found, first a victim is randomly selected and tested
3659  * for eviction, otherwise then the LRU list of objects within the GTT
3660  * is scanned to find the first set of replacement nodes to create the hole.
3661  * Those old overlapping nodes are evicted from the GTT (and so must be
3662  * rebound before any future use). Any node that is currently pinned cannot
3663  * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3664  * active and #PIN_NONBLOCK is specified, that node is also skipped when
3665  * searching for an eviction candidate. See i915_gem_evict_something() for
3666  * the gory details on the eviction algorithm.
3667  *
3668  * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3669  * asked to wait for eviction and interrupted.
3670  */
3671 int i915_gem_gtt_insert(struct i915_address_space *vm,
3672 			struct drm_mm_node *node,
3673 			u64 size, u64 alignment, unsigned long color,
3674 			u64 start, u64 end, unsigned int flags)
3675 {
3676 	enum drm_mm_insert_mode mode;
3677 	u64 offset;
3678 	int err;
3679 
3680 	lockdep_assert_held(&vm->i915->drm.struct_mutex);
3681 	GEM_BUG_ON(!size);
3682 	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3683 	GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3684 	GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3685 	GEM_BUG_ON(start >= end);
3686 	GEM_BUG_ON(start > 0  && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3687 	GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
3688 	GEM_BUG_ON(vm == &vm->i915->ggtt.alias->vm);
3689 	GEM_BUG_ON(drm_mm_node_allocated(node));
3690 
3691 	if (unlikely(range_overflows(start, size, end)))
3692 		return -ENOSPC;
3693 
3694 	if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3695 		return -ENOSPC;
3696 
3697 	mode = DRM_MM_INSERT_BEST;
3698 	if (flags & PIN_HIGH)
3699 		mode = DRM_MM_INSERT_HIGHEST;
3700 	if (flags & PIN_MAPPABLE)
3701 		mode = DRM_MM_INSERT_LOW;
3702 
3703 	/* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3704 	 * so we know that we always have a minimum alignment of 4096.
3705 	 * The drm_mm range manager is optimised to return results
3706 	 * with zero alignment, so where possible use the optimal
3707 	 * path.
3708 	 */
3709 	BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3710 	if (alignment <= I915_GTT_MIN_ALIGNMENT)
3711 		alignment = 0;
3712 
3713 	err = drm_mm_insert_node_in_range(&vm->mm, node,
3714 					  size, alignment, color,
3715 					  start, end, mode);
3716 	if (err != -ENOSPC)
3717 		return err;
3718 
3719 	if (mode & DRM_MM_INSERT_ONCE) {
3720 		err = drm_mm_insert_node_in_range(&vm->mm, node,
3721 						  size, alignment, color,
3722 						  start, end,
3723 						  DRM_MM_INSERT_BEST);
3724 		if (err != -ENOSPC)
3725 			return err;
3726 	}
3727 
3728 	if (flags & PIN_NOEVICT)
3729 		return -ENOSPC;
3730 
3731 	/* No free space, pick a slot at random.
3732 	 *
3733 	 * There is a pathological case here using a GTT shared between
3734 	 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3735 	 *
3736 	 *    |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3737 	 *         (64k objects)             (448k objects)
3738 	 *
3739 	 * Now imagine that the eviction LRU is ordered top-down (just because
3740 	 * pathology meets real life), and that we need to evict an object to
3741 	 * make room inside the aperture. The eviction scan then has to walk
3742 	 * the 448k list before it finds one within range. And now imagine that
3743 	 * it has to search for a new hole between every byte inside the memcpy,
3744 	 * for several simultaneous clients.
3745 	 *
3746 	 * On a full-ppgtt system, if we have run out of available space, there
3747 	 * will be lots and lots of objects in the eviction list! Again,
3748 	 * searching that LRU list may be slow if we are also applying any
3749 	 * range restrictions (e.g. restriction to low 4GiB) and so, for
3750 	 * simplicity and similarilty between different GTT, try the single
3751 	 * random replacement first.
3752 	 */
3753 	offset = random_offset(start, end,
3754 			       size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3755 	err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3756 	if (err != -ENOSPC)
3757 		return err;
3758 
3759 	/* Randomly selected placement is pinned, do a search */
3760 	err = i915_gem_evict_something(vm, size, alignment, color,
3761 				       start, end, flags);
3762 	if (err)
3763 		return err;
3764 
3765 	return drm_mm_insert_node_in_range(&vm->mm, node,
3766 					   size, alignment, color,
3767 					   start, end, DRM_MM_INSERT_EVICT);
3768 }
3769 
3770 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3771 #include "selftests/mock_gtt.c"
3772 #include "selftests/i915_gem_gtt.c"
3773 #endif
3774