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