book3s_xive.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2017 Benjamin Herrenschmidt, IBM Corporation
  4. */
  5. #ifndef _KVM_PPC_BOOK3S_XIVE_H
  6. #define _KVM_PPC_BOOK3S_XIVE_H
  7. #ifdef CONFIG_KVM_XICS
  8. #include "book3s_xics.h"
  9. /*
  10. * The XIVE Interrupt source numbers are within the range 0 to
  11. * KVMPPC_XICS_NR_IRQS.
  12. */
  13. #define KVMPPC_XIVE_FIRST_IRQ 0
  14. #define KVMPPC_XIVE_NR_IRQS KVMPPC_XICS_NR_IRQS
  15. /*
  16. * State for one guest irq source.
  17. *
  18. * For each guest source we allocate a HW interrupt in the XIVE
  19. * which we use for all SW triggers. It will be unused for
  20. * pass-through but it's easier to keep around as the same
  21. * guest interrupt can alternatively be emulated or pass-through
  22. * if a physical device is hot unplugged and replaced with an
  23. * emulated one.
  24. *
  25. * This state structure is very similar to the XICS one with
  26. * additional XIVE specific tracking.
  27. */
  28. struct kvmppc_xive_irq_state {
  29. bool valid; /* Interrupt entry is valid */
  30. u32 number; /* Guest IRQ number */
  31. u32 ipi_number; /* XIVE IPI HW number */
  32. struct xive_irq_data ipi_data; /* XIVE IPI associated data */
  33. u32 pt_number; /* XIVE Pass-through number if any */
  34. struct xive_irq_data *pt_data; /* XIVE Pass-through associated data */
  35. /* Targetting as set by guest */
  36. u8 guest_priority; /* Guest set priority */
  37. u8 saved_priority; /* Saved priority when masking */
  38. /* Actual targetting */
  39. u32 act_server; /* Actual server */
  40. u8 act_priority; /* Actual priority */
  41. /* Various state bits */
  42. bool in_eoi; /* Synchronize with H_EOI */
  43. bool old_p; /* P bit state when masking */
  44. bool old_q; /* Q bit state when masking */
  45. bool lsi; /* level-sensitive interrupt */
  46. bool asserted; /* Only for emulated LSI: current state */
  47. /* Saved for migration state */
  48. bool in_queue;
  49. bool saved_p;
  50. bool saved_q;
  51. u8 saved_scan_prio;
  52. /* Xive native */
  53. u32 eisn; /* Guest Effective IRQ number */
  54. };
  55. /* Select the "right" interrupt (IPI vs. passthrough) */
  56. static inline void kvmppc_xive_select_irq(struct kvmppc_xive_irq_state *state,
  57. u32 *out_hw_irq,
  58. struct xive_irq_data **out_xd)
  59. {
  60. if (state->pt_number) {
  61. if (out_hw_irq)
  62. *out_hw_irq = state->pt_number;
  63. if (out_xd)
  64. *out_xd = state->pt_data;
  65. } else {
  66. if (out_hw_irq)
  67. *out_hw_irq = state->ipi_number;
  68. if (out_xd)
  69. *out_xd = &state->ipi_data;
  70. }
  71. }
  72. /*
  73. * This corresponds to an "ICS" in XICS terminology, we use it
  74. * as a mean to break up source information into multiple structures.
  75. */
  76. struct kvmppc_xive_src_block {
  77. arch_spinlock_t lock;
  78. u16 id;
  79. struct kvmppc_xive_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS];
  80. };
  81. struct kvmppc_xive;
  82. struct kvmppc_xive_ops {
  83. int (*reset_mapped)(struct kvm *kvm, unsigned long guest_irq);
  84. };
  85. struct kvmppc_xive {
  86. struct kvm *kvm;
  87. struct kvm_device *dev;
  88. struct dentry *dentry;
  89. /* VP block associated with the VM */
  90. u32 vp_base;
  91. /* Blocks of sources */
  92. struct kvmppc_xive_src_block *src_blocks[KVMPPC_XICS_MAX_ICS_ID + 1];
  93. u32 max_sbid;
  94. /*
  95. * For state save, we lazily scan the queues on the first interrupt
  96. * being migrated. We don't have a clean way to reset that flags
  97. * so we keep track of the number of valid sources and how many of
  98. * them were migrated so we can reset when all of them have been
  99. * processed.
  100. */
  101. u32 src_count;
  102. u32 saved_src_count;
  103. /*
  104. * Some irqs are delayed on restore until the source is created,
  105. * keep track here of how many of them
  106. */
  107. u32 delayed_irqs;
  108. /* Which queues (priorities) are in use by the guest */
  109. u8 qmap;
  110. /* Queue orders */
  111. u32 q_order;
  112. u32 q_page_order;
  113. /* Flags */
  114. u8 single_escalation;
  115. /* Number of entries in the VP block */
  116. u32 nr_servers;
  117. struct kvmppc_xive_ops *ops;
  118. struct address_space *mapping;
  119. struct mutex mapping_lock;
  120. struct mutex lock;
  121. };
  122. #define KVMPPC_XIVE_Q_COUNT 8
  123. struct kvmppc_xive_vcpu {
  124. struct kvmppc_xive *xive;
  125. struct kvm_vcpu *vcpu;
  126. bool valid;
  127. /* Server number. This is the HW CPU ID from a guest perspective */
  128. u32 server_num;
  129. /*
  130. * HW VP corresponding to this VCPU. This is the base of the VP
  131. * block plus the server number.
  132. */
  133. u32 vp_id;
  134. u32 vp_chip_id;
  135. u32 vp_cam;
  136. /* IPI used for sending ... IPIs */
  137. u32 vp_ipi;
  138. struct xive_irq_data vp_ipi_data;
  139. /* Local emulation state */
  140. uint8_t cppr; /* guest CPPR */
  141. uint8_t hw_cppr;/* Hardware CPPR */
  142. uint8_t mfrr;
  143. uint8_t pending;
  144. /* Each VP has 8 queues though we only provision some */
  145. struct xive_q queues[KVMPPC_XIVE_Q_COUNT];
  146. u32 esc_virq[KVMPPC_XIVE_Q_COUNT];
  147. char *esc_virq_names[KVMPPC_XIVE_Q_COUNT];
  148. /* Stash a delayed irq on restore from migration (see set_icp) */
  149. u32 delayed_irq;
  150. /* Stats */
  151. u64 stat_rm_h_xirr;
  152. u64 stat_rm_h_ipoll;
  153. u64 stat_rm_h_cppr;
  154. u64 stat_rm_h_eoi;
  155. u64 stat_rm_h_ipi;
  156. u64 stat_vm_h_xirr;
  157. u64 stat_vm_h_ipoll;
  158. u64 stat_vm_h_cppr;
  159. u64 stat_vm_h_eoi;
  160. u64 stat_vm_h_ipi;
  161. };
  162. static inline struct kvm_vcpu *kvmppc_xive_find_server(struct kvm *kvm, u32 nr)
  163. {
  164. struct kvm_vcpu *vcpu = NULL;
  165. int i;
  166. kvm_for_each_vcpu(i, vcpu, kvm) {
  167. if (vcpu->arch.xive_vcpu && nr == vcpu->arch.xive_vcpu->server_num)
  168. return vcpu;
  169. }
  170. return NULL;
  171. }
  172. static inline struct kvmppc_xive_src_block *kvmppc_xive_find_source(struct kvmppc_xive *xive,
  173. u32 irq, u16 *source)
  174. {
  175. u32 bid = irq >> KVMPPC_XICS_ICS_SHIFT;
  176. u16 src = irq & KVMPPC_XICS_SRC_MASK;
  177. if (source)
  178. *source = src;
  179. if (bid > KVMPPC_XICS_MAX_ICS_ID)
  180. return NULL;
  181. return xive->src_blocks[bid];
  182. }
  183. static inline u32 kvmppc_xive_vp(struct kvmppc_xive *xive, u32 server)
  184. {
  185. return xive->vp_base + kvmppc_pack_vcpu_id(xive->kvm, server);
  186. }
  187. static inline bool kvmppc_xive_vp_in_use(struct kvm *kvm, u32 vp_id)
  188. {
  189. struct kvm_vcpu *vcpu = NULL;
  190. int i;
  191. kvm_for_each_vcpu(i, vcpu, kvm) {
  192. if (vcpu->arch.xive_vcpu && vp_id == vcpu->arch.xive_vcpu->vp_id)
  193. return true;
  194. }
  195. return false;
  196. }
  197. /*
  198. * Mapping between guest priorities and host priorities
  199. * is as follow.
  200. *
  201. * Guest request for 0...6 are honored. Guest request for anything
  202. * higher results in a priority of 6 being applied.
  203. *
  204. * Similar mapping is done for CPPR values
  205. */
  206. static inline u8 xive_prio_from_guest(u8 prio)
  207. {
  208. if (prio == 0xff || prio < 6)
  209. return prio;
  210. return 6;
  211. }
  212. static inline u8 xive_prio_to_guest(u8 prio)
  213. {
  214. return prio;
  215. }
  216. static inline u32 __xive_read_eq(__be32 *qpage, u32 msk, u32 *idx, u32 *toggle)
  217. {
  218. u32 cur;
  219. if (!qpage)
  220. return 0;
  221. cur = be32_to_cpup(qpage + *idx);
  222. if ((cur >> 31) == *toggle)
  223. return 0;
  224. *idx = (*idx + 1) & msk;
  225. if (*idx == 0)
  226. (*toggle) ^= 1;
  227. return cur & 0x7fffffff;
  228. }
  229. extern unsigned long xive_rm_h_xirr(struct kvm_vcpu *vcpu);
  230. extern unsigned long xive_rm_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server);
  231. extern int xive_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
  232. unsigned long mfrr);
  233. extern int xive_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
  234. extern int xive_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
  235. extern unsigned long (*__xive_vm_h_xirr)(struct kvm_vcpu *vcpu);
  236. extern unsigned long (*__xive_vm_h_ipoll)(struct kvm_vcpu *vcpu, unsigned long server);
  237. extern int (*__xive_vm_h_ipi)(struct kvm_vcpu *vcpu, unsigned long server,
  238. unsigned long mfrr);
  239. extern int (*__xive_vm_h_cppr)(struct kvm_vcpu *vcpu, unsigned long cppr);
  240. extern int (*__xive_vm_h_eoi)(struct kvm_vcpu *vcpu, unsigned long xirr);
  241. /*
  242. * Common Xive routines for XICS-over-XIVE and XIVE native
  243. */
  244. void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu);
  245. int kvmppc_xive_debug_show_queues(struct seq_file *m, struct kvm_vcpu *vcpu);
  246. struct kvmppc_xive_src_block *kvmppc_xive_create_src_block(
  247. struct kvmppc_xive *xive, int irq);
  248. void kvmppc_xive_free_sources(struct kvmppc_xive_src_block *sb);
  249. int kvmppc_xive_select_target(struct kvm *kvm, u32 *server, u8 prio);
  250. int kvmppc_xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio,
  251. bool single_escalation);
  252. struct kvmppc_xive *kvmppc_xive_get_device(struct kvm *kvm, u32 type);
  253. void xive_cleanup_single_escalation(struct kvm_vcpu *vcpu,
  254. struct kvmppc_xive_vcpu *xc, int irq);
  255. int kvmppc_xive_compute_vp_id(struct kvmppc_xive *xive, u32 cpu, u32 *vp);
  256. int kvmppc_xive_set_nr_servers(struct kvmppc_xive *xive, u64 addr);
  257. #endif /* CONFIG_KVM_XICS */
  258. #endif /* _KVM_PPC_BOOK3S_XICS_H */