blob: b3bf4f61b30c66e638f6606c92198cd9c1464a3b [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>
Paul Mackerras03f95332019-02-04 22:07:20 +110039#include <asm/xive.h>
40#include <asm/cpu_has_feature.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000041#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050042
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +053043/*
44 * KVMPPC_INST_SW_BREAKPOINT is debug Instruction
45 * for supporting software breakpoint.
46 */
47#define KVMPPC_INST_SW_BREAKPOINT 0x00dddd00
48
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050049enum emulation_result {
50 EMULATE_DONE, /* no further processing */
51 EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050052 EMULATE_FAIL, /* can't emulate this instruction */
Alexander Graf37f5bca2010-02-19 11:00:31 +010053 EMULATE_AGAIN, /* something went wrong. go again */
Bharat Bhushanc402a3f2013-04-08 00:32:13 +000054 EMULATE_EXIT_USER, /* emulation requires exit to user-space */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050055};
56
Simon Guo70923602018-05-21 13:24:21 +080057enum instruction_fetch_type {
Mihai Caraman51f04722014-07-23 19:06:21 +030058 INST_GENERIC,
59 INST_SC, /* system call */
60};
61
Alexander Graf7d15c06f2014-06-20 13:52:36 +020062enum xlate_instdata {
63 XLATE_INST, /* translate instruction address */
64 XLATE_DATA /* translate data address */
65};
66
67enum xlate_readwrite {
68 XLATE_READ, /* check for read permissions */
69 XLATE_WRITE /* check for write permissions */
70};
71
Paul Mackerrasdf6909e52011-06-29 00:19:50 +000072extern int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050073extern int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
Alexander Graf29eb61b2009-10-30 05:47:07 +000074extern void kvmppc_handler_highmem(void);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050075
76extern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu);
77extern int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
78 unsigned int rt, unsigned int bytes,
Cédric Le Goater73601772014-01-09 11:51:16 +010079 int is_default_endian);
Alexander Graf3587d532010-02-19 11:00:30 +010080extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
81 unsigned int rt, unsigned int bytes,
Cédric Le Goater73601772014-01-09 11:51:16 +010082 int is_default_endian);
Bin Lu6f63e812017-02-21 21:12:36 +080083extern int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
84 unsigned int rt, unsigned int bytes,
85 int is_default_endian, int mmio_sign_extend);
Simon Guoacc9eb92018-05-21 13:24:26 +080086extern int kvmppc_handle_vmx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
87 unsigned int rt, unsigned int bytes, int is_default_endian);
88extern int kvmppc_handle_vmx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
89 unsigned int rs, unsigned int bytes, int is_default_endian);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050090extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
Cédric Le Goater73601772014-01-09 11:51:16 +010091 u64 val, unsigned int bytes,
92 int is_default_endian);
Bin Lu6f63e812017-02-21 21:12:36 +080093extern int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
94 int rs, unsigned int bytes,
95 int is_default_endian);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050096
Mihai Caraman51f04722014-07-23 19:06:21 +030097extern int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
Simon Guo70923602018-05-21 13:24:21 +080098 enum instruction_fetch_type type, u32 *inst);
Mihai Caraman51f04722014-07-23 19:06:21 +030099
Alexander Graf35c4a732014-06-20 13:58:16 +0200100extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
101 bool data);
102extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
103 bool data);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500104extern int kvmppc_emulate_instruction(struct kvm_run *run,
105 struct kvm_vcpu *vcpu);
Alexander Grafd69614a2014-06-18 14:53:49 +0200106extern int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu);
Hollis Blanchardce263d72008-05-21 18:22:51 -0500107extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600108extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -0500109extern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb);
Mihai Caramand02d4d12014-09-01 17:19:56 +0300110extern void kvmppc_decrementer_func(struct kvm_vcpu *vcpu);
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200111extern int kvmppc_sanity_check(struct kvm_vcpu *vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000112extern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu);
113extern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500114
Hollis Blanchardecc09812009-01-03 16:22:59 -0600115/* Core-specific hooks */
116
Hollis Blanchard89168612008-12-02 15:51:53 -0600117extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600118 unsigned int gtlb_idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500119extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500120extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
Hollis Blanchardecc09812009-01-03 16:22:59 -0600121extern void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu);
Alexander Graf9cc5e952010-04-16 00:11:45 +0200122extern int kvmppc_mmu_init(struct kvm_vcpu *vcpu);
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600123extern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
124extern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600125extern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
126 gva_t eaddr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600127extern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu);
128extern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu);
Alexander Graf7d15c06f2014-06-20 13:52:36 +0200129extern int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr,
130 enum xlate_instdata xlid, enum xlate_readwrite xlrw,
131 struct kvmppc_pte *pte);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600132
Hollis Blancharddb93f572008-11-05 09:36:18 -0600133extern struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm,
134 unsigned int id);
135extern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu);
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600136extern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600137extern int kvmppc_core_check_processor_compat(void);
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600138extern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
139 struct kvm_translation *tr);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600140
141extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
142extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
143
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000144extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600145extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
Alexander Graf25a8a022010-01-08 02:58:07 +0100146extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags);
Paul Mackerras307d9272017-03-22 21:02:08 +1100147extern void kvmppc_core_queue_fpunavail(struct kvm_vcpu *vcpu);
148extern void kvmppc_core_queue_vec_unavail(struct kvm_vcpu *vcpu);
149extern void kvmppc_core_queue_vsx_unavail(struct kvm_vcpu *vcpu);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600150extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
Alexander Graf7706664d2009-12-21 20:21:24 +0100151extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600152extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
153 struct kvm_interrupt *irq);
Paul Mackerras4fe27d22013-02-14 14:00:25 +0000154extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
Alexander Graf8de12012014-06-18 21:56:55 +0200155extern void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu, ulong dear_flags,
156 ulong esr_flags);
157extern void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
158 ulong dear_flags,
159 ulong esr_flags);
160extern void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu);
161extern void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
162 ulong esr_flags);
Alexander Graf862d31f2012-07-31 00:19:50 +0200163extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
Alexander Graf7c973a22012-08-13 12:50:35 +0200164extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600165
Hollis Blancharddb93f572008-11-05 09:36:18 -0600166extern int kvmppc_booke_init(void);
167extern void kvmppc_booke_exit(void);
168
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600169extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
Alexander Graf2a342ed2010-07-29 14:47:48 +0200170extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
Scott Wooda4cd8b22011-06-14 18:34:41 -0500171extern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
Hollis Blanchardc30f8a62008-11-24 11:37:38 -0600172
David Gibsonaae07772016-12-20 16:49:02 +1100173extern int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order);
174extern void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info);
David Gibsonf98a8bf2016-12-20 16:49:03 +1100175extern long kvmppc_alloc_reset_hpt(struct kvm *kvm, int order);
David Gibsonaae07772016-12-20 16:49:02 +1100176extern void kvmppc_free_hpt(struct kvm_hpt_info *info);
Paul Mackerras18c36402017-09-13 16:00:10 +1000177extern void kvmppc_rmap_reset(struct kvm *kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000178extern long kvmppc_prepare_vrma(struct kvm *kvm,
179 struct kvm_userspace_memory_region *mem);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000180extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000181 struct kvm_memory_slot *memslot, unsigned long porder);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000182extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
Alexey Kardashevskiy121f80b2017-03-22 15:21:56 +1100183extern long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
184 struct iommu_group *grp);
185extern void kvm_spapr_tce_release_iommu_group(struct kvm *kvm,
186 struct iommu_group *grp);
Paul Mackerras18c36402017-09-13 16:00:10 +1000187extern int kvmppc_switch_mmu_to_hpt(struct kvm *kvm);
188extern int kvmppc_switch_mmu_to_radix(struct kvm *kvm);
Paul Mackerrasded13fc2017-11-22 14:38:53 +1100189extern void kvmppc_setup_partition_table(struct kvm *kvm);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000190
David Gibson54738c02011-06-29 00:22:41 +0000191extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
Alexey Kardashevskiy58ded422016-03-01 17:54:40 +1100192 struct kvm_create_spapr_tce_64 *args);
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100193extern struct kvmppc_spapr_tce_table *kvmppc_find_table(
Alexey Kardashevskiy503bfcb2017-03-22 15:21:53 +1100194 struct kvm *kvm, unsigned long liobn);
Alexey Kardashevskiyb1af23d2017-03-22 15:21:55 +1100195#define kvmppc_ioba_validate(stt, ioba, npages) \
196 (iommu_tce_check_ioba((stt)->page_shift, (stt)->offset, \
197 (stt)->size, (ioba), (npages)) ? \
198 H_PARAMETER : H_SUCCESS)
Alexey Kardashevskiya3ac0772018-09-10 18:29:12 +1000199extern long kvmppc_tce_to_ua(struct kvm *kvm, unsigned long tce,
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100200 unsigned long *ua, unsigned long **prmap);
201extern void kvmppc_tce_put(struct kvmppc_spapr_tce_table *tt,
202 unsigned long idx, unsigned long tce);
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000203extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
204 unsigned long ioba, unsigned long tce);
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100205extern long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
206 unsigned long liobn, unsigned long ioba,
207 unsigned long tce_list, unsigned long npages);
208extern long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
209 unsigned long liobn, unsigned long ioba,
210 unsigned long tce_value, unsigned long npages);
Laurent Dufour69e9fbb22014-02-21 16:31:10 +0100211extern long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
212 unsigned long ioba);
David Gibsondb9a2902016-12-20 16:48:59 +1100213extern struct page *kvm_alloc_hpt_cma(unsigned long nr_pages);
214extern void kvm_free_hpt_cma(struct page *page, unsigned long nr_pages);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000215extern int kvmppc_core_init_vm(struct kvm *kvm);
216extern void kvmppc_core_destroy_vm(struct kvm *kvm);
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530217extern void kvmppc_core_free_memslot(struct kvm *kvm,
218 struct kvm_memory_slot *free,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000219 struct kvm_memory_slot *dont);
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530220extern int kvmppc_core_create_memslot(struct kvm *kvm,
221 struct kvm_memory_slot *slot,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000222 unsigned long npages);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000223extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000224 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200225 const struct kvm_userspace_memory_region *mem);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000226extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200227 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200228 const struct kvm_memory_slot *old,
Bharata B Raof032b732018-12-12 15:15:30 +1100229 const struct kvm_memory_slot *new,
230 enum kvm_mr_change change);
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +0000231extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
232 struct kvm_ppc_smmu_info *info);
Paul Mackerrasdfe49db2012-09-11 13:28:18 +0000233extern void kvmppc_core_flush_memslot(struct kvm *kvm,
234 struct kvm_memory_slot *memslot);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000235
Scott Woodd30f6e42011-12-20 15:34:43 +0000236extern int kvmppc_bookehv_init(void);
237extern void kvmppc_bookehv_exit(void);
238
Alexander Graf03d25c52012-08-10 12:28:50 +0200239extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
240
Paul Mackerrasa2932922012-11-19 22:57:20 +0000241extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
David Gibson5e985962016-12-20 16:49:05 +1100242extern long kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm,
243 struct kvm_ppc_resize_hpt *rhpt);
244extern long kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm,
245 struct kvm_ppc_resize_hpt *rhpt);
Paul Mackerrasa2932922012-11-19 22:57:20 +0000246
Scott Wood5df554ad2013-04-12 14:08:46 +0000247int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
248
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000249extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
250extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
251extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000252
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000253extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server,
254 u32 priority);
255extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server,
256 u32 *priority);
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000257extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
258extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000259
Bharat Bhushan2f699a52014-08-13 14:39:44 +0530260void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu);
261void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu);
262
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530263union kvmppc_one_reg {
264 u32 wval;
265 u64 dval;
266 vector128 vval;
267 u64 vsxval[2];
Bin Lu6f63e812017-02-21 21:12:36 +0800268 u32 vsx32val[4];
Simon Guoacc9eb92018-05-21 13:24:26 +0800269 u16 vsx16val[8];
270 u8 vsx8val[16];
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530271 struct {
272 u64 addr;
273 u64 length;
274 } vpaval;
275};
276
277struct kvmppc_ops {
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530278 struct module *owner;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530279 int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
280 int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
281 int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id,
282 union kvmppc_one_reg *val);
283 int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id,
284 union kvmppc_one_reg *val);
285 void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
286 void (*vcpu_put)(struct kvm_vcpu *vcpu);
287 void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr);
288 int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu);
289 struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned int id);
290 void (*vcpu_free)(struct kvm_vcpu *vcpu);
291 int (*check_requests)(struct kvm_vcpu *vcpu);
292 int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log);
293 void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot);
294 int (*prepare_memory_region)(struct kvm *kvm,
295 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200296 const struct kvm_userspace_memory_region *mem);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530297 void (*commit_memory_region)(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200298 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200299 const struct kvm_memory_slot *old,
Bharata B Raof032b732018-12-12 15:15:30 +1100300 const struct kvm_memory_slot *new,
301 enum kvm_mr_change change);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530302 int (*unmap_hva_range)(struct kvm *kvm, unsigned long start,
303 unsigned long end);
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700304 int (*age_hva)(struct kvm *kvm, unsigned long start, unsigned long end);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530305 int (*test_age_hva)(struct kvm *kvm, unsigned long hva);
306 void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte);
307 void (*mmu_destroy)(struct kvm_vcpu *vcpu);
308 void (*free_memslot)(struct kvm_memory_slot *free,
309 struct kvm_memory_slot *dont);
310 int (*create_memslot)(struct kvm_memory_slot *slot,
311 unsigned long npages);
312 int (*init_vm)(struct kvm *kvm);
313 void (*destroy_vm)(struct kvm *kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530314 int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info);
315 int (*emulate_op)(struct kvm_run *run, struct kvm_vcpu *vcpu,
316 unsigned int inst, int *advance);
317 int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val);
318 int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val);
319 void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu);
320 long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl,
321 unsigned long arg);
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000322 int (*hcall_implemented)(unsigned long hcall);
Suresh Warrier95767302016-08-19 15:35:47 +1000323 int (*irq_bypass_add_producer)(struct irq_bypass_consumer *,
324 struct irq_bypass_producer *);
325 void (*irq_bypass_del_producer)(struct irq_bypass_consumer *,
326 struct irq_bypass_producer *);
Paul Mackerrasc9270132017-01-30 21:21:41 +1100327 int (*configure_mmu)(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg);
328 int (*get_rmmu_info)(struct kvm *kvm, struct kvm_ppc_rmmu_info *info);
Paul Mackerras3c313522017-02-06 13:24:41 +1100329 int (*set_smt_mode)(struct kvm *kvm, unsigned long mode,
330 unsigned long flags);
Simon Guo2e6baa42018-05-21 13:24:22 +0800331 void (*giveup_ext)(struct kvm_vcpu *vcpu, ulong msr);
Paul Mackerrasaa069a92018-09-21 20:02:01 +1000332 int (*enable_nested)(struct kvm *kvm);
Suraj Jitindar Singhdceadcf2018-12-14 16:29:06 +1100333 int (*load_from_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
334 int size);
335 int (*store_to_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
336 int size);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530337};
338
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530339extern struct kvmppc_ops *kvmppc_hv_ops;
340extern struct kvmppc_ops *kvmppc_pr_ops;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530341
Mihai Caraman51f04722014-07-23 19:06:21 +0300342static inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu,
Simon Guo70923602018-05-21 13:24:21 +0800343 enum instruction_fetch_type type, u32 *inst)
Mihai Caraman51f04722014-07-23 19:06:21 +0300344{
345 int ret = EMULATE_DONE;
346 u32 fetched_inst;
347
348 /* Load the instruction manually if it failed to do so in the
349 * exit path */
350 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
351 ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst);
352
353 /* Write fetch_failed unswapped if the fetch failed */
354 if (ret == EMULATE_DONE)
355 fetched_inst = kvmppc_need_byteswap(vcpu) ?
356 swab32(vcpu->arch.last_inst) :
357 vcpu->arch.last_inst;
358 else
359 fetched_inst = vcpu->arch.last_inst;
360
361 *inst = fetched_inst;
362 return ret;
363}
364
Aneesh Kumar K.Va78b55d2013-10-07 22:18:02 +0530365static inline bool is_kvmppc_hv_enabled(struct kvm *kvm)
366{
367 return kvm->arch.kvm_ops == kvmppc_hv_ops;
368}
369
Michael Ellermane928e9c2015-03-20 20:39:41 +1100370extern int kvmppc_hwrng_present(void);
371
Alexander Graf0564ee82010-02-19 11:00:42 +0100372/*
373 * Cuts out inst bits with ordering according to spec.
374 * That means the leftmost bit is zero. All given bits are included.
375 */
376static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
377{
378 u32 r;
379 u32 mask;
380
381 BUG_ON(msb > lsb);
382
383 mask = (1 << (lsb - msb + 1)) - 1;
384 r = (inst >> (63 - lsb)) & mask;
385
386 return r;
387}
388
389/*
390 * Replaces inst bits with ordering according to spec.
391 */
392static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
393{
394 u32 r;
395 u32 mask;
396
397 BUG_ON(msb > lsb);
398
399 mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
400 r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
401
402 return r;
403}
404
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000405#define one_reg_size(id) \
406 (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
407
408#define get_reg_val(id, reg) ({ \
409 union kvmppc_one_reg __u; \
410 switch (one_reg_size(id)) { \
411 case 4: __u.wval = (reg); break; \
412 case 8: __u.dval = (reg); break; \
413 default: BUG(); \
414 } \
415 __u; \
416})
417
418
419#define set_reg_val(id, val) ({ \
420 u64 __v; \
421 switch (one_reg_size(id)) { \
422 case 4: __v = (val).wval; break; \
423 case 8: __v = (val).dval; break; \
424 default: BUG(); \
425 } \
426 __v; \
427})
428
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530429int kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
Scott Wood5ce941e2011-04-27 17:24:21 -0500430int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
431
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530432int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
Scott Wood5ce941e2011-04-27 17:24:21 -0500433int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
434
Paul Mackerras31f34382011-12-12 12:26:50 +0000435int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
436int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
Paul Mackerrasa136a8b2012-09-25 20:31:56 +0000437int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
438int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
Paul Mackerras31f34382011-12-12 12:26:50 +0000439
Scott Wood5ce941e2011-04-27 17:24:21 -0500440void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
441
Scott Wood5df554ad2013-04-12 14:08:46 +0000442struct openpic;
Scott Wood5df554ad2013-04-12 14:08:46 +0000443
Aneesh Kumar K.V9975f5e2013-10-07 22:17:52 +0530444#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +0530445extern void kvm_cma_reserve(void) __init;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000446static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
447{
Nicholas Piggind2e60072018-02-14 01:08:12 +1000448 paca_ptrs[cpu]->kvm_hstate.xics_phys = (void __iomem *)addr;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000449}
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000450
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000451static inline void kvmppc_set_xive_tima(int cpu,
452 unsigned long phys_addr,
453 void __iomem *virt_addr)
454{
Nicholas Piggind2e60072018-02-14 01:08:12 +1000455 paca_ptrs[cpu]->kvm_hstate.xive_tima_phys = (void __iomem *)phys_addr;
456 paca_ptrs[cpu]->kvm_hstate.xive_tima_virt = virt_addr;
Paul Mackerras371fefd2011-06-29 00:23:08 +0000457}
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000458
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000459static inline u32 kvmppc_get_xics_latch(void)
460{
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530461 u32 xirr;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000462
Aneesh Kumar K.V699cc872013-10-07 22:17:56 +0530463 xirr = get_paca()->kvm_hstate.saved_xirr;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000464 get_paca()->kvm_hstate.saved_xirr = 0;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000465 return xirr;
466}
467
468static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
469{
Nicholas Piggind2e60072018-02-14 01:08:12 +1000470 paca_ptrs[cpu]->kvm_hstate.host_ipi = host_ipi;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000471}
472
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530473static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
474{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530475 vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530476}
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000477
Michael Ellerman441c19c2014-05-23 18:15:25 +1000478extern void kvm_hv_vm_activated(void);
479extern void kvm_hv_vm_deactivated(void);
480extern bool kvm_hv_mode_active(void);
481
Paul Mackerras371fefd2011-06-29 00:23:08 +0000482#else
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +0530483static inline void __init kvm_cma_reserve(void)
484{}
485
Paul Mackerras371fefd2011-06-29 00:23:08 +0000486static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
487{}
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000488
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000489static inline void kvmppc_set_xive_tima(int cpu,
490 unsigned long phys_addr,
491 void __iomem *virt_addr)
492{}
493
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000494static inline u32 kvmppc_get_xics_latch(void)
495{
496 return 0;
497}
498
499static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
500{}
501
502static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
503{
504 kvm_vcpu_kick(vcpu);
505}
Michael Ellerman441c19c2014-05-23 18:15:25 +1000506
507static inline bool kvm_hv_mode_active(void) { return false; }
508
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000509#endif
510
511#ifdef CONFIG_KVM_XICS
512static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
513{
514 return vcpu->arch.irq_type == KVMPPC_IRQ_XICS;
515}
Suresh Warrier8daaafc2016-08-19 15:35:48 +1000516
517static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
518 struct kvm *kvm)
519{
Suresh Warrier644abbb2016-08-19 15:35:54 +1000520 if (kvm && kvm_irq_bypass)
Suresh Warrier8daaafc2016-08-19 15:35:48 +1000521 return kvm->arch.pimap;
522 return NULL;
523}
524
Suresh Warrier79b6c242015-12-17 14:59:06 -0600525extern void kvmppc_alloc_host_rm_ops(void);
526extern void kvmppc_free_host_rm_ops(void);
Suresh Warrier8daaafc2016-08-19 15:35:48 +1000527extern void kvmppc_free_pimap(struct kvm *kvm);
Suresh Warrierf7af5202016-08-19 15:35:52 +1000528extern int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000529extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000530extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
Paul Mackerras8b786452013-04-17 20:32:26 +0000531extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
532extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
Paul Mackerras5975a2e2013-04-27 00:28:37 +0000533extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
534 struct kvm_vcpu *vcpu, u32 cpu);
Suresh Warrier0c2a6602015-12-17 14:59:09 -0600535extern void kvmppc_xics_ipi_action(void);
Paul Mackerras5d375192016-08-19 15:35:56 +1000536extern void kvmppc_xics_set_mapped(struct kvm *kvm, unsigned long guest_irq,
537 unsigned long host_irq);
538extern void kvmppc_xics_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
539 unsigned long host_irq);
Paul Mackerrasf7257582016-11-18 09:02:08 +1100540extern long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu, __be32 xirr,
541 struct kvmppc_irq_map *irq_map,
542 struct kvmppc_passthru_irqmap *pimap,
543 bool *again);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000544
545extern int kvmppc_xics_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
546 int level, bool line_status);
547
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600548extern int h_ipi_redirect;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000549#else
Suresh Warrier8daaafc2016-08-19 15:35:48 +1000550static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
551 struct kvm *kvm)
552 { return NULL; }
Suresh Warrier79b6c242015-12-17 14:59:06 -0600553static inline void kvmppc_alloc_host_rm_ops(void) {};
554static inline void kvmppc_free_host_rm_ops(void) {};
Suresh Warrier8daaafc2016-08-19 15:35:48 +1000555static inline void kvmppc_free_pimap(struct kvm *kvm) {};
Suresh Warrierf7af5202016-08-19 15:35:52 +1000556static inline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
557 { return 0; }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000558static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
559 { return 0; }
560static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000561static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
562 { return 0; }
Paul Mackerras371fefd2011-06-29 00:23:08 +0000563#endif
564
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000565#ifdef CONFIG_KVM_XIVE
566/*
567 * Below the first "xive" is the "eXternal Interrupt Virtualization Engine"
568 * ie. P9 new interrupt controller, while the second "xive" is the legacy
569 * "eXternal Interrupt Vector Entry" which is the configuration of an
570 * interrupt on the "xics" interrupt controller on P8 and earlier. Those
571 * two function consume or produce a legacy "XIVE" state from the
572 * new "XIVE" interrupt controller.
573 */
574extern int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
575 u32 priority);
576extern int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
577 u32 *priority);
578extern int kvmppc_xive_int_on(struct kvm *kvm, u32 irq);
579extern int kvmppc_xive_int_off(struct kvm *kvm, u32 irq);
580extern void kvmppc_xive_init_module(void);
581extern void kvmppc_xive_exit_module(void);
582
583extern int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
584 struct kvm_vcpu *vcpu, u32 cpu);
585extern void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu);
586extern int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
587 struct irq_desc *host_desc);
588extern int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
589 struct irq_desc *host_desc);
590extern u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu);
591extern int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
592
593extern int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
594 int level, bool line_status);
Paul Mackerras95a64322018-10-08 16:30:55 +1100595extern void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000596#else
597static inline int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
598 u32 priority) { return -1; }
599static inline int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
600 u32 *priority) { return -1; }
601static inline int kvmppc_xive_int_on(struct kvm *kvm, u32 irq) { return -1; }
602static inline int kvmppc_xive_int_off(struct kvm *kvm, u32 irq) { return -1; }
603static inline void kvmppc_xive_init_module(void) { }
604static inline void kvmppc_xive_exit_module(void) { }
605
606static inline int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
607 struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
608static inline void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
609static inline int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
610 struct irq_desc *host_desc) { return -ENODEV; }
611static inline int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
612 struct irq_desc *host_desc) { return -ENODEV; }
613static inline u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu) { return 0; }
614static inline int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval) { return -ENOENT; }
615
616static inline int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
617 int level, bool line_status) { return -ENODEV; }
Paul Mackerras95a64322018-10-08 16:30:55 +1100618static inline void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu) { }
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000619#endif /* CONFIG_KVM_XIVE */
620
Paul Mackerras03f95332019-02-04 22:07:20 +1100621#ifdef CONFIG_PPC_POWERNV
622static inline bool xics_on_xive(void)
623{
624 return xive_enabled() && cpu_has_feature(CPU_FTR_HVMODE);
625}
626#else
627static inline bool xics_on_xive(void)
628{
629 return false;
630}
631#endif
632
Suresh Warrier79b6c242015-12-17 14:59:06 -0600633/*
Paul Mackerrase34af782016-12-01 14:03:46 +1100634 * Prototypes for functions called only from assembler code.
635 * Having prototypes reduces sparse errors.
636 */
637long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
638 unsigned long ioba, unsigned long tce);
639long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
640 unsigned long liobn, unsigned long ioba,
641 unsigned long tce_list, unsigned long npages);
642long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
643 unsigned long liobn, unsigned long ioba,
644 unsigned long tce_value, unsigned long npages);
645long int kvmppc_rm_h_confer(struct kvm_vcpu *vcpu, int target,
646 unsigned int yield_count);
647long kvmppc_h_random(struct kvm_vcpu *vcpu);
648void kvmhv_commence_exit(int trap);
649long kvmppc_realmode_machine_check(struct kvm_vcpu *vcpu);
650void kvmppc_subcore_enter_guest(void);
651void kvmppc_subcore_exit_guest(void);
652long kvmppc_realmode_hmi_handler(void);
653long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
654 long pte_index, unsigned long pteh, unsigned long ptel);
655long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
656 unsigned long pte_index, unsigned long avpn);
657long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu);
658long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
659 unsigned long pte_index, unsigned long avpn,
660 unsigned long va);
661long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
662 unsigned long pte_index);
663long kvmppc_h_clear_ref(struct kvm_vcpu *vcpu, unsigned long flags,
664 unsigned long pte_index);
665long kvmppc_h_clear_mod(struct kvm_vcpu *vcpu, unsigned long flags,
666 unsigned long pte_index);
667long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
668 unsigned long slb_v, unsigned int status, bool data);
669unsigned long kvmppc_rm_h_xirr(struct kvm_vcpu *vcpu);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +1000670unsigned long kvmppc_rm_h_xirr_x(struct kvm_vcpu *vcpu);
671unsigned long kvmppc_rm_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server);
Paul Mackerrase34af782016-12-01 14:03:46 +1100672int kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
673 unsigned long mfrr);
674int kvmppc_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
675int kvmppc_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
Paul Mackerrasf7035ce2018-10-08 16:30:50 +1100676void kvmppc_guest_entry_inject_int(struct kvm_vcpu *vcpu);
Paul Mackerrase34af782016-12-01 14:03:46 +1100677
678/*
Suresh Warrier79b6c242015-12-17 14:59:06 -0600679 * Host-side operations we want to set up while running in real
680 * mode in the guest operating on the xics.
681 * Currently only VCPU wakeup is supported.
682 */
683
684union kvmppc_rm_state {
685 unsigned long raw;
686 struct {
687 u32 in_host;
688 u32 rm_action;
689 };
690};
691
692struct kvmppc_host_rm_core {
693 union kvmppc_rm_state rm_state;
694 void *rm_data;
695 char pad[112];
696};
697
698struct kvmppc_host_rm_ops {
699 struct kvmppc_host_rm_core *rm_core;
700 void (*vcpu_kick)(struct kvm_vcpu *vcpu);
701};
702
703extern struct kvmppc_host_rm_ops *kvmppc_host_rm_ops_hv;
704
Bharat Bhushan34f754b2014-07-17 17:01:40 +0530705static inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu)
706{
707#ifdef CONFIG_KVM_BOOKE_HV
708 return mfspr(SPRN_GEPR);
709#elif defined(CONFIG_BOOKE)
710 return vcpu->arch.epr;
711#else
712 return 0;
713#endif
714}
715
Alexander Graf1c810632013-01-04 18:12:48 +0100716static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
717{
718#ifdef CONFIG_KVM_BOOKE_HV
719 mtspr(SPRN_GEPR, epr);
720#elif defined(CONFIG_BOOKE)
721 vcpu->arch.epr = epr;
722#endif
723}
724
Scott Wood5df554ad2013-04-12 14:08:46 +0000725#ifdef CONFIG_KVM_MPIC
726
727void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu);
Scott Woodeb1e4f42013-04-12 14:08:47 +0000728int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
729 u32 cpu);
730void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu);
Scott Wood5df554ad2013-04-12 14:08:46 +0000731
732#else
733
734static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu)
735{
736}
737
Scott Woodeb1e4f42013-04-12 14:08:47 +0000738static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev,
739 struct kvm_vcpu *vcpu, u32 cpu)
740{
741 return -EINVAL;
742}
743
744static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp,
745 struct kvm_vcpu *vcpu)
746{
747}
748
Scott Wood5df554ad2013-04-12 14:08:46 +0000749#endif /* CONFIG_KVM_MPIC */
750
Scott Wooddc83b8b2011-08-18 15:25:21 -0500751int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
752 struct kvm_config_tlb *cfg);
753int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
754 struct kvm_dirty_tlb *cfg);
755
Scott Wood043cc4d2011-12-20 15:34:20 +0000756long kvmppc_alloc_lpid(void);
757void kvmppc_claim_lpid(long lpid);
758void kvmppc_free_lpid(long lpid);
759void kvmppc_init_lpid(unsigned long nr_lpids);
760
Dan Williamsba049e92016-01-15 16:56:11 -0800761static inline void kvmppc_mmu_flush_icache(kvm_pfn_t pfn)
Alexander Graf249ba1e2012-08-03 13:56:33 +0200762{
Alexander Graf249ba1e2012-08-03 13:56:33 +0200763 struct page *page;
Bharat Bhushanadccf652013-04-25 06:33:57 +0000764 /*
765 * We can only access pages that the kernel maps
766 * as memory. Bail out for unmapped ones.
767 */
768 if (!pfn_valid(pfn))
769 return;
770
771 /* Clear i-cache for new pages */
Alexander Graf249ba1e2012-08-03 13:56:33 +0200772 page = pfn_to_page(pfn);
773 if (!test_bit(PG_arch_1, &page->flags)) {
774 flush_dcache_icache_page(page);
775 set_bit(PG_arch_1, &page->flags);
776 }
777}
778
Scott Wood5f1c2482013-07-10 17:47:39 -0500779/*
Alexander Graf5deb8e72014-04-24 13:46:24 +0200780 * Shared struct helpers. The shared struct can be little or big endian,
781 * depending on the guest endianness. So expose helpers to all of them.
782 */
783static inline bool kvmppc_shared_big_endian(struct kvm_vcpu *vcpu)
784{
785#if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
786 /* Only Book3S_64 PR supports bi-endian for now */
787 return vcpu->arch.shared_big_endian;
788#elif defined(CONFIG_PPC_BOOK3S_64) && defined(__LITTLE_ENDIAN__)
789 /* Book3s_64 HV on little endian is always little endian */
790 return false;
791#else
792 return true;
793#endif
794}
795
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530796#define SPRNG_WRAPPER_GET(reg, bookehv_spr) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530797static inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
798{ \
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530799 return mfspr(bookehv_spr); \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530800} \
801
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530802#define SPRNG_WRAPPER_SET(reg, bookehv_spr) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530803static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val) \
804{ \
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530805 mtspr(bookehv_spr, val); \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530806} \
807
Alexander Graf5deb8e72014-04-24 13:46:24 +0200808#define SHARED_WRAPPER_GET(reg, size) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530809static inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
Alexander Graf5deb8e72014-04-24 13:46:24 +0200810{ \
811 if (kvmppc_shared_big_endian(vcpu)) \
812 return be##size##_to_cpu(vcpu->arch.shared->reg); \
813 else \
814 return le##size##_to_cpu(vcpu->arch.shared->reg); \
815} \
816
817#define SHARED_WRAPPER_SET(reg, size) \
818static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val) \
819{ \
820 if (kvmppc_shared_big_endian(vcpu)) \
821 vcpu->arch.shared->reg = cpu_to_be##size(val); \
822 else \
823 vcpu->arch.shared->reg = cpu_to_le##size(val); \
824} \
825
826#define SHARED_WRAPPER(reg, size) \
827 SHARED_WRAPPER_GET(reg, size) \
828 SHARED_WRAPPER_SET(reg, size) \
829
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530830#define SPRNG_WRAPPER(reg, bookehv_spr) \
831 SPRNG_WRAPPER_GET(reg, bookehv_spr) \
832 SPRNG_WRAPPER_SET(reg, bookehv_spr) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530833
834#ifdef CONFIG_KVM_BOOKE_HV
835
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530836#define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
837 SPRNG_WRAPPER(reg, bookehv_spr) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530838
839#else
840
Bharat Bhushan5a484c72014-07-30 15:03:56 +0530841#define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530842 SHARED_WRAPPER(reg, size) \
843
844#endif
845
Alexander Graf5deb8e72014-04-24 13:46:24 +0200846SHARED_WRAPPER(critical, 64)
Bharat Bhushan1dc0c5b2014-07-17 17:01:35 +0530847SHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0)
848SHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1)
849SHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2)
850SHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3)
851SHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0)
852SHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1)
853SHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR)
Bharat Bhushandc168542014-07-17 17:01:38 +0530854SHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR)
Alexander Graf5deb8e72014-04-24 13:46:24 +0200855SHARED_WRAPPER_GET(msr, 64)
856static inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val)
857{
858 if (kvmppc_shared_big_endian(vcpu))
859 vcpu->arch.shared->msr = cpu_to_be64(val);
860 else
861 vcpu->arch.shared->msr = cpu_to_le64(val);
862}
863SHARED_WRAPPER(dsisr, 32)
864SHARED_WRAPPER(int_pending, 32)
865SHARED_WRAPPER(sprg4, 64)
866SHARED_WRAPPER(sprg5, 64)
867SHARED_WRAPPER(sprg6, 64)
868SHARED_WRAPPER(sprg7, 64)
869
870static inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr)
871{
872 if (kvmppc_shared_big_endian(vcpu))
873 return be32_to_cpu(vcpu->arch.shared->sr[nr]);
874 else
875 return le32_to_cpu(vcpu->arch.shared->sr[nr]);
876}
877
878static inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val)
879{
880 if (kvmppc_shared_big_endian(vcpu))
881 vcpu->arch.shared->sr[nr] = cpu_to_be32(val);
882 else
883 vcpu->arch.shared->sr[nr] = cpu_to_le32(val);
884}
885
886/*
Scott Wood5f1c2482013-07-10 17:47:39 -0500887 * Please call after prepare_to_enter. This function puts the lazy ee and irq
888 * disabled tracking state back to normal mode, without actually enabling
889 * interrupts.
890 */
891static inline void kvmppc_fix_ee_before_entry(void)
Alexander Grafbd2be682012-08-13 01:04:19 +0200892{
Scott Wood5f1c2482013-07-10 17:47:39 -0500893 trace_hardirqs_on();
894
Alexander Grafbd2be682012-08-13 01:04:19 +0200895#ifdef CONFIG_PPC64
Scott Wood6c85f522014-01-09 19:18:40 -0600896 /*
897 * To avoid races, the caller must have gone directly from having
898 * interrupts fully-enabled to hard-disabled.
899 */
900 WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS);
901
Alexander Grafbd2be682012-08-13 01:04:19 +0200902 /* Only need to enable IRQs by hard enabling them after this */
903 local_paca->irq_happened = 0;
Madhavan Srinivasan4e26bc42017-12-20 09:25:50 +0530904 irq_soft_mask_set(IRQS_ENABLED);
Alexander Grafbd2be682012-08-13 01:04:19 +0200905#endif
906}
Alexander Graf249ba1e2012-08-03 13:56:33 +0200907
Mihai Caraman7cdd7a92012-10-11 06:13:22 +0000908static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
909{
910 ulong ea;
Mihai Caraman8823a8f2012-10-11 06:13:23 +0000911 ulong msr_64bit = 0;
Mihai Caraman7cdd7a92012-10-11 06:13:22 +0000912
913 ea = kvmppc_get_gpr(vcpu, rb);
914 if (ra)
915 ea += kvmppc_get_gpr(vcpu, ra);
916
Mihai Caraman8823a8f2012-10-11 06:13:23 +0000917#if defined(CONFIG_PPC_BOOK3E_64)
918 msr_64bit = MSR_CM;
919#elif defined(CONFIG_PPC_BOOK3S_64)
920 msr_64bit = MSR_SF;
921#endif
922
Alexander Graf5deb8e72014-04-24 13:46:24 +0200923 if (!(kvmppc_get_msr(vcpu) & msr_64bit))
Mihai Caraman8823a8f2012-10-11 06:13:23 +0000924 ea = (uint32_t)ea;
925
Mihai Caraman7cdd7a92012-10-11 06:13:22 +0000926 return ea;
927}
928
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000929extern void xics_wake_cpu(int cpu);
930
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500931#endif /* __POWERPC_KVM_PPC_H__ */