intel_ppgtt.c 6.6 KB
Newer Older
1 2 3 4 5 6 7
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2020 Intel Corporation
 */

#include <linux/slab.h>

8 9
#include "gem/i915_gem_lmem.h"

10 11 12 13 14
#include "i915_trace.h"
#include "intel_gtt.h"
#include "gen6_ppgtt.h"
#include "gen8_ppgtt.h"

15
struct i915_page_table *alloc_pt(struct i915_address_space *vm, int sz)
16 17 18 19 20 21 22
{
	struct i915_page_table *pt;

	pt = kmalloc(sizeof(*pt), I915_GFP_ALLOW_FAIL);
	if (unlikely(!pt))
		return ERR_PTR(-ENOMEM);

23
	pt->base = vm->alloc_pt_dma(vm, sz);
24
	if (IS_ERR(pt->base)) {
25 26 27 28
		kfree(pt);
		return ERR_PTR(-ENOMEM);
	}

29
	pt->is_compact = false;
30 31 32 33
	atomic_set(&pt->used, 0);
	return pt;
}

34
struct i915_page_directory *__alloc_pd(int count)
35 36 37
{
	struct i915_page_directory *pd;

38
	pd = kzalloc(sizeof(*pd), I915_GFP_ALLOW_FAIL);
39 40 41
	if (unlikely(!pd))
		return NULL;

42 43 44 45 46 47
	pd->entry = kcalloc(count, sizeof(*pd->entry), I915_GFP_ALLOW_FAIL);
	if (unlikely(!pd->entry)) {
		kfree(pd);
		return NULL;
	}

48 49 50 51 52 53 54 55
	spin_lock_init(&pd->lock);
	return pd;
}

struct i915_page_directory *alloc_pd(struct i915_address_space *vm)
{
	struct i915_page_directory *pd;

56
	pd = __alloc_pd(I915_PDES);
57 58 59
	if (unlikely(!pd))
		return ERR_PTR(-ENOMEM);

60 61
	pd->pt.base = vm->alloc_pt_dma(vm, I915_GTT_PAGE_SIZE_4K);
	if (IS_ERR(pd->pt.base)) {
62
		kfree(pd->entry);
63 64 65 66 67 68 69
		kfree(pd);
		return ERR_PTR(-ENOMEM);
	}

	return pd;
}

70
void free_px(struct i915_address_space *vm, struct i915_page_table *pt, int lvl)
71
{
72 73 74 75 76 77 78 79 80 81 82
	BUILD_BUG_ON(offsetof(struct i915_page_directory, pt));

	if (lvl) {
		struct i915_page_directory *pd =
			container_of(pt, typeof(*pd), pt);
		kfree(pd->entry);
	}

	if (pt->base)
		i915_gem_object_put(pt->base);

83
	kfree(pt);
84 85
}

C
Chris Wilson 已提交
86
static void
87
write_dma_entry(struct drm_i915_gem_object * const pdma,
88 89 90
		const unsigned short idx,
		const u64 encoded_entry)
{
M
Matthew Auld 已提交
91
	u64 * const vaddr = __px_vaddr(pdma);
92 93

	vaddr[idx] = encoded_entry;
94
	drm_clflush_virt_range(&vaddr[idx], sizeof(u64));
95 96 97 98 99
}

void
__set_pd_entry(struct i915_page_directory * const pd,
	       const unsigned short idx,
100
	       struct i915_page_table * const to,
101 102 103
	       u64 (*encode)(const dma_addr_t, const enum i915_cache_level))
{
	/* Each thread pre-pins the pd, and we may have a thread per pde. */
104
	GEM_BUG_ON(atomic_read(px_used(pd)) > NALLOC * I915_PDES);
105 106 107

	atomic_inc(px_used(pd));
	pd->entry[idx] = to;
108
	write_dma_entry(px_base(pd), idx, encode(px_dma(to), I915_CACHE_LLC));
109 110 111 112 113
}

void
clear_pd_entry(struct i915_page_directory * const pd,
	       const unsigned short idx,
114
	       const struct drm_i915_gem_object * const scratch)
115 116 117 118 119 120 121 122 123 124 125 126
{
	GEM_BUG_ON(atomic_read(px_used(pd)) == 0);

	write_dma_entry(px_base(pd), idx, scratch->encode);
	pd->entry[idx] = NULL;
	atomic_dec(px_used(pd));
}

bool
release_pd_entry(struct i915_page_directory * const pd,
		 const unsigned short idx,
		 struct i915_page_table * const pt,
127
		 const struct drm_i915_gem_object * const scratch)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
{
	bool free = false;

	if (atomic_add_unless(&pt->used, -1, 1))
		return false;

	spin_lock(&pd->lock);
	if (atomic_dec_and_test(&pt->used)) {
		clear_pd_entry(pd, idx, scratch);
		free = true;
	}
	spin_unlock(&pd->lock);

	return free;
}

int i915_ppgtt_init_hw(struct intel_gt *gt)
{
	struct drm_i915_private *i915 = gt->i915;

	gtt_write_workarounds(gt);

150
	if (GRAPHICS_VER(i915) == 6)
151
		gen6_ppgtt_enable(gt);
152
	else if (GRAPHICS_VER(i915) == 7)
153 154 155 156 157 158
		gen7_ppgtt_enable(gt);

	return 0;
}

static struct i915_ppgtt *
159
__ppgtt_create(struct intel_gt *gt, unsigned long lmem_pt_obj_flags)
160
{
161
	if (GRAPHICS_VER(gt->i915) < 8)
162 163
		return gen6_ppgtt_create(gt);
	else
164
		return gen8_ppgtt_create(gt, lmem_pt_obj_flags);
165 166
}

167 168
struct i915_ppgtt *i915_ppgtt_create(struct intel_gt *gt,
				     unsigned long lmem_pt_obj_flags)
169 170 171
{
	struct i915_ppgtt *ppgtt;

172
	ppgtt = __ppgtt_create(gt, lmem_pt_obj_flags);
173 174 175 176 177 178 179 180
	if (IS_ERR(ppgtt))
		return ppgtt;

	trace_i915_ppgtt_create(&ppgtt->vm);

	return ppgtt;
}

181 182
void ppgtt_bind_vma(struct i915_address_space *vm,
		    struct i915_vm_pt_stash *stash,
183
		    struct i915_vma_resource *vma_res,
184 185
		    enum i915_cache_level cache_level,
		    u32 flags)
186 187 188
{
	u32 pte_flags;

189 190 191 192
	if (!vma_res->allocated) {
		vm->allocate_va_range(vm, stash, vma_res->start,
				      vma_res->vma_size);
		vma_res->allocated = true;
193 194 195 196
	}

	/* Applicable to VLV, and gen8+ */
	pte_flags = 0;
197
	if (vma_res->bi.readonly)
198
		pte_flags |= PTE_READ_ONLY;
199
	if (vma_res->bi.lmem)
200
		pte_flags |= PTE_LM;
201

202
	vm->insert_entries(vm, vma_res, cache_level, pte_flags);
203 204 205
	wmb();
}

206 207
void ppgtt_unbind_vma(struct i915_address_space *vm,
		      struct i915_vma_resource *vma_res)
208
{
209 210
	if (vma_res->allocated)
		vm->clear_range(vm, vma_res->start, vma_res->vma_size);
211 212
}

213 214 215 216 217 218 219 220 221 222 223
static unsigned long pd_count(u64 size, int shift)
{
	/* Beware later misalignment */
	return (size + 2 * (BIT_ULL(shift) - 1)) >> shift;
}

int i915_vm_alloc_pt_stash(struct i915_address_space *vm,
			   struct i915_vm_pt_stash *stash,
			   u64 size)
{
	unsigned long count;
224
	int shift, n, pt_sz;
225 226 227 228 229

	shift = vm->pd_shift;
	if (!shift)
		return 0;

230 231 232 233 234 235 236 237
	pt_sz = stash->pt_sz;
	if (!pt_sz)
		pt_sz = I915_GTT_PAGE_SIZE_4K;
	else
		GEM_BUG_ON(!IS_DGFX(vm->i915));

	GEM_BUG_ON(!is_power_of_2(pt_sz));

238 239 240 241
	count = pd_count(size, shift);
	while (count--) {
		struct i915_page_table *pt;

242
		pt = alloc_pt(vm, pt_sz);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
		if (IS_ERR(pt)) {
			i915_vm_free_pt_stash(vm, stash);
			return PTR_ERR(pt);
		}

		pt->stash = stash->pt[0];
		stash->pt[0] = pt;
	}

	for (n = 1; n < vm->top; n++) {
		shift += ilog2(I915_PDES); /* Each PD holds 512 entries */
		count = pd_count(size, shift);
		while (count--) {
			struct i915_page_directory *pd;

			pd = alloc_pd(vm);
			if (IS_ERR(pd)) {
				i915_vm_free_pt_stash(vm, stash);
				return PTR_ERR(pd);
			}

			pd->pt.stash = stash->pt[1];
			stash->pt[1] = &pd->pt;
		}
	}

	return 0;
}

M
Matthew Auld 已提交
272
int i915_vm_map_pt_stash(struct i915_address_space *vm,
273 274 275 276 277 278 279
			 struct i915_vm_pt_stash *stash)
{
	struct i915_page_table *pt;
	int n, err;

	for (n = 0; n < ARRAY_SIZE(stash->pt); n++) {
		for (pt = stash->pt[n]; pt; pt = pt->stash) {
M
Matthew Auld 已提交
280
			err = map_pt_dma_locked(vm, pt->base);
281 282 283 284 285 286 287 288
			if (err)
				return err;
		}
	}

	return 0;
}

289 290 291 292 293 294 295 296 297
void i915_vm_free_pt_stash(struct i915_address_space *vm,
			   struct i915_vm_pt_stash *stash)
{
	struct i915_page_table *pt;
	int n;

	for (n = 0; n < ARRAY_SIZE(stash->pt); n++) {
		while ((pt = stash->pt[n])) {
			stash->pt[n] = pt->stash;
298
			free_px(vm, pt, n);
299 300 301 302
		}
	}
}

303 304
void ppgtt_init(struct i915_ppgtt *ppgtt, struct intel_gt *gt,
		unsigned long lmem_pt_obj_flags)
305 306 307 308 309
{
	struct drm_i915_private *i915 = gt->i915;

	ppgtt->vm.gt = gt;
	ppgtt->vm.i915 = i915;
310
	ppgtt->vm.dma = i915->drm.dev;
311
	ppgtt->vm.total = BIT_ULL(INTEL_INFO(i915)->ppgtt_size);
312
	ppgtt->vm.lmem_pt_obj_flags = lmem_pt_obj_flags;
313

314
	dma_resv_init(&ppgtt->vm._resv);
315 316 317 318 319
	i915_address_space_init(&ppgtt->vm, VM_CLASS_PPGTT);

	ppgtt->vm.vma_ops.bind_vma    = ppgtt_bind_vma;
	ppgtt->vm.vma_ops.unbind_vma  = ppgtt_unbind_vma;
}