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