blob: 91d41fabc5b00767f882663cd8737c316eb64dbd [file] [log] [blame]
Alexander Graf4e3420252009-10-30 05:47:05 +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 SUSE Linux Products GmbH 2009
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#ifndef __ASM_KVM_BOOK3S_H__
21#define __ASM_KVM_BOOK3S_H__
22
23#include <linux/types.h>
24#include <linux/kvm_host.h>
Alexander Graf2191d652010-04-16 00:11:32 +020025#include <asm/kvm_book3s_asm.h>
Alexander Graf4e3420252009-10-30 05:47:05 +000026
Alexander Graf4e3420252009-10-30 05:47:05 +000027struct kvmppc_bat {
Alexander Grafe15a1132009-11-30 03:02:02 +000028 u64 raw;
Alexander Graf4e3420252009-10-30 05:47:05 +000029 u32 bepi;
30 u32 bepi_mask;
Alexander Graf4e3420252009-10-30 05:47:05 +000031 u32 brpn;
32 u8 wimg;
33 u8 pp;
Alexander Graf3ed9c6d2010-03-24 21:48:36 +010034 bool vs : 1;
35 bool vp : 1;
Alexander Graf4e3420252009-10-30 05:47:05 +000036};
37
38struct kvmppc_sid_map {
39 u64 guest_vsid;
40 u64 guest_esid;
41 u64 host_vsid;
Alexander Graf3ed9c6d2010-03-24 21:48:36 +010042 bool valid : 1;
Alexander Graf4e3420252009-10-30 05:47:05 +000043};
44
45#define SID_MAP_BITS 9
46#define SID_MAP_NUM (1 << SID_MAP_BITS)
47#define SID_MAP_MASK (SID_MAP_NUM - 1)
48
Alexander Graf8b6db3b2010-08-15 08:04:24 +020049#ifdef CONFIG_PPC_BOOK3S_64
50#define SID_CONTEXTS 1
51#else
52#define SID_CONTEXTS 128
53#define VSID_POOL_SIZE (SID_CONTEXTS * 16)
54#endif
55
Paul Mackerrasc4befc52011-06-29 00:17:33 +000056struct hpte_cache {
57 struct hlist_node list_pte;
58 struct hlist_node list_pte_long;
59 struct hlist_node list_vpte;
60 struct hlist_node list_vpte_long;
61 struct rcu_head rcu_head;
62 u64 host_va;
63 u64 pfn;
64 ulong slot;
65 struct kvmppc_pte pte;
66};
67
Alexander Graf4e3420252009-10-30 05:47:05 +000068struct kvmppc_vcpu_book3s {
69 struct kvm_vcpu vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +020070 struct kvmppc_book3s_shadow_vcpu *shadow_vcpu;
Alexander Graf4e3420252009-10-30 05:47:05 +000071 struct kvmppc_sid_map sid_map[SID_MAP_NUM];
Alexander Graf4e3420252009-10-30 05:47:05 +000072 struct {
73 u64 esid;
74 u64 vsid;
75 } slb_shadow[64];
76 u8 slb_shadow_max;
Alexander Graf4e3420252009-10-30 05:47:05 +000077 struct kvmppc_bat ibat[8];
78 struct kvmppc_bat dbat[8];
79 u64 hid[6];
Alexander Grafd6d549b2010-02-19 11:00:33 +010080 u64 gqr[8];
Alexander Graf4e3420252009-10-30 05:47:05 +000081 u64 sdr1;
Alexander Graf4e3420252009-10-30 05:47:05 +000082 u64 hior;
83 u64 msr_mask;
Alexander Graf4e3420252009-10-30 05:47:05 +000084 u64 vsid_next;
Alexander Graf8b6db3b2010-08-15 08:04:24 +020085#ifdef CONFIG_PPC_BOOK3S_32
86 u32 vsid_pool[VSID_POOL_SIZE];
87#else
88 u64 vsid_first;
Alexander Graf4e3420252009-10-30 05:47:05 +000089 u64 vsid_max;
Alexander Graf8b6db3b2010-08-15 08:04:24 +020090#endif
91 int context_id[SID_CONTEXTS];
Paul Mackerrasc4befc52011-06-29 00:17:33 +000092
Alexander Grafa15bd352011-08-08 17:17:09 +020093 bool hior_sregs; /* HIOR is set by SREGS, not PVR */
94
Paul Mackerrasc4befc52011-06-29 00:17:33 +000095 struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE];
96 struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG];
97 struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
98 struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
99 int hpte_cache_count;
100 spinlock_t mmu_lock;
Alexander Graf4e3420252009-10-30 05:47:05 +0000101};
102
103#define CONTEXT_HOST 0
104#define CONTEXT_GUEST 1
105#define CONTEXT_GUEST_END 2
106
Alexander Graff7bc74e2010-04-20 02:49:48 +0200107#define VSID_REAL 0x1fffffffffc00000ULL
108#define VSID_BAT 0x1fffffffffb00000ULL
109#define VSID_REAL_DR 0x2000000000000000ULL
110#define VSID_REAL_IR 0x4000000000000000ULL
Alexander Graf5a1b4192010-03-24 21:48:35 +0100111#define VSID_PR 0x8000000000000000ULL
Alexander Graf4e3420252009-10-30 05:47:05 +0000112
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200113extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask);
Alexander Graf4e3420252009-10-30 05:47:05 +0000114extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200115extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end);
Alexander Graf4e3420252009-10-30 05:47:05 +0000116extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000117extern void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr);
Alexander Graf4e3420252009-10-30 05:47:05 +0000118extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
119extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000120extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu);
Alexander Graf4e3420252009-10-30 05:47:05 +0000121extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
122extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
123extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
Alexander Graffef093be2010-06-30 15:18:46 +0200124
125extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
126extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
127extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
128extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
129extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
130extern int kvmppc_mmu_hpte_sysinit(void);
131extern void kvmppc_mmu_hpte_sysexit(void);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000132extern int kvmppc_mmu_hv_init(void);
Alexander Graffef093be2010-06-30 15:18:46 +0200133
Alexander Graf5467a972010-02-19 11:00:38 +0100134extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
135extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
Alexander Graf4e3420252009-10-30 05:47:05 +0000136extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000137extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
Alexander Grafe15a1132009-11-30 03:02:02 +0000138extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
139 bool upper, u32 val);
Alexander Grafaba3bd72010-02-19 11:00:39 +0100140extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
Alexander Graf831317b2010-02-19 11:00:44 +0100141extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
Alexander Grafe8508942010-07-29 14:47:54 +0200142extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
Alexander Graf4e3420252009-10-30 05:47:05 +0000143
Alexander Grafa22a2da2011-06-07 20:45:34 +0200144extern void kvmppc_handler_lowmem_trampoline(void);
145extern void kvmppc_handler_trampoline_enter(void);
Alexander Graf021ec9c2010-01-08 02:58:06 +0100146extern void kvmppc_rmcall(ulong srr0, ulong srr1);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000147extern void kvmppc_hv_entry_trampoline(void);
Alexander Grafd5e52812010-01-15 14:49:10 +0100148extern void kvmppc_load_up_fpu(void);
149extern void kvmppc_load_up_altivec(void);
150extern void kvmppc_load_up_vsx(void);
Alexander Grafca7f4202010-03-24 21:48:28 +0100151extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
152extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
Alexander Graf0254f0742011-08-08 17:21:15 +0200153extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
Alexander Graf4e3420252009-10-30 05:47:05 +0000154
155static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
156{
157 return container_of(vcpu, struct kvmppc_vcpu_book3s, vcpu);
158}
159
Paul Mackerrasde56a942011-06-29 00:21:34 +0000160extern void kvm_return_point(void);
161
162/* Also add subarch specific defines */
163
164#ifdef CONFIG_KVM_BOOK3S_32_HANDLER
165#include <asm/kvm_book3s_32.h>
166#endif
167#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
168#include <asm/kvm_book3s_64.h>
169#endif
170
171#ifdef CONFIG_KVM_BOOK3S_PR
172
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000173static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
174{
175 return to_book3s(vcpu)->hior;
176}
177
178static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
179 unsigned long pending_now, unsigned long old_pending)
180{
181 if (pending_now)
182 vcpu->arch.shared->int_pending = 1;
183 else if (old_pending)
184 vcpu->arch.shared->int_pending = 0;
185}
186
Alexander Grafc7f38f42010-04-16 00:11:40 +0200187static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
188{
189 if ( num < 14 ) {
190 to_svcpu(vcpu)->gpr[num] = val;
191 to_book3s(vcpu)->shadow_vcpu->gpr[num] = val;
192 } else
193 vcpu->arch.gpr[num] = val;
194}
195
196static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
197{
198 if ( num < 14 )
199 return to_svcpu(vcpu)->gpr[num];
200 else
201 return vcpu->arch.gpr[num];
202}
203
204static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
205{
206 to_svcpu(vcpu)->cr = val;
207 to_book3s(vcpu)->shadow_vcpu->cr = val;
208}
209
210static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
211{
212 return to_svcpu(vcpu)->cr;
213}
214
215static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
216{
217 to_svcpu(vcpu)->xer = val;
218 to_book3s(vcpu)->shadow_vcpu->xer = val;
219}
220
221static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
222{
223 return to_svcpu(vcpu)->xer;
224}
225
226static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
227{
228 to_svcpu(vcpu)->ctr = val;
229}
230
231static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
232{
233 return to_svcpu(vcpu)->ctr;
234}
235
236static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
237{
238 to_svcpu(vcpu)->lr = val;
239}
240
241static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
242{
243 return to_svcpu(vcpu)->lr;
244}
245
246static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
247{
248 to_svcpu(vcpu)->pc = val;
249}
250
251static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
252{
253 return to_svcpu(vcpu)->pc;
254}
255
256static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
257{
258 ulong pc = kvmppc_get_pc(vcpu);
259 struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);
260
261 /* Load the instruction manually if it failed to do so in the
262 * exit path */
263 if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
264 kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
265
266 return svcpu->last_inst;
267}
268
269static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
270{
271 return to_svcpu(vcpu)->fault_dar;
272}
Alexander Graf4e3420252009-10-30 05:47:05 +0000273
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000274static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
275{
276 ulong crit_raw = vcpu->arch.shared->critical;
277 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
278 bool crit;
279
280 /* Truncate crit indicators in 32 bit mode */
281 if (!(vcpu->arch.shared->msr & MSR_SF)) {
282 crit_raw &= 0xffffffff;
283 crit_r1 &= 0xffffffff;
284 }
285
286 /* Critical section when crit == r1 */
287 crit = (crit_raw == crit_r1);
288 /* ... and we're in supervisor mode */
289 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
290
291 return crit;
292}
Paul Mackerrasde56a942011-06-29 00:21:34 +0000293#else /* CONFIG_KVM_BOOK3S_PR */
294
295static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
296{
297 return 0;
298}
299
300static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
301 unsigned long pending_now, unsigned long old_pending)
302{
Paul Mackerrasde56a942011-06-29 00:21:34 +0000303}
304
305static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
306{
307 vcpu->arch.gpr[num] = val;
308}
309
310static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
311{
312 return vcpu->arch.gpr[num];
313}
314
315static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
316{
317 vcpu->arch.cr = val;
318}
319
320static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
321{
322 return vcpu->arch.cr;
323}
324
325static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
326{
327 vcpu->arch.xer = val;
328}
329
330static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
331{
332 return vcpu->arch.xer;
333}
334
335static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
336{
337 vcpu->arch.ctr = val;
338}
339
340static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
341{
342 return vcpu->arch.ctr;
343}
344
345static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
346{
347 vcpu->arch.lr = val;
348}
349
350static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
351{
352 return vcpu->arch.lr;
353}
354
355static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
356{
357 vcpu->arch.pc = val;
358}
359
360static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
361{
362 return vcpu->arch.pc;
363}
364
365static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
366{
367 ulong pc = kvmppc_get_pc(vcpu);
368
369 /* Load the instruction manually if it failed to do so in the
370 * exit path */
371 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
372 kvmppc_ld(vcpu, &pc, sizeof(u32), &vcpu->arch.last_inst, false);
373
374 return vcpu->arch.last_inst;
375}
376
377static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
378{
379 return vcpu->arch.fault_dar;
380}
381
382static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
383{
384 return false;
385}
386#endif
Paul Mackerrasf05ed4d2011-06-29 00:17:58 +0000387
Alexander Grafdb507c32011-07-08 13:40:10 +0200388static inline unsigned long compute_tlbie_rb(unsigned long v, unsigned long r,
389 unsigned long pte_index)
390{
391 unsigned long rb, va_low;
392
393 rb = (v & ~0x7fUL) << 16; /* AVA field */
394 va_low = pte_index >> 3;
395 if (v & HPTE_V_SECONDARY)
396 va_low = ~va_low;
397 /* xor vsid from AVA */
398 if (!(v & HPTE_V_1TB_SEG))
399 va_low ^= v >> 12;
400 else
401 va_low ^= v >> 24;
402 va_low &= 0x7ff;
403 if (v & HPTE_V_LARGE) {
404 rb |= 1; /* L field */
405 if (cpu_has_feature(CPU_FTR_ARCH_206) &&
406 (r & 0xff000)) {
407 /* non-16MB large page, must be 64k */
408 /* (masks depend on page size) */
409 rb |= 0x1000; /* page encoding in LP field */
410 rb |= (va_low & 0x7f) << 16; /* 7b of VA in AVA/LP field */
411 rb |= (va_low & 0xfe); /* AVAL field (P7 doesn't seem to care) */
412 }
413 } else {
414 /* 4kB page */
415 rb |= (va_low & 0x7ff) << 12; /* remaining 11b of VA */
416 }
417 rb |= (v >> 54) & 0x300; /* B field */
418 return rb;
419}
420
Alexander Grafad0a0482010-03-24 21:48:30 +0100421/* Magic register values loaded into r3 and r4 before the 'sc' assembly
422 * instruction for the OSI hypercalls */
423#define OSI_SC_MAGIC_R3 0x113724FA
424#define OSI_SC_MAGIC_R4 0x77810F9B
425
Alexander Graf4e3420252009-10-30 05:47:05 +0000426#define INS_DCBZ 0x7c0007ec
427
Alexander Graf4e3420252009-10-30 05:47:05 +0000428#endif /* __ASM_KVM_BOOK3S_H__ */