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/intel/i915_drm.h>
13 #include <drm/intel/intel-gtt.h>
14 
15 #include "gem/i915_gem_lmem.h"
16 
17 #include "intel_context.h"
18 #include "intel_ggtt_gmch.h"
19 #include "intel_gpu_commands.h"
20 #include "intel_gt.h"
21 #include "intel_gt_regs.h"
22 #include "intel_pci_config.h"
23 #include "intel_ring.h"
24 #include "i915_drv.h"
25 #include "i915_pci.h"
26 #include "i915_reg.h"
27 #include "i915_request.h"
28 #include "i915_scatterlist.h"
29 #include "i915_utils.h"
30 #include "i915_vgpu.h"
31 
32 #include "intel_gtt.h"
33 #include "gen8_ppgtt.h"
34 #include "intel_engine_pm.h"
35 
i915_ggtt_color_adjust(const struct drm_mm_node * node,unsigned long color,u64 * start,u64 * end)36 static void i915_ggtt_color_adjust(const struct drm_mm_node *node,
37 				   unsigned long color,
38 				   u64 *start,
39 				   u64 *end)
40 {
41 	if (i915_node_color_differs(node, color))
42 		*start += I915_GTT_PAGE_SIZE;
43 
44 	/*
45 	 * Also leave a space between the unallocated reserved node after the
46 	 * GTT and any objects within the GTT, i.e. we use the color adjustment
47 	 * to insert a guard page to prevent prefetches crossing over the
48 	 * GTT boundary.
49 	 */
50 	node = list_next_entry(node, node_list);
51 	if (node->color != color)
52 		*end -= I915_GTT_PAGE_SIZE;
53 }
54 
ggtt_init_hw(struct i915_ggtt * ggtt)55 static int ggtt_init_hw(struct i915_ggtt *ggtt)
56 {
57 	struct drm_i915_private *i915 = ggtt->vm.i915;
58 
59 	i915_address_space_init(&ggtt->vm, VM_CLASS_GGTT);
60 
61 	ggtt->vm.is_ggtt = true;
62 
63 	/* Only VLV supports read-only GGTT mappings */
64 	ggtt->vm.has_read_only = IS_VALLEYVIEW(i915);
65 
66 	if (!HAS_LLC(i915) && !HAS_PPGTT(i915))
67 		ggtt->vm.mm.color_adjust = i915_ggtt_color_adjust;
68 
69 	if (ggtt->mappable_end) {
70 #ifdef __linux__
71 		if (!io_mapping_init_wc(&ggtt->iomap,
72 					ggtt->gmadr.start,
73 					ggtt->mappable_end)) {
74 			ggtt->vm.cleanup(&ggtt->vm);
75 			return -EIO;
76 		}
77 
78 		ggtt->mtrr = arch_phys_wc_add(ggtt->gmadr.start,
79 					      ggtt->mappable_end);
80 #else
81 		bus_space_handle_t bsh;
82 		int i;
83 
84 		/* XXX would be a lot nicer to get agp info before now */
85 		uvm_page_physload(atop(ggtt->gmadr.start),
86 		    atop(ggtt->gmadr.start + ggtt->mappable_end),
87 		    atop(ggtt->gmadr.start),
88 		    atop(ggtt->gmadr.start + ggtt->mappable_end),
89 		    PHYSLOAD_DEVICE);
90 		/* array of vm pages that physload introduced. */
91 		i915->pgs = PHYS_TO_VM_PAGE(ggtt->gmadr.start);
92 		KASSERT(i915->pgs != NULL);
93 		/*
94 		 * XXX mark all pages write combining so user mmaps get the
95 		 * right bits. We really need a proper MI api for doing this,
96 		 * but for now this allows us to use PAT where available.
97 		 */
98 		for (i = 0; i < atop(ggtt->mappable_end); i++)
99 			atomic_setbits_int(&(i915->pgs[i].pg_flags),
100 			    PG_PMAP_WC);
101 		if (bus_space_map(i915->bst, ggtt->gmadr.start,
102 		    ggtt->mappable_end,
103 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &bsh))
104 			panic("can't map aperture");
105 		ggtt->iomap.base = ggtt->gmadr.start;
106 		ggtt->iomap.size = ggtt->mappable_end;
107 		ggtt->iomap.iomem = bus_space_vaddr(i915->bst, bsh);
108 #endif
109 	}
110 
111 	intel_ggtt_init_fences(ggtt);
112 
113 	return 0;
114 }
115 
116 /**
117  * i915_ggtt_init_hw - Initialize GGTT hardware
118  * @i915: i915 device
119  */
i915_ggtt_init_hw(struct drm_i915_private * i915)120 int i915_ggtt_init_hw(struct drm_i915_private *i915)
121 {
122 	int ret;
123 
124 	/*
125 	 * Note that we use page colouring to enforce a guard page at the
126 	 * end of the address space. This is required as the CS may prefetch
127 	 * beyond the end of the batch buffer, across the page boundary,
128 	 * and beyond the end of the GTT if we do not provide a guard.
129 	 */
130 	ret = ggtt_init_hw(to_gt(i915)->ggtt);
131 	if (ret)
132 		return ret;
133 
134 	return 0;
135 }
136 
137 /**
138  * i915_ggtt_suspend_vm - Suspend the memory mappings for a GGTT or DPT VM
139  * @vm: The VM to suspend the mappings for
140  *
141  * Suspend the memory mappings for all objects mapped to HW via the GGTT or a
142  * DPT page table.
143  */
i915_ggtt_suspend_vm(struct i915_address_space * vm)144 void i915_ggtt_suspend_vm(struct i915_address_space *vm)
145 {
146 	struct i915_vma *vma, *vn;
147 	int save_skip_rewrite;
148 
149 	drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
150 
151 retry:
152 	i915_gem_drain_freed_objects(vm->i915);
153 
154 	mutex_lock(&vm->mutex);
155 
156 	/*
157 	 * Skip rewriting PTE on VMA unbind.
158 	 * FIXME: Use an argument to i915_vma_unbind() instead?
159 	 */
160 	save_skip_rewrite = vm->skip_pte_rewrite;
161 	vm->skip_pte_rewrite = true;
162 
163 	list_for_each_entry_safe(vma, vn, &vm->bound_list, vm_link) {
164 		struct drm_i915_gem_object *obj = vma->obj;
165 
166 		GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
167 
168 		if (i915_vma_is_pinned(vma) || !i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
169 			continue;
170 
171 		/* unlikely to race when GPU is idle, so no worry about slowpath.. */
172 		if (WARN_ON(!i915_gem_object_trylock(obj, NULL))) {
173 			/*
174 			 * No dead objects should appear here, GPU should be
175 			 * completely idle, and userspace suspended
176 			 */
177 			i915_gem_object_get(obj);
178 
179 			mutex_unlock(&vm->mutex);
180 
181 			i915_gem_object_lock(obj, NULL);
182 			GEM_WARN_ON(i915_vma_unbind(vma));
183 			i915_gem_object_unlock(obj);
184 			i915_gem_object_put(obj);
185 
186 			vm->skip_pte_rewrite = save_skip_rewrite;
187 			goto retry;
188 		}
189 
190 		if (!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND)) {
191 			i915_vma_wait_for_bind(vma);
192 
193 			__i915_vma_evict(vma, false);
194 			drm_mm_remove_node(&vma->node);
195 		}
196 
197 		i915_gem_object_unlock(obj);
198 	}
199 
200 	vm->clear_range(vm, 0, vm->total);
201 
202 	vm->skip_pte_rewrite = save_skip_rewrite;
203 
204 	mutex_unlock(&vm->mutex);
205 }
206 
i915_ggtt_suspend(struct i915_ggtt * ggtt)207 void i915_ggtt_suspend(struct i915_ggtt *ggtt)
208 {
209 	struct intel_gt *gt;
210 
211 	i915_ggtt_suspend_vm(&ggtt->vm);
212 	ggtt->invalidate(ggtt);
213 
214 	list_for_each_entry(gt, &ggtt->gt_list, ggtt_link)
215 		intel_gt_check_and_clear_faults(gt);
216 }
217 
gen6_ggtt_invalidate(struct i915_ggtt * ggtt)218 void gen6_ggtt_invalidate(struct i915_ggtt *ggtt)
219 {
220 	struct intel_uncore *uncore = ggtt->vm.gt->uncore;
221 
222 	spin_lock_irq(&uncore->lock);
223 	intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
224 	intel_uncore_read_fw(uncore, GFX_FLSH_CNTL_GEN6);
225 	spin_unlock_irq(&uncore->lock);
226 }
227 
needs_wc_ggtt_mapping(struct drm_i915_private * i915)228 static bool needs_wc_ggtt_mapping(struct drm_i915_private *i915)
229 {
230 	/*
231 	 * On BXT+/ICL+ writes larger than 64 bit to the GTT pagetable range
232 	 * will be dropped. For WC mappings in general we have 64 byte burst
233 	 * writes when the WC buffer is flushed, so we can't use it, but have to
234 	 * resort to an uncached mapping. The WC issue is easily caught by the
235 	 * readback check when writing GTT PTE entries.
236 	 */
237 	if (!IS_GEN9_LP(i915) && GRAPHICS_VER(i915) < 11)
238 		return true;
239 
240 	return false;
241 }
242 
gen8_ggtt_invalidate(struct i915_ggtt * ggtt)243 static void gen8_ggtt_invalidate(struct i915_ggtt *ggtt)
244 {
245 	struct intel_uncore *uncore = ggtt->vm.gt->uncore;
246 
247 	/*
248 	 * Note that as an uncached mmio write, this will flush the
249 	 * WCB of the writes into the GGTT before it triggers the invalidate.
250 	 *
251 	 * Only perform this when GGTT is mapped as WC, see ggtt_probe_common().
252 	 */
253 	if (needs_wc_ggtt_mapping(ggtt->vm.i915))
254 		intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6,
255 				      GFX_FLSH_CNTL_EN);
256 }
257 
guc_ggtt_ct_invalidate(struct intel_gt * gt)258 static void guc_ggtt_ct_invalidate(struct intel_gt *gt)
259 {
260 	struct intel_uncore *uncore = gt->uncore;
261 	intel_wakeref_t wakeref;
262 
263 	with_intel_runtime_pm_if_active(uncore->rpm, wakeref)
264 		intel_guc_invalidate_tlb_guc(gt_to_guc(gt));
265 }
266 
guc_ggtt_invalidate(struct i915_ggtt * ggtt)267 static void guc_ggtt_invalidate(struct i915_ggtt *ggtt)
268 {
269 	struct drm_i915_private *i915 = ggtt->vm.i915;
270 	struct intel_gt *gt;
271 
272 	gen8_ggtt_invalidate(ggtt);
273 
274 	list_for_each_entry(gt, &ggtt->gt_list, ggtt_link) {
275 		if (intel_guc_tlb_invalidation_is_available(gt_to_guc(gt)))
276 			guc_ggtt_ct_invalidate(gt);
277 		else if (GRAPHICS_VER(i915) >= 12)
278 			intel_uncore_write_fw(gt->uncore,
279 					      GEN12_GUC_TLB_INV_CR,
280 					      GEN12_GUC_TLB_INV_CR_INVALIDATE);
281 		else
282 			intel_uncore_write_fw(gt->uncore,
283 					      GEN8_GTCR, GEN8_GTCR_INVALIDATE);
284 	}
285 }
286 
mtl_ggtt_pte_encode(dma_addr_t addr,unsigned int pat_index,u32 flags)287 static u64 mtl_ggtt_pte_encode(dma_addr_t addr,
288 			       unsigned int pat_index,
289 			       u32 flags)
290 {
291 	gen8_pte_t pte = addr | GEN8_PAGE_PRESENT;
292 
293 	WARN_ON_ONCE(addr & ~GEN12_GGTT_PTE_ADDR_MASK);
294 
295 	if (flags & PTE_LM)
296 		pte |= GEN12_GGTT_PTE_LM;
297 
298 	if (pat_index & BIT(0))
299 		pte |= MTL_GGTT_PTE_PAT0;
300 
301 	if (pat_index & BIT(1))
302 		pte |= MTL_GGTT_PTE_PAT1;
303 
304 	return pte;
305 }
306 
gen8_ggtt_pte_encode(dma_addr_t addr,unsigned int pat_index,u32 flags)307 u64 gen8_ggtt_pte_encode(dma_addr_t addr,
308 			 unsigned int pat_index,
309 			 u32 flags)
310 {
311 	gen8_pte_t pte = addr | GEN8_PAGE_PRESENT;
312 
313 	if (flags & PTE_LM)
314 		pte |= GEN12_GGTT_PTE_LM;
315 
316 	return pte;
317 }
318 
should_update_ggtt_with_bind(struct i915_ggtt * ggtt)319 static bool should_update_ggtt_with_bind(struct i915_ggtt *ggtt)
320 {
321 	struct intel_gt *gt = ggtt->vm.gt;
322 
323 	return intel_gt_is_bind_context_ready(gt);
324 }
325 
gen8_ggtt_bind_get_ce(struct i915_ggtt * ggtt,intel_wakeref_t * wakeref)326 static struct intel_context *gen8_ggtt_bind_get_ce(struct i915_ggtt *ggtt, intel_wakeref_t *wakeref)
327 {
328 	struct intel_context *ce;
329 	struct intel_gt *gt = ggtt->vm.gt;
330 
331 	if (intel_gt_is_wedged(gt))
332 		return NULL;
333 
334 	ce = gt->engine[BCS0]->bind_context;
335 	GEM_BUG_ON(!ce);
336 
337 	/*
338 	 * If the GT is not awake already at this stage then fallback
339 	 * to pci based GGTT update otherwise __intel_wakeref_get_first()
340 	 * would conflict with fs_reclaim trying to allocate memory while
341 	 * doing rpm_resume().
342 	 */
343 	*wakeref = intel_gt_pm_get_if_awake(gt);
344 	if (!*wakeref)
345 		return NULL;
346 
347 	intel_engine_pm_get(ce->engine);
348 
349 	return ce;
350 }
351 
gen8_ggtt_bind_put_ce(struct intel_context * ce,intel_wakeref_t wakeref)352 static void gen8_ggtt_bind_put_ce(struct intel_context *ce, intel_wakeref_t wakeref)
353 {
354 	intel_engine_pm_put(ce->engine);
355 	intel_gt_pm_put(ce->engine->gt, wakeref);
356 }
357 
gen8_ggtt_bind_ptes(struct i915_ggtt * ggtt,u32 offset,struct sg_table * pages,u32 num_entries,const gen8_pte_t pte)358 static bool gen8_ggtt_bind_ptes(struct i915_ggtt *ggtt, u32 offset,
359 				struct sg_table *pages, u32 num_entries,
360 				const gen8_pte_t pte)
361 {
362 	struct i915_sched_attr attr = {};
363 	struct intel_gt *gt = ggtt->vm.gt;
364 	const gen8_pte_t scratch_pte = ggtt->vm.scratch[0]->encode;
365 	struct sgt_iter iter;
366 	struct i915_request *rq;
367 	struct intel_context *ce;
368 	intel_wakeref_t wakeref;
369 	u32 *cs;
370 
371 	if (!num_entries)
372 		return true;
373 
374 	ce = gen8_ggtt_bind_get_ce(ggtt, &wakeref);
375 	if (!ce)
376 		return false;
377 
378 	if (pages)
379 		iter = __sgt_iter(pages->sgl, true);
380 
381 	while (num_entries) {
382 		int count = 0;
383 		dma_addr_t addr;
384 		/*
385 		 * MI_UPDATE_GTT can update 512 entries in a single command but
386 		 * that end up with engine reset, 511 works.
387 		 */
388 		u32 n_ptes = min_t(u32, 511, num_entries);
389 
390 		if (mutex_lock_interruptible(&ce->timeline->mutex))
391 			goto put_ce;
392 
393 		intel_context_enter(ce);
394 		rq = __i915_request_create(ce, GFP_NOWAIT | GFP_ATOMIC);
395 		intel_context_exit(ce);
396 		if (IS_ERR(rq)) {
397 			GT_TRACE(gt, "Failed to get bind request\n");
398 			mutex_unlock(&ce->timeline->mutex);
399 			goto put_ce;
400 		}
401 
402 		cs = intel_ring_begin(rq, 2 * n_ptes + 2);
403 		if (IS_ERR(cs)) {
404 			GT_TRACE(gt, "Failed to ring space for GGTT bind\n");
405 			i915_request_set_error_once(rq, PTR_ERR(cs));
406 			/* once a request is created, it must be queued */
407 			goto queue_err_rq;
408 		}
409 
410 		*cs++ = MI_UPDATE_GTT | (2 * n_ptes);
411 		*cs++ = offset << 12;
412 
413 		if (pages) {
414 			for_each_sgt_daddr_next(addr, iter) {
415 				if (count == n_ptes)
416 					break;
417 				*cs++ = lower_32_bits(pte | addr);
418 				*cs++ = upper_32_bits(pte | addr);
419 				count++;
420 			}
421 			/* fill remaining with scratch pte, if any */
422 			if (count < n_ptes) {
423 				memset64((u64 *)cs, scratch_pte,
424 					 n_ptes - count);
425 				cs += (n_ptes - count) * 2;
426 			}
427 		} else {
428 			memset64((u64 *)cs, pte, n_ptes);
429 			cs += n_ptes * 2;
430 		}
431 
432 		intel_ring_advance(rq, cs);
433 queue_err_rq:
434 		i915_request_get(rq);
435 		__i915_request_commit(rq);
436 		__i915_request_queue(rq, &attr);
437 
438 		mutex_unlock(&ce->timeline->mutex);
439 		/* This will break if the request is complete or after engine reset */
440 		i915_request_wait(rq, 0, MAX_SCHEDULE_TIMEOUT);
441 		if (rq->fence.error)
442 			goto err_rq;
443 
444 		i915_request_put(rq);
445 
446 		num_entries -= n_ptes;
447 		offset += n_ptes;
448 	}
449 
450 	gen8_ggtt_bind_put_ce(ce, wakeref);
451 	return true;
452 
453 err_rq:
454 	i915_request_put(rq);
455 put_ce:
456 	gen8_ggtt_bind_put_ce(ce, wakeref);
457 	return false;
458 }
459 
gen8_set_pte(void __iomem * addr,gen8_pte_t pte)460 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
461 {
462 	writeq(pte, addr);
463 }
464 
gen8_ggtt_insert_page(struct i915_address_space * vm,dma_addr_t addr,u64 offset,unsigned int pat_index,u32 flags)465 static void gen8_ggtt_insert_page(struct i915_address_space *vm,
466 				  dma_addr_t addr,
467 				  u64 offset,
468 				  unsigned int pat_index,
469 				  u32 flags)
470 {
471 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
472 	gen8_pte_t __iomem *pte =
473 		(gen8_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
474 
475 	gen8_set_pte(pte, ggtt->vm.pte_encode(addr, pat_index, flags));
476 
477 	ggtt->invalidate(ggtt);
478 }
479 
gen8_ggtt_insert_page_bind(struct i915_address_space * vm,dma_addr_t addr,u64 offset,unsigned int pat_index,u32 flags)480 static void gen8_ggtt_insert_page_bind(struct i915_address_space *vm,
481 				       dma_addr_t addr, u64 offset,
482 				       unsigned int pat_index, u32 flags)
483 {
484 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
485 	gen8_pte_t pte;
486 
487 	pte = ggtt->vm.pte_encode(addr, pat_index, flags);
488 	if (should_update_ggtt_with_bind(i915_vm_to_ggtt(vm)) &&
489 	    gen8_ggtt_bind_ptes(ggtt, offset, NULL, 1, pte))
490 		return ggtt->invalidate(ggtt);
491 
492 	gen8_ggtt_insert_page(vm, addr, offset, pat_index, flags);
493 }
494 
gen8_ggtt_insert_entries(struct i915_address_space * vm,struct i915_vma_resource * vma_res,unsigned int pat_index,u32 flags)495 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
496 				     struct i915_vma_resource *vma_res,
497 				     unsigned int pat_index,
498 				     u32 flags)
499 {
500 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
501 	const gen8_pte_t pte_encode = ggtt->vm.pte_encode(0, pat_index, flags);
502 	gen8_pte_t __iomem *gte;
503 	gen8_pte_t __iomem *end;
504 	struct sgt_iter iter;
505 	dma_addr_t addr;
506 
507 	/*
508 	 * Note that we ignore PTE_READ_ONLY here. The caller must be careful
509 	 * not to allow the user to override access to a read only page.
510 	 */
511 
512 	gte = (gen8_pte_t __iomem *)ggtt->gsm;
513 	gte += (vma_res->start - vma_res->guard) / I915_GTT_PAGE_SIZE;
514 	end = gte + vma_res->guard / I915_GTT_PAGE_SIZE;
515 	while (gte < end)
516 		gen8_set_pte(gte++, vm->scratch[0]->encode);
517 	end += (vma_res->node_size + vma_res->guard) / I915_GTT_PAGE_SIZE;
518 
519 	for_each_sgt_daddr(addr, iter, vma_res->bi.pages)
520 		gen8_set_pte(gte++, pte_encode | addr);
521 	GEM_BUG_ON(gte > end);
522 
523 	/* Fill the allocated but "unused" space beyond the end of the buffer */
524 	while (gte < end)
525 		gen8_set_pte(gte++, vm->scratch[0]->encode);
526 
527 	/*
528 	 * We want to flush the TLBs only after we're certain all the PTE
529 	 * updates have finished.
530 	 */
531 	ggtt->invalidate(ggtt);
532 }
533 
__gen8_ggtt_insert_entries_bind(struct i915_address_space * vm,struct i915_vma_resource * vma_res,unsigned int pat_index,u32 flags)534 static bool __gen8_ggtt_insert_entries_bind(struct i915_address_space *vm,
535 					    struct i915_vma_resource *vma_res,
536 					    unsigned int pat_index, u32 flags)
537 {
538 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
539 	gen8_pte_t scratch_pte = vm->scratch[0]->encode;
540 	gen8_pte_t pte_encode;
541 	u64 start, end;
542 
543 	pte_encode = ggtt->vm.pte_encode(0, pat_index, flags);
544 	start = (vma_res->start - vma_res->guard) / I915_GTT_PAGE_SIZE;
545 	end = start + vma_res->guard / I915_GTT_PAGE_SIZE;
546 	if (!gen8_ggtt_bind_ptes(ggtt, start, NULL, end - start, scratch_pte))
547 		goto err;
548 
549 	start = end;
550 	end += (vma_res->node_size + vma_res->guard) / I915_GTT_PAGE_SIZE;
551 	if (!gen8_ggtt_bind_ptes(ggtt, start, vma_res->bi.pages,
552 	      vma_res->node_size / I915_GTT_PAGE_SIZE, pte_encode))
553 		goto err;
554 
555 	start += vma_res->node_size / I915_GTT_PAGE_SIZE;
556 	if (!gen8_ggtt_bind_ptes(ggtt, start, NULL, end - start, scratch_pte))
557 		goto err;
558 
559 	return true;
560 
561 err:
562 	return false;
563 }
564 
gen8_ggtt_insert_entries_bind(struct i915_address_space * vm,struct i915_vma_resource * vma_res,unsigned int pat_index,u32 flags)565 static void gen8_ggtt_insert_entries_bind(struct i915_address_space *vm,
566 					  struct i915_vma_resource *vma_res,
567 					  unsigned int pat_index, u32 flags)
568 {
569 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
570 
571 	if (should_update_ggtt_with_bind(i915_vm_to_ggtt(vm)) &&
572 	    __gen8_ggtt_insert_entries_bind(vm, vma_res, pat_index, flags))
573 		return ggtt->invalidate(ggtt);
574 
575 	gen8_ggtt_insert_entries(vm, vma_res, pat_index, flags);
576 }
577 
gen8_ggtt_clear_range(struct i915_address_space * vm,u64 start,u64 length)578 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
579 				  u64 start, u64 length)
580 {
581 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
582 	unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
583 	unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
584 	const gen8_pte_t scratch_pte = vm->scratch[0]->encode;
585 	gen8_pte_t __iomem *gtt_base =
586 		(gen8_pte_t __iomem *)ggtt->gsm + first_entry;
587 	const int max_entries = ggtt_total_entries(ggtt) - first_entry;
588 	int i;
589 
590 	if (WARN(num_entries > max_entries,
591 		 "First entry = %d; Num entries = %d (max=%d)\n",
592 		 first_entry, num_entries, max_entries))
593 		num_entries = max_entries;
594 
595 	for (i = 0; i < num_entries; i++)
596 		gen8_set_pte(&gtt_base[i], scratch_pte);
597 }
598 
gen8_ggtt_scratch_range_bind(struct i915_address_space * vm,u64 start,u64 length)599 static void gen8_ggtt_scratch_range_bind(struct i915_address_space *vm,
600 					 u64 start, u64 length)
601 {
602 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
603 	unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
604 	unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
605 	const gen8_pte_t scratch_pte = vm->scratch[0]->encode;
606 	const int max_entries = ggtt_total_entries(ggtt) - first_entry;
607 
608 	if (WARN(num_entries > max_entries,
609 		 "First entry = %d; Num entries = %d (max=%d)\n",
610 		 first_entry, num_entries, max_entries))
611 		num_entries = max_entries;
612 
613 	if (should_update_ggtt_with_bind(ggtt) && gen8_ggtt_bind_ptes(ggtt, first_entry,
614 	     NULL, num_entries, scratch_pte))
615 		return ggtt->invalidate(ggtt);
616 
617 	gen8_ggtt_clear_range(vm, start, length);
618 }
619 
gen6_ggtt_insert_page(struct i915_address_space * vm,dma_addr_t addr,u64 offset,unsigned int pat_index,u32 flags)620 static void gen6_ggtt_insert_page(struct i915_address_space *vm,
621 				  dma_addr_t addr,
622 				  u64 offset,
623 				  unsigned int pat_index,
624 				  u32 flags)
625 {
626 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
627 	gen6_pte_t __iomem *pte =
628 		(gen6_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
629 
630 	iowrite32(vm->pte_encode(addr, pat_index, flags), pte);
631 
632 	ggtt->invalidate(ggtt);
633 }
634 
635 /*
636  * Binds an object into the global gtt with the specified cache level.
637  * The object will be accessible to the GPU via commands whose operands
638  * reference offsets within the global GTT as well as accessible by the GPU
639  * through the GMADR mapped BAR (i915->mm.gtt->gtt).
640  */
gen6_ggtt_insert_entries(struct i915_address_space * vm,struct i915_vma_resource * vma_res,unsigned int pat_index,u32 flags)641 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
642 				     struct i915_vma_resource *vma_res,
643 				     unsigned int pat_index,
644 				     u32 flags)
645 {
646 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
647 	gen6_pte_t __iomem *gte;
648 	gen6_pte_t __iomem *end;
649 	struct sgt_iter iter;
650 	dma_addr_t addr;
651 
652 	gte = (gen6_pte_t __iomem *)ggtt->gsm;
653 	gte += (vma_res->start - vma_res->guard) / I915_GTT_PAGE_SIZE;
654 
655 	end = gte + vma_res->guard / I915_GTT_PAGE_SIZE;
656 	while (gte < end)
657 		iowrite32(vm->scratch[0]->encode, gte++);
658 	end += (vma_res->node_size + vma_res->guard) / I915_GTT_PAGE_SIZE;
659 	for_each_sgt_daddr(addr, iter, vma_res->bi.pages)
660 		iowrite32(vm->pte_encode(addr, pat_index, flags), gte++);
661 	GEM_BUG_ON(gte > end);
662 
663 	/* Fill the allocated but "unused" space beyond the end of the buffer */
664 	while (gte < end)
665 		iowrite32(vm->scratch[0]->encode, gte++);
666 
667 	/*
668 	 * We want to flush the TLBs only after we're certain all the PTE
669 	 * updates have finished.
670 	 */
671 	ggtt->invalidate(ggtt);
672 }
673 
nop_clear_range(struct i915_address_space * vm,u64 start,u64 length)674 static void nop_clear_range(struct i915_address_space *vm,
675 			    u64 start, u64 length)
676 {
677 }
678 
bxt_vtd_ggtt_wa(struct i915_address_space * vm)679 static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
680 {
681 	/*
682 	 * Make sure the internal GAM fifo has been cleared of all GTT
683 	 * writes before exiting stop_machine(). This guarantees that
684 	 * any aperture accesses waiting to start in another process
685 	 * cannot back up behind the GTT writes causing a hang.
686 	 * The register can be any arbitrary GAM register.
687 	 */
688 	intel_uncore_posting_read_fw(vm->gt->uncore, GFX_FLSH_CNTL_GEN6);
689 }
690 
691 struct insert_page {
692 	struct i915_address_space *vm;
693 	dma_addr_t addr;
694 	u64 offset;
695 	unsigned int pat_index;
696 };
697 
bxt_vtd_ggtt_insert_page__cb(void * _arg)698 static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
699 {
700 	struct insert_page *arg = _arg;
701 
702 	gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset,
703 			      arg->pat_index, 0);
704 	bxt_vtd_ggtt_wa(arg->vm);
705 
706 	return 0;
707 }
708 
bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space * vm,dma_addr_t addr,u64 offset,unsigned int pat_index,u32 unused)709 static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
710 					  dma_addr_t addr,
711 					  u64 offset,
712 					  unsigned int pat_index,
713 					  u32 unused)
714 {
715 	struct insert_page arg = { vm, addr, offset, pat_index };
716 
717 	stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
718 }
719 
720 struct insert_entries {
721 	struct i915_address_space *vm;
722 	struct i915_vma_resource *vma_res;
723 	unsigned int pat_index;
724 	u32 flags;
725 };
726 
bxt_vtd_ggtt_insert_entries__cb(void * _arg)727 static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
728 {
729 	struct insert_entries *arg = _arg;
730 
731 	gen8_ggtt_insert_entries(arg->vm, arg->vma_res,
732 				 arg->pat_index, arg->flags);
733 	bxt_vtd_ggtt_wa(arg->vm);
734 
735 	return 0;
736 }
737 
bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space * vm,struct i915_vma_resource * vma_res,unsigned int pat_index,u32 flags)738 static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
739 					     struct i915_vma_resource *vma_res,
740 					     unsigned int pat_index,
741 					     u32 flags)
742 {
743 	struct insert_entries arg = { vm, vma_res, pat_index, flags };
744 
745 	stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
746 }
747 
gen6_ggtt_clear_range(struct i915_address_space * vm,u64 start,u64 length)748 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
749 				  u64 start, u64 length)
750 {
751 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
752 	unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
753 	unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
754 	gen6_pte_t scratch_pte, __iomem *gtt_base =
755 		(gen6_pte_t __iomem *)ggtt->gsm + first_entry;
756 	const int max_entries = ggtt_total_entries(ggtt) - first_entry;
757 	int i;
758 
759 	if (WARN(num_entries > max_entries,
760 		 "First entry = %d; Num entries = %d (max=%d)\n",
761 		 first_entry, num_entries, max_entries))
762 		num_entries = max_entries;
763 
764 	scratch_pte = vm->scratch[0]->encode;
765 	for (i = 0; i < num_entries; i++)
766 		iowrite32(scratch_pte, &gtt_base[i]);
767 }
768 
intel_ggtt_bind_vma(struct i915_address_space * vm,struct i915_vm_pt_stash * stash,struct i915_vma_resource * vma_res,unsigned int pat_index,u32 flags)769 void intel_ggtt_bind_vma(struct i915_address_space *vm,
770 			 struct i915_vm_pt_stash *stash,
771 			 struct i915_vma_resource *vma_res,
772 			 unsigned int pat_index,
773 			 u32 flags)
774 {
775 	u32 pte_flags;
776 
777 	if (vma_res->bound_flags & (~flags & I915_VMA_BIND_MASK))
778 		return;
779 
780 	vma_res->bound_flags |= flags;
781 
782 	/* Applicable to VLV (gen8+ do not support RO in the GGTT) */
783 	pte_flags = 0;
784 	if (vma_res->bi.readonly)
785 		pte_flags |= PTE_READ_ONLY;
786 	if (vma_res->bi.lmem)
787 		pte_flags |= PTE_LM;
788 
789 	vm->insert_entries(vm, vma_res, pat_index, pte_flags);
790 	vma_res->page_sizes_gtt = I915_GTT_PAGE_SIZE;
791 }
792 
intel_ggtt_unbind_vma(struct i915_address_space * vm,struct i915_vma_resource * vma_res)793 void intel_ggtt_unbind_vma(struct i915_address_space *vm,
794 			   struct i915_vma_resource *vma_res)
795 {
796 	vm->clear_range(vm, vma_res->start, vma_res->vma_size);
797 }
798 
799 /*
800  * Reserve the top of the GuC address space for firmware images. Addresses
801  * beyond GUC_GGTT_TOP in the GuC address space are inaccessible by GuC,
802  * which makes for a suitable range to hold GuC/HuC firmware images if the
803  * size of the GGTT is 4G. However, on a 32-bit platform the size of the GGTT
804  * is limited to 2G, which is less than GUC_GGTT_TOP, but we reserve a chunk
805  * of the same size anyway, which is far more than needed, to keep the logic
806  * in uc_fw_ggtt_offset() simple.
807  */
808 #define GUC_TOP_RESERVE_SIZE (SZ_4G - GUC_GGTT_TOP)
809 
ggtt_reserve_guc_top(struct i915_ggtt * ggtt)810 static int ggtt_reserve_guc_top(struct i915_ggtt *ggtt)
811 {
812 	u64 offset;
813 	int ret;
814 
815 	if (!intel_uc_uses_guc(&ggtt->vm.gt->uc))
816 		return 0;
817 
818 	GEM_BUG_ON(ggtt->vm.total <= GUC_TOP_RESERVE_SIZE);
819 	offset = ggtt->vm.total - GUC_TOP_RESERVE_SIZE;
820 
821 	ret = i915_gem_gtt_reserve(&ggtt->vm, NULL, &ggtt->uc_fw,
822 				   GUC_TOP_RESERVE_SIZE, offset,
823 				   I915_COLOR_UNEVICTABLE, PIN_NOEVICT);
824 	if (ret)
825 		drm_dbg(&ggtt->vm.i915->drm,
826 			"Failed to reserve top of GGTT for GuC\n");
827 
828 	return ret;
829 }
830 
ggtt_release_guc_top(struct i915_ggtt * ggtt)831 static void ggtt_release_guc_top(struct i915_ggtt *ggtt)
832 {
833 	if (drm_mm_node_allocated(&ggtt->uc_fw))
834 		drm_mm_remove_node(&ggtt->uc_fw);
835 }
836 
cleanup_init_ggtt(struct i915_ggtt * ggtt)837 static void cleanup_init_ggtt(struct i915_ggtt *ggtt)
838 {
839 	ggtt_release_guc_top(ggtt);
840 	if (drm_mm_node_allocated(&ggtt->error_capture))
841 		drm_mm_remove_node(&ggtt->error_capture);
842 	mutex_destroy(&ggtt->error_mutex);
843 }
844 
init_ggtt(struct i915_ggtt * ggtt)845 static int init_ggtt(struct i915_ggtt *ggtt)
846 {
847 	/*
848 	 * Let GEM Manage all of the aperture.
849 	 *
850 	 * However, leave one page at the end still bound to the scratch page.
851 	 * There are a number of places where the hardware apparently prefetches
852 	 * past the end of the object, and we've seen multiple hangs with the
853 	 * GPU head pointer stuck in a batchbuffer bound at the last page of the
854 	 * aperture.  One page should be enough to keep any prefetching inside
855 	 * of the aperture.
856 	 */
857 	unsigned long hole_start, hole_end;
858 	struct drm_mm_node *entry;
859 	int ret;
860 
861 	/*
862 	 * GuC requires all resources that we're sharing with it to be placed in
863 	 * non-WOPCM memory. If GuC is not present or not in use we still need a
864 	 * small bias as ring wraparound at offset 0 sometimes hangs. No idea
865 	 * why.
866 	 */
867 	ggtt->pin_bias = max_t(u32, I915_GTT_PAGE_SIZE,
868 			       intel_wopcm_guc_size(&ggtt->vm.gt->wopcm));
869 
870 	ret = intel_vgt_balloon(ggtt);
871 	if (ret)
872 		return ret;
873 
874 	rw_init(&ggtt->error_mutex, "ggtter");
875 	if (ggtt->mappable_end) {
876 		/*
877 		 * Reserve a mappable slot for our lockless error capture.
878 		 *
879 		 * We strongly prefer taking address 0x0 in order to protect
880 		 * other critical buffers against accidental overwrites,
881 		 * as writing to address 0 is a very common mistake.
882 		 *
883 		 * Since 0 may already be in use by the system (e.g. the BIOS
884 		 * framebuffer), we let the reservation fail quietly and hope
885 		 * 0 remains reserved always.
886 		 *
887 		 * If we fail to reserve 0, and then fail to find any space
888 		 * for an error-capture, remain silent. We can afford not
889 		 * to reserve an error_capture node as we have fallback
890 		 * paths, and we trust that 0 will remain reserved. However,
891 		 * the only likely reason for failure to insert is a driver
892 		 * bug, which we expect to cause other failures...
893 		 *
894 		 * Since CPU can perform speculative reads on error capture
895 		 * (write-combining allows it) add scratch page after error
896 		 * capture to avoid DMAR errors.
897 		 */
898 		ggtt->error_capture.size = 2 * I915_GTT_PAGE_SIZE;
899 		ggtt->error_capture.color = I915_COLOR_UNEVICTABLE;
900 		if (drm_mm_reserve_node(&ggtt->vm.mm, &ggtt->error_capture))
901 			drm_mm_insert_node_in_range(&ggtt->vm.mm,
902 						    &ggtt->error_capture,
903 						    ggtt->error_capture.size, 0,
904 						    ggtt->error_capture.color,
905 						    0, ggtt->mappable_end,
906 						    DRM_MM_INSERT_LOW);
907 	}
908 	if (drm_mm_node_allocated(&ggtt->error_capture)) {
909 		u64 start = ggtt->error_capture.start;
910 		u64 size = ggtt->error_capture.size;
911 
912 		ggtt->vm.scratch_range(&ggtt->vm, start, size);
913 		drm_dbg(&ggtt->vm.i915->drm,
914 			"Reserved GGTT:[%llx, %llx] for use by error capture\n",
915 			start, start + size);
916 	}
917 
918 	/*
919 	 * The upper portion of the GuC address space has a sizeable hole
920 	 * (several MB) that is inaccessible by GuC. Reserve this range within
921 	 * GGTT as it can comfortably hold GuC/HuC firmware images.
922 	 */
923 	ret = ggtt_reserve_guc_top(ggtt);
924 	if (ret)
925 		goto err;
926 
927 	/* Clear any non-preallocated blocks */
928 	drm_mm_for_each_hole(entry, &ggtt->vm.mm, hole_start, hole_end) {
929 		drm_dbg(&ggtt->vm.i915->drm,
930 			"clearing unused GTT space: [%lx, %lx]\n",
931 			hole_start, hole_end);
932 		ggtt->vm.clear_range(&ggtt->vm, hole_start,
933 				     hole_end - hole_start);
934 	}
935 
936 	/* And finally clear the reserved guard page */
937 	ggtt->vm.clear_range(&ggtt->vm, ggtt->vm.total - PAGE_SIZE, PAGE_SIZE);
938 
939 	return 0;
940 
941 err:
942 	cleanup_init_ggtt(ggtt);
943 	return ret;
944 }
945 
aliasing_gtt_bind_vma(struct i915_address_space * vm,struct i915_vm_pt_stash * stash,struct i915_vma_resource * vma_res,unsigned int pat_index,u32 flags)946 static void aliasing_gtt_bind_vma(struct i915_address_space *vm,
947 				  struct i915_vm_pt_stash *stash,
948 				  struct i915_vma_resource *vma_res,
949 				  unsigned int pat_index,
950 				  u32 flags)
951 {
952 	u32 pte_flags;
953 
954 	/* Currently applicable only to VLV */
955 	pte_flags = 0;
956 	if (vma_res->bi.readonly)
957 		pte_flags |= PTE_READ_ONLY;
958 
959 	if (flags & I915_VMA_LOCAL_BIND)
960 		ppgtt_bind_vma(&i915_vm_to_ggtt(vm)->alias->vm,
961 			       stash, vma_res, pat_index, flags);
962 
963 	if (flags & I915_VMA_GLOBAL_BIND)
964 		vm->insert_entries(vm, vma_res, pat_index, pte_flags);
965 
966 	vma_res->bound_flags |= flags;
967 }
968 
aliasing_gtt_unbind_vma(struct i915_address_space * vm,struct i915_vma_resource * vma_res)969 static void aliasing_gtt_unbind_vma(struct i915_address_space *vm,
970 				    struct i915_vma_resource *vma_res)
971 {
972 	if (vma_res->bound_flags & I915_VMA_GLOBAL_BIND)
973 		vm->clear_range(vm, vma_res->start, vma_res->vma_size);
974 
975 	if (vma_res->bound_flags & I915_VMA_LOCAL_BIND)
976 		ppgtt_unbind_vma(&i915_vm_to_ggtt(vm)->alias->vm, vma_res);
977 }
978 
init_aliasing_ppgtt(struct i915_ggtt * ggtt)979 static int init_aliasing_ppgtt(struct i915_ggtt *ggtt)
980 {
981 	struct i915_vm_pt_stash stash = {};
982 	struct i915_ppgtt *ppgtt;
983 	int err;
984 
985 	ppgtt = i915_ppgtt_create(ggtt->vm.gt, 0);
986 	if (IS_ERR(ppgtt))
987 		return PTR_ERR(ppgtt);
988 
989 	if (GEM_WARN_ON(ppgtt->vm.total < ggtt->vm.total)) {
990 		err = -ENODEV;
991 		goto err_ppgtt;
992 	}
993 
994 	err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, ggtt->vm.total);
995 	if (err)
996 		goto err_ppgtt;
997 
998 	i915_gem_object_lock(ppgtt->vm.scratch[0], NULL);
999 	err = i915_vm_map_pt_stash(&ppgtt->vm, &stash);
1000 	i915_gem_object_unlock(ppgtt->vm.scratch[0]);
1001 	if (err)
1002 		goto err_stash;
1003 
1004 	/*
1005 	 * Note we only pre-allocate as far as the end of the global
1006 	 * GTT. On 48b / 4-level page-tables, the difference is very,
1007 	 * very significant! We have to preallocate as GVT/vgpu does
1008 	 * not like the page directory disappearing.
1009 	 */
1010 	ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash, 0, ggtt->vm.total);
1011 
1012 	ggtt->alias = ppgtt;
1013 	ggtt->vm.bind_async_flags |= ppgtt->vm.bind_async_flags;
1014 
1015 	GEM_BUG_ON(ggtt->vm.vma_ops.bind_vma != intel_ggtt_bind_vma);
1016 	ggtt->vm.vma_ops.bind_vma = aliasing_gtt_bind_vma;
1017 
1018 	GEM_BUG_ON(ggtt->vm.vma_ops.unbind_vma != intel_ggtt_unbind_vma);
1019 	ggtt->vm.vma_ops.unbind_vma = aliasing_gtt_unbind_vma;
1020 
1021 	i915_vm_free_pt_stash(&ppgtt->vm, &stash);
1022 	return 0;
1023 
1024 err_stash:
1025 	i915_vm_free_pt_stash(&ppgtt->vm, &stash);
1026 err_ppgtt:
1027 	i915_vm_put(&ppgtt->vm);
1028 	return err;
1029 }
1030 
fini_aliasing_ppgtt(struct i915_ggtt * ggtt)1031 static void fini_aliasing_ppgtt(struct i915_ggtt *ggtt)
1032 {
1033 	struct i915_ppgtt *ppgtt;
1034 
1035 	ppgtt = fetch_and_zero(&ggtt->alias);
1036 	if (!ppgtt)
1037 		return;
1038 
1039 	i915_vm_put(&ppgtt->vm);
1040 
1041 	ggtt->vm.vma_ops.bind_vma   = intel_ggtt_bind_vma;
1042 	ggtt->vm.vma_ops.unbind_vma = intel_ggtt_unbind_vma;
1043 }
1044 
i915_init_ggtt(struct drm_i915_private * i915)1045 int i915_init_ggtt(struct drm_i915_private *i915)
1046 {
1047 	int ret;
1048 
1049 	ret = init_ggtt(to_gt(i915)->ggtt);
1050 	if (ret)
1051 		return ret;
1052 
1053 	if (INTEL_PPGTT(i915) == INTEL_PPGTT_ALIASING) {
1054 		ret = init_aliasing_ppgtt(to_gt(i915)->ggtt);
1055 		if (ret)
1056 			cleanup_init_ggtt(to_gt(i915)->ggtt);
1057 	}
1058 
1059 	return 0;
1060 }
1061 
ggtt_cleanup_hw(struct i915_ggtt * ggtt)1062 static void ggtt_cleanup_hw(struct i915_ggtt *ggtt)
1063 {
1064 	struct i915_vma *vma, *vn;
1065 
1066 	flush_workqueue(ggtt->vm.i915->wq);
1067 	i915_gem_drain_freed_objects(ggtt->vm.i915);
1068 
1069 	mutex_lock(&ggtt->vm.mutex);
1070 
1071 	ggtt->vm.skip_pte_rewrite = true;
1072 
1073 	list_for_each_entry_safe(vma, vn, &ggtt->vm.bound_list, vm_link) {
1074 		struct drm_i915_gem_object *obj = vma->obj;
1075 		bool trylock;
1076 
1077 		trylock = i915_gem_object_trylock(obj, NULL);
1078 		WARN_ON(!trylock);
1079 
1080 		WARN_ON(__i915_vma_unbind(vma));
1081 		if (trylock)
1082 			i915_gem_object_unlock(obj);
1083 	}
1084 
1085 	if (drm_mm_node_allocated(&ggtt->error_capture))
1086 		drm_mm_remove_node(&ggtt->error_capture);
1087 	mutex_destroy(&ggtt->error_mutex);
1088 
1089 	ggtt_release_guc_top(ggtt);
1090 	intel_vgt_deballoon(ggtt);
1091 
1092 	ggtt->vm.cleanup(&ggtt->vm);
1093 
1094 	mutex_unlock(&ggtt->vm.mutex);
1095 	i915_address_space_fini(&ggtt->vm);
1096 
1097 #ifdef notyet
1098 	arch_phys_wc_del(ggtt->mtrr);
1099 
1100 	if (ggtt->iomap.size)
1101 		io_mapping_fini(&ggtt->iomap);
1102 #endif
1103 }
1104 
1105 /**
1106  * i915_ggtt_driver_release - Clean up GGTT hardware initialization
1107  * @i915: i915 device
1108  */
i915_ggtt_driver_release(struct drm_i915_private * i915)1109 void i915_ggtt_driver_release(struct drm_i915_private *i915)
1110 {
1111 	struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
1112 
1113 	fini_aliasing_ppgtt(ggtt);
1114 
1115 	intel_ggtt_fini_fences(ggtt);
1116 	ggtt_cleanup_hw(ggtt);
1117 }
1118 
1119 /**
1120  * i915_ggtt_driver_late_release - Cleanup of GGTT that needs to be done after
1121  * all free objects have been drained.
1122  * @i915: i915 device
1123  */
i915_ggtt_driver_late_release(struct drm_i915_private * i915)1124 void i915_ggtt_driver_late_release(struct drm_i915_private *i915)
1125 {
1126 	struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
1127 
1128 	GEM_WARN_ON(kref_read(&ggtt->vm.resv_ref) != 1);
1129 	dma_resv_fini(&ggtt->vm._resv);
1130 }
1131 
gen6_get_total_gtt_size(u16 snb_gmch_ctl)1132 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1133 {
1134 	snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1135 	snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1136 	return snb_gmch_ctl << 20;
1137 }
1138 
gen8_get_total_gtt_size(u16 bdw_gmch_ctl)1139 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1140 {
1141 	bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1142 	bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1143 	if (bdw_gmch_ctl)
1144 		bdw_gmch_ctl = 1 << bdw_gmch_ctl;
1145 
1146 #ifdef CONFIG_X86_32
1147 	/* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * I915_GTT_PAGE_SIZE */
1148 	if (bdw_gmch_ctl > 4)
1149 		bdw_gmch_ctl = 4;
1150 #endif
1151 
1152 	return bdw_gmch_ctl << 20;
1153 }
1154 
chv_get_total_gtt_size(u16 gmch_ctrl)1155 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
1156 {
1157 	gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
1158 	gmch_ctrl &= SNB_GMCH_GGMS_MASK;
1159 
1160 	if (gmch_ctrl)
1161 		return 1 << (20 + gmch_ctrl);
1162 
1163 	return 0;
1164 }
1165 
gen6_gttmmadr_size(struct drm_i915_private * i915)1166 static unsigned int gen6_gttmmadr_size(struct drm_i915_private *i915)
1167 {
1168 	/*
1169 	 * GEN6: GTTMMADR size is 4MB and GTTADR starts at 2MB offset
1170 	 * GEN8: GTTMMADR size is 16MB and GTTADR starts at 8MB offset
1171 	 */
1172 	GEM_BUG_ON(GRAPHICS_VER(i915) < 6);
1173 	return (GRAPHICS_VER(i915) < 8) ? SZ_4M : SZ_16M;
1174 }
1175 
gen6_gttadr_offset(struct drm_i915_private * i915)1176 static unsigned int gen6_gttadr_offset(struct drm_i915_private *i915)
1177 {
1178 	return gen6_gttmmadr_size(i915) / 2;
1179 }
1180 
1181 #ifdef __linux__
1182 
ggtt_probe_common(struct i915_ggtt * ggtt,u64 size)1183 static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
1184 {
1185 	struct drm_i915_private *i915 = ggtt->vm.i915;
1186 	struct intel_uncore *uncore = ggtt->vm.gt->uncore;
1187 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
1188 	phys_addr_t phys_addr;
1189 	u32 pte_flags;
1190 	int ret;
1191 
1192 	GEM_WARN_ON(pci_resource_len(pdev, GEN4_GTTMMADR_BAR) != gen6_gttmmadr_size(i915));
1193 
1194 	if (i915_direct_stolen_access(i915)) {
1195 		drm_dbg(&i915->drm, "Using direct GSM access\n");
1196 		phys_addr = intel_uncore_read64(uncore, GEN6_GSMBASE) & GEN11_BDSM_MASK;
1197 	} else {
1198 		phys_addr = pci_resource_start(pdev, GEN4_GTTMMADR_BAR) + gen6_gttadr_offset(i915);
1199 	}
1200 
1201 	if (needs_wc_ggtt_mapping(i915))
1202 		ggtt->gsm = ioremap_wc(phys_addr, size);
1203 	else
1204 		ggtt->gsm = ioremap(phys_addr, size);
1205 
1206 	if (!ggtt->gsm) {
1207 		drm_err(&i915->drm, "Failed to map the ggtt page table\n");
1208 		return -ENOMEM;
1209 	}
1210 
1211 	kref_init(&ggtt->vm.resv_ref);
1212 	ret = setup_scratch_page(&ggtt->vm);
1213 	if (ret) {
1214 		drm_err(&i915->drm, "Scratch setup failed\n");
1215 		/* iounmap will also get called at remove, but meh */
1216 		iounmap(ggtt->gsm);
1217 		return ret;
1218 	}
1219 
1220 	pte_flags = 0;
1221 	if (i915_gem_object_is_lmem(ggtt->vm.scratch[0]))
1222 		pte_flags |= PTE_LM;
1223 
1224 	ggtt->vm.scratch[0]->encode =
1225 		ggtt->vm.pte_encode(px_dma(ggtt->vm.scratch[0]),
1226 				    i915_gem_get_pat_index(i915,
1227 							   I915_CACHE_NONE),
1228 				    pte_flags);
1229 
1230 	return 0;
1231 }
1232 
1233 #else
1234 
ggtt_probe_common(struct i915_ggtt * ggtt,u64 size)1235 static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
1236 {
1237 	struct drm_i915_private *i915 = ggtt->vm.i915;
1238 	struct pci_dev *pdev = i915->drm.pdev;
1239 	phys_addr_t phys_addr;
1240 	bus_addr_t addr;
1241 	bus_size_t len;
1242 	pcireg_t type;
1243 	int flags;
1244 	u32 pte_flags;
1245 	int ret;
1246 
1247 	type = pci_mapreg_type(i915->pc, i915->tag, 0x10);
1248 	ret = -pci_mapreg_info(i915->pc, i915->tag, 0x10, type,
1249 	    &addr, &len, NULL);
1250 	if (ret)
1251 		return ret;
1252 
1253 	GEM_WARN_ON(len != gen6_gttmmadr_size(i915));
1254 	phys_addr = addr + gen6_gttadr_offset(i915);
1255 
1256 	if (needs_wc_ggtt_mapping(i915))
1257 		flags = BUS_SPACE_MAP_PREFETCHABLE;
1258 	else
1259 		flags = 0;
1260 
1261 	ret = -bus_space_map(i915->bst, phys_addr, size,
1262 	    flags | BUS_SPACE_MAP_LINEAR, &ggtt->gsm_bsh);
1263 	if (ret) {
1264 		drm_err(&i915->drm, "Failed to map the ggtt page table\n");
1265 		return ret;
1266 	}
1267 	ggtt->gsm = bus_space_vaddr(i915->bst, ggtt->gsm_bsh);
1268 	ggtt->gsm_size = size;
1269 	if (!ggtt->gsm) {
1270 		drm_err(&i915->drm, "Failed to map the ggtt page table\n");
1271 		return -ENOMEM;
1272 	}
1273 
1274 	kref_init(&ggtt->vm.resv_ref);
1275 	ret = setup_scratch_page(&ggtt->vm);
1276 	if (ret) {
1277 		drm_err(&i915->drm, "Scratch setup failed\n");
1278 		/* iounmap will also get called at remove, but meh */
1279 		bus_space_unmap(i915->bst, ggtt->gsm_bsh, size);
1280 		return ret;
1281 	}
1282 
1283 	pte_flags = 0;
1284 	if (i915_gem_object_is_lmem(ggtt->vm.scratch[0]))
1285 		pte_flags |= PTE_LM;
1286 
1287 	ggtt->vm.scratch[0]->encode =
1288 		ggtt->vm.pte_encode(px_dma(ggtt->vm.scratch[0]),
1289 				    i915_gem_get_pat_index(i915,
1290 							   I915_CACHE_NONE),
1291 				    pte_flags);
1292 
1293 	return 0;
1294 }
1295 
1296 #endif
1297 
gen6_gmch_remove(struct i915_address_space * vm)1298 static void gen6_gmch_remove(struct i915_address_space *vm)
1299 {
1300 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
1301 
1302 #ifdef __linux__
1303 	iounmap(ggtt->gsm);
1304 #else
1305 	bus_space_unmap(vm->i915->bst, ggtt->gsm_bsh, ggtt->gsm_size);
1306 #endif
1307 	free_scratch(vm);
1308 }
1309 
1310 #ifdef __linux__
pci_resource(struct pci_dev * pdev,int bar)1311 static struct resource pci_resource(struct pci_dev *pdev, int bar)
1312 {
1313 	return DEFINE_RES_MEM(pci_resource_start(pdev, bar),
1314 			      pci_resource_len(pdev, bar));
1315 }
1316 #endif
1317 
gen8_gmch_probe(struct i915_ggtt * ggtt)1318 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
1319 {
1320 	struct drm_i915_private *i915 = ggtt->vm.i915;
1321 	struct pci_dev *pdev = i915->drm.pdev;
1322 	unsigned int size;
1323 	u16 snb_gmch_ctl;
1324 
1325 	if (!HAS_LMEM(i915) && !HAS_LMEMBAR_SMEM_STOLEN(i915)) {
1326 #ifdef __linux__
1327 		if (!i915_pci_resource_valid(pdev, GEN4_GMADR_BAR))
1328 			return -ENXIO;
1329 
1330 		ggtt->gmadr = pci_resource(pdev, GEN4_GMADR_BAR);
1331 		ggtt->mappable_end = resource_size(&ggtt->gmadr);
1332 #else
1333 		bus_addr_t base;
1334 		bus_size_t sz;
1335 		pcireg_t type;
1336 		int err;
1337 
1338 		type = pci_mapreg_type(i915->pc, i915->tag, 0x18);
1339 		err = -pci_mapreg_info(i915->pc, i915->tag, 0x18, type,
1340 		    &base, &sz, NULL);
1341 		if (err)
1342 			return err;
1343 		ggtt->gmadr.start = base;
1344 		ggtt->mappable_end = sz;
1345 #endif
1346 	}
1347 
1348 	pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1349 	if (IS_CHERRYVIEW(i915))
1350 		size = chv_get_total_gtt_size(snb_gmch_ctl);
1351 	else
1352 		size = gen8_get_total_gtt_size(snb_gmch_ctl);
1353 
1354 	ggtt->vm.alloc_pt_dma = alloc_pt_dma;
1355 	ggtt->vm.alloc_scratch_dma = alloc_pt_dma;
1356 	ggtt->vm.lmem_pt_obj_flags = I915_BO_ALLOC_PM_EARLY;
1357 
1358 	ggtt->vm.total = (size / sizeof(gen8_pte_t)) * I915_GTT_PAGE_SIZE;
1359 	ggtt->vm.cleanup = gen6_gmch_remove;
1360 	ggtt->vm.insert_page = gen8_ggtt_insert_page;
1361 	ggtt->vm.clear_range = nop_clear_range;
1362 	ggtt->vm.scratch_range = gen8_ggtt_clear_range;
1363 
1364 	ggtt->vm.insert_entries = gen8_ggtt_insert_entries;
1365 
1366 	/*
1367 	 * Serialize GTT updates with aperture access on BXT if VT-d is on,
1368 	 * and always on CHV.
1369 	 */
1370 	if (intel_vm_no_concurrent_access_wa(i915)) {
1371 		ggtt->vm.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
1372 		ggtt->vm.insert_page    = bxt_vtd_ggtt_insert_page__BKL;
1373 
1374 		/*
1375 		 * Calling stop_machine() version of GGTT update function
1376 		 * at error capture/reset path will raise lockdep warning.
1377 		 * Allow calling gen8_ggtt_insert_* directly at reset path
1378 		 * which is safe from parallel GGTT updates.
1379 		 */
1380 		ggtt->vm.raw_insert_page = gen8_ggtt_insert_page;
1381 		ggtt->vm.raw_insert_entries = gen8_ggtt_insert_entries;
1382 
1383 		ggtt->vm.bind_async_flags =
1384 			I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
1385 	}
1386 
1387 	if (i915_ggtt_require_binder(i915)) {
1388 		ggtt->vm.scratch_range = gen8_ggtt_scratch_range_bind;
1389 		ggtt->vm.insert_page = gen8_ggtt_insert_page_bind;
1390 		ggtt->vm.insert_entries = gen8_ggtt_insert_entries_bind;
1391 		/*
1392 		 * On GPU is hung, we might bind VMAs for error capture.
1393 		 * Fallback to CPU GGTT updates in that case.
1394 		 */
1395 		ggtt->vm.raw_insert_page = gen8_ggtt_insert_page;
1396 	}
1397 
1398 	if (intel_uc_wants_guc_submission(&ggtt->vm.gt->uc))
1399 		ggtt->invalidate = guc_ggtt_invalidate;
1400 	else
1401 		ggtt->invalidate = gen8_ggtt_invalidate;
1402 
1403 	ggtt->vm.vma_ops.bind_vma    = intel_ggtt_bind_vma;
1404 	ggtt->vm.vma_ops.unbind_vma  = intel_ggtt_unbind_vma;
1405 
1406 	if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70))
1407 		ggtt->vm.pte_encode = mtl_ggtt_pte_encode;
1408 	else
1409 		ggtt->vm.pte_encode = gen8_ggtt_pte_encode;
1410 
1411 	return ggtt_probe_common(ggtt, size);
1412 }
1413 
1414 /*
1415  * For pre-gen8 platforms pat_index is the same as enum i915_cache_level,
1416  * so the switch-case statements in these PTE encode functions are still valid.
1417  * See translation table LEGACY_CACHELEVEL.
1418  */
snb_pte_encode(dma_addr_t addr,unsigned int pat_index,u32 flags)1419 static u64 snb_pte_encode(dma_addr_t addr,
1420 			  unsigned int pat_index,
1421 			  u32 flags)
1422 {
1423 	gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1424 
1425 	switch (pat_index) {
1426 	case I915_CACHE_L3_LLC:
1427 	case I915_CACHE_LLC:
1428 		pte |= GEN6_PTE_CACHE_LLC;
1429 		break;
1430 	case I915_CACHE_NONE:
1431 		pte |= GEN6_PTE_UNCACHED;
1432 		break;
1433 	default:
1434 		MISSING_CASE(pat_index);
1435 	}
1436 
1437 	return pte;
1438 }
1439 
ivb_pte_encode(dma_addr_t addr,unsigned int pat_index,u32 flags)1440 static u64 ivb_pte_encode(dma_addr_t addr,
1441 			  unsigned int pat_index,
1442 			  u32 flags)
1443 {
1444 	gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1445 
1446 	switch (pat_index) {
1447 	case I915_CACHE_L3_LLC:
1448 		pte |= GEN7_PTE_CACHE_L3_LLC;
1449 		break;
1450 	case I915_CACHE_LLC:
1451 		pte |= GEN6_PTE_CACHE_LLC;
1452 		break;
1453 	case I915_CACHE_NONE:
1454 		pte |= GEN6_PTE_UNCACHED;
1455 		break;
1456 	default:
1457 		MISSING_CASE(pat_index);
1458 	}
1459 
1460 	return pte;
1461 }
1462 
byt_pte_encode(dma_addr_t addr,unsigned int pat_index,u32 flags)1463 static u64 byt_pte_encode(dma_addr_t addr,
1464 			  unsigned int pat_index,
1465 			  u32 flags)
1466 {
1467 	gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1468 
1469 	if (!(flags & PTE_READ_ONLY))
1470 		pte |= BYT_PTE_WRITEABLE;
1471 
1472 	if (pat_index != I915_CACHE_NONE)
1473 		pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
1474 
1475 	return pte;
1476 }
1477 
hsw_pte_encode(dma_addr_t addr,unsigned int pat_index,u32 flags)1478 static u64 hsw_pte_encode(dma_addr_t addr,
1479 			  unsigned int pat_index,
1480 			  u32 flags)
1481 {
1482 	gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1483 
1484 	if (pat_index != I915_CACHE_NONE)
1485 		pte |= HSW_WB_LLC_AGE3;
1486 
1487 	return pte;
1488 }
1489 
iris_pte_encode(dma_addr_t addr,unsigned int pat_index,u32 flags)1490 static u64 iris_pte_encode(dma_addr_t addr,
1491 			   unsigned int pat_index,
1492 			   u32 flags)
1493 {
1494 	gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1495 
1496 	switch (pat_index) {
1497 	case I915_CACHE_NONE:
1498 		break;
1499 	case I915_CACHE_WT:
1500 		pte |= HSW_WT_ELLC_LLC_AGE3;
1501 		break;
1502 	default:
1503 		pte |= HSW_WB_ELLC_LLC_AGE3;
1504 		break;
1505 	}
1506 
1507 	return pte;
1508 }
1509 
gen6_gmch_probe(struct i915_ggtt * ggtt)1510 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
1511 {
1512 	struct drm_i915_private *i915 = ggtt->vm.i915;
1513 	struct pci_dev *pdev = i915->drm.pdev;
1514 	unsigned int size;
1515 	u16 snb_gmch_ctl;
1516 
1517 #ifdef __linux__
1518 	if (!i915_pci_resource_valid(pdev, GEN4_GMADR_BAR))
1519 		return -ENXIO;
1520 
1521 	ggtt->gmadr = pci_resource(pdev, GEN4_GMADR_BAR);
1522 	ggtt->mappable_end = resource_size(&ggtt->gmadr);
1523 #else
1524 	bus_addr_t base;
1525 	bus_size_t sz;
1526 	pcireg_t type;
1527 	int err;
1528 
1529 	type = pci_mapreg_type(i915->pc, i915->tag, 0x18);
1530 	err = -pci_mapreg_info(i915->pc, i915->tag, 0x18, type,
1531 	    &base, &sz, NULL);
1532 	if (err)
1533 		return err;
1534 	ggtt->gmadr.start = base;
1535 	ggtt->mappable_end = sz;
1536 #endif
1537 
1538 	/*
1539 	 * 64/512MB is the current min/max we actually know of, but this is
1540 	 * just a coarse sanity check.
1541 	 */
1542 	if (ggtt->mappable_end < (64 << 20) ||
1543 	    ggtt->mappable_end > (512 << 20)) {
1544 		drm_err(&i915->drm, "Unknown GMADR size (%pa)\n",
1545 			&ggtt->mappable_end);
1546 		return -ENXIO;
1547 	}
1548 
1549 	pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1550 
1551 	size = gen6_get_total_gtt_size(snb_gmch_ctl);
1552 	ggtt->vm.total = (size / sizeof(gen6_pte_t)) * I915_GTT_PAGE_SIZE;
1553 
1554 	ggtt->vm.alloc_pt_dma = alloc_pt_dma;
1555 	ggtt->vm.alloc_scratch_dma = alloc_pt_dma;
1556 
1557 	ggtt->vm.clear_range = nop_clear_range;
1558 	if (!HAS_FULL_PPGTT(i915))
1559 		ggtt->vm.clear_range = gen6_ggtt_clear_range;
1560 	ggtt->vm.scratch_range = gen6_ggtt_clear_range;
1561 	ggtt->vm.insert_page = gen6_ggtt_insert_page;
1562 	ggtt->vm.insert_entries = gen6_ggtt_insert_entries;
1563 	ggtt->vm.cleanup = gen6_gmch_remove;
1564 
1565 	ggtt->invalidate = gen6_ggtt_invalidate;
1566 
1567 	if (HAS_EDRAM(i915))
1568 		ggtt->vm.pte_encode = iris_pte_encode;
1569 	else if (IS_HASWELL(i915))
1570 		ggtt->vm.pte_encode = hsw_pte_encode;
1571 	else if (IS_VALLEYVIEW(i915))
1572 		ggtt->vm.pte_encode = byt_pte_encode;
1573 	else if (GRAPHICS_VER(i915) >= 7)
1574 		ggtt->vm.pte_encode = ivb_pte_encode;
1575 	else
1576 		ggtt->vm.pte_encode = snb_pte_encode;
1577 
1578 	ggtt->vm.vma_ops.bind_vma    = intel_ggtt_bind_vma;
1579 	ggtt->vm.vma_ops.unbind_vma  = intel_ggtt_unbind_vma;
1580 
1581 	return ggtt_probe_common(ggtt, size);
1582 }
1583 
ggtt_probe_hw(struct i915_ggtt * ggtt,struct intel_gt * gt)1584 static int ggtt_probe_hw(struct i915_ggtt *ggtt, struct intel_gt *gt)
1585 {
1586 	struct drm_i915_private *i915 = gt->i915;
1587 	int ret;
1588 
1589 	ggtt->vm.gt = gt;
1590 	ggtt->vm.i915 = i915;
1591 #ifdef notyet
1592 	ggtt->vm.dma = i915->drm.dev;
1593 #endif
1594 	dma_resv_init(&ggtt->vm._resv);
1595 
1596 	if (GRAPHICS_VER(i915) >= 8)
1597 		ret = gen8_gmch_probe(ggtt);
1598 	else if (GRAPHICS_VER(i915) >= 6)
1599 		ret = gen6_gmch_probe(ggtt);
1600 	else
1601 		ret = intel_ggtt_gmch_probe(ggtt);
1602 
1603 	if (ret) {
1604 		dma_resv_fini(&ggtt->vm._resv);
1605 		return ret;
1606 	}
1607 
1608 	if ((ggtt->vm.total - 1) >> 32) {
1609 		drm_err(&i915->drm,
1610 			"We never expected a Global GTT with more than 32bits"
1611 			" of address space! Found %lldM!\n",
1612 			ggtt->vm.total >> 20);
1613 		ggtt->vm.total = 1ULL << 32;
1614 		ggtt->mappable_end =
1615 			min_t(u64, ggtt->mappable_end, ggtt->vm.total);
1616 	}
1617 
1618 	if (ggtt->mappable_end > ggtt->vm.total) {
1619 		drm_err(&i915->drm,
1620 			"mappable aperture extends past end of GGTT,"
1621 			" aperture=%pa, total=%llx\n",
1622 			&ggtt->mappable_end, ggtt->vm.total);
1623 		ggtt->mappable_end = ggtt->vm.total;
1624 	}
1625 
1626 	/* GMADR is the PCI mmio aperture into the global GTT. */
1627 	drm_dbg(&i915->drm, "GGTT size = %lluM\n", ggtt->vm.total >> 20);
1628 	drm_dbg(&i915->drm, "GMADR size = %lluM\n",
1629 		(u64)ggtt->mappable_end >> 20);
1630 	drm_dbg(&i915->drm, "DSM size = %lluM\n",
1631 		(u64)resource_size(&intel_graphics_stolen_res) >> 20);
1632 
1633 	return 0;
1634 }
1635 
1636 /**
1637  * i915_ggtt_probe_hw - Probe GGTT hardware location
1638  * @i915: i915 device
1639  */
i915_ggtt_probe_hw(struct drm_i915_private * i915)1640 int i915_ggtt_probe_hw(struct drm_i915_private *i915)
1641 {
1642 	struct intel_gt *gt;
1643 	int ret, i;
1644 
1645 	for_each_gt(gt, i915, i) {
1646 		ret = intel_gt_assign_ggtt(gt);
1647 		if (ret)
1648 			return ret;
1649 	}
1650 
1651 	ret = ggtt_probe_hw(to_gt(i915)->ggtt, to_gt(i915));
1652 	if (ret)
1653 		return ret;
1654 
1655 	if (i915_vtd_active(i915))
1656 		drm_info(&i915->drm, "VT-d active for gfx access\n");
1657 
1658 	return 0;
1659 }
1660 
i915_ggtt_create(struct drm_i915_private * i915)1661 struct i915_ggtt *i915_ggtt_create(struct drm_i915_private *i915)
1662 {
1663 	struct i915_ggtt *ggtt;
1664 
1665 	ggtt = drmm_kzalloc(&i915->drm, sizeof(*ggtt), GFP_KERNEL);
1666 	if (!ggtt)
1667 		return ERR_PTR(-ENOMEM);
1668 
1669 	INIT_LIST_HEAD(&ggtt->gt_list);
1670 
1671 	return ggtt;
1672 }
1673 
i915_ggtt_enable_hw(struct drm_i915_private * i915)1674 int i915_ggtt_enable_hw(struct drm_i915_private *i915)
1675 {
1676 	if (GRAPHICS_VER(i915) < 6)
1677 		return intel_ggtt_gmch_enable_hw(i915);
1678 
1679 	return 0;
1680 }
1681 
1682 /**
1683  * i915_ggtt_resume_vm - Restore the memory mappings for a GGTT or DPT VM
1684  * @vm: The VM to restore the mappings for
1685  *
1686  * Restore the memory mappings for all objects mapped to HW via the GGTT or a
1687  * DPT page table.
1688  *
1689  * Returns %true if restoring the mapping for any object that was in a write
1690  * domain before suspend.
1691  */
i915_ggtt_resume_vm(struct i915_address_space * vm)1692 bool i915_ggtt_resume_vm(struct i915_address_space *vm)
1693 {
1694 	struct i915_vma *vma;
1695 	bool write_domain_objs = false;
1696 
1697 	drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
1698 
1699 	/* First fill our portion of the GTT with scratch pages */
1700 	vm->clear_range(vm, 0, vm->total);
1701 
1702 	/* clflush objects bound into the GGTT and rebind them. */
1703 	list_for_each_entry(vma, &vm->bound_list, vm_link) {
1704 		struct drm_i915_gem_object *obj = vma->obj;
1705 		unsigned int was_bound =
1706 			atomic_read(&vma->flags) & I915_VMA_BIND_MASK;
1707 
1708 		GEM_BUG_ON(!was_bound);
1709 
1710 		/*
1711 		 * Clear the bound flags of the vma resource to allow
1712 		 * ptes to be repopulated.
1713 		 */
1714 		vma->resource->bound_flags = 0;
1715 		vma->ops->bind_vma(vm, NULL, vma->resource,
1716 				   obj ? obj->pat_index :
1717 					 i915_gem_get_pat_index(vm->i915,
1718 								I915_CACHE_NONE),
1719 				   was_bound);
1720 
1721 		if (obj) { /* only used during resume => exclusive access */
1722 			write_domain_objs |= fetch_and_zero(&obj->write_domain);
1723 			obj->read_domains |= I915_GEM_DOMAIN_GTT;
1724 		}
1725 	}
1726 
1727 	return write_domain_objs;
1728 }
1729 
i915_ggtt_resume(struct i915_ggtt * ggtt)1730 void i915_ggtt_resume(struct i915_ggtt *ggtt)
1731 {
1732 	struct intel_gt *gt;
1733 	bool flush;
1734 
1735 	list_for_each_entry(gt, &ggtt->gt_list, ggtt_link)
1736 		intel_gt_check_and_clear_faults(gt);
1737 
1738 	flush = i915_ggtt_resume_vm(&ggtt->vm);
1739 
1740 	if (drm_mm_node_allocated(&ggtt->error_capture))
1741 		ggtt->vm.scratch_range(&ggtt->vm, ggtt->error_capture.start,
1742 				       ggtt->error_capture.size);
1743 
1744 	list_for_each_entry(gt, &ggtt->gt_list, ggtt_link)
1745 		intel_uc_resume_mappings(&gt->uc);
1746 
1747 	ggtt->invalidate(ggtt);
1748 
1749 	if (flush)
1750 		wbinvd_on_all_cpus();
1751 
1752 	intel_ggtt_restore_fences(ggtt);
1753 }
1754