base.c 11.3 KB
Newer Older
B
Ben Skeggs 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright 2010 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */

25
#include <core/gpuobj.h>
26
#include <core/mm.h>
27 28

#include <subdev/fb.h>
29
#include <subdev/mmu.h>
B
Ben Skeggs 已提交
30 31

void
32
nouveau_vm_map_at(struct nouveau_vma *vma, u64 delta, struct nouveau_mem *node)
B
Ben Skeggs 已提交
33 34
{
	struct nouveau_vm *vm = vma->vm;
35
	struct nouveau_mmu *mmu = vm->mmu;
B
Ben Skeggs 已提交
36
	struct nouveau_mm_node *r;
37
	int big = vma->node->type != mmu->spg_shift;
B
Ben Skeggs 已提交
38 39
	u32 offset = vma->node->offset + (delta >> 12);
	u32 bits = vma->node->type - 12;
40 41 42
	u32 pde  = (offset >> mmu->pgt_bits) - vm->fpde;
	u32 pte  = (offset & ((1 << mmu->pgt_bits) - 1)) >> bits;
	u32 max  = 1 << (mmu->pgt_bits - bits);
B
Ben Skeggs 已提交
43 44
	u32 end, len;

B
Ben Skeggs 已提交
45
	delta = 0;
46
	list_for_each_entry(r, &node->regions, rl_entry) {
B
Ben Skeggs 已提交
47 48 49 50
		u64 phys = (u64)r->offset << 12;
		u32 num  = r->length >> bits;

		while (num) {
51
			struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
B
Ben Skeggs 已提交
52 53 54 55 56 57

			end = (pte + num);
			if (unlikely(end >= max))
				end = max;
			len = end - pte;

58
			mmu->map(vma, pgt, node, pte, len, phys, delta);
B
Ben Skeggs 已提交
59 60 61 62

			num -= len;
			pte += len;
			if (unlikely(end >= max)) {
63
				phys += len << (bits + 12);
B
Ben Skeggs 已提交
64 65 66
				pde++;
				pte = 0;
			}
B
Ben Skeggs 已提交
67 68

			delta += (u64)len << vma->node->type;
B
Ben Skeggs 已提交
69 70 71
		}
	}

72
	mmu->flush(vm);
B
Ben Skeggs 已提交
73 74
}

75
static void
D
Dave Airlie 已提交
76 77 78 79
nouveau_vm_map_sg_table(struct nouveau_vma *vma, u64 delta, u64 length,
			struct nouveau_mem *mem)
{
	struct nouveau_vm *vm = vma->vm;
80 81
	struct nouveau_mmu *mmu = vm->mmu;
	int big = vma->node->type != mmu->spg_shift;
D
Dave Airlie 已提交
82 83 84
	u32 offset = vma->node->offset + (delta >> 12);
	u32 bits = vma->node->type - 12;
	u32 num  = length >> vma->node->type;
85 86 87
	u32 pde  = (offset >> mmu->pgt_bits) - vm->fpde;
	u32 pte  = (offset & ((1 << mmu->pgt_bits) - 1)) >> bits;
	u32 max  = 1 << (mmu->pgt_bits - bits);
D
Dave Airlie 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	unsigned m, sglen;
	u32 end, len;
	int i;
	struct scatterlist *sg;

	for_each_sg(mem->sg->sgl, sg, mem->sg->nents, i) {
		struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
		sglen = sg_dma_len(sg) >> PAGE_SHIFT;

		end = pte + sglen;
		if (unlikely(end >= max))
			end = max;
		len = end - pte;

		for (m = 0; m < len; m++) {
			dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);

105
			mmu->map_sg(vma, pgt, mem, pte, 1, &addr);
D
Dave Airlie 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119
			num--;
			pte++;

			if (num == 0)
				goto finish;
		}
		if (unlikely(end >= max)) {
			pde++;
			pte = 0;
		}
		if (m < sglen) {
			for (; m < sglen; m++) {
				dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);

120
				mmu->map_sg(vma, pgt, mem, pte, 1, &addr);
D
Dave Airlie 已提交
121 122 123 124 125 126 127 128 129
				num--;
				pte++;
				if (num == 0)
					goto finish;
			}
		}

	}
finish:
130
	mmu->flush(vm);
D
Dave Airlie 已提交
131 132
}

133
static void
B
Ben Skeggs 已提交
134
nouveau_vm_map_sg(struct nouveau_vma *vma, u64 delta, u64 length,
135
		  struct nouveau_mem *mem)
B
Ben Skeggs 已提交
136 137
{
	struct nouveau_vm *vm = vma->vm;
138
	struct nouveau_mmu *mmu = vm->mmu;
139
	dma_addr_t *list = mem->pages;
140
	int big = vma->node->type != mmu->spg_shift;
B
Ben Skeggs 已提交
141 142 143
	u32 offset = vma->node->offset + (delta >> 12);
	u32 bits = vma->node->type - 12;
	u32 num  = length >> vma->node->type;
144 145 146
	u32 pde  = (offset >> mmu->pgt_bits) - vm->fpde;
	u32 pte  = (offset & ((1 << mmu->pgt_bits) - 1)) >> bits;
	u32 max  = 1 << (mmu->pgt_bits - bits);
B
Ben Skeggs 已提交
147 148 149
	u32 end, len;

	while (num) {
150
		struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
B
Ben Skeggs 已提交
151 152 153 154 155 156

		end = (pte + num);
		if (unlikely(end >= max))
			end = max;
		len = end - pte;

157
		mmu->map_sg(vma, pgt, mem, pte, len, list);
B
Ben Skeggs 已提交
158 159 160 161 162 163 164 165 166 167

		num  -= len;
		pte  += len;
		list += len;
		if (unlikely(end >= max)) {
			pde++;
			pte = 0;
		}
	}

168
	mmu->flush(vm);
B
Ben Skeggs 已提交
169 170
}

171 172 173 174 175 176 177 178 179 180 181 182
void
nouveau_vm_map(struct nouveau_vma *vma, struct nouveau_mem *node)
{
	if (node->sg)
		nouveau_vm_map_sg_table(vma, 0, node->size << 12, node);
	else
	if (node->pages)
		nouveau_vm_map_sg(vma, 0, node->size << 12, node);
	else
		nouveau_vm_map_at(vma, 0, node);
}

B
Ben Skeggs 已提交
183 184 185 186
void
nouveau_vm_unmap_at(struct nouveau_vma *vma, u64 delta, u64 length)
{
	struct nouveau_vm *vm = vma->vm;
187 188
	struct nouveau_mmu *mmu = vm->mmu;
	int big = vma->node->type != mmu->spg_shift;
B
Ben Skeggs 已提交
189 190 191
	u32 offset = vma->node->offset + (delta >> 12);
	u32 bits = vma->node->type - 12;
	u32 num  = length >> vma->node->type;
192 193 194
	u32 pde  = (offset >> mmu->pgt_bits) - vm->fpde;
	u32 pte  = (offset & ((1 << mmu->pgt_bits) - 1)) >> bits;
	u32 max  = 1 << (mmu->pgt_bits - bits);
B
Ben Skeggs 已提交
195 196 197
	u32 end, len;

	while (num) {
198
		struct nouveau_gpuobj *pgt = vm->pgt[pde].obj[big];
B
Ben Skeggs 已提交
199 200 201 202 203 204

		end = (pte + num);
		if (unlikely(end >= max))
			end = max;
		len = end - pte;

205
		mmu->unmap(pgt, pte, len);
B
Ben Skeggs 已提交
206 207 208 209 210 211 212 213 214

		num -= len;
		pte += len;
		if (unlikely(end >= max)) {
			pde++;
			pte = 0;
		}
	}

215
	mmu->flush(vm);
B
Ben Skeggs 已提交
216 217 218 219 220 221 222 223 224
}

void
nouveau_vm_unmap(struct nouveau_vma *vma)
{
	nouveau_vm_unmap_at(vma, 0, (u64)vma->node->length << 12);
}

static void
225
nouveau_vm_unmap_pgt(struct nouveau_vm *vm, int big, u32 fpde, u32 lpde)
B
Ben Skeggs 已提交
226
{
227
	struct nouveau_mmu *mmu = vm->mmu;
B
Ben Skeggs 已提交
228 229 230 231 232 233 234
	struct nouveau_vm_pgd *vpgd;
	struct nouveau_vm_pgt *vpgt;
	struct nouveau_gpuobj *pgt;
	u32 pde;

	for (pde = fpde; pde <= lpde; pde++) {
		vpgt = &vm->pgt[pde - vm->fpde];
235
		if (--vpgt->refcount[big])
B
Ben Skeggs 已提交
236 237
			continue;

238 239 240
		pgt = vpgt->obj[big];
		vpgt->obj[big] = NULL;

B
Ben Skeggs 已提交
241
		list_for_each_entry(vpgd, &vm->pgd_list, head) {
242
			mmu->map_pgt(vpgd->obj, pde, vpgt->obj);
B
Ben Skeggs 已提交
243 244
		}

245
		mutex_unlock(&nv_subdev(mmu)->mutex);
B
Ben Skeggs 已提交
246
		nouveau_gpuobj_ref(NULL, &pgt);
247
		mutex_lock(&nv_subdev(mmu)->mutex);
B
Ben Skeggs 已提交
248 249 250 251 252 253
	}
}

static int
nouveau_vm_map_pgt(struct nouveau_vm *vm, u32 pde, u32 type)
{
254
	struct nouveau_mmu *mmu = vm->mmu;
B
Ben Skeggs 已提交
255 256 257
	struct nouveau_vm_pgt *vpgt = &vm->pgt[pde - vm->fpde];
	struct nouveau_vm_pgd *vpgd;
	struct nouveau_gpuobj *pgt;
258
	int big = (type != mmu->spg_shift);
B
Ben Skeggs 已提交
259 260 261
	u32 pgt_size;
	int ret;

262
	pgt_size  = (1 << (mmu->pgt_bits + 12)) >> type;
B
Ben Skeggs 已提交
263 264
	pgt_size *= 8;

265 266
	mutex_unlock(&nv_subdev(mmu)->mutex);
	ret = nouveau_gpuobj_new(nv_object(vm->mmu), NULL, pgt_size, 0x1000,
B
Ben Skeggs 已提交
267
				 NVOBJ_FLAG_ZERO_ALLOC, &pgt);
268
	mutex_lock(&nv_subdev(mmu)->mutex);
B
Ben Skeggs 已提交
269 270 271 272
	if (unlikely(ret))
		return ret;

	/* someone beat us to filling the PDE while we didn't have the lock */
273
	if (unlikely(vpgt->refcount[big]++)) {
274
		mutex_unlock(&nv_subdev(mmu)->mutex);
B
Ben Skeggs 已提交
275
		nouveau_gpuobj_ref(NULL, &pgt);
276
		mutex_lock(&nv_subdev(mmu)->mutex);
B
Ben Skeggs 已提交
277 278 279
		return 0;
	}

280
	vpgt->obj[big] = pgt;
B
Ben Skeggs 已提交
281
	list_for_each_entry(vpgd, &vm->pgd_list, head) {
282
		mmu->map_pgt(vpgd->obj, pde, vpgt->obj);
B
Ben Skeggs 已提交
283 284 285 286 287 288 289 290 291
	}

	return 0;
}

int
nouveau_vm_get(struct nouveau_vm *vm, u64 size, u32 page_shift,
	       u32 access, struct nouveau_vma *vma)
{
292
	struct nouveau_mmu *mmu = vm->mmu;
B
Ben Skeggs 已提交
293 294 295 296 297
	u32 align = (1 << page_shift) >> 12;
	u32 msize = size >> 12;
	u32 fpde, lpde, pde;
	int ret;

298
	mutex_lock(&nv_subdev(mmu)->mutex);
299
	ret = nouveau_mm_head(&vm->mm, 0, page_shift, msize, msize, align,
300
			     &vma->node);
B
Ben Skeggs 已提交
301
	if (unlikely(ret != 0)) {
302
		mutex_unlock(&nv_subdev(mmu)->mutex);
B
Ben Skeggs 已提交
303 304 305
		return ret;
	}

306 307
	fpde = (vma->node->offset >> mmu->pgt_bits);
	lpde = (vma->node->offset + vma->node->length - 1) >> mmu->pgt_bits;
308

B
Ben Skeggs 已提交
309 310
	for (pde = fpde; pde <= lpde; pde++) {
		struct nouveau_vm_pgt *vpgt = &vm->pgt[pde - vm->fpde];
311
		int big = (vma->node->type != mmu->spg_shift);
B
Ben Skeggs 已提交
312

313 314
		if (likely(vpgt->refcount[big])) {
			vpgt->refcount[big]++;
B
Ben Skeggs 已提交
315 316 317 318 319 320
			continue;
		}

		ret = nouveau_vm_map_pgt(vm, pde, vma->node->type);
		if (ret) {
			if (pde != fpde)
321
				nouveau_vm_unmap_pgt(vm, big, fpde, pde - 1);
322
			nouveau_mm_free(&vm->mm, &vma->node);
323
			mutex_unlock(&nv_subdev(mmu)->mutex);
B
Ben Skeggs 已提交
324 325 326
			return ret;
		}
	}
327
	mutex_unlock(&nv_subdev(mmu)->mutex);
B
Ben Skeggs 已提交
328

329 330
	vma->vm = NULL;
	nouveau_vm_ref(vm, &vma->vm, NULL);
B
Ben Skeggs 已提交
331 332 333 334 335 336 337 338 339
	vma->offset = (u64)vma->node->offset << 12;
	vma->access = access;
	return 0;
}

void
nouveau_vm_put(struct nouveau_vma *vma)
{
	struct nouveau_vm *vm = vma->vm;
340
	struct nouveau_mmu *mmu = vm->mmu;
B
Ben Skeggs 已提交
341 342 343 344
	u32 fpde, lpde;

	if (unlikely(vma->node == NULL))
		return;
345 346
	fpde = (vma->node->offset >> mmu->pgt_bits);
	lpde = (vma->node->offset + vma->node->length - 1) >> mmu->pgt_bits;
B
Ben Skeggs 已提交
347

348 349
	mutex_lock(&nv_subdev(mmu)->mutex);
	nouveau_vm_unmap_pgt(vm, vma->node->type != mmu->spg_shift, fpde, lpde);
350
	nouveau_mm_free(&vm->mm, &vma->node);
351
	mutex_unlock(&nv_subdev(mmu)->mutex);
352 353

	nouveau_vm_ref(NULL, &vma->vm, NULL);
B
Ben Skeggs 已提交
354 355 356
}

int
357
nouveau_vm_create(struct nouveau_mmu *mmu, u64 offset, u64 length,
358
		  u64 mm_offset, u32 block, struct nouveau_vm **pvm)
B
Ben Skeggs 已提交
359 360 361 362 363
{
	struct nouveau_vm *vm;
	u64 mm_length = (offset + length) - mm_offset;
	int ret;

364
	vm = kzalloc(sizeof(*vm), GFP_KERNEL);
B
Ben Skeggs 已提交
365 366 367
	if (!vm)
		return -ENOMEM;

368
	INIT_LIST_HEAD(&vm->pgd_list);
369
	vm->mmu = mmu;
370
	kref_init(&vm->refcount);
371 372
	vm->fpde = offset >> (mmu->pgt_bits + 12);
	vm->lpde = (offset + length - 1) >> (mmu->pgt_bits + 12);
B
Ben Skeggs 已提交
373

374
	vm->pgt  = vzalloc((vm->lpde - vm->fpde + 1) * sizeof(*vm->pgt));
B
Ben Skeggs 已提交
375 376 377 378 379 380 381 382
	if (!vm->pgt) {
		kfree(vm);
		return -ENOMEM;
	}

	ret = nouveau_mm_init(&vm->mm, mm_offset >> 12, mm_length >> 12,
			      block >> 12);
	if (ret) {
383
		vfree(vm->pgt);
B
Ben Skeggs 已提交
384 385 386 387
		kfree(vm);
		return ret;
	}

388 389
	*pvm = vm;

B
Ben Skeggs 已提交
390 391 392
	return 0;
}

393 394 395 396
int
nouveau_vm_new(struct nouveau_device *device, u64 offset, u64 length,
	       u64 mm_offset, struct nouveau_vm **pvm)
{
397 398
	struct nouveau_mmu *mmu = nouveau_mmu(device);
	return mmu->create(mmu, offset, length, mm_offset, pvm);
399 400
}

B
Ben Skeggs 已提交
401 402 403
static int
nouveau_vm_link(struct nouveau_vm *vm, struct nouveau_gpuobj *pgd)
{
404
	struct nouveau_mmu *mmu = vm->mmu;
B
Ben Skeggs 已提交
405 406 407 408 409 410 411 412 413 414 415 416
	struct nouveau_vm_pgd *vpgd;
	int i;

	if (!pgd)
		return 0;

	vpgd = kzalloc(sizeof(*vpgd), GFP_KERNEL);
	if (!vpgd)
		return -ENOMEM;

	nouveau_gpuobj_ref(pgd, &vpgd->obj);

417
	mutex_lock(&nv_subdev(mmu)->mutex);
418
	for (i = vm->fpde; i <= vm->lpde; i++)
419
		mmu->map_pgt(pgd, i, vm->pgt[i - vm->fpde].obj);
B
Ben Skeggs 已提交
420
	list_add(&vpgd->head, &vm->pgd_list);
421
	mutex_unlock(&nv_subdev(mmu)->mutex);
B
Ben Skeggs 已提交
422 423 424 425
	return 0;
}

static void
426
nouveau_vm_unlink(struct nouveau_vm *vm, struct nouveau_gpuobj *mpgd)
B
Ben Skeggs 已提交
427
{
428
	struct nouveau_mmu *mmu = vm->mmu;
B
Ben Skeggs 已提交
429
	struct nouveau_vm_pgd *vpgd, *tmp;
430
	struct nouveau_gpuobj *pgd = NULL;
B
Ben Skeggs 已提交
431

432
	if (!mpgd)
B
Ben Skeggs 已提交
433 434
		return;

435
	mutex_lock(&nv_subdev(mmu)->mutex);
B
Ben Skeggs 已提交
436
	list_for_each_entry_safe(vpgd, tmp, &vm->pgd_list, head) {
437 438 439 440 441 442
		if (vpgd->obj == mpgd) {
			pgd = vpgd->obj;
			list_del(&vpgd->head);
			kfree(vpgd);
			break;
		}
B
Ben Skeggs 已提交
443
	}
444
	mutex_unlock(&nv_subdev(mmu)->mutex);
445 446

	nouveau_gpuobj_ref(NULL, &pgd);
B
Ben Skeggs 已提交
447 448 449
}

static void
450
nouveau_vm_del(struct kref *kref)
B
Ben Skeggs 已提交
451
{
452
	struct nouveau_vm *vm = container_of(kref, typeof(*vm), refcount);
B
Ben Skeggs 已提交
453 454 455 456 457 458
	struct nouveau_vm_pgd *vpgd, *tmp;

	list_for_each_entry_safe(vpgd, tmp, &vm->pgd_list, head) {
		nouveau_vm_unlink(vm, vpgd->obj);
	}

459
	nouveau_mm_fini(&vm->mm);
460
	vfree(vm->pgt);
B
Ben Skeggs 已提交
461 462 463 464 465 466 467
	kfree(vm);
}

int
nouveau_vm_ref(struct nouveau_vm *ref, struct nouveau_vm **ptr,
	       struct nouveau_gpuobj *pgd)
{
468 469
	if (ref) {
		int ret = nouveau_vm_link(ref, pgd);
B
Ben Skeggs 已提交
470 471 472
		if (ret)
			return ret;

473
		kref_get(&ref->refcount);
B
Ben Skeggs 已提交
474 475
	}

476 477 478
	if (*ptr) {
		nouveau_vm_unlink(*ptr, pgd);
		kref_put(&(*ptr)->refcount, nouveau_vm_del);
B
Ben Skeggs 已提交
479 480
	}

481
	*ptr = ref;
B
Ben Skeggs 已提交
482 483
	return 0;
}