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