blob: fb86a2299d8afb9f635b71a92ef2e7345abdedff [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 */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050044 EMULATE_FAIL, /* can't emulate this instruction */
Alexander Graf37f5bca2010-02-19 11:00:31 +010045 EMULATE_AGAIN, /* something went wrong. go again */
Bharat Bhushanc402a3f2013-04-08 00:32:13 +000046 EMULATE_EXIT_USER, /* emulation requires exit to user-space */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050047};
48
Mihai Caraman51f04722014-07-23 19:06:21 +030049enum instruction_type {
50 INST_GENERIC,
51 INST_SC, /* system call */
52};
53
Alexander Graf7d15c062014-06-20 13:52:36 +020054enum xlate_instdata {
55 XLATE_INST, /* translate instruction address */
56 XLATE_DATA /* translate data address */
57};
58
59enum xlate_readwrite {
60 XLATE_READ, /* check for read permissions */
61 XLATE_WRITE /* check for write permissions */
62};
63
Paul Mackerrasdf6909e52011-06-29 00:19:50 +000064extern int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050065extern int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Alexander Graf29eb61b2009-10-30 05:47:07 +000066extern void kvmppc_handler_highmem(void);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050067
68extern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu);
69extern int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
70 unsigned int rt, unsigned int bytes,
Cédric Le Goater73601772014-01-09 11:51:16 +010071 int is_default_endian);
Alexander Graf3587d532010-02-19 11:00:30 +010072extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
73 unsigned int rt, unsigned int bytes,
Cédric Le Goater73601772014-01-09 11:51:16 +010074 int is_default_endian);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050075extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
Cédric Le Goater73601772014-01-09 11:51:16 +010076 u64 val, unsigned int bytes,
77 int is_default_endian);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050078
Mihai Caraman51f04722014-07-23 19:06:21 +030079extern int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
80 enum instruction_type type, u32 *inst);
81
Alexander Graf35c4a732014-06-20 13:58:16 +020082extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
83 bool data);
84extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
85 bool data);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050086extern int kvmppc_emulate_instruction(struct kvm_run *run,
87 struct kvm_vcpu *vcpu);
Alexander Grafd69614a2014-06-18 14:53:49 +020088extern int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu);
Hollis Blanchardce263d72008-05-21 18:22:51 -050089extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
Hollis Blanchard75f74f02008-11-05 09:36:16 -060090extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -050091extern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb);
Scott Wooddfd4d472011-11-17 12:39:59 +000092extern void kvmppc_decrementer_func(unsigned long data);
Alexander Grafaf8f38b2011-08-10 13:57:08 +020093extern int kvmppc_sanity_check(struct kvm_vcpu *vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +000094extern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu);
95extern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050096
Hollis Blanchardecc09812009-01-03 16:22:59 -060097/* Core-specific hooks */
98
Hollis Blanchard89168612008-12-02 15:51:53 -060099extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600100 unsigned int gtlb_idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500101extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500102extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
Hollis Blanchardecc09812009-01-03 16:22:59 -0600103extern void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu);
Alexander Graf9cc5e952010-04-16 00:11:45 +0200104extern int kvmppc_mmu_init(struct kvm_vcpu *vcpu);
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600105extern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
106extern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600107extern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
108 gva_t eaddr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600109extern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu);
110extern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu);
Alexander Graf7d15c062014-06-20 13:52:36 +0200111extern int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr,
112 enum xlate_instdata xlid, enum xlate_readwrite xlrw,
113 struct kvmppc_pte *pte);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600114
Hollis Blancharddb93f572008-11-05 09:36:18 -0600115extern struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm,
116 unsigned int id);
117extern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu);
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600118extern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600119extern int kvmppc_core_check_processor_compat(void);
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600120extern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
121 struct kvm_translation *tr);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600122
123extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
124extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
125
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000126extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600127extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
Alexander Graf25a8a022010-01-08 02:58:07 +0100128extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600129extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
Alexander Graf7706664d2009-12-21 20:21:24 +0100130extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600131extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
132 struct kvm_interrupt *irq);
Paul Mackerras4fe27d22013-02-14 14:00:25 +0000133extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
Alexander Graf8de12012014-06-18 21:56:55 +0200134extern void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu, ulong dear_flags,
135 ulong esr_flags);
136extern void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
137 ulong dear_flags,
138 ulong esr_flags);
139extern void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu);
140extern void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
141 ulong esr_flags);
Alexander Graf862d31f2012-07-31 00:19:50 +0200142extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
Alexander Graf7c973a22012-08-13 12:50:35 +0200143extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600144
Hollis Blancharddb93f572008-11-05 09:36:18 -0600145extern int kvmppc_booke_init(void);
146extern void kvmppc_booke_exit(void);
147
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600148extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
Alexander Graf2a342ed2010-07-29 14:47:48 +0200149extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
Scott Wooda4cd8b22011-06-14 18:34:41 -0500150extern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600151
Paul Mackerras32fad282012-05-04 02:32:53 +0000152extern long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp);
153extern long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000154extern void kvmppc_free_hpt(struct kvm *kvm);
155extern long kvmppc_prepare_vrma(struct kvm *kvm,
156 struct kvm_userspace_memory_region *mem);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000157extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000158 struct kvm_memory_slot *memslot, unsigned long porder);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000159extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000160
David Gibson54738c02011-06-29 00:22:41 +0000161extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
162 struct kvm_create_spapr_tce *args);
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000163extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
164 unsigned long ioba, unsigned long tce);
Laurent Dufour69e9fbb22014-02-21 16:31:10 +0100165extern long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
166 unsigned long ioba);
Aneesh Kumar K.V6c45b812013-07-02 11:15:17 +0530167extern struct kvm_rma_info *kvm_alloc_rma(void);
168extern void kvm_release_rma(struct kvm_rma_info *ri);
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +0530169extern struct page *kvm_alloc_hpt(unsigned long nr_pages);
170extern void kvm_release_hpt(struct page *page, unsigned long nr_pages);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000171extern int kvmppc_core_init_vm(struct kvm *kvm);
172extern void kvmppc_core_destroy_vm(struct kvm *kvm);
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530173extern void kvmppc_core_free_memslot(struct kvm *kvm,
174 struct kvm_memory_slot *free,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000175 struct kvm_memory_slot *dont);
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530176extern int kvmppc_core_create_memslot(struct kvm *kvm,
177 struct kvm_memory_slot *slot,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000178 unsigned long npages);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000179extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000180 struct kvm_memory_slot *memslot,
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000181 struct kvm_userspace_memory_region *mem);
182extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +0000183 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +0900184 const struct kvm_memory_slot *old);
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +0000185extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
186 struct kvm_ppc_smmu_info *info);
Paul Mackerrasdfe49db2012-09-11 13:28:18 +0000187extern void kvmppc_core_flush_memslot(struct kvm *kvm,
188 struct kvm_memory_slot *memslot);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000189
Scott Woodd30f6e42011-12-20 15:34:43 +0000190extern int kvmppc_bookehv_init(void);
191extern void kvmppc_bookehv_exit(void);
192
Alexander Graf03d25c52012-08-10 12:28:50 +0200193extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
194
Paul Mackerrasa2932922012-11-19 22:57:20 +0000195extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
196
Scott Wood5df554a2013-04-12 14:08:46 +0000197int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
198
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000199extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
200extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
201extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000202extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server,
203 u32 priority);
204extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server,
205 u32 *priority);
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000206extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
207extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000208
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530209union kvmppc_one_reg {
210 u32 wval;
211 u64 dval;
212 vector128 vval;
213 u64 vsxval[2];
214 struct {
215 u64 addr;
216 u64 length;
217 } vpaval;
218};
219
220struct kvmppc_ops {
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530221 struct module *owner;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530222 int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
223 int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
224 int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id,
225 union kvmppc_one_reg *val);
226 int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id,
227 union kvmppc_one_reg *val);
228 void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
229 void (*vcpu_put)(struct kvm_vcpu *vcpu);
230 void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr);
231 int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu);
232 struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned int id);
233 void (*vcpu_free)(struct kvm_vcpu *vcpu);
234 int (*check_requests)(struct kvm_vcpu *vcpu);
235 int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log);
236 void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot);
237 int (*prepare_memory_region)(struct kvm *kvm,
238 struct kvm_memory_slot *memslot,
239 struct kvm_userspace_memory_region *mem);
240 void (*commit_memory_region)(struct kvm *kvm,
241 struct kvm_userspace_memory_region *mem,
242 const struct kvm_memory_slot *old);
243 int (*unmap_hva)(struct kvm *kvm, unsigned long hva);
244 int (*unmap_hva_range)(struct kvm *kvm, unsigned long start,
245 unsigned long end);
246 int (*age_hva)(struct kvm *kvm, unsigned long hva);
247 int (*test_age_hva)(struct kvm *kvm, unsigned long hva);
248 void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte);
249 void (*mmu_destroy)(struct kvm_vcpu *vcpu);
250 void (*free_memslot)(struct kvm_memory_slot *free,
251 struct kvm_memory_slot *dont);
252 int (*create_memslot)(struct kvm_memory_slot *slot,
253 unsigned long npages);
254 int (*init_vm)(struct kvm *kvm);
255 void (*destroy_vm)(struct kvm *kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530256 int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info);
257 int (*emulate_op)(struct kvm_run *run, struct kvm_vcpu *vcpu,
258 unsigned int inst, int *advance);
259 int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val);
260 int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val);
261 void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu);
262 long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl,
263 unsigned long arg);
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000264 int (*hcall_implemented)(unsigned long hcall);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530265};
266
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530267extern struct kvmppc_ops *kvmppc_hv_ops;
268extern struct kvmppc_ops *kvmppc_pr_ops;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530269
Mihai Caraman51f04722014-07-23 19:06:21 +0300270static inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu,
271 enum instruction_type type, u32 *inst)
272{
273 int ret = EMULATE_DONE;
274 u32 fetched_inst;
275
276 /* Load the instruction manually if it failed to do so in the
277 * exit path */
278 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
279 ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst);
280
281 /* Write fetch_failed unswapped if the fetch failed */
282 if (ret == EMULATE_DONE)
283 fetched_inst = kvmppc_need_byteswap(vcpu) ?
284 swab32(vcpu->arch.last_inst) :
285 vcpu->arch.last_inst;
286 else
287 fetched_inst = vcpu->arch.last_inst;
288
289 *inst = fetched_inst;
290 return ret;
291}
292
Aneesh Kumar K.Va78b55d2013-10-07 22:18:02 +0530293static inline bool is_kvmppc_hv_enabled(struct kvm *kvm)
294{
295 return kvm->arch.kvm_ops == kvmppc_hv_ops;
296}
297
Alexander Graf0564ee82010-02-19 11:00:42 +0100298/*
299 * Cuts out inst bits with ordering according to spec.
300 * That means the leftmost bit is zero. All given bits are included.
301 */
302static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
303{
304 u32 r;
305 u32 mask;
306
307 BUG_ON(msb > lsb);
308
309 mask = (1 << (lsb - msb + 1)) - 1;
310 r = (inst >> (63 - lsb)) & mask;
311
312 return r;
313}
314
315/*
316 * Replaces inst bits with ordering according to spec.
317 */
318static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
319{
320 u32 r;
321 u32 mask;
322
323 BUG_ON(msb > lsb);
324
325 mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
326 r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
327
328 return r;
329}
330
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000331#define one_reg_size(id) \
332 (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
333
334#define get_reg_val(id, reg) ({ \
335 union kvmppc_one_reg __u; \
336 switch (one_reg_size(id)) { \
337 case 4: __u.wval = (reg); break; \
338 case 8: __u.dval = (reg); break; \
339 default: BUG(); \
340 } \
341 __u; \
342})
343
344
345#define set_reg_val(id, val) ({ \
346 u64 __v; \
347 switch (one_reg_size(id)) { \
348 case 4: __v = (val).wval; break; \
349 case 8: __v = (val).dval; break; \
350 default: BUG(); \
351 } \
352 __v; \
353})
354
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530355int kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
Scott Wood5ce941e2011-04-27 17:24:21 -0500356int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
357
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530358int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
Scott Wood5ce941e2011-04-27 17:24:21 -0500359int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
360
Paul Mackerras31f34382011-12-12 12:26:50 +0000361int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
362int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000363int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
364int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
Paul Mackerras31f34382011-12-12 12:26:50 +0000365
Scott Wood5ce941e2011-04-27 17:24:21 -0500366void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
367
Scott Wood5df554a2013-04-12 14:08:46 +0000368struct openpic;
Scott Wood5df554a2013-04-12 14:08:46 +0000369
Aneesh Kumar K.V9975f5e2013-10-07 22:17:52 +0530370#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +0530371extern void kvm_cma_reserve(void) __init;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000372static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
373{
374 paca[cpu].kvm_hstate.xics_phys = addr;
375}
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000376
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000377static inline u32 kvmppc_get_xics_latch(void)
378{
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530379 u32 xirr;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000380
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530381 xirr = get_paca()->kvm_hstate.saved_xirr;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000382 get_paca()->kvm_hstate.saved_xirr = 0;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000383 return xirr;
384}
385
386static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
387{
388 paca[cpu].kvm_hstate.host_ipi = host_ipi;
389}
390
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530391static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
392{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530393 vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530394}
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000395
Michael Ellerman441c19c2014-05-23 18:15:25 +1000396extern void kvm_hv_vm_activated(void);
397extern void kvm_hv_vm_deactivated(void);
398extern bool kvm_hv_mode_active(void);
399
Paul Mackerras371fefd2011-06-29 00:23:08 +0000400#else
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +0530401static inline void __init kvm_cma_reserve(void)
402{}
403
Paul Mackerras371fefd2011-06-29 00:23:08 +0000404static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
405{}
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000406
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000407static inline u32 kvmppc_get_xics_latch(void)
408{
409 return 0;
410}
411
412static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
413{}
414
415static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
416{
417 kvm_vcpu_kick(vcpu);
418}
Michael Ellerman441c19c2014-05-23 18:15:25 +1000419
420static inline bool kvm_hv_mode_active(void) { return false; }
421
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000422#endif
423
424#ifdef CONFIG_KVM_XICS
425static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
426{
427 return vcpu->arch.irq_type == KVMPPC_IRQ_XICS;
428}
429extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
430extern int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server);
431extern int kvm_vm_ioctl_xics_irq(struct kvm *kvm, struct kvm_irq_level *args);
432extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
Paul Mackerras8b786452013-04-17 20:32:26 +0000433extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
434extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
Paul Mackerras5975a2e2013-04-27 00:28:37 +0000435extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
436 struct kvm_vcpu *vcpu, u32 cpu);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000437#else
438static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
439 { return 0; }
440static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { }
441static inline int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu,
442 unsigned long server)
443 { return -EINVAL; }
444static inline int kvm_vm_ioctl_xics_irq(struct kvm *kvm,
445 struct kvm_irq_level *args)
446 { return -ENOTTY; }
447static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
448 { return 0; }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000449#endif
450
Bharat Bhushan34f754b2014-07-17 17:01:40 +0530451static inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu)
452{
453#ifdef CONFIG_KVM_BOOKE_HV
454 return mfspr(SPRN_GEPR);
455#elif defined(CONFIG_BOOKE)
456 return vcpu->arch.epr;
457#else
458 return 0;
459#endif
460}
461
Alexander Graf1c810632013-01-04 18:12:48 +0100462static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
463{
464#ifdef CONFIG_KVM_BOOKE_HV
465 mtspr(SPRN_GEPR, epr);
466#elif defined(CONFIG_BOOKE)
467 vcpu->arch.epr = epr;
468#endif
469}
470
Scott Wood5df554a2013-04-12 14:08:46 +0000471#ifdef CONFIG_KVM_MPIC
472
473void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu);
Scott Woodeb1e4f42013-04-12 14:08:47 +0000474int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
475 u32 cpu);
476void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu);
Scott Wood5df554a2013-04-12 14:08:46 +0000477
478#else
479
480static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu)
481{
482}
483
Scott Woodeb1e4f42013-04-12 14:08:47 +0000484static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev,
485 struct kvm_vcpu *vcpu, u32 cpu)
486{
487 return -EINVAL;
488}
489
490static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp,
491 struct kvm_vcpu *vcpu)
492{
493}
494
Scott Wood5df554a2013-04-12 14:08:46 +0000495#endif /* CONFIG_KVM_MPIC */
496
Scott Wooddc83b8b2011-08-18 15:25:21 -0500497int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
498 struct kvm_config_tlb *cfg);
499int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
500 struct kvm_dirty_tlb *cfg);
501
Scott Wood043cc4d2011-12-20 15:34:20 +0000502long kvmppc_alloc_lpid(void);
503void kvmppc_claim_lpid(long lpid);
504void kvmppc_free_lpid(long lpid);
505void kvmppc_init_lpid(unsigned long nr_lpids);
506
Alexander Graf249ba1e2012-08-03 13:56:33 +0200507static inline void kvmppc_mmu_flush_icache(pfn_t pfn)
508{
Alexander Graf249ba1e2012-08-03 13:56:33 +0200509 struct page *page;
Bharat Bhushanadccf652013-04-25 06:33:57 +0000510 /*
511 * We can only access pages that the kernel maps
512 * as memory. Bail out for unmapped ones.
513 */
514 if (!pfn_valid(pfn))
515 return;
516
517 /* Clear i-cache for new pages */
Alexander Graf249ba1e2012-08-03 13:56:33 +0200518 page = pfn_to_page(pfn);
519 if (!test_bit(PG_arch_1, &page->flags)) {
520 flush_dcache_icache_page(page);
521 set_bit(PG_arch_1, &page->flags);
522 }
523}
524
Scott Wood5f1c2482013-07-10 17:47:39 -0500525/*
Alexander Graf5deb8e72014-04-24 13:46:24 +0200526 * Shared struct helpers. The shared struct can be little or big endian,
527 * depending on the guest endianness. So expose helpers to all of them.
528 */
529static inline bool kvmppc_shared_big_endian(struct kvm_vcpu *vcpu)
530{
531#if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
532 /* Only Book3S_64 PR supports bi-endian for now */
533 return vcpu->arch.shared_big_endian;
534#elif defined(CONFIG_PPC_BOOK3S_64) && defined(__LITTLE_ENDIAN__)
535 /* Book3s_64 HV on little endian is always little endian */
536 return false;
537#else
538 return true;
539#endif
540}
541
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530542#define SPRNG_WRAPPER_GET(reg, bookehv_spr) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530543static inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
544{ \
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530545 return mfspr(bookehv_spr); \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530546} \
547
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530548#define SPRNG_WRAPPER_SET(reg, bookehv_spr) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530549static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val) \
550{ \
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530551 mtspr(bookehv_spr, val); \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530552} \
553
Alexander Graf5deb8e72014-04-24 13:46:24 +0200554#define SHARED_WRAPPER_GET(reg, size) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530555static inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
Alexander Graf5deb8e72014-04-24 13:46:24 +0200556{ \
557 if (kvmppc_shared_big_endian(vcpu)) \
558 return be##size##_to_cpu(vcpu->arch.shared->reg); \
559 else \
560 return le##size##_to_cpu(vcpu->arch.shared->reg); \
561} \
562
563#define SHARED_WRAPPER_SET(reg, size) \
564static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val) \
565{ \
566 if (kvmppc_shared_big_endian(vcpu)) \
567 vcpu->arch.shared->reg = cpu_to_be##size(val); \
568 else \
569 vcpu->arch.shared->reg = cpu_to_le##size(val); \
570} \
571
572#define SHARED_WRAPPER(reg, size) \
573 SHARED_WRAPPER_GET(reg, size) \
574 SHARED_WRAPPER_SET(reg, size) \
575
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530576#define SPRNG_WRAPPER(reg, bookehv_spr) \
577 SPRNG_WRAPPER_GET(reg, bookehv_spr) \
578 SPRNG_WRAPPER_SET(reg, bookehv_spr) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530579
580#ifdef CONFIG_KVM_BOOKE_HV
581
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530582#define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
583 SPRNG_WRAPPER(reg, bookehv_spr) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530584
585#else
586
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530587#define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530588 SHARED_WRAPPER(reg, size) \
589
590#endif
591
Alexander Graf5deb8e72014-04-24 13:46:24 +0200592SHARED_WRAPPER(critical, 64)
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530593SHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0)
594SHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1)
595SHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2)
596SHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3)
597SHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0)
598SHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1)
599SHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR)
Bharat Bhushandc168542014-07-17 17:01:38 +0530600SHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR)
Alexander Graf5deb8e72014-04-24 13:46:24 +0200601SHARED_WRAPPER_GET(msr, 64)
602static inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val)
603{
604 if (kvmppc_shared_big_endian(vcpu))
605 vcpu->arch.shared->msr = cpu_to_be64(val);
606 else
607 vcpu->arch.shared->msr = cpu_to_le64(val);
608}
609SHARED_WRAPPER(dsisr, 32)
610SHARED_WRAPPER(int_pending, 32)
611SHARED_WRAPPER(sprg4, 64)
612SHARED_WRAPPER(sprg5, 64)
613SHARED_WRAPPER(sprg6, 64)
614SHARED_WRAPPER(sprg7, 64)
615
616static inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr)
617{
618 if (kvmppc_shared_big_endian(vcpu))
619 return be32_to_cpu(vcpu->arch.shared->sr[nr]);
620 else
621 return le32_to_cpu(vcpu->arch.shared->sr[nr]);
622}
623
624static inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val)
625{
626 if (kvmppc_shared_big_endian(vcpu))
627 vcpu->arch.shared->sr[nr] = cpu_to_be32(val);
628 else
629 vcpu->arch.shared->sr[nr] = cpu_to_le32(val);
630}
631
632/*
Scott Wood5f1c2482013-07-10 17:47:39 -0500633 * Please call after prepare_to_enter. This function puts the lazy ee and irq
634 * disabled tracking state back to normal mode, without actually enabling
635 * interrupts.
636 */
637static inline void kvmppc_fix_ee_before_entry(void)
Alexander Grafbd2be682012-08-13 01:04:19 +0200638{
Scott Wood5f1c2482013-07-10 17:47:39 -0500639 trace_hardirqs_on();
640
Alexander Grafbd2be682012-08-13 01:04:19 +0200641#ifdef CONFIG_PPC64
Scott Wood6c85f522014-01-09 19:18:40 -0600642 /*
643 * To avoid races, the caller must have gone directly from having
644 * interrupts fully-enabled to hard-disabled.
645 */
646 WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS);
647
Alexander Grafbd2be682012-08-13 01:04:19 +0200648 /* Only need to enable IRQs by hard enabling them after this */
649 local_paca->irq_happened = 0;
650 local_paca->soft_enabled = 1;
651#endif
652}
Alexander Graf249ba1e2012-08-03 13:56:33 +0200653
Mihai Caraman7cdd7a92012-10-11 06:13:22 +0000654static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
655{
656 ulong ea;
Mihai Caraman8823a8f2012-10-11 06:13:23 +0000657 ulong msr_64bit = 0;
Mihai Caraman7cdd7a92012-10-11 06:13:22 +0000658
659 ea = kvmppc_get_gpr(vcpu, rb);
660 if (ra)
661 ea += kvmppc_get_gpr(vcpu, ra);
662
Mihai Caraman8823a8f2012-10-11 06:13:23 +0000663#if defined(CONFIG_PPC_BOOK3E_64)
664 msr_64bit = MSR_CM;
665#elif defined(CONFIG_PPC_BOOK3S_64)
666 msr_64bit = MSR_SF;
667#endif
668
Alexander Graf5deb8e72014-04-24 13:46:24 +0200669 if (!(kvmppc_get_msr(vcpu) & msr_64bit))
Mihai Caraman8823a8f2012-10-11 06:13:23 +0000670 ea = (uint32_t)ea;
671
Mihai Caraman7cdd7a92012-10-11 06:13:22 +0000672 return ea;
673}
674
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000675extern void xics_wake_cpu(int cpu);
676
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500677#endif /* __POWERPC_KVM_PPC_H__ */