Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2012 Michael Ellerman, IBM Corporation. |
| 4 | * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef _KVM_PPC_BOOK3S_XICS_H |
| 8 | #define _KVM_PPC_BOOK3S_XICS_H |
| 9 | |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 10 | #ifdef CONFIG_KVM_XICS |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 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 | /* |
| 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 | |
| 29 | /* Priority value to use for disabling an interrupt */ |
| 30 | #define MASKED 0xff |
| 31 | |
Li Zhong | 17d4861 | 2016-11-11 12:57:35 +0800 | [diff] [blame] | 32 | #define PQ_PRESENTED 1 |
| 33 | #define PQ_QUEUED 2 |
| 34 | |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 35 | /* State for one irq source */ |
| 36 | struct ics_irq_state { |
| 37 | u32 number; |
| 38 | u32 server; |
Li Zhong | 17d4861 | 2016-11-11 12:57:35 +0800 | [diff] [blame] | 39 | u32 pq_state; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 40 | u8 priority; |
Paul Mackerras | d19bd862 | 2013-04-17 20:32:04 +0000 | [diff] [blame] | 41 | u8 saved_priority; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 42 | u8 resend; |
| 43 | u8 masked_pending; |
Paul Mackerras | b1a4286 | 2016-05-04 21:07:52 +1000 | [diff] [blame] | 44 | u8 lsi; /* level-sensitive interrupt */ |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 45 | u8 exists; |
Paul Mackerras | 5d37519 | 2016-08-19 15:35:56 +1000 | [diff] [blame] | 46 | int intr_cpu; |
| 47 | u32 host_irq; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | /* Atomic ICP state, updated with a single compare & swap */ |
| 51 | union kvmppc_icp_state { |
| 52 | unsigned long raw; |
| 53 | struct { |
| 54 | u8 out_ee:1; |
| 55 | u8 need_resend:1; |
| 56 | u8 cppr; |
| 57 | u8 mfrr; |
| 58 | u8 pending_pri; |
| 59 | u32 xisr; |
| 60 | }; |
| 61 | }; |
| 62 | |
| 63 | /* One bit per ICS */ |
| 64 | #define ICP_RESEND_MAP_SIZE (KVMPPC_XICS_MAX_ICS_ID / BITS_PER_LONG + 1) |
| 65 | |
| 66 | struct kvmppc_icp { |
| 67 | struct kvm_vcpu *vcpu; |
| 68 | unsigned long server_num; |
| 69 | union kvmppc_icp_state state; |
| 70 | unsigned long resend_map[ICP_RESEND_MAP_SIZE]; |
Benjamin Herrenschmidt | e7d26f2 | 2013-04-17 20:31:15 +0000 | [diff] [blame] | 71 | |
| 72 | /* Real mode might find something too hard, here's the action |
| 73 | * it might request from virtual mode |
| 74 | */ |
| 75 | #define XICS_RM_KICK_VCPU 0x1 |
| 76 | #define XICS_RM_CHECK_RESEND 0x2 |
Paul Mackerras | 25a2150b | 2014-06-30 20:51:14 +1000 | [diff] [blame] | 77 | #define XICS_RM_NOTIFY_EOI 0x8 |
Benjamin Herrenschmidt | e7d26f2 | 2013-04-17 20:31:15 +0000 | [diff] [blame] | 78 | u32 rm_action; |
| 79 | struct kvm_vcpu *rm_kick_target; |
Suresh E. Warrier | 5b88cda | 2014-11-03 15:51:59 +1100 | [diff] [blame] | 80 | struct kvmppc_icp *rm_resend_icp; |
Benjamin Herrenschmidt | e7d26f2 | 2013-04-17 20:31:15 +0000 | [diff] [blame] | 81 | u32 rm_reject; |
Paul Mackerras | 25a2150b | 2014-06-30 20:51:14 +1000 | [diff] [blame] | 82 | u32 rm_eoied_irq; |
Benjamin Herrenschmidt | e7d26f2 | 2013-04-17 20:31:15 +0000 | [diff] [blame] | 83 | |
Suresh E. Warrier | 878610f | 2015-03-20 20:39:45 +1100 | [diff] [blame] | 84 | /* Counters for each reason we exited real mode */ |
| 85 | unsigned long n_rm_kick_vcpu; |
| 86 | unsigned long n_rm_check_resend; |
Suresh E. Warrier | 878610f | 2015-03-20 20:39:45 +1100 | [diff] [blame] | 87 | unsigned long n_rm_notify_eoi; |
Suresh Warrier | 6e0365b | 2015-03-20 20:39:48 +1100 | [diff] [blame] | 88 | /* Counters for handling ICP processing in real mode */ |
| 89 | unsigned long n_check_resend; |
| 90 | unsigned long n_reject; |
Suresh E. Warrier | 878610f | 2015-03-20 20:39:45 +1100 | [diff] [blame] | 91 | |
Benjamin Herrenschmidt | e7d26f2 | 2013-04-17 20:31:15 +0000 | [diff] [blame] | 92 | /* Debug stuff for real mode */ |
| 93 | union kvmppc_icp_state rm_dbgstate; |
| 94 | struct kvm_vcpu *rm_dbgtgt; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | struct kvmppc_ics { |
Suresh Warrier | 34cb795 | 2015-03-20 20:39:46 +1100 | [diff] [blame] | 98 | arch_spinlock_t lock; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 99 | u16 icsid; |
| 100 | struct ics_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS]; |
| 101 | }; |
| 102 | |
| 103 | struct kvmppc_xics { |
| 104 | struct kvm *kvm; |
Paul Mackerras | 5975a2e | 2013-04-27 00:28:37 +0000 | [diff] [blame] | 105 | struct kvm_device *dev; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 106 | struct dentry *dentry; |
| 107 | u32 max_icsid; |
Benjamin Herrenschmidt | e7d26f2 | 2013-04-17 20:31:15 +0000 | [diff] [blame] | 108 | bool real_mode; |
| 109 | bool real_mode_dbg; |
Suresh Warrier | 6e0365b | 2015-03-20 20:39:48 +1100 | [diff] [blame] | 110 | u32 err_noics; |
| 111 | u32 err_noicp; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 112 | struct kvmppc_ics *ics[KVMPPC_XICS_MAX_ICS_ID + 1]; |
| 113 | }; |
| 114 | |
| 115 | static inline struct kvmppc_icp *kvmppc_xics_find_server(struct kvm *kvm, |
| 116 | u32 nr) |
| 117 | { |
| 118 | struct kvm_vcpu *vcpu = NULL; |
| 119 | int i; |
| 120 | |
| 121 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 122 | if (vcpu->arch.icp && nr == vcpu->arch.icp->server_num) |
| 123 | return vcpu->arch.icp; |
| 124 | } |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | static inline struct kvmppc_ics *kvmppc_xics_find_ics(struct kvmppc_xics *xics, |
| 129 | u32 irq, u16 *source) |
| 130 | { |
| 131 | u32 icsid = irq >> KVMPPC_XICS_ICS_SHIFT; |
| 132 | u16 src = irq & KVMPPC_XICS_SRC_MASK; |
| 133 | struct kvmppc_ics *ics; |
| 134 | |
| 135 | if (source) |
| 136 | *source = src; |
| 137 | if (icsid > KVMPPC_XICS_MAX_ICS_ID) |
| 138 | return NULL; |
| 139 | ics = xics->ics[icsid]; |
| 140 | if (!ics) |
| 141 | return NULL; |
| 142 | return ics; |
| 143 | } |
| 144 | |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 145 | extern unsigned long xics_rm_h_xirr(struct kvm_vcpu *vcpu); |
| 146 | extern int xics_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server, |
| 147 | unsigned long mfrr); |
| 148 | extern int xics_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr); |
| 149 | extern int xics_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr); |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 150 | |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 151 | #endif /* CONFIG_KVM_XICS */ |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 152 | #endif /* _KVM_PPC_BOOK3S_XICS_H */ |