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 IBM Corp. 2008
16  *
17  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18  */
19 
20 #include <linux/kvm_host.h>
21 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/export.h>
24 
25 #include <asm/reg.h>
26 #include <asm/cputable.h>
27 #include <asm/tlbflush.h>
28 #include <asm/kvm_44x.h>
29 #include <asm/kvm_ppc.h>
30 
31 #include "44x_tlb.h"
32 
kvmppc_core_vcpu_load(struct kvm_vcpu * vcpu,int cpu)33 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
34 {
35 	kvmppc_44x_tlb_load(vcpu);
36 }
37 
kvmppc_core_vcpu_put(struct kvm_vcpu * vcpu)38 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
39 {
40 	kvmppc_44x_tlb_put(vcpu);
41 }
42 
kvmppc_core_check_processor_compat(void)43 int kvmppc_core_check_processor_compat(void)
44 {
45 	int r;
46 
47 	if (strncmp(cur_cpu_spec->platform, "ppc440", 6) == 0)
48 		r = 0;
49 	else
50 		r = -ENOTSUPP;
51 
52 	return r;
53 }
54 
kvmppc_core_vcpu_setup(struct kvm_vcpu * vcpu)55 int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
56 {
57 	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
58 	struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[0];
59 	int i;
60 
61 	tlbe->tid = 0;
62 	tlbe->word0 = PPC44x_TLB_16M | PPC44x_TLB_VALID;
63 	tlbe->word1 = 0;
64 	tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR;
65 
66 	tlbe++;
67 	tlbe->tid = 0;
68 	tlbe->word0 = 0xef600000 | PPC44x_TLB_4K | PPC44x_TLB_VALID;
69 	tlbe->word1 = 0xef600000;
70 	tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR
71 	              | PPC44x_TLB_I | PPC44x_TLB_G;
72 
73 	/* Since the guest can directly access the timebase, it must know the
74 	 * real timebase frequency. Accordingly, it must see the state of
75 	 * CCR1[TCS]. */
76 	/* XXX CCR1 doesn't exist on all 440 SoCs. */
77 	vcpu->arch.ccr1 = mfspr(SPRN_CCR1);
78 
79 	for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++)
80 		vcpu_44x->shadow_refs[i].gtlb_index = -1;
81 
82 	vcpu->arch.cpu_type = KVM_CPU_440;
83 
84 	return 0;
85 }
86 
87 /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
kvmppc_core_vcpu_translate(struct kvm_vcpu * vcpu,struct kvm_translation * tr)88 int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
89                                struct kvm_translation *tr)
90 {
91 	int index;
92 	gva_t eaddr;
93 	u8 pid;
94 	u8 as;
95 
96 	eaddr = tr->linear_address;
97 	pid = (tr->linear_address >> 32) & 0xff;
98 	as = (tr->linear_address >> 40) & 0x1;
99 
100 	index = kvmppc_44x_tlb_index(vcpu, eaddr, pid, as);
101 	if (index == -1) {
102 		tr->valid = 0;
103 		return 0;
104 	}
105 
106 	tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
107 	/* XXX what does "writeable" and "usermode" even mean? */
108 	tr->valid = 1;
109 
110 	return 0;
111 }
112 
kvmppc_core_get_sregs(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)113 void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
114 {
115 	kvmppc_get_sregs_ivor(vcpu, sregs);
116 }
117 
kvmppc_core_set_sregs(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)118 int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
119 {
120 	return kvmppc_set_sregs_ivor(vcpu, sregs);
121 }
122 
kvmppc_core_vcpu_create(struct kvm * kvm,unsigned int id)123 struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
124 {
125 	struct kvmppc_vcpu_44x *vcpu_44x;
126 	struct kvm_vcpu *vcpu;
127 	int err;
128 
129 	vcpu_44x = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
130 	if (!vcpu_44x) {
131 		err = -ENOMEM;
132 		goto out;
133 	}
134 
135 	vcpu = &vcpu_44x->vcpu;
136 	err = kvm_vcpu_init(vcpu, kvm, id);
137 	if (err)
138 		goto free_vcpu;
139 
140 	vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
141 	if (!vcpu->arch.shared)
142 		goto uninit_vcpu;
143 
144 	return vcpu;
145 
146 uninit_vcpu:
147 	kvm_vcpu_uninit(vcpu);
148 free_vcpu:
149 	kmem_cache_free(kvm_vcpu_cache, vcpu_44x);
150 out:
151 	return ERR_PTR(err);
152 }
153 
kvmppc_core_vcpu_free(struct kvm_vcpu * vcpu)154 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
155 {
156 	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
157 
158 	free_page((unsigned long)vcpu->arch.shared);
159 	kvm_vcpu_uninit(vcpu);
160 	kmem_cache_free(kvm_vcpu_cache, vcpu_44x);
161 }
162 
kvmppc_44x_init(void)163 static int __init kvmppc_44x_init(void)
164 {
165 	int r;
166 
167 	r = kvmppc_booke_init();
168 	if (r)
169 		return r;
170 
171 	return kvm_init(NULL, sizeof(struct kvmppc_vcpu_44x), 0, THIS_MODULE);
172 }
173 
kvmppc_44x_exit(void)174 static void __exit kvmppc_44x_exit(void)
175 {
176 	kvmppc_booke_exit();
177 }
178 
179 module_init(kvmppc_44x_init);
180 module_exit(kvmppc_44x_exit);
181