blob: e38136304c1f99fa3ceebda40f1f8fafd855dab7 [file] [log] [blame]
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001/*
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#ifndef __POWERPC_KVM_PPC_H__
21#define __POWERPC_KVM_PPC_H__
22
23/* This file exists just so we can dereference kvm_vcpu, avoiding nested header
24 * dependencies. */
25
26#include <linux/mutex.h>
27#include <linux/timer.h>
28#include <linux/types.h>
29#include <linux/kvm_types.h>
30#include <linux/kvm_host.h>
Paul Mackerrasa136a8b2012-09-25 20:31:56 +000031#include <linux/bug.h>
Alexander Graf1c0006d2010-01-15 14:49:12 +010032#ifdef CONFIG_PPC_BOOK3S
33#include <asm/kvm_book3s.h>
Alexander Grafc7f38f42010-04-16 00:11:40 +020034#else
35#include <asm/kvm_booke.h>
Alexander Graf1c0006d2010-01-15 14:49:12 +010036#endif
Paul Mackerras371fefd2011-06-29 00:23:08 +000037#ifdef CONFIG_KVM_BOOK3S_64_HANDLER
38#include <asm/paca.h>
39#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050040
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050041enum emulation_result {
42 EMULATE_DONE, /* no further processing */
43 EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */
44 EMULATE_DO_DCR, /* kvm_run filled with DCR request */
45 EMULATE_FAIL, /* can't emulate this instruction */
Alexander Graf37f5bca2010-02-19 11:00:31 +010046 EMULATE_AGAIN, /* something went wrong. go again */
Bharat Bhushanc402a3f2013-04-08 00:32:13 +000047 EMULATE_EXIT_USER, /* emulation requires exit to user-space */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050048};
49
Mihai Caraman51f04722014-07-23 19:06:21 +030050enum instruction_type {
51 INST_GENERIC,
52 INST_SC, /* system call */
53};
54
Paul Mackerrasdf6909e52011-06-29 00:19:50 +000055extern int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050056extern int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Alexander Graf29eb61b2009-10-30 05:47:07 +000057extern void kvmppc_handler_highmem(void);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050058
59extern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu);
60extern int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
61 unsigned int rt, unsigned int bytes,
Cédric Le Goater73601772014-01-09 11:51:16 +010062 int is_default_endian);
Alexander Graf3587d532010-02-19 11:00:30 +010063extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
64 unsigned int rt, unsigned int bytes,
Cédric Le Goater73601772014-01-09 11:51:16 +010065 int is_default_endian);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050066extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
Cédric Le Goater73601772014-01-09 11:51:16 +010067 u64 val, unsigned int bytes,
68 int is_default_endian);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050069
Mihai Caraman51f04722014-07-23 19:06:21 +030070extern int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
71 enum instruction_type type, u32 *inst);
72
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050073extern int kvmppc_emulate_instruction(struct kvm_run *run,
74 struct kvm_vcpu *vcpu);
Hollis Blanchardce263d72008-05-21 18:22:51 -050075extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
Hollis Blanchard75f74f02008-11-05 09:36:16 -060076extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -050077extern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb);
Scott Wooddfd4d472011-11-17 12:39:59 +000078extern void kvmppc_decrementer_func(unsigned long data);
Alexander Grafaf8f38b2011-08-10 13:57:08 +020079extern int kvmppc_sanity_check(struct kvm_vcpu *vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +000080extern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu);
81extern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050082
Hollis Blanchardecc09812009-01-03 16:22:59 -060083/* Core-specific hooks */
84
Hollis Blanchard89168612008-12-02 15:51:53 -060085extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
Hollis Blanchard7924bd42008-12-02 15:51:55 -060086 unsigned int gtlb_idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050087extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
Hollis Blanchard49dd2c42008-07-25 13:54:53 -050088extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
Hollis Blanchardecc09812009-01-03 16:22:59 -060089extern void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu);
Alexander Graf9cc5e952010-04-16 00:11:45 +020090extern int kvmppc_mmu_init(struct kvm_vcpu *vcpu);
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -060091extern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
92extern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -060093extern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
94 gva_t eaddr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -060095extern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu);
96extern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060097
Hollis Blancharddb93f572008-11-05 09:36:18 -060098extern struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm,
99 unsigned int id);
100extern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu);
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600101extern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600102extern int kvmppc_core_check_processor_compat(void);
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600103extern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
104 struct kvm_translation *tr);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600105
106extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
107extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
108
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000109extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600110extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
Alexander Graf25a8a022010-01-08 02:58:07 +0100111extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600112extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
Alexander Graf7706664d2009-12-21 20:21:24 +0100113extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600114extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
115 struct kvm_interrupt *irq);
Paul Mackerras4fe27d22013-02-14 14:00:25 +0000116extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200117extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
Alexander Graf7c973a22012-08-13 12:50:35 +0200118extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600119
Hollis Blancharddb93f572008-11-05 09:36:18 -0600120extern int kvmppc_booke_init(void);
121extern void kvmppc_booke_exit(void);
122
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600123extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
Alexander Graf2a342ed2010-07-29 14:47:48 +0200124extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
Scott Wooda4cd8b22011-06-14 18:34:41 -0500125extern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600126
Paul Mackerras32fad282012-05-04 02:32:53 +0000127extern long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp);
128extern long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000129extern void kvmppc_free_hpt(struct kvm *kvm);
130extern long kvmppc_prepare_vrma(struct kvm *kvm,
131 struct kvm_userspace_memory_region *mem);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000132extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000133 struct kvm_memory_slot *memslot, unsigned long porder);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000134extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000135
David Gibson54738c02011-06-29 00:22:41 +0000136extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
137 struct kvm_create_spapr_tce *args);
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000138extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
139 unsigned long ioba, unsigned long tce);
Laurent Dufour69e9fbb22014-02-21 16:31:10 +0100140extern long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
141 unsigned long ioba);
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +0530142extern struct kvm_rma_info *kvm_alloc_rma(void);
143extern void kvm_release_rma(struct kvm_rma_info *ri);
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +0530144extern struct page *kvm_alloc_hpt(unsigned long nr_pages);
145extern void kvm_release_hpt(struct page *page, unsigned long nr_pages);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000146extern int kvmppc_core_init_vm(struct kvm *kvm);
147extern void kvmppc_core_destroy_vm(struct kvm *kvm);
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530148extern void kvmppc_core_free_memslot(struct kvm *kvm,
149 struct kvm_memory_slot *free,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000150 struct kvm_memory_slot *dont);
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530151extern int kvmppc_core_create_memslot(struct kvm *kvm,
152 struct kvm_memory_slot *slot,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000153 unsigned long npages);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000154extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000155 struct kvm_memory_slot *memslot,
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000156 struct kvm_userspace_memory_region *mem);
157extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +0000158 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +0900159 const struct kvm_memory_slot *old);
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +0000160extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
161 struct kvm_ppc_smmu_info *info);
Paul Mackerrasdfe49db2012-09-11 13:28:18 +0000162extern void kvmppc_core_flush_memslot(struct kvm *kvm,
163 struct kvm_memory_slot *memslot);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000164
Scott Woodd30f6e42011-12-20 15:34:43 +0000165extern int kvmppc_bookehv_init(void);
166extern void kvmppc_bookehv_exit(void);
167
Alexander Graf03d25c52012-08-10 12:28:50 +0200168extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
169
Paul Mackerrasa2932922012-11-19 22:57:20 +0000170extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
171
Scott Wood5df554ad2013-04-12 14:08:46 +0000172int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
173
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000174extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
175extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
176extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000177extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server,
178 u32 priority);
179extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server,
180 u32 *priority);
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000181extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
182extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000183
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530184union kvmppc_one_reg {
185 u32 wval;
186 u64 dval;
187 vector128 vval;
188 u64 vsxval[2];
189 struct {
190 u64 addr;
191 u64 length;
192 } vpaval;
193};
194
195struct kvmppc_ops {
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530196 struct module *owner;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530197 int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
198 int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
199 int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id,
200 union kvmppc_one_reg *val);
201 int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id,
202 union kvmppc_one_reg *val);
203 void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
204 void (*vcpu_put)(struct kvm_vcpu *vcpu);
205 void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr);
206 int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu);
207 struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned int id);
208 void (*vcpu_free)(struct kvm_vcpu *vcpu);
209 int (*check_requests)(struct kvm_vcpu *vcpu);
210 int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log);
211 void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot);
212 int (*prepare_memory_region)(struct kvm *kvm,
213 struct kvm_memory_slot *memslot,
214 struct kvm_userspace_memory_region *mem);
215 void (*commit_memory_region)(struct kvm *kvm,
216 struct kvm_userspace_memory_region *mem,
217 const struct kvm_memory_slot *old);
218 int (*unmap_hva)(struct kvm *kvm, unsigned long hva);
219 int (*unmap_hva_range)(struct kvm *kvm, unsigned long start,
220 unsigned long end);
221 int (*age_hva)(struct kvm *kvm, unsigned long hva);
222 int (*test_age_hva)(struct kvm *kvm, unsigned long hva);
223 void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte);
224 void (*mmu_destroy)(struct kvm_vcpu *vcpu);
225 void (*free_memslot)(struct kvm_memory_slot *free,
226 struct kvm_memory_slot *dont);
227 int (*create_memslot)(struct kvm_memory_slot *slot,
228 unsigned long npages);
229 int (*init_vm)(struct kvm *kvm);
230 void (*destroy_vm)(struct kvm *kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530231 int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info);
232 int (*emulate_op)(struct kvm_run *run, struct kvm_vcpu *vcpu,
233 unsigned int inst, int *advance);
234 int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val);
235 int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val);
236 void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu);
237 long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl,
238 unsigned long arg);
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000239 int (*hcall_implemented)(unsigned long hcall);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530240};
241
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530242extern struct kvmppc_ops *kvmppc_hv_ops;
243extern struct kvmppc_ops *kvmppc_pr_ops;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530244
Mihai Caraman51f04722014-07-23 19:06:21 +0300245static inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu,
246 enum instruction_type type, u32 *inst)
247{
248 int ret = EMULATE_DONE;
249 u32 fetched_inst;
250
251 /* Load the instruction manually if it failed to do so in the
252 * exit path */
253 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
254 ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst);
255
256 /* Write fetch_failed unswapped if the fetch failed */
257 if (ret == EMULATE_DONE)
258 fetched_inst = kvmppc_need_byteswap(vcpu) ?
259 swab32(vcpu->arch.last_inst) :
260 vcpu->arch.last_inst;
261 else
262 fetched_inst = vcpu->arch.last_inst;
263
264 *inst = fetched_inst;
265 return ret;
266}
267
Aneesh Kumar K.Va78b55d2013-10-07 22:18:02 +0530268static inline bool is_kvmppc_hv_enabled(struct kvm *kvm)
269{
270 return kvm->arch.kvm_ops == kvmppc_hv_ops;
271}
272
Alexander Graf0564ee82010-02-19 11:00:42 +0100273/*
274 * Cuts out inst bits with ordering according to spec.
275 * That means the leftmost bit is zero. All given bits are included.
276 */
277static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
278{
279 u32 r;
280 u32 mask;
281
282 BUG_ON(msb > lsb);
283
284 mask = (1 << (lsb - msb + 1)) - 1;
285 r = (inst >> (63 - lsb)) & mask;
286
287 return r;
288}
289
290/*
291 * Replaces inst bits with ordering according to spec.
292 */
293static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
294{
295 u32 r;
296 u32 mask;
297
298 BUG_ON(msb > lsb);
299
300 mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
301 r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
302
303 return r;
304}
305
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000306#define one_reg_size(id) \
307 (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
308
309#define get_reg_val(id, reg) ({ \
310 union kvmppc_one_reg __u; \
311 switch (one_reg_size(id)) { \
312 case 4: __u.wval = (reg); break; \
313 case 8: __u.dval = (reg); break; \
314 default: BUG(); \
315 } \
316 __u; \
317})
318
319
320#define set_reg_val(id, val) ({ \
321 u64 __v; \
322 switch (one_reg_size(id)) { \
323 case 4: __v = (val).wval; break; \
324 case 8: __v = (val).dval; break; \
325 default: BUG(); \
326 } \
327 __v; \
328})
329
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530330int kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
Scott Wood5ce941e2011-04-27 17:24:21 -0500331int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
332
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530333int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
Scott Wood5ce941e2011-04-27 17:24:21 -0500334int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
335
Paul Mackerras31f34382011-12-12 12:26:50 +0000336int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
337int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000338int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
339int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
Paul Mackerras31f34382011-12-12 12:26:50 +0000340
Scott Wood5ce941e2011-04-27 17:24:21 -0500341void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
342
Scott Wood5df554ad2013-04-12 14:08:46 +0000343struct openpic;
Scott Wood5df554ad2013-04-12 14:08:46 +0000344
Aneesh Kumar K.V9975f5e2013-10-07 22:17:52 +0530345#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +0530346extern void kvm_cma_reserve(void) __init;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000347static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
348{
349 paca[cpu].kvm_hstate.xics_phys = addr;
350}
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000351
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000352static inline u32 kvmppc_get_xics_latch(void)
353{
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530354 u32 xirr;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000355
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530356 xirr = get_paca()->kvm_hstate.saved_xirr;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000357 get_paca()->kvm_hstate.saved_xirr = 0;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000358 return xirr;
359}
360
361static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
362{
363 paca[cpu].kvm_hstate.host_ipi = host_ipi;
364}
365
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530366static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
367{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530368 vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530369}
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000370
Michael Ellerman441c19c2014-05-23 18:15:25 +1000371extern void kvm_hv_vm_activated(void);
372extern void kvm_hv_vm_deactivated(void);
373extern bool kvm_hv_mode_active(void);
374
Paul Mackerras371fefd2011-06-29 00:23:08 +0000375#else
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +0530376static inline void __init kvm_cma_reserve(void)
377{}
378
Paul Mackerras371fefd2011-06-29 00:23:08 +0000379static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
380{}
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000381
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000382static inline u32 kvmppc_get_xics_latch(void)
383{
384 return 0;
385}
386
387static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
388{}
389
390static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
391{
392 kvm_vcpu_kick(vcpu);
393}
Michael Ellerman441c19c2014-05-23 18:15:25 +1000394
395static inline bool kvm_hv_mode_active(void) { return false; }
396
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000397#endif
398
399#ifdef CONFIG_KVM_XICS
400static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
401{
402 return vcpu->arch.irq_type == KVMPPC_IRQ_XICS;
403}
404extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
405extern int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server);
406extern int kvm_vm_ioctl_xics_irq(struct kvm *kvm, struct kvm_irq_level *args);
407extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
Paul Mackerras8b786452013-04-17 20:32:26 +0000408extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
409extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
Paul Mackerras5975a2e2013-04-27 00:28:37 +0000410extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
411 struct kvm_vcpu *vcpu, u32 cpu);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000412#else
413static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
414 { return 0; }
415static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { }
416static inline int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu,
417 unsigned long server)
418 { return -EINVAL; }
419static inline int kvm_vm_ioctl_xics_irq(struct kvm *kvm,
420 struct kvm_irq_level *args)
421 { return -ENOTTY; }
422static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
423 { return 0; }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000424#endif
425
Bharat Bhushan34f754b2014-07-17 17:01:40 +0530426static inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu)
427{
428#ifdef CONFIG_KVM_BOOKE_HV
429 return mfspr(SPRN_GEPR);
430#elif defined(CONFIG_BOOKE)
431 return vcpu->arch.epr;
432#else
433 return 0;
434#endif
435}
436
Alexander Graf1c810632013-01-04 18:12:48 +0100437static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
438{
439#ifdef CONFIG_KVM_BOOKE_HV
440 mtspr(SPRN_GEPR, epr);
441#elif defined(CONFIG_BOOKE)
442 vcpu->arch.epr = epr;
443#endif
444}
445
Scott Wood5df554ad2013-04-12 14:08:46 +0000446#ifdef CONFIG_KVM_MPIC
447
448void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu);
Scott Woodeb1e4f42013-04-12 14:08:47 +0000449int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
450 u32 cpu);
451void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu);
Scott Wood5df554ad2013-04-12 14:08:46 +0000452
453#else
454
455static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu)
456{
457}
458
Scott Woodeb1e4f42013-04-12 14:08:47 +0000459static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev,
460 struct kvm_vcpu *vcpu, u32 cpu)
461{
462 return -EINVAL;
463}
464
465static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp,
466 struct kvm_vcpu *vcpu)
467{
468}
469
Scott Wood5df554ad2013-04-12 14:08:46 +0000470#endif /* CONFIG_KVM_MPIC */
471
Scott Wooddc83b8b2011-08-18 15:25:21 -0500472int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
473 struct kvm_config_tlb *cfg);
474int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
475 struct kvm_dirty_tlb *cfg);
476
Scott Wood043cc4d2011-12-20 15:34:20 +0000477long kvmppc_alloc_lpid(void);
478void kvmppc_claim_lpid(long lpid);
479void kvmppc_free_lpid(long lpid);
480void kvmppc_init_lpid(unsigned long nr_lpids);
481
Alexander Graf249ba1e2012-08-03 13:56:33 +0200482static inline void kvmppc_mmu_flush_icache(pfn_t pfn)
483{
Alexander Graf249ba1e2012-08-03 13:56:33 +0200484 struct page *page;
Bharat Bhushanadccf652013-04-25 06:33:57 +0000485 /*
486 * We can only access pages that the kernel maps
487 * as memory. Bail out for unmapped ones.
488 */
489 if (!pfn_valid(pfn))
490 return;
491
492 /* Clear i-cache for new pages */
Alexander Graf249ba1e2012-08-03 13:56:33 +0200493 page = pfn_to_page(pfn);
494 if (!test_bit(PG_arch_1, &page->flags)) {
495 flush_dcache_icache_page(page);
496 set_bit(PG_arch_1, &page->flags);
497 }
498}
499
Scott Wood5f1c2482013-07-10 17:47:39 -0500500/*
Alexander Graf5deb8e72014-04-24 13:46:24 +0200501 * Shared struct helpers. The shared struct can be little or big endian,
502 * depending on the guest endianness. So expose helpers to all of them.
503 */
504static inline bool kvmppc_shared_big_endian(struct kvm_vcpu *vcpu)
505{
506#if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
507 /* Only Book3S_64 PR supports bi-endian for now */
508 return vcpu->arch.shared_big_endian;
509#elif defined(CONFIG_PPC_BOOK3S_64) && defined(__LITTLE_ENDIAN__)
510 /* Book3s_64 HV on little endian is always little endian */
511 return false;
512#else
513 return true;
514#endif
515}
516
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530517#define SPRNG_WRAPPER_GET(reg, e500hv_spr) \
518static inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
519{ \
520 return mfspr(e500hv_spr); \
521} \
522
523#define SPRNG_WRAPPER_SET(reg, e500hv_spr) \
524static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val) \
525{ \
526 mtspr(e500hv_spr, val); \
527} \
528
Alexander Graf5deb8e72014-04-24 13:46:24 +0200529#define SHARED_WRAPPER_GET(reg, size) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530530static inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
Alexander Graf5deb8e72014-04-24 13:46:24 +0200531{ \
532 if (kvmppc_shared_big_endian(vcpu)) \
533 return be##size##_to_cpu(vcpu->arch.shared->reg); \
534 else \
535 return le##size##_to_cpu(vcpu->arch.shared->reg); \
536} \
537
538#define SHARED_WRAPPER_SET(reg, size) \
539static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val) \
540{ \
541 if (kvmppc_shared_big_endian(vcpu)) \
542 vcpu->arch.shared->reg = cpu_to_be##size(val); \
543 else \
544 vcpu->arch.shared->reg = cpu_to_le##size(val); \
545} \
546
547#define SHARED_WRAPPER(reg, size) \
548 SHARED_WRAPPER_GET(reg, size) \
549 SHARED_WRAPPER_SET(reg, size) \
550
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530551#define SPRNG_WRAPPER(reg, e500hv_spr) \
552 SPRNG_WRAPPER_GET(reg, e500hv_spr) \
553 SPRNG_WRAPPER_SET(reg, e500hv_spr) \
554
555#ifdef CONFIG_KVM_BOOKE_HV
556
557#define SHARED_SPRNG_WRAPPER(reg, size, e500hv_spr) \
558 SPRNG_WRAPPER(reg, e500hv_spr) \
559
560#else
561
562#define SHARED_SPRNG_WRAPPER(reg, size, e500hv_spr) \
563 SHARED_WRAPPER(reg, size) \
564
565#endif
566
Alexander Graf5deb8e72014-04-24 13:46:24 +0200567SHARED_WRAPPER(critical, 64)
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530568SHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0)
569SHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1)
570SHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2)
571SHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3)
572SHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0)
573SHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1)
574SHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR)
Bharat Bhushandc168542014-07-17 17:01:38 +0530575SHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR)
Alexander Graf5deb8e72014-04-24 13:46:24 +0200576SHARED_WRAPPER_GET(msr, 64)
577static inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val)
578{
579 if (kvmppc_shared_big_endian(vcpu))
580 vcpu->arch.shared->msr = cpu_to_be64(val);
581 else
582 vcpu->arch.shared->msr = cpu_to_le64(val);
583}
584SHARED_WRAPPER(dsisr, 32)
585SHARED_WRAPPER(int_pending, 32)
586SHARED_WRAPPER(sprg4, 64)
587SHARED_WRAPPER(sprg5, 64)
588SHARED_WRAPPER(sprg6, 64)
589SHARED_WRAPPER(sprg7, 64)
590
591static inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr)
592{
593 if (kvmppc_shared_big_endian(vcpu))
594 return be32_to_cpu(vcpu->arch.shared->sr[nr]);
595 else
596 return le32_to_cpu(vcpu->arch.shared->sr[nr]);
597}
598
599static inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val)
600{
601 if (kvmppc_shared_big_endian(vcpu))
602 vcpu->arch.shared->sr[nr] = cpu_to_be32(val);
603 else
604 vcpu->arch.shared->sr[nr] = cpu_to_le32(val);
605}
606
607/*
Scott Wood5f1c2482013-07-10 17:47:39 -0500608 * Please call after prepare_to_enter. This function puts the lazy ee and irq
609 * disabled tracking state back to normal mode, without actually enabling
610 * interrupts.
611 */
612static inline void kvmppc_fix_ee_before_entry(void)
Alexander Grafbd2be682012-08-13 01:04:19 +0200613{
Scott Wood5f1c2482013-07-10 17:47:39 -0500614 trace_hardirqs_on();
615
Alexander Grafbd2be682012-08-13 01:04:19 +0200616#ifdef CONFIG_PPC64
Scott Wood6c85f522014-01-09 19:18:40 -0600617 /*
618 * To avoid races, the caller must have gone directly from having
619 * interrupts fully-enabled to hard-disabled.
620 */
621 WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS);
622
Alexander Grafbd2be682012-08-13 01:04:19 +0200623 /* Only need to enable IRQs by hard enabling them after this */
624 local_paca->irq_happened = 0;
625 local_paca->soft_enabled = 1;
626#endif
627}
Alexander Graf249ba1e2012-08-03 13:56:33 +0200628
Mihai Caraman7cdd7a92012-10-11 06:13:22 +0000629static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
630{
631 ulong ea;
Mihai Caraman8823a8f2012-10-11 06:13:23 +0000632 ulong msr_64bit = 0;
Mihai Caraman7cdd7a92012-10-11 06:13:22 +0000633
634 ea = kvmppc_get_gpr(vcpu, rb);
635 if (ra)
636 ea += kvmppc_get_gpr(vcpu, ra);
637
Mihai Caraman8823a8f2012-10-11 06:13:23 +0000638#if defined(CONFIG_PPC_BOOK3E_64)
639 msr_64bit = MSR_CM;
640#elif defined(CONFIG_PPC_BOOK3S_64)
641 msr_64bit = MSR_SF;
642#endif
643
Alexander Graf5deb8e72014-04-24 13:46:24 +0200644 if (!(kvmppc_get_msr(vcpu) & msr_64bit))
Mihai Caraman8823a8f2012-10-11 06:13:23 +0000645 ea = (uint32_t)ea;
646
Mihai Caraman7cdd7a92012-10-11 06:13:22 +0000647 return ea;
648}
649
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000650extern void xics_wake_cpu(int cpu);
651
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500652#endif /* __POWERPC_KVM_PPC_H__ */