Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Xen event channels |
| 3 | * |
| 4 | * Xen models interrupts with abstract event channels. Because each |
| 5 | * domain gets 1024 event channels, but NR_IRQ is not that large, we |
| 6 | * must dynamically map irqs<->event channels. The event channels |
| 7 | * interface with the rest of the kernel by defining a xen interrupt |
| 8 | * chip. When an event is recieved, it is mapped to an irq and sent |
| 9 | * through the normal interrupt processing path. |
| 10 | * |
| 11 | * There are four kinds of events which can be mapped to an event |
| 12 | * channel: |
| 13 | * |
| 14 | * 1. Inter-domain notifications. This includes all the virtual |
| 15 | * device events, since they're driven by front-ends in another domain |
| 16 | * (typically dom0). |
| 17 | * 2. VIRQs, typically used for timers. These are per-cpu events. |
| 18 | * 3. IPIs. |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 19 | * 4. PIRQs - Hardware interrupts. |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 20 | * |
| 21 | * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007 |
| 22 | */ |
| 23 | |
| 24 | #include <linux/linkage.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/irq.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/string.h> |
Christophe Saout | 28e0886 | 2009-01-11 11:46:23 -0800 | [diff] [blame] | 29 | #include <linux/bootmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Jeremy Fitzhardinge | b21ddbf | 2010-06-07 16:28:49 -0400 | [diff] [blame] | 31 | #include <linux/irqnr.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 32 | |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 33 | #include <asm/desc.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 34 | #include <asm/ptrace.h> |
| 35 | #include <asm/irq.h> |
Jeremy Fitzhardinge | 792dc4f | 2009-02-06 14:09:43 -0800 | [diff] [blame] | 36 | #include <asm/idle.h> |
Konrad Rzeszutek Wilk | 0794bfc | 2010-10-18 10:41:08 -0400 | [diff] [blame] | 37 | #include <asm/io_apic.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 38 | #include <asm/sync_bitops.h> |
| 39 | #include <asm/xen/hypercall.h> |
Adrian Bunk | 8d1b875 | 2007-07-20 00:31:44 -0700 | [diff] [blame] | 40 | #include <asm/xen/hypervisor.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 41 | |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 42 | #include <xen/xen.h> |
| 43 | #include <xen/hvm.h> |
Isaku Yamahata | e04d0d0 | 2008-04-02 10:53:55 -0700 | [diff] [blame] | 44 | #include <xen/xen-ops.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 45 | #include <xen/events.h> |
| 46 | #include <xen/interface/xen.h> |
| 47 | #include <xen/interface/event_channel.h> |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 48 | #include <xen/interface/hvm/hvm_op.h> |
| 49 | #include <xen/interface/hvm/params.h> |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 50 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 51 | /* |
| 52 | * This lock protects updates to the following mapping and reference-count |
| 53 | * arrays. The lock does not need to be acquired to read the mapping tables. |
| 54 | */ |
| 55 | static DEFINE_SPINLOCK(irq_mapping_update_lock); |
| 56 | |
| 57 | /* IRQ <-> VIRQ mapping. */ |
Tejun Heo | 204fba4 | 2009-06-24 15:13:45 +0900 | [diff] [blame] | 58 | static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1}; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 59 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 60 | /* IRQ <-> IPI mapping */ |
Tejun Heo | 204fba4 | 2009-06-24 15:13:45 +0900 | [diff] [blame] | 61 | static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1}; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 62 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 63 | /* Interrupt types. */ |
| 64 | enum xen_irq_type { |
Jeremy Fitzhardinge | d77bbd4 | 2009-02-06 14:09:45 -0800 | [diff] [blame] | 65 | IRQT_UNBOUND = 0, |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 66 | IRQT_PIRQ, |
| 67 | IRQT_VIRQ, |
| 68 | IRQT_IPI, |
| 69 | IRQT_EVTCHN |
| 70 | }; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 71 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 72 | /* |
| 73 | * Packed IRQ information: |
| 74 | * type - enum xen_irq_type |
| 75 | * event channel - irq->event channel mapping |
| 76 | * cpu - cpu this event channel is bound to |
| 77 | * index - type-specific information: |
| 78 | * PIRQ - vector, with MSB being "needs EIO" |
| 79 | * VIRQ - virq number |
| 80 | * IPI - IPI vector |
| 81 | * EVTCHN - |
| 82 | */ |
| 83 | struct irq_info |
| 84 | { |
| 85 | enum xen_irq_type type; /* type */ |
| 86 | unsigned short evtchn; /* event channel */ |
| 87 | unsigned short cpu; /* cpu bound */ |
| 88 | |
| 89 | union { |
| 90 | unsigned short virq; |
| 91 | enum ipi_vector ipi; |
| 92 | struct { |
| 93 | unsigned short gsi; |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 94 | unsigned char vector; |
| 95 | unsigned char flags; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 96 | } pirq; |
| 97 | } u; |
| 98 | }; |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 99 | #define PIRQ_NEEDS_EOI (1 << 0) |
Konrad Rzeszutek Wilk | 15ebbb8 | 2010-10-04 13:43:27 -0400 | [diff] [blame] | 100 | #define PIRQ_SHAREABLE (1 << 1) |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 101 | |
Jeremy Fitzhardinge | b21ddbf | 2010-06-07 16:28:49 -0400 | [diff] [blame] | 102 | static struct irq_info *irq_info; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 103 | |
Jeremy Fitzhardinge | b21ddbf | 2010-06-07 16:28:49 -0400 | [diff] [blame] | 104 | static int *evtchn_to_irq; |
Mike Travis | c7a3589 | 2009-01-10 21:58:11 -0800 | [diff] [blame] | 105 | struct cpu_evtchn_s { |
| 106 | unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG]; |
| 107 | }; |
Jeremy Fitzhardinge | 3b32f57 | 2009-08-13 12:50:37 -0700 | [diff] [blame] | 108 | |
| 109 | static __initdata struct cpu_evtchn_s init_evtchn_mask = { |
| 110 | .bits[0 ... (NR_EVENT_CHANNELS/BITS_PER_LONG)-1] = ~0ul, |
| 111 | }; |
| 112 | static struct cpu_evtchn_s *cpu_evtchn_mask_p = &init_evtchn_mask; |
| 113 | |
Mike Travis | c7a3589 | 2009-01-10 21:58:11 -0800 | [diff] [blame] | 114 | static inline unsigned long *cpu_evtchn_mask(int cpu) |
| 115 | { |
| 116 | return cpu_evtchn_mask_p[cpu].bits; |
| 117 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 118 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 119 | /* Xen will never allocate port zero for any purpose. */ |
| 120 | #define VALID_EVTCHN(chn) ((chn) != 0) |
| 121 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 122 | static struct irq_chip xen_dynamic_chip; |
Jeremy Fitzhardinge | aaca496 | 2010-08-20 18:57:53 -0700 | [diff] [blame] | 123 | static struct irq_chip xen_percpu_chip; |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 124 | static struct irq_chip xen_pirq_chip; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 125 | |
| 126 | /* Constructor for packed IRQ information. */ |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 127 | static struct irq_info mk_unbound_info(void) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 128 | { |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 129 | return (struct irq_info) { .type = IRQT_UNBOUND }; |
| 130 | } |
| 131 | |
| 132 | static struct irq_info mk_evtchn_info(unsigned short evtchn) |
| 133 | { |
Ian Campbell | 90af951 | 2009-02-06 16:55:58 -0800 | [diff] [blame] | 134 | return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn, |
| 135 | .cpu = 0 }; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi) |
| 139 | { |
| 140 | return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn, |
Ian Campbell | 90af951 | 2009-02-06 16:55:58 -0800 | [diff] [blame] | 141 | .cpu = 0, .u.ipi = ipi }; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq) |
| 145 | { |
| 146 | return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn, |
Ian Campbell | 90af951 | 2009-02-06 16:55:58 -0800 | [diff] [blame] | 147 | .cpu = 0, .u.virq = virq }; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static struct irq_info mk_pirq_info(unsigned short evtchn, |
| 151 | unsigned short gsi, unsigned short vector) |
| 152 | { |
| 153 | return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn, |
Ian Campbell | 90af951 | 2009-02-06 16:55:58 -0800 | [diff] [blame] | 154 | .cpu = 0, .u.pirq = { .gsi = gsi, .vector = vector } }; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Accessors for packed IRQ information. |
| 159 | */ |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 160 | static struct irq_info *info_for_irq(unsigned irq) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 161 | { |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 162 | return &irq_info[irq]; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 165 | static unsigned int evtchn_from_irq(unsigned irq) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 166 | { |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 167 | return info_for_irq(irq)->evtchn; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Ian Campbell | d4c0453 | 2009-02-06 19:20:31 -0800 | [diff] [blame] | 170 | unsigned irq_from_evtchn(unsigned int evtchn) |
| 171 | { |
| 172 | return evtchn_to_irq[evtchn]; |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(irq_from_evtchn); |
| 175 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 176 | static enum ipi_vector ipi_from_irq(unsigned irq) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 177 | { |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 178 | struct irq_info *info = info_for_irq(irq); |
| 179 | |
| 180 | BUG_ON(info == NULL); |
| 181 | BUG_ON(info->type != IRQT_IPI); |
| 182 | |
| 183 | return info->u.ipi; |
| 184 | } |
| 185 | |
| 186 | static unsigned virq_from_irq(unsigned irq) |
| 187 | { |
| 188 | struct irq_info *info = info_for_irq(irq); |
| 189 | |
| 190 | BUG_ON(info == NULL); |
| 191 | BUG_ON(info->type != IRQT_VIRQ); |
| 192 | |
| 193 | return info->u.virq; |
| 194 | } |
| 195 | |
| 196 | static unsigned gsi_from_irq(unsigned irq) |
| 197 | { |
| 198 | struct irq_info *info = info_for_irq(irq); |
| 199 | |
| 200 | BUG_ON(info == NULL); |
| 201 | BUG_ON(info->type != IRQT_PIRQ); |
| 202 | |
| 203 | return info->u.pirq.gsi; |
| 204 | } |
| 205 | |
| 206 | static unsigned vector_from_irq(unsigned irq) |
| 207 | { |
| 208 | struct irq_info *info = info_for_irq(irq); |
| 209 | |
| 210 | BUG_ON(info == NULL); |
| 211 | BUG_ON(info->type != IRQT_PIRQ); |
| 212 | |
| 213 | return info->u.pirq.vector; |
| 214 | } |
| 215 | |
| 216 | static enum xen_irq_type type_from_irq(unsigned irq) |
| 217 | { |
| 218 | return info_for_irq(irq)->type; |
| 219 | } |
| 220 | |
| 221 | static unsigned cpu_from_irq(unsigned irq) |
| 222 | { |
| 223 | return info_for_irq(irq)->cpu; |
| 224 | } |
| 225 | |
| 226 | static unsigned int cpu_from_evtchn(unsigned int evtchn) |
| 227 | { |
| 228 | int irq = evtchn_to_irq[evtchn]; |
| 229 | unsigned ret = 0; |
| 230 | |
| 231 | if (irq != -1) |
| 232 | ret = cpu_from_irq(irq); |
| 233 | |
| 234 | return ret; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 237 | static bool pirq_needs_eoi(unsigned irq) |
| 238 | { |
| 239 | struct irq_info *info = info_for_irq(irq); |
| 240 | |
| 241 | BUG_ON(info->type != IRQT_PIRQ); |
| 242 | |
| 243 | return info->u.pirq.flags & PIRQ_NEEDS_EOI; |
| 244 | } |
| 245 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 246 | static inline unsigned long active_evtchns(unsigned int cpu, |
| 247 | struct shared_info *sh, |
| 248 | unsigned int idx) |
| 249 | { |
| 250 | return (sh->evtchn_pending[idx] & |
Mike Travis | c7a3589 | 2009-01-10 21:58:11 -0800 | [diff] [blame] | 251 | cpu_evtchn_mask(cpu)[idx] & |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 252 | ~sh->evtchn_mask[idx]); |
| 253 | } |
| 254 | |
| 255 | static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu) |
| 256 | { |
| 257 | int irq = evtchn_to_irq[chn]; |
| 258 | |
| 259 | BUG_ON(irq == -1); |
| 260 | #ifdef CONFIG_SMP |
Mike Travis | 7f7ace0 | 2009-01-10 21:58:08 -0800 | [diff] [blame] | 261 | cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu)); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 262 | #endif |
| 263 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 264 | __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq))); |
Mike Travis | c7a3589 | 2009-01-10 21:58:11 -0800 | [diff] [blame] | 265 | __set_bit(chn, cpu_evtchn_mask(cpu)); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 266 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 267 | irq_info[irq].cpu = cpu; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static void init_evtchn_cpu_bindings(void) |
| 271 | { |
| 272 | #ifdef CONFIG_SMP |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 273 | struct irq_desc *desc; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 274 | int i; |
Thomas Gleixner | 10e5808 | 2008-10-16 14:19:04 +0200 | [diff] [blame] | 275 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 276 | /* By default all event channels notify CPU#0. */ |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 277 | for_each_irq_desc(i, desc) { |
Mike Travis | 7f7ace0 | 2009-01-10 21:58:08 -0800 | [diff] [blame] | 278 | cpumask_copy(desc->affinity, cpumask_of(0)); |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 279 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 280 | #endif |
| 281 | |
Mike Travis | c7a3589 | 2009-01-10 21:58:11 -0800 | [diff] [blame] | 282 | memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0))); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 285 | static inline void clear_evtchn(int port) |
| 286 | { |
| 287 | struct shared_info *s = HYPERVISOR_shared_info; |
| 288 | sync_clear_bit(port, &s->evtchn_pending[0]); |
| 289 | } |
| 290 | |
| 291 | static inline void set_evtchn(int port) |
| 292 | { |
| 293 | struct shared_info *s = HYPERVISOR_shared_info; |
| 294 | sync_set_bit(port, &s->evtchn_pending[0]); |
| 295 | } |
| 296 | |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 297 | static inline int test_evtchn(int port) |
| 298 | { |
| 299 | struct shared_info *s = HYPERVISOR_shared_info; |
| 300 | return sync_test_bit(port, &s->evtchn_pending[0]); |
| 301 | } |
| 302 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * notify_remote_via_irq - send event to remote end of event channel via irq |
| 306 | * @irq: irq of event channel to send event to |
| 307 | * |
| 308 | * Unlike notify_remote_via_evtchn(), this is safe to use across |
| 309 | * save/restore. Notifications on a broken connection are silently |
| 310 | * dropped. |
| 311 | */ |
| 312 | void notify_remote_via_irq(int irq) |
| 313 | { |
| 314 | int evtchn = evtchn_from_irq(irq); |
| 315 | |
| 316 | if (VALID_EVTCHN(evtchn)) |
| 317 | notify_remote_via_evtchn(evtchn); |
| 318 | } |
| 319 | EXPORT_SYMBOL_GPL(notify_remote_via_irq); |
| 320 | |
| 321 | static void mask_evtchn(int port) |
| 322 | { |
| 323 | struct shared_info *s = HYPERVISOR_shared_info; |
| 324 | sync_set_bit(port, &s->evtchn_mask[0]); |
| 325 | } |
| 326 | |
| 327 | static void unmask_evtchn(int port) |
| 328 | { |
| 329 | struct shared_info *s = HYPERVISOR_shared_info; |
| 330 | unsigned int cpu = get_cpu(); |
| 331 | |
| 332 | BUG_ON(!irqs_disabled()); |
| 333 | |
| 334 | /* Slow path (hypercall) if this is a non-local port. */ |
| 335 | if (unlikely(cpu != cpu_from_evtchn(port))) { |
| 336 | struct evtchn_unmask unmask = { .port = port }; |
| 337 | (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask); |
| 338 | } else { |
| 339 | struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu); |
| 340 | |
| 341 | sync_clear_bit(port, &s->evtchn_mask[0]); |
| 342 | |
| 343 | /* |
| 344 | * The following is basically the equivalent of |
| 345 | * 'hw_resend_irq'. Just like a real IO-APIC we 'lose |
| 346 | * the interrupt edge' if the channel is masked. |
| 347 | */ |
| 348 | if (sync_test_bit(port, &s->evtchn_pending[0]) && |
| 349 | !sync_test_and_set_bit(port / BITS_PER_LONG, |
| 350 | &vcpu_info->evtchn_pending_sel)) |
| 351 | vcpu_info->evtchn_upcall_pending = 1; |
| 352 | } |
| 353 | |
| 354 | put_cpu(); |
| 355 | } |
| 356 | |
Konrad Rzeszutek Wilk | 0794bfc | 2010-10-18 10:41:08 -0400 | [diff] [blame] | 357 | static int get_nr_hw_irqs(void) |
| 358 | { |
| 359 | int ret = 1; |
| 360 | |
| 361 | #ifdef CONFIG_X86_IO_APIC |
| 362 | ret = get_nr_irqs_gsi(); |
| 363 | #endif |
| 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 368 | static int find_unbound_irq(void) |
| 369 | { |
Thomas Gleixner | 77dff1c | 2010-09-29 17:37:10 +0200 | [diff] [blame] | 370 | struct irq_data *data; |
| 371 | int irq, res; |
Konrad Rzeszutek Wilk | 3a69e91 | 2010-10-18 10:49:10 -0400 | [diff] [blame] | 372 | int start = get_nr_hw_irqs(); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 373 | |
Konrad Rzeszutek Wilk | 3a69e91 | 2010-10-18 10:49:10 -0400 | [diff] [blame] | 374 | if (start == nr_irqs) |
| 375 | goto no_irqs; |
| 376 | |
| 377 | /* nr_irqs is a magic value. Must not use it.*/ |
| 378 | for (irq = nr_irqs-1; irq > start; irq--) { |
Thomas Gleixner | 77dff1c | 2010-09-29 17:37:10 +0200 | [diff] [blame] | 379 | data = irq_get_irq_data(irq); |
Stefano Stabellini | 99ad198 | 2010-05-14 12:41:20 +0100 | [diff] [blame] | 380 | /* only 0->15 have init'd desc; handle irq > 16 */ |
Thomas Gleixner | 77dff1c | 2010-09-29 17:37:10 +0200 | [diff] [blame] | 381 | if (!data) |
Stefano Stabellini | 99ad198 | 2010-05-14 12:41:20 +0100 | [diff] [blame] | 382 | break; |
Thomas Gleixner | 77dff1c | 2010-09-29 17:37:10 +0200 | [diff] [blame] | 383 | if (data->chip == &no_irq_chip) |
Stefano Stabellini | 99ad198 | 2010-05-14 12:41:20 +0100 | [diff] [blame] | 384 | break; |
Thomas Gleixner | 77dff1c | 2010-09-29 17:37:10 +0200 | [diff] [blame] | 385 | if (data->chip != &xen_dynamic_chip) |
Stefano Stabellini | 99ad198 | 2010-05-14 12:41:20 +0100 | [diff] [blame] | 386 | continue; |
Jeremy Fitzhardinge | d77bbd4 | 2009-02-06 14:09:45 -0800 | [diff] [blame] | 387 | if (irq_info[irq].type == IRQT_UNBOUND) |
Thomas Gleixner | 77dff1c | 2010-09-29 17:37:10 +0200 | [diff] [blame] | 388 | return irq; |
Stefano Stabellini | 99ad198 | 2010-05-14 12:41:20 +0100 | [diff] [blame] | 389 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 390 | |
Konrad Rzeszutek Wilk | 3a69e91 | 2010-10-18 10:49:10 -0400 | [diff] [blame] | 391 | if (irq == start) |
| 392 | goto no_irqs; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 393 | |
Thomas Gleixner | 77dff1c | 2010-09-29 17:37:10 +0200 | [diff] [blame] | 394 | res = irq_alloc_desc_at(irq, 0); |
Jeremy Fitzhardinge | 6f8a0ed | 2008-12-17 13:42:29 -0800 | [diff] [blame] | 395 | |
Thomas Gleixner | 77dff1c | 2010-09-29 17:37:10 +0200 | [diff] [blame] | 396 | if (WARN_ON(res != irq)) |
| 397 | return -1; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 398 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 399 | return irq; |
Konrad Rzeszutek Wilk | 3a69e91 | 2010-10-18 10:49:10 -0400 | [diff] [blame] | 400 | |
| 401 | no_irqs: |
| 402 | panic("No available IRQ to bind to: increase nr_irqs!\n"); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 405 | static bool identity_mapped_irq(unsigned irq) |
| 406 | { |
Konrad Rzeszutek Wilk | 0794bfc | 2010-10-18 10:41:08 -0400 | [diff] [blame] | 407 | /* identity map all the hardware irqs */ |
| 408 | return irq < get_nr_hw_irqs(); |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static void pirq_unmask_notify(int irq) |
| 412 | { |
| 413 | struct physdev_eoi eoi = { .irq = irq }; |
| 414 | |
| 415 | if (unlikely(pirq_needs_eoi(irq))) { |
| 416 | int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi); |
| 417 | WARN_ON(rc); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static void pirq_query_unmask(int irq) |
| 422 | { |
| 423 | struct physdev_irq_status_query irq_status; |
| 424 | struct irq_info *info = info_for_irq(irq); |
| 425 | |
| 426 | BUG_ON(info->type != IRQT_PIRQ); |
| 427 | |
| 428 | irq_status.irq = irq; |
| 429 | if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status)) |
| 430 | irq_status.flags = 0; |
| 431 | |
| 432 | info->u.pirq.flags &= ~PIRQ_NEEDS_EOI; |
| 433 | if (irq_status.flags & XENIRQSTAT_needs_eoi) |
| 434 | info->u.pirq.flags |= PIRQ_NEEDS_EOI; |
| 435 | } |
| 436 | |
| 437 | static bool probing_irq(int irq) |
| 438 | { |
| 439 | struct irq_desc *desc = irq_to_desc(irq); |
| 440 | |
| 441 | return desc && desc->action == NULL; |
| 442 | } |
| 443 | |
| 444 | static unsigned int startup_pirq(unsigned int irq) |
| 445 | { |
| 446 | struct evtchn_bind_pirq bind_pirq; |
| 447 | struct irq_info *info = info_for_irq(irq); |
| 448 | int evtchn = evtchn_from_irq(irq); |
Konrad Rzeszutek Wilk | 15ebbb8 | 2010-10-04 13:43:27 -0400 | [diff] [blame] | 449 | int rc; |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 450 | |
| 451 | BUG_ON(info->type != IRQT_PIRQ); |
| 452 | |
| 453 | if (VALID_EVTCHN(evtchn)) |
| 454 | goto out; |
| 455 | |
| 456 | bind_pirq.pirq = irq; |
| 457 | /* NB. We are happy to share unless we are probing. */ |
Konrad Rzeszutek Wilk | 15ebbb8 | 2010-10-04 13:43:27 -0400 | [diff] [blame] | 458 | bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ? |
| 459 | BIND_PIRQ__WILL_SHARE : 0; |
| 460 | rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq); |
| 461 | if (rc != 0) { |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 462 | if (!probing_irq(irq)) |
| 463 | printk(KERN_INFO "Failed to obtain physical IRQ %d\n", |
| 464 | irq); |
| 465 | return 0; |
| 466 | } |
| 467 | evtchn = bind_pirq.port; |
| 468 | |
| 469 | pirq_query_unmask(irq); |
| 470 | |
| 471 | evtchn_to_irq[evtchn] = irq; |
| 472 | bind_evtchn_to_cpu(evtchn, 0); |
| 473 | info->evtchn = evtchn; |
| 474 | |
| 475 | out: |
| 476 | unmask_evtchn(evtchn); |
| 477 | pirq_unmask_notify(irq); |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static void shutdown_pirq(unsigned int irq) |
| 483 | { |
| 484 | struct evtchn_close close; |
| 485 | struct irq_info *info = info_for_irq(irq); |
| 486 | int evtchn = evtchn_from_irq(irq); |
| 487 | |
| 488 | BUG_ON(info->type != IRQT_PIRQ); |
| 489 | |
| 490 | if (!VALID_EVTCHN(evtchn)) |
| 491 | return; |
| 492 | |
| 493 | mask_evtchn(evtchn); |
| 494 | |
| 495 | close.port = evtchn; |
| 496 | if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0) |
| 497 | BUG(); |
| 498 | |
| 499 | bind_evtchn_to_cpu(evtchn, 0); |
| 500 | evtchn_to_irq[evtchn] = -1; |
| 501 | info->evtchn = 0; |
| 502 | } |
| 503 | |
| 504 | static void enable_pirq(unsigned int irq) |
| 505 | { |
| 506 | startup_pirq(irq); |
| 507 | } |
| 508 | |
| 509 | static void disable_pirq(unsigned int irq) |
| 510 | { |
| 511 | } |
| 512 | |
| 513 | static void ack_pirq(unsigned int irq) |
| 514 | { |
| 515 | int evtchn = evtchn_from_irq(irq); |
| 516 | |
| 517 | move_native_irq(irq); |
| 518 | |
| 519 | if (VALID_EVTCHN(evtchn)) { |
| 520 | mask_evtchn(evtchn); |
| 521 | clear_evtchn(evtchn); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | static void end_pirq(unsigned int irq) |
| 526 | { |
| 527 | int evtchn = evtchn_from_irq(irq); |
| 528 | struct irq_desc *desc = irq_to_desc(irq); |
| 529 | |
| 530 | if (WARN_ON(!desc)) |
| 531 | return; |
| 532 | |
| 533 | if ((desc->status & (IRQ_DISABLED|IRQ_PENDING)) == |
| 534 | (IRQ_DISABLED|IRQ_PENDING)) { |
| 535 | shutdown_pirq(irq); |
| 536 | } else if (VALID_EVTCHN(evtchn)) { |
| 537 | unmask_evtchn(evtchn); |
| 538 | pirq_unmask_notify(irq); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | static int find_irq_by_gsi(unsigned gsi) |
| 543 | { |
| 544 | int irq; |
| 545 | |
Jeremy Fitzhardinge | b21ddbf | 2010-06-07 16:28:49 -0400 | [diff] [blame] | 546 | for (irq = 0; irq < nr_irqs; irq++) { |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 547 | struct irq_info *info = info_for_irq(irq); |
| 548 | |
| 549 | if (info == NULL || info->type != IRQT_PIRQ) |
| 550 | continue; |
| 551 | |
| 552 | if (gsi_from_irq(irq) == gsi) |
| 553 | return irq; |
| 554 | } |
| 555 | |
| 556 | return -1; |
| 557 | } |
| 558 | |
Konrad Rzeszutek Wilk | 3a69e91 | 2010-10-18 10:49:10 -0400 | [diff] [blame] | 559 | /* xen_allocate_irq might allocate irqs from the top down, as a |
| 560 | * consequence don't assume that the irq number returned has a low value |
| 561 | * or can be used as a pirq number unless you know otherwise. |
| 562 | * |
| 563 | * One notable exception is when xen_allocate_irq is called passing an |
| 564 | * hardware gsi as argument, in that case the irq number returned |
| 565 | * matches the gsi number passed as first argument. |
| 566 | |
| 567 | * Note: We don't assign an |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 568 | * event channel until the irq actually started up. Return an |
| 569 | * existing irq if we've already got one for the gsi. |
| 570 | */ |
Konrad Rzeszutek Wilk | 15ebbb8 | 2010-10-04 13:43:27 -0400 | [diff] [blame] | 571 | int xen_allocate_pirq(unsigned gsi, int shareable, char *name) |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 572 | { |
| 573 | int irq; |
| 574 | struct physdev_irq irq_op; |
| 575 | |
| 576 | spin_lock(&irq_mapping_update_lock); |
| 577 | |
| 578 | irq = find_irq_by_gsi(gsi); |
| 579 | if (irq != -1) { |
| 580 | printk(KERN_INFO "xen_allocate_pirq: returning irq %d for gsi %u\n", |
| 581 | irq, gsi); |
| 582 | goto out; /* XXX need refcount? */ |
| 583 | } |
| 584 | |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame^] | 585 | /* If we are a PV guest, we don't have GSIs (no ACPI passed). Therefore |
| 586 | * we are using the !xen_initial_domain() to drop in the function.*/ |
| 587 | if (identity_mapped_irq(gsi) || !xen_initial_domain()) { |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 588 | irq = gsi; |
Konrad Rzeszutek Wilk | 0794bfc | 2010-10-18 10:41:08 -0400 | [diff] [blame] | 589 | irq_to_desc_alloc_node(irq, 0); |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 590 | dynamic_irq_init(irq); |
| 591 | } else |
| 592 | irq = find_unbound_irq(); |
| 593 | |
| 594 | set_irq_chip_and_handler_name(irq, &xen_pirq_chip, |
Gerd Hoffmann | 1a60d05 | 2010-10-04 13:42:27 -0400 | [diff] [blame] | 595 | handle_level_irq, name); |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 596 | |
| 597 | irq_op.irq = irq; |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame^] | 598 | irq_op.vector = 0; |
| 599 | |
| 600 | /* Only the privileged domain can do this. For non-priv, the pcifront |
| 601 | * driver provides a PCI bus that does the call to do exactly |
| 602 | * this in the priv domain. */ |
| 603 | if (xen_initial_domain() && |
| 604 | HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) { |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 605 | dynamic_irq_cleanup(irq); |
| 606 | irq = -ENOSPC; |
| 607 | goto out; |
| 608 | } |
| 609 | |
| 610 | irq_info[irq] = mk_pirq_info(0, gsi, irq_op.vector); |
Konrad Rzeszutek Wilk | 15ebbb8 | 2010-10-04 13:43:27 -0400 | [diff] [blame] | 611 | irq_info[irq].u.pirq.flags |= shareable ? PIRQ_SHAREABLE : 0; |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 612 | |
| 613 | out: |
| 614 | spin_unlock(&irq_mapping_update_lock); |
| 615 | |
| 616 | return irq; |
| 617 | } |
| 618 | |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame^] | 619 | int xen_destroy_irq(int irq) |
| 620 | { |
| 621 | struct irq_desc *desc; |
| 622 | int rc = -ENOENT; |
| 623 | |
| 624 | spin_lock(&irq_mapping_update_lock); |
| 625 | |
| 626 | desc = irq_to_desc(irq); |
| 627 | if (!desc) |
| 628 | goto out; |
| 629 | |
| 630 | irq_info[irq] = mk_unbound_info(); |
| 631 | |
| 632 | dynamic_irq_cleanup(irq); |
| 633 | |
| 634 | out: |
| 635 | spin_unlock(&irq_mapping_update_lock); |
| 636 | return rc; |
| 637 | } |
| 638 | |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 639 | int xen_vector_from_irq(unsigned irq) |
| 640 | { |
| 641 | return vector_from_irq(irq); |
| 642 | } |
| 643 | |
| 644 | int xen_gsi_from_irq(unsigned irq) |
| 645 | { |
| 646 | return gsi_from_irq(irq); |
| 647 | } |
| 648 | |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 649 | int bind_evtchn_to_irq(unsigned int evtchn) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 650 | { |
| 651 | int irq; |
| 652 | |
| 653 | spin_lock(&irq_mapping_update_lock); |
| 654 | |
| 655 | irq = evtchn_to_irq[evtchn]; |
| 656 | |
| 657 | if (irq == -1) { |
| 658 | irq = find_unbound_irq(); |
| 659 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 660 | set_irq_chip_and_handler_name(irq, &xen_dynamic_chip, |
Jeremy Fitzhardinge | dffe2e1 | 2010-08-20 19:10:01 -0700 | [diff] [blame] | 661 | handle_edge_irq, "event"); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 662 | |
| 663 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 664 | irq_info[irq] = mk_evtchn_info(evtchn); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 667 | spin_unlock(&irq_mapping_update_lock); |
| 668 | |
| 669 | return irq; |
| 670 | } |
Jeremy Fitzhardinge | b536b4b | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 671 | EXPORT_SYMBOL_GPL(bind_evtchn_to_irq); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 672 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 673 | static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu) |
| 674 | { |
| 675 | struct evtchn_bind_ipi bind_ipi; |
| 676 | int evtchn, irq; |
| 677 | |
| 678 | spin_lock(&irq_mapping_update_lock); |
| 679 | |
| 680 | irq = per_cpu(ipi_to_irq, cpu)[ipi]; |
Ian Campbell | 90af951 | 2009-02-06 16:55:58 -0800 | [diff] [blame] | 681 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 682 | if (irq == -1) { |
| 683 | irq = find_unbound_irq(); |
| 684 | if (irq < 0) |
| 685 | goto out; |
| 686 | |
Jeremy Fitzhardinge | aaca496 | 2010-08-20 18:57:53 -0700 | [diff] [blame] | 687 | set_irq_chip_and_handler_name(irq, &xen_percpu_chip, |
| 688 | handle_percpu_irq, "ipi"); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 689 | |
| 690 | bind_ipi.vcpu = cpu; |
| 691 | if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi, |
| 692 | &bind_ipi) != 0) |
| 693 | BUG(); |
| 694 | evtchn = bind_ipi.port; |
| 695 | |
| 696 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 697 | irq_info[irq] = mk_ipi_info(evtchn, ipi); |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 698 | per_cpu(ipi_to_irq, cpu)[ipi] = irq; |
| 699 | |
| 700 | bind_evtchn_to_cpu(evtchn, cpu); |
| 701 | } |
| 702 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 703 | out: |
| 704 | spin_unlock(&irq_mapping_update_lock); |
| 705 | return irq; |
| 706 | } |
| 707 | |
| 708 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 709 | static int bind_virq_to_irq(unsigned int virq, unsigned int cpu) |
| 710 | { |
| 711 | struct evtchn_bind_virq bind_virq; |
| 712 | int evtchn, irq; |
| 713 | |
| 714 | spin_lock(&irq_mapping_update_lock); |
| 715 | |
| 716 | irq = per_cpu(virq_to_irq, cpu)[virq]; |
| 717 | |
| 718 | if (irq == -1) { |
| 719 | bind_virq.virq = virq; |
| 720 | bind_virq.vcpu = cpu; |
| 721 | if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, |
| 722 | &bind_virq) != 0) |
| 723 | BUG(); |
| 724 | evtchn = bind_virq.port; |
| 725 | |
| 726 | irq = find_unbound_irq(); |
| 727 | |
Jeremy Fitzhardinge | aaca496 | 2010-08-20 18:57:53 -0700 | [diff] [blame] | 728 | set_irq_chip_and_handler_name(irq, &xen_percpu_chip, |
| 729 | handle_percpu_irq, "virq"); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 730 | |
| 731 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 732 | irq_info[irq] = mk_virq_info(evtchn, virq); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 733 | |
| 734 | per_cpu(virq_to_irq, cpu)[virq] = irq; |
| 735 | |
| 736 | bind_evtchn_to_cpu(evtchn, cpu); |
| 737 | } |
| 738 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 739 | spin_unlock(&irq_mapping_update_lock); |
| 740 | |
| 741 | return irq; |
| 742 | } |
| 743 | |
| 744 | static void unbind_from_irq(unsigned int irq) |
| 745 | { |
| 746 | struct evtchn_close close; |
| 747 | int evtchn = evtchn_from_irq(irq); |
| 748 | |
| 749 | spin_lock(&irq_mapping_update_lock); |
| 750 | |
Jeremy Fitzhardinge | d77bbd4 | 2009-02-06 14:09:45 -0800 | [diff] [blame] | 751 | if (VALID_EVTCHN(evtchn)) { |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 752 | close.port = evtchn; |
| 753 | if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0) |
| 754 | BUG(); |
| 755 | |
| 756 | switch (type_from_irq(irq)) { |
| 757 | case IRQT_VIRQ: |
| 758 | per_cpu(virq_to_irq, cpu_from_evtchn(evtchn)) |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 759 | [virq_from_irq(irq)] = -1; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 760 | break; |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 761 | case IRQT_IPI: |
| 762 | per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn)) |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 763 | [ipi_from_irq(irq)] = -1; |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 764 | break; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 765 | default: |
| 766 | break; |
| 767 | } |
| 768 | |
| 769 | /* Closed ports are implicitly re-bound to VCPU0. */ |
| 770 | bind_evtchn_to_cpu(evtchn, 0); |
| 771 | |
| 772 | evtchn_to_irq[evtchn] = -1; |
Ian Campbell | fed5ea8 | 2009-12-01 16:15:30 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | if (irq_info[irq].type != IRQT_UNBOUND) { |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 776 | irq_info[irq] = mk_unbound_info(); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 777 | |
Thomas Gleixner | 77dff1c | 2010-09-29 17:37:10 +0200 | [diff] [blame] | 778 | irq_free_desc(irq); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | spin_unlock(&irq_mapping_update_lock); |
| 782 | } |
| 783 | |
| 784 | int bind_evtchn_to_irqhandler(unsigned int evtchn, |
Jeff Garzik | 7c23997 | 2007-10-19 03:12:20 -0400 | [diff] [blame] | 785 | irq_handler_t handler, |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 786 | unsigned long irqflags, |
| 787 | const char *devname, void *dev_id) |
| 788 | { |
| 789 | unsigned int irq; |
| 790 | int retval; |
| 791 | |
| 792 | irq = bind_evtchn_to_irq(evtchn); |
| 793 | retval = request_irq(irq, handler, irqflags, devname, dev_id); |
| 794 | if (retval != 0) { |
| 795 | unbind_from_irq(irq); |
| 796 | return retval; |
| 797 | } |
| 798 | |
| 799 | return irq; |
| 800 | } |
| 801 | EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler); |
| 802 | |
| 803 | int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu, |
Jeff Garzik | 7c23997 | 2007-10-19 03:12:20 -0400 | [diff] [blame] | 804 | irq_handler_t handler, |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 805 | unsigned long irqflags, const char *devname, void *dev_id) |
| 806 | { |
| 807 | unsigned int irq; |
| 808 | int retval; |
| 809 | |
| 810 | irq = bind_virq_to_irq(virq, cpu); |
| 811 | retval = request_irq(irq, handler, irqflags, devname, dev_id); |
| 812 | if (retval != 0) { |
| 813 | unbind_from_irq(irq); |
| 814 | return retval; |
| 815 | } |
| 816 | |
| 817 | return irq; |
| 818 | } |
| 819 | EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler); |
| 820 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 821 | int bind_ipi_to_irqhandler(enum ipi_vector ipi, |
| 822 | unsigned int cpu, |
| 823 | irq_handler_t handler, |
| 824 | unsigned long irqflags, |
| 825 | const char *devname, |
| 826 | void *dev_id) |
| 827 | { |
| 828 | int irq, retval; |
| 829 | |
| 830 | irq = bind_ipi_to_irq(ipi, cpu); |
| 831 | if (irq < 0) |
| 832 | return irq; |
| 833 | |
Ian Campbell | 4877c73 | 2010-07-29 11:16:35 +0100 | [diff] [blame] | 834 | irqflags |= IRQF_NO_SUSPEND; |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 835 | retval = request_irq(irq, handler, irqflags, devname, dev_id); |
| 836 | if (retval != 0) { |
| 837 | unbind_from_irq(irq); |
| 838 | return retval; |
| 839 | } |
| 840 | |
| 841 | return irq; |
| 842 | } |
| 843 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 844 | void unbind_from_irqhandler(unsigned int irq, void *dev_id) |
| 845 | { |
| 846 | free_irq(irq, dev_id); |
| 847 | unbind_from_irq(irq); |
| 848 | } |
| 849 | EXPORT_SYMBOL_GPL(unbind_from_irqhandler); |
| 850 | |
Jeremy Fitzhardinge | f87e4ca | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 851 | void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector) |
| 852 | { |
| 853 | int irq = per_cpu(ipi_to_irq, cpu)[vector]; |
| 854 | BUG_ON(irq < 0); |
| 855 | notify_remote_via_irq(irq); |
| 856 | } |
| 857 | |
Jeremy Fitzhardinge | ee523ca | 2008-03-17 16:37:18 -0700 | [diff] [blame] | 858 | irqreturn_t xen_debug_interrupt(int irq, void *dev_id) |
| 859 | { |
| 860 | struct shared_info *sh = HYPERVISOR_shared_info; |
| 861 | int cpu = smp_processor_id(); |
| 862 | int i; |
| 863 | unsigned long flags; |
| 864 | static DEFINE_SPINLOCK(debug_lock); |
| 865 | |
| 866 | spin_lock_irqsave(&debug_lock, flags); |
| 867 | |
| 868 | printk("vcpu %d\n ", cpu); |
| 869 | |
| 870 | for_each_online_cpu(i) { |
| 871 | struct vcpu_info *v = per_cpu(xen_vcpu, i); |
| 872 | printk("%d: masked=%d pending=%d event_sel %08lx\n ", i, |
Isaku Yamahata | e849c3e | 2008-04-02 10:53:56 -0700 | [diff] [blame] | 873 | (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask, |
Jeremy Fitzhardinge | ee523ca | 2008-03-17 16:37:18 -0700 | [diff] [blame] | 874 | v->evtchn_upcall_pending, |
| 875 | v->evtchn_pending_sel); |
| 876 | } |
| 877 | printk("pending:\n "); |
| 878 | for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--) |
| 879 | printk("%08lx%s", sh->evtchn_pending[i], |
| 880 | i % 8 == 0 ? "\n " : " "); |
| 881 | printk("\nmasks:\n "); |
| 882 | for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) |
| 883 | printk("%08lx%s", sh->evtchn_mask[i], |
| 884 | i % 8 == 0 ? "\n " : " "); |
| 885 | |
| 886 | printk("\nunmasked:\n "); |
| 887 | for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) |
| 888 | printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i], |
| 889 | i % 8 == 0 ? "\n " : " "); |
| 890 | |
| 891 | printk("\npending list:\n"); |
| 892 | for(i = 0; i < NR_EVENT_CHANNELS; i++) { |
| 893 | if (sync_test_bit(i, sh->evtchn_pending)) { |
| 894 | printk(" %d: event %d -> irq %d\n", |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 895 | cpu_from_evtchn(i), i, |
| 896 | evtchn_to_irq[i]); |
Jeremy Fitzhardinge | ee523ca | 2008-03-17 16:37:18 -0700 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
| 900 | spin_unlock_irqrestore(&debug_lock, flags); |
| 901 | |
| 902 | return IRQ_HANDLED; |
| 903 | } |
| 904 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 905 | static DEFINE_PER_CPU(unsigned, xed_nesting_count); |
| 906 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 907 | /* |
| 908 | * Search the CPUs pending events bitmasks. For each one found, map |
| 909 | * the event number to an irq, and feed it into do_IRQ() for |
| 910 | * handling. |
| 911 | * |
| 912 | * Xen uses a two-level bitmap to speed searching. The first level is |
| 913 | * a bitset of words which contain pending event bits. The second |
| 914 | * level is a bitset of pending events themselves. |
| 915 | */ |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 916 | static void __xen_evtchn_do_upcall(void) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 917 | { |
| 918 | int cpu = get_cpu(); |
| 919 | struct shared_info *s = HYPERVISOR_shared_info; |
| 920 | struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu); |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 921 | unsigned count; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 922 | |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 923 | do { |
| 924 | unsigned long pending_words; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 925 | |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 926 | vcpu_info->evtchn_upcall_pending = 0; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 927 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 928 | if (__get_cpu_var(xed_nesting_count)++) |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 929 | goto out; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 930 | |
Isaku Yamahata | e849c3e | 2008-04-02 10:53:56 -0700 | [diff] [blame] | 931 | #ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */ |
| 932 | /* Clear master flag /before/ clearing selector flag. */ |
Isaku Yamahata | 6673cf6 | 2008-06-16 14:58:13 -0700 | [diff] [blame] | 933 | wmb(); |
Isaku Yamahata | e849c3e | 2008-04-02 10:53:56 -0700 | [diff] [blame] | 934 | #endif |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 935 | pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0); |
| 936 | while (pending_words != 0) { |
| 937 | unsigned long pending_bits; |
| 938 | int word_idx = __ffs(pending_words); |
| 939 | pending_words &= ~(1UL << word_idx); |
| 940 | |
| 941 | while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) { |
| 942 | int bit_idx = __ffs(pending_bits); |
| 943 | int port = (word_idx * BITS_PER_LONG) + bit_idx; |
| 944 | int irq = evtchn_to_irq[port]; |
Eric W. Biederman | ca4dbc6 | 2010-02-17 18:49:54 -0800 | [diff] [blame] | 945 | struct irq_desc *desc; |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 946 | |
Eric W. Biederman | ca4dbc6 | 2010-02-17 18:49:54 -0800 | [diff] [blame] | 947 | if (irq != -1) { |
| 948 | desc = irq_to_desc(irq); |
| 949 | if (desc) |
| 950 | generic_handle_irq_desc(irq, desc); |
| 951 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 952 | } |
| 953 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 954 | |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 955 | BUG_ON(!irqs_disabled()); |
| 956 | |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 957 | count = __get_cpu_var(xed_nesting_count); |
| 958 | __get_cpu_var(xed_nesting_count) = 0; |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 959 | } while (count != 1 || vcpu_info->evtchn_upcall_pending); |
Jeremy Fitzhardinge | 229664b | 2008-03-17 16:37:20 -0700 | [diff] [blame] | 960 | |
| 961 | out: |
Jeremy Fitzhardinge | 3445a8f | 2009-02-06 14:09:46 -0800 | [diff] [blame] | 962 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 963 | put_cpu(); |
| 964 | } |
| 965 | |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 966 | void xen_evtchn_do_upcall(struct pt_regs *regs) |
| 967 | { |
| 968 | struct pt_regs *old_regs = set_irq_regs(regs); |
| 969 | |
| 970 | exit_idle(); |
| 971 | irq_enter(); |
| 972 | |
| 973 | __xen_evtchn_do_upcall(); |
| 974 | |
| 975 | irq_exit(); |
| 976 | set_irq_regs(old_regs); |
| 977 | } |
| 978 | |
| 979 | void xen_hvm_evtchn_do_upcall(void) |
| 980 | { |
| 981 | __xen_evtchn_do_upcall(); |
| 982 | } |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 983 | EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall); |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 984 | |
Jeremy Fitzhardinge | eb1e305 | 2008-05-26 23:31:23 +0100 | [diff] [blame] | 985 | /* Rebind a new event channel to an existing irq. */ |
| 986 | void rebind_evtchn_irq(int evtchn, int irq) |
| 987 | { |
Jeremy Fitzhardinge | d77bbd4 | 2009-02-06 14:09:45 -0800 | [diff] [blame] | 988 | struct irq_info *info = info_for_irq(irq); |
| 989 | |
Jeremy Fitzhardinge | eb1e305 | 2008-05-26 23:31:23 +0100 | [diff] [blame] | 990 | /* Make sure the irq is masked, since the new event channel |
| 991 | will also be masked. */ |
| 992 | disable_irq(irq); |
| 993 | |
| 994 | spin_lock(&irq_mapping_update_lock); |
| 995 | |
| 996 | /* After resume the irq<->evtchn mappings are all cleared out */ |
| 997 | BUG_ON(evtchn_to_irq[evtchn] != -1); |
| 998 | /* Expect irq to have been bound before, |
Jeremy Fitzhardinge | d77bbd4 | 2009-02-06 14:09:45 -0800 | [diff] [blame] | 999 | so there should be a proper type */ |
| 1000 | BUG_ON(info->type == IRQT_UNBOUND); |
Jeremy Fitzhardinge | eb1e305 | 2008-05-26 23:31:23 +0100 | [diff] [blame] | 1001 | |
| 1002 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 1003 | irq_info[irq] = mk_evtchn_info(evtchn); |
Jeremy Fitzhardinge | eb1e305 | 2008-05-26 23:31:23 +0100 | [diff] [blame] | 1004 | |
| 1005 | spin_unlock(&irq_mapping_update_lock); |
| 1006 | |
| 1007 | /* new event channels are always bound to cpu 0 */ |
Rusty Russell | 0de2652 | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 1008 | irq_set_affinity(irq, cpumask_of(0)); |
Jeremy Fitzhardinge | eb1e305 | 2008-05-26 23:31:23 +0100 | [diff] [blame] | 1009 | |
| 1010 | /* Unmask the event channel. */ |
| 1011 | enable_irq(irq); |
| 1012 | } |
| 1013 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1014 | /* Rebind an evtchn so that it gets delivered to a specific cpu */ |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 1015 | static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1016 | { |
| 1017 | struct evtchn_bind_vcpu bind_vcpu; |
| 1018 | int evtchn = evtchn_from_irq(irq); |
| 1019 | |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 1020 | /* events delivered via platform PCI interrupts are always |
| 1021 | * routed to vcpu 0 */ |
| 1022 | if (!VALID_EVTCHN(evtchn) || |
| 1023 | (xen_hvm_domain() && !xen_have_vector_callback)) |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 1024 | return -1; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1025 | |
| 1026 | /* Send future instances of this interrupt to other vcpu. */ |
| 1027 | bind_vcpu.port = evtchn; |
| 1028 | bind_vcpu.vcpu = tcpu; |
| 1029 | |
| 1030 | /* |
| 1031 | * If this fails, it usually just indicates that we're dealing with a |
| 1032 | * virq or IPI channel, which don't actually need to be rebound. Ignore |
| 1033 | * it, but don't do the xenlinux-level rebind in that case. |
| 1034 | */ |
| 1035 | if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0) |
| 1036 | bind_evtchn_to_cpu(evtchn, tcpu); |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 1037 | |
| 1038 | return 0; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 1041 | static int set_affinity_irq(unsigned irq, const struct cpumask *dest) |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1042 | { |
Rusty Russell | 0de2652 | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 1043 | unsigned tcpu = cpumask_first(dest); |
Yinghai Lu | d5dedd4 | 2009-04-27 17:59:21 -0700 | [diff] [blame] | 1044 | |
| 1045 | return rebind_irq_to_cpu(irq, tcpu); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Isaku Yamahata | 642e0c8 | 2008-04-02 10:53:57 -0700 | [diff] [blame] | 1048 | int resend_irq_on_evtchn(unsigned int irq) |
| 1049 | { |
| 1050 | int masked, evtchn = evtchn_from_irq(irq); |
| 1051 | struct shared_info *s = HYPERVISOR_shared_info; |
| 1052 | |
| 1053 | if (!VALID_EVTCHN(evtchn)) |
| 1054 | return 1; |
| 1055 | |
| 1056 | masked = sync_test_and_set_bit(evtchn, s->evtchn_mask); |
| 1057 | sync_set_bit(evtchn, s->evtchn_pending); |
| 1058 | if (!masked) |
| 1059 | unmask_evtchn(evtchn); |
| 1060 | |
| 1061 | return 1; |
| 1062 | } |
| 1063 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1064 | static void enable_dynirq(unsigned int irq) |
| 1065 | { |
| 1066 | int evtchn = evtchn_from_irq(irq); |
| 1067 | |
| 1068 | if (VALID_EVTCHN(evtchn)) |
| 1069 | unmask_evtchn(evtchn); |
| 1070 | } |
| 1071 | |
| 1072 | static void disable_dynirq(unsigned int irq) |
| 1073 | { |
| 1074 | int evtchn = evtchn_from_irq(irq); |
| 1075 | |
| 1076 | if (VALID_EVTCHN(evtchn)) |
| 1077 | mask_evtchn(evtchn); |
| 1078 | } |
| 1079 | |
| 1080 | static void ack_dynirq(unsigned int irq) |
| 1081 | { |
| 1082 | int evtchn = evtchn_from_irq(irq); |
| 1083 | |
| 1084 | move_native_irq(irq); |
| 1085 | |
| 1086 | if (VALID_EVTCHN(evtchn)) |
| 1087 | clear_evtchn(evtchn); |
| 1088 | } |
| 1089 | |
| 1090 | static int retrigger_dynirq(unsigned int irq) |
| 1091 | { |
| 1092 | int evtchn = evtchn_from_irq(irq); |
Jeremy Fitzhardinge | ee8fa1c | 2008-03-17 16:37:19 -0700 | [diff] [blame] | 1093 | struct shared_info *sh = HYPERVISOR_shared_info; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1094 | int ret = 0; |
| 1095 | |
| 1096 | if (VALID_EVTCHN(evtchn)) { |
Jeremy Fitzhardinge | ee8fa1c | 2008-03-17 16:37:19 -0700 | [diff] [blame] | 1097 | int masked; |
| 1098 | |
| 1099 | masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask); |
| 1100 | sync_set_bit(evtchn, sh->evtchn_pending); |
| 1101 | if (!masked) |
| 1102 | unmask_evtchn(evtchn); |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1103 | ret = 1; |
| 1104 | } |
| 1105 | |
| 1106 | return ret; |
| 1107 | } |
| 1108 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 1109 | static void restore_cpu_virqs(unsigned int cpu) |
| 1110 | { |
| 1111 | struct evtchn_bind_virq bind_virq; |
| 1112 | int virq, irq, evtchn; |
| 1113 | |
| 1114 | for (virq = 0; virq < NR_VIRQS; virq++) { |
| 1115 | if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1) |
| 1116 | continue; |
| 1117 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 1118 | BUG_ON(virq_from_irq(irq) != virq); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 1119 | |
| 1120 | /* Get a new binding from Xen. */ |
| 1121 | bind_virq.virq = virq; |
| 1122 | bind_virq.vcpu = cpu; |
| 1123 | if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, |
| 1124 | &bind_virq) != 0) |
| 1125 | BUG(); |
| 1126 | evtchn = bind_virq.port; |
| 1127 | |
| 1128 | /* Record the new mapping. */ |
| 1129 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 1130 | irq_info[irq] = mk_virq_info(evtchn, virq); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 1131 | bind_evtchn_to_cpu(evtchn, cpu); |
| 1132 | |
| 1133 | /* Ready for use. */ |
| 1134 | unmask_evtchn(evtchn); |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | static void restore_cpu_ipis(unsigned int cpu) |
| 1139 | { |
| 1140 | struct evtchn_bind_ipi bind_ipi; |
| 1141 | int ipi, irq, evtchn; |
| 1142 | |
| 1143 | for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) { |
| 1144 | if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1) |
| 1145 | continue; |
| 1146 | |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 1147 | BUG_ON(ipi_from_irq(irq) != ipi); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 1148 | |
| 1149 | /* Get a new binding from Xen. */ |
| 1150 | bind_ipi.vcpu = cpu; |
| 1151 | if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi, |
| 1152 | &bind_ipi) != 0) |
| 1153 | BUG(); |
| 1154 | evtchn = bind_ipi.port; |
| 1155 | |
| 1156 | /* Record the new mapping. */ |
| 1157 | evtchn_to_irq[evtchn] = irq; |
Jeremy Fitzhardinge | ced40d0 | 2009-02-06 14:09:44 -0800 | [diff] [blame] | 1158 | irq_info[irq] = mk_ipi_info(evtchn, ipi); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 1159 | bind_evtchn_to_cpu(evtchn, cpu); |
| 1160 | |
| 1161 | /* Ready for use. */ |
| 1162 | unmask_evtchn(evtchn); |
| 1163 | |
| 1164 | } |
| 1165 | } |
| 1166 | |
Jeremy Fitzhardinge | 2d9e1e2 | 2008-07-07 12:07:53 -0700 | [diff] [blame] | 1167 | /* Clear an irq's pending state, in preparation for polling on it */ |
| 1168 | void xen_clear_irq_pending(int irq) |
| 1169 | { |
| 1170 | int evtchn = evtchn_from_irq(irq); |
| 1171 | |
| 1172 | if (VALID_EVTCHN(evtchn)) |
| 1173 | clear_evtchn(evtchn); |
| 1174 | } |
Konrad Rzeszutek Wilk | d9a8814 | 2009-11-05 16:33:09 -0500 | [diff] [blame] | 1175 | EXPORT_SYMBOL(xen_clear_irq_pending); |
Jeremy Fitzhardinge | 168d2f4 | 2008-08-20 17:02:18 -0700 | [diff] [blame] | 1176 | void xen_set_irq_pending(int irq) |
| 1177 | { |
| 1178 | int evtchn = evtchn_from_irq(irq); |
| 1179 | |
| 1180 | if (VALID_EVTCHN(evtchn)) |
| 1181 | set_evtchn(evtchn); |
| 1182 | } |
| 1183 | |
| 1184 | bool xen_test_irq_pending(int irq) |
| 1185 | { |
| 1186 | int evtchn = evtchn_from_irq(irq); |
| 1187 | bool ret = false; |
| 1188 | |
| 1189 | if (VALID_EVTCHN(evtchn)) |
| 1190 | ret = test_evtchn(evtchn); |
| 1191 | |
| 1192 | return ret; |
| 1193 | } |
| 1194 | |
Konrad Rzeszutek Wilk | d9a8814 | 2009-11-05 16:33:09 -0500 | [diff] [blame] | 1195 | /* Poll waiting for an irq to become pending with timeout. In the usual case, |
| 1196 | * the irq will be disabled so it won't deliver an interrupt. */ |
| 1197 | void xen_poll_irq_timeout(int irq, u64 timeout) |
Jeremy Fitzhardinge | 2d9e1e2 | 2008-07-07 12:07:53 -0700 | [diff] [blame] | 1198 | { |
| 1199 | evtchn_port_t evtchn = evtchn_from_irq(irq); |
| 1200 | |
| 1201 | if (VALID_EVTCHN(evtchn)) { |
| 1202 | struct sched_poll poll; |
| 1203 | |
| 1204 | poll.nr_ports = 1; |
Konrad Rzeszutek Wilk | d9a8814 | 2009-11-05 16:33:09 -0500 | [diff] [blame] | 1205 | poll.timeout = timeout; |
Isaku Yamahata | ff3c536 | 2008-10-14 17:50:44 -0700 | [diff] [blame] | 1206 | set_xen_guest_handle(poll.ports, &evtchn); |
Jeremy Fitzhardinge | 2d9e1e2 | 2008-07-07 12:07:53 -0700 | [diff] [blame] | 1207 | |
| 1208 | if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0) |
| 1209 | BUG(); |
| 1210 | } |
| 1211 | } |
Konrad Rzeszutek Wilk | d9a8814 | 2009-11-05 16:33:09 -0500 | [diff] [blame] | 1212 | EXPORT_SYMBOL(xen_poll_irq_timeout); |
| 1213 | /* Poll waiting for an irq to become pending. In the usual case, the |
| 1214 | * irq will be disabled so it won't deliver an interrupt. */ |
| 1215 | void xen_poll_irq(int irq) |
| 1216 | { |
| 1217 | xen_poll_irq_timeout(irq, 0 /* no timeout */); |
| 1218 | } |
Jeremy Fitzhardinge | 2d9e1e2 | 2008-07-07 12:07:53 -0700 | [diff] [blame] | 1219 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 1220 | void xen_irq_resume(void) |
| 1221 | { |
| 1222 | unsigned int cpu, irq, evtchn; |
| 1223 | |
| 1224 | init_evtchn_cpu_bindings(); |
| 1225 | |
| 1226 | /* New event-channel space is not 'live' yet. */ |
| 1227 | for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++) |
| 1228 | mask_evtchn(evtchn); |
| 1229 | |
| 1230 | /* No IRQ <-> event-channel mappings. */ |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 1231 | for (irq = 0; irq < nr_irqs; irq++) |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 1232 | irq_info[irq].evtchn = 0; /* zap event-channel binding */ |
| 1233 | |
| 1234 | for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++) |
| 1235 | evtchn_to_irq[evtchn] = -1; |
| 1236 | |
| 1237 | for_each_possible_cpu(cpu) { |
| 1238 | restore_cpu_virqs(cpu); |
| 1239 | restore_cpu_ipis(cpu); |
| 1240 | } |
| 1241 | } |
| 1242 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1243 | static struct irq_chip xen_dynamic_chip __read_mostly = { |
| 1244 | .name = "xen-dyn", |
Jeremy Fitzhardinge | 54a353a | 2009-02-06 14:09:42 -0800 | [diff] [blame] | 1245 | |
| 1246 | .disable = disable_dynirq, |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1247 | .mask = disable_dynirq, |
| 1248 | .unmask = enable_dynirq, |
Jeremy Fitzhardinge | 54a353a | 2009-02-06 14:09:42 -0800 | [diff] [blame] | 1249 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1250 | .ack = ack_dynirq, |
| 1251 | .set_affinity = set_affinity_irq, |
| 1252 | .retrigger = retrigger_dynirq, |
| 1253 | }; |
| 1254 | |
Jeremy Fitzhardinge | d46a78b | 2010-10-01 12:20:09 -0400 | [diff] [blame] | 1255 | static struct irq_chip xen_pirq_chip __read_mostly = { |
| 1256 | .name = "xen-pirq", |
| 1257 | |
| 1258 | .startup = startup_pirq, |
| 1259 | .shutdown = shutdown_pirq, |
| 1260 | |
| 1261 | .enable = enable_pirq, |
| 1262 | .unmask = enable_pirq, |
| 1263 | |
| 1264 | .disable = disable_pirq, |
| 1265 | .mask = disable_pirq, |
| 1266 | |
| 1267 | .ack = ack_pirq, |
| 1268 | .end = end_pirq, |
| 1269 | |
| 1270 | .set_affinity = set_affinity_irq, |
| 1271 | |
| 1272 | .retrigger = retrigger_dynirq, |
| 1273 | }; |
| 1274 | |
Jeremy Fitzhardinge | aaca496 | 2010-08-20 18:57:53 -0700 | [diff] [blame] | 1275 | static struct irq_chip xen_percpu_chip __read_mostly = { |
| 1276 | .name = "xen-percpu", |
| 1277 | |
| 1278 | .disable = disable_dynirq, |
| 1279 | .mask = disable_dynirq, |
| 1280 | .unmask = enable_dynirq, |
| 1281 | |
| 1282 | .ack = ack_dynirq, |
| 1283 | }; |
| 1284 | |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 1285 | int xen_set_callback_via(uint64_t via) |
| 1286 | { |
| 1287 | struct xen_hvm_param a; |
| 1288 | a.domid = DOMID_SELF; |
| 1289 | a.index = HVM_PARAM_CALLBACK_IRQ; |
| 1290 | a.value = via; |
| 1291 | return HYPERVISOR_hvm_op(HVMOP_set_param, &a); |
| 1292 | } |
| 1293 | EXPORT_SYMBOL_GPL(xen_set_callback_via); |
| 1294 | |
Stefano Stabellini | ca65f9f | 2010-07-29 14:37:48 +0100 | [diff] [blame] | 1295 | #ifdef CONFIG_XEN_PVHVM |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 1296 | /* Vector callbacks are better than PCI interrupts to receive event |
| 1297 | * channel notifications because we can receive vector callbacks on any |
| 1298 | * vcpu and we don't need PCI support or APIC interactions. */ |
| 1299 | void xen_callback_vector(void) |
| 1300 | { |
| 1301 | int rc; |
| 1302 | uint64_t callback_via; |
| 1303 | if (xen_have_vector_callback) { |
| 1304 | callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK); |
| 1305 | rc = xen_set_callback_via(callback_via); |
| 1306 | if (rc) { |
| 1307 | printk(KERN_ERR "Request for Xen HVM callback vector" |
| 1308 | " failed.\n"); |
| 1309 | xen_have_vector_callback = 0; |
| 1310 | return; |
| 1311 | } |
| 1312 | printk(KERN_INFO "Xen HVM callback vector for event delivery is " |
| 1313 | "enabled\n"); |
| 1314 | /* in the restore case the vector has already been allocated */ |
| 1315 | if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors)) |
| 1316 | alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector); |
| 1317 | } |
| 1318 | } |
Stefano Stabellini | ca65f9f | 2010-07-29 14:37:48 +0100 | [diff] [blame] | 1319 | #else |
| 1320 | void xen_callback_vector(void) {} |
| 1321 | #endif |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 1322 | |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1323 | void __init xen_init_IRQ(void) |
| 1324 | { |
| 1325 | int i; |
Mike Travis | c7a3589 | 2009-01-10 21:58:11 -0800 | [diff] [blame] | 1326 | |
Pekka Enberg | a70c352 | 2009-07-01 11:51:18 +0300 | [diff] [blame] | 1327 | cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s), |
| 1328 | GFP_KERNEL); |
Jeremy Fitzhardinge | b21ddbf | 2010-06-07 16:28:49 -0400 | [diff] [blame] | 1329 | irq_info = kcalloc(nr_irqs, sizeof(*irq_info), GFP_KERNEL); |
| 1330 | |
| 1331 | evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq), |
| 1332 | GFP_KERNEL); |
| 1333 | for (i = 0; i < NR_EVENT_CHANNELS; i++) |
| 1334 | evtchn_to_irq[i] = -1; |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1335 | |
| 1336 | init_evtchn_cpu_bindings(); |
| 1337 | |
| 1338 | /* No event channels are 'live' right now. */ |
| 1339 | for (i = 0; i < NR_EVENT_CHANNELS; i++) |
| 1340 | mask_evtchn(i); |
| 1341 | |
Sheng Yang | 38e20b0 | 2010-05-14 12:40:51 +0100 | [diff] [blame] | 1342 | if (xen_hvm_domain()) { |
| 1343 | xen_callback_vector(); |
| 1344 | native_init_IRQ(); |
| 1345 | } else { |
| 1346 | irq_ctx_init(smp_processor_id()); |
| 1347 | } |
Jeremy Fitzhardinge | e46cdb6 | 2007-07-17 18:37:05 -0700 | [diff] [blame] | 1348 | } |