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