1 /*
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>
38
39 #define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64))
40
41 /* WARNING: This will be called in real-mode on HV KVM and virtual
42 * mode on PR KVM
43 */
kvmppc_h_put_tce(struct kvm_vcpu * vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce)44 long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
45 unsigned long ioba, unsigned long tce)
46 {
47 struct kvm *kvm = vcpu->kvm;
48 struct kvmppc_spapr_tce_table *stt;
49
50 /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
51 /* liobn, ioba, tce); */
52
53 list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) {
54 if (stt->liobn == liobn) {
55 unsigned long idx = ioba >> SPAPR_TCE_SHIFT;
56 struct page *page;
57 u64 *tbl;
58
59 /* udbg_printf("H_PUT_TCE: liobn 0x%lx => stt=%p window_size=0x%x\n", */
60 /* liobn, stt, stt->window_size); */
61 if (ioba >= stt->window_size)
62 return H_PARAMETER;
63
64 page = stt->pages[idx / TCES_PER_PAGE];
65 tbl = (u64 *)page_address(page);
66
67 /* FIXME: Need to validate the TCE itself */
68 /* udbg_printf("tce @ %p\n", &tbl[idx % TCES_PER_PAGE]); */
69 tbl[idx % TCES_PER_PAGE] = tce;
70 return H_SUCCESS;
71 }
72 }
73
74 /* Didn't find the liobn, punt it to userspace */
75 return H_TOO_HARD;
76 }
77 EXPORT_SYMBOL_GPL(kvmppc_h_put_tce);
78
kvmppc_h_get_tce(struct kvm_vcpu * vcpu,unsigned long liobn,unsigned long ioba)79 long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
80 unsigned long ioba)
81 {
82 struct kvm *kvm = vcpu->kvm;
83 struct kvmppc_spapr_tce_table *stt;
84
85 list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) {
86 if (stt->liobn == liobn) {
87 unsigned long idx = ioba >> SPAPR_TCE_SHIFT;
88 struct page *page;
89 u64 *tbl;
90
91 if (ioba >= stt->window_size)
92 return H_PARAMETER;
93
94 page = stt->pages[idx / TCES_PER_PAGE];
95 tbl = (u64 *)page_address(page);
96
97 vcpu->arch.gpr[4] = tbl[idx % TCES_PER_PAGE];
98 return H_SUCCESS;
99 }
100 }
101
102 /* Didn't find the liobn, punt it to userspace */
103 return H_TOO_HARD;
104 }
105 EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);
106