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