kvm_ppc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. enum emulation_result {
  38. EMULATE_DONE, /* no further processing */
  39. EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */
  40. EMULATE_DO_DCR, /* kvm_run filled with DCR request */
  41. EMULATE_FAIL, /* can't emulate this instruction */
  42. EMULATE_AGAIN, /* something went wrong. go again */
  43. EMULATE_EXIT_USER, /* emulation requires exit to user-space */
  44. };
  45. extern int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  46. extern int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  47. extern void kvmppc_handler_highmem(void);
  48. extern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu);
  49. extern int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  50. unsigned int rt, unsigned int bytes,
  51. int is_bigendian);
  52. extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  53. unsigned int rt, unsigned int bytes,
  54. int is_bigendian);
  55. extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  56. u64 val, unsigned int bytes, int is_bigendian);
  57. extern int kvmppc_emulate_instruction(struct kvm_run *run,
  58. struct kvm_vcpu *vcpu);
  59. extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
  60. extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
  61. extern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb);
  62. extern void kvmppc_decrementer_func(unsigned long data);
  63. extern int kvmppc_sanity_check(struct kvm_vcpu *vcpu);
  64. extern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu);
  65. extern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu);
  66. /* Core-specific hooks */
  67. extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
  68. unsigned int gtlb_idx);
  69. extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
  70. extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
  71. extern void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu);
  72. extern int kvmppc_mmu_init(struct kvm_vcpu *vcpu);
  73. extern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
  74. extern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
  75. extern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
  76. gva_t eaddr);
  77. extern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu);
  78. extern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu);
  79. extern struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm,
  80. unsigned int id);
  81. extern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu);
  82. extern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu);
  83. extern int kvmppc_core_check_processor_compat(void);
  84. extern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
  85. struct kvm_translation *tr);
  86. extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  87. extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
  88. extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
  89. extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
  90. extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags);
  91. extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
  92. extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
  93. extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
  94. struct kvm_interrupt *irq);
  95. extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
  96. extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
  97. extern int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  98. unsigned int op, int *advance);
  99. extern int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn,
  100. ulong val);
  101. extern int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn,
  102. ulong *val);
  103. extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
  104. extern int kvmppc_booke_init(void);
  105. extern void kvmppc_booke_exit(void);
  106. extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
  107. extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
  108. extern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
  109. extern long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp);
  110. extern long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp);
  111. extern void kvmppc_free_hpt(struct kvm *kvm);
  112. extern long kvmppc_prepare_vrma(struct kvm *kvm,
  113. struct kvm_userspace_memory_region *mem);
  114. extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
  115. struct kvm_memory_slot *memslot, unsigned long porder);
  116. extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
  117. extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
  118. struct kvm_create_spapr_tce *args);
  119. extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
  120. unsigned long ioba, unsigned long tce);
  121. extern long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
  122. struct kvm_allocate_rma *rma);
  123. extern struct kvmppc_linear_info *kvm_alloc_rma(void);
  124. extern void kvm_release_rma(struct kvmppc_linear_info *ri);
  125. extern struct kvmppc_linear_info *kvm_alloc_hpt(void);
  126. extern void kvm_release_hpt(struct kvmppc_linear_info *li);
  127. extern int kvmppc_core_init_vm(struct kvm *kvm);
  128. extern void kvmppc_core_destroy_vm(struct kvm *kvm);
  129. extern void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
  130. struct kvm_memory_slot *dont);
  131. extern int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
  132. unsigned long npages);
  133. extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  134. struct kvm_memory_slot *memslot,
  135. struct kvm_userspace_memory_region *mem);
  136. extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
  137. struct kvm_userspace_memory_region *mem,
  138. const struct kvm_memory_slot *old);
  139. extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
  140. struct kvm_ppc_smmu_info *info);
  141. extern void kvmppc_core_flush_memslot(struct kvm *kvm,
  142. struct kvm_memory_slot *memslot);
  143. extern int kvmppc_bookehv_init(void);
  144. extern void kvmppc_bookehv_exit(void);
  145. extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
  146. extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
  147. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
  148. extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
  149. extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
  150. extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
  151. extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server,
  152. u32 priority);
  153. extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server,
  154. u32 *priority);
  155. extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
  156. extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
  157. /*
  158. * Cuts out inst bits with ordering according to spec.
  159. * That means the leftmost bit is zero. All given bits are included.
  160. */
  161. static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
  162. {
  163. u32 r;
  164. u32 mask;
  165. BUG_ON(msb > lsb);
  166. mask = (1 << (lsb - msb + 1)) - 1;
  167. r = (inst >> (63 - lsb)) & mask;
  168. return r;
  169. }
  170. /*
  171. * Replaces inst bits with ordering according to spec.
  172. */
  173. static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
  174. {
  175. u32 r;
  176. u32 mask;
  177. BUG_ON(msb > lsb);
  178. mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
  179. r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
  180. return r;
  181. }
  182. union kvmppc_one_reg {
  183. u32 wval;
  184. u64 dval;
  185. vector128 vval;
  186. u64 vsxval[2];
  187. struct {
  188. u64 addr;
  189. u64 length;
  190. } vpaval;
  191. };
  192. #define one_reg_size(id) \
  193. (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
  194. #define get_reg_val(id, reg) ({ \
  195. union kvmppc_one_reg __u; \
  196. switch (one_reg_size(id)) { \
  197. case 4: __u.wval = (reg); break; \
  198. case 8: __u.dval = (reg); break; \
  199. default: BUG(); \
  200. } \
  201. __u; \
  202. })
  203. #define set_reg_val(id, val) ({ \
  204. u64 __v; \
  205. switch (one_reg_size(id)) { \
  206. case 4: __v = (val).wval; break; \
  207. case 8: __v = (val).dval; break; \
  208. default: BUG(); \
  209. } \
  210. __v; \
  211. })
  212. void kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  213. int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  214. void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  215. int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
  216. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
  217. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
  218. int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
  219. int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
  220. void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
  221. struct openpic;
  222. #ifdef CONFIG_KVM_BOOK3S_64_HV
  223. static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
  224. {
  225. paca[cpu].kvm_hstate.xics_phys = addr;
  226. }
  227. static inline u32 kvmppc_get_xics_latch(void)
  228. {
  229. u32 xirr = get_paca()->kvm_hstate.saved_xirr;
  230. get_paca()->kvm_hstate.saved_xirr = 0;
  231. return xirr;
  232. }
  233. static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
  234. {
  235. paca[cpu].kvm_hstate.host_ipi = host_ipi;
  236. }
  237. extern void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu);
  238. extern void kvm_linear_init(void);
  239. #else
  240. static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
  241. {}
  242. static inline void kvm_linear_init(void)
  243. {}
  244. static inline u32 kvmppc_get_xics_latch(void)
  245. {
  246. return 0;
  247. }
  248. static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
  249. {}
  250. static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
  251. {
  252. kvm_vcpu_kick(vcpu);
  253. }
  254. #endif
  255. #ifdef CONFIG_KVM_XICS
  256. static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
  257. {
  258. return vcpu->arch.irq_type == KVMPPC_IRQ_XICS;
  259. }
  260. extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
  261. extern int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server);
  262. extern int kvm_vm_ioctl_xics_irq(struct kvm *kvm, struct kvm_irq_level *args);
  263. extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
  264. extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
  265. extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
  266. extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
  267. struct kvm_vcpu *vcpu, u32 cpu);
  268. #else
  269. static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
  270. { return 0; }
  271. static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { }
  272. static inline int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu,
  273. unsigned long server)
  274. { return -EINVAL; }
  275. static inline int kvm_vm_ioctl_xics_irq(struct kvm *kvm,
  276. struct kvm_irq_level *args)
  277. { return -ENOTTY; }
  278. static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
  279. { return 0; }
  280. #endif
  281. static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
  282. {
  283. #ifdef CONFIG_KVM_BOOKE_HV
  284. mtspr(SPRN_GEPR, epr);
  285. #elif defined(CONFIG_BOOKE)
  286. vcpu->arch.epr = epr;
  287. #endif
  288. }
  289. #ifdef CONFIG_KVM_MPIC
  290. void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu);
  291. int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
  292. u32 cpu);
  293. void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu);
  294. #else
  295. static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu)
  296. {
  297. }
  298. static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev,
  299. struct kvm_vcpu *vcpu, u32 cpu)
  300. {
  301. return -EINVAL;
  302. }
  303. static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp,
  304. struct kvm_vcpu *vcpu)
  305. {
  306. }
  307. #endif /* CONFIG_KVM_MPIC */
  308. int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
  309. struct kvm_config_tlb *cfg);
  310. int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
  311. struct kvm_dirty_tlb *cfg);
  312. long kvmppc_alloc_lpid(void);
  313. void kvmppc_claim_lpid(long lpid);
  314. void kvmppc_free_lpid(long lpid);
  315. void kvmppc_init_lpid(unsigned long nr_lpids);
  316. static inline void kvmppc_mmu_flush_icache(pfn_t pfn)
  317. {
  318. struct page *page;
  319. /*
  320. * We can only access pages that the kernel maps
  321. * as memory. Bail out for unmapped ones.
  322. */
  323. if (!pfn_valid(pfn))
  324. return;
  325. /* Clear i-cache for new pages */
  326. page = pfn_to_page(pfn);
  327. if (!test_bit(PG_arch_1, &page->flags)) {
  328. flush_dcache_icache_page(page);
  329. set_bit(PG_arch_1, &page->flags);
  330. }
  331. }
  332. /* Please call after prepare_to_enter. This function puts the lazy ee state
  333. back to normal mode, without actually enabling interrupts. */
  334. static inline void kvmppc_lazy_ee_enable(void)
  335. {
  336. #ifdef CONFIG_PPC64
  337. /* Only need to enable IRQs by hard enabling them after this */
  338. local_paca->irq_happened = 0;
  339. local_paca->soft_enabled = 1;
  340. #endif
  341. }
  342. static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
  343. {
  344. ulong ea;
  345. ulong msr_64bit = 0;
  346. ea = kvmppc_get_gpr(vcpu, rb);
  347. if (ra)
  348. ea += kvmppc_get_gpr(vcpu, ra);
  349. #if defined(CONFIG_PPC_BOOK3E_64)
  350. msr_64bit = MSR_CM;
  351. #elif defined(CONFIG_PPC_BOOK3S_64)
  352. msr_64bit = MSR_SF;
  353. #endif
  354. if (!(vcpu->arch.shared->msr & msr_64bit))
  355. ea = (uint32_t)ea;
  356. return ea;
  357. }
  358. extern void xics_wake_cpu(int cpu);
  359. #endif /* __POWERPC_KVM_PPC_H__ */