blob: f29ba2c63e07a2ff4281b5339757f36d74049955 [file] [log] [blame]
David Gibson54738c02011-06-29 00:22:41 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
16 * Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com>
17 */
18
19#include <linux/types.h>
20#include <linux/string.h>
21#include <linux/kvm.h>
22#include <linux/kvm_host.h>
23#include <linux/highmem.h>
24#include <linux/gfp.h>
25#include <linux/slab.h>
26#include <linux/hugetlb.h>
27#include <linux/list.h>
28
29#include <asm/tlbflush.h>
30#include <asm/kvm_ppc.h>
31#include <asm/kvm_book3s.h>
32#include <asm/mmu-hash64.h>
33#include <asm/hvcall.h>
34#include <asm/synch.h>
35#include <asm/ppc-opcode.h>
36#include <asm/kvm_host.h>
37#include <asm/udbg.h>
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +110038#include <asm/iommu.h>
David Gibson54738c02011-06-29 00:22:41 +000039
40#define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64))
41
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +110042/*
43 * Finds a TCE table descriptor by LIOBN.
44 *
45 * WARNING: This will be called in real or virtual mode on HV KVM and virtual
46 * mode on PR KVM
47 */
48static struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm_vcpu *vcpu,
49 unsigned long liobn)
50{
51 struct kvm *kvm = vcpu->kvm;
52 struct kvmppc_spapr_tce_table *stt;
53
54 list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list)
55 if (stt->liobn == liobn)
56 return stt;
57
58 return NULL;
59}
60
61/*
62 * Validates IO address.
63 *
64 * WARNING: This will be called in real-mode on HV KVM and virtual
65 * mode on PR KVM
66 */
67static long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table *stt,
68 unsigned long ioba, unsigned long npages)
69{
70 unsigned long mask = (1ULL << IOMMU_PAGE_SHIFT_4K) - 1;
71 unsigned long idx = ioba >> IOMMU_PAGE_SHIFT_4K;
72 unsigned long size = stt->window_size >> IOMMU_PAGE_SHIFT_4K;
73
74 if ((ioba & mask) || (idx + npages > size) || (idx + npages < idx))
75 return H_PARAMETER;
76
77 return H_SUCCESS;
78}
79
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +000080/* WARNING: This will be called in real-mode on HV KVM and virtual
81 * mode on PR KVM
82 */
David Gibson54738c02011-06-29 00:22:41 +000083long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
84 unsigned long ioba, unsigned long tce)
85{
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +110086 struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn);
87 long ret;
88 unsigned long idx;
89 struct page *page;
90 u64 *tbl;
David Gibson54738c02011-06-29 00:22:41 +000091
92 /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
93 /* liobn, ioba, tce); */
94
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +110095 if (!stt)
96 return H_TOO_HARD;
David Gibson54738c02011-06-29 00:22:41 +000097
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +110098 ret = kvmppc_ioba_validate(stt, ioba, 1);
99 if (ret != H_SUCCESS)
100 return ret;
David Gibson54738c02011-06-29 00:22:41 +0000101
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +1100102 idx = ioba >> SPAPR_TCE_SHIFT;
103 page = stt->pages[idx / TCES_PER_PAGE];
104 tbl = (u64 *)page_address(page);
David Gibson54738c02011-06-29 00:22:41 +0000105
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +1100106 /* FIXME: Need to validate the TCE itself */
107 /* udbg_printf("tce @ %p\n", &tbl[idx % TCES_PER_PAGE]); */
108 tbl[idx % TCES_PER_PAGE] = tce;
David Gibson54738c02011-06-29 00:22:41 +0000109
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +1100110 return H_SUCCESS;
David Gibson54738c02011-06-29 00:22:41 +0000111}
Paul Mackerras066212e2013-10-07 22:17:50 +0530112EXPORT_SYMBOL_GPL(kvmppc_h_put_tce);
Laurent Dufour69e9fbb22014-02-21 16:31:10 +0100113
114long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
115 unsigned long ioba)
116{
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +1100117 struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn);
118 long ret;
119 unsigned long idx;
120 struct page *page;
121 u64 *tbl;
Laurent Dufour69e9fbb22014-02-21 16:31:10 +0100122
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +1100123 if (!stt)
124 return H_TOO_HARD;
Laurent Dufour69e9fbb22014-02-21 16:31:10 +0100125
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +1100126 ret = kvmppc_ioba_validate(stt, ioba, 1);
127 if (ret != H_SUCCESS)
128 return ret;
Laurent Dufour69e9fbb22014-02-21 16:31:10 +0100129
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +1100130 idx = ioba >> SPAPR_TCE_SHIFT;
131 page = stt->pages[idx / TCES_PER_PAGE];
132 tbl = (u64 *)page_address(page);
Laurent Dufour69e9fbb22014-02-21 16:31:10 +0100133
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +1100134 vcpu->arch.gpr[4] = tbl[idx % TCES_PER_PAGE];
Laurent Dufour69e9fbb22014-02-21 16:31:10 +0100135
Alexey Kardashevskiyfcbb2ce2016-02-15 12:55:04 +1100136 return H_SUCCESS;
Laurent Dufour69e9fbb22014-02-21 16:31:10 +0100137}
138EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);