book3s_64_vio.c 17.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
 * Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com>
17
 * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com>
18 19 20 21 22 23 24 25 26
 */

#include <linux/types.h>
#include <linux/string.h>
#include <linux/kvm.h>
#include <linux/kvm_host.h>
#include <linux/highmem.h>
#include <linux/gfp.h>
#include <linux/slab.h>
27
#include <linux/sched/signal.h>
28 29 30
#include <linux/hugetlb.h>
#include <linux/list.h>
#include <linux/anon_inodes.h>
31 32
#include <linux/iommu.h>
#include <linux/file.h>
33 34 35

#include <asm/kvm_ppc.h>
#include <asm/kvm_book3s.h>
36
#include <asm/book3s/64/mmu-hash.h>
37 38 39 40 41
#include <asm/hvcall.h>
#include <asm/synch.h>
#include <asm/ppc-opcode.h>
#include <asm/kvm_host.h>
#include <asm/udbg.h>
42
#include <asm/iommu.h>
43
#include <asm/tce.h>
44
#include <asm/mmu_context.h>
45

46
static unsigned long kvmppc_tce_pages(unsigned long iommu_pages)
47
{
48
	return ALIGN(iommu_pages * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
49 50
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
static unsigned long kvmppc_stt_pages(unsigned long tce_pages)
{
	unsigned long stt_bytes = sizeof(struct kvmppc_spapr_tce_table) +
			(tce_pages * sizeof(struct page *));

	return tce_pages + ALIGN(stt_bytes, PAGE_SIZE) / PAGE_SIZE;
}

static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)
{
	long ret = 0;

	if (!current || !current->mm)
		return ret; /* process exited */

	down_write(&current->mm->mmap_sem);

	if (inc) {
		unsigned long locked, lock_limit;

		locked = current->mm->locked_vm + stt_pages;
		lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
			ret = -ENOMEM;
		else
			current->mm->locked_vm += stt_pages;
	} else {
		if (WARN_ON_ONCE(stt_pages > current->mm->locked_vm))
			stt_pages = current->mm->locked_vm;

		current->mm->locked_vm -= stt_pages;
	}

	pr_debug("[%d] RLIMIT_MEMLOCK KVM %c%ld %ld/%ld%s\n", current->pid,
			inc ? '+' : '-',
			stt_pages << PAGE_SHIFT,
			current->mm->locked_vm << PAGE_SHIFT,
			rlimit(RLIMIT_MEMLOCK),
			ret ? " - exceeded" : "");

	up_write(&current->mm->mmap_sem);

	return ret;
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
static void kvm_spapr_tce_iommu_table_free(struct rcu_head *head)
{
	struct kvmppc_spapr_tce_iommu_table *stit = container_of(head,
			struct kvmppc_spapr_tce_iommu_table, rcu);

	iommu_tce_table_put(stit->tbl);

	kfree(stit);
}

static void kvm_spapr_tce_liobn_put(struct kref *kref)
{
	struct kvmppc_spapr_tce_iommu_table *stit = container_of(kref,
			struct kvmppc_spapr_tce_iommu_table, kref);

	list_del_rcu(&stit->next);

	call_rcu(&stit->rcu, kvm_spapr_tce_iommu_table_free);
}

extern void kvm_spapr_tce_release_iommu_group(struct kvm *kvm,
		struct iommu_group *grp)
{
	int i;
	struct kvmppc_spapr_tce_table *stt;
	struct kvmppc_spapr_tce_iommu_table *stit, *tmp;
	struct iommu_table_group *table_group = NULL;

	list_for_each_entry_rcu(stt, &kvm->arch.spapr_tce_tables, list) {

		table_group = iommu_group_get_iommudata(grp);
		if (WARN_ON(!table_group))
			continue;

		list_for_each_entry_safe(stit, tmp, &stt->iommu_tables, next) {
			for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
				if (table_group->tables[i] != stit->tbl)
					continue;

				kref_put(&stit->kref, kvm_spapr_tce_liobn_put);
				return;
			}
		}
	}
}

extern long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
		struct iommu_group *grp)
{
	struct kvmppc_spapr_tce_table *stt = NULL;
	bool found = false;
	struct iommu_table *tbl = NULL;
	struct iommu_table_group *table_group;
	long i;
	struct kvmppc_spapr_tce_iommu_table *stit;
	struct fd f;

	f = fdget(tablefd);
	if (!f.file)
		return -EBADF;

	list_for_each_entry_rcu(stt, &kvm->arch.spapr_tce_tables, list) {
		if (stt == f.file->private_data) {
			found = true;
			break;
		}
	}

	fdput(f);

	if (!found)
		return -EINVAL;

	table_group = iommu_group_get_iommudata(grp);
	if (WARN_ON(!table_group))
		return -EFAULT;

	for (i = 0; i < IOMMU_TABLE_GROUP_MAX_TABLES; ++i) {
		struct iommu_table *tbltmp = table_group->tables[i];

		if (!tbltmp)
			continue;
178 179 180 181
		/* Make sure hardware table parameters are compatible */
		if ((tbltmp->it_page_shift <= stt->page_shift) &&
				(tbltmp->it_offset << tbltmp->it_page_shift ==
				 stt->offset << stt->page_shift) &&
182
				(tbltmp->it_size << tbltmp->it_page_shift >=
183
				 stt->size << stt->page_shift)) {
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
			/*
			 * Reference the table to avoid races with
			 * add/remove DMA windows.
			 */
			tbl = iommu_tce_table_get(tbltmp);
			break;
		}
	}
	if (!tbl)
		return -EINVAL;

	list_for_each_entry_rcu(stit, &stt->iommu_tables, next) {
		if (tbl != stit->tbl)
			continue;

		if (!kref_get_unless_zero(&stit->kref)) {
			/* stit is being destroyed */
			iommu_tce_table_put(tbl);
			return -ENOTTY;
		}
		/*
		 * The table is already known to this KVM, we just increased
		 * its KVM reference counter and can return.
		 */
		return 0;
	}

	stit = kzalloc(sizeof(*stit), GFP_KERNEL);
	if (!stit) {
		iommu_tce_table_put(tbl);
		return -ENOMEM;
	}

	stit->tbl = tbl;
	kref_init(&stit->kref);

	list_add_rcu(&stit->next, &stt->iommu_tables);

	return 0;
}

225
static void release_spapr_tce_table(struct rcu_head *head)
226
{
227 228
	struct kvmppc_spapr_tce_table *stt = container_of(head,
			struct kvmppc_spapr_tce_table, rcu);
229
	unsigned long i, npages = kvmppc_tce_pages(stt->size);
230

231
	for (i = 0; i < npages; i++)
232 233
		__free_page(stt->pages[i]);

234
	kfree(stt);
235 236
}

237
static vm_fault_t kvm_spapr_tce_fault(struct vm_fault *vmf)
238
{
239
	struct kvmppc_spapr_tce_table *stt = vmf->vma->vm_file->private_data;
240 241
	struct page *page;

242
	if (vmf->pgoff >= kvmppc_tce_pages(stt->size))
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
		return VM_FAULT_SIGBUS;

	page = stt->pages[vmf->pgoff];
	get_page(page);
	vmf->page = page;
	return 0;
}

static const struct vm_operations_struct kvm_spapr_tce_vm_ops = {
	.fault = kvm_spapr_tce_fault,
};

static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma)
{
	vma->vm_ops = &kvm_spapr_tce_vm_ops;
	return 0;
}

static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
{
	struct kvmppc_spapr_tce_table *stt = filp->private_data;
264
	struct kvmppc_spapr_tce_iommu_table *stit, *tmp;
265
	struct kvm *kvm = stt->kvm;
266

267
	mutex_lock(&kvm->lock);
268
	list_del_rcu(&stt->list);
269
	mutex_unlock(&kvm->lock);
270

271 272 273 274 275 276 277 278
	list_for_each_entry_safe(stit, tmp, &stt->iommu_tables, next) {
		WARN_ON(!kref_read(&stit->kref));
		while (1) {
			if (kref_put(&stit->kref, kvm_spapr_tce_liobn_put))
				break;
		}
	}

279 280
	kvm_put_kvm(stt->kvm);

281
	kvmppc_account_memlimit(
282
		kvmppc_stt_pages(kvmppc_tce_pages(stt->size)), false);
283 284
	call_rcu(&stt->rcu, release_spapr_tce_table);

285 286 287
	return 0;
}

288
static const struct file_operations kvm_spapr_tce_fops = {
289 290 291 292 293
	.mmap           = kvm_spapr_tce_mmap,
	.release	= kvm_spapr_tce_release,
};

long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
294
				   struct kvm_create_spapr_tce_64 *args)
295 296
{
	struct kvmppc_spapr_tce_table *stt = NULL;
297
	struct kvmppc_spapr_tce_table *siter;
298
	unsigned long npages, size = args->size;
299 300 301
	int ret = -ENOMEM;
	int i;

302 303
	if (!args->size || args->page_shift < 12 || args->page_shift > 34 ||
		(args->offset + args->size > (ULLONG_MAX >> args->page_shift)))
304 305
		return -EINVAL;

306
	npages = kvmppc_tce_pages(size);
307
	ret = kvmppc_account_memlimit(kvmppc_stt_pages(npages), true);
308 309
	if (ret)
		return ret;
310

311
	ret = -ENOMEM;
312 313 314
	stt = kzalloc(sizeof(*stt) + npages * sizeof(struct page *),
		      GFP_KERNEL);
	if (!stt)
315
		goto fail_acct;
316 317

	stt->liobn = args->liobn;
318 319
	stt->page_shift = args->page_shift;
	stt->offset = args->offset;
320
	stt->size = size;
321
	stt->kvm = kvm;
322
	INIT_LIST_HEAD_RCU(&stt->iommu_tables);
323 324 325 326 327 328 329 330

	for (i = 0; i < npages; i++) {
		stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
		if (!stt->pages[i])
			goto fail;
	}

	mutex_lock(&kvm->lock);
331 332 333 334 335 336 337 338 339 340

	/* Check this LIOBN hasn't been previously allocated */
	ret = 0;
	list_for_each_entry(siter, &kvm->arch.spapr_tce_tables, list) {
		if (siter->liobn == args->liobn) {
			ret = -EBUSY;
			break;
		}
	}

341 342 343 344 345
	if (!ret)
		ret = anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
				       stt, O_RDWR | O_CLOEXEC);

	if (ret >= 0) {
346 347 348
		list_add_rcu(&stt->list, &kvm->arch.spapr_tce_tables);
		kvm_get_kvm(kvm);
	}
349 350 351

	mutex_unlock(&kvm->lock);

352 353
	if (ret >= 0)
		return ret;
354

355 356 357 358 359 360 361 362
 fail:
	for (i = 0; i < npages; i++)
		if (stt->pages[i])
			__free_page(stt->pages[i]);

	kfree(stt);
 fail_acct:
	kvmppc_account_memlimit(kvmppc_stt_pages(npages), false);
363 364
	return ret;
}
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
static long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *stt,
		unsigned long tce)
{
	unsigned long gpa = tce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
	enum dma_data_direction dir = iommu_tce_direction(tce);
	struct kvmppc_spapr_tce_iommu_table *stit;
	unsigned long ua = 0;

	/* Allow userspace to poison TCE table */
	if (dir == DMA_NONE)
		return H_SUCCESS;

	if (iommu_tce_check_gpa(stt->page_shift, gpa))
		return H_TOO_HARD;

381
	if (kvmppc_tce_to_ua(stt->kvm, tce, &ua, NULL))
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
		return H_TOO_HARD;

	list_for_each_entry_rcu(stit, &stt->iommu_tables, next) {
		unsigned long hpa = 0;
		struct mm_iommu_table_group_mem_t *mem;
		long shift = stit->tbl->it_page_shift;

		mem = mm_iommu_lookup(stt->kvm->mm, ua, 1ULL << shift);
		if (!mem)
			return H_TOO_HARD;

		if (mm_iommu_ua_to_hpa(mem, ua, shift, &hpa))
			return H_TOO_HARD;
	}

	return H_SUCCESS;
}

400 401 402 403 404 405 406 407 408 409 410 411 412
static void kvmppc_clear_tce(struct iommu_table *tbl, unsigned long entry)
{
	unsigned long hpa = 0;
	enum dma_data_direction dir = DMA_NONE;

	iommu_tce_xchg(tbl, entry, &hpa, &dir);
}

static long kvmppc_tce_iommu_mapped_dec(struct kvm *kvm,
		struct iommu_table *tbl, unsigned long entry)
{
	struct mm_iommu_table_group_mem_t *mem = NULL;
	const unsigned long pgsize = 1ULL << tbl->it_page_shift;
413
	__be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY(tbl, entry);
414 415 416 417 418

	if (!pua)
		/* it_userspace allocation might be delayed */
		return H_TOO_HARD;

419
	mem = mm_iommu_lookup(kvm->mm, be64_to_cpu(*pua), pgsize);
420 421 422 423 424
	if (!mem)
		return H_TOO_HARD;

	mm_iommu_mapped_dec(mem);

425
	*pua = cpu_to_be64(0);
426 427 428 429

	return H_SUCCESS;
}

430
static long kvmppc_tce_iommu_do_unmap(struct kvm *kvm,
431 432 433 434 435 436 437
		struct iommu_table *tbl, unsigned long entry)
{
	enum dma_data_direction dir = DMA_NONE;
	unsigned long hpa = 0;
	long ret;

	if (WARN_ON_ONCE(iommu_tce_xchg(tbl, entry, &hpa, &dir)))
438
		return H_TOO_HARD;
439 440 441 442 443 444 445 446 447 448 449

	if (dir == DMA_NONE)
		return H_SUCCESS;

	ret = kvmppc_tce_iommu_mapped_dec(kvm, tbl, entry);
	if (ret != H_SUCCESS)
		iommu_tce_xchg(tbl, entry, &hpa, &dir);

	return ret;
}

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
static long kvmppc_tce_iommu_unmap(struct kvm *kvm,
		struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
		unsigned long entry)
{
	unsigned long i, ret = H_SUCCESS;
	unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
	unsigned long io_entry = entry * subpages;

	for (i = 0; i < subpages; ++i) {
		ret = kvmppc_tce_iommu_do_unmap(kvm, tbl, io_entry + i);
		if (ret != H_SUCCESS)
			break;
	}

	return ret;
}

long kvmppc_tce_iommu_do_map(struct kvm *kvm, struct iommu_table *tbl,
468 469 470 471
		unsigned long entry, unsigned long ua,
		enum dma_data_direction dir)
{
	long ret;
472 473
	unsigned long hpa;
	__be64 *pua = IOMMU_TABLE_USERSPACE_ENTRY(tbl, entry);
474 475 476 477 478 479 480 481 482 483 484
	struct mm_iommu_table_group_mem_t *mem;

	if (!pua)
		/* it_userspace allocation might be delayed */
		return H_TOO_HARD;

	mem = mm_iommu_lookup(kvm->mm, ua, 1ULL << tbl->it_page_shift);
	if (!mem)
		/* This only handles v2 IOMMU type, v1 is handled via ioctl() */
		return H_TOO_HARD;

485
	if (WARN_ON_ONCE(mm_iommu_ua_to_hpa(mem, ua, tbl->it_page_shift, &hpa)))
486
		return H_TOO_HARD;
487 488

	if (mm_iommu_mapped_inc(mem))
489
		return H_TOO_HARD;
490 491 492 493

	ret = iommu_tce_xchg(tbl, entry, &hpa, &dir);
	if (WARN_ON_ONCE(ret)) {
		mm_iommu_mapped_dec(mem);
494
		return H_TOO_HARD;
495 496 497 498 499
	}

	if (dir != DMA_NONE)
		kvmppc_tce_iommu_mapped_dec(kvm, tbl, entry);

500
	*pua = cpu_to_be64(ua);
501 502 503 504

	return 0;
}

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
static long kvmppc_tce_iommu_map(struct kvm *kvm,
		struct kvmppc_spapr_tce_table *stt, struct iommu_table *tbl,
		unsigned long entry, unsigned long ua,
		enum dma_data_direction dir)
{
	unsigned long i, pgoff, ret = H_SUCCESS;
	unsigned long subpages = 1ULL << (stt->page_shift - tbl->it_page_shift);
	unsigned long io_entry = entry * subpages;

	for (i = 0, pgoff = 0; i < subpages;
			++i, pgoff += IOMMU_PAGE_SIZE(tbl)) {

		ret = kvmppc_tce_iommu_do_map(kvm, tbl,
				io_entry + i, ua + pgoff, dir);
		if (ret != H_SUCCESS)
			break;
	}

	return ret;
}

526 527 528
long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
		      unsigned long ioba, unsigned long tce)
{
529
	struct kvmppc_spapr_tce_table *stt;
530 531 532 533
	long ret, idx;
	struct kvmppc_spapr_tce_iommu_table *stit;
	unsigned long entry, ua = 0;
	enum dma_data_direction dir;
534 535 536 537

	/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
	/* 	    liobn, ioba, tce); */

538
	stt = kvmppc_find_table(vcpu->kvm, liobn);
539 540 541 542 543 544 545 546 547 548 549
	if (!stt)
		return H_TOO_HARD;

	ret = kvmppc_ioba_validate(stt, ioba, 1);
	if (ret != H_SUCCESS)
		return ret;

	ret = kvmppc_tce_validate(stt, tce);
	if (ret != H_SUCCESS)
		return ret;

550
	dir = iommu_tce_direction(tce);
551 552 553

	idx = srcu_read_lock(&vcpu->kvm->srcu);

554
	if ((dir != DMA_NONE) && kvmppc_tce_to_ua(vcpu->kvm, tce, &ua, NULL)) {
555 556 557
		ret = H_PARAMETER;
		goto unlock_exit;
	}
558 559 560 561

	entry = ioba >> stt->page_shift;

	list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
562
		if (dir == DMA_NONE)
563
			ret = kvmppc_tce_iommu_unmap(vcpu->kvm, stt,
564
					stit->tbl, entry);
565
		else
566
			ret = kvmppc_tce_iommu_map(vcpu->kvm, stt, stit->tbl,
567 568
					entry, ua, dir);

569 570
		if (ret != H_SUCCESS) {
			kvmppc_clear_tce(stit->tbl, entry);
571
			goto unlock_exit;
572
		}
573 574 575
	}

	kvmppc_tce_put(stt, entry, tce);
576

577 578 579 580
unlock_exit:
	srcu_read_unlock(&vcpu->kvm->srcu, idx);

	return ret;
581 582 583
}
EXPORT_SYMBOL_GPL(kvmppc_h_put_tce);

584 585 586 587 588 589 590
long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
		unsigned long liobn, unsigned long ioba,
		unsigned long tce_list, unsigned long npages)
{
	struct kvmppc_spapr_tce_table *stt;
	long i, ret = H_SUCCESS, idx;
	unsigned long entry, ua = 0;
591 592
	u64 __user *tces;
	u64 tce;
593
	struct kvmppc_spapr_tce_iommu_table *stit;
594

595
	stt = kvmppc_find_table(vcpu->kvm, liobn);
596 597 598
	if (!stt)
		return H_TOO_HARD;

599
	entry = ioba >> stt->page_shift;
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	/*
	 * SPAPR spec says that the maximum size of the list is 512 TCEs
	 * so the whole table fits in 4K page
	 */
	if (npages > 512)
		return H_PARAMETER;

	if (tce_list & (SZ_4K - 1))
		return H_PARAMETER;

	ret = kvmppc_ioba_validate(stt, ioba, npages);
	if (ret != H_SUCCESS)
		return ret;

	idx = srcu_read_lock(&vcpu->kvm->srcu);
615
	if (kvmppc_tce_to_ua(vcpu->kvm, tce_list, &ua, NULL)) {
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
		ret = H_TOO_HARD;
		goto unlock_exit;
	}
	tces = (u64 __user *) ua;

	for (i = 0; i < npages; ++i) {
		if (get_user(tce, tces + i)) {
			ret = H_TOO_HARD;
			goto unlock_exit;
		}
		tce = be64_to_cpu(tce);

		ret = kvmppc_tce_validate(stt, tce);
		if (ret != H_SUCCESS)
			goto unlock_exit;
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
	}

	for (i = 0; i < npages; ++i) {
		/*
		 * This looks unsafe, because we validate, then regrab
		 * the TCE from userspace which could have been changed by
		 * another thread.
		 *
		 * But it actually is safe, because the relevant checks will be
		 * re-executed in the following code.  If userspace tries to
		 * change this dodgily it will result in a messier failure mode
		 * but won't threaten the host.
		 */
		if (get_user(tce, tces + i)) {
			ret = H_TOO_HARD;
			goto unlock_exit;
		}
		tce = be64_to_cpu(tce);
649

650
		if (kvmppc_tce_to_ua(vcpu->kvm, tce, &ua, NULL))
651 652 653
			return H_PARAMETER;

		list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
654
			ret = kvmppc_tce_iommu_map(vcpu->kvm, stt,
655 656 657
					stit->tbl, entry + i, ua,
					iommu_tce_direction(tce));

658 659
			if (ret != H_SUCCESS) {
				kvmppc_clear_tce(stit->tbl, entry);
660
				goto unlock_exit;
661
			}
662 663
		}

664 665 666 667 668 669 670 671 672
		kvmppc_tce_put(stt, entry + i, tce);
	}

unlock_exit:
	srcu_read_unlock(&vcpu->kvm->srcu, idx);

	return ret;
}
EXPORT_SYMBOL_GPL(kvmppc_h_put_tce_indirect);
673 674 675 676 677 678 679

long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
		unsigned long liobn, unsigned long ioba,
		unsigned long tce_value, unsigned long npages)
{
	struct kvmppc_spapr_tce_table *stt;
	long i, ret;
680
	struct kvmppc_spapr_tce_iommu_table *stit;
681

682
	stt = kvmppc_find_table(vcpu->kvm, liobn);
683 684 685 686 687 688 689 690 691 692 693
	if (!stt)
		return H_TOO_HARD;

	ret = kvmppc_ioba_validate(stt, ioba, npages);
	if (ret != H_SUCCESS)
		return ret;

	/* Check permission bits only to allow userspace poison TCE for debug */
	if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
		return H_PARAMETER;

694
	list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
695
		unsigned long entry = ioba >> stt->page_shift;
696 697

		for (i = 0; i < npages; ++i) {
698
			ret = kvmppc_tce_iommu_unmap(vcpu->kvm, stt,
699 700 701 702 703 704 705 706 707 708 709 710 711
					stit->tbl, entry + i);

			if (ret == H_SUCCESS)
				continue;

			if (ret == H_TOO_HARD)
				return ret;

			WARN_ON_ONCE(1);
			kvmppc_clear_tce(stit->tbl, entry);
		}
	}

712 713 714 715 716 717
	for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
		kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);

	return H_SUCCESS;
}
EXPORT_SYMBOL_GPL(kvmppc_h_stuff_tce);