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