2011-06-29 08:22:41 +08:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
#include <linux/hugetlb.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
|
|
|
|
#include <asm/tlbflush.h>
|
|
|
|
#include <asm/kvm_ppc.h>
|
|
|
|
#include <asm/kvm_book3s.h>
|
|
|
|
#include <asm/mmu-hash64.h>
|
|
|
|
#include <asm/hvcall.h>
|
|
|
|
#include <asm/synch.h>
|
|
|
|
#include <asm/ppc-opcode.h>
|
|
|
|
#include <asm/kvm_host.h>
|
|
|
|
#include <asm/udbg.h>
|
2016-02-15 09:55:04 +08:00
|
|
|
#include <asm/iommu.h>
|
2011-06-29 08:22:41 +08:00
|
|
|
|
|
|
|
#define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64))
|
|
|
|
|
2016-02-15 09:55:04 +08:00
|
|
|
/*
|
|
|
|
* Finds a TCE table descriptor by LIOBN.
|
|
|
|
*
|
|
|
|
* WARNING: This will be called in real or virtual mode on HV KVM and virtual
|
|
|
|
* mode on PR KVM
|
|
|
|
*/
|
|
|
|
static struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm_vcpu *vcpu,
|
|
|
|
unsigned long liobn)
|
|
|
|
{
|
|
|
|
struct kvm *kvm = vcpu->kvm;
|
|
|
|
struct kvmppc_spapr_tce_table *stt;
|
|
|
|
|
|
|
|
list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list)
|
|
|
|
if (stt->liobn == liobn)
|
|
|
|
return stt;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validates IO address.
|
|
|
|
*
|
|
|
|
* WARNING: This will be called in real-mode on HV KVM and virtual
|
|
|
|
* mode on PR KVM
|
|
|
|
*/
|
|
|
|
static long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table *stt,
|
|
|
|
unsigned long ioba, unsigned long npages)
|
|
|
|
{
|
|
|
|
unsigned long mask = (1ULL << IOMMU_PAGE_SHIFT_4K) - 1;
|
|
|
|
unsigned long idx = ioba >> IOMMU_PAGE_SHIFT_4K;
|
|
|
|
unsigned long size = stt->window_size >> IOMMU_PAGE_SHIFT_4K;
|
|
|
|
|
|
|
|
if ((ioba & mask) || (idx + npages > size) || (idx + npages < idx))
|
|
|
|
return H_PARAMETER;
|
|
|
|
|
|
|
|
return H_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2012-03-16 05:58:34 +08:00
|
|
|
/* WARNING: This will be called in real-mode on HV KVM and virtual
|
|
|
|
* mode on PR KVM
|
|
|
|
*/
|
2011-06-29 08:22:41 +08:00
|
|
|
long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
|
|
|
|
unsigned long ioba, unsigned long tce)
|
|
|
|
{
|
2016-02-15 09:55:04 +08:00
|
|
|
struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn);
|
|
|
|
long ret;
|
|
|
|
unsigned long idx;
|
|
|
|
struct page *page;
|
|
|
|
u64 *tbl;
|
2011-06-29 08:22:41 +08:00
|
|
|
|
|
|
|
/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
|
|
|
|
/* liobn, ioba, tce); */
|
|
|
|
|
2016-02-15 09:55:04 +08:00
|
|
|
if (!stt)
|
|
|
|
return H_TOO_HARD;
|
|
|
|
|
|
|
|
ret = kvmppc_ioba_validate(stt, ioba, 1);
|
|
|
|
if (ret != H_SUCCESS)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
idx = ioba >> SPAPR_TCE_SHIFT;
|
|
|
|
page = stt->pages[idx / TCES_PER_PAGE];
|
|
|
|
tbl = (u64 *)page_address(page);
|
|
|
|
|
|
|
|
/* FIXME: Need to validate the TCE itself */
|
|
|
|
/* udbg_printf("tce @ %p\n", &tbl[idx % TCES_PER_PAGE]); */
|
|
|
|
tbl[idx % TCES_PER_PAGE] = tce;
|
|
|
|
|
|
|
|
return H_SUCCESS;
|
2011-06-29 08:22:41 +08:00
|
|
|
}
|
2013-10-08 00:47:50 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kvmppc_h_put_tce);
|
2014-02-21 23:31:10 +08:00
|
|
|
|
|
|
|
long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
|
|
|
|
unsigned long ioba)
|
|
|
|
{
|
2016-02-15 09:55:04 +08:00
|
|
|
struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn);
|
|
|
|
long ret;
|
|
|
|
unsigned long idx;
|
|
|
|
struct page *page;
|
|
|
|
u64 *tbl;
|
2014-02-21 23:31:10 +08:00
|
|
|
|
2016-02-15 09:55:04 +08:00
|
|
|
if (!stt)
|
|
|
|
return H_TOO_HARD;
|
2014-02-21 23:31:10 +08:00
|
|
|
|
2016-02-15 09:55:04 +08:00
|
|
|
ret = kvmppc_ioba_validate(stt, ioba, 1);
|
|
|
|
if (ret != H_SUCCESS)
|
|
|
|
return ret;
|
2014-02-21 23:31:10 +08:00
|
|
|
|
2016-02-15 09:55:04 +08:00
|
|
|
idx = ioba >> SPAPR_TCE_SHIFT;
|
|
|
|
page = stt->pages[idx / TCES_PER_PAGE];
|
|
|
|
tbl = (u64 *)page_address(page);
|
2014-02-21 23:31:10 +08:00
|
|
|
|
2016-02-15 09:55:04 +08:00
|
|
|
vcpu->arch.gpr[4] = tbl[idx % TCES_PER_PAGE];
|
2014-02-21 23:31:10 +08:00
|
|
|
|
2016-02-15 09:55:04 +08:00
|
|
|
return H_SUCCESS;
|
2014-02-21 23:31:10 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);
|