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