book3s_xics.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2012 Michael Ellerman, IBM Corporation.
  4. * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation
  5. */
  6. #ifndef _KVM_PPC_BOOK3S_XICS_H
  7. #define _KVM_PPC_BOOK3S_XICS_H
  8. #ifdef CONFIG_KVM_XICS
  9. /*
  10. * We use a two-level tree to store interrupt source information.
  11. * There are up to 1024 ICS nodes, each of which can represent
  12. * 1024 sources.
  13. */
  14. #define KVMPPC_XICS_MAX_ICS_ID 1023
  15. #define KVMPPC_XICS_ICS_SHIFT 10
  16. #define KVMPPC_XICS_IRQ_PER_ICS (1 << KVMPPC_XICS_ICS_SHIFT)
  17. #define KVMPPC_XICS_SRC_MASK (KVMPPC_XICS_IRQ_PER_ICS - 1)
  18. /*
  19. * Interrupt source numbers below this are reserved, for example
  20. * 0 is "no interrupt", and 2 is used for IPIs.
  21. */
  22. #define KVMPPC_XICS_FIRST_IRQ 16
  23. #define KVMPPC_XICS_NR_IRQS ((KVMPPC_XICS_MAX_ICS_ID + 1) * \
  24. KVMPPC_XICS_IRQ_PER_ICS)
  25. /* Priority value to use for disabling an interrupt */
  26. #define MASKED 0xff
  27. #define PQ_PRESENTED 1
  28. #define PQ_QUEUED 2
  29. /* State for one irq source */
  30. struct ics_irq_state {
  31. u32 number;
  32. u32 server;
  33. u32 pq_state;
  34. u8 priority;
  35. u8 saved_priority;
  36. u8 resend;
  37. u8 masked_pending;
  38. u8 lsi; /* level-sensitive interrupt */
  39. u8 exists;
  40. int intr_cpu;
  41. u32 host_irq;
  42. };
  43. /* Atomic ICP state, updated with a single compare & swap */
  44. union kvmppc_icp_state {
  45. unsigned long raw;
  46. struct {
  47. u8 out_ee:1;
  48. u8 need_resend:1;
  49. u8 cppr;
  50. u8 mfrr;
  51. u8 pending_pri;
  52. u32 xisr;
  53. };
  54. };
  55. /* One bit per ICS */
  56. #define ICP_RESEND_MAP_SIZE (KVMPPC_XICS_MAX_ICS_ID / BITS_PER_LONG + 1)
  57. struct kvmppc_icp {
  58. struct kvm_vcpu *vcpu;
  59. unsigned long server_num;
  60. union kvmppc_icp_state state;
  61. unsigned long resend_map[ICP_RESEND_MAP_SIZE];
  62. /* Real mode might find something too hard, here's the action
  63. * it might request from virtual mode
  64. */
  65. #define XICS_RM_KICK_VCPU 0x1
  66. #define XICS_RM_CHECK_RESEND 0x2
  67. #define XICS_RM_NOTIFY_EOI 0x8
  68. u32 rm_action;
  69. struct kvm_vcpu *rm_kick_target;
  70. struct kvmppc_icp *rm_resend_icp;
  71. u32 rm_reject;
  72. u32 rm_eoied_irq;
  73. /* Counters for each reason we exited real mode */
  74. unsigned long n_rm_kick_vcpu;
  75. unsigned long n_rm_check_resend;
  76. unsigned long n_rm_notify_eoi;
  77. /* Counters for handling ICP processing in real mode */
  78. unsigned long n_check_resend;
  79. unsigned long n_reject;
  80. /* Debug stuff for real mode */
  81. union kvmppc_icp_state rm_dbgstate;
  82. struct kvm_vcpu *rm_dbgtgt;
  83. };
  84. struct kvmppc_ics {
  85. arch_spinlock_t lock;
  86. u16 icsid;
  87. struct ics_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS];
  88. };
  89. struct kvmppc_xics {
  90. struct kvm *kvm;
  91. struct kvm_device *dev;
  92. struct dentry *dentry;
  93. u32 max_icsid;
  94. bool real_mode;
  95. bool real_mode_dbg;
  96. u32 err_noics;
  97. u32 err_noicp;
  98. struct kvmppc_ics *ics[KVMPPC_XICS_MAX_ICS_ID + 1];
  99. };
  100. static inline struct kvmppc_icp *kvmppc_xics_find_server(struct kvm *kvm,
  101. u32 nr)
  102. {
  103. struct kvm_vcpu *vcpu = NULL;
  104. unsigned long i;
  105. kvm_for_each_vcpu(i, vcpu, kvm) {
  106. if (vcpu->arch.icp && nr == vcpu->arch.icp->server_num)
  107. return vcpu->arch.icp;
  108. }
  109. return NULL;
  110. }
  111. static inline struct kvmppc_ics *kvmppc_xics_find_ics(struct kvmppc_xics *xics,
  112. u32 irq, u16 *source)
  113. {
  114. u32 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
  115. u16 src = irq & KVMPPC_XICS_SRC_MASK;
  116. struct kvmppc_ics *ics;
  117. if (source)
  118. *source = src;
  119. if (icsid > KVMPPC_XICS_MAX_ICS_ID)
  120. return NULL;
  121. ics = xics->ics[icsid];
  122. if (!ics)
  123. return NULL;
  124. return ics;
  125. }
  126. extern unsigned long xics_rm_h_xirr(struct kvm_vcpu *vcpu);
  127. extern unsigned long xics_rm_h_xirr_x(struct kvm_vcpu *vcpu);
  128. extern int xics_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
  129. unsigned long mfrr);
  130. extern int xics_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
  131. extern int xics_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
  132. #endif /* CONFIG_KVM_XICS */
  133. #endif /* _KVM_PPC_BOOK3S_XICS_H */