kvm_ppc.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  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. #ifndef __POWERPC_KVM_PPC_H__
  20. #define __POWERPC_KVM_PPC_H__
  21. /* This file exists just so we can dereference kvm_vcpu, avoiding nested header
  22. * dependencies. */
  23. #include <linux/mutex.h>
  24. #include <linux/timer.h>
  25. #include <linux/types.h>
  26. #include <linux/kvm_types.h>
  27. #include <linux/kvm_host.h>
  28. #include <linux/bug.h>
  29. #ifdef CONFIG_PPC_BOOK3S
  30. #include <asm/kvm_book3s.h>
  31. #else
  32. #include <asm/kvm_booke.h>
  33. #endif
  34. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  35. #include <asm/paca.h>
  36. #endif
  37. /*
  38. * KVMPPC_INST_SW_BREAKPOINT is debug Instruction
  39. * for supporting software breakpoint.
  40. */
  41. #define KVMPPC_INST_SW_BREAKPOINT 0x00dddd00
  42. enum emulation_result {
  43. EMULATE_DONE, /* no further processing */
  44. EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */
  45. EMULATE_FAIL, /* can't emulate this instruction */
  46. EMULATE_AGAIN, /* something went wrong. go again */
  47. EMULATE_EXIT_USER, /* emulation requires exit to user-space */
  48. };
  49. enum instruction_type {
  50. INST_GENERIC,
  51. INST_SC, /* system call */
  52. };
  53. enum xlate_instdata {
  54. XLATE_INST, /* translate instruction address */
  55. XLATE_DATA /* translate data address */
  56. };
  57. enum xlate_readwrite {
  58. XLATE_READ, /* check for read permissions */
  59. XLATE_WRITE /* check for write permissions */
  60. };
  61. extern int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  62. extern int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  63. extern void kvmppc_handler_highmem(void);
  64. extern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu);
  65. extern int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  66. unsigned int rt, unsigned int bytes,
  67. int is_default_endian);
  68. extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  69. unsigned int rt, unsigned int bytes,
  70. int is_default_endian);
  71. extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  72. u64 val, unsigned int bytes,
  73. int is_default_endian);
  74. extern int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
  75. enum instruction_type type, u32 *inst);
  76. extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  77. bool data);
  78. extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  79. bool data);
  80. extern int kvmppc_emulate_instruction(struct kvm_run *run,
  81. struct kvm_vcpu *vcpu);
  82. extern int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu);
  83. extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
  84. extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
  85. extern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb);
  86. extern void kvmppc_decrementer_func(struct kvm_vcpu *vcpu);
  87. extern int kvmppc_sanity_check(struct kvm_vcpu *vcpu);
  88. extern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu);
  89. extern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu);
  90. /* Core-specific hooks */
  91. extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
  92. unsigned int gtlb_idx);
  93. extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
  94. extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
  95. extern void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu);
  96. extern int kvmppc_mmu_init(struct kvm_vcpu *vcpu);
  97. extern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
  98. extern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
  99. extern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
  100. gva_t eaddr);
  101. extern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu);
  102. extern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu);
  103. extern int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr,
  104. enum xlate_instdata xlid, enum xlate_readwrite xlrw,
  105. struct kvmppc_pte *pte);
  106. extern struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm,
  107. unsigned int id);
  108. extern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu);
  109. extern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu);
  110. extern int kvmppc_core_check_processor_compat(void);
  111. extern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  112. struct kvm_translation *tr);
  113. extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  114. extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
  115. extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
  116. extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
  117. extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags);
  118. extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
  119. extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
  120. extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
  121. struct kvm_interrupt *irq);
  122. extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
  123. extern void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu, ulong dear_flags,
  124. ulong esr_flags);
  125. extern void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
  126. ulong dear_flags,
  127. ulong esr_flags);
  128. extern void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu);
  129. extern void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
  130. ulong esr_flags);
  131. extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
  132. extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
  133. extern int kvmppc_booke_init(void);
  134. extern void kvmppc_booke_exit(void);
  135. extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
  136. extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
  137. extern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
  138. extern long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp);
  139. extern long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp);
  140. extern void kvmppc_free_hpt(struct kvm *kvm);
  141. extern long kvmppc_prepare_vrma(struct kvm *kvm,
  142. struct kvm_userspace_memory_region *mem);
  143. extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
  144. struct kvm_memory_slot *memslot, unsigned long porder);
  145. extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
  146. extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
  147. struct kvm_create_spapr_tce_64 *args);
  148. extern struct kvmppc_spapr_tce_table *kvmppc_find_table(
  149. struct kvm_vcpu *vcpu, unsigned long liobn);
  150. extern long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table *stt,
  151. unsigned long ioba, unsigned long npages);
  152. extern long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *tt,
  153. unsigned long tce);
  154. extern long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,
  155. unsigned long *ua, unsigned long **prmap);
  156. extern void kvmppc_tce_put(struct kvmppc_spapr_tce_table *tt,
  157. unsigned long idx, unsigned long tce);
  158. extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
  159. unsigned long ioba, unsigned long tce);
  160. extern long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
  161. unsigned long liobn, unsigned long ioba,
  162. unsigned long tce_list, unsigned long npages);
  163. extern long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
  164. unsigned long liobn, unsigned long ioba,
  165. unsigned long tce_value, unsigned long npages);
  166. extern long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
  167. unsigned long ioba);
  168. extern struct page *kvm_alloc_hpt(unsigned long nr_pages);
  169. extern void kvm_release_hpt(struct page *page, unsigned long nr_pages);
  170. extern int kvmppc_core_init_vm(struct kvm *kvm);
  171. extern void kvmppc_core_destroy_vm(struct kvm *kvm);
  172. extern void kvmppc_core_free_memslot(struct kvm *kvm,
  173. struct kvm_memory_slot *free,
  174. struct kvm_memory_slot *dont);
  175. extern int kvmppc_core_create_memslot(struct kvm *kvm,
  176. struct kvm_memory_slot *slot,
  177. unsigned long npages);
  178. extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  179. struct kvm_memory_slot *memslot,
  180. const struct kvm_userspace_memory_region *mem);
  181. extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
  182. const struct kvm_userspace_memory_region *mem,
  183. const struct kvm_memory_slot *old,
  184. const struct kvm_memory_slot *new);
  185. extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
  186. struct kvm_ppc_smmu_info *info);
  187. extern void kvmppc_core_flush_memslot(struct kvm *kvm,
  188. struct kvm_memory_slot *memslot);
  189. extern int kvmppc_bookehv_init(void);
  190. extern void kvmppc_bookehv_exit(void);
  191. extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
  192. extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
  193. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
  194. extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
  195. extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
  196. extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
  197. extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server,
  198. u32 priority);
  199. extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server,
  200. u32 *priority);
  201. extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
  202. extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
  203. void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu);
  204. void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu);
  205. union kvmppc_one_reg {
  206. u32 wval;
  207. u64 dval;
  208. vector128 vval;
  209. u64 vsxval[2];
  210. struct {
  211. u64 addr;
  212. u64 length;
  213. } vpaval;
  214. };
  215. struct kvmppc_ops {
  216. struct module *owner;
  217. int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  218. int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  219. int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id,
  220. union kvmppc_one_reg *val);
  221. int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id,
  222. union kvmppc_one_reg *val);
  223. void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
  224. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  225. void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr);
  226. int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu);
  227. struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned int id);
  228. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  229. int (*check_requests)(struct kvm_vcpu *vcpu);
  230. int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log);
  231. void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot);
  232. int (*prepare_memory_region)(struct kvm *kvm,
  233. struct kvm_memory_slot *memslot,
  234. const struct kvm_userspace_memory_region *mem);
  235. void (*commit_memory_region)(struct kvm *kvm,
  236. const struct kvm_userspace_memory_region *mem,
  237. const struct kvm_memory_slot *old,
  238. const struct kvm_memory_slot *new);
  239. int (*unmap_hva)(struct kvm *kvm, unsigned long hva);
  240. int (*unmap_hva_range)(struct kvm *kvm, unsigned long start,
  241. unsigned long end);
  242. int (*age_hva)(struct kvm *kvm, unsigned long start, unsigned long end);
  243. int (*test_age_hva)(struct kvm *kvm, unsigned long hva);
  244. void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte);
  245. void (*mmu_destroy)(struct kvm_vcpu *vcpu);
  246. void (*free_memslot)(struct kvm_memory_slot *free,
  247. struct kvm_memory_slot *dont);
  248. int (*create_memslot)(struct kvm_memory_slot *slot,
  249. unsigned long npages);
  250. int (*init_vm)(struct kvm *kvm);
  251. void (*destroy_vm)(struct kvm *kvm);
  252. int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info);
  253. int (*emulate_op)(struct kvm_run *run, struct kvm_vcpu *vcpu,
  254. unsigned int inst, int *advance);
  255. int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val);
  256. int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val);
  257. void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu);
  258. long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl,
  259. unsigned long arg);
  260. int (*hcall_implemented)(unsigned long hcall);
  261. int (*irq_bypass_add_producer)(struct irq_bypass_consumer *,
  262. struct irq_bypass_producer *);
  263. void (*irq_bypass_del_producer)(struct irq_bypass_consumer *,
  264. struct irq_bypass_producer *);
  265. };
  266. extern struct kvmppc_ops *kvmppc_hv_ops;
  267. extern struct kvmppc_ops *kvmppc_pr_ops;
  268. static inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu,
  269. enum instruction_type type, u32 *inst)
  270. {
  271. int ret = EMULATE_DONE;
  272. u32 fetched_inst;
  273. /* Load the instruction manually if it failed to do so in the
  274. * exit path */
  275. if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
  276. ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst);
  277. /* Write fetch_failed unswapped if the fetch failed */
  278. if (ret == EMULATE_DONE)
  279. fetched_inst = kvmppc_need_byteswap(vcpu) ?
  280. swab32(vcpu->arch.last_inst) :
  281. vcpu->arch.last_inst;
  282. else
  283. fetched_inst = vcpu->arch.last_inst;
  284. *inst = fetched_inst;
  285. return ret;
  286. }
  287. static inline bool is_kvmppc_hv_enabled(struct kvm *kvm)
  288. {
  289. return kvm->arch.kvm_ops == kvmppc_hv_ops;
  290. }
  291. extern int kvmppc_hwrng_present(void);
  292. /*
  293. * Cuts out inst bits with ordering according to spec.
  294. * That means the leftmost bit is zero. All given bits are included.
  295. */
  296. static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
  297. {
  298. u32 r;
  299. u32 mask;
  300. BUG_ON(msb > lsb);
  301. mask = (1 << (lsb - msb + 1)) - 1;
  302. r = (inst >> (63 - lsb)) & mask;
  303. return r;
  304. }
  305. /*
  306. * Replaces inst bits with ordering according to spec.
  307. */
  308. static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
  309. {
  310. u32 r;
  311. u32 mask;
  312. BUG_ON(msb > lsb);
  313. mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
  314. r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
  315. return r;
  316. }
  317. #define one_reg_size(id) \
  318. (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
  319. #define get_reg_val(id, reg) ({ \
  320. union kvmppc_one_reg __u; \
  321. switch (one_reg_size(id)) { \
  322. case 4: __u.wval = (reg); break; \
  323. case 8: __u.dval = (reg); break; \
  324. default: BUG(); \
  325. } \
  326. __u; \
  327. })
  328. #define set_reg_val(id, val) ({ \
  329. u64 __v; \
  330. switch (one_reg_size(id)) { \
  331. case 4: __v = (val).wval; break; \
  332. case 8: __v = (val).dval; break; \
  333. default: BUG(); \
  334. } \
  335. __v; \
  336. })
  337. int kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  338. int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  339. int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  340. int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  341. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
  342. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
  343. int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
  344. int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
  345. void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
  346. struct openpic;
  347. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  348. extern void kvm_cma_reserve(void) __init;
  349. static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
  350. {
  351. paca[cpu].kvm_hstate.xics_phys = addr;
  352. }
  353. static inline u32 kvmppc_get_xics_latch(void)
  354. {
  355. u32 xirr;
  356. xirr = get_paca()->kvm_hstate.saved_xirr;
  357. get_paca()->kvm_hstate.saved_xirr = 0;
  358. return xirr;
  359. }
  360. static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
  361. {
  362. paca[cpu].kvm_hstate.host_ipi = host_ipi;
  363. }
  364. static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
  365. {
  366. vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu);
  367. }
  368. extern void kvm_hv_vm_activated(void);
  369. extern void kvm_hv_vm_deactivated(void);
  370. extern bool kvm_hv_mode_active(void);
  371. #else
  372. static inline void __init kvm_cma_reserve(void)
  373. {}
  374. static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
  375. {}
  376. static inline u32 kvmppc_get_xics_latch(void)
  377. {
  378. return 0;
  379. }
  380. static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
  381. {}
  382. static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
  383. {
  384. kvm_vcpu_kick(vcpu);
  385. }
  386. static inline bool kvm_hv_mode_active(void) { return false; }
  387. #endif
  388. #ifdef CONFIG_KVM_XICS
  389. static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
  390. {
  391. return vcpu->arch.irq_type == KVMPPC_IRQ_XICS;
  392. }
  393. static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
  394. struct kvm *kvm)
  395. {
  396. if (kvm && kvm_irq_bypass)
  397. return kvm->arch.pimap;
  398. return NULL;
  399. }
  400. extern void kvmppc_alloc_host_rm_ops(void);
  401. extern void kvmppc_free_host_rm_ops(void);
  402. extern void kvmppc_free_pimap(struct kvm *kvm);
  403. extern int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall);
  404. extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
  405. extern int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server);
  406. extern int kvm_vm_ioctl_xics_irq(struct kvm *kvm, struct kvm_irq_level *args);
  407. extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
  408. extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
  409. extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
  410. extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
  411. struct kvm_vcpu *vcpu, u32 cpu);
  412. extern void kvmppc_xics_ipi_action(void);
  413. extern void kvmppc_xics_set_mapped(struct kvm *kvm, unsigned long guest_irq,
  414. unsigned long host_irq);
  415. extern void kvmppc_xics_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
  416. unsigned long host_irq);
  417. extern long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu, u32 xirr,
  418. struct kvmppc_irq_map *irq_map,
  419. struct kvmppc_passthru_irqmap *pimap);
  420. extern int h_ipi_redirect;
  421. #else
  422. static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
  423. struct kvm *kvm)
  424. { return NULL; }
  425. static inline void kvmppc_alloc_host_rm_ops(void) {};
  426. static inline void kvmppc_free_host_rm_ops(void) {};
  427. static inline void kvmppc_free_pimap(struct kvm *kvm) {};
  428. static inline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
  429. { return 0; }
  430. static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
  431. { return 0; }
  432. static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { }
  433. static inline int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu,
  434. unsigned long server)
  435. { return -EINVAL; }
  436. static inline int kvm_vm_ioctl_xics_irq(struct kvm *kvm,
  437. struct kvm_irq_level *args)
  438. { return -ENOTTY; }
  439. static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
  440. { return 0; }
  441. #endif
  442. /*
  443. * Host-side operations we want to set up while running in real
  444. * mode in the guest operating on the xics.
  445. * Currently only VCPU wakeup is supported.
  446. */
  447. union kvmppc_rm_state {
  448. unsigned long raw;
  449. struct {
  450. u32 in_host;
  451. u32 rm_action;
  452. };
  453. };
  454. struct kvmppc_host_rm_core {
  455. union kvmppc_rm_state rm_state;
  456. void *rm_data;
  457. char pad[112];
  458. };
  459. struct kvmppc_host_rm_ops {
  460. struct kvmppc_host_rm_core *rm_core;
  461. void (*vcpu_kick)(struct kvm_vcpu *vcpu);
  462. };
  463. extern struct kvmppc_host_rm_ops *kvmppc_host_rm_ops_hv;
  464. static inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu)
  465. {
  466. #ifdef CONFIG_KVM_BOOKE_HV
  467. return mfspr(SPRN_GEPR);
  468. #elif defined(CONFIG_BOOKE)
  469. return vcpu->arch.epr;
  470. #else
  471. return 0;
  472. #endif
  473. }
  474. static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
  475. {
  476. #ifdef CONFIG_KVM_BOOKE_HV
  477. mtspr(SPRN_GEPR, epr);
  478. #elif defined(CONFIG_BOOKE)
  479. vcpu->arch.epr = epr;
  480. #endif
  481. }
  482. #ifdef CONFIG_KVM_MPIC
  483. void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu);
  484. int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
  485. u32 cpu);
  486. void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu);
  487. #else
  488. static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu)
  489. {
  490. }
  491. static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev,
  492. struct kvm_vcpu *vcpu, u32 cpu)
  493. {
  494. return -EINVAL;
  495. }
  496. static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp,
  497. struct kvm_vcpu *vcpu)
  498. {
  499. }
  500. #endif /* CONFIG_KVM_MPIC */
  501. int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
  502. struct kvm_config_tlb *cfg);
  503. int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
  504. struct kvm_dirty_tlb *cfg);
  505. long kvmppc_alloc_lpid(void);
  506. void kvmppc_claim_lpid(long lpid);
  507. void kvmppc_free_lpid(long lpid);
  508. void kvmppc_init_lpid(unsigned long nr_lpids);
  509. static inline void kvmppc_mmu_flush_icache(kvm_pfn_t pfn)
  510. {
  511. struct page *page;
  512. /*
  513. * We can only access pages that the kernel maps
  514. * as memory. Bail out for unmapped ones.
  515. */
  516. if (!pfn_valid(pfn))
  517. return;
  518. /* Clear i-cache for new pages */
  519. page = pfn_to_page(pfn);
  520. if (!test_bit(PG_arch_1, &page->flags)) {
  521. flush_dcache_icache_page(page);
  522. set_bit(PG_arch_1, &page->flags);
  523. }
  524. }
  525. /*
  526. * 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. */
  529. static 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. #define SPRNG_WRAPPER_GET(reg, bookehv_spr) \
  542. static inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
  543. { \
  544. return mfspr(bookehv_spr); \
  545. } \
  546. #define SPRNG_WRAPPER_SET(reg, bookehv_spr) \
  547. static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val) \
  548. { \
  549. mtspr(bookehv_spr, val); \
  550. } \
  551. #define SHARED_WRAPPER_GET(reg, size) \
  552. static inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
  553. { \
  554. if (kvmppc_shared_big_endian(vcpu)) \
  555. return be##size##_to_cpu(vcpu->arch.shared->reg); \
  556. else \
  557. return le##size##_to_cpu(vcpu->arch.shared->reg); \
  558. } \
  559. #define SHARED_WRAPPER_SET(reg, size) \
  560. static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val) \
  561. { \
  562. if (kvmppc_shared_big_endian(vcpu)) \
  563. vcpu->arch.shared->reg = cpu_to_be##size(val); \
  564. else \
  565. vcpu->arch.shared->reg = cpu_to_le##size(val); \
  566. } \
  567. #define SHARED_WRAPPER(reg, size) \
  568. SHARED_WRAPPER_GET(reg, size) \
  569. SHARED_WRAPPER_SET(reg, size) \
  570. #define SPRNG_WRAPPER(reg, bookehv_spr) \
  571. SPRNG_WRAPPER_GET(reg, bookehv_spr) \
  572. SPRNG_WRAPPER_SET(reg, bookehv_spr) \
  573. #ifdef CONFIG_KVM_BOOKE_HV
  574. #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
  575. SPRNG_WRAPPER(reg, bookehv_spr) \
  576. #else
  577. #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
  578. SHARED_WRAPPER(reg, size) \
  579. #endif
  580. SHARED_WRAPPER(critical, 64)
  581. SHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0)
  582. SHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1)
  583. SHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2)
  584. SHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3)
  585. SHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0)
  586. SHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1)
  587. SHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR)
  588. SHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR)
  589. SHARED_WRAPPER_GET(msr, 64)
  590. static inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val)
  591. {
  592. if (kvmppc_shared_big_endian(vcpu))
  593. vcpu->arch.shared->msr = cpu_to_be64(val);
  594. else
  595. vcpu->arch.shared->msr = cpu_to_le64(val);
  596. }
  597. SHARED_WRAPPER(dsisr, 32)
  598. SHARED_WRAPPER(int_pending, 32)
  599. SHARED_WRAPPER(sprg4, 64)
  600. SHARED_WRAPPER(sprg5, 64)
  601. SHARED_WRAPPER(sprg6, 64)
  602. SHARED_WRAPPER(sprg7, 64)
  603. static inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr)
  604. {
  605. if (kvmppc_shared_big_endian(vcpu))
  606. return be32_to_cpu(vcpu->arch.shared->sr[nr]);
  607. else
  608. return le32_to_cpu(vcpu->arch.shared->sr[nr]);
  609. }
  610. static inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val)
  611. {
  612. if (kvmppc_shared_big_endian(vcpu))
  613. vcpu->arch.shared->sr[nr] = cpu_to_be32(val);
  614. else
  615. vcpu->arch.shared->sr[nr] = cpu_to_le32(val);
  616. }
  617. /*
  618. * Please call after prepare_to_enter. This function puts the lazy ee and irq
  619. * disabled tracking state back to normal mode, without actually enabling
  620. * interrupts.
  621. */
  622. static inline void kvmppc_fix_ee_before_entry(void)
  623. {
  624. trace_hardirqs_on();
  625. #ifdef CONFIG_PPC64
  626. /*
  627. * To avoid races, the caller must have gone directly from having
  628. * interrupts fully-enabled to hard-disabled.
  629. */
  630. WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS);
  631. /* Only need to enable IRQs by hard enabling them after this */
  632. local_paca->irq_happened = 0;
  633. local_paca->soft_enabled = 1;
  634. #endif
  635. }
  636. static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
  637. {
  638. ulong ea;
  639. ulong msr_64bit = 0;
  640. ea = kvmppc_get_gpr(vcpu, rb);
  641. if (ra)
  642. ea += kvmppc_get_gpr(vcpu, ra);
  643. #if defined(CONFIG_PPC_BOOK3E_64)
  644. msr_64bit = MSR_CM;
  645. #elif defined(CONFIG_PPC_BOOK3S_64)
  646. msr_64bit = MSR_SF;
  647. #endif
  648. if (!(kvmppc_get_msr(vcpu) & msr_64bit))
  649. ea = (uint32_t)ea;
  650. return ea;
  651. }
  652. extern void xics_wake_cpu(int cpu);
  653. #endif /* __POWERPC_KVM_PPC_H__ */