2 * Copyright 2012 Michael Ellerman, IBM Corporation.
3 * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation
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.
10 #ifndef _KVM_PPC_BOOK3S_XICS_H
11 #define _KVM_PPC_BOOK3S_XICS_H
13 #ifdef CONFIG_KVM_XICS
15 * We use a two-level tree to store interrupt source information.
16 * There are up to 1024 ICS nodes, each of which can represent
19 #define KVMPPC_XICS_MAX_ICS_ID 1023
20 #define KVMPPC_XICS_ICS_SHIFT 10
21 #define KVMPPC_XICS_IRQ_PER_ICS (1 << KVMPPC_XICS_ICS_SHIFT)
22 #define KVMPPC_XICS_SRC_MASK (KVMPPC_XICS_IRQ_PER_ICS - 1)
25 * Interrupt source numbers below this are reserved, for example
26 * 0 is "no interrupt", and 2 is used for IPIs.
28 #define KVMPPC_XICS_FIRST_IRQ 16
29 #define KVMPPC_XICS_NR_IRQS ((KVMPPC_XICS_MAX_ICS_ID + 1) * \
30 KVMPPC_XICS_IRQ_PER_ICS)
32 /* Priority value to use for disabling an interrupt */
35 #define PQ_PRESENTED 1
38 /* State for one irq source */
39 struct ics_irq_state {
47 u8 lsi; /* level-sensitive interrupt */
53 /* Atomic ICP state, updated with a single compare & swap */
54 union kvmppc_icp_state {
67 #define ICP_RESEND_MAP_SIZE (KVMPPC_XICS_MAX_ICS_ID / BITS_PER_LONG + 1)
70 struct kvm_vcpu *vcpu;
71 unsigned long server_num;
72 union kvmppc_icp_state state;
73 unsigned long resend_map[ICP_RESEND_MAP_SIZE];
75 /* Real mode might find something too hard, here's the action
76 * it might request from virtual mode
78 #define XICS_RM_KICK_VCPU 0x1
79 #define XICS_RM_CHECK_RESEND 0x2
80 #define XICS_RM_NOTIFY_EOI 0x8
82 struct kvm_vcpu *rm_kick_target;
83 struct kvmppc_icp *rm_resend_icp;
87 /* Counters for each reason we exited real mode */
88 unsigned long n_rm_kick_vcpu;
89 unsigned long n_rm_check_resend;
90 unsigned long n_rm_notify_eoi;
91 /* Counters for handling ICP processing in real mode */
92 unsigned long n_check_resend;
93 unsigned long n_reject;
95 /* Debug stuff for real mode */
96 union kvmppc_icp_state rm_dbgstate;
97 struct kvm_vcpu *rm_dbgtgt;
101 arch_spinlock_t lock;
103 struct ics_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS];
108 struct kvm_device *dev;
109 struct dentry *dentry;
115 struct kvmppc_ics *ics[KVMPPC_XICS_MAX_ICS_ID + 1];
118 static inline struct kvmppc_icp *kvmppc_xics_find_server(struct kvm *kvm,
121 struct kvm_vcpu *vcpu = NULL;
124 kvm_for_each_vcpu(i, vcpu, kvm) {
125 if (vcpu->arch.icp && nr == vcpu->arch.icp->server_num)
126 return vcpu->arch.icp;
131 static inline struct kvmppc_ics *kvmppc_xics_find_ics(struct kvmppc_xics *xics,
132 u32 irq, u16 *source)
134 u32 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
135 u16 src = irq & KVMPPC_XICS_SRC_MASK;
136 struct kvmppc_ics *ics;
140 if (icsid > KVMPPC_XICS_MAX_ICS_ID)
142 ics = xics->ics[icsid];
148 extern unsigned long xics_rm_h_xirr(struct kvm_vcpu *vcpu);
149 extern int xics_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
151 extern int xics_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
152 extern int xics_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
154 #endif /* CONFIG_KVM_XICS */
155 #endif /* _KVM_PPC_BOOK3S_XICS_H */