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