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