book3s_xics.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /*
  12. * We use a two-level tree to store interrupt source information.
  13. * There are up to 1024 ICS nodes, each of which can represent
  14. * 1024 sources.
  15. */
  16. #define KVMPPC_XICS_MAX_ICS_ID 1023
  17. #define KVMPPC_XICS_ICS_SHIFT 10
  18. #define KVMPPC_XICS_IRQ_PER_ICS (1 << KVMPPC_XICS_ICS_SHIFT)
  19. #define KVMPPC_XICS_SRC_MASK (KVMPPC_XICS_IRQ_PER_ICS - 1)
  20. /*
  21. * Interrupt source numbers below this are reserved, for example
  22. * 0 is "no interrupt", and 2 is used for IPIs.
  23. */
  24. #define KVMPPC_XICS_FIRST_IRQ 16
  25. #define KVMPPC_XICS_NR_IRQS ((KVMPPC_XICS_MAX_ICS_ID + 1) * \
  26. KVMPPC_XICS_IRQ_PER_ICS)
  27. /* Priority value to use for disabling an interrupt */
  28. #define MASKED 0xff
  29. /* State for one irq source */
  30. struct ics_irq_state {
  31. u32 number;
  32. u32 server;
  33. u8 priority;
  34. u8 saved_priority;
  35. u8 resend;
  36. u8 masked_pending;
  37. u8 asserted; /* Only for LSI */
  38. u8 exists;
  39. };
  40. /* Atomic ICP state, updated with a single compare & swap */
  41. union kvmppc_icp_state {
  42. unsigned long raw;
  43. struct {
  44. u8 out_ee:1;
  45. u8 need_resend:1;
  46. u8 cppr;
  47. u8 mfrr;
  48. u8 pending_pri;
  49. u32 xisr;
  50. };
  51. };
  52. /* One bit per ICS */
  53. #define ICP_RESEND_MAP_SIZE (KVMPPC_XICS_MAX_ICS_ID / BITS_PER_LONG + 1)
  54. struct kvmppc_icp {
  55. struct kvm_vcpu *vcpu;
  56. unsigned long server_num;
  57. union kvmppc_icp_state state;
  58. unsigned long resend_map[ICP_RESEND_MAP_SIZE];
  59. /* Real mode might find something too hard, here's the action
  60. * it might request from virtual mode
  61. */
  62. #define XICS_RM_KICK_VCPU 0x1
  63. #define XICS_RM_CHECK_RESEND 0x2
  64. #define XICS_RM_REJECT 0x4
  65. u32 rm_action;
  66. struct kvm_vcpu *rm_kick_target;
  67. u32 rm_reject;
  68. /* Debug stuff for real mode */
  69. union kvmppc_icp_state rm_dbgstate;
  70. struct kvm_vcpu *rm_dbgtgt;
  71. };
  72. struct kvmppc_ics {
  73. struct mutex lock;
  74. u16 icsid;
  75. struct ics_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS];
  76. };
  77. struct kvmppc_xics {
  78. struct kvm *kvm;
  79. struct kvm_device *dev;
  80. struct dentry *dentry;
  81. u32 max_icsid;
  82. bool real_mode;
  83. bool real_mode_dbg;
  84. struct kvmppc_ics *ics[KVMPPC_XICS_MAX_ICS_ID + 1];
  85. };
  86. static inline struct kvmppc_icp *kvmppc_xics_find_server(struct kvm *kvm,
  87. u32 nr)
  88. {
  89. struct kvm_vcpu *vcpu = NULL;
  90. int i;
  91. kvm_for_each_vcpu(i, vcpu, kvm) {
  92. if (vcpu->arch.icp && nr == vcpu->arch.icp->server_num)
  93. return vcpu->arch.icp;
  94. }
  95. return NULL;
  96. }
  97. static inline struct kvmppc_ics *kvmppc_xics_find_ics(struct kvmppc_xics *xics,
  98. u32 irq, u16 *source)
  99. {
  100. u32 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
  101. u16 src = irq & KVMPPC_XICS_SRC_MASK;
  102. struct kvmppc_ics *ics;
  103. if (source)
  104. *source = src;
  105. if (icsid > KVMPPC_XICS_MAX_ICS_ID)
  106. return NULL;
  107. ics = xics->ics[icsid];
  108. if (!ics)
  109. return NULL;
  110. return ics;
  111. }
  112. #endif /* _KVM_PPC_BOOK3S_XICS_H */