• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2020 Intel Corporation
4  */
5 
6 #include <linux/log2.h>
7 
8 #include "gen6_ppgtt.h"
9 #include "i915_scatterlist.h"
10 #include "i915_trace.h"
11 #include "i915_vgpu.h"
12 #include "intel_gt.h"
13 
14 /* Write pde (index) from the page directory @pd to the page table @pt */
gen6_write_pde(const struct gen6_ppgtt * ppgtt,const unsigned int pde,const struct i915_page_table * pt)15 static void gen6_write_pde(const struct gen6_ppgtt *ppgtt,
16 			   const unsigned int pde,
17 			   const struct i915_page_table *pt)
18 {
19 	dma_addr_t addr = pt ? px_dma(pt) : px_dma(ppgtt->base.vm.scratch[1]);
20 
21 	/* Caller needs to make sure the write completes if necessary */
22 	iowrite32(GEN6_PDE_ADDR_ENCODE(addr) | GEN6_PDE_VALID,
23 		  ppgtt->pd_addr + pde);
24 }
25 
gen7_ppgtt_enable(struct intel_gt * gt)26 void gen7_ppgtt_enable(struct intel_gt *gt)
27 {
28 	struct drm_i915_private *i915 = gt->i915;
29 	struct intel_uncore *uncore = gt->uncore;
30 	u32 ecochk;
31 
32 	intel_uncore_rmw(uncore, GAC_ECO_BITS, 0, ECOBITS_PPGTT_CACHE64B);
33 
34 	ecochk = intel_uncore_read(uncore, GAM_ECOCHK);
35 	if (IS_HASWELL(i915)) {
36 		ecochk |= ECOCHK_PPGTT_WB_HSW;
37 	} else {
38 		ecochk |= ECOCHK_PPGTT_LLC_IVB;
39 		ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
40 	}
41 	intel_uncore_write(uncore, GAM_ECOCHK, ecochk);
42 }
43 
gen6_ppgtt_enable(struct intel_gt * gt)44 void gen6_ppgtt_enable(struct intel_gt *gt)
45 {
46 	struct intel_uncore *uncore = gt->uncore;
47 
48 	intel_uncore_rmw(uncore,
49 			 GAC_ECO_BITS,
50 			 0,
51 			 ECOBITS_SNB_BIT | ECOBITS_PPGTT_CACHE64B);
52 
53 	intel_uncore_rmw(uncore,
54 			 GAB_CTL,
55 			 0,
56 			 GAB_CTL_CONT_AFTER_PAGEFAULT);
57 
58 	intel_uncore_rmw(uncore,
59 			 GAM_ECOCHK,
60 			 0,
61 			 ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
62 
63 	if (HAS_PPGTT(uncore->i915)) /* may be disabled for VT-d */
64 		intel_uncore_write(uncore,
65 				   GFX_MODE,
66 				   _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
67 }
68 
69 /* PPGTT support for Sandybdrige/Gen6 and later */
gen6_ppgtt_clear_range(struct i915_address_space * vm,u64 start,u64 length)70 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
71 				   u64 start, u64 length)
72 {
73 	struct gen6_ppgtt * const ppgtt = to_gen6_ppgtt(i915_vm_to_ppgtt(vm));
74 	const unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
75 	const gen6_pte_t scratch_pte = vm->scratch[0]->encode;
76 	unsigned int pde = first_entry / GEN6_PTES;
77 	unsigned int pte = first_entry % GEN6_PTES;
78 	unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
79 
80 	while (num_entries) {
81 		struct i915_page_table * const pt =
82 			i915_pt_entry(ppgtt->base.pd, pde++);
83 		const unsigned int count = min(num_entries, GEN6_PTES - pte);
84 		gen6_pte_t *vaddr;
85 
86 		num_entries -= count;
87 
88 		GEM_BUG_ON(count > atomic_read(&pt->used));
89 		if (!atomic_sub_return(count, &pt->used))
90 			ppgtt->scan_for_unused_pt = true;
91 
92 		/*
93 		 * Note that the hw doesn't support removing PDE on the fly
94 		 * (they are cached inside the context with no means to
95 		 * invalidate the cache), so we can only reset the PTE
96 		 * entries back to scratch.
97 		 */
98 
99 		vaddr = px_vaddr(pt);
100 		memset32(vaddr + pte, scratch_pte, count);
101 
102 		pte = 0;
103 	}
104 }
105 
gen6_ppgtt_insert_entries(struct i915_address_space * vm,struct i915_vma * vma,enum i915_cache_level cache_level,u32 flags)106 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
107 				      struct i915_vma *vma,
108 				      enum i915_cache_level cache_level,
109 				      u32 flags)
110 {
111 	struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
112 	struct i915_page_directory * const pd = ppgtt->pd;
113 	unsigned int first_entry = vma->node.start / I915_GTT_PAGE_SIZE;
114 	unsigned int act_pt = first_entry / GEN6_PTES;
115 	unsigned int act_pte = first_entry % GEN6_PTES;
116 	const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
117 	struct sgt_dma iter = sgt_dma(vma);
118 	gen6_pte_t *vaddr;
119 
120 	GEM_BUG_ON(!pd->entry[act_pt]);
121 
122 	vaddr = px_vaddr(i915_pt_entry(pd, act_pt));
123 	do {
124 		GEM_BUG_ON(sg_dma_len(iter.sg) < I915_GTT_PAGE_SIZE);
125 		vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
126 
127 		iter.dma += I915_GTT_PAGE_SIZE;
128 		if (iter.dma == iter.max) {
129 			iter.sg = __sg_next(iter.sg);
130 			if (!iter.sg || sg_dma_len(iter.sg) == 0)
131 				break;
132 
133 			iter.dma = sg_dma_address(iter.sg);
134 			iter.max = iter.dma + sg_dma_len(iter.sg);
135 		}
136 
137 		if (++act_pte == GEN6_PTES) {
138 			vaddr = px_vaddr(i915_pt_entry(pd, ++act_pt));
139 			act_pte = 0;
140 		}
141 	} while (1);
142 
143 	vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
144 }
145 
gen6_flush_pd(struct gen6_ppgtt * ppgtt,u64 start,u64 end)146 static void gen6_flush_pd(struct gen6_ppgtt *ppgtt, u64 start, u64 end)
147 {
148 	struct i915_page_directory * const pd = ppgtt->base.pd;
149 	struct i915_page_table *pt;
150 	unsigned int pde;
151 
152 	start = round_down(start, SZ_64K);
153 	end = round_up(end, SZ_64K) - start;
154 
155 	mutex_lock(&ppgtt->flush);
156 
157 	gen6_for_each_pde(pt, pd, start, end, pde)
158 		gen6_write_pde(ppgtt, pde, pt);
159 
160 	mb();
161 	ioread32(ppgtt->pd_addr + pde - 1);
162 	gen6_ggtt_invalidate(ppgtt->base.vm.gt->ggtt);
163 	mb();
164 
165 	mutex_unlock(&ppgtt->flush);
166 }
167 
gen6_alloc_va_range(struct i915_address_space * vm,struct i915_vm_pt_stash * stash,u64 start,u64 length)168 static void gen6_alloc_va_range(struct i915_address_space *vm,
169 				struct i915_vm_pt_stash *stash,
170 				u64 start, u64 length)
171 {
172 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(i915_vm_to_ppgtt(vm));
173 	struct i915_page_directory * const pd = ppgtt->base.pd;
174 	struct i915_page_table *pt;
175 	bool flush = false;
176 	u64 from = start;
177 	unsigned int pde;
178 
179 	spin_lock(&pd->lock);
180 	gen6_for_each_pde(pt, pd, start, length, pde) {
181 		const unsigned int count = gen6_pte_count(start, length);
182 
183 		if (!pt) {
184 			spin_unlock(&pd->lock);
185 
186 			pt = stash->pt[0];
187 			__i915_gem_object_pin_pages(pt->base);
188 			i915_gem_object_make_unshrinkable(pt->base);
189 
190 			fill32_px(pt, vm->scratch[0]->encode);
191 
192 			spin_lock(&pd->lock);
193 			if (!pd->entry[pde]) {
194 				stash->pt[0] = pt->stash;
195 				atomic_set(&pt->used, 0);
196 				pd->entry[pde] = pt;
197 			} else {
198 				pt = pd->entry[pde];
199 			}
200 
201 			flush = true;
202 		}
203 
204 		atomic_add(count, &pt->used);
205 	}
206 	spin_unlock(&pd->lock);
207 
208 	if (flush && i915_vma_is_bound(ppgtt->vma, I915_VMA_GLOBAL_BIND)) {
209 		intel_wakeref_t wakeref;
210 
211 		with_intel_runtime_pm(&vm->i915->runtime_pm, wakeref)
212 			gen6_flush_pd(ppgtt, from, start);
213 	}
214 }
215 
gen6_ppgtt_init_scratch(struct gen6_ppgtt * ppgtt)216 static int gen6_ppgtt_init_scratch(struct gen6_ppgtt *ppgtt)
217 {
218 	struct i915_address_space * const vm = &ppgtt->base.vm;
219 	int ret;
220 
221 	ret = setup_scratch_page(vm);
222 	if (ret)
223 		return ret;
224 
225 	vm->scratch[0]->encode =
226 		vm->pte_encode(px_dma(vm->scratch[0]),
227 			       I915_CACHE_NONE, PTE_READ_ONLY);
228 
229 	vm->scratch[1] = vm->alloc_pt_dma(vm, I915_GTT_PAGE_SIZE_4K);
230 	if (IS_ERR(vm->scratch[1])) {
231 		ret = PTR_ERR(vm->scratch[1]);
232 		goto err_scratch0;
233 	}
234 
235 	ret = map_pt_dma(vm, vm->scratch[1]);
236 	if (ret)
237 		goto err_scratch1;
238 
239 	fill32_px(vm->scratch[1], vm->scratch[0]->encode);
240 
241 	return 0;
242 
243 err_scratch1:
244 	i915_gem_object_put(vm->scratch[1]);
245 err_scratch0:
246 	i915_gem_object_put(vm->scratch[0]);
247 	vm->scratch[0] = NULL;
248 	return ret;
249 }
250 
gen6_ppgtt_free_pd(struct gen6_ppgtt * ppgtt)251 static void gen6_ppgtt_free_pd(struct gen6_ppgtt *ppgtt)
252 {
253 	struct i915_page_directory * const pd = ppgtt->base.pd;
254 	struct i915_page_table *pt;
255 	u32 pde;
256 
257 	gen6_for_all_pdes(pt, pd, pde)
258 		if (pt)
259 			free_pt(&ppgtt->base.vm, pt);
260 }
261 
gen6_ppgtt_cleanup(struct i915_address_space * vm)262 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
263 {
264 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(i915_vm_to_ppgtt(vm));
265 
266 	gen6_ppgtt_free_pd(ppgtt);
267 	free_scratch(vm);
268 
269 	if (ppgtt->base.pd)
270 		free_pd(&ppgtt->base.vm, ppgtt->base.pd);
271 
272 	mutex_destroy(&ppgtt->flush);
273 }
274 
pd_vma_set_pages(struct i915_vma * vma)275 static int pd_vma_set_pages(struct i915_vma *vma)
276 {
277 	vma->pages = ERR_PTR(-ENODEV);
278 	return 0;
279 }
280 
pd_vma_clear_pages(struct i915_vma * vma)281 static void pd_vma_clear_pages(struct i915_vma *vma)
282 {
283 	GEM_BUG_ON(!vma->pages);
284 
285 	vma->pages = NULL;
286 }
287 
pd_vma_bind(struct i915_address_space * vm,struct i915_vm_pt_stash * stash,struct i915_vma * vma,enum i915_cache_level cache_level,u32 unused)288 static void pd_vma_bind(struct i915_address_space *vm,
289 			struct i915_vm_pt_stash *stash,
290 			struct i915_vma *vma,
291 			enum i915_cache_level cache_level,
292 			u32 unused)
293 {
294 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
295 	struct gen6_ppgtt *ppgtt = vma->private;
296 	u32 ggtt_offset = i915_ggtt_offset(vma) / I915_GTT_PAGE_SIZE;
297 
298 	ppgtt->pp_dir = ggtt_offset * sizeof(gen6_pte_t) << 10;
299 	ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm + ggtt_offset;
300 
301 	gen6_flush_pd(ppgtt, 0, ppgtt->base.vm.total);
302 }
303 
pd_vma_unbind(struct i915_address_space * vm,struct i915_vma * vma)304 static void pd_vma_unbind(struct i915_address_space *vm, struct i915_vma *vma)
305 {
306 	struct gen6_ppgtt *ppgtt = vma->private;
307 	struct i915_page_directory * const pd = ppgtt->base.pd;
308 	struct i915_page_table *pt;
309 	unsigned int pde;
310 
311 	if (!ppgtt->scan_for_unused_pt)
312 		return;
313 
314 	/* Free all no longer used page tables */
315 	gen6_for_all_pdes(pt, ppgtt->base.pd, pde) {
316 		if (!pt || atomic_read(&pt->used))
317 			continue;
318 
319 		free_pt(&ppgtt->base.vm, pt);
320 		pd->entry[pde] = NULL;
321 	}
322 
323 	ppgtt->scan_for_unused_pt = false;
324 }
325 
326 static const struct i915_vma_ops pd_vma_ops = {
327 	.set_pages = pd_vma_set_pages,
328 	.clear_pages = pd_vma_clear_pages,
329 	.bind_vma = pd_vma_bind,
330 	.unbind_vma = pd_vma_unbind,
331 };
332 
gen6_ppgtt_pin(struct i915_ppgtt * base,struct i915_gem_ww_ctx * ww)333 int gen6_ppgtt_pin(struct i915_ppgtt *base, struct i915_gem_ww_ctx *ww)
334 {
335 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(base);
336 	int err;
337 
338 	GEM_BUG_ON(!atomic_read(&ppgtt->base.vm.open));
339 
340 	/*
341 	 * Workaround the limited maximum vma->pin_count and the aliasing_ppgtt
342 	 * which will be pinned into every active context.
343 	 * (When vma->pin_count becomes atomic, I expect we will naturally
344 	 * need a larger, unpacked, type and kill this redundancy.)
345 	 */
346 	if (atomic_add_unless(&ppgtt->pin_count, 1, 0))
347 		return 0;
348 
349 	/* grab the ppgtt resv to pin the object */
350 	err = i915_vm_lock_objects(&ppgtt->base.vm, ww);
351 	if (err)
352 		return err;
353 
354 	/*
355 	 * PPGTT PDEs reside in the GGTT and consists of 512 entries. The
356 	 * allocator works in address space sizes, so it's multiplied by page
357 	 * size. We allocate at the top of the GTT to avoid fragmentation.
358 	 */
359 	if (!atomic_read(&ppgtt->pin_count)) {
360 		err = i915_ggtt_pin(ppgtt->vma, ww, GEN6_PD_ALIGN, PIN_HIGH);
361 
362 		GEM_BUG_ON(ppgtt->vma->fence);
363 		clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(ppgtt->vma));
364 	}
365 	if (!err)
366 		atomic_inc(&ppgtt->pin_count);
367 
368 	return err;
369 }
370 
pd_dummy_obj_get_pages(struct drm_i915_gem_object * obj)371 static int pd_dummy_obj_get_pages(struct drm_i915_gem_object *obj)
372 {
373 	obj->mm.pages = ZERO_SIZE_PTR;
374 	return 0;
375 }
376 
pd_dummy_obj_put_pages(struct drm_i915_gem_object * obj,struct sg_table * pages)377 static void pd_dummy_obj_put_pages(struct drm_i915_gem_object *obj,
378 				   struct sg_table *pages)
379 {
380 }
381 
382 static const struct drm_i915_gem_object_ops pd_dummy_obj_ops = {
383 	.name = "pd_dummy_obj",
384 	.get_pages = pd_dummy_obj_get_pages,
385 	.put_pages = pd_dummy_obj_put_pages,
386 };
387 
388 static struct i915_page_directory *
gen6_alloc_top_pd(struct gen6_ppgtt * ppgtt)389 gen6_alloc_top_pd(struct gen6_ppgtt *ppgtt)
390 {
391 	struct i915_ggtt * const ggtt = ppgtt->base.vm.gt->ggtt;
392 	struct i915_page_directory *pd;
393 	int err;
394 
395 	pd = __alloc_pd(I915_PDES);
396 	if (unlikely(!pd))
397 		return ERR_PTR(-ENOMEM);
398 
399 	pd->pt.base = __i915_gem_object_create_internal(ppgtt->base.vm.gt->i915,
400 							&pd_dummy_obj_ops,
401 							I915_PDES * SZ_4K);
402 	if (IS_ERR(pd->pt.base)) {
403 		err = PTR_ERR(pd->pt.base);
404 		pd->pt.base = NULL;
405 		goto err_pd;
406 	}
407 
408 	pd->pt.base->base.resv = i915_vm_resv_get(&ppgtt->base.vm);
409 	pd->pt.base->shares_resv_from = &ppgtt->base.vm;
410 
411 	ppgtt->vma = i915_vma_instance(pd->pt.base, &ggtt->vm, NULL);
412 	if (IS_ERR(ppgtt->vma)) {
413 		err = PTR_ERR(ppgtt->vma);
414 		ppgtt->vma = NULL;
415 		goto err_pd;
416 	}
417 
418 	/* The dummy object we create is special, override ops.. */
419 	ppgtt->vma->ops = &pd_vma_ops;
420 	ppgtt->vma->private = ppgtt;
421 	return pd;
422 
423 err_pd:
424 	free_pd(&ppgtt->base.vm, pd);
425 	return ERR_PTR(err);
426 }
427 
gen6_ppgtt_unpin(struct i915_ppgtt * base)428 void gen6_ppgtt_unpin(struct i915_ppgtt *base)
429 {
430 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(base);
431 
432 	GEM_BUG_ON(!atomic_read(&ppgtt->pin_count));
433 	if (atomic_dec_and_test(&ppgtt->pin_count))
434 		i915_vma_unpin(ppgtt->vma);
435 }
436 
gen6_ppgtt_unpin_all(struct i915_ppgtt * base)437 void gen6_ppgtt_unpin_all(struct i915_ppgtt *base)
438 {
439 	struct gen6_ppgtt *ppgtt = to_gen6_ppgtt(base);
440 
441 	if (!atomic_read(&ppgtt->pin_count))
442 		return;
443 
444 	i915_vma_unpin(ppgtt->vma);
445 	atomic_set(&ppgtt->pin_count, 0);
446 }
447 
gen6_ppgtt_create(struct intel_gt * gt)448 struct i915_ppgtt *gen6_ppgtt_create(struct intel_gt *gt)
449 {
450 	struct i915_ggtt * const ggtt = gt->ggtt;
451 	struct gen6_ppgtt *ppgtt;
452 	int err;
453 
454 	ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
455 	if (!ppgtt)
456 		return ERR_PTR(-ENOMEM);
457 
458 	mutex_init(&ppgtt->flush);
459 
460 	ppgtt_init(&ppgtt->base, gt);
461 	ppgtt->base.vm.pd_shift = ilog2(SZ_4K * SZ_4K / sizeof(gen6_pte_t));
462 	ppgtt->base.vm.top = 1;
463 
464 	ppgtt->base.vm.bind_async_flags = I915_VMA_LOCAL_BIND;
465 	ppgtt->base.vm.allocate_va_range = gen6_alloc_va_range;
466 	ppgtt->base.vm.clear_range = gen6_ppgtt_clear_range;
467 	ppgtt->base.vm.insert_entries = gen6_ppgtt_insert_entries;
468 	ppgtt->base.vm.cleanup = gen6_ppgtt_cleanup;
469 
470 	ppgtt->base.vm.alloc_pt_dma = alloc_pt_dma;
471 	ppgtt->base.vm.pte_encode = ggtt->vm.pte_encode;
472 
473 	err = gen6_ppgtt_init_scratch(ppgtt);
474 	if (err)
475 		goto err_put;
476 
477 	ppgtt->base.pd = gen6_alloc_top_pd(ppgtt);
478 	if (IS_ERR(ppgtt->base.pd)) {
479 		err = PTR_ERR(ppgtt->base.pd);
480 		goto err_put;
481 	}
482 
483 	return &ppgtt->base;
484 
485 err_put:
486 	i915_vm_put(&ppgtt->base.vm);
487 	return ERR_PTR(err);
488 }
489