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 #include <asm/smp.h>
36 
37 #include <drm/i915_drm.h>
38 
39 #include "display/intel_frontbuffer.h"
40 #include "gt/intel_gt.h"
41 
42 #include "i915_drv.h"
43 #include "i915_scatterlist.h"
44 #include "i915_trace.h"
45 #include "i915_vgpu.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.gt->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.gt->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 	/* Each thread pre-pins the pd, and we may have a thread per pde. */
776 	GEM_BUG_ON(atomic_read(px_used(pd)) > 2 * ARRAY_SIZE(pd->entry));
777 
778 	atomic_inc(px_used(pd));
779 	pd->entry[idx] = to;
780 	write_dma_entry(px_base(pd), idx, encode(to->daddr, I915_CACHE_LLC));
781 }
782 
783 #define set_pd_entry(pd, idx, to) \
784 	__set_pd_entry((pd), (idx), px_base(to), gen8_pde_encode)
785 
786 static inline void
787 clear_pd_entry(struct i915_page_directory * const pd,
788 	       const unsigned short idx,
789 	       const struct i915_page_scratch * const scratch)
790 {
791 	GEM_BUG_ON(atomic_read(px_used(pd)) == 0);
792 
793 	write_dma_entry(px_base(pd), idx, scratch->encode);
794 	pd->entry[idx] = NULL;
795 	atomic_dec(px_used(pd));
796 }
797 
798 static bool
799 release_pd_entry(struct i915_page_directory * const pd,
800 		 const unsigned short idx,
801 		 struct i915_page_table * const pt,
802 		 const struct i915_page_scratch * const scratch)
803 {
804 	bool free = false;
805 
806 	if (atomic_add_unless(&pt->used, -1, 1))
807 		return false;
808 
809 	spin_lock(&pd->lock);
810 	if (atomic_dec_and_test(&pt->used)) {
811 		clear_pd_entry(pd, idx, scratch);
812 		free = true;
813 	}
814 	spin_unlock(&pd->lock);
815 
816 	return free;
817 }
818 
819 /*
820  * PDE TLBs are a pain to invalidate on GEN8+. When we modify
821  * the page table structures, we mark them dirty so that
822  * context switching/execlist queuing code takes extra steps
823  * to ensure that tlbs are flushed.
824  */
825 static void mark_tlbs_dirty(struct i915_ppgtt *ppgtt)
826 {
827 	ppgtt->pd_dirty_engines = ALL_ENGINES;
828 }
829 
830 static int gen8_ppgtt_notify_vgt(struct i915_ppgtt *ppgtt, bool create)
831 {
832 	struct i915_address_space *vm = &ppgtt->vm;
833 	struct drm_i915_private *dev_priv = vm->i915;
834 	enum vgt_g2v_type msg;
835 	int i;
836 
837 	if (create)
838 		atomic_inc(px_used(ppgtt->pd)); /* never remove */
839 	else
840 		atomic_dec(px_used(ppgtt->pd));
841 
842 	if (i915_vm_is_4lvl(vm)) {
843 		const u64 daddr = px_dma(ppgtt->pd);
844 
845 		I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
846 		I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
847 
848 		msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
849 				VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
850 	} else {
851 		for (i = 0; i < GEN8_3LVL_PDPES; i++) {
852 			const u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
853 
854 			I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
855 			I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
856 		}
857 
858 		msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
859 				VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
860 	}
861 
862 	I915_WRITE(vgtif_reg(g2v_notify), msg);
863 
864 	return 0;
865 }
866 
867 /* Index shifts into the pagetable are offset by GEN8_PTE_SHIFT [12] */
868 #define GEN8_PAGE_SIZE (SZ_4K) /* page and page-directory sizes are the same */
869 #define GEN8_PTE_SHIFT (ilog2(GEN8_PAGE_SIZE))
870 #define GEN8_PDES (GEN8_PAGE_SIZE / sizeof(u64))
871 #define gen8_pd_shift(lvl) ((lvl) * ilog2(GEN8_PDES))
872 #define gen8_pd_index(i, lvl) i915_pde_index((i), gen8_pd_shift(lvl))
873 #define __gen8_pte_shift(lvl) (GEN8_PTE_SHIFT + gen8_pd_shift(lvl))
874 #define __gen8_pte_index(a, lvl) i915_pde_index((a), __gen8_pte_shift(lvl))
875 
876 static inline unsigned int
877 gen8_pd_range(u64 start, u64 end, int lvl, unsigned int *idx)
878 {
879 	const int shift = gen8_pd_shift(lvl);
880 	const u64 mask = ~0ull << gen8_pd_shift(lvl + 1);
881 
882 	GEM_BUG_ON(start >= end);
883 	end += ~mask >> gen8_pd_shift(1);
884 
885 	*idx = i915_pde_index(start, shift);
886 	if ((start ^ end) & mask)
887 		return GEN8_PDES - *idx;
888 	else
889 		return i915_pde_index(end, shift) - *idx;
890 }
891 
892 static inline bool gen8_pd_contains(u64 start, u64 end, int lvl)
893 {
894 	const u64 mask = ~0ull << gen8_pd_shift(lvl + 1);
895 
896 	GEM_BUG_ON(start >= end);
897 	return (start ^ end) & mask && (start & ~mask) == 0;
898 }
899 
900 static inline unsigned int gen8_pt_count(u64 start, u64 end)
901 {
902 	GEM_BUG_ON(start >= end);
903 	if ((start ^ end) >> gen8_pd_shift(1))
904 		return GEN8_PDES - (start & (GEN8_PDES - 1));
905 	else
906 		return end - start;
907 }
908 
909 static inline unsigned int gen8_pd_top_count(const struct i915_address_space *vm)
910 {
911 	unsigned int shift = __gen8_pte_shift(vm->top);
912 	return (vm->total + (1ull << shift) - 1) >> shift;
913 }
914 
915 static inline struct i915_page_directory *
916 gen8_pdp_for_page_index(struct i915_address_space * const vm, const u64 idx)
917 {
918 	struct i915_ppgtt * const ppgtt = i915_vm_to_ppgtt(vm);
919 
920 	if (vm->top == 2)
921 		return ppgtt->pd;
922 	else
923 		return i915_pd_entry(ppgtt->pd, gen8_pd_index(idx, vm->top));
924 }
925 
926 static inline struct i915_page_directory *
927 gen8_pdp_for_page_address(struct i915_address_space * const vm, const u64 addr)
928 {
929 	return gen8_pdp_for_page_index(vm, addr >> GEN8_PTE_SHIFT);
930 }
931 
932 static void __gen8_ppgtt_cleanup(struct i915_address_space *vm,
933 				 struct i915_page_directory *pd,
934 				 int count, int lvl)
935 {
936 	if (lvl) {
937 		void **pde = pd->entry;
938 
939 		do {
940 			if (!*pde)
941 				continue;
942 
943 			__gen8_ppgtt_cleanup(vm, *pde, GEN8_PDES, lvl - 1);
944 		} while (pde++, --count);
945 	}
946 
947 	free_px(vm, pd);
948 }
949 
950 static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
951 {
952 	struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
953 
954 	if (intel_vgpu_active(vm->i915))
955 		gen8_ppgtt_notify_vgt(ppgtt, false);
956 
957 	__gen8_ppgtt_cleanup(vm, ppgtt->pd, gen8_pd_top_count(vm), vm->top);
958 	free_scratch(vm);
959 }
960 
961 static u64 __gen8_ppgtt_clear(struct i915_address_space * const vm,
962 			      struct i915_page_directory * const pd,
963 			      u64 start, const u64 end, int lvl)
964 {
965 	const struct i915_page_scratch * const scratch = &vm->scratch[lvl];
966 	unsigned int idx, len;
967 
968 	GEM_BUG_ON(end > vm->total >> GEN8_PTE_SHIFT);
969 
970 	len = gen8_pd_range(start, end, lvl--, &idx);
971 	DBG("%s(%p):{ lvl:%d, start:%llx, end:%llx, idx:%d, len:%d, used:%d }\n",
972 	    __func__, vm, lvl + 1, start, end,
973 	    idx, len, atomic_read(px_used(pd)));
974 	GEM_BUG_ON(!len || len >= atomic_read(px_used(pd)));
975 
976 	do {
977 		struct i915_page_table *pt = pd->entry[idx];
978 
979 		if (atomic_fetch_inc(&pt->used) >> gen8_pd_shift(1) &&
980 		    gen8_pd_contains(start, end, lvl)) {
981 			DBG("%s(%p):{ lvl:%d, idx:%d, start:%llx, end:%llx } removing pd\n",
982 			    __func__, vm, lvl + 1, idx, start, end);
983 			clear_pd_entry(pd, idx, scratch);
984 			__gen8_ppgtt_cleanup(vm, as_pd(pt), I915_PDES, lvl);
985 			start += (u64)I915_PDES << gen8_pd_shift(lvl);
986 			continue;
987 		}
988 
989 		if (lvl) {
990 			start = __gen8_ppgtt_clear(vm, as_pd(pt),
991 						   start, end, lvl);
992 		} else {
993 			unsigned int count;
994 			u64 *vaddr;
995 
996 			count = gen8_pt_count(start, end);
997 			DBG("%s(%p):{ lvl:%d, start:%llx, end:%llx, idx:%d, len:%d, used:%d } removing pte\n",
998 			    __func__, vm, lvl, start, end,
999 			    gen8_pd_index(start, 0), count,
1000 			    atomic_read(&pt->used));
1001 			GEM_BUG_ON(!count || count >= atomic_read(&pt->used));
1002 
1003 			vaddr = kmap_atomic_px(pt);
1004 			memset64(vaddr + gen8_pd_index(start, 0),
1005 				 vm->scratch[0].encode,
1006 				 count);
1007 			kunmap_atomic(vaddr);
1008 
1009 			atomic_sub(count, &pt->used);
1010 			start += count;
1011 		}
1012 
1013 		if (release_pd_entry(pd, idx, pt, scratch))
1014 			free_px(vm, pt);
1015 	} while (idx++, --len);
1016 
1017 	return start;
1018 }
1019 
1020 static void gen8_ppgtt_clear(struct i915_address_space *vm,
1021 			     u64 start, u64 length)
1022 {
1023 	GEM_BUG_ON(!IS_ALIGNED(start, BIT_ULL(GEN8_PTE_SHIFT)));
1024 	GEM_BUG_ON(!IS_ALIGNED(length, BIT_ULL(GEN8_PTE_SHIFT)));
1025 	GEM_BUG_ON(range_overflows(start, length, vm->total));
1026 
1027 	start >>= GEN8_PTE_SHIFT;
1028 	length >>= GEN8_PTE_SHIFT;
1029 	GEM_BUG_ON(length == 0);
1030 
1031 	__gen8_ppgtt_clear(vm, i915_vm_to_ppgtt(vm)->pd,
1032 			   start, start + length, vm->top);
1033 }
1034 
1035 static int __gen8_ppgtt_alloc(struct i915_address_space * const vm,
1036 			      struct i915_page_directory * const pd,
1037 			      u64 * const start, const u64 end, int lvl)
1038 {
1039 	const struct i915_page_scratch * const scratch = &vm->scratch[lvl];
1040 	struct i915_page_table *alloc = NULL;
1041 	unsigned int idx, len;
1042 	int ret = 0;
1043 
1044 	GEM_BUG_ON(end > vm->total >> GEN8_PTE_SHIFT);
1045 
1046 	len = gen8_pd_range(*start, end, lvl--, &idx);
1047 	DBG("%s(%p):{ lvl:%d, start:%llx, end:%llx, idx:%d, len:%d, used:%d }\n",
1048 	    __func__, vm, lvl + 1, *start, end,
1049 	    idx, len, atomic_read(px_used(pd)));
1050 	GEM_BUG_ON(!len || (idx + len - 1) >> gen8_pd_shift(1));
1051 
1052 	spin_lock(&pd->lock);
1053 	GEM_BUG_ON(!atomic_read(px_used(pd))); /* Must be pinned! */
1054 	do {
1055 		struct i915_page_table *pt = pd->entry[idx];
1056 
1057 		if (!pt) {
1058 			spin_unlock(&pd->lock);
1059 
1060 			DBG("%s(%p):{ lvl:%d, idx:%d } allocating new tree\n",
1061 			    __func__, vm, lvl + 1, idx);
1062 
1063 			pt = fetch_and_zero(&alloc);
1064 			if (lvl) {
1065 				if (!pt) {
1066 					pt = &alloc_pd(vm)->pt;
1067 					if (IS_ERR(pt)) {
1068 						ret = PTR_ERR(pt);
1069 						goto out;
1070 					}
1071 				}
1072 
1073 				fill_px(pt, vm->scratch[lvl].encode);
1074 			} else {
1075 				if (!pt) {
1076 					pt = alloc_pt(vm);
1077 					if (IS_ERR(pt)) {
1078 						ret = PTR_ERR(pt);
1079 						goto out;
1080 					}
1081 				}
1082 
1083 				if (intel_vgpu_active(vm->i915) ||
1084 				    gen8_pt_count(*start, end) < I915_PDES)
1085 					fill_px(pt, vm->scratch[lvl].encode);
1086 			}
1087 
1088 			spin_lock(&pd->lock);
1089 			if (likely(!pd->entry[idx]))
1090 				set_pd_entry(pd, idx, pt);
1091 			else
1092 				alloc = pt, pt = pd->entry[idx];
1093 		}
1094 
1095 		if (lvl) {
1096 			atomic_inc(&pt->used);
1097 			spin_unlock(&pd->lock);
1098 
1099 			ret = __gen8_ppgtt_alloc(vm, as_pd(pt),
1100 						 start, end, lvl);
1101 			if (unlikely(ret)) {
1102 				if (release_pd_entry(pd, idx, pt, scratch))
1103 					free_px(vm, pt);
1104 				goto out;
1105 			}
1106 
1107 			spin_lock(&pd->lock);
1108 			atomic_dec(&pt->used);
1109 			GEM_BUG_ON(!atomic_read(&pt->used));
1110 		} else {
1111 			unsigned int count = gen8_pt_count(*start, end);
1112 
1113 			DBG("%s(%p):{ lvl:%d, start:%llx, end:%llx, idx:%d, len:%d, used:%d } inserting pte\n",
1114 			    __func__, vm, lvl, *start, end,
1115 			    gen8_pd_index(*start, 0), count,
1116 			    atomic_read(&pt->used));
1117 
1118 			atomic_add(count, &pt->used);
1119 			/* All other pdes may be simultaneously removed */
1120 			GEM_BUG_ON(atomic_read(&pt->used) > 2 * I915_PDES);
1121 			*start += count;
1122 		}
1123 	} while (idx++, --len);
1124 	spin_unlock(&pd->lock);
1125 out:
1126 	if (alloc)
1127 		free_px(vm, alloc);
1128 	return ret;
1129 }
1130 
1131 static int gen8_ppgtt_alloc(struct i915_address_space *vm,
1132 			    u64 start, u64 length)
1133 {
1134 	u64 from;
1135 	int err;
1136 
1137 	GEM_BUG_ON(!IS_ALIGNED(start, BIT_ULL(GEN8_PTE_SHIFT)));
1138 	GEM_BUG_ON(!IS_ALIGNED(length, BIT_ULL(GEN8_PTE_SHIFT)));
1139 	GEM_BUG_ON(range_overflows(start, length, vm->total));
1140 
1141 	start >>= GEN8_PTE_SHIFT;
1142 	length >>= GEN8_PTE_SHIFT;
1143 	GEM_BUG_ON(length == 0);
1144 	from = start;
1145 
1146 	err = __gen8_ppgtt_alloc(vm, i915_vm_to_ppgtt(vm)->pd,
1147 				 &start, start + length, vm->top);
1148 	if (unlikely(err && from != start))
1149 		__gen8_ppgtt_clear(vm, i915_vm_to_ppgtt(vm)->pd,
1150 				   from, start, vm->top);
1151 
1152 	return err;
1153 }
1154 
1155 static inline struct sgt_dma {
1156 	struct scatterlist *sg;
1157 	dma_addr_t dma, max;
1158 } sgt_dma(struct i915_vma *vma) {
1159 	struct scatterlist *sg = vma->pages->sgl;
1160 	dma_addr_t addr = sg_dma_address(sg);
1161 	return (struct sgt_dma) { sg, addr, addr + sg->length };
1162 }
1163 
1164 static __always_inline u64
1165 gen8_ppgtt_insert_pte(struct i915_ppgtt *ppgtt,
1166 		      struct i915_page_directory *pdp,
1167 		      struct sgt_dma *iter,
1168 		      u64 idx,
1169 		      enum i915_cache_level cache_level,
1170 		      u32 flags)
1171 {
1172 	struct i915_page_directory *pd;
1173 	const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level, flags);
1174 	gen8_pte_t *vaddr;
1175 
1176 	pd = i915_pd_entry(pdp, gen8_pd_index(idx, 2));
1177 	vaddr = kmap_atomic_px(i915_pt_entry(pd, gen8_pd_index(idx, 1)));
1178 	do {
1179 		vaddr[gen8_pd_index(idx, 0)] = pte_encode | iter->dma;
1180 
1181 		iter->dma += I915_GTT_PAGE_SIZE;
1182 		if (iter->dma >= iter->max) {
1183 			iter->sg = __sg_next(iter->sg);
1184 			if (!iter->sg) {
1185 				idx = 0;
1186 				break;
1187 			}
1188 
1189 			iter->dma = sg_dma_address(iter->sg);
1190 			iter->max = iter->dma + iter->sg->length;
1191 		}
1192 
1193 		if (gen8_pd_index(++idx, 0) == 0) {
1194 			if (gen8_pd_index(idx, 1) == 0) {
1195 				/* Limited by sg length for 3lvl */
1196 				if (gen8_pd_index(idx, 2) == 0)
1197 					break;
1198 
1199 				pd = pdp->entry[gen8_pd_index(idx, 2)];
1200 			}
1201 
1202 			kunmap_atomic(vaddr);
1203 			vaddr = kmap_atomic_px(i915_pt_entry(pd, gen8_pd_index(idx, 1)));
1204 		}
1205 	} while (1);
1206 	kunmap_atomic(vaddr);
1207 
1208 	return idx;
1209 }
1210 
1211 static void gen8_ppgtt_insert_huge(struct i915_vma *vma,
1212 				   struct sgt_dma *iter,
1213 				   enum i915_cache_level cache_level,
1214 				   u32 flags)
1215 {
1216 	const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level, flags);
1217 	u64 start = vma->node.start;
1218 	dma_addr_t rem = iter->sg->length;
1219 
1220 	GEM_BUG_ON(!i915_vm_is_4lvl(vma->vm));
1221 
1222 	do {
1223 		struct i915_page_directory * const pdp =
1224 			gen8_pdp_for_page_address(vma->vm, start);
1225 		struct i915_page_directory * const pd =
1226 			i915_pd_entry(pdp, __gen8_pte_index(start, 2));
1227 		gen8_pte_t encode = pte_encode;
1228 		unsigned int maybe_64K = -1;
1229 		unsigned int page_size;
1230 		gen8_pte_t *vaddr;
1231 		u16 index;
1232 
1233 		if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_2M &&
1234 		    IS_ALIGNED(iter->dma, I915_GTT_PAGE_SIZE_2M) &&
1235 		    rem >= I915_GTT_PAGE_SIZE_2M &&
1236 		    !__gen8_pte_index(start, 0)) {
1237 			index = __gen8_pte_index(start, 1);
1238 			encode |= GEN8_PDE_PS_2M;
1239 			page_size = I915_GTT_PAGE_SIZE_2M;
1240 
1241 			vaddr = kmap_atomic_px(pd);
1242 		} else {
1243 			struct i915_page_table *pt =
1244 				i915_pt_entry(pd, __gen8_pte_index(start, 1));
1245 
1246 			index = __gen8_pte_index(start, 0);
1247 			page_size = I915_GTT_PAGE_SIZE;
1248 
1249 			if (!index &&
1250 			    vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K &&
1251 			    IS_ALIGNED(iter->dma, I915_GTT_PAGE_SIZE_64K) &&
1252 			    (IS_ALIGNED(rem, I915_GTT_PAGE_SIZE_64K) ||
1253 			     rem >= (I915_PDES - index) * I915_GTT_PAGE_SIZE))
1254 				maybe_64K = __gen8_pte_index(start, 1);
1255 
1256 			vaddr = kmap_atomic_px(pt);
1257 		}
1258 
1259 		do {
1260 			GEM_BUG_ON(iter->sg->length < page_size);
1261 			vaddr[index++] = encode | iter->dma;
1262 
1263 			start += page_size;
1264 			iter->dma += page_size;
1265 			rem -= page_size;
1266 			if (iter->dma >= iter->max) {
1267 				iter->sg = __sg_next(iter->sg);
1268 				if (!iter->sg)
1269 					break;
1270 
1271 				rem = iter->sg->length;
1272 				iter->dma = sg_dma_address(iter->sg);
1273 				iter->max = iter->dma + rem;
1274 
1275 				if (maybe_64K != -1 && index < I915_PDES &&
1276 				    !(IS_ALIGNED(iter->dma, I915_GTT_PAGE_SIZE_64K) &&
1277 				      (IS_ALIGNED(rem, I915_GTT_PAGE_SIZE_64K) ||
1278 				       rem >= (I915_PDES - index) * I915_GTT_PAGE_SIZE)))
1279 					maybe_64K = -1;
1280 
1281 				if (unlikely(!IS_ALIGNED(iter->dma, page_size)))
1282 					break;
1283 			}
1284 		} while (rem >= page_size && index < I915_PDES);
1285 
1286 		kunmap_atomic(vaddr);
1287 
1288 		/*
1289 		 * Is it safe to mark the 2M block as 64K? -- Either we have
1290 		 * filled whole page-table with 64K entries, or filled part of
1291 		 * it and have reached the end of the sg table and we have
1292 		 * enough padding.
1293 		 */
1294 		if (maybe_64K != -1 &&
1295 		    (index == I915_PDES ||
1296 		     (i915_vm_has_scratch_64K(vma->vm) &&
1297 		      !iter->sg && IS_ALIGNED(vma->node.start +
1298 					      vma->node.size,
1299 					      I915_GTT_PAGE_SIZE_2M)))) {
1300 			vaddr = kmap_atomic_px(pd);
1301 			vaddr[maybe_64K] |= GEN8_PDE_IPS_64K;
1302 			kunmap_atomic(vaddr);
1303 			page_size = I915_GTT_PAGE_SIZE_64K;
1304 
1305 			/*
1306 			 * We write all 4K page entries, even when using 64K
1307 			 * pages. In order to verify that the HW isn't cheating
1308 			 * by using the 4K PTE instead of the 64K PTE, we want
1309 			 * to remove all the surplus entries. If the HW skipped
1310 			 * the 64K PTE, it will read/write into the scratch page
1311 			 * instead - which we detect as missing results during
1312 			 * selftests.
1313 			 */
1314 			if (I915_SELFTEST_ONLY(vma->vm->scrub_64K)) {
1315 				u16 i;
1316 
1317 				encode = vma->vm->scratch[0].encode;
1318 				vaddr = kmap_atomic_px(i915_pt_entry(pd, maybe_64K));
1319 
1320 				for (i = 1; i < index; i += 16)
1321 					memset64(vaddr + i, encode, 15);
1322 
1323 				kunmap_atomic(vaddr);
1324 			}
1325 		}
1326 
1327 		vma->page_sizes.gtt |= page_size;
1328 	} while (iter->sg);
1329 }
1330 
1331 static void gen8_ppgtt_insert(struct i915_address_space *vm,
1332 			      struct i915_vma *vma,
1333 			      enum i915_cache_level cache_level,
1334 			      u32 flags)
1335 {
1336 	struct i915_ppgtt * const ppgtt = i915_vm_to_ppgtt(vm);
1337 	struct sgt_dma iter = sgt_dma(vma);
1338 
1339 	if (vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
1340 		gen8_ppgtt_insert_huge(vma, &iter, cache_level, flags);
1341 	} else  {
1342 		u64 idx = vma->node.start >> GEN8_PTE_SHIFT;
1343 
1344 		do {
1345 			struct i915_page_directory * const pdp =
1346 				gen8_pdp_for_page_index(vm, idx);
1347 
1348 			idx = gen8_ppgtt_insert_pte(ppgtt, pdp, &iter, idx,
1349 						    cache_level, flags);
1350 		} while (idx);
1351 
1352 		vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
1353 	}
1354 }
1355 
1356 static int gen8_init_scratch(struct i915_address_space *vm)
1357 {
1358 	int ret;
1359 	int i;
1360 
1361 	/*
1362 	 * If everybody agrees to not to write into the scratch page,
1363 	 * we can reuse it for all vm, keeping contexts and processes separate.
1364 	 */
1365 	if (vm->has_read_only &&
1366 	    vm->i915->kernel_context &&
1367 	    vm->i915->kernel_context->vm) {
1368 		struct i915_address_space *clone = vm->i915->kernel_context->vm;
1369 
1370 		GEM_BUG_ON(!clone->has_read_only);
1371 
1372 		vm->scratch_order = clone->scratch_order;
1373 		memcpy(vm->scratch, clone->scratch, sizeof(vm->scratch));
1374 		px_dma(&vm->scratch[0]) = 0; /* no xfer of ownership */
1375 		return 0;
1376 	}
1377 
1378 	ret = setup_scratch_page(vm, __GFP_HIGHMEM);
1379 	if (ret)
1380 		return ret;
1381 
1382 	vm->scratch[0].encode =
1383 		gen8_pte_encode(px_dma(&vm->scratch[0]),
1384 				I915_CACHE_LLC, vm->has_read_only);
1385 
1386 	for (i = 1; i <= vm->top; i++) {
1387 		if (unlikely(setup_page_dma(vm, px_base(&vm->scratch[i]))))
1388 			goto free_scratch;
1389 
1390 		fill_px(&vm->scratch[i], vm->scratch[i - 1].encode);
1391 		vm->scratch[i].encode =
1392 			gen8_pde_encode(px_dma(&vm->scratch[i]),
1393 					I915_CACHE_LLC);
1394 	}
1395 
1396 	return 0;
1397 
1398 free_scratch:
1399 	free_scratch(vm);
1400 	return -ENOMEM;
1401 }
1402 
1403 static int gen8_preallocate_top_level_pdp(struct i915_ppgtt *ppgtt)
1404 {
1405 	struct i915_address_space *vm = &ppgtt->vm;
1406 	struct i915_page_directory *pd = ppgtt->pd;
1407 	unsigned int idx;
1408 
1409 	GEM_BUG_ON(vm->top != 2);
1410 	GEM_BUG_ON(gen8_pd_top_count(vm) != GEN8_3LVL_PDPES);
1411 
1412 	for (idx = 0; idx < GEN8_3LVL_PDPES; idx++) {
1413 		struct i915_page_directory *pde;
1414 
1415 		pde = alloc_pd(vm);
1416 		if (IS_ERR(pde))
1417 			return PTR_ERR(pde);
1418 
1419 		fill_px(pde, vm->scratch[1].encode);
1420 		set_pd_entry(pd, idx, pde);
1421 		atomic_inc(px_used(pde)); /* keep pinned */
1422 	}
1423 
1424 	return 0;
1425 }
1426 
1427 static void ppgtt_init(struct i915_ppgtt *ppgtt, struct intel_gt *gt)
1428 {
1429 	struct drm_i915_private *i915 = gt->i915;
1430 
1431 	ppgtt->vm.gt = gt;
1432 	ppgtt->vm.i915 = i915;
1433 	ppgtt->vm.dma = &i915->drm.pdev->dev;
1434 	ppgtt->vm.total = BIT_ULL(INTEL_INFO(i915)->ppgtt_size);
1435 
1436 	i915_address_space_init(&ppgtt->vm, VM_CLASS_PPGTT);
1437 
1438 	ppgtt->vm.vma_ops.bind_vma    = ppgtt_bind_vma;
1439 	ppgtt->vm.vma_ops.unbind_vma  = ppgtt_unbind_vma;
1440 	ppgtt->vm.vma_ops.set_pages   = ppgtt_set_pages;
1441 	ppgtt->vm.vma_ops.clear_pages = clear_pages;
1442 }
1443 
1444 static struct i915_page_directory *
1445 gen8_alloc_top_pd(struct i915_address_space *vm)
1446 {
1447 	const unsigned int count = gen8_pd_top_count(vm);
1448 	struct i915_page_directory *pd;
1449 
1450 	GEM_BUG_ON(count > ARRAY_SIZE(pd->entry));
1451 
1452 	pd = __alloc_pd(offsetof(typeof(*pd), entry[count]));
1453 	if (unlikely(!pd))
1454 		return ERR_PTR(-ENOMEM);
1455 
1456 	if (unlikely(setup_page_dma(vm, px_base(pd)))) {
1457 		kfree(pd);
1458 		return ERR_PTR(-ENOMEM);
1459 	}
1460 
1461 	fill_page_dma(px_base(pd), vm->scratch[vm->top].encode, count);
1462 	atomic_inc(px_used(pd)); /* mark as pinned */
1463 	return pd;
1464 }
1465 
1466 /*
1467  * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1468  * with a net effect resembling a 2-level page table in normal x86 terms. Each
1469  * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1470  * space.
1471  *
1472  */
1473 static struct i915_ppgtt *gen8_ppgtt_create(struct drm_i915_private *i915)
1474 {
1475 	struct i915_ppgtt *ppgtt;
1476 	int err;
1477 
1478 	ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1479 	if (!ppgtt)
1480 		return ERR_PTR(-ENOMEM);
1481 
1482 	ppgtt_init(ppgtt, &i915->gt);
1483 	ppgtt->vm.top = i915_vm_is_4lvl(&ppgtt->vm) ? 3 : 2;
1484 
1485 	/*
1486 	 * From bdw, there is hw support for read-only pages in the PPGTT.
1487 	 *
1488 	 * Gen11 has HSDES#:1807136187 unresolved. Disable ro support
1489 	 * for now.
1490 	 */
1491 	ppgtt->vm.has_read_only = INTEL_GEN(i915) != 11;
1492 
1493 	/* There are only few exceptions for gen >=6. chv and bxt.
1494 	 * And we are not sure about the latter so play safe for now.
1495 	 */
1496 	if (IS_CHERRYVIEW(i915) || IS_BROXTON(i915))
1497 		ppgtt->vm.pt_kmap_wc = true;
1498 
1499 	err = gen8_init_scratch(&ppgtt->vm);
1500 	if (err)
1501 		goto err_free;
1502 
1503 	ppgtt->pd = gen8_alloc_top_pd(&ppgtt->vm);
1504 	if (IS_ERR(ppgtt->pd)) {
1505 		err = PTR_ERR(ppgtt->pd);
1506 		goto err_free_scratch;
1507 	}
1508 
1509 	if (!i915_vm_is_4lvl(&ppgtt->vm)) {
1510 		if (intel_vgpu_active(i915)) {
1511 			err = gen8_preallocate_top_level_pdp(ppgtt);
1512 			if (err)
1513 				goto err_free_pd;
1514 		}
1515 	}
1516 
1517 	ppgtt->vm.insert_entries = gen8_ppgtt_insert;
1518 	ppgtt->vm.allocate_va_range = gen8_ppgtt_alloc;
1519 	ppgtt->vm.clear_range = gen8_ppgtt_clear;
1520 
1521 	if (intel_vgpu_active(i915))
1522 		gen8_ppgtt_notify_vgt(ppgtt, true);
1523 
1524 	ppgtt->vm.cleanup = gen8_ppgtt_cleanup;
1525 
1526 	return ppgtt;
1527 
1528 err_free_pd:
1529 	__gen8_ppgtt_cleanup(&ppgtt->vm, ppgtt->pd,
1530 			     gen8_pd_top_count(&ppgtt->vm), ppgtt->vm.top);
1531 err_free_scratch:
1532 	free_scratch(&ppgtt->vm);
1533 err_free:
1534 	kfree(ppgtt);
1535 	return ERR_PTR(err);
1536 }
1537 
1538 /* Write pde (index) from the page directory @pd to the page table @pt */
1539 static inline void gen6_write_pde(const struct gen6_ppgtt *ppgtt,
1540 				  const unsigned int pde,
1541 				  const struct i915_page_table *pt)
1542 {
1543 	/* Caller needs to make sure the write completes if necessary */
1544 	iowrite32(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
1545 		  ppgtt->pd_addr + pde);
1546 }
1547 
1548 static void gen7_ppgtt_enable(struct intel_gt *gt)
1549 {
1550 	struct drm_i915_private *i915 = gt->i915;
1551 	struct intel_uncore *uncore = gt->uncore;
1552 	struct intel_engine_cs *engine;
1553 	enum intel_engine_id id;
1554 	u32 ecochk;
1555 
1556 	intel_uncore_rmw(uncore, GAC_ECO_BITS, 0, ECOBITS_PPGTT_CACHE64B);
1557 
1558 	ecochk = intel_uncore_read(uncore, GAM_ECOCHK);
1559 	if (IS_HASWELL(i915)) {
1560 		ecochk |= ECOCHK_PPGTT_WB_HSW;
1561 	} else {
1562 		ecochk |= ECOCHK_PPGTT_LLC_IVB;
1563 		ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1564 	}
1565 	intel_uncore_write(uncore, GAM_ECOCHK, ecochk);
1566 
1567 	for_each_engine(engine, i915, id) {
1568 		/* GFX_MODE is per-ring on gen7+ */
1569 		ENGINE_WRITE(engine,
1570 			     RING_MODE_GEN7,
1571 			     _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1572 	}
1573 }
1574 
1575 static void gen6_ppgtt_enable(struct intel_gt *gt)
1576 {
1577 	struct intel_uncore *uncore = gt->uncore;
1578 
1579 	intel_uncore_rmw(uncore,
1580 			 GAC_ECO_BITS,
1581 			 0,
1582 			 ECOBITS_SNB_BIT | ECOBITS_PPGTT_CACHE64B);
1583 
1584 	intel_uncore_rmw(uncore,
1585 			 GAB_CTL,
1586 			 0,
1587 			 GAB_CTL_CONT_AFTER_PAGEFAULT);
1588 
1589 	intel_uncore_rmw(uncore,
1590 			 GAM_ECOCHK,
1591 			 0,
1592 			 ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1593 
1594 	if (HAS_PPGTT(uncore->i915)) /* may be disabled for VT-d */
1595 		intel_uncore_write(uncore,
1596 				   GFX_MODE,
1597 				   _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1598 }
1599 
1600 /* PPGTT support for Sandybdrige/Gen6 and later */
1601 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
1602 				   u64 start, u64 length)
1603 {
1604 	struct gen6_ppgtt * const ppgtt = to_gen6_ppgtt(i915_vm_to_ppgtt(vm));
1605 	const unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
1606 	const gen6_pte_t scratch_pte = vm->scratch[0].encode;
1607 	unsigned int pde = first_entry / GEN6_PTES;
1608 	unsigned int pte = first_entry % GEN6_PTES;
1609 	unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
1610 
1611 	while (num_entries) {
1612 		struct i915_page_table * const pt =
1613 			i915_pt_entry(ppgtt->base.pd, pde++);
1614 		const unsigned int count = min(num_entries, GEN6_PTES - pte);
1615 		gen6_pte_t *vaddr;
1616 
1617 		GEM_BUG_ON(px_base(pt) == px_base(&vm->scratch[1]));
1618 
1619 		num_entries -= count;
1620 
1621 		GEM_BUG_ON(count > atomic_read(&pt->used));
1622 		if (!atomic_sub_return(count, &pt->used))
1623 			ppgtt->scan_for_unused_pt = true;
1624 
1625 		/*
1626 		 * Note that the hw doesn't support removing PDE on the fly
1627 		 * (they are cached inside the context with no means to
1628 		 * invalidate the cache), so we can only reset the PTE
1629 		 * entries back to scratch.
1630 		 */
1631 
1632 		vaddr = kmap_atomic_px(pt);
1633 		memset32(vaddr + pte, scratch_pte, count);
1634 		kunmap_atomic(vaddr);
1635 
1636 		pte = 0;
1637 	}
1638 }
1639 
1640 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
1641 				      struct i915_vma *vma,
1642 				      enum i915_cache_level cache_level,
1643 				      u32 flags)
1644 {
1645 	struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1646 	struct i915_page_directory * const pd = ppgtt->pd;
1647 	unsigned first_entry = vma->node.start / I915_GTT_PAGE_SIZE;
1648 	unsigned act_pt = first_entry / GEN6_PTES;
1649 	unsigned act_pte = first_entry % GEN6_PTES;
1650 	const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1651 	struct sgt_dma iter = sgt_dma(vma);
1652 	gen6_pte_t *vaddr;
1653 
1654 	GEM_BUG_ON(pd->entry[act_pt] == &vm->scratch[1]);
1655 
1656 	vaddr = kmap_atomic_px(i915_pt_entry(pd, act_pt));
1657 	do {
1658 		vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
1659 
1660 		iter.dma += I915_GTT_PAGE_SIZE;
1661 		if (iter.dma == iter.max) {
1662 			iter.sg = __sg_next(iter.sg);
1663 			if (!iter.sg)
1664 				break;
1665 
1666 			iter.dma = sg_dma_address(iter.sg);
1667 			iter.max = iter.dma + iter.sg->length;
1668 		}
1669 
1670 		if (++act_pte == GEN6_PTES) {
1671 			kunmap_atomic(vaddr);
1672 			vaddr = kmap_atomic_px(i915_pt_entry(pd, ++act_pt));
1673 			act_pte = 0;
1674 		}
1675 	} while (1);
1676 	kunmap_atomic(vaddr);
1677 
1678 	vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
1679 }
1680 
1681 static int gen6_alloc_va_range(struct i915_address_space *vm,
1682 			       u64 start, u64 length)
1683 {
1684 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(i915_vm_to_ppgtt(vm));
1685 	struct i915_page_directory * const pd = ppgtt->base.pd;
1686 	struct i915_page_table *pt, *alloc = NULL;
1687 	intel_wakeref_t wakeref;
1688 	u64 from = start;
1689 	unsigned int pde;
1690 	bool flush = false;
1691 	int ret = 0;
1692 
1693 	wakeref = intel_runtime_pm_get(&vm->i915->runtime_pm);
1694 
1695 	spin_lock(&pd->lock);
1696 	gen6_for_each_pde(pt, pd, start, length, pde) {
1697 		const unsigned int count = gen6_pte_count(start, length);
1698 
1699 		if (px_base(pt) == px_base(&vm->scratch[1])) {
1700 			spin_unlock(&pd->lock);
1701 
1702 			pt = fetch_and_zero(&alloc);
1703 			if (!pt)
1704 				pt = alloc_pt(vm);
1705 			if (IS_ERR(pt)) {
1706 				ret = PTR_ERR(pt);
1707 				goto unwind_out;
1708 			}
1709 
1710 			fill32_px(pt, vm->scratch[0].encode);
1711 
1712 			spin_lock(&pd->lock);
1713 			if (pd->entry[pde] == &vm->scratch[1]) {
1714 				pd->entry[pde] = pt;
1715 				if (i915_vma_is_bound(ppgtt->vma,
1716 						      I915_VMA_GLOBAL_BIND)) {
1717 					gen6_write_pde(ppgtt, pde, pt);
1718 					flush = true;
1719 				}
1720 			} else {
1721 				alloc = pt;
1722 				pt = pd->entry[pde];
1723 			}
1724 		}
1725 
1726 		atomic_add(count, &pt->used);
1727 	}
1728 	spin_unlock(&pd->lock);
1729 
1730 	if (flush) {
1731 		mark_tlbs_dirty(&ppgtt->base);
1732 		gen6_ggtt_invalidate(vm->gt->ggtt);
1733 	}
1734 
1735 	goto out;
1736 
1737 unwind_out:
1738 	gen6_ppgtt_clear_range(vm, from, start - from);
1739 out:
1740 	if (alloc)
1741 		free_px(vm, alloc);
1742 	intel_runtime_pm_put(&vm->i915->runtime_pm, wakeref);
1743 	return ret;
1744 }
1745 
1746 static int gen6_ppgtt_init_scratch(struct gen6_ppgtt *ppgtt)
1747 {
1748 	struct i915_address_space * const vm = &ppgtt->base.vm;
1749 	struct i915_page_directory * const pd = ppgtt->base.pd;
1750 	int ret;
1751 
1752 	ret = setup_scratch_page(vm, __GFP_HIGHMEM);
1753 	if (ret)
1754 		return ret;
1755 
1756 	vm->scratch[0].encode =
1757 		vm->pte_encode(px_dma(&vm->scratch[0]),
1758 			       I915_CACHE_NONE, PTE_READ_ONLY);
1759 
1760 	if (unlikely(setup_page_dma(vm, px_base(&vm->scratch[1])))) {
1761 		cleanup_scratch_page(vm);
1762 		return -ENOMEM;
1763 	}
1764 
1765 	fill32_px(&vm->scratch[1], vm->scratch[0].encode);
1766 	memset_p(pd->entry, &vm->scratch[1], I915_PDES);
1767 
1768 	return 0;
1769 }
1770 
1771 static void gen6_ppgtt_free_pd(struct gen6_ppgtt *ppgtt)
1772 {
1773 	struct i915_page_directory * const pd = ppgtt->base.pd;
1774 	struct i915_page_dma * const scratch =
1775 		px_base(&ppgtt->base.vm.scratch[1]);
1776 	struct i915_page_table *pt;
1777 	u32 pde;
1778 
1779 	gen6_for_all_pdes(pt, pd, pde)
1780 		if (px_base(pt) != scratch)
1781 			free_px(&ppgtt->base.vm, pt);
1782 }
1783 
1784 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
1785 {
1786 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(i915_vm_to_ppgtt(vm));
1787 	struct drm_i915_private *i915 = vm->i915;
1788 
1789 	/* FIXME remove the struct_mutex to bring the locking under control */
1790 	mutex_lock(&i915->drm.struct_mutex);
1791 	i915_vma_destroy(ppgtt->vma);
1792 	mutex_unlock(&i915->drm.struct_mutex);
1793 
1794 	gen6_ppgtt_free_pd(ppgtt);
1795 	free_scratch(vm);
1796 	kfree(ppgtt->base.pd);
1797 }
1798 
1799 static int pd_vma_set_pages(struct i915_vma *vma)
1800 {
1801 	vma->pages = ERR_PTR(-ENODEV);
1802 	return 0;
1803 }
1804 
1805 static void pd_vma_clear_pages(struct i915_vma *vma)
1806 {
1807 	GEM_BUG_ON(!vma->pages);
1808 
1809 	vma->pages = NULL;
1810 }
1811 
1812 static int pd_vma_bind(struct i915_vma *vma,
1813 		       enum i915_cache_level cache_level,
1814 		       u32 unused)
1815 {
1816 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vma->vm);
1817 	struct gen6_ppgtt *ppgtt = vma->private;
1818 	u32 ggtt_offset = i915_ggtt_offset(vma) / I915_GTT_PAGE_SIZE;
1819 	struct i915_page_table *pt;
1820 	unsigned int pde;
1821 
1822 	px_base(ppgtt->base.pd)->ggtt_offset = ggtt_offset * sizeof(gen6_pte_t);
1823 	ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm + ggtt_offset;
1824 
1825 	gen6_for_all_pdes(pt, ppgtt->base.pd, pde)
1826 		gen6_write_pde(ppgtt, pde, pt);
1827 
1828 	mark_tlbs_dirty(&ppgtt->base);
1829 	gen6_ggtt_invalidate(ggtt);
1830 
1831 	return 0;
1832 }
1833 
1834 static void pd_vma_unbind(struct i915_vma *vma)
1835 {
1836 	struct gen6_ppgtt *ppgtt = vma->private;
1837 	struct i915_page_directory * const pd = ppgtt->base.pd;
1838 	struct i915_page_dma * const scratch =
1839 		px_base(&ppgtt->base.vm.scratch[1]);
1840 	struct i915_page_table *pt;
1841 	unsigned int pde;
1842 
1843 	if (!ppgtt->scan_for_unused_pt)
1844 		return;
1845 
1846 	/* Free all no longer used page tables */
1847 	gen6_for_all_pdes(pt, ppgtt->base.pd, pde) {
1848 		if (px_base(pt) == scratch || atomic_read(&pt->used))
1849 			continue;
1850 
1851 		free_px(&ppgtt->base.vm, pt);
1852 		pd->entry[pde] = scratch;
1853 	}
1854 
1855 	ppgtt->scan_for_unused_pt = false;
1856 }
1857 
1858 static const struct i915_vma_ops pd_vma_ops = {
1859 	.set_pages = pd_vma_set_pages,
1860 	.clear_pages = pd_vma_clear_pages,
1861 	.bind_vma = pd_vma_bind,
1862 	.unbind_vma = pd_vma_unbind,
1863 };
1864 
1865 static struct i915_vma *pd_vma_create(struct gen6_ppgtt *ppgtt, int size)
1866 {
1867 	struct drm_i915_private *i915 = ppgtt->base.vm.i915;
1868 	struct i915_ggtt *ggtt = ppgtt->base.vm.gt->ggtt;
1869 	struct i915_vma *vma;
1870 
1871 	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
1872 	GEM_BUG_ON(size > ggtt->vm.total);
1873 
1874 	vma = i915_vma_alloc();
1875 	if (!vma)
1876 		return ERR_PTR(-ENOMEM);
1877 
1878 	i915_active_init(i915, &vma->active, NULL, NULL);
1879 
1880 	vma->vm = &ggtt->vm;
1881 	vma->ops = &pd_vma_ops;
1882 	vma->private = ppgtt;
1883 
1884 	vma->size = size;
1885 	vma->fence_size = size;
1886 	vma->flags = I915_VMA_GGTT;
1887 	vma->ggtt_view.type = I915_GGTT_VIEW_ROTATED; /* prevent fencing */
1888 
1889 	INIT_LIST_HEAD(&vma->obj_link);
1890 	INIT_LIST_HEAD(&vma->closed_link);
1891 
1892 	mutex_lock(&vma->vm->mutex);
1893 	list_add(&vma->vm_link, &vma->vm->unbound_list);
1894 	mutex_unlock(&vma->vm->mutex);
1895 
1896 	return vma;
1897 }
1898 
1899 int gen6_ppgtt_pin(struct i915_ppgtt *base)
1900 {
1901 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(base);
1902 	int err;
1903 
1904 	GEM_BUG_ON(ppgtt->base.vm.closed);
1905 
1906 	/*
1907 	 * Workaround the limited maximum vma->pin_count and the aliasing_ppgtt
1908 	 * which will be pinned into every active context.
1909 	 * (When vma->pin_count becomes atomic, I expect we will naturally
1910 	 * need a larger, unpacked, type and kill this redundancy.)
1911 	 */
1912 	if (ppgtt->pin_count++)
1913 		return 0;
1914 
1915 	/*
1916 	 * PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1917 	 * allocator works in address space sizes, so it's multiplied by page
1918 	 * size. We allocate at the top of the GTT to avoid fragmentation.
1919 	 */
1920 	err = i915_vma_pin(ppgtt->vma,
1921 			   0, GEN6_PD_ALIGN,
1922 			   PIN_GLOBAL | PIN_HIGH);
1923 	if (err)
1924 		goto unpin;
1925 
1926 	return 0;
1927 
1928 unpin:
1929 	ppgtt->pin_count = 0;
1930 	return err;
1931 }
1932 
1933 void gen6_ppgtt_unpin(struct i915_ppgtt *base)
1934 {
1935 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(base);
1936 
1937 	GEM_BUG_ON(!ppgtt->pin_count);
1938 	if (--ppgtt->pin_count)
1939 		return;
1940 
1941 	i915_vma_unpin(ppgtt->vma);
1942 }
1943 
1944 void gen6_ppgtt_unpin_all(struct i915_ppgtt *base)
1945 {
1946 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(base);
1947 
1948 	if (!ppgtt->pin_count)
1949 		return;
1950 
1951 	ppgtt->pin_count = 0;
1952 	i915_vma_unpin(ppgtt->vma);
1953 }
1954 
1955 static struct i915_ppgtt *gen6_ppgtt_create(struct drm_i915_private *i915)
1956 {
1957 	struct i915_ggtt * const ggtt = &i915->ggtt;
1958 	struct gen6_ppgtt *ppgtt;
1959 	int err;
1960 
1961 	ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1962 	if (!ppgtt)
1963 		return ERR_PTR(-ENOMEM);
1964 
1965 	ppgtt_init(&ppgtt->base, &i915->gt);
1966 	ppgtt->base.vm.top = 1;
1967 
1968 	ppgtt->base.vm.allocate_va_range = gen6_alloc_va_range;
1969 	ppgtt->base.vm.clear_range = gen6_ppgtt_clear_range;
1970 	ppgtt->base.vm.insert_entries = gen6_ppgtt_insert_entries;
1971 	ppgtt->base.vm.cleanup = gen6_ppgtt_cleanup;
1972 
1973 	ppgtt->base.vm.pte_encode = ggtt->vm.pte_encode;
1974 
1975 	ppgtt->base.pd = __alloc_pd(sizeof(*ppgtt->base.pd));
1976 	if (!ppgtt->base.pd) {
1977 		err = -ENOMEM;
1978 		goto err_free;
1979 	}
1980 
1981 	err = gen6_ppgtt_init_scratch(ppgtt);
1982 	if (err)
1983 		goto err_pd;
1984 
1985 	ppgtt->vma = pd_vma_create(ppgtt, GEN6_PD_SIZE);
1986 	if (IS_ERR(ppgtt->vma)) {
1987 		err = PTR_ERR(ppgtt->vma);
1988 		goto err_scratch;
1989 	}
1990 
1991 	return &ppgtt->base;
1992 
1993 err_scratch:
1994 	free_scratch(&ppgtt->base.vm);
1995 err_pd:
1996 	kfree(ppgtt->base.pd);
1997 err_free:
1998 	kfree(ppgtt);
1999 	return ERR_PTR(err);
2000 }
2001 
2002 static void gtt_write_workarounds(struct intel_gt *gt)
2003 {
2004 	struct drm_i915_private *i915 = gt->i915;
2005 	struct intel_uncore *uncore = gt->uncore;
2006 
2007 	/* This function is for gtt related workarounds. This function is
2008 	 * called on driver load and after a GPU reset, so you can place
2009 	 * workarounds here even if they get overwritten by GPU reset.
2010 	 */
2011 	/* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk,cfl,cnl,icl */
2012 	if (IS_BROADWELL(i915))
2013 		intel_uncore_write(uncore,
2014 				   GEN8_L3_LRA_1_GPGPU,
2015 				   GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
2016 	else if (IS_CHERRYVIEW(i915))
2017 		intel_uncore_write(uncore,
2018 				   GEN8_L3_LRA_1_GPGPU,
2019 				   GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
2020 	else if (IS_GEN9_LP(i915))
2021 		intel_uncore_write(uncore,
2022 				   GEN8_L3_LRA_1_GPGPU,
2023 				   GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2024 	else if (INTEL_GEN(i915) >= 9)
2025 		intel_uncore_write(uncore,
2026 				   GEN8_L3_LRA_1_GPGPU,
2027 				   GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
2028 
2029 	/*
2030 	 * To support 64K PTEs we need to first enable the use of the
2031 	 * Intermediate-Page-Size(IPS) bit of the PDE field via some magical
2032 	 * mmio, otherwise the page-walker will simply ignore the IPS bit. This
2033 	 * shouldn't be needed after GEN10.
2034 	 *
2035 	 * 64K pages were first introduced from BDW+, although technically they
2036 	 * only *work* from gen9+. For pre-BDW we instead have the option for
2037 	 * 32K pages, but we don't currently have any support for it in our
2038 	 * driver.
2039 	 */
2040 	if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_64K) &&
2041 	    INTEL_GEN(i915) <= 10)
2042 		intel_uncore_rmw(uncore,
2043 				 GEN8_GAMW_ECO_DEV_RW_IA,
2044 				 0,
2045 				 GAMW_ECO_ENABLE_64K_IPS_FIELD);
2046 
2047 	if (IS_GEN_RANGE(i915, 8, 11)) {
2048 		bool can_use_gtt_cache = true;
2049 
2050 		/*
2051 		 * According to the BSpec if we use 2M/1G pages then we also
2052 		 * need to disable the GTT cache. At least on BDW we can see
2053 		 * visual corruption when using 2M pages, and not disabling the
2054 		 * GTT cache.
2055 		 */
2056 		if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_2M))
2057 			can_use_gtt_cache = false;
2058 
2059 		/* WaGttCachingOffByDefault */
2060 		intel_uncore_write(uncore,
2061 				   HSW_GTT_CACHE_EN,
2062 				   can_use_gtt_cache ? GTT_CACHE_EN_ALL : 0);
2063 		WARN_ON_ONCE(can_use_gtt_cache &&
2064 			     intel_uncore_read(uncore,
2065 					       HSW_GTT_CACHE_EN) == 0);
2066 	}
2067 }
2068 
2069 int i915_ppgtt_init_hw(struct intel_gt *gt)
2070 {
2071 	struct drm_i915_private *i915 = gt->i915;
2072 
2073 	gtt_write_workarounds(gt);
2074 
2075 	if (IS_GEN(i915, 6))
2076 		gen6_ppgtt_enable(gt);
2077 	else if (IS_GEN(i915, 7))
2078 		gen7_ppgtt_enable(gt);
2079 
2080 	return 0;
2081 }
2082 
2083 static struct i915_ppgtt *
2084 __ppgtt_create(struct drm_i915_private *i915)
2085 {
2086 	if (INTEL_GEN(i915) < 8)
2087 		return gen6_ppgtt_create(i915);
2088 	else
2089 		return gen8_ppgtt_create(i915);
2090 }
2091 
2092 struct i915_ppgtt *
2093 i915_ppgtt_create(struct drm_i915_private *i915)
2094 {
2095 	struct i915_ppgtt *ppgtt;
2096 
2097 	ppgtt = __ppgtt_create(i915);
2098 	if (IS_ERR(ppgtt))
2099 		return ppgtt;
2100 
2101 	trace_i915_ppgtt_create(&ppgtt->vm);
2102 
2103 	return ppgtt;
2104 }
2105 
2106 /* Certain Gen5 chipsets require require idling the GPU before
2107  * unmapping anything from the GTT when VT-d is enabled.
2108  */
2109 static bool needs_idle_maps(struct drm_i915_private *dev_priv)
2110 {
2111 	/* Query intel_iommu to see if we need the workaround. Presumably that
2112 	 * was loaded first.
2113 	 */
2114 	return IS_GEN(dev_priv, 5) && IS_MOBILE(dev_priv) && intel_vtd_active();
2115 }
2116 
2117 static void ggtt_suspend_mappings(struct i915_ggtt *ggtt)
2118 {
2119 	struct drm_i915_private *i915 = ggtt->vm.i915;
2120 
2121 	/* Don't bother messing with faults pre GEN6 as we have little
2122 	 * documentation supporting that it's a good idea.
2123 	 */
2124 	if (INTEL_GEN(i915) < 6)
2125 		return;
2126 
2127 	intel_gt_check_and_clear_faults(ggtt->vm.gt);
2128 
2129 	ggtt->vm.clear_range(&ggtt->vm, 0, ggtt->vm.total);
2130 
2131 	ggtt->invalidate(ggtt);
2132 }
2133 
2134 void i915_gem_suspend_gtt_mappings(struct drm_i915_private *i915)
2135 {
2136 	ggtt_suspend_mappings(&i915->ggtt);
2137 }
2138 
2139 int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2140 			       struct sg_table *pages)
2141 {
2142 	do {
2143 		if (dma_map_sg_attrs(&obj->base.dev->pdev->dev,
2144 				     pages->sgl, pages->nents,
2145 				     PCI_DMA_BIDIRECTIONAL,
2146 				     DMA_ATTR_NO_WARN))
2147 			return 0;
2148 
2149 		/*
2150 		 * If the DMA remap fails, one cause can be that we have
2151 		 * too many objects pinned in a small remapping table,
2152 		 * such as swiotlb. Incrementally purge all other objects and
2153 		 * try again - if there are no more pages to remove from
2154 		 * the DMA remapper, i915_gem_shrink will return 0.
2155 		 */
2156 		GEM_BUG_ON(obj->mm.pages == pages);
2157 	} while (i915_gem_shrink(to_i915(obj->base.dev),
2158 				 obj->base.size >> PAGE_SHIFT, NULL,
2159 				 I915_SHRINK_BOUND |
2160 				 I915_SHRINK_UNBOUND));
2161 
2162 	return -ENOSPC;
2163 }
2164 
2165 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
2166 {
2167 	writeq(pte, addr);
2168 }
2169 
2170 static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2171 				  dma_addr_t addr,
2172 				  u64 offset,
2173 				  enum i915_cache_level level,
2174 				  u32 unused)
2175 {
2176 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2177 	gen8_pte_t __iomem *pte =
2178 		(gen8_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
2179 
2180 	gen8_set_pte(pte, gen8_pte_encode(addr, level, 0));
2181 
2182 	ggtt->invalidate(ggtt);
2183 }
2184 
2185 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2186 				     struct i915_vma *vma,
2187 				     enum i915_cache_level level,
2188 				     u32 flags)
2189 {
2190 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2191 	struct sgt_iter sgt_iter;
2192 	gen8_pte_t __iomem *gtt_entries;
2193 	const gen8_pte_t pte_encode = gen8_pte_encode(0, level, 0);
2194 	dma_addr_t addr;
2195 
2196 	/*
2197 	 * Note that we ignore PTE_READ_ONLY here. The caller must be careful
2198 	 * not to allow the user to override access to a read only page.
2199 	 */
2200 
2201 	gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
2202 	gtt_entries += vma->node.start / I915_GTT_PAGE_SIZE;
2203 	for_each_sgt_dma(addr, sgt_iter, vma->pages)
2204 		gen8_set_pte(gtt_entries++, pte_encode | addr);
2205 
2206 	/*
2207 	 * We want to flush the TLBs only after we're certain all the PTE
2208 	 * updates have finished.
2209 	 */
2210 	ggtt->invalidate(ggtt);
2211 }
2212 
2213 static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2214 				  dma_addr_t addr,
2215 				  u64 offset,
2216 				  enum i915_cache_level level,
2217 				  u32 flags)
2218 {
2219 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2220 	gen6_pte_t __iomem *pte =
2221 		(gen6_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
2222 
2223 	iowrite32(vm->pte_encode(addr, level, flags), pte);
2224 
2225 	ggtt->invalidate(ggtt);
2226 }
2227 
2228 /*
2229  * Binds an object into the global gtt with the specified cache level. The object
2230  * will be accessible to the GPU via commands whose operands reference offsets
2231  * within the global GTT as well as accessible by the GPU through the GMADR
2232  * mapped BAR (dev_priv->mm.gtt->gtt).
2233  */
2234 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
2235 				     struct i915_vma *vma,
2236 				     enum i915_cache_level level,
2237 				     u32 flags)
2238 {
2239 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2240 	gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
2241 	unsigned int i = vma->node.start / I915_GTT_PAGE_SIZE;
2242 	struct sgt_iter iter;
2243 	dma_addr_t addr;
2244 	for_each_sgt_dma(addr, iter, vma->pages)
2245 		iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2246 
2247 	/*
2248 	 * We want to flush the TLBs only after we're certain all the PTE
2249 	 * updates have finished.
2250 	 */
2251 	ggtt->invalidate(ggtt);
2252 }
2253 
2254 static void nop_clear_range(struct i915_address_space *vm,
2255 			    u64 start, u64 length)
2256 {
2257 }
2258 
2259 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
2260 				  u64 start, u64 length)
2261 {
2262 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2263 	unsigned first_entry = start / I915_GTT_PAGE_SIZE;
2264 	unsigned num_entries = length / I915_GTT_PAGE_SIZE;
2265 	const gen8_pte_t scratch_pte = vm->scratch[0].encode;
2266 	gen8_pte_t __iomem *gtt_base =
2267 		(gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2268 	const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2269 	int i;
2270 
2271 	if (WARN(num_entries > max_entries,
2272 		 "First entry = %d; Num entries = %d (max=%d)\n",
2273 		 first_entry, num_entries, max_entries))
2274 		num_entries = max_entries;
2275 
2276 	for (i = 0; i < num_entries; i++)
2277 		gen8_set_pte(&gtt_base[i], scratch_pte);
2278 }
2279 
2280 static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
2281 {
2282 	struct drm_i915_private *dev_priv = vm->i915;
2283 
2284 	/*
2285 	 * Make sure the internal GAM fifo has been cleared of all GTT
2286 	 * writes before exiting stop_machine(). This guarantees that
2287 	 * any aperture accesses waiting to start in another process
2288 	 * cannot back up behind the GTT writes causing a hang.
2289 	 * The register can be any arbitrary GAM register.
2290 	 */
2291 	POSTING_READ(GFX_FLSH_CNTL_GEN6);
2292 }
2293 
2294 struct insert_page {
2295 	struct i915_address_space *vm;
2296 	dma_addr_t addr;
2297 	u64 offset;
2298 	enum i915_cache_level level;
2299 };
2300 
2301 static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
2302 {
2303 	struct insert_page *arg = _arg;
2304 
2305 	gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset, arg->level, 0);
2306 	bxt_vtd_ggtt_wa(arg->vm);
2307 
2308 	return 0;
2309 }
2310 
2311 static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
2312 					  dma_addr_t addr,
2313 					  u64 offset,
2314 					  enum i915_cache_level level,
2315 					  u32 unused)
2316 {
2317 	struct insert_page arg = { vm, addr, offset, level };
2318 
2319 	stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
2320 }
2321 
2322 struct insert_entries {
2323 	struct i915_address_space *vm;
2324 	struct i915_vma *vma;
2325 	enum i915_cache_level level;
2326 	u32 flags;
2327 };
2328 
2329 static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
2330 {
2331 	struct insert_entries *arg = _arg;
2332 
2333 	gen8_ggtt_insert_entries(arg->vm, arg->vma, arg->level, arg->flags);
2334 	bxt_vtd_ggtt_wa(arg->vm);
2335 
2336 	return 0;
2337 }
2338 
2339 static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2340 					     struct i915_vma *vma,
2341 					     enum i915_cache_level level,
2342 					     u32 flags)
2343 {
2344 	struct insert_entries arg = { vm, vma, level, flags };
2345 
2346 	stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
2347 }
2348 
2349 struct clear_range {
2350 	struct i915_address_space *vm;
2351 	u64 start;
2352 	u64 length;
2353 };
2354 
2355 static int bxt_vtd_ggtt_clear_range__cb(void *_arg)
2356 {
2357 	struct clear_range *arg = _arg;
2358 
2359 	gen8_ggtt_clear_range(arg->vm, arg->start, arg->length);
2360 	bxt_vtd_ggtt_wa(arg->vm);
2361 
2362 	return 0;
2363 }
2364 
2365 static void bxt_vtd_ggtt_clear_range__BKL(struct i915_address_space *vm,
2366 					  u64 start,
2367 					  u64 length)
2368 {
2369 	struct clear_range arg = { vm, start, length };
2370 
2371 	stop_machine(bxt_vtd_ggtt_clear_range__cb, &arg, NULL);
2372 }
2373 
2374 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
2375 				  u64 start, u64 length)
2376 {
2377 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2378 	unsigned first_entry = start / I915_GTT_PAGE_SIZE;
2379 	unsigned num_entries = length / I915_GTT_PAGE_SIZE;
2380 	gen6_pte_t scratch_pte, __iomem *gtt_base =
2381 		(gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2382 	const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2383 	int i;
2384 
2385 	if (WARN(num_entries > max_entries,
2386 		 "First entry = %d; Num entries = %d (max=%d)\n",
2387 		 first_entry, num_entries, max_entries))
2388 		num_entries = max_entries;
2389 
2390 	scratch_pte = vm->scratch[0].encode;
2391 	for (i = 0; i < num_entries; i++)
2392 		iowrite32(scratch_pte, &gtt_base[i]);
2393 }
2394 
2395 static void i915_ggtt_insert_page(struct i915_address_space *vm,
2396 				  dma_addr_t addr,
2397 				  u64 offset,
2398 				  enum i915_cache_level cache_level,
2399 				  u32 unused)
2400 {
2401 	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2402 		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2403 
2404 	intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
2405 }
2406 
2407 static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2408 				     struct i915_vma *vma,
2409 				     enum i915_cache_level cache_level,
2410 				     u32 unused)
2411 {
2412 	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2413 		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2414 
2415 	intel_gtt_insert_sg_entries(vma->pages, vma->node.start >> PAGE_SHIFT,
2416 				    flags);
2417 }
2418 
2419 static void i915_ggtt_clear_range(struct i915_address_space *vm,
2420 				  u64 start, u64 length)
2421 {
2422 	intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
2423 }
2424 
2425 static int ggtt_bind_vma(struct i915_vma *vma,
2426 			 enum i915_cache_level cache_level,
2427 			 u32 flags)
2428 {
2429 	struct drm_i915_private *i915 = vma->vm->i915;
2430 	struct drm_i915_gem_object *obj = vma->obj;
2431 	intel_wakeref_t wakeref;
2432 	u32 pte_flags;
2433 
2434 	/* Applicable to VLV (gen8+ do not support RO in the GGTT) */
2435 	pte_flags = 0;
2436 	if (i915_gem_object_is_readonly(obj))
2437 		pte_flags |= PTE_READ_ONLY;
2438 
2439 	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
2440 		vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
2441 
2442 	vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
2443 
2444 	/*
2445 	 * Without aliasing PPGTT there's no difference between
2446 	 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2447 	 * upgrade to both bound if we bind either to avoid double-binding.
2448 	 */
2449 	vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
2450 
2451 	return 0;
2452 }
2453 
2454 static void ggtt_unbind_vma(struct i915_vma *vma)
2455 {
2456 	struct drm_i915_private *i915 = vma->vm->i915;
2457 	intel_wakeref_t wakeref;
2458 
2459 	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
2460 		vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2461 }
2462 
2463 static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2464 				 enum i915_cache_level cache_level,
2465 				 u32 flags)
2466 {
2467 	struct drm_i915_private *i915 = vma->vm->i915;
2468 	u32 pte_flags;
2469 	int ret;
2470 
2471 	/* Currently applicable only to VLV */
2472 	pte_flags = 0;
2473 	if (i915_gem_object_is_readonly(vma->obj))
2474 		pte_flags |= PTE_READ_ONLY;
2475 
2476 	if (flags & I915_VMA_LOCAL_BIND) {
2477 		struct i915_ppgtt *alias = i915_vm_to_ggtt(vma->vm)->alias;
2478 
2479 		if (!(vma->flags & I915_VMA_LOCAL_BIND)) {
2480 			ret = alias->vm.allocate_va_range(&alias->vm,
2481 							  vma->node.start,
2482 							  vma->size);
2483 			if (ret)
2484 				return ret;
2485 		}
2486 
2487 		alias->vm.insert_entries(&alias->vm, vma,
2488 					 cache_level, pte_flags);
2489 	}
2490 
2491 	if (flags & I915_VMA_GLOBAL_BIND) {
2492 		intel_wakeref_t wakeref;
2493 
2494 		with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
2495 			vma->vm->insert_entries(vma->vm, vma,
2496 						cache_level, pte_flags);
2497 		}
2498 	}
2499 
2500 	return 0;
2501 }
2502 
2503 static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
2504 {
2505 	struct drm_i915_private *i915 = vma->vm->i915;
2506 
2507 	if (vma->flags & I915_VMA_GLOBAL_BIND) {
2508 		struct i915_address_space *vm = vma->vm;
2509 		intel_wakeref_t wakeref;
2510 
2511 		with_intel_runtime_pm(&i915->runtime_pm, wakeref)
2512 			vm->clear_range(vm, vma->node.start, vma->size);
2513 	}
2514 
2515 	if (vma->flags & I915_VMA_LOCAL_BIND) {
2516 		struct i915_address_space *vm =
2517 			&i915_vm_to_ggtt(vma->vm)->alias->vm;
2518 
2519 		vm->clear_range(vm, vma->node.start, vma->size);
2520 	}
2521 }
2522 
2523 void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2524 			       struct sg_table *pages)
2525 {
2526 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2527 	struct device *kdev = &dev_priv->drm.pdev->dev;
2528 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
2529 
2530 	if (unlikely(ggtt->do_idle_maps)) {
2531 		if (i915_gem_wait_for_idle(dev_priv, 0, MAX_SCHEDULE_TIMEOUT)) {
2532 			DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2533 			/* Wait a bit, in hopes it avoids the hang */
2534 			udelay(10);
2535 		}
2536 	}
2537 
2538 	dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
2539 }
2540 
2541 static int ggtt_set_pages(struct i915_vma *vma)
2542 {
2543 	int ret;
2544 
2545 	GEM_BUG_ON(vma->pages);
2546 
2547 	ret = i915_get_ggtt_vma_pages(vma);
2548 	if (ret)
2549 		return ret;
2550 
2551 	vma->page_sizes = vma->obj->mm.page_sizes;
2552 
2553 	return 0;
2554 }
2555 
2556 static void i915_gtt_color_adjust(const struct drm_mm_node *node,
2557 				  unsigned long color,
2558 				  u64 *start,
2559 				  u64 *end)
2560 {
2561 	if (node->allocated && node->color != color)
2562 		*start += I915_GTT_PAGE_SIZE;
2563 
2564 	/* Also leave a space between the unallocated reserved node after the
2565 	 * GTT and any objects within the GTT, i.e. we use the color adjustment
2566 	 * to insert a guard page to prevent prefetches crossing over the
2567 	 * GTT boundary.
2568 	 */
2569 	node = list_next_entry(node, node_list);
2570 	if (node->color != color)
2571 		*end -= I915_GTT_PAGE_SIZE;
2572 }
2573 
2574 static int init_aliasing_ppgtt(struct i915_ggtt *ggtt)
2575 {
2576 	struct i915_ppgtt *ppgtt;
2577 	int err;
2578 
2579 	ppgtt = i915_ppgtt_create(ggtt->vm.i915);
2580 	if (IS_ERR(ppgtt))
2581 		return PTR_ERR(ppgtt);
2582 
2583 	if (GEM_WARN_ON(ppgtt->vm.total < ggtt->vm.total)) {
2584 		err = -ENODEV;
2585 		goto err_ppgtt;
2586 	}
2587 
2588 	/*
2589 	 * Note we only pre-allocate as far as the end of the global
2590 	 * GTT. On 48b / 4-level page-tables, the difference is very,
2591 	 * very significant! We have to preallocate as GVT/vgpu does
2592 	 * not like the page directory disappearing.
2593 	 */
2594 	err = ppgtt->vm.allocate_va_range(&ppgtt->vm, 0, ggtt->vm.total);
2595 	if (err)
2596 		goto err_ppgtt;
2597 
2598 	ggtt->alias = ppgtt;
2599 
2600 	GEM_BUG_ON(ggtt->vm.vma_ops.bind_vma != ggtt_bind_vma);
2601 	ggtt->vm.vma_ops.bind_vma = aliasing_gtt_bind_vma;
2602 
2603 	GEM_BUG_ON(ggtt->vm.vma_ops.unbind_vma != ggtt_unbind_vma);
2604 	ggtt->vm.vma_ops.unbind_vma = aliasing_gtt_unbind_vma;
2605 
2606 	return 0;
2607 
2608 err_ppgtt:
2609 	i915_vm_put(&ppgtt->vm);
2610 	return err;
2611 }
2612 
2613 static void fini_aliasing_ppgtt(struct i915_ggtt *ggtt)
2614 {
2615 	struct drm_i915_private *i915 = ggtt->vm.i915;
2616 	struct i915_ppgtt *ppgtt;
2617 
2618 	mutex_lock(&i915->drm.struct_mutex);
2619 
2620 	ppgtt = fetch_and_zero(&ggtt->alias);
2621 	if (!ppgtt)
2622 		goto out;
2623 
2624 	i915_vm_put(&ppgtt->vm);
2625 
2626 	ggtt->vm.vma_ops.bind_vma   = ggtt_bind_vma;
2627 	ggtt->vm.vma_ops.unbind_vma = ggtt_unbind_vma;
2628 
2629 out:
2630 	mutex_unlock(&i915->drm.struct_mutex);
2631 }
2632 
2633 static int ggtt_reserve_guc_top(struct i915_ggtt *ggtt)
2634 {
2635 	u64 size;
2636 	int ret;
2637 
2638 	if (!USES_GUC(ggtt->vm.i915))
2639 		return 0;
2640 
2641 	GEM_BUG_ON(ggtt->vm.total <= GUC_GGTT_TOP);
2642 	size = ggtt->vm.total - GUC_GGTT_TOP;
2643 
2644 	ret = i915_gem_gtt_reserve(&ggtt->vm, &ggtt->uc_fw, size,
2645 				   GUC_GGTT_TOP, I915_COLOR_UNEVICTABLE,
2646 				   PIN_NOEVICT);
2647 	if (ret)
2648 		DRM_DEBUG_DRIVER("Failed to reserve top of GGTT for GuC\n");
2649 
2650 	return ret;
2651 }
2652 
2653 static void ggtt_release_guc_top(struct i915_ggtt *ggtt)
2654 {
2655 	if (drm_mm_node_allocated(&ggtt->uc_fw))
2656 		drm_mm_remove_node(&ggtt->uc_fw);
2657 }
2658 
2659 static void cleanup_init_ggtt(struct i915_ggtt *ggtt)
2660 {
2661 	ggtt_release_guc_top(ggtt);
2662 	drm_mm_remove_node(&ggtt->error_capture);
2663 }
2664 
2665 static int init_ggtt(struct i915_ggtt *ggtt)
2666 {
2667 	/* Let GEM Manage all of the aperture.
2668 	 *
2669 	 * However, leave one page at the end still bound to the scratch page.
2670 	 * There are a number of places where the hardware apparently prefetches
2671 	 * past the end of the object, and we've seen multiple hangs with the
2672 	 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2673 	 * aperture.  One page should be enough to keep any prefetching inside
2674 	 * of the aperture.
2675 	 */
2676 	unsigned long hole_start, hole_end;
2677 	struct drm_mm_node *entry;
2678 	int ret;
2679 
2680 	/*
2681 	 * GuC requires all resources that we're sharing with it to be placed in
2682 	 * non-WOPCM memory. If GuC is not present or not in use we still need a
2683 	 * small bias as ring wraparound at offset 0 sometimes hangs. No idea
2684 	 * why.
2685 	 */
2686 	ggtt->pin_bias = max_t(u32, I915_GTT_PAGE_SIZE,
2687 			       intel_wopcm_guc_size(&ggtt->vm.i915->wopcm));
2688 
2689 	ret = intel_vgt_balloon(ggtt);
2690 	if (ret)
2691 		return ret;
2692 
2693 	/* Reserve a mappable slot for our lockless error capture */
2694 	ret = drm_mm_insert_node_in_range(&ggtt->vm.mm, &ggtt->error_capture,
2695 					  PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2696 					  0, ggtt->mappable_end,
2697 					  DRM_MM_INSERT_LOW);
2698 	if (ret)
2699 		return ret;
2700 
2701 	/*
2702 	 * The upper portion of the GuC address space has a sizeable hole
2703 	 * (several MB) that is inaccessible by GuC. Reserve this range within
2704 	 * GGTT as it can comfortably hold GuC/HuC firmware images.
2705 	 */
2706 	ret = ggtt_reserve_guc_top(ggtt);
2707 	if (ret)
2708 		goto err;
2709 
2710 	/* Clear any non-preallocated blocks */
2711 	drm_mm_for_each_hole(entry, &ggtt->vm.mm, hole_start, hole_end) {
2712 		DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2713 			      hole_start, hole_end);
2714 		ggtt->vm.clear_range(&ggtt->vm, hole_start,
2715 				     hole_end - hole_start);
2716 	}
2717 
2718 	/* And finally clear the reserved guard page */
2719 	ggtt->vm.clear_range(&ggtt->vm, ggtt->vm.total - PAGE_SIZE, PAGE_SIZE);
2720 
2721 	return 0;
2722 
2723 err:
2724 	cleanup_init_ggtt(ggtt);
2725 	return ret;
2726 }
2727 
2728 int i915_init_ggtt(struct drm_i915_private *i915)
2729 {
2730 	int ret;
2731 
2732 	ret = init_ggtt(&i915->ggtt);
2733 	if (ret)
2734 		return ret;
2735 
2736 	if (INTEL_PPGTT(i915) == INTEL_PPGTT_ALIASING) {
2737 		ret = init_aliasing_ppgtt(&i915->ggtt);
2738 		if (ret)
2739 			cleanup_init_ggtt(&i915->ggtt);
2740 	}
2741 
2742 	return 0;
2743 }
2744 
2745 static void ggtt_cleanup_hw(struct i915_ggtt *ggtt)
2746 {
2747 	struct drm_i915_private *i915 = ggtt->vm.i915;
2748 	struct i915_vma *vma, *vn;
2749 
2750 	ggtt->vm.closed = true;
2751 
2752 	rcu_barrier(); /* flush the RCU'ed__i915_vm_release */
2753 	flush_workqueue(i915->wq);
2754 
2755 	mutex_lock(&i915->drm.struct_mutex);
2756 
2757 	list_for_each_entry_safe(vma, vn, &ggtt->vm.bound_list, vm_link)
2758 		WARN_ON(i915_vma_unbind(vma));
2759 
2760 	if (drm_mm_node_allocated(&ggtt->error_capture))
2761 		drm_mm_remove_node(&ggtt->error_capture);
2762 
2763 	ggtt_release_guc_top(ggtt);
2764 
2765 	if (drm_mm_initialized(&ggtt->vm.mm)) {
2766 		intel_vgt_deballoon(ggtt);
2767 		i915_address_space_fini(&ggtt->vm);
2768 	}
2769 
2770 	ggtt->vm.cleanup(&ggtt->vm);
2771 
2772 	mutex_unlock(&i915->drm.struct_mutex);
2773 
2774 	arch_phys_wc_del(ggtt->mtrr);
2775 	io_mapping_fini(&ggtt->iomap);
2776 }
2777 
2778 /**
2779  * i915_ggtt_driver_release - Clean up GGTT hardware initialization
2780  * @i915: i915 device
2781  */
2782 void i915_ggtt_driver_release(struct drm_i915_private *i915)
2783 {
2784 	struct pagevec *pvec;
2785 
2786 	fini_aliasing_ppgtt(&i915->ggtt);
2787 
2788 	ggtt_cleanup_hw(&i915->ggtt);
2789 
2790 	pvec = &i915->mm.wc_stash.pvec;
2791 	if (pvec->nr) {
2792 		set_pages_array_wb(pvec->pages, pvec->nr);
2793 		__pagevec_release(pvec);
2794 	}
2795 
2796 	i915_gem_cleanup_stolen(i915);
2797 }
2798 
2799 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2800 {
2801 	snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2802 	snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2803 	return snb_gmch_ctl << 20;
2804 }
2805 
2806 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2807 {
2808 	bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2809 	bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2810 	if (bdw_gmch_ctl)
2811 		bdw_gmch_ctl = 1 << bdw_gmch_ctl;
2812 
2813 #ifdef CONFIG_X86_32
2814 	/* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * I915_GTT_PAGE_SIZE */
2815 	if (bdw_gmch_ctl > 4)
2816 		bdw_gmch_ctl = 4;
2817 #endif
2818 
2819 	return bdw_gmch_ctl << 20;
2820 }
2821 
2822 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2823 {
2824 	gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2825 	gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2826 
2827 	if (gmch_ctrl)
2828 		return 1 << (20 + gmch_ctrl);
2829 
2830 	return 0;
2831 }
2832 
2833 static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
2834 {
2835 	struct drm_i915_private *dev_priv = ggtt->vm.i915;
2836 	struct pci_dev *pdev = dev_priv->drm.pdev;
2837 	phys_addr_t phys_addr;
2838 	int ret;
2839 
2840 	/* For Modern GENs the PTEs and register space are split in the BAR */
2841 	phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
2842 
2843 	/*
2844 	 * On BXT+/CNL+ writes larger than 64 bit to the GTT pagetable range
2845 	 * will be dropped. For WC mappings in general we have 64 byte burst
2846 	 * writes when the WC buffer is flushed, so we can't use it, but have to
2847 	 * resort to an uncached mapping. The WC issue is easily caught by the
2848 	 * readback check when writing GTT PTE entries.
2849 	 */
2850 	if (IS_GEN9_LP(dev_priv) || INTEL_GEN(dev_priv) >= 10)
2851 		ggtt->gsm = ioremap_nocache(phys_addr, size);
2852 	else
2853 		ggtt->gsm = ioremap_wc(phys_addr, size);
2854 	if (!ggtt->gsm) {
2855 		DRM_ERROR("Failed to map the ggtt page table\n");
2856 		return -ENOMEM;
2857 	}
2858 
2859 	ret = setup_scratch_page(&ggtt->vm, GFP_DMA32);
2860 	if (ret) {
2861 		DRM_ERROR("Scratch setup failed\n");
2862 		/* iounmap will also get called at remove, but meh */
2863 		iounmap(ggtt->gsm);
2864 		return ret;
2865 	}
2866 
2867 	ggtt->vm.scratch[0].encode =
2868 		ggtt->vm.pte_encode(px_dma(&ggtt->vm.scratch[0]),
2869 				    I915_CACHE_NONE, 0);
2870 
2871 	return 0;
2872 }
2873 
2874 static void tgl_setup_private_ppat(struct drm_i915_private *dev_priv)
2875 {
2876 	/* TGL doesn't support LLC or AGE settings */
2877 	I915_WRITE(GEN12_PAT_INDEX(0), GEN8_PPAT_WB);
2878 	I915_WRITE(GEN12_PAT_INDEX(1), GEN8_PPAT_WC);
2879 	I915_WRITE(GEN12_PAT_INDEX(2), GEN8_PPAT_WT);
2880 	I915_WRITE(GEN12_PAT_INDEX(3), GEN8_PPAT_UC);
2881 	I915_WRITE(GEN12_PAT_INDEX(4), GEN8_PPAT_WB);
2882 	I915_WRITE(GEN12_PAT_INDEX(5), GEN8_PPAT_WB);
2883 	I915_WRITE(GEN12_PAT_INDEX(6), GEN8_PPAT_WB);
2884 	I915_WRITE(GEN12_PAT_INDEX(7), GEN8_PPAT_WB);
2885 }
2886 
2887 static void cnl_setup_private_ppat(struct drm_i915_private *dev_priv)
2888 {
2889 	I915_WRITE(GEN10_PAT_INDEX(0), GEN8_PPAT_WB | GEN8_PPAT_LLC);
2890 	I915_WRITE(GEN10_PAT_INDEX(1), GEN8_PPAT_WC | GEN8_PPAT_LLCELLC);
2891 	I915_WRITE(GEN10_PAT_INDEX(2), GEN8_PPAT_WT | GEN8_PPAT_LLCELLC);
2892 	I915_WRITE(GEN10_PAT_INDEX(3), GEN8_PPAT_UC);
2893 	I915_WRITE(GEN10_PAT_INDEX(4), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0));
2894 	I915_WRITE(GEN10_PAT_INDEX(5), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1));
2895 	I915_WRITE(GEN10_PAT_INDEX(6), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2));
2896 	I915_WRITE(GEN10_PAT_INDEX(7), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2897 }
2898 
2899 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2900  * bits. When using advanced contexts each context stores its own PAT, but
2901  * writing this data shouldn't be harmful even in those cases. */
2902 static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
2903 {
2904 	u64 pat;
2905 
2906 	pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) |	/* for normal objects, no eLLC */
2907 	      GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) |	/* for something pointing to ptes? */
2908 	      GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) |	/* for scanout with eLLC */
2909 	      GEN8_PPAT(3, GEN8_PPAT_UC) |			/* Uncached objects, mostly for scanout */
2910 	      GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2911 	      GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2912 	      GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2913 	      GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2914 
2915 	I915_WRITE(GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
2916 	I915_WRITE(GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
2917 }
2918 
2919 static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2920 {
2921 	u64 pat;
2922 
2923 	/*
2924 	 * Map WB on BDW to snooped on CHV.
2925 	 *
2926 	 * Only the snoop bit has meaning for CHV, the rest is
2927 	 * ignored.
2928 	 *
2929 	 * The hardware will never snoop for certain types of accesses:
2930 	 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2931 	 * - PPGTT page tables
2932 	 * - some other special cycles
2933 	 *
2934 	 * As with BDW, we also need to consider the following for GT accesses:
2935 	 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2936 	 * so RTL will always use the value corresponding to
2937 	 * pat_sel = 000".
2938 	 * Which means we must set the snoop bit in PAT entry 0
2939 	 * in order to keep the global status page working.
2940 	 */
2941 
2942 	pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2943 	      GEN8_PPAT(1, 0) |
2944 	      GEN8_PPAT(2, 0) |
2945 	      GEN8_PPAT(3, 0) |
2946 	      GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2947 	      GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2948 	      GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2949 	      GEN8_PPAT(7, CHV_PPAT_SNOOP);
2950 
2951 	I915_WRITE(GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
2952 	I915_WRITE(GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
2953 }
2954 
2955 static void gen6_gmch_remove(struct i915_address_space *vm)
2956 {
2957 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2958 
2959 	iounmap(ggtt->gsm);
2960 	cleanup_scratch_page(vm);
2961 }
2962 
2963 static void setup_private_pat(struct drm_i915_private *dev_priv)
2964 {
2965 	GEM_BUG_ON(INTEL_GEN(dev_priv) < 8);
2966 
2967 	if (INTEL_GEN(dev_priv) >= 12)
2968 		tgl_setup_private_ppat(dev_priv);
2969 	else if (INTEL_GEN(dev_priv) >= 10)
2970 		cnl_setup_private_ppat(dev_priv);
2971 	else if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
2972 		chv_setup_private_ppat(dev_priv);
2973 	else
2974 		bdw_setup_private_ppat(dev_priv);
2975 }
2976 
2977 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
2978 {
2979 	struct drm_i915_private *dev_priv = ggtt->vm.i915;
2980 	struct pci_dev *pdev = dev_priv->drm.pdev;
2981 	unsigned int size;
2982 	u16 snb_gmch_ctl;
2983 	int err;
2984 
2985 	/* TODO: We're not aware of mappable constraints on gen8 yet */
2986 	ggtt->gmadr =
2987 		(struct resource) DEFINE_RES_MEM(pci_resource_start(pdev, 2),
2988 						 pci_resource_len(pdev, 2));
2989 	ggtt->mappable_end = resource_size(&ggtt->gmadr);
2990 
2991 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(39));
2992 	if (!err)
2993 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
2994 	if (err)
2995 		DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
2996 
2997 	pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2998 	if (IS_CHERRYVIEW(dev_priv))
2999 		size = chv_get_total_gtt_size(snb_gmch_ctl);
3000 	else
3001 		size = gen8_get_total_gtt_size(snb_gmch_ctl);
3002 
3003 	ggtt->vm.total = (size / sizeof(gen8_pte_t)) * I915_GTT_PAGE_SIZE;
3004 	ggtt->vm.cleanup = gen6_gmch_remove;
3005 	ggtt->vm.insert_page = gen8_ggtt_insert_page;
3006 	ggtt->vm.clear_range = nop_clear_range;
3007 	if (intel_scanout_needs_vtd_wa(dev_priv))
3008 		ggtt->vm.clear_range = gen8_ggtt_clear_range;
3009 
3010 	ggtt->vm.insert_entries = gen8_ggtt_insert_entries;
3011 
3012 	/* Serialize GTT updates with aperture access on BXT if VT-d is on. */
3013 	if (intel_ggtt_update_needs_vtd_wa(dev_priv) ||
3014 	    IS_CHERRYVIEW(dev_priv) /* fails with concurrent use/update */) {
3015 		ggtt->vm.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
3016 		ggtt->vm.insert_page    = bxt_vtd_ggtt_insert_page__BKL;
3017 		if (ggtt->vm.clear_range != nop_clear_range)
3018 			ggtt->vm.clear_range = bxt_vtd_ggtt_clear_range__BKL;
3019 	}
3020 
3021 	ggtt->invalidate = gen6_ggtt_invalidate;
3022 
3023 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
3024 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
3025 	ggtt->vm.vma_ops.set_pages   = ggtt_set_pages;
3026 	ggtt->vm.vma_ops.clear_pages = clear_pages;
3027 
3028 	ggtt->vm.pte_encode = gen8_pte_encode;
3029 
3030 	setup_private_pat(dev_priv);
3031 
3032 	return ggtt_probe_common(ggtt, size);
3033 }
3034 
3035 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
3036 {
3037 	struct drm_i915_private *dev_priv = ggtt->vm.i915;
3038 	struct pci_dev *pdev = dev_priv->drm.pdev;
3039 	unsigned int size;
3040 	u16 snb_gmch_ctl;
3041 	int err;
3042 
3043 	ggtt->gmadr =
3044 		(struct resource) DEFINE_RES_MEM(pci_resource_start(pdev, 2),
3045 						 pci_resource_len(pdev, 2));
3046 	ggtt->mappable_end = resource_size(&ggtt->gmadr);
3047 
3048 	/* 64/512MB is the current min/max we actually know of, but this is just
3049 	 * a coarse sanity check.
3050 	 */
3051 	if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
3052 		DRM_ERROR("Unknown GMADR size (%pa)\n", &ggtt->mappable_end);
3053 		return -ENXIO;
3054 	}
3055 
3056 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
3057 	if (!err)
3058 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3059 	if (err)
3060 		DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
3061 	pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3062 
3063 	size = gen6_get_total_gtt_size(snb_gmch_ctl);
3064 	ggtt->vm.total = (size / sizeof(gen6_pte_t)) * I915_GTT_PAGE_SIZE;
3065 
3066 	ggtt->vm.clear_range = nop_clear_range;
3067 	if (!HAS_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
3068 		ggtt->vm.clear_range = gen6_ggtt_clear_range;
3069 	ggtt->vm.insert_page = gen6_ggtt_insert_page;
3070 	ggtt->vm.insert_entries = gen6_ggtt_insert_entries;
3071 	ggtt->vm.cleanup = gen6_gmch_remove;
3072 
3073 	ggtt->invalidate = gen6_ggtt_invalidate;
3074 
3075 	if (HAS_EDRAM(dev_priv))
3076 		ggtt->vm.pte_encode = iris_pte_encode;
3077 	else if (IS_HASWELL(dev_priv))
3078 		ggtt->vm.pte_encode = hsw_pte_encode;
3079 	else if (IS_VALLEYVIEW(dev_priv))
3080 		ggtt->vm.pte_encode = byt_pte_encode;
3081 	else if (INTEL_GEN(dev_priv) >= 7)
3082 		ggtt->vm.pte_encode = ivb_pte_encode;
3083 	else
3084 		ggtt->vm.pte_encode = snb_pte_encode;
3085 
3086 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
3087 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
3088 	ggtt->vm.vma_ops.set_pages   = ggtt_set_pages;
3089 	ggtt->vm.vma_ops.clear_pages = clear_pages;
3090 
3091 	return ggtt_probe_common(ggtt, size);
3092 }
3093 
3094 static void i915_gmch_remove(struct i915_address_space *vm)
3095 {
3096 	intel_gmch_remove();
3097 }
3098 
3099 static int i915_gmch_probe(struct i915_ggtt *ggtt)
3100 {
3101 	struct drm_i915_private *dev_priv = ggtt->vm.i915;
3102 	phys_addr_t gmadr_base;
3103 	int ret;
3104 
3105 	ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
3106 	if (!ret) {
3107 		DRM_ERROR("failed to set up gmch\n");
3108 		return -EIO;
3109 	}
3110 
3111 	intel_gtt_get(&ggtt->vm.total, &gmadr_base, &ggtt->mappable_end);
3112 
3113 	ggtt->gmadr =
3114 		(struct resource) DEFINE_RES_MEM(gmadr_base,
3115 						 ggtt->mappable_end);
3116 
3117 	ggtt->do_idle_maps = needs_idle_maps(dev_priv);
3118 	ggtt->vm.insert_page = i915_ggtt_insert_page;
3119 	ggtt->vm.insert_entries = i915_ggtt_insert_entries;
3120 	ggtt->vm.clear_range = i915_ggtt_clear_range;
3121 	ggtt->vm.cleanup = i915_gmch_remove;
3122 
3123 	ggtt->invalidate = gmch_ggtt_invalidate;
3124 
3125 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
3126 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
3127 	ggtt->vm.vma_ops.set_pages   = ggtt_set_pages;
3128 	ggtt->vm.vma_ops.clear_pages = clear_pages;
3129 
3130 	if (unlikely(ggtt->do_idle_maps))
3131 		dev_notice(dev_priv->drm.dev,
3132 			   "Applying Ironlake quirks for intel_iommu\n");
3133 
3134 	return 0;
3135 }
3136 
3137 static int ggtt_probe_hw(struct i915_ggtt *ggtt, struct intel_gt *gt)
3138 {
3139 	struct drm_i915_private *i915 = gt->i915;
3140 	int ret;
3141 
3142 	ggtt->vm.gt = gt;
3143 	ggtt->vm.i915 = i915;
3144 	ggtt->vm.dma = &i915->drm.pdev->dev;
3145 
3146 	if (INTEL_GEN(i915) <= 5)
3147 		ret = i915_gmch_probe(ggtt);
3148 	else if (INTEL_GEN(i915) < 8)
3149 		ret = gen6_gmch_probe(ggtt);
3150 	else
3151 		ret = gen8_gmch_probe(ggtt);
3152 	if (ret)
3153 		return ret;
3154 
3155 	if ((ggtt->vm.total - 1) >> 32) {
3156 		DRM_ERROR("We never expected a Global GTT with more than 32bits"
3157 			  " of address space! Found %lldM!\n",
3158 			  ggtt->vm.total >> 20);
3159 		ggtt->vm.total = 1ULL << 32;
3160 		ggtt->mappable_end =
3161 			min_t(u64, ggtt->mappable_end, ggtt->vm.total);
3162 	}
3163 
3164 	if (ggtt->mappable_end > ggtt->vm.total) {
3165 		DRM_ERROR("mappable aperture extends past end of GGTT,"
3166 			  " aperture=%pa, total=%llx\n",
3167 			  &ggtt->mappable_end, ggtt->vm.total);
3168 		ggtt->mappable_end = ggtt->vm.total;
3169 	}
3170 
3171 	/* GMADR is the PCI mmio aperture into the global GTT. */
3172 	DRM_DEBUG_DRIVER("GGTT size = %lluM\n", ggtt->vm.total >> 20);
3173 	DRM_DEBUG_DRIVER("GMADR size = %lluM\n", (u64)ggtt->mappable_end >> 20);
3174 	DRM_DEBUG_DRIVER("DSM size = %lluM\n",
3175 			 (u64)resource_size(&intel_graphics_stolen_res) >> 20);
3176 
3177 	return 0;
3178 }
3179 
3180 /**
3181  * i915_ggtt_probe_hw - Probe GGTT hardware location
3182  * @i915: i915 device
3183  */
3184 int i915_ggtt_probe_hw(struct drm_i915_private *i915)
3185 {
3186 	int ret;
3187 
3188 	ret = ggtt_probe_hw(&i915->ggtt, &i915->gt);
3189 	if (ret)
3190 		return ret;
3191 
3192 	if (intel_vtd_active())
3193 		dev_info(i915->drm.dev, "VT-d active for gfx access\n");
3194 
3195 	return 0;
3196 }
3197 
3198 static int ggtt_init_hw(struct i915_ggtt *ggtt)
3199 {
3200 	struct drm_i915_private *i915 = ggtt->vm.i915;
3201 	int ret = 0;
3202 
3203 	mutex_lock(&i915->drm.struct_mutex);
3204 
3205 	i915_address_space_init(&ggtt->vm, VM_CLASS_GGTT);
3206 
3207 	ggtt->vm.is_ggtt = true;
3208 
3209 	/* Only VLV supports read-only GGTT mappings */
3210 	ggtt->vm.has_read_only = IS_VALLEYVIEW(i915);
3211 
3212 	if (!HAS_LLC(i915) && !HAS_PPGTT(i915))
3213 		ggtt->vm.mm.color_adjust = i915_gtt_color_adjust;
3214 
3215 	if (!io_mapping_init_wc(&ggtt->iomap,
3216 				ggtt->gmadr.start,
3217 				ggtt->mappable_end)) {
3218 		ggtt->vm.cleanup(&ggtt->vm);
3219 		ret = -EIO;
3220 		goto out;
3221 	}
3222 
3223 	ggtt->mtrr = arch_phys_wc_add(ggtt->gmadr.start, ggtt->mappable_end);
3224 
3225 	i915_ggtt_init_fences(ggtt);
3226 
3227 out:
3228 	mutex_unlock(&i915->drm.struct_mutex);
3229 
3230 	return ret;
3231 }
3232 
3233 /**
3234  * i915_ggtt_init_hw - Initialize GGTT hardware
3235  * @dev_priv: i915 device
3236  */
3237 int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
3238 {
3239 	int ret;
3240 
3241 	stash_init(&dev_priv->mm.wc_stash);
3242 
3243 	/* Note that we use page colouring to enforce a guard page at the
3244 	 * end of the address space. This is required as the CS may prefetch
3245 	 * beyond the end of the batch buffer, across the page boundary,
3246 	 * and beyond the end of the GTT if we do not provide a guard.
3247 	 */
3248 	ret = ggtt_init_hw(&dev_priv->ggtt);
3249 	if (ret)
3250 		return ret;
3251 
3252 	/*
3253 	 * Initialise stolen early so that we may reserve preallocated
3254 	 * objects for the BIOS to KMS transition.
3255 	 */
3256 	ret = i915_gem_init_stolen(dev_priv);
3257 	if (ret)
3258 		goto out_gtt_cleanup;
3259 
3260 	return 0;
3261 
3262 out_gtt_cleanup:
3263 	dev_priv->ggtt.vm.cleanup(&dev_priv->ggtt.vm);
3264 	return ret;
3265 }
3266 
3267 int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
3268 {
3269 	if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
3270 		return -EIO;
3271 
3272 	return 0;
3273 }
3274 
3275 void i915_ggtt_enable_guc(struct i915_ggtt *ggtt)
3276 {
3277 	GEM_BUG_ON(ggtt->invalidate != gen6_ggtt_invalidate);
3278 
3279 	ggtt->invalidate = guc_ggtt_invalidate;
3280 
3281 	ggtt->invalidate(ggtt);
3282 }
3283 
3284 void i915_ggtt_disable_guc(struct i915_ggtt *ggtt)
3285 {
3286 	/* XXX Temporary pardon for error unload */
3287 	if (ggtt->invalidate == gen6_ggtt_invalidate)
3288 		return;
3289 
3290 	/* We should only be called after i915_ggtt_enable_guc() */
3291 	GEM_BUG_ON(ggtt->invalidate != guc_ggtt_invalidate);
3292 
3293 	ggtt->invalidate = gen6_ggtt_invalidate;
3294 
3295 	ggtt->invalidate(ggtt);
3296 }
3297 
3298 static void ggtt_restore_mappings(struct i915_ggtt *ggtt)
3299 {
3300 	struct i915_vma *vma, *vn;
3301 	bool flush = false;
3302 
3303 	intel_gt_check_and_clear_faults(ggtt->vm.gt);
3304 
3305 	mutex_lock(&ggtt->vm.mutex);
3306 
3307 	/* First fill our portion of the GTT with scratch pages */
3308 	ggtt->vm.clear_range(&ggtt->vm, 0, ggtt->vm.total);
3309 	ggtt->vm.closed = true; /* skip rewriting PTE on VMA unbind */
3310 
3311 	/* clflush objects bound into the GGTT and rebind them. */
3312 	list_for_each_entry_safe(vma, vn, &ggtt->vm.bound_list, vm_link) {
3313 		struct drm_i915_gem_object *obj = vma->obj;
3314 
3315 		if (!(vma->flags & I915_VMA_GLOBAL_BIND))
3316 			continue;
3317 
3318 		mutex_unlock(&ggtt->vm.mutex);
3319 
3320 		if (!i915_vma_unbind(vma))
3321 			goto lock;
3322 
3323 		WARN_ON(i915_vma_bind(vma,
3324 				      obj ? obj->cache_level : 0,
3325 				      PIN_UPDATE));
3326 		if (obj) { /* only used during resume => exclusive access */
3327 			flush |= fetch_and_zero(&obj->write_domain);
3328 			obj->read_domains |= I915_GEM_DOMAIN_GTT;
3329 		}
3330 
3331 lock:
3332 		mutex_lock(&ggtt->vm.mutex);
3333 	}
3334 
3335 	ggtt->vm.closed = false;
3336 	ggtt->invalidate(ggtt);
3337 
3338 	mutex_unlock(&ggtt->vm.mutex);
3339 
3340 	if (flush)
3341 		wbinvd_on_all_cpus();
3342 }
3343 
3344 void i915_gem_restore_gtt_mappings(struct drm_i915_private *i915)
3345 {
3346 	ggtt_restore_mappings(&i915->ggtt);
3347 
3348 	if (INTEL_GEN(i915) >= 8)
3349 		setup_private_pat(i915);
3350 }
3351 
3352 static struct scatterlist *
3353 rotate_pages(struct drm_i915_gem_object *obj, unsigned int offset,
3354 	     unsigned int width, unsigned int height,
3355 	     unsigned int stride,
3356 	     struct sg_table *st, struct scatterlist *sg)
3357 {
3358 	unsigned int column, row;
3359 	unsigned int src_idx;
3360 
3361 	for (column = 0; column < width; column++) {
3362 		src_idx = stride * (height - 1) + column + offset;
3363 		for (row = 0; row < height; row++) {
3364 			st->nents++;
3365 			/* We don't need the pages, but need to initialize
3366 			 * the entries so the sg list can be happily traversed.
3367 			 * The only thing we need are DMA addresses.
3368 			 */
3369 			sg_set_page(sg, NULL, I915_GTT_PAGE_SIZE, 0);
3370 			sg_dma_address(sg) =
3371 				i915_gem_object_get_dma_address(obj, src_idx);
3372 			sg_dma_len(sg) = I915_GTT_PAGE_SIZE;
3373 			sg = sg_next(sg);
3374 			src_idx -= stride;
3375 		}
3376 	}
3377 
3378 	return sg;
3379 }
3380 
3381 static noinline struct sg_table *
3382 intel_rotate_pages(struct intel_rotation_info *rot_info,
3383 		   struct drm_i915_gem_object *obj)
3384 {
3385 	unsigned int size = intel_rotation_info_size(rot_info);
3386 	struct sg_table *st;
3387 	struct scatterlist *sg;
3388 	int ret = -ENOMEM;
3389 	int i;
3390 
3391 	/* Allocate target SG list. */
3392 	st = kmalloc(sizeof(*st), GFP_KERNEL);
3393 	if (!st)
3394 		goto err_st_alloc;
3395 
3396 	ret = sg_alloc_table(st, size, GFP_KERNEL);
3397 	if (ret)
3398 		goto err_sg_alloc;
3399 
3400 	st->nents = 0;
3401 	sg = st->sgl;
3402 
3403 	for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3404 		sg = rotate_pages(obj, rot_info->plane[i].offset,
3405 				  rot_info->plane[i].width, rot_info->plane[i].height,
3406 				  rot_info->plane[i].stride, st, sg);
3407 	}
3408 
3409 	return st;
3410 
3411 err_sg_alloc:
3412 	kfree(st);
3413 err_st_alloc:
3414 
3415 	DRM_DEBUG_DRIVER("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3416 			 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3417 
3418 	return ERR_PTR(ret);
3419 }
3420 
3421 static struct scatterlist *
3422 remap_pages(struct drm_i915_gem_object *obj, unsigned int offset,
3423 	    unsigned int width, unsigned int height,
3424 	    unsigned int stride,
3425 	    struct sg_table *st, struct scatterlist *sg)
3426 {
3427 	unsigned int row;
3428 
3429 	for (row = 0; row < height; row++) {
3430 		unsigned int left = width * I915_GTT_PAGE_SIZE;
3431 
3432 		while (left) {
3433 			dma_addr_t addr;
3434 			unsigned int length;
3435 
3436 			/* We don't need the pages, but need to initialize
3437 			 * the entries so the sg list can be happily traversed.
3438 			 * The only thing we need are DMA addresses.
3439 			 */
3440 
3441 			addr = i915_gem_object_get_dma_address_len(obj, offset, &length);
3442 
3443 			length = min(left, length);
3444 
3445 			st->nents++;
3446 
3447 			sg_set_page(sg, NULL, length, 0);
3448 			sg_dma_address(sg) = addr;
3449 			sg_dma_len(sg) = length;
3450 			sg = sg_next(sg);
3451 
3452 			offset += length / I915_GTT_PAGE_SIZE;
3453 			left -= length;
3454 		}
3455 
3456 		offset += stride - width;
3457 	}
3458 
3459 	return sg;
3460 }
3461 
3462 static noinline struct sg_table *
3463 intel_remap_pages(struct intel_remapped_info *rem_info,
3464 		  struct drm_i915_gem_object *obj)
3465 {
3466 	unsigned int size = intel_remapped_info_size(rem_info);
3467 	struct sg_table *st;
3468 	struct scatterlist *sg;
3469 	int ret = -ENOMEM;
3470 	int i;
3471 
3472 	/* Allocate target SG list. */
3473 	st = kmalloc(sizeof(*st), GFP_KERNEL);
3474 	if (!st)
3475 		goto err_st_alloc;
3476 
3477 	ret = sg_alloc_table(st, size, GFP_KERNEL);
3478 	if (ret)
3479 		goto err_sg_alloc;
3480 
3481 	st->nents = 0;
3482 	sg = st->sgl;
3483 
3484 	for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++) {
3485 		sg = remap_pages(obj, rem_info->plane[i].offset,
3486 				 rem_info->plane[i].width, rem_info->plane[i].height,
3487 				 rem_info->plane[i].stride, st, sg);
3488 	}
3489 
3490 	i915_sg_trim(st);
3491 
3492 	return st;
3493 
3494 err_sg_alloc:
3495 	kfree(st);
3496 err_st_alloc:
3497 
3498 	DRM_DEBUG_DRIVER("Failed to create remapped mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3499 			 obj->base.size, rem_info->plane[0].width, rem_info->plane[0].height, size);
3500 
3501 	return ERR_PTR(ret);
3502 }
3503 
3504 static noinline struct sg_table *
3505 intel_partial_pages(const struct i915_ggtt_view *view,
3506 		    struct drm_i915_gem_object *obj)
3507 {
3508 	struct sg_table *st;
3509 	struct scatterlist *sg, *iter;
3510 	unsigned int count = view->partial.size;
3511 	unsigned int offset;
3512 	int ret = -ENOMEM;
3513 
3514 	st = kmalloc(sizeof(*st), GFP_KERNEL);
3515 	if (!st)
3516 		goto err_st_alloc;
3517 
3518 	ret = sg_alloc_table(st, count, GFP_KERNEL);
3519 	if (ret)
3520 		goto err_sg_alloc;
3521 
3522 	iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
3523 	GEM_BUG_ON(!iter);
3524 
3525 	sg = st->sgl;
3526 	st->nents = 0;
3527 	do {
3528 		unsigned int len;
3529 
3530 		len = min(iter->length - (offset << PAGE_SHIFT),
3531 			  count << PAGE_SHIFT);
3532 		sg_set_page(sg, NULL, len, 0);
3533 		sg_dma_address(sg) =
3534 			sg_dma_address(iter) + (offset << PAGE_SHIFT);
3535 		sg_dma_len(sg) = len;
3536 
3537 		st->nents++;
3538 		count -= len >> PAGE_SHIFT;
3539 		if (count == 0) {
3540 			sg_mark_end(sg);
3541 			i915_sg_trim(st); /* Drop any unused tail entries. */
3542 
3543 			return st;
3544 		}
3545 
3546 		sg = __sg_next(sg);
3547 		iter = __sg_next(iter);
3548 		offset = 0;
3549 	} while (1);
3550 
3551 err_sg_alloc:
3552 	kfree(st);
3553 err_st_alloc:
3554 	return ERR_PTR(ret);
3555 }
3556 
3557 static int
3558 i915_get_ggtt_vma_pages(struct i915_vma *vma)
3559 {
3560 	int ret;
3561 
3562 	/* The vma->pages are only valid within the lifespan of the borrowed
3563 	 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3564 	 * must be the vma->pages. A simple rule is that vma->pages must only
3565 	 * be accessed when the obj->mm.pages are pinned.
3566 	 */
3567 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3568 
3569 	switch (vma->ggtt_view.type) {
3570 	default:
3571 		GEM_BUG_ON(vma->ggtt_view.type);
3572 		/* fall through */
3573 	case I915_GGTT_VIEW_NORMAL:
3574 		vma->pages = vma->obj->mm.pages;
3575 		return 0;
3576 
3577 	case I915_GGTT_VIEW_ROTATED:
3578 		vma->pages =
3579 			intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3580 		break;
3581 
3582 	case I915_GGTT_VIEW_REMAPPED:
3583 		vma->pages =
3584 			intel_remap_pages(&vma->ggtt_view.remapped, vma->obj);
3585 		break;
3586 
3587 	case I915_GGTT_VIEW_PARTIAL:
3588 		vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
3589 		break;
3590 	}
3591 
3592 	ret = 0;
3593 	if (IS_ERR(vma->pages)) {
3594 		ret = PTR_ERR(vma->pages);
3595 		vma->pages = NULL;
3596 		DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3597 			  vma->ggtt_view.type, ret);
3598 	}
3599 	return ret;
3600 }
3601 
3602 /**
3603  * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
3604  * @vm: the &struct i915_address_space
3605  * @node: the &struct drm_mm_node (typically i915_vma.mode)
3606  * @size: how much space to allocate inside the GTT,
3607  *        must be #I915_GTT_PAGE_SIZE aligned
3608  * @offset: where to insert inside the GTT,
3609  *          must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3610  *          (@offset + @size) must fit within the address space
3611  * @color: color to apply to node, if this node is not from a VMA,
3612  *         color must be #I915_COLOR_UNEVICTABLE
3613  * @flags: control search and eviction behaviour
3614  *
3615  * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3616  * the address space (using @size and @color). If the @node does not fit, it
3617  * tries to evict any overlapping nodes from the GTT, including any
3618  * neighbouring nodes if the colors do not match (to ensure guard pages between
3619  * differing domains). See i915_gem_evict_for_node() for the gory details
3620  * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3621  * evicting active overlapping objects, and any overlapping node that is pinned
3622  * or marked as unevictable will also result in failure.
3623  *
3624  * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3625  * asked to wait for eviction and interrupted.
3626  */
3627 int i915_gem_gtt_reserve(struct i915_address_space *vm,
3628 			 struct drm_mm_node *node,
3629 			 u64 size, u64 offset, unsigned long color,
3630 			 unsigned int flags)
3631 {
3632 	int err;
3633 
3634 	GEM_BUG_ON(!size);
3635 	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3636 	GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3637 	GEM_BUG_ON(range_overflows(offset, size, vm->total));
3638 	GEM_BUG_ON(vm == &vm->i915->ggtt.alias->vm);
3639 	GEM_BUG_ON(drm_mm_node_allocated(node));
3640 
3641 	node->size = size;
3642 	node->start = offset;
3643 	node->color = color;
3644 
3645 	err = drm_mm_reserve_node(&vm->mm, node);
3646 	if (err != -ENOSPC)
3647 		return err;
3648 
3649 	if (flags & PIN_NOEVICT)
3650 		return -ENOSPC;
3651 
3652 	err = i915_gem_evict_for_node(vm, node, flags);
3653 	if (err == 0)
3654 		err = drm_mm_reserve_node(&vm->mm, node);
3655 
3656 	return err;
3657 }
3658 
3659 static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3660 {
3661 	u64 range, addr;
3662 
3663 	GEM_BUG_ON(range_overflows(start, len, end));
3664 	GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3665 
3666 	range = round_down(end - len, align) - round_up(start, align);
3667 	if (range) {
3668 		if (sizeof(unsigned long) == sizeof(u64)) {
3669 			addr = get_random_long();
3670 		} else {
3671 			addr = get_random_int();
3672 			if (range > U32_MAX) {
3673 				addr <<= 32;
3674 				addr |= get_random_int();
3675 			}
3676 		}
3677 		div64_u64_rem(addr, range, &addr);
3678 		start += addr;
3679 	}
3680 
3681 	return round_up(start, align);
3682 }
3683 
3684 /**
3685  * i915_gem_gtt_insert - insert a node into an address_space (GTT)
3686  * @vm: the &struct i915_address_space
3687  * @node: the &struct drm_mm_node (typically i915_vma.node)
3688  * @size: how much space to allocate inside the GTT,
3689  *        must be #I915_GTT_PAGE_SIZE aligned
3690  * @alignment: required alignment of starting offset, may be 0 but
3691  *             if specified, this must be a power-of-two and at least
3692  *             #I915_GTT_MIN_ALIGNMENT
3693  * @color: color to apply to node
3694  * @start: start of any range restriction inside GTT (0 for all),
3695  *         must be #I915_GTT_PAGE_SIZE aligned
3696  * @end: end of any range restriction inside GTT (U64_MAX for all),
3697  *       must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3698  * @flags: control search and eviction behaviour
3699  *
3700  * i915_gem_gtt_insert() first searches for an available hole into which
3701  * is can insert the node. The hole address is aligned to @alignment and
3702  * its @size must then fit entirely within the [@start, @end] bounds. The
3703  * nodes on either side of the hole must match @color, or else a guard page
3704  * will be inserted between the two nodes (or the node evicted). If no
3705  * suitable hole is found, first a victim is randomly selected and tested
3706  * for eviction, otherwise then the LRU list of objects within the GTT
3707  * is scanned to find the first set of replacement nodes to create the hole.
3708  * Those old overlapping nodes are evicted from the GTT (and so must be
3709  * rebound before any future use). Any node that is currently pinned cannot
3710  * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3711  * active and #PIN_NONBLOCK is specified, that node is also skipped when
3712  * searching for an eviction candidate. See i915_gem_evict_something() for
3713  * the gory details on the eviction algorithm.
3714  *
3715  * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3716  * asked to wait for eviction and interrupted.
3717  */
3718 int i915_gem_gtt_insert(struct i915_address_space *vm,
3719 			struct drm_mm_node *node,
3720 			u64 size, u64 alignment, unsigned long color,
3721 			u64 start, u64 end, unsigned int flags)
3722 {
3723 	enum drm_mm_insert_mode mode;
3724 	u64 offset;
3725 	int err;
3726 
3727 	lockdep_assert_held(&vm->i915->drm.struct_mutex);
3728 	GEM_BUG_ON(!size);
3729 	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3730 	GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3731 	GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3732 	GEM_BUG_ON(start >= end);
3733 	GEM_BUG_ON(start > 0  && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3734 	GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
3735 	GEM_BUG_ON(vm == &vm->i915->ggtt.alias->vm);
3736 	GEM_BUG_ON(drm_mm_node_allocated(node));
3737 
3738 	if (unlikely(range_overflows(start, size, end)))
3739 		return -ENOSPC;
3740 
3741 	if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3742 		return -ENOSPC;
3743 
3744 	mode = DRM_MM_INSERT_BEST;
3745 	if (flags & PIN_HIGH)
3746 		mode = DRM_MM_INSERT_HIGHEST;
3747 	if (flags & PIN_MAPPABLE)
3748 		mode = DRM_MM_INSERT_LOW;
3749 
3750 	/* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3751 	 * so we know that we always have a minimum alignment of 4096.
3752 	 * The drm_mm range manager is optimised to return results
3753 	 * with zero alignment, so where possible use the optimal
3754 	 * path.
3755 	 */
3756 	BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3757 	if (alignment <= I915_GTT_MIN_ALIGNMENT)
3758 		alignment = 0;
3759 
3760 	err = drm_mm_insert_node_in_range(&vm->mm, node,
3761 					  size, alignment, color,
3762 					  start, end, mode);
3763 	if (err != -ENOSPC)
3764 		return err;
3765 
3766 	if (mode & DRM_MM_INSERT_ONCE) {
3767 		err = drm_mm_insert_node_in_range(&vm->mm, node,
3768 						  size, alignment, color,
3769 						  start, end,
3770 						  DRM_MM_INSERT_BEST);
3771 		if (err != -ENOSPC)
3772 			return err;
3773 	}
3774 
3775 	if (flags & PIN_NOEVICT)
3776 		return -ENOSPC;
3777 
3778 	/*
3779 	 * No free space, pick a slot at random.
3780 	 *
3781 	 * There is a pathological case here using a GTT shared between
3782 	 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3783 	 *
3784 	 *    |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3785 	 *         (64k objects)             (448k objects)
3786 	 *
3787 	 * Now imagine that the eviction LRU is ordered top-down (just because
3788 	 * pathology meets real life), and that we need to evict an object to
3789 	 * make room inside the aperture. The eviction scan then has to walk
3790 	 * the 448k list before it finds one within range. And now imagine that
3791 	 * it has to search for a new hole between every byte inside the memcpy,
3792 	 * for several simultaneous clients.
3793 	 *
3794 	 * On a full-ppgtt system, if we have run out of available space, there
3795 	 * will be lots and lots of objects in the eviction list! Again,
3796 	 * searching that LRU list may be slow if we are also applying any
3797 	 * range restrictions (e.g. restriction to low 4GiB) and so, for
3798 	 * simplicity and similarilty between different GTT, try the single
3799 	 * random replacement first.
3800 	 */
3801 	offset = random_offset(start, end,
3802 			       size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3803 	err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3804 	if (err != -ENOSPC)
3805 		return err;
3806 
3807 	if (flags & PIN_NOSEARCH)
3808 		return -ENOSPC;
3809 
3810 	/* Randomly selected placement is pinned, do a search */
3811 	err = i915_gem_evict_something(vm, size, alignment, color,
3812 				       start, end, flags);
3813 	if (err)
3814 		return err;
3815 
3816 	return drm_mm_insert_node_in_range(&vm->mm, node,
3817 					   size, alignment, color,
3818 					   start, end, DRM_MM_INSERT_EVICT);
3819 }
3820 
3821 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3822 #include "selftests/mock_gtt.c"
3823 #include "selftests/i915_gem_gtt.c"
3824 #endif
3825