xref: /linux/drivers/gpu/drm/i915/gt/intel_ggtt.c (revision b9d7eb6a31be296ca0af95641a23c4c758703c0a)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2020 Intel Corporation
4  */
5 
6 #include <linux/agp_backend.h>
7 #include <linux/stop_machine.h>
8 
9 #include <asm/set_memory.h>
10 #include <asm/smp.h>
11 
12 #include <drm/i915_drm.h>
13 #include <drm/intel-gtt.h>
14 
15 #include "gem/i915_gem_lmem.h"
16 
17 #include "intel_gt.h"
18 #include "intel_gt_regs.h"
19 #include "i915_drv.h"
20 #include "i915_scatterlist.h"
21 #include "i915_vgpu.h"
22 
23 #include "intel_gtt.h"
24 #include "gen8_ppgtt.h"
25 
26 static void i915_ggtt_color_adjust(const struct drm_mm_node *node,
27 				   unsigned long color,
28 				   u64 *start,
29 				   u64 *end)
30 {
31 	if (i915_node_color_differs(node, color))
32 		*start += I915_GTT_PAGE_SIZE;
33 
34 	/*
35 	 * Also leave a space between the unallocated reserved node after the
36 	 * GTT and any objects within the GTT, i.e. we use the color adjustment
37 	 * to insert a guard page to prevent prefetches crossing over the
38 	 * GTT boundary.
39 	 */
40 	node = list_next_entry(node, node_list);
41 	if (node->color != color)
42 		*end -= I915_GTT_PAGE_SIZE;
43 }
44 
45 static int ggtt_init_hw(struct i915_ggtt *ggtt)
46 {
47 	struct drm_i915_private *i915 = ggtt->vm.i915;
48 
49 	i915_address_space_init(&ggtt->vm, VM_CLASS_GGTT);
50 
51 	ggtt->vm.is_ggtt = true;
52 
53 	/* Only VLV supports read-only GGTT mappings */
54 	ggtt->vm.has_read_only = IS_VALLEYVIEW(i915);
55 
56 	if (!HAS_LLC(i915) && !HAS_PPGTT(i915))
57 		ggtt->vm.mm.color_adjust = i915_ggtt_color_adjust;
58 
59 	if (ggtt->mappable_end) {
60 		if (!io_mapping_init_wc(&ggtt->iomap,
61 					ggtt->gmadr.start,
62 					ggtt->mappable_end)) {
63 			ggtt->vm.cleanup(&ggtt->vm);
64 			return -EIO;
65 		}
66 
67 		ggtt->mtrr = arch_phys_wc_add(ggtt->gmadr.start,
68 					      ggtt->mappable_end);
69 	}
70 
71 	intel_ggtt_init_fences(ggtt);
72 
73 	return 0;
74 }
75 
76 /**
77  * i915_ggtt_init_hw - Initialize GGTT hardware
78  * @i915: i915 device
79  */
80 int i915_ggtt_init_hw(struct drm_i915_private *i915)
81 {
82 	int ret;
83 
84 	/*
85 	 * Note that we use page colouring to enforce a guard page at the
86 	 * end of the address space. This is required as the CS may prefetch
87 	 * beyond the end of the batch buffer, across the page boundary,
88 	 * and beyond the end of the GTT if we do not provide a guard.
89 	 */
90 	ret = ggtt_init_hw(&i915->ggtt);
91 	if (ret)
92 		return ret;
93 
94 	return 0;
95 }
96 
97 /*
98  * Certain Gen5 chipsets require idling the GPU before
99  * unmapping anything from the GTT when VT-d is enabled.
100  */
101 static bool needs_idle_maps(struct drm_i915_private *i915)
102 {
103 	/*
104 	 * Query intel_iommu to see if we need the workaround. Presumably that
105 	 * was loaded first.
106 	 */
107 	if (!intel_vtd_active(i915))
108 		return false;
109 
110 	if (GRAPHICS_VER(i915) == 5 && IS_MOBILE(i915))
111 		return true;
112 
113 	if (GRAPHICS_VER(i915) == 12)
114 		return true; /* XXX DMAR fault reason 7 */
115 
116 	return false;
117 }
118 
119 /**
120  * i915_ggtt_suspend_vm - Suspend the memory mappings for a GGTT or DPT VM
121  * @vm: The VM to suspend the mappings for
122  *
123  * Suspend the memory mappings for all objects mapped to HW via the GGTT or a
124  * DPT page table.
125  */
126 void i915_ggtt_suspend_vm(struct i915_address_space *vm)
127 {
128 	struct i915_vma *vma, *vn;
129 	int open;
130 
131 	drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
132 
133 	mutex_lock(&vm->mutex);
134 
135 	/* Skip rewriting PTE on VMA unbind. */
136 	open = atomic_xchg(&vm->open, 0);
137 
138 	list_for_each_entry_safe(vma, vn, &vm->bound_list, vm_link) {
139 		GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
140 		i915_vma_wait_for_bind(vma);
141 
142 		if (i915_vma_is_pinned(vma))
143 			continue;
144 
145 		if (!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND)) {
146 			__i915_vma_evict(vma);
147 			drm_mm_remove_node(&vma->node);
148 		}
149 	}
150 
151 	vm->clear_range(vm, 0, vm->total);
152 
153 	atomic_set(&vm->open, open);
154 
155 	mutex_unlock(&vm->mutex);
156 }
157 
158 void i915_ggtt_suspend(struct i915_ggtt *ggtt)
159 {
160 	i915_ggtt_suspend_vm(&ggtt->vm);
161 	ggtt->invalidate(ggtt);
162 
163 	intel_gt_check_and_clear_faults(ggtt->vm.gt);
164 }
165 
166 void gen6_ggtt_invalidate(struct i915_ggtt *ggtt)
167 {
168 	struct intel_uncore *uncore = ggtt->vm.gt->uncore;
169 
170 	spin_lock_irq(&uncore->lock);
171 	intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
172 	intel_uncore_read_fw(uncore, GFX_FLSH_CNTL_GEN6);
173 	spin_unlock_irq(&uncore->lock);
174 }
175 
176 static void gen8_ggtt_invalidate(struct i915_ggtt *ggtt)
177 {
178 	struct intel_uncore *uncore = ggtt->vm.gt->uncore;
179 
180 	/*
181 	 * Note that as an uncached mmio write, this will flush the
182 	 * WCB of the writes into the GGTT before it triggers the invalidate.
183 	 */
184 	intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
185 }
186 
187 static void guc_ggtt_invalidate(struct i915_ggtt *ggtt)
188 {
189 	struct intel_uncore *uncore = ggtt->vm.gt->uncore;
190 	struct drm_i915_private *i915 = ggtt->vm.i915;
191 
192 	gen8_ggtt_invalidate(ggtt);
193 
194 	if (GRAPHICS_VER(i915) >= 12)
195 		intel_uncore_write_fw(uncore, GEN12_GUC_TLB_INV_CR,
196 				      GEN12_GUC_TLB_INV_CR_INVALIDATE);
197 	else
198 		intel_uncore_write_fw(uncore, GEN8_GTCR, GEN8_GTCR_INVALIDATE);
199 }
200 
201 static void gmch_ggtt_invalidate(struct i915_ggtt *ggtt)
202 {
203 	intel_gtt_chipset_flush();
204 }
205 
206 u64 gen8_ggtt_pte_encode(dma_addr_t addr,
207 			 enum i915_cache_level level,
208 			 u32 flags)
209 {
210 	gen8_pte_t pte = addr | GEN8_PAGE_PRESENT;
211 
212 	if (flags & PTE_LM)
213 		pte |= GEN12_GGTT_PTE_LM;
214 
215 	return pte;
216 }
217 
218 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
219 {
220 	writeq(pte, addr);
221 }
222 
223 static void gen8_ggtt_insert_page(struct i915_address_space *vm,
224 				  dma_addr_t addr,
225 				  u64 offset,
226 				  enum i915_cache_level level,
227 				  u32 flags)
228 {
229 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
230 	gen8_pte_t __iomem *pte =
231 		(gen8_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
232 
233 	gen8_set_pte(pte, gen8_ggtt_pte_encode(addr, level, flags));
234 
235 	ggtt->invalidate(ggtt);
236 }
237 
238 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
239 				     struct i915_vma *vma,
240 				     enum i915_cache_level level,
241 				     u32 flags)
242 {
243 	const gen8_pte_t pte_encode = gen8_ggtt_pte_encode(0, level, flags);
244 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
245 	gen8_pte_t __iomem *gte;
246 	gen8_pte_t __iomem *end;
247 	struct sgt_iter iter;
248 	dma_addr_t addr;
249 
250 	/*
251 	 * Note that we ignore PTE_READ_ONLY here. The caller must be careful
252 	 * not to allow the user to override access to a read only page.
253 	 */
254 
255 	gte = (gen8_pte_t __iomem *)ggtt->gsm;
256 	gte += vma->node.start / I915_GTT_PAGE_SIZE;
257 	end = gte + vma->node.size / I915_GTT_PAGE_SIZE;
258 
259 	for_each_sgt_daddr(addr, iter, vma->pages)
260 		gen8_set_pte(gte++, pte_encode | addr);
261 	GEM_BUG_ON(gte > end);
262 
263 	/* Fill the allocated but "unused" space beyond the end of the buffer */
264 	while (gte < end)
265 		gen8_set_pte(gte++, vm->scratch[0]->encode);
266 
267 	/*
268 	 * We want to flush the TLBs only after we're certain all the PTE
269 	 * updates have finished.
270 	 */
271 	ggtt->invalidate(ggtt);
272 }
273 
274 static void gen6_ggtt_insert_page(struct i915_address_space *vm,
275 				  dma_addr_t addr,
276 				  u64 offset,
277 				  enum i915_cache_level level,
278 				  u32 flags)
279 {
280 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
281 	gen6_pte_t __iomem *pte =
282 		(gen6_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
283 
284 	iowrite32(vm->pte_encode(addr, level, flags), pte);
285 
286 	ggtt->invalidate(ggtt);
287 }
288 
289 /*
290  * Binds an object into the global gtt with the specified cache level.
291  * The object will be accessible to the GPU via commands whose operands
292  * reference offsets within the global GTT as well as accessible by the GPU
293  * through the GMADR mapped BAR (i915->mm.gtt->gtt).
294  */
295 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
296 				     struct i915_vma *vma,
297 				     enum i915_cache_level level,
298 				     u32 flags)
299 {
300 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
301 	gen6_pte_t __iomem *gte;
302 	gen6_pte_t __iomem *end;
303 	struct sgt_iter iter;
304 	dma_addr_t addr;
305 
306 	gte = (gen6_pte_t __iomem *)ggtt->gsm;
307 	gte += vma->node.start / I915_GTT_PAGE_SIZE;
308 	end = gte + vma->node.size / I915_GTT_PAGE_SIZE;
309 
310 	for_each_sgt_daddr(addr, iter, vma->pages)
311 		iowrite32(vm->pte_encode(addr, level, flags), gte++);
312 	GEM_BUG_ON(gte > end);
313 
314 	/* Fill the allocated but "unused" space beyond the end of the buffer */
315 	while (gte < end)
316 		iowrite32(vm->scratch[0]->encode, gte++);
317 
318 	/*
319 	 * We want to flush the TLBs only after we're certain all the PTE
320 	 * updates have finished.
321 	 */
322 	ggtt->invalidate(ggtt);
323 }
324 
325 static void nop_clear_range(struct i915_address_space *vm,
326 			    u64 start, u64 length)
327 {
328 }
329 
330 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
331 				  u64 start, u64 length)
332 {
333 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
334 	unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
335 	unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
336 	const gen8_pte_t scratch_pte = vm->scratch[0]->encode;
337 	gen8_pte_t __iomem *gtt_base =
338 		(gen8_pte_t __iomem *)ggtt->gsm + first_entry;
339 	const int max_entries = ggtt_total_entries(ggtt) - first_entry;
340 	int i;
341 
342 	if (WARN(num_entries > max_entries,
343 		 "First entry = %d; Num entries = %d (max=%d)\n",
344 		 first_entry, num_entries, max_entries))
345 		num_entries = max_entries;
346 
347 	for (i = 0; i < num_entries; i++)
348 		gen8_set_pte(&gtt_base[i], scratch_pte);
349 }
350 
351 static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
352 {
353 	/*
354 	 * Make sure the internal GAM fifo has been cleared of all GTT
355 	 * writes before exiting stop_machine(). This guarantees that
356 	 * any aperture accesses waiting to start in another process
357 	 * cannot back up behind the GTT writes causing a hang.
358 	 * The register can be any arbitrary GAM register.
359 	 */
360 	intel_uncore_posting_read_fw(vm->gt->uncore, GFX_FLSH_CNTL_GEN6);
361 }
362 
363 struct insert_page {
364 	struct i915_address_space *vm;
365 	dma_addr_t addr;
366 	u64 offset;
367 	enum i915_cache_level level;
368 };
369 
370 static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
371 {
372 	struct insert_page *arg = _arg;
373 
374 	gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset, arg->level, 0);
375 	bxt_vtd_ggtt_wa(arg->vm);
376 
377 	return 0;
378 }
379 
380 static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
381 					  dma_addr_t addr,
382 					  u64 offset,
383 					  enum i915_cache_level level,
384 					  u32 unused)
385 {
386 	struct insert_page arg = { vm, addr, offset, level };
387 
388 	stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
389 }
390 
391 struct insert_entries {
392 	struct i915_address_space *vm;
393 	struct i915_vma *vma;
394 	enum i915_cache_level level;
395 	u32 flags;
396 };
397 
398 static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
399 {
400 	struct insert_entries *arg = _arg;
401 
402 	gen8_ggtt_insert_entries(arg->vm, arg->vma, arg->level, arg->flags);
403 	bxt_vtd_ggtt_wa(arg->vm);
404 
405 	return 0;
406 }
407 
408 static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
409 					     struct i915_vma *vma,
410 					     enum i915_cache_level level,
411 					     u32 flags)
412 {
413 	struct insert_entries arg = { vm, vma, level, flags };
414 
415 	stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
416 }
417 
418 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
419 				  u64 start, u64 length)
420 {
421 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
422 	unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
423 	unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
424 	gen6_pte_t scratch_pte, __iomem *gtt_base =
425 		(gen6_pte_t __iomem *)ggtt->gsm + first_entry;
426 	const int max_entries = ggtt_total_entries(ggtt) - first_entry;
427 	int i;
428 
429 	if (WARN(num_entries > max_entries,
430 		 "First entry = %d; Num entries = %d (max=%d)\n",
431 		 first_entry, num_entries, max_entries))
432 		num_entries = max_entries;
433 
434 	scratch_pte = vm->scratch[0]->encode;
435 	for (i = 0; i < num_entries; i++)
436 		iowrite32(scratch_pte, &gtt_base[i]);
437 }
438 
439 static void i915_ggtt_insert_page(struct i915_address_space *vm,
440 				  dma_addr_t addr,
441 				  u64 offset,
442 				  enum i915_cache_level cache_level,
443 				  u32 unused)
444 {
445 	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
446 		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
447 
448 	intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
449 }
450 
451 static void i915_ggtt_insert_entries(struct i915_address_space *vm,
452 				     struct i915_vma *vma,
453 				     enum i915_cache_level cache_level,
454 				     u32 unused)
455 {
456 	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
457 		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
458 
459 	intel_gtt_insert_sg_entries(vma->pages, vma->node.start >> PAGE_SHIFT,
460 				    flags);
461 }
462 
463 static void i915_ggtt_clear_range(struct i915_address_space *vm,
464 				  u64 start, u64 length)
465 {
466 	intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
467 }
468 
469 static void ggtt_bind_vma(struct i915_address_space *vm,
470 			  struct i915_vm_pt_stash *stash,
471 			  struct i915_vma *vma,
472 			  enum i915_cache_level cache_level,
473 			  u32 flags)
474 {
475 	struct drm_i915_gem_object *obj = vma->obj;
476 	u32 pte_flags;
477 
478 	if (i915_vma_is_bound(vma, ~flags & I915_VMA_BIND_MASK))
479 		return;
480 
481 	/* Applicable to VLV (gen8+ do not support RO in the GGTT) */
482 	pte_flags = 0;
483 	if (i915_gem_object_is_readonly(obj))
484 		pte_flags |= PTE_READ_ONLY;
485 	if (i915_gem_object_is_lmem(obj))
486 		pte_flags |= PTE_LM;
487 
488 	vm->insert_entries(vm, vma, cache_level, pte_flags);
489 	vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
490 }
491 
492 static void ggtt_unbind_vma(struct i915_address_space *vm, struct i915_vma *vma)
493 {
494 	vm->clear_range(vm, vma->node.start, vma->size);
495 }
496 
497 static int ggtt_reserve_guc_top(struct i915_ggtt *ggtt)
498 {
499 	u64 size;
500 	int ret;
501 
502 	if (!intel_uc_uses_guc(&ggtt->vm.gt->uc))
503 		return 0;
504 
505 	GEM_BUG_ON(ggtt->vm.total <= GUC_GGTT_TOP);
506 	size = ggtt->vm.total - GUC_GGTT_TOP;
507 
508 	ret = i915_gem_gtt_reserve(&ggtt->vm, &ggtt->uc_fw, size,
509 				   GUC_GGTT_TOP, I915_COLOR_UNEVICTABLE,
510 				   PIN_NOEVICT);
511 	if (ret)
512 		drm_dbg(&ggtt->vm.i915->drm,
513 			"Failed to reserve top of GGTT for GuC\n");
514 
515 	return ret;
516 }
517 
518 static void ggtt_release_guc_top(struct i915_ggtt *ggtt)
519 {
520 	if (drm_mm_node_allocated(&ggtt->uc_fw))
521 		drm_mm_remove_node(&ggtt->uc_fw);
522 }
523 
524 static void cleanup_init_ggtt(struct i915_ggtt *ggtt)
525 {
526 	ggtt_release_guc_top(ggtt);
527 	if (drm_mm_node_allocated(&ggtt->error_capture))
528 		drm_mm_remove_node(&ggtt->error_capture);
529 	mutex_destroy(&ggtt->error_mutex);
530 }
531 
532 static int init_ggtt(struct i915_ggtt *ggtt)
533 {
534 	/*
535 	 * Let GEM Manage all of the aperture.
536 	 *
537 	 * However, leave one page at the end still bound to the scratch page.
538 	 * There are a number of places where the hardware apparently prefetches
539 	 * past the end of the object, and we've seen multiple hangs with the
540 	 * GPU head pointer stuck in a batchbuffer bound at the last page of the
541 	 * aperture.  One page should be enough to keep any prefetching inside
542 	 * of the aperture.
543 	 */
544 	unsigned long hole_start, hole_end;
545 	struct drm_mm_node *entry;
546 	int ret;
547 
548 	/*
549 	 * GuC requires all resources that we're sharing with it to be placed in
550 	 * non-WOPCM memory. If GuC is not present or not in use we still need a
551 	 * small bias as ring wraparound at offset 0 sometimes hangs. No idea
552 	 * why.
553 	 */
554 	ggtt->pin_bias = max_t(u32, I915_GTT_PAGE_SIZE,
555 			       intel_wopcm_guc_size(&ggtt->vm.i915->wopcm));
556 
557 	ret = intel_vgt_balloon(ggtt);
558 	if (ret)
559 		return ret;
560 
561 	mutex_init(&ggtt->error_mutex);
562 	if (ggtt->mappable_end) {
563 		/*
564 		 * Reserve a mappable slot for our lockless error capture.
565 		 *
566 		 * We strongly prefer taking address 0x0 in order to protect
567 		 * other critical buffers against accidental overwrites,
568 		 * as writing to address 0 is a very common mistake.
569 		 *
570 		 * Since 0 may already be in use by the system (e.g. the BIOS
571 		 * framebuffer), we let the reservation fail quietly and hope
572 		 * 0 remains reserved always.
573 		 *
574 		 * If we fail to reserve 0, and then fail to find any space
575 		 * for an error-capture, remain silent. We can afford not
576 		 * to reserve an error_capture node as we have fallback
577 		 * paths, and we trust that 0 will remain reserved. However,
578 		 * the only likely reason for failure to insert is a driver
579 		 * bug, which we expect to cause other failures...
580 		 */
581 		ggtt->error_capture.size = I915_GTT_PAGE_SIZE;
582 		ggtt->error_capture.color = I915_COLOR_UNEVICTABLE;
583 		if (drm_mm_reserve_node(&ggtt->vm.mm, &ggtt->error_capture))
584 			drm_mm_insert_node_in_range(&ggtt->vm.mm,
585 						    &ggtt->error_capture,
586 						    ggtt->error_capture.size, 0,
587 						    ggtt->error_capture.color,
588 						    0, ggtt->mappable_end,
589 						    DRM_MM_INSERT_LOW);
590 	}
591 	if (drm_mm_node_allocated(&ggtt->error_capture))
592 		drm_dbg(&ggtt->vm.i915->drm,
593 			"Reserved GGTT:[%llx, %llx] for use by error capture\n",
594 			ggtt->error_capture.start,
595 			ggtt->error_capture.start + ggtt->error_capture.size);
596 
597 	/*
598 	 * The upper portion of the GuC address space has a sizeable hole
599 	 * (several MB) that is inaccessible by GuC. Reserve this range within
600 	 * GGTT as it can comfortably hold GuC/HuC firmware images.
601 	 */
602 	ret = ggtt_reserve_guc_top(ggtt);
603 	if (ret)
604 		goto err;
605 
606 	/* Clear any non-preallocated blocks */
607 	drm_mm_for_each_hole(entry, &ggtt->vm.mm, hole_start, hole_end) {
608 		drm_dbg(&ggtt->vm.i915->drm,
609 			"clearing unused GTT space: [%lx, %lx]\n",
610 			hole_start, hole_end);
611 		ggtt->vm.clear_range(&ggtt->vm, hole_start,
612 				     hole_end - hole_start);
613 	}
614 
615 	/* And finally clear the reserved guard page */
616 	ggtt->vm.clear_range(&ggtt->vm, ggtt->vm.total - PAGE_SIZE, PAGE_SIZE);
617 
618 	return 0;
619 
620 err:
621 	cleanup_init_ggtt(ggtt);
622 	return ret;
623 }
624 
625 static void aliasing_gtt_bind_vma(struct i915_address_space *vm,
626 				  struct i915_vm_pt_stash *stash,
627 				  struct i915_vma *vma,
628 				  enum i915_cache_level cache_level,
629 				  u32 flags)
630 {
631 	u32 pte_flags;
632 
633 	/* Currently applicable only to VLV */
634 	pte_flags = 0;
635 	if (i915_gem_object_is_readonly(vma->obj))
636 		pte_flags |= PTE_READ_ONLY;
637 
638 	if (flags & I915_VMA_LOCAL_BIND)
639 		ppgtt_bind_vma(&i915_vm_to_ggtt(vm)->alias->vm,
640 			       stash, vma, cache_level, flags);
641 
642 	if (flags & I915_VMA_GLOBAL_BIND)
643 		vm->insert_entries(vm, vma, cache_level, pte_flags);
644 }
645 
646 static void aliasing_gtt_unbind_vma(struct i915_address_space *vm,
647 				    struct i915_vma *vma)
648 {
649 	if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
650 		vm->clear_range(vm, vma->node.start, vma->size);
651 
652 	if (i915_vma_is_bound(vma, I915_VMA_LOCAL_BIND))
653 		ppgtt_unbind_vma(&i915_vm_to_ggtt(vm)->alias->vm, vma);
654 }
655 
656 static int init_aliasing_ppgtt(struct i915_ggtt *ggtt)
657 {
658 	struct i915_vm_pt_stash stash = {};
659 	struct i915_ppgtt *ppgtt;
660 	int err;
661 
662 	ppgtt = i915_ppgtt_create(ggtt->vm.gt, 0);
663 	if (IS_ERR(ppgtt))
664 		return PTR_ERR(ppgtt);
665 
666 	if (GEM_WARN_ON(ppgtt->vm.total < ggtt->vm.total)) {
667 		err = -ENODEV;
668 		goto err_ppgtt;
669 	}
670 
671 	err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, ggtt->vm.total);
672 	if (err)
673 		goto err_ppgtt;
674 
675 	i915_gem_object_lock(ppgtt->vm.scratch[0], NULL);
676 	err = i915_vm_map_pt_stash(&ppgtt->vm, &stash);
677 	i915_gem_object_unlock(ppgtt->vm.scratch[0]);
678 	if (err)
679 		goto err_stash;
680 
681 	/*
682 	 * Note we only pre-allocate as far as the end of the global
683 	 * GTT. On 48b / 4-level page-tables, the difference is very,
684 	 * very significant! We have to preallocate as GVT/vgpu does
685 	 * not like the page directory disappearing.
686 	 */
687 	ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash, 0, ggtt->vm.total);
688 
689 	ggtt->alias = ppgtt;
690 	ggtt->vm.bind_async_flags |= ppgtt->vm.bind_async_flags;
691 
692 	GEM_BUG_ON(ggtt->vm.vma_ops.bind_vma != ggtt_bind_vma);
693 	ggtt->vm.vma_ops.bind_vma = aliasing_gtt_bind_vma;
694 
695 	GEM_BUG_ON(ggtt->vm.vma_ops.unbind_vma != ggtt_unbind_vma);
696 	ggtt->vm.vma_ops.unbind_vma = aliasing_gtt_unbind_vma;
697 
698 	i915_vm_free_pt_stash(&ppgtt->vm, &stash);
699 	return 0;
700 
701 err_stash:
702 	i915_vm_free_pt_stash(&ppgtt->vm, &stash);
703 err_ppgtt:
704 	i915_vm_put(&ppgtt->vm);
705 	return err;
706 }
707 
708 static void fini_aliasing_ppgtt(struct i915_ggtt *ggtt)
709 {
710 	struct i915_ppgtt *ppgtt;
711 
712 	ppgtt = fetch_and_zero(&ggtt->alias);
713 	if (!ppgtt)
714 		return;
715 
716 	i915_vm_put(&ppgtt->vm);
717 
718 	ggtt->vm.vma_ops.bind_vma   = ggtt_bind_vma;
719 	ggtt->vm.vma_ops.unbind_vma = ggtt_unbind_vma;
720 }
721 
722 int i915_init_ggtt(struct drm_i915_private *i915)
723 {
724 	int ret;
725 
726 	ret = init_ggtt(&i915->ggtt);
727 	if (ret)
728 		return ret;
729 
730 	if (INTEL_PPGTT(i915) == INTEL_PPGTT_ALIASING) {
731 		ret = init_aliasing_ppgtt(&i915->ggtt);
732 		if (ret)
733 			cleanup_init_ggtt(&i915->ggtt);
734 	}
735 
736 	return 0;
737 }
738 
739 static void ggtt_cleanup_hw(struct i915_ggtt *ggtt)
740 {
741 	struct i915_vma *vma, *vn;
742 
743 	atomic_set(&ggtt->vm.open, 0);
744 
745 	flush_workqueue(ggtt->vm.i915->wq);
746 
747 	mutex_lock(&ggtt->vm.mutex);
748 
749 	list_for_each_entry_safe(vma, vn, &ggtt->vm.bound_list, vm_link)
750 		WARN_ON(__i915_vma_unbind(vma));
751 
752 	if (drm_mm_node_allocated(&ggtt->error_capture))
753 		drm_mm_remove_node(&ggtt->error_capture);
754 	mutex_destroy(&ggtt->error_mutex);
755 
756 	ggtt_release_guc_top(ggtt);
757 	intel_vgt_deballoon(ggtt);
758 
759 	ggtt->vm.cleanup(&ggtt->vm);
760 
761 	mutex_unlock(&ggtt->vm.mutex);
762 	i915_address_space_fini(&ggtt->vm);
763 
764 	arch_phys_wc_del(ggtt->mtrr);
765 
766 	if (ggtt->iomap.size)
767 		io_mapping_fini(&ggtt->iomap);
768 }
769 
770 /**
771  * i915_ggtt_driver_release - Clean up GGTT hardware initialization
772  * @i915: i915 device
773  */
774 void i915_ggtt_driver_release(struct drm_i915_private *i915)
775 {
776 	struct i915_ggtt *ggtt = &i915->ggtt;
777 
778 	fini_aliasing_ppgtt(ggtt);
779 
780 	intel_ggtt_fini_fences(ggtt);
781 	ggtt_cleanup_hw(ggtt);
782 }
783 
784 /**
785  * i915_ggtt_driver_late_release - Cleanup of GGTT that needs to be done after
786  * all free objects have been drained.
787  * @i915: i915 device
788  */
789 void i915_ggtt_driver_late_release(struct drm_i915_private *i915)
790 {
791 	struct i915_ggtt *ggtt = &i915->ggtt;
792 
793 	GEM_WARN_ON(kref_read(&ggtt->vm.resv_ref) != 1);
794 	dma_resv_fini(&ggtt->vm._resv);
795 }
796 
797 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
798 {
799 	snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
800 	snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
801 	return snb_gmch_ctl << 20;
802 }
803 
804 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
805 {
806 	bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
807 	bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
808 	if (bdw_gmch_ctl)
809 		bdw_gmch_ctl = 1 << bdw_gmch_ctl;
810 
811 #ifdef CONFIG_X86_32
812 	/* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * I915_GTT_PAGE_SIZE */
813 	if (bdw_gmch_ctl > 4)
814 		bdw_gmch_ctl = 4;
815 #endif
816 
817 	return bdw_gmch_ctl << 20;
818 }
819 
820 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
821 {
822 	gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
823 	gmch_ctrl &= SNB_GMCH_GGMS_MASK;
824 
825 	if (gmch_ctrl)
826 		return 1 << (20 + gmch_ctrl);
827 
828 	return 0;
829 }
830 
831 static unsigned int gen6_gttmmadr_size(struct drm_i915_private *i915)
832 {
833 	/*
834 	 * GEN6: GTTMMADR size is 4MB and GTTADR starts at 2MB offset
835 	 * GEN8: GTTMMADR size is 16MB and GTTADR starts at 8MB offset
836 	 */
837 	GEM_BUG_ON(GRAPHICS_VER(i915) < 6);
838 	return (GRAPHICS_VER(i915) < 8) ? SZ_4M : SZ_16M;
839 }
840 
841 static unsigned int gen6_gttadr_offset(struct drm_i915_private *i915)
842 {
843 	return gen6_gttmmadr_size(i915) / 2;
844 }
845 
846 static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
847 {
848 	struct drm_i915_private *i915 = ggtt->vm.i915;
849 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
850 	phys_addr_t phys_addr;
851 	u32 pte_flags;
852 	int ret;
853 
854 	GEM_WARN_ON(pci_resource_len(pdev, 0) != gen6_gttmmadr_size(i915));
855 	phys_addr = pci_resource_start(pdev, 0) + gen6_gttadr_offset(i915);
856 
857 	/*
858 	 * On BXT+/ICL+ writes larger than 64 bit to the GTT pagetable range
859 	 * will be dropped. For WC mappings in general we have 64 byte burst
860 	 * writes when the WC buffer is flushed, so we can't use it, but have to
861 	 * resort to an uncached mapping. The WC issue is easily caught by the
862 	 * readback check when writing GTT PTE entries.
863 	 */
864 	if (IS_GEN9_LP(i915) || GRAPHICS_VER(i915) >= 11)
865 		ggtt->gsm = ioremap(phys_addr, size);
866 	else
867 		ggtt->gsm = ioremap_wc(phys_addr, size);
868 	if (!ggtt->gsm) {
869 		drm_err(&i915->drm, "Failed to map the ggtt page table\n");
870 		return -ENOMEM;
871 	}
872 
873 	kref_init(&ggtt->vm.resv_ref);
874 	ret = setup_scratch_page(&ggtt->vm);
875 	if (ret) {
876 		drm_err(&i915->drm, "Scratch setup failed\n");
877 		/* iounmap will also get called at remove, but meh */
878 		iounmap(ggtt->gsm);
879 		return ret;
880 	}
881 
882 	pte_flags = 0;
883 	if (i915_gem_object_is_lmem(ggtt->vm.scratch[0]))
884 		pte_flags |= PTE_LM;
885 
886 	ggtt->vm.scratch[0]->encode =
887 		ggtt->vm.pte_encode(px_dma(ggtt->vm.scratch[0]),
888 				    I915_CACHE_NONE, pte_flags);
889 
890 	return 0;
891 }
892 
893 static void gen6_gmch_remove(struct i915_address_space *vm)
894 {
895 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
896 
897 	iounmap(ggtt->gsm);
898 	free_scratch(vm);
899 }
900 
901 static struct resource pci_resource(struct pci_dev *pdev, int bar)
902 {
903 	return (struct resource)DEFINE_RES_MEM(pci_resource_start(pdev, bar),
904 					       pci_resource_len(pdev, bar));
905 }
906 
907 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
908 {
909 	struct drm_i915_private *i915 = ggtt->vm.i915;
910 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
911 	unsigned int size;
912 	u16 snb_gmch_ctl;
913 
914 	/* TODO: We're not aware of mappable constraints on gen8 yet */
915 	if (!HAS_LMEM(i915)) {
916 		ggtt->gmadr = pci_resource(pdev, 2);
917 		ggtt->mappable_end = resource_size(&ggtt->gmadr);
918 	}
919 
920 	pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
921 	if (IS_CHERRYVIEW(i915))
922 		size = chv_get_total_gtt_size(snb_gmch_ctl);
923 	else
924 		size = gen8_get_total_gtt_size(snb_gmch_ctl);
925 
926 	ggtt->vm.alloc_pt_dma = alloc_pt_dma;
927 	ggtt->vm.alloc_scratch_dma = alloc_pt_dma;
928 	ggtt->vm.lmem_pt_obj_flags = I915_BO_ALLOC_PM_EARLY;
929 
930 	ggtt->vm.total = (size / sizeof(gen8_pte_t)) * I915_GTT_PAGE_SIZE;
931 	ggtt->vm.cleanup = gen6_gmch_remove;
932 	ggtt->vm.insert_page = gen8_ggtt_insert_page;
933 	ggtt->vm.clear_range = nop_clear_range;
934 	if (intel_scanout_needs_vtd_wa(i915))
935 		ggtt->vm.clear_range = gen8_ggtt_clear_range;
936 
937 	ggtt->vm.insert_entries = gen8_ggtt_insert_entries;
938 
939 	/*
940 	 * Serialize GTT updates with aperture access on BXT if VT-d is on,
941 	 * and always on CHV.
942 	 */
943 	if (intel_vm_no_concurrent_access_wa(i915)) {
944 		ggtt->vm.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
945 		ggtt->vm.insert_page    = bxt_vtd_ggtt_insert_page__BKL;
946 		ggtt->vm.bind_async_flags =
947 			I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
948 	}
949 
950 	ggtt->invalidate = gen8_ggtt_invalidate;
951 
952 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
953 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
954 
955 	ggtt->vm.pte_encode = gen8_ggtt_pte_encode;
956 
957 	setup_private_pat(ggtt->vm.gt->uncore);
958 
959 	return ggtt_probe_common(ggtt, size);
960 }
961 
962 static u64 snb_pte_encode(dma_addr_t addr,
963 			  enum i915_cache_level level,
964 			  u32 flags)
965 {
966 	gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
967 
968 	switch (level) {
969 	case I915_CACHE_L3_LLC:
970 	case I915_CACHE_LLC:
971 		pte |= GEN6_PTE_CACHE_LLC;
972 		break;
973 	case I915_CACHE_NONE:
974 		pte |= GEN6_PTE_UNCACHED;
975 		break;
976 	default:
977 		MISSING_CASE(level);
978 	}
979 
980 	return pte;
981 }
982 
983 static u64 ivb_pte_encode(dma_addr_t addr,
984 			  enum i915_cache_level level,
985 			  u32 flags)
986 {
987 	gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
988 
989 	switch (level) {
990 	case I915_CACHE_L3_LLC:
991 		pte |= GEN7_PTE_CACHE_L3_LLC;
992 		break;
993 	case I915_CACHE_LLC:
994 		pte |= GEN6_PTE_CACHE_LLC;
995 		break;
996 	case I915_CACHE_NONE:
997 		pte |= GEN6_PTE_UNCACHED;
998 		break;
999 	default:
1000 		MISSING_CASE(level);
1001 	}
1002 
1003 	return pte;
1004 }
1005 
1006 static u64 byt_pte_encode(dma_addr_t addr,
1007 			  enum i915_cache_level level,
1008 			  u32 flags)
1009 {
1010 	gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1011 
1012 	if (!(flags & PTE_READ_ONLY))
1013 		pte |= BYT_PTE_WRITEABLE;
1014 
1015 	if (level != I915_CACHE_NONE)
1016 		pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
1017 
1018 	return pte;
1019 }
1020 
1021 static u64 hsw_pte_encode(dma_addr_t addr,
1022 			  enum i915_cache_level level,
1023 			  u32 flags)
1024 {
1025 	gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1026 
1027 	if (level != I915_CACHE_NONE)
1028 		pte |= HSW_WB_LLC_AGE3;
1029 
1030 	return pte;
1031 }
1032 
1033 static u64 iris_pte_encode(dma_addr_t addr,
1034 			   enum i915_cache_level level,
1035 			   u32 flags)
1036 {
1037 	gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1038 
1039 	switch (level) {
1040 	case I915_CACHE_NONE:
1041 		break;
1042 	case I915_CACHE_WT:
1043 		pte |= HSW_WT_ELLC_LLC_AGE3;
1044 		break;
1045 	default:
1046 		pte |= HSW_WB_ELLC_LLC_AGE3;
1047 		break;
1048 	}
1049 
1050 	return pte;
1051 }
1052 
1053 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
1054 {
1055 	struct drm_i915_private *i915 = ggtt->vm.i915;
1056 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
1057 	unsigned int size;
1058 	u16 snb_gmch_ctl;
1059 
1060 	ggtt->gmadr = pci_resource(pdev, 2);
1061 	ggtt->mappable_end = resource_size(&ggtt->gmadr);
1062 
1063 	/*
1064 	 * 64/512MB is the current min/max we actually know of, but this is
1065 	 * just a coarse sanity check.
1066 	 */
1067 	if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
1068 		drm_err(&i915->drm, "Unknown GMADR size (%pa)\n",
1069 			&ggtt->mappable_end);
1070 		return -ENXIO;
1071 	}
1072 
1073 	pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1074 
1075 	size = gen6_get_total_gtt_size(snb_gmch_ctl);
1076 	ggtt->vm.total = (size / sizeof(gen6_pte_t)) * I915_GTT_PAGE_SIZE;
1077 
1078 	ggtt->vm.alloc_pt_dma = alloc_pt_dma;
1079 	ggtt->vm.alloc_scratch_dma = alloc_pt_dma;
1080 
1081 	ggtt->vm.clear_range = nop_clear_range;
1082 	if (!HAS_FULL_PPGTT(i915) || intel_scanout_needs_vtd_wa(i915))
1083 		ggtt->vm.clear_range = gen6_ggtt_clear_range;
1084 	ggtt->vm.insert_page = gen6_ggtt_insert_page;
1085 	ggtt->vm.insert_entries = gen6_ggtt_insert_entries;
1086 	ggtt->vm.cleanup = gen6_gmch_remove;
1087 
1088 	ggtt->invalidate = gen6_ggtt_invalidate;
1089 
1090 	if (HAS_EDRAM(i915))
1091 		ggtt->vm.pte_encode = iris_pte_encode;
1092 	else if (IS_HASWELL(i915))
1093 		ggtt->vm.pte_encode = hsw_pte_encode;
1094 	else if (IS_VALLEYVIEW(i915))
1095 		ggtt->vm.pte_encode = byt_pte_encode;
1096 	else if (GRAPHICS_VER(i915) >= 7)
1097 		ggtt->vm.pte_encode = ivb_pte_encode;
1098 	else
1099 		ggtt->vm.pte_encode = snb_pte_encode;
1100 
1101 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
1102 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
1103 
1104 	return ggtt_probe_common(ggtt, size);
1105 }
1106 
1107 static void i915_gmch_remove(struct i915_address_space *vm)
1108 {
1109 	intel_gmch_remove();
1110 }
1111 
1112 static int i915_gmch_probe(struct i915_ggtt *ggtt)
1113 {
1114 	struct drm_i915_private *i915 = ggtt->vm.i915;
1115 	phys_addr_t gmadr_base;
1116 	int ret;
1117 
1118 	ret = intel_gmch_probe(i915->bridge_dev, to_pci_dev(i915->drm.dev), NULL);
1119 	if (!ret) {
1120 		drm_err(&i915->drm, "failed to set up gmch\n");
1121 		return -EIO;
1122 	}
1123 
1124 	intel_gtt_get(&ggtt->vm.total, &gmadr_base, &ggtt->mappable_end);
1125 
1126 	ggtt->gmadr =
1127 		(struct resource)DEFINE_RES_MEM(gmadr_base, ggtt->mappable_end);
1128 
1129 	ggtt->vm.alloc_pt_dma = alloc_pt_dma;
1130 	ggtt->vm.alloc_scratch_dma = alloc_pt_dma;
1131 
1132 	if (needs_idle_maps(i915)) {
1133 		drm_notice(&i915->drm,
1134 			   "Flushing DMA requests before IOMMU unmaps; performance may be degraded\n");
1135 		ggtt->do_idle_maps = true;
1136 	}
1137 
1138 	ggtt->vm.insert_page = i915_ggtt_insert_page;
1139 	ggtt->vm.insert_entries = i915_ggtt_insert_entries;
1140 	ggtt->vm.clear_range = i915_ggtt_clear_range;
1141 	ggtt->vm.cleanup = i915_gmch_remove;
1142 
1143 	ggtt->invalidate = gmch_ggtt_invalidate;
1144 
1145 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
1146 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
1147 
1148 	if (unlikely(ggtt->do_idle_maps))
1149 		drm_notice(&i915->drm,
1150 			   "Applying Ironlake quirks for intel_iommu\n");
1151 
1152 	return 0;
1153 }
1154 
1155 static int ggtt_probe_hw(struct i915_ggtt *ggtt, struct intel_gt *gt)
1156 {
1157 	struct drm_i915_private *i915 = gt->i915;
1158 	int ret;
1159 
1160 	ggtt->vm.gt = gt;
1161 	ggtt->vm.i915 = i915;
1162 	ggtt->vm.dma = i915->drm.dev;
1163 	dma_resv_init(&ggtt->vm._resv);
1164 
1165 	if (GRAPHICS_VER(i915) <= 5)
1166 		ret = i915_gmch_probe(ggtt);
1167 	else if (GRAPHICS_VER(i915) < 8)
1168 		ret = gen6_gmch_probe(ggtt);
1169 	else
1170 		ret = gen8_gmch_probe(ggtt);
1171 	if (ret) {
1172 		dma_resv_fini(&ggtt->vm._resv);
1173 		return ret;
1174 	}
1175 
1176 	if ((ggtt->vm.total - 1) >> 32) {
1177 		drm_err(&i915->drm,
1178 			"We never expected a Global GTT with more than 32bits"
1179 			" of address space! Found %lldM!\n",
1180 			ggtt->vm.total >> 20);
1181 		ggtt->vm.total = 1ULL << 32;
1182 		ggtt->mappable_end =
1183 			min_t(u64, ggtt->mappable_end, ggtt->vm.total);
1184 	}
1185 
1186 	if (ggtt->mappable_end > ggtt->vm.total) {
1187 		drm_err(&i915->drm,
1188 			"mappable aperture extends past end of GGTT,"
1189 			" aperture=%pa, total=%llx\n",
1190 			&ggtt->mappable_end, ggtt->vm.total);
1191 		ggtt->mappable_end = ggtt->vm.total;
1192 	}
1193 
1194 	/* GMADR is the PCI mmio aperture into the global GTT. */
1195 	drm_dbg(&i915->drm, "GGTT size = %lluM\n", ggtt->vm.total >> 20);
1196 	drm_dbg(&i915->drm, "GMADR size = %lluM\n",
1197 		(u64)ggtt->mappable_end >> 20);
1198 	drm_dbg(&i915->drm, "DSM size = %lluM\n",
1199 		(u64)resource_size(&intel_graphics_stolen_res) >> 20);
1200 
1201 	return 0;
1202 }
1203 
1204 /**
1205  * i915_ggtt_probe_hw - Probe GGTT hardware location
1206  * @i915: i915 device
1207  */
1208 int i915_ggtt_probe_hw(struct drm_i915_private *i915)
1209 {
1210 	int ret;
1211 
1212 	ret = ggtt_probe_hw(&i915->ggtt, to_gt(i915));
1213 	if (ret)
1214 		return ret;
1215 
1216 	if (intel_vtd_active(i915))
1217 		drm_info(&i915->drm, "VT-d active for gfx access\n");
1218 
1219 	return 0;
1220 }
1221 
1222 int i915_ggtt_enable_hw(struct drm_i915_private *i915)
1223 {
1224 	if (GRAPHICS_VER(i915) < 6 && !intel_enable_gtt())
1225 		return -EIO;
1226 
1227 	return 0;
1228 }
1229 
1230 void i915_ggtt_enable_guc(struct i915_ggtt *ggtt)
1231 {
1232 	GEM_BUG_ON(ggtt->invalidate != gen8_ggtt_invalidate);
1233 
1234 	ggtt->invalidate = guc_ggtt_invalidate;
1235 
1236 	ggtt->invalidate(ggtt);
1237 }
1238 
1239 void i915_ggtt_disable_guc(struct i915_ggtt *ggtt)
1240 {
1241 	/* XXX Temporary pardon for error unload */
1242 	if (ggtt->invalidate == gen8_ggtt_invalidate)
1243 		return;
1244 
1245 	/* We should only be called after i915_ggtt_enable_guc() */
1246 	GEM_BUG_ON(ggtt->invalidate != guc_ggtt_invalidate);
1247 
1248 	ggtt->invalidate = gen8_ggtt_invalidate;
1249 
1250 	ggtt->invalidate(ggtt);
1251 }
1252 
1253 /**
1254  * i915_ggtt_resume_vm - Restore the memory mappings for a GGTT or DPT VM
1255  * @vm: The VM to restore the mappings for
1256  *
1257  * Restore the memory mappings for all objects mapped to HW via the GGTT or a
1258  * DPT page table.
1259  *
1260  * Returns %true if restoring the mapping for any object that was in a write
1261  * domain before suspend.
1262  */
1263 bool i915_ggtt_resume_vm(struct i915_address_space *vm)
1264 {
1265 	struct i915_vma *vma;
1266 	bool write_domain_objs = false;
1267 	int open;
1268 
1269 	drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
1270 
1271 	/* First fill our portion of the GTT with scratch pages */
1272 	vm->clear_range(vm, 0, vm->total);
1273 
1274 	/* Skip rewriting PTE on VMA unbind. */
1275 	open = atomic_xchg(&vm->open, 0);
1276 
1277 	/* clflush objects bound into the GGTT and rebind them. */
1278 	list_for_each_entry(vma, &vm->bound_list, vm_link) {
1279 		struct drm_i915_gem_object *obj = vma->obj;
1280 		unsigned int was_bound =
1281 			atomic_read(&vma->flags) & I915_VMA_BIND_MASK;
1282 
1283 		GEM_BUG_ON(!was_bound);
1284 		vma->ops->bind_vma(vm, NULL, vma,
1285 				   obj ? obj->cache_level : 0,
1286 				   was_bound);
1287 		if (obj) { /* only used during resume => exclusive access */
1288 			write_domain_objs |= fetch_and_zero(&obj->write_domain);
1289 			obj->read_domains |= I915_GEM_DOMAIN_GTT;
1290 		}
1291 	}
1292 
1293 	atomic_set(&vm->open, open);
1294 
1295 	return write_domain_objs;
1296 }
1297 
1298 void i915_ggtt_resume(struct i915_ggtt *ggtt)
1299 {
1300 	bool flush;
1301 
1302 	intel_gt_check_and_clear_faults(ggtt->vm.gt);
1303 
1304 	flush = i915_ggtt_resume_vm(&ggtt->vm);
1305 
1306 	ggtt->invalidate(ggtt);
1307 
1308 	if (flush)
1309 		wbinvd_on_all_cpus();
1310 
1311 	if (GRAPHICS_VER(ggtt->vm.i915) >= 8)
1312 		setup_private_pat(ggtt->vm.gt->uncore);
1313 
1314 	intel_ggtt_restore_fences(ggtt);
1315 }
1316