book3s_xics.h 3.8 KB

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