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