blob: 05d72fc8b4784e1721a3b26705f51e1085eea527 [file] [log] [blame]
Hollis Blanchard9dd921c2008-11-05 09:36:14 -06001/*
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>
Hollis Blancharddb93f572008-11-05 09:36:18 -060021#include <linux/err.h>
22
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060023#include <asm/reg.h>
24#include <asm/cputable.h>
25#include <asm/tlbflush.h>
Hollis Blancharddb93f572008-11-05 09:36:18 -060026#include <asm/kvm_44x.h>
27#include <asm/kvm_ppc.h>
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060028
29#include "44x_tlb.h"
30
31/* Note: clearing MSR[DE] just means that the debug interrupt will not be
32 * delivered *immediately*. Instead, it simply sets the appropriate DBSR bits.
33 * If those DBSR bits are still set when MSR[DE] is re-enabled, the interrupt
34 * will be delivered as an "imprecise debug event" (which is indicated by
35 * DBSR[IDE].
36 */
37static void kvm44x_disable_debug_interrupts(void)
38{
39 mtmsr(mfmsr() & ~MSR_DE);
40}
41
42void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
43{
44 kvm44x_disable_debug_interrupts();
45
46 mtspr(SPRN_IAC1, vcpu->arch.host_iac[0]);
47 mtspr(SPRN_IAC2, vcpu->arch.host_iac[1]);
48 mtspr(SPRN_IAC3, vcpu->arch.host_iac[2]);
49 mtspr(SPRN_IAC4, vcpu->arch.host_iac[3]);
50 mtspr(SPRN_DBCR1, vcpu->arch.host_dbcr1);
51 mtspr(SPRN_DBCR2, vcpu->arch.host_dbcr2);
52 mtspr(SPRN_DBCR0, vcpu->arch.host_dbcr0);
53 mtmsr(vcpu->arch.host_msr);
54}
55
56void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
57{
58 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
59 u32 dbcr0 = 0;
60
61 vcpu->arch.host_msr = mfmsr();
62 kvm44x_disable_debug_interrupts();
63
64 /* Save host debug register state. */
65 vcpu->arch.host_iac[0] = mfspr(SPRN_IAC1);
66 vcpu->arch.host_iac[1] = mfspr(SPRN_IAC2);
67 vcpu->arch.host_iac[2] = mfspr(SPRN_IAC3);
68 vcpu->arch.host_iac[3] = mfspr(SPRN_IAC4);
69 vcpu->arch.host_dbcr0 = mfspr(SPRN_DBCR0);
70 vcpu->arch.host_dbcr1 = mfspr(SPRN_DBCR1);
71 vcpu->arch.host_dbcr2 = mfspr(SPRN_DBCR2);
72
73 /* set registers up for guest */
74
75 if (dbg->bp[0]) {
76 mtspr(SPRN_IAC1, dbg->bp[0]);
77 dbcr0 |= DBCR0_IAC1 | DBCR0_IDM;
78 }
79 if (dbg->bp[1]) {
80 mtspr(SPRN_IAC2, dbg->bp[1]);
81 dbcr0 |= DBCR0_IAC2 | DBCR0_IDM;
82 }
83 if (dbg->bp[2]) {
84 mtspr(SPRN_IAC3, dbg->bp[2]);
85 dbcr0 |= DBCR0_IAC3 | DBCR0_IDM;
86 }
87 if (dbg->bp[3]) {
88 mtspr(SPRN_IAC4, dbg->bp[3]);
89 dbcr0 |= DBCR0_IAC4 | DBCR0_IDM;
90 }
91
92 mtspr(SPRN_DBCR0, dbcr0);
93 mtspr(SPRN_DBCR1, 0);
94 mtspr(SPRN_DBCR2, 0);
95}
96
97void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
98{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060099}
100
101void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
102{
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600103 /* XXX Since every guest uses TS=1 TID=0/1 mappings, we can't leave any TLB
104 * entries around when we're descheduled, so we must completely flush the
105 * TLB of all guest mappings. On the other hand, if there is only one
106 * guest, this flush is completely unnecessary. */
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600107 _tlbia();
108}
109
110int kvmppc_core_check_processor_compat(void)
111{
112 int r;
113
114 if (strcmp(cur_cpu_spec->platform, "ppc440") == 0)
115 r = 0;
116 else
117 r = -ENOTSUPP;
118
119 return r;
120}
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600121
122int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
123{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600124 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
125 struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[0];
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600126 int i;
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600127
128 tlbe->tid = 0;
129 tlbe->word0 = PPC44x_TLB_16M | PPC44x_TLB_VALID;
130 tlbe->word1 = 0;
131 tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR;
132
133 tlbe++;
134 tlbe->tid = 0;
135 tlbe->word0 = 0xef600000 | PPC44x_TLB_4K | PPC44x_TLB_VALID;
136 tlbe->word1 = 0xef600000;
137 tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR
138 | PPC44x_TLB_I | PPC44x_TLB_G;
139
140 /* Since the guest can directly access the timebase, it must know the
141 * real timebase frequency. Accordingly, it must see the state of
142 * CCR1[TCS]. */
143 vcpu->arch.ccr1 = mfspr(SPRN_CCR1);
144
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600145 for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++)
146 vcpu_44x->shadow_refs[i].gtlb_index = -1;
147
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600148 return 0;
149}
150
151/* 'linear_address' is actually an encoding of AS|PID|EADDR . */
152int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
153 struct kvm_translation *tr)
154{
Hollis Blancharddb93f572008-11-05 09:36:18 -0600155 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600156 struct kvmppc_44x_tlbe *gtlbe;
157 int index;
158 gva_t eaddr;
159 u8 pid;
160 u8 as;
161
162 eaddr = tr->linear_address;
163 pid = (tr->linear_address >> 32) & 0xff;
164 as = (tr->linear_address >> 40) & 0x1;
165
166 index = kvmppc_44x_tlb_index(vcpu, eaddr, pid, as);
167 if (index == -1) {
168 tr->valid = 0;
169 return 0;
170 }
171
Hollis Blancharddb93f572008-11-05 09:36:18 -0600172 gtlbe = &vcpu_44x->guest_tlb[index];
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600173
174 tr->physical_address = tlb_xlate(gtlbe, eaddr);
175 /* XXX what does "writeable" and "usermode" even mean? */
176 tr->valid = 1;
177
178 return 0;
179}
Hollis Blancharddb93f572008-11-05 09:36:18 -0600180
181struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
182{
183 struct kvmppc_vcpu_44x *vcpu_44x;
184 struct kvm_vcpu *vcpu;
185 int err;
186
187 vcpu_44x = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
188 if (!vcpu_44x) {
189 err = -ENOMEM;
190 goto out;
191 }
192
193 vcpu = &vcpu_44x->vcpu;
194 err = kvm_vcpu_init(vcpu, kvm, id);
195 if (err)
196 goto free_vcpu;
197
198 return vcpu;
199
200free_vcpu:
201 kmem_cache_free(kvm_vcpu_cache, vcpu_44x);
202out:
203 return ERR_PTR(err);
204}
205
206void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
207{
208 struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
209
210 kvm_vcpu_uninit(vcpu);
211 kmem_cache_free(kvm_vcpu_cache, vcpu_44x);
212}
213
214static int kvmppc_44x_init(void)
215{
216 int r;
217
218 r = kvmppc_booke_init();
219 if (r)
220 return r;
221
222 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_44x), THIS_MODULE);
223}
224
225static void kvmppc_44x_exit(void)
226{
227 kvmppc_booke_exit();
228}
229
230module_init(kvmppc_44x_init);
231module_exit(kvmppc_44x_exit);