kvm_book3s.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 SUSE Linux Products GmbH 2009
  16. *
  17. * Authors: Alexander Graf <agraf@suse.de>
  18. */
  19. #ifndef __ASM_KVM_BOOK3S_H__
  20. #define __ASM_KVM_BOOK3S_H__
  21. #include <linux/types.h>
  22. #include <linux/kvm_host.h>
  23. #include <asm/kvm_book3s_asm.h>
  24. struct kvmppc_bat {
  25. u64 raw;
  26. u32 bepi;
  27. u32 bepi_mask;
  28. u32 brpn;
  29. u8 wimg;
  30. u8 pp;
  31. bool vs : 1;
  32. bool vp : 1;
  33. };
  34. struct kvmppc_sid_map {
  35. u64 guest_vsid;
  36. u64 guest_esid;
  37. u64 host_vsid;
  38. bool valid : 1;
  39. };
  40. #define SID_MAP_BITS 9
  41. #define SID_MAP_NUM (1 << SID_MAP_BITS)
  42. #define SID_MAP_MASK (SID_MAP_NUM - 1)
  43. #ifdef CONFIG_PPC_BOOK3S_64
  44. #define SID_CONTEXTS 1
  45. #else
  46. #define SID_CONTEXTS 128
  47. #define VSID_POOL_SIZE (SID_CONTEXTS * 16)
  48. #endif
  49. struct hpte_cache {
  50. struct hlist_node list_pte;
  51. struct hlist_node list_pte_long;
  52. struct hlist_node list_vpte;
  53. struct hlist_node list_vpte_long;
  54. #ifdef CONFIG_PPC_BOOK3S_64
  55. struct hlist_node list_vpte_64k;
  56. #endif
  57. struct rcu_head rcu_head;
  58. u64 host_vpn;
  59. u64 pfn;
  60. ulong slot;
  61. struct kvmppc_pte pte;
  62. int pagesize;
  63. };
  64. /*
  65. * Struct for a virtual core.
  66. * Note: entry_exit_map combines a bitmap of threads that have entered
  67. * in the bottom 8 bits and a bitmap of threads that have exited in the
  68. * next 8 bits. This is so that we can atomically set the entry bit
  69. * iff the exit map is 0 without taking a lock.
  70. */
  71. struct kvmppc_vcore {
  72. int n_runnable;
  73. int num_threads;
  74. int entry_exit_map;
  75. int napping_threads;
  76. int first_vcpuid;
  77. u16 pcpu;
  78. u16 last_cpu;
  79. u8 vcore_state;
  80. u8 in_guest;
  81. struct kvm_vcpu *runnable_threads[MAX_SMT_THREADS];
  82. struct list_head preempt_list;
  83. spinlock_t lock;
  84. struct swait_queue_head wq;
  85. spinlock_t stoltb_lock; /* protects stolen_tb and preempt_tb */
  86. u64 stolen_tb;
  87. u64 preempt_tb;
  88. struct kvm_vcpu *runner;
  89. struct kvm *kvm;
  90. u64 tb_offset; /* guest timebase - host timebase */
  91. u64 tb_offset_applied; /* timebase offset currently in force */
  92. ulong lpcr;
  93. u32 arch_compat;
  94. ulong pcr;
  95. ulong dpdes; /* doorbell state (POWER8) */
  96. ulong vtb; /* virtual timebase */
  97. ulong conferring_threads;
  98. unsigned int halt_poll_ns;
  99. atomic_t online_count;
  100. };
  101. struct kvmppc_vcpu_book3s {
  102. struct kvmppc_sid_map sid_map[SID_MAP_NUM];
  103. struct {
  104. u64 esid;
  105. u64 vsid;
  106. } slb_shadow[64];
  107. u8 slb_shadow_max;
  108. struct kvmppc_bat ibat[8];
  109. struct kvmppc_bat dbat[8];
  110. u64 hid[6];
  111. u64 gqr[8];
  112. u64 sdr1;
  113. u64 hior;
  114. u64 msr_mask;
  115. u64 vtb;
  116. #ifdef CONFIG_PPC_BOOK3S_32
  117. u32 vsid_pool[VSID_POOL_SIZE];
  118. u32 vsid_next;
  119. #else
  120. u64 proto_vsid_first;
  121. u64 proto_vsid_max;
  122. u64 proto_vsid_next;
  123. #endif
  124. int context_id[SID_CONTEXTS];
  125. bool hior_explicit; /* HIOR is set by ioctl, not PVR */
  126. struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE];
  127. struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG];
  128. struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
  129. struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
  130. #ifdef CONFIG_PPC_BOOK3S_64
  131. struct hlist_head hpte_hash_vpte_64k[HPTEG_HASH_NUM_VPTE_64K];
  132. #endif
  133. int hpte_cache_count;
  134. spinlock_t mmu_lock;
  135. };
  136. #define VSID_REAL 0x07ffffffffc00000ULL
  137. #define VSID_BAT 0x07ffffffffb00000ULL
  138. #define VSID_64K 0x0800000000000000ULL
  139. #define VSID_1T 0x1000000000000000ULL
  140. #define VSID_REAL_DR 0x2000000000000000ULL
  141. #define VSID_REAL_IR 0x4000000000000000ULL
  142. #define VSID_PR 0x8000000000000000ULL
  143. extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask);
  144. extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
  145. extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end);
  146. extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
  147. extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
  148. extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
  149. extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu);
  150. extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
  151. bool iswrite);
  152. extern void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
  153. extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
  154. extern void kvmppc_mmu_flush_segment(struct kvm_vcpu *vcpu, ulong eaddr, ulong seg_size);
  155. extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
  156. extern int kvmppc_book3s_hv_page_fault(struct kvm_run *run,
  157. struct kvm_vcpu *vcpu, unsigned long addr,
  158. unsigned long status);
  159. extern long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr,
  160. unsigned long slb_v, unsigned long valid);
  161. extern int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
  162. unsigned long gpa, gva_t ea, int is_store);
  163. extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
  164. extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
  165. extern void kvmppc_mmu_hpte_cache_free(struct hpte_cache *pte);
  166. extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
  167. extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
  168. extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
  169. extern int kvmppc_mmu_hpte_sysinit(void);
  170. extern void kvmppc_mmu_hpte_sysexit(void);
  171. extern int kvmppc_mmu_hv_init(void);
  172. extern int kvmppc_book3s_hcall_implemented(struct kvm *kvm, unsigned long hc);
  173. extern int kvmppc_book3s_radix_page_fault(struct kvm_run *run,
  174. struct kvm_vcpu *vcpu,
  175. unsigned long ea, unsigned long dsisr);
  176. extern int kvmppc_mmu_walk_radix_tree(struct kvm_vcpu *vcpu, gva_t eaddr,
  177. struct kvmppc_pte *gpte, u64 root,
  178. u64 *pte_ret_p);
  179. extern int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr,
  180. struct kvmppc_pte *gpte, u64 table,
  181. int table_index, u64 *pte_ret_p);
  182. extern int kvmppc_mmu_radix_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  183. struct kvmppc_pte *gpte, bool data, bool iswrite);
  184. extern void kvmppc_unmap_pte(struct kvm *kvm, pte_t *pte, unsigned long gpa,
  185. unsigned int shift, struct kvm_memory_slot *memslot,
  186. unsigned int lpid);
  187. extern bool kvmppc_hv_handle_set_rc(struct kvm *kvm, pgd_t *pgtable,
  188. bool writing, unsigned long gpa,
  189. unsigned int lpid);
  190. extern int kvmppc_book3s_instantiate_page(struct kvm_vcpu *vcpu,
  191. unsigned long gpa,
  192. struct kvm_memory_slot *memslot,
  193. bool writing, bool kvm_ro,
  194. pte_t *inserted_pte, unsigned int *levelp);
  195. extern int kvmppc_init_vm_radix(struct kvm *kvm);
  196. extern void kvmppc_free_radix(struct kvm *kvm);
  197. extern void kvmppc_free_pgtable_radix(struct kvm *kvm, pgd_t *pgd,
  198. unsigned int lpid);
  199. extern int kvmppc_radix_init(void);
  200. extern void kvmppc_radix_exit(void);
  201. extern int kvm_unmap_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
  202. unsigned long gfn);
  203. extern void kvmppc_unmap_pte(struct kvm *kvm, pte_t *pte,
  204. unsigned long gpa, unsigned int shift,
  205. struct kvm_memory_slot *memslot,
  206. unsigned int lpid);
  207. extern int kvm_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
  208. unsigned long gfn);
  209. extern int kvm_test_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
  210. unsigned long gfn);
  211. extern long kvmppc_hv_get_dirty_log_radix(struct kvm *kvm,
  212. struct kvm_memory_slot *memslot, unsigned long *map);
  213. extern int kvmhv_get_rmmu_info(struct kvm *kvm, struct kvm_ppc_rmmu_info *info);
  214. /* XXX remove this export when load_last_inst() is generic */
  215. extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
  216. extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
  217. extern void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
  218. unsigned int vec);
  219. extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
  220. extern void kvmppc_trigger_fac_interrupt(struct kvm_vcpu *vcpu, ulong fac);
  221. extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
  222. bool upper, u32 val);
  223. extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
  224. extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
  225. extern kvm_pfn_t kvmppc_gpa_to_pfn(struct kvm_vcpu *vcpu, gpa_t gpa,
  226. bool writing, bool *writable);
  227. extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
  228. unsigned long *rmap, long pte_index, int realmode);
  229. extern void kvmppc_update_dirty_map(struct kvm_memory_slot *memslot,
  230. unsigned long gfn, unsigned long psize);
  231. extern void kvmppc_invalidate_hpte(struct kvm *kvm, __be64 *hptep,
  232. unsigned long pte_index);
  233. void kvmppc_clear_ref_hpte(struct kvm *kvm, __be64 *hptep,
  234. unsigned long pte_index);
  235. extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr,
  236. unsigned long *nb_ret);
  237. extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr,
  238. unsigned long gpa, bool dirty);
  239. extern long kvmppc_do_h_enter(struct kvm *kvm, unsigned long flags,
  240. long pte_index, unsigned long pteh, unsigned long ptel,
  241. pgd_t *pgdir, bool realmode, unsigned long *idx_ret);
  242. extern long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags,
  243. unsigned long pte_index, unsigned long avpn,
  244. unsigned long *hpret);
  245. extern long kvmppc_hv_get_dirty_log_hpt(struct kvm *kvm,
  246. struct kvm_memory_slot *memslot, unsigned long *map);
  247. extern void kvmppc_harvest_vpa_dirty(struct kvmppc_vpa *vpa,
  248. struct kvm_memory_slot *memslot,
  249. unsigned long *map);
  250. extern void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr,
  251. unsigned long mask);
  252. extern void kvmppc_set_fscr(struct kvm_vcpu *vcpu, u64 fscr);
  253. extern int kvmhv_p9_tm_emulation_early(struct kvm_vcpu *vcpu);
  254. extern int kvmhv_p9_tm_emulation(struct kvm_vcpu *vcpu);
  255. extern void kvmhv_emulate_tm_rollback(struct kvm_vcpu *vcpu);
  256. extern void kvmppc_entry_trampoline(void);
  257. extern void kvmppc_hv_entry_trampoline(void);
  258. extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
  259. extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
  260. extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
  261. extern void kvmppc_pr_init_default_hcalls(struct kvm *kvm);
  262. extern int kvmppc_hcall_impl_pr(unsigned long cmd);
  263. extern int kvmppc_hcall_impl_hv_realmode(unsigned long cmd);
  264. extern void kvmppc_copy_to_svcpu(struct kvm_vcpu *vcpu);
  265. extern void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu);
  266. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  267. void kvmppc_save_tm_pr(struct kvm_vcpu *vcpu);
  268. void kvmppc_restore_tm_pr(struct kvm_vcpu *vcpu);
  269. void kvmppc_save_tm_sprs(struct kvm_vcpu *vcpu);
  270. void kvmppc_restore_tm_sprs(struct kvm_vcpu *vcpu);
  271. #else
  272. static inline void kvmppc_save_tm_pr(struct kvm_vcpu *vcpu) {}
  273. static inline void kvmppc_restore_tm_pr(struct kvm_vcpu *vcpu) {}
  274. static inline void kvmppc_save_tm_sprs(struct kvm_vcpu *vcpu) {}
  275. static inline void kvmppc_restore_tm_sprs(struct kvm_vcpu *vcpu) {}
  276. #endif
  277. long kvmhv_nested_init(void);
  278. void kvmhv_nested_exit(void);
  279. void kvmhv_vm_nested_init(struct kvm *kvm);
  280. long kvmhv_set_partition_table(struct kvm_vcpu *vcpu);
  281. void kvmhv_set_ptbl_entry(unsigned int lpid, u64 dw0, u64 dw1);
  282. void kvmhv_release_all_nested(struct kvm *kvm);
  283. long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu);
  284. long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu);
  285. int kvmhv_run_single_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu,
  286. u64 time_limit, unsigned long lpcr);
  287. void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr);
  288. void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,
  289. struct hv_guest_state *hr);
  290. long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu);
  291. void kvmppc_giveup_fac(struct kvm_vcpu *vcpu, ulong fac);
  292. extern int kvm_irq_bypass;
  293. static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
  294. {
  295. return vcpu->arch.book3s;
  296. }
  297. /* Also add subarch specific defines */
  298. #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
  299. #include <asm/kvm_book3s_32.h>
  300. #endif
  301. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  302. #include <asm/kvm_book3s_64.h>
  303. #endif
  304. static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
  305. {
  306. vcpu->arch.regs.gpr[num] = val;
  307. }
  308. static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
  309. {
  310. return vcpu->arch.regs.gpr[num];
  311. }
  312. static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
  313. {
  314. vcpu->arch.regs.ccr = val;
  315. }
  316. static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
  317. {
  318. return vcpu->arch.regs.ccr;
  319. }
  320. static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, ulong val)
  321. {
  322. vcpu->arch.regs.xer = val;
  323. }
  324. static inline ulong kvmppc_get_xer(struct kvm_vcpu *vcpu)
  325. {
  326. return vcpu->arch.regs.xer;
  327. }
  328. static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
  329. {
  330. vcpu->arch.regs.ctr = val;
  331. }
  332. static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
  333. {
  334. return vcpu->arch.regs.ctr;
  335. }
  336. static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
  337. {
  338. vcpu->arch.regs.link = val;
  339. }
  340. static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
  341. {
  342. return vcpu->arch.regs.link;
  343. }
  344. static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
  345. {
  346. vcpu->arch.regs.nip = val;
  347. }
  348. static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
  349. {
  350. return vcpu->arch.regs.nip;
  351. }
  352. static inline u64 kvmppc_get_msr(struct kvm_vcpu *vcpu);
  353. static inline bool kvmppc_need_byteswap(struct kvm_vcpu *vcpu)
  354. {
  355. return (kvmppc_get_msr(vcpu) & MSR_LE) != (MSR_KERNEL & MSR_LE);
  356. }
  357. static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
  358. {
  359. return vcpu->arch.fault_dar;
  360. }
  361. static inline bool is_kvmppc_resume_guest(int r)
  362. {
  363. return (r == RESUME_GUEST || r == RESUME_GUEST_NV);
  364. }
  365. static inline bool is_kvmppc_hv_enabled(struct kvm *kvm);
  366. static inline bool kvmppc_supports_magic_page(struct kvm_vcpu *vcpu)
  367. {
  368. /* Only PR KVM supports the magic page */
  369. return !is_kvmppc_hv_enabled(vcpu->kvm);
  370. }
  371. extern int kvmppc_h_logical_ci_load(struct kvm_vcpu *vcpu);
  372. extern int kvmppc_h_logical_ci_store(struct kvm_vcpu *vcpu);
  373. /* Magic register values loaded into r3 and r4 before the 'sc' assembly
  374. * instruction for the OSI hypercalls */
  375. #define OSI_SC_MAGIC_R3 0x113724FA
  376. #define OSI_SC_MAGIC_R4 0x77810F9B
  377. #define INS_DCBZ 0x7c0007ec
  378. /* TO = 31 for unconditional trap */
  379. #define INS_TW 0x7fe00008
  380. #define SPLIT_HACK_MASK 0xff000000
  381. #define SPLIT_HACK_OFFS 0xfb000000
  382. /*
  383. * This packs a VCPU ID from the [0..KVM_MAX_VCPU_ID) space down to the
  384. * [0..KVM_MAX_VCPUS) space, using knowledge of the guest's core stride
  385. * (but not its actual threading mode, which is not available) to avoid
  386. * collisions.
  387. *
  388. * The implementation leaves VCPU IDs from the range [0..KVM_MAX_VCPUS) (block
  389. * 0) unchanged: if the guest is filling each VCORE completely then it will be
  390. * using consecutive IDs and it will fill the space without any packing.
  391. *
  392. * For higher VCPU IDs, the packed ID is based on the VCPU ID modulo
  393. * KVM_MAX_VCPUS (effectively masking off the top bits) and then an offset is
  394. * added to avoid collisions.
  395. *
  396. * VCPU IDs in the range [KVM_MAX_VCPUS..(KVM_MAX_VCPUS*2)) (block 1) are only
  397. * possible if the guest is leaving at least 1/2 of each VCORE empty, so IDs
  398. * can be safely packed into the second half of each VCORE by adding an offset
  399. * of (stride / 2).
  400. *
  401. * Similarly, if VCPU IDs in the range [(KVM_MAX_VCPUS*2)..(KVM_MAX_VCPUS*4))
  402. * (blocks 2 and 3) are seen, the guest must be leaving at least 3/4 of each
  403. * VCORE empty so packed IDs can be offset by (stride / 4) and (stride * 3 / 4).
  404. *
  405. * Finally, VCPU IDs from blocks 5..7 will only be seen if the guest is using a
  406. * stride of 8 and 1 thread per core so the remaining offsets of 1, 5, 3 and 7
  407. * must be free to use.
  408. *
  409. * (The offsets for each block are stored in block_offsets[], indexed by the
  410. * block number if the stride is 8. For cases where the guest's stride is less
  411. * than 8, we can re-use the block_offsets array by multiplying the block
  412. * number by (MAX_SMT_THREADS / stride) to reach the correct entry.)
  413. */
  414. static inline u32 kvmppc_pack_vcpu_id(struct kvm *kvm, u32 id)
  415. {
  416. const int block_offsets[MAX_SMT_THREADS] = {0, 4, 2, 6, 1, 5, 3, 7};
  417. int stride = kvm->arch.emul_smt_mode;
  418. int block = (id / KVM_MAX_VCPUS) * (MAX_SMT_THREADS / stride);
  419. u32 packed_id;
  420. if (WARN_ONCE(block >= MAX_SMT_THREADS, "VCPU ID too large to pack"))
  421. return 0;
  422. packed_id = (id % KVM_MAX_VCPUS) + block_offsets[block];
  423. if (WARN_ONCE(packed_id >= KVM_MAX_VCPUS, "VCPU ID packing failed"))
  424. return 0;
  425. return packed_id;
  426. }
  427. #endif /* __ASM_KVM_BOOK3S_H__ */