book3s_xive.h 6.5 KB

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