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