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