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