blob: 359e983d97e40fe5c21cce45c96f7d021e27aaf7 [file] [log] [blame]
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001/*
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 Marchi25985ed2011-03-30 22:57:33 -03008 * chip. When an event is received, it is mapped to an irq and sent
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07009 * 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 Fitzhardinged46a78b2010-10-01 12:20:09 -040019 * 4. PIRQs - Hardware interrupts.
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070020 *
21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
22 */
23
Joe Perches283c0972013-06-28 03:21:41 -070024#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
25
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070026#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 Saout28e08862009-01-11 11:46:23 -080031#include <linux/bootmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -040033#include <linux/irqnr.h>
Qing Hef731e3ef2010-10-11 15:30:09 +010034#include <linux/pci.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070035
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000036#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +010037#include <asm/desc.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070038#include <asm/ptrace.h>
39#include <asm/irq.h>
Jeremy Fitzhardinge792dc4f2009-02-06 14:09:43 -080040#include <asm/idle.h>
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -040041#include <asm/io_apic.h>
Stefano Stabellini9846ff12012-01-30 16:21:48 +000042#include <asm/xen/page.h>
Stefano Stabellini42a1de52010-06-24 16:42:04 +010043#include <asm/xen/pci.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000044#endif
45#include <asm/sync_bitops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070046#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070047#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070048
Sheng Yang38e20b02010-05-14 12:40:51 +010049#include <xen/xen.h>
50#include <xen/hvm.h>
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070051#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070052#include <xen/events.h>
53#include <xen/interface/xen.h>
54#include <xen/interface/event_channel.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010055#include <xen/interface/hvm/hvm_op.h>
56#include <xen/interface/hvm/params.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000057#include <xen/interface/physdev.h>
58#include <xen/interface/sched.h>
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -040059#include <xen/interface/vcpu.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000060#include <asm/hw_irq.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070061
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070062/*
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 Wilk77365942011-09-14 05:10:00 -040066static DEFINE_MUTEX(irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070067
Ian Campbell6cb65372011-03-10 16:08:11 +000068static LIST_HEAD(xen_irq_list_head);
69
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070070/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090071static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070072
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070073/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090074static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070075
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080076/* Interrupt types. */
77enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080078 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070079 IRQT_PIRQ,
80 IRQT_VIRQ,
81 IRQT_IPI,
82 IRQT_EVTCHN
83};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070084
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080085/*
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 Beulichdec02de2013-04-03 15:52:50 +010091 * PIRQ - physical IRQ, GSI, flags, and owner domain
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080092 * VIRQ - virq number
93 * IPI - IPI vector
94 * EVTCHN -
95 */
Ruslan Pisarev088c05a2011-07-26 14:16:13 +030096struct irq_info {
Ian Campbell6cb65372011-03-10 16:08:11 +000097 struct list_head list;
Daniel De Graaf420eb552011-10-27 17:58:47 -040098 int refcnt;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080099 enum xen_irq_type type; /* type */
Ian Campbell6cb65372011-03-10 16:08:11 +0000100 unsigned irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800101 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 Stabellini7a043f12010-07-01 17:08:14 +0100108 unsigned short pirq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800109 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400110 unsigned char flags;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400111 uint16_t domid;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800112 } pirq;
113 } u;
114};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400115#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400116#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800117
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400118static int *evtchn_to_irq;
Ian Campbellbf86ad82012-10-17 09:39:12 +0100119#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000120static unsigned long *pirq_eoi_map;
Ian Campbellbf86ad82012-10-17 09:39:12 +0100121#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000122static bool (*pirq_needs_eoi)(unsigned irq);
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700123
Ian Campbellc81611c2013-02-20 11:48:06 +0000124/*
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
139static DEFINE_PER_CPU(xen_ulong_t [NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD],
Ian Campbellcb60d112011-03-10 16:08:08 +0000140 cpu_evtchn_mask);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700141
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700142/* Xen will never allocate port zero for any purpose. */
143#define VALID_EVTCHN(chn) ((chn) != 0)
144
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700145static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700146static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400147static struct irq_chip xen_pirq_chip;
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100148static void enable_dynirq(struct irq_data *data);
149static void disable_dynirq(struct irq_data *data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700150
Ian Campbell9158c352011-03-10 16:08:09 +0000151/* Get info for IRQ */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800152static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700153{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100154 return irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700155}
156
Ian Campbell9158c352011-03-10 16:08:09 +0000157/* Constructors for packed IRQ information. */
158static void xen_irq_info_common_init(struct irq_info *info,
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000159 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000160 enum xen_irq_type type,
161 unsigned short evtchn,
162 unsigned short cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700163{
Ian Campbell9158c352011-03-10 16:08:09 +0000164
165 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
166
167 info->type = type;
Ian Campbell6cb65372011-03-10 16:08:11 +0000168 info->irq = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000169 info->evtchn = evtchn;
170 info->cpu = cpu;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000171
172 evtchn_to_irq[evtchn] = irq;
Julien Grall934f5852013-04-30 18:29:13 +0100173
174 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700175}
176
Ian Campbell9158c352011-03-10 16:08:09 +0000177static void xen_irq_info_evtchn_init(unsigned irq,
178 unsigned short evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700179{
Ian Campbell9158c352011-03-10 16:08:09 +0000180 struct irq_info *info = info_for_irq(irq);
181
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000182 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700183}
184
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000185static void xen_irq_info_ipi_init(unsigned cpu,
186 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000187 unsigned short evtchn,
188 enum ipi_vector ipi)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700189{
Ian Campbell9158c352011-03-10 16:08:09 +0000190 struct irq_info *info = info_for_irq(irq);
191
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000192 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000193
194 info->u.ipi = ipi;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000195
196 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700197}
198
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000199static void xen_irq_info_virq_init(unsigned cpu,
200 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000201 unsigned short evtchn,
202 unsigned short virq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700203{
Ian Campbell9158c352011-03-10 16:08:09 +0000204 struct irq_info *info = info_for_irq(irq);
205
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000206 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000207
208 info->u.virq = virq;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000209
210 per_cpu(virq_to_irq, cpu)[virq] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000211}
212
213static void xen_irq_info_pirq_init(unsigned irq,
214 unsigned short evtchn,
215 unsigned short pirq,
216 unsigned short gsi,
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400217 uint16_t domid,
Ian Campbell9158c352011-03-10 16:08:09 +0000218 unsigned char flags)
219{
220 struct irq_info *info = info_for_irq(irq);
221
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000222 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000223
224 info->u.pirq.pirq = pirq;
225 info->u.pirq.gsi = gsi;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400226 info->u.pirq.domid = domid;
Ian Campbell9158c352011-03-10 16:08:09 +0000227 info->u.pirq.flags = flags;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700228}
229
230/*
231 * Accessors for packed IRQ information.
232 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800233static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700234{
Joe Jin110e7c72011-01-07 14:50:12 +0800235 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
236 return 0;
237
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800238 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700239}
240
Ian Campbelld4c04532009-02-06 19:20:31 -0800241unsigned irq_from_evtchn(unsigned int evtchn)
242{
243 return evtchn_to_irq[evtchn];
244}
245EXPORT_SYMBOL_GPL(irq_from_evtchn);
246
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800247static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700248{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800249 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
257static 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 Stabellini7a043f12010-07-01 17:08:14 +0100267static 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 Fitzhardingeced40d02009-02-06 14:09:44 -0800277static enum xen_irq_type type_from_irq(unsigned irq)
278{
279 return info_for_irq(irq)->type;
280}
281
282static unsigned cpu_from_irq(unsigned irq)
283{
284 return info_for_irq(irq)->cpu;
285}
286
287static 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 Fitzhardingee46cdb62007-07-17 18:37:05 -0700296}
297
Ian Campbellbf86ad82012-10-17 09:39:12 +0100298#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000299static bool pirq_check_eoi_map(unsigned irq)
300{
Stefano Stabellini521394e2012-04-25 16:11:38 +0100301 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000302}
Ian Campbellbf86ad82012-10-17 09:39:12 +0100303#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000304
305static bool pirq_needs_eoi_flag(unsigned irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400306{
307 struct irq_info *info = info_for_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400308 BUG_ON(info->type != IRQT_PIRQ);
309
310 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
311}
312
Ian Campbellc81611c2013-02-20 11:48:06 +0000313static inline xen_ulong_t active_evtchns(unsigned int cpu,
314 struct shared_info *sh,
315 unsigned int idx)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700316{
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300317 return sh->evtchn_pending[idx] &
Ian Campbellcb60d112011-03-10 16:08:08 +0000318 per_cpu(cpu_evtchn_mask, cpu)[idx] &
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300319 ~sh->evtchn_mask[idx];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700320}
321
322static 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 Gleixnerc9e265e2011-02-05 20:08:54 +0000328 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700329#endif
330
Ian Campbellc81611c2013-02-20 11:48:06 +0000331 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 Fitzhardingee46cdb62007-07-17 18:37:05 -0700333
Ian Campbellca62ce82011-03-10 16:08:12 +0000334 info_for_irq(irq)->cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700335}
336
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700337static inline void clear_evtchn(int port)
338{
339 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000340 sync_clear_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700341}
342
343static inline void set_evtchn(int port)
344{
345 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000346 sync_set_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700347}
348
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700349static inline int test_evtchn(int port)
350{
351 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000352 return sync_test_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700353}
354
Wei Liu3f70fa82013-03-07 15:50:27 +0000355static 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 Fitzhardingee46cdb62007-07-17 18:37:05 -0700361
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 */
370void 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}
377EXPORT_SYMBOL_GPL(notify_remote_via_irq);
378
379static void mask_evtchn(int port)
380{
381 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000382 sync_set_bit(port, BM(&s->evtchn_mask[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700383}
384
385static void unmask_evtchn(int port)
386{
387 struct shared_info *s = HYPERVISOR_shared_info;
388 unsigned int cpu = get_cpu();
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100389 int do_hypercall = 0, evtchn_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700390
391 BUG_ON(!irqs_disabled());
392
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100393 if (unlikely((cpu != cpu_from_evtchn(port))))
394 do_hypercall = 1;
David Vrabelc26377e2013-03-25 14:11:19 +0000395 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 Campbellc81611c2013-02-20 11:48:06 +0000405 evtchn_pending = sync_test_bit(port, BM(&s->evtchn_pending[0]));
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100406
David Vrabelc26377e2013-03-25 14:11:19 +0000407 if (unlikely(evtchn_pending && xen_hvm_domain())) {
408 sync_set_bit(port, BM(&s->evtchn_mask[0]));
409 do_hypercall = 1;
410 }
411 }
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100412
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 Fitzhardingee46cdb62007-07-17 18:37:05 -0700417 struct evtchn_unmask unmask = { .port = port };
418 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
419 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600420 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700421
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700422 /*
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 Stabellinib5e57922012-08-22 17:20:11 +0100427 if (evtchn_pending &&
Ian Campbellc81611c2013-02-20 11:48:06 +0000428 !sync_test_and_set_bit(port / BITS_PER_EVTCHN_WORD,
429 BM(&vcpu_info->evtchn_pending_sel)))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700430 vcpu_info->evtchn_upcall_pending = 1;
431 }
432
433 put_cpu();
434}
435
Ian Campbell6cb65372011-03-10 16:08:11 +0000436static void xen_irq_init(unsigned irq)
437{
438 struct irq_info *info;
Konrad Rzeszutek Wilkb5328cd2011-06-15 14:24:29 -0400439#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000440 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 Wilk44626e42011-03-15 16:40:33 -0400444#endif
Ian Campbell6cb65372011-03-10 16:08:11 +0000445
Ian Campbellca62ce82011-03-10 16:08:12 +0000446 info = kzalloc(sizeof(*info), GFP_KERNEL);
447 if (info == NULL)
448 panic("Unable to allocate metadata for IRQ%d\n", irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000449
450 info->type = IRQT_UNBOUND;
Daniel De Graaf420eb552011-10-27 17:58:47 -0400451 info->refcnt = -1;
Ian Campbell6cb65372011-03-10 16:08:11 +0000452
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100453 irq_set_handler_data(irq, info);
Ian Campbellca62ce82011-03-10 16:08:12 +0000454
Ian Campbell6cb65372011-03-10 16:08:11 +0000455 list_add_tail(&info->list, &xen_irq_list_head);
456}
457
Ian Campbell7bee9762011-03-10 16:08:15 +0000458static int __must_check xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700459{
Ian Campbell89911502011-03-03 11:57:44 -0500460 int first = 0;
461 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700462
Ian Campbell89911502011-03-03 11:57:44 -0500463#ifdef CONFIG_X86_IO_APIC
464 /*
465 * For an HVM guest or domain 0 which see "real" (emulated or
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300466 * actual respectively) GSIs we allocate dynamic IRQs
Ian Campbell89911502011-03-03 11:57:44 -0500467 * e.g. those corresponding to event channels or MSIs
468 * etc. from the range above those "real" GSIs to avoid
469 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500470 */
Ian Campbell89911502011-03-03 11:57:44 -0500471 if (xen_initial_domain() || xen_hvm_domain())
472 first = get_nr_irqs_gsi();
473#endif
474
Ian Campbell89911502011-03-03 11:57:44 -0500475 irq = irq_alloc_desc_from(first, -1);
476
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400477 if (irq >= 0)
478 xen_irq_init(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800479
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700480 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400481}
482
Ian Campbell7bee9762011-03-10 16:08:15 +0000483static int __must_check xen_allocate_irq_gsi(unsigned gsi)
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000484{
485 int irq;
486
Ian Campbell89911502011-03-03 11:57:44 -0500487 /*
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 Campbellc9df1ce2011-01-11 17:20:15 +0000494 return xen_allocate_irq_dynamic();
495
496 /* Legacy IRQ descriptors are already allocated by the arch. */
497 if (gsi < NR_IRQS_LEGACY)
Ian Campbell6cb65372011-03-10 16:08:11 +0000498 irq = gsi;
499 else
500 irq = irq_alloc_desc_at(gsi, -1);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000501
Ian Campbell6cb65372011-03-10 16:08:11 +0000502 xen_irq_init(irq);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000503
504 return irq;
505}
506
507static void xen_free_irq(unsigned irq)
508{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100509 struct irq_info *info = irq_get_handler_data(irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000510
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -0400511 if (WARN_ON(!info))
512 return;
513
Ian Campbell6cb65372011-03-10 16:08:11 +0000514 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000515
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100516 irq_set_handler_data(irq, NULL);
Ian Campbellca62ce82011-03-10 16:08:12 +0000517
Daniel De Graaf420eb552011-10-27 17:58:47 -0400518 WARN_ON(info->refcnt > 0);
519
Ian Campbellca62ce82011-03-10 16:08:12 +0000520 kfree(info);
521
Ian Campbell72146102011-02-03 09:49:35 +0000522 /* Legacy IRQ descriptors are managed by the arch. */
523 if (irq < NR_IRQS_LEGACY)
524 return;
525
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000526 irq_free_desc(irq);
527}
528
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400529static 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 Stabellini7a043f12010-07-01 17:08:14 +0100536 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400537 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
545static 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 Stabellini7e186bd2011-05-06 12:27:50 +0100552static 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
569static void mask_ack_pirq(struct irq_data *data)
570{
571 disable_dynirq(data);
572 eoi_pirq(data);
573}
574
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000575static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400576{
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 Wilk15ebbb82010-10-04 13:43:27 -0400580 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400581
582 BUG_ON(info->type != IRQT_PIRQ);
583
584 if (VALID_EVTCHN(evtchn))
585 goto out;
586
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100587 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400588 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400589 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 Fitzhardinged46a78b2010-10-01 12:20:09 -0400593 if (!probing_irq(irq))
Joe Perches283c0972013-06-28 03:21:41 -0700594 pr_info("Failed to obtain physical IRQ %d\n", irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400595 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
605out:
606 unmask_evtchn(evtchn);
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100607 eoi_pirq(irq_get_irq_data(irq));
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400608
609 return 0;
610}
611
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000612static unsigned int startup_pirq(struct irq_data *data)
613{
614 return __startup_pirq(data->irq);
615}
616
617static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400618{
619 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000620 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400621 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 Gleixnerc9e265e2011-02-05 20:08:54 +0000640static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400641{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000642 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400643}
644
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000645static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400646{
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100647 disable_dynirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400648}
649
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100650int xen_irq_from_gsi(unsigned gsi)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400651{
Ian Campbell6cb65372011-03-10 16:08:11 +0000652 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400653
Ian Campbell6cb65372011-03-10 16:08:11 +0000654 list_for_each_entry(info, &xen_irq_list_head, list) {
655 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400656 continue;
657
Ian Campbell6cb65372011-03-10 16:08:11 +0000658 if (info->u.pirq.gsi == gsi)
659 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400660 }
661
662 return -1;
663}
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100664EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400665
Ian Campbell653378a2011-03-10 16:08:04 +0000666/*
667 * Do not make any assumptions regarding the relationship between the
668 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100669 *
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 Stabellinie5ac0bd2011-05-25 12:33:23 +0100672 *
673 * Shareable implies level triggered, not shareable implies edge
674 * triggered here.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400675 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000676int xen_bind_pirq_gsi_to_irq(unsigned gsi,
677 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400678{
Ian Campbella0e18112011-03-10 16:08:03 +0000679 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400680 struct physdev_irq irq_op;
681
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400682 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400683
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100684 irq = xen_irq_from_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400685 if (irq != -1) {
Joe Perches283c0972013-06-28 03:21:41 -0700686 pr_info("%s: returning irq %d for gsi %u\n",
687 __func__, irq, gsi);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400688 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400689 }
690
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000691 irq = xen_allocate_irq_gsi(gsi);
Ian Campbell7bee9762011-03-10 16:08:15 +0000692 if (irq < 0)
693 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400694
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400695 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400696 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 Campbellc9df1ce2011-01-11 17:20:15 +0000703 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400704 irq = -ENOSPC;
705 goto out;
706 }
707
Jan Beulichdec02de2013-04-03 15:52:50 +0100708 xen_irq_info_pirq_init(irq, 0, pirq, gsi, DOMID_SELF,
Ian Campbell9158c352011-03-10 16:08:09 +0000709 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400710
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100711 pirq_query_unmask(irq);
712 /* We try to use the handler with the appropriate semantic for the
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100713 * type of interrupt: if the interrupt is an edge triggered
714 * interrupt we use handle_edge_irq.
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100715 *
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100716 * 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 Stabellini7e186bd2011-05-06 12:27:50 +0100718 * interrupts.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100719 *
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100720 * 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 Stabellinie5ac0bd2011-05-25 12:33:23 +0100727 if (shareable)
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100728 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 Fitzhardinged46a78b2010-10-01 12:20:09 -0400734out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400735 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400736
737 return irq;
738}
739
Qing Hef731e3ef2010-10-11 15:30:09 +0100740#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000741int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000742{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000743 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000744 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000745
Ian Campbellbf480d92011-02-18 16:43:32 +0000746 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000747 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000748
Ian Campbell5cad61a2011-02-18 16:43:31 +0000749 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 Campbellcbf6aa82011-01-11 17:20:14 +0000753}
754
Ian Campbellbf480d92011-02-18 16:43:32 +0000755int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Jan Beulichdec02de2013-04-03 15:52:50 +0100756 int pirq, const char *name, domid_t domid)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100757{
Ian Campbellbf480d92011-02-18 16:43:32 +0000758 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000759
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400760 mutex_lock(&irq_mapping_update_lock);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100761
Ian Campbell4b41df72011-02-18 16:43:29 +0000762 irq = xen_allocate_irq_dynamic();
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400763 if (irq < 0)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000764 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100765
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100766 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
767 name);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100768
Jan Beulichdec02de2013-04-03 15:52:50 +0100769 xen_irq_info_pirq_init(irq, 0, pirq, 0, domid, 0);
Linus Torvalds5f6fb452011-03-15 19:23:40 -0700770 ret = irq_set_msi_desc(irq, msidesc);
Ian Campbellbf480d92011-02-18 16:43:32 +0000771 if (ret < 0)
772 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100773out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400774 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000775 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000776error_irq:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400777 mutex_unlock(&irq_mapping_update_lock);
Ian Campbellbf480d92011-02-18 16:43:32 +0000778 xen_free_irq(irq);
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400779 return ret;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100780}
Qing Hef731e3ef2010-10-11 15:30:09 +0100781#endif
782
Alex Nixonb5401a92010-03-18 16:31:34 -0400783int xen_destroy_irq(int irq)
784{
785 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100786 struct physdev_unmap_pirq unmap_irq;
787 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400788 int rc = -ENOENT;
789
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400790 mutex_lock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400791
792 desc = irq_to_desc(irq);
793 if (!desc)
794 goto out;
795
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100796 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500797 unmap_irq.pirq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400798 unmap_irq.domid = info->u.pirq.domid;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100799 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500800 /* 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 Perches283c0972013-06-28 03:21:41 -0700805 pr_info("domain %d does not have %d anymore\n",
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500806 info->u.pirq.domid, info->u.pirq.pirq);
807 else if (rc) {
Joe Perches283c0972013-06-28 03:21:41 -0700808 pr_warn("unmap irq failed %d\n", rc);
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100809 goto out;
810 }
811 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400812
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000813 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400814
815out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400816 mutex_unlock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400817 return rc;
818}
819
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000820int xen_irq_from_pirq(unsigned pirq)
821{
Ian Campbell69c358c2011-03-10 16:08:13 +0000822 int irq;
823
824 struct irq_info *info;
825
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400826 mutex_lock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000827
828 list_for_each_entry(info, &xen_irq_list_head, list) {
Konrad Rzeszutek Wilk9bb9efe2011-09-29 13:13:30 -0400829 if (info->type != IRQT_PIRQ)
Ian Campbell69c358c2011-03-10 16:08:13 +0000830 continue;
831 irq = info->irq;
832 if (info->u.pirq.pirq == pirq)
833 goto out;
834 }
835 irq = -1;
836out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400837 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000838
839 return irq;
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000840}
841
Konrad Rzeszutek Wilke6197ac2011-02-24 14:20:12 -0500842
843int xen_pirq_from_irq(unsigned irq)
844{
845 return pirq_from_irq(irq);
846}
847EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700848int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700849{
850 int irq;
851
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400852 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700853
854 irq = evtchn_to_irq[evtchn];
855
856 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000857 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000858 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000859 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700860
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100861 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100862 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700863
Ian Campbell9158c352011-03-10 16:08:09 +0000864 xen_irq_info_evtchn_init(irq, evtchn);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400865 } else {
866 struct irq_info *info = info_for_irq(irq);
867 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700868 }
869
Ian Campbell7bee9762011-03-10 16:08:15 +0000870out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400871 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700872
873 return irq;
874}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700875EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700876
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700877static 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 Wilk77365942011-09-14 05:10:00 -0400882 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700883
884 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800885
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700886 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000887 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700888 if (irq < 0)
889 goto out;
890
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100891 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700892 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700893
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 Campbell3d4cfa32011-03-10 16:08:10 +0000900 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700901
902 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400903 } else {
904 struct irq_info *info = info_for_irq(irq);
905 WARN_ON(info == NULL || info->type != IRQT_IPI);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700906 }
907
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700908 out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400909 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700910 return irq;
911}
912
Ian Campbell2e820f52009-02-09 12:05:50 -0800913static 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 Hering62cc5fc2011-08-25 18:30:48 +0200928static 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 Fitzhardingef87e4ca2007-07-17 18:37:06 -0700949
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100950int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700951{
952 struct evtchn_bind_virq bind_virq;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200953 int evtchn, irq, ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700954
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400955 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700956
957 irq = per_cpu(virq_to_irq, cpu)[virq];
958
959 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000960 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000961 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000962 goto out;
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700963
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100964 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700965 handle_percpu_irq, "virq");
966
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700967 bind_virq.virq = virq;
968 bind_virq.vcpu = cpu;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200969 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 Fitzhardingee46cdb62007-07-17 18:37:05 -0700979
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000980 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700981
982 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400983 } else {
984 struct irq_info *info = info_for_irq(irq);
985 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700986 }
987
Ian Campbell7bee9762011-03-10 16:08:15 +0000988out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400989 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700990
991 return irq;
992}
993
994static void unbind_from_irq(unsigned int irq)
995{
996 struct evtchn_close close;
997 int evtchn = evtchn_from_irq(irq);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400998 struct irq_info *info = irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700999
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001000 if (WARN_ON(!info))
1001 return;
1002
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001003 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001004
Daniel De Graaf420eb552011-10-27 17:58:47 -04001005 if (info->refcnt > 0) {
1006 info->refcnt--;
1007 if (info->refcnt != 0)
1008 goto done;
1009 }
1010
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001011 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001012 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 Fitzhardingeced40d02009-02-06 14:09:44 -08001019 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001020 break;
Alex Nixond68d82a2008-08-22 11:52:15 +01001021 case IRQT_IPI:
1022 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001023 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +01001024 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001025 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 Campbellfed5ea82009-12-01 16:15:30 +00001033 }
1034
Ian Campbellca62ce82011-03-10 16:08:12 +00001035 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001036
Ian Campbell9158c352011-03-10 16:08:09 +00001037 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001038
Daniel De Graaf420eb552011-10-27 17:58:47 -04001039 done:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001040 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001041}
1042
1043int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -04001044 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001045 unsigned long irqflags,
1046 const char *devname, void *dev_id)
1047{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001048 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001049
1050 irq = bind_evtchn_to_irq(evtchn);
Ian Campbell7bee9762011-03-10 16:08:15 +00001051 if (irq < 0)
1052 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001053 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}
1061EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1062
Ian Campbell2e820f52009-02-09 12:05:50 -08001063int 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}
1084EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1085
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001086int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -04001087 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001088 unsigned long irqflags, const char *devname, void *dev_id)
1089{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001090 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001091
1092 irq = bind_virq_to_irq(virq, cpu);
Ian Campbell7bee9762011-03-10 16:08:15 +00001093 if (irq < 0)
1094 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001095 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}
1103EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1104
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001105int 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 Campbell9bab0b72011-10-03 15:37:00 +01001118 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001119 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 Fitzhardingee46cdb62007-07-17 18:37:05 -07001128void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1129{
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001130 struct irq_info *info = irq_get_handler_data(irq);
1131
1132 if (WARN_ON(!info))
1133 return;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001134 free_irq(irq, dev_id);
1135 unbind_from_irq(irq);
1136}
1137EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1138
Daniel De Graaf420eb552011-10-27 17:58:47 -04001139int 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}
1158EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1159
1160int evtchn_get(unsigned int evtchn)
1161{
1162 int irq;
1163 struct irq_info *info;
1164 int err = -ENOENT;
1165
Daniel De Graafc3b3f162011-11-28 11:49:09 -05001166 if (evtchn >= NR_EVENT_CHANNELS)
1167 return -EINVAL;
1168
Daniel De Graaf420eb552011-10-27 17:58:47 -04001169 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}
1191EXPORT_SYMBOL_GPL(evtchn_get);
1192
1193void 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}
1200EXPORT_SYMBOL_GPL(evtchn_put);
1201
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001202void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1203{
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -04001204 int irq;
1205
Stefano Stabellini072b2062013-08-13 16:57:06 +00001206#ifdef CONFIG_X86
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -04001207 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 Stabellini072b2062013-08-13 16:57:06 +00001213#endif
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -04001214 irq = per_cpu(ipi_to_irq, cpu)[vector];
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001215 BUG_ON(irq < 0);
1216 notify_remote_via_irq(irq);
1217}
1218
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001219irqreturn_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 Campbellc81611c2013-02-20 11:48:06 +00001223 xen_ulong_t *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001224 int i;
1225 unsigned long flags;
1226 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001227 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001228
1229 spin_lock_irqsave(&debug_lock, flags);
1230
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001231 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001232
1233 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001234 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 Campbellc81611c2013-02-20 11:48:06 +00001239 printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong"\n ", i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001240 pending, v->evtchn_upcall_pending,
1241 (int)(sizeof(v->evtchn_pending_sel)*2),
1242 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001243 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001244 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001245
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001246 printk("\npending:\n ");
1247 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001248 printk("%0*"PRI_xen_ulong"%s",
1249 (int)sizeof(sh->evtchn_pending[0])*2,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001250 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 Campbellc81611c2013-02-20 11:48:06 +00001254 printk("%0*"PRI_xen_ulong"%s",
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001255 (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 Campbellc81611c2013-02-20 11:48:06 +00001261 printk("%0*"PRI_xen_ulong"%s",
1262 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001263 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1264 i % 8 == 0 ? "\n " : " ");
1265
1266 printk("\nlocal cpu%d mask:\n ", cpu);
Ian Campbellc81611c2013-02-20 11:48:06 +00001267 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 Campbellcb52e6d2010-10-15 11:52:46 +01001269 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 Campbellc81611c2013-02-20 11:48:06 +00001274 xen_ulong_t pending = sh->evtchn_pending[i]
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001275 & ~sh->evtchn_mask[i]
1276 & cpu_evtchn[i];
Ian Campbellc81611c2013-02-20 11:48:06 +00001277 printk("%0*"PRI_xen_ulong"%s",
1278 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001279 pending, i % 8 == 0 ? "\n " : " ");
1280 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001281
1282 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001283 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001284 if (sync_test_bit(i, BM(sh->evtchn_pending))) {
1285 int word_idx = i / BITS_PER_EVTCHN_WORD;
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001286 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001287 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001288 evtchn_to_irq[i],
Ian Campbellc81611c2013-02-20 11:48:06 +00001289 sync_test_bit(word_idx, BM(&v->evtchn_pending_sel))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001290 ? "" : " l2-clear",
Ian Campbellc81611c2013-02-20 11:48:06 +00001291 !sync_test_bit(i, BM(sh->evtchn_mask))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001292 ? "" : " globally-masked",
Ian Campbellc81611c2013-02-20 11:48:06 +00001293 sync_test_bit(i, BM(cpu_evtchn))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001294 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001295 }
1296 }
1297
1298 spin_unlock_irqrestore(&debug_lock, flags);
1299
1300 return IRQ_HANDLED;
1301}
1302
Tejun Heo245b2e72009-06-24 15:13:48 +09001303static DEFINE_PER_CPU(unsigned, xed_nesting_count);
Keir Fraserada68142011-03-03 10:01:11 +00001304static DEFINE_PER_CPU(unsigned int, current_word_idx);
1305static DEFINE_PER_CPU(unsigned int, current_bit_idx);
Tejun Heo245b2e72009-06-24 15:13:48 +09001306
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001307/*
Scott Rixnerab7f8632011-03-03 09:30:08 +00001308 * Mask out the i least significant bits of w
1309 */
Ian Campbellc81611c2013-02-20 11:48:06 +00001310#define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001311
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 Yang38e20b02010-05-14 12:40:51 +01001321static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001322{
Keir Fraser24b51c22011-03-03 11:06:28 +00001323 int start_word_idx, start_bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001324 int word_idx, bit_idx;
Keir Fraserbee980d2013-03-28 10:03:36 -04001325 int i, irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001326 int cpu = get_cpu();
1327 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001328 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Ruslan Pisarev088c05a2011-07-26 14:16:13 +03001329 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001330
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001331 do {
Ian Campbellc81611c2013-02-20 11:48:06 +00001332 xen_ulong_t pending_words;
Keir Fraserbee980d2013-03-28 10:03:36 -04001333 xen_ulong_t pending_bits;
1334 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001335
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001336 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001337
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001338 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001339 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001340
Ian Campbellc81611c2013-02-20 11:48:06 +00001341 /*
1342 * Master flag must be cleared /before/ clearing
1343 * selector flag. xchg_xen_ulong must contain an
1344 * appropriate barrier.
1345 */
Keir Fraserbee980d2013-03-28 10:03:36 -04001346 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 Campbellc81611c2013-02-20 11:48:06 +00001357 pending_words = xchg_xen_ulong(&vcpu_info->evtchn_pending_sel, 0);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001358
Keir Fraser24b51c22011-03-03 11:06:28 +00001359 start_word_idx = __this_cpu_read(current_word_idx);
1360 start_bit_idx = __this_cpu_read(current_bit_idx);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001361
Keir Fraser24b51c22011-03-03 11:06:28 +00001362 word_idx = start_word_idx;
1363
1364 for (i = 0; pending_words != 0; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001365 xen_ulong_t words;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001366
Scott Rixnerab7f8632011-03-03 09:30:08 +00001367 words = MASK_LSBS(pending_words, word_idx);
1368
1369 /*
Keir Fraserada68142011-03-03 10:01:11 +00001370 * If we masked out all events, wrap to beginning.
Scott Rixnerab7f8632011-03-03 09:30:08 +00001371 */
1372 if (words == 0) {
Keir Fraserada68142011-03-03 10:01:11 +00001373 word_idx = 0;
1374 bit_idx = 0;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001375 continue;
1376 }
Ian Campbellc81611c2013-02-20 11:48:06 +00001377 word_idx = EVTCHN_FIRST_BIT(words);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001378
Keir Fraser24b51c22011-03-03 11:06:28 +00001379 pending_bits = active_evtchns(cpu, s, word_idx);
1380 bit_idx = 0; /* usually scan entire word from start */
David Vrabel3ef02962013-08-15 13:21:05 +01001381 /*
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 Fraser24b51c22011-03-03 11:06:28 +00001393 if (word_idx == start_word_idx) {
Keir Fraser24b51c22011-03-03 11:06:28 +00001394 if (i == 0)
Keir Fraser24b51c22011-03-03 11:06:28 +00001395 bit_idx = start_bit_idx;
Keir Fraser24b51c22011-03-03 11:06:28 +00001396 }
1397
Scott Rixnerab7f8632011-03-03 09:30:08 +00001398 do {
Ian Campbellc81611c2013-02-20 11:48:06 +00001399 xen_ulong_t bits;
Keir Fraserbee980d2013-03-28 10:03:36 -04001400 int port;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001401
Scott Rixnerab7f8632011-03-03 09:30:08 +00001402 bits = MASK_LSBS(pending_bits, bit_idx);
1403
1404 /* If we masked out all events, move on. */
Keir Fraserada68142011-03-03 10:01:11 +00001405 if (bits == 0)
Scott Rixnerab7f8632011-03-03 09:30:08 +00001406 break;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001407
Ian Campbellc81611c2013-02-20 11:48:06 +00001408 bit_idx = EVTCHN_FIRST_BIT(bits);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001409
1410 /* Process port. */
Ian Campbellc81611c2013-02-20 11:48:06 +00001411 port = (word_idx * BITS_PER_EVTCHN_WORD) + bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001412 irq = evtchn_to_irq[port];
1413
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001414 if (irq != -1) {
1415 desc = irq_to_desc(irq);
1416 if (desc)
1417 generic_handle_irq_desc(irq, desc);
1418 }
Scott Rixnerab7f8632011-03-03 09:30:08 +00001419
Ian Campbellc81611c2013-02-20 11:48:06 +00001420 bit_idx = (bit_idx + 1) % BITS_PER_EVTCHN_WORD;
Keir Fraserada68142011-03-03 10:01:11 +00001421
1422 /* Next caller starts at last processed + 1 */
1423 __this_cpu_write(current_word_idx,
1424 bit_idx ? word_idx :
Ian Campbellc81611c2013-02-20 11:48:06 +00001425 (word_idx+1) % BITS_PER_EVTCHN_WORD);
Keir Fraserada68142011-03-03 10:01:11 +00001426 __this_cpu_write(current_bit_idx, bit_idx);
1427 } while (bit_idx != 0);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001428
Keir Fraser24b51c22011-03-03 11:06:28 +00001429 /* Scan start_l1i twice; all others once. */
1430 if ((word_idx != start_word_idx) || (i != 0))
Scott Rixnerab7f8632011-03-03 09:30:08 +00001431 pending_words &= ~(1UL << word_idx);
Keir Fraserada68142011-03-03 10:01:11 +00001432
Ian Campbellc81611c2013-02-20 11:48:06 +00001433 word_idx = (word_idx + 1) % BITS_PER_EVTCHN_WORD;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001434 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001435
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001436 BUG_ON(!irqs_disabled());
1437
Christoph Lameter780f36d2010-12-06 11:16:29 -06001438 count = __this_cpu_read(xed_nesting_count);
1439 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001440 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001441
1442out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001443
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001444 put_cpu();
1445}
1446
Sheng Yang38e20b02010-05-14 12:40:51 +01001447void xen_evtchn_do_upcall(struct pt_regs *regs)
1448{
1449 struct pt_regs *old_regs = set_irq_regs(regs);
1450
Mojiong Qiu772aebc2012-11-06 16:08:15 +08001451 irq_enter();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001452#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001453 exit_idle();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001454#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001455
1456 __xen_evtchn_do_upcall();
1457
1458 irq_exit();
1459 set_irq_regs(old_regs);
1460}
1461
1462void xen_hvm_evtchn_do_upcall(void)
1463{
1464 __xen_evtchn_do_upcall();
1465}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001466EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001467
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001468/* Rebind a new event channel to an existing irq. */
1469void rebind_evtchn_irq(int evtchn, int irq)
1470{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001471 struct irq_info *info = info_for_irq(irq);
1472
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001473 if (WARN_ON(!info))
1474 return;
1475
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001476 /* Make sure the irq is masked, since the new event channel
1477 will also be masked. */
1478 disable_irq(irq);
1479
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001480 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001481
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 Fitzhardinged77bbd42009-02-06 14:09:45 -08001485 so there should be a proper type */
1486 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001487
Ian Campbell9158c352011-03-10 16:08:09 +00001488 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001489
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001490 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001491
1492 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301493 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001494
1495 /* Unmask the event channel. */
1496 enable_irq(irq);
1497}
1498
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001499/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001500static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001501{
1502 struct evtchn_bind_vcpu bind_vcpu;
1503 int evtchn = evtchn_from_irq(irq);
David Vrabel4704fe42013-08-15 13:21:07 +01001504 int masked;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001505
Ian Campbellbe494722011-03-10 16:08:02 +00001506 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 Lud5dedd42009-04-27 17:59:21 -07001514 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001515
1516 /* Send future instances of this interrupt to other vcpu. */
1517 bind_vcpu.port = evtchn;
1518 bind_vcpu.vcpu = tcpu;
1519
1520 /*
David Vrabel4704fe42013-08-15 13:21:07 +01001521 * Mask the event while changing the VCPU binding to prevent
1522 * it being delivered on an unexpected VCPU.
1523 */
Wei Liu3f70fa82013-03-07 15:50:27 +00001524 masked = test_and_set_mask(evtchn);
David Vrabel4704fe42013-08-15 13:21:07 +01001525
1526 /*
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001527 * 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 Lud5dedd42009-04-27 17:59:21 -07001533
David Vrabel4704fe42013-08-15 13:21:07 +01001534 if (!masked)
1535 unmask_evtchn(evtchn);
1536
Yinghai Lud5dedd42009-04-27 17:59:21 -07001537 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001538}
1539
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001540static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1541 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001542{
Rusty Russell0de26522008-12-13 21:20:26 +10301543 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001544
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001545 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001546}
1547
David Vrabel87295182013-03-12 18:28:04 +00001548static int retrigger_evtchn(int evtchn)
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001549{
David Vrabel87295182013-03-12 18:28:04 +00001550 int masked;
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001551 struct shared_info *s = HYPERVISOR_shared_info;
1552
1553 if (!VALID_EVTCHN(evtchn))
David Vrabel87295182013-03-12 18:28:04 +00001554 return 0;
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001555
Wei Liu3f70fa82013-03-07 15:50:27 +00001556 masked = test_and_set_mask(evtchn);
Ian Campbellc81611c2013-02-20 11:48:06 +00001557 sync_set_bit(evtchn, BM(s->evtchn_pending));
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001558 if (!masked)
1559 unmask_evtchn(evtchn);
1560
1561 return 1;
1562}
1563
David Vrabel87295182013-03-12 18:28:04 +00001564int resend_irq_on_evtchn(unsigned int irq)
1565{
1566 return retrigger_evtchn(evtchn_from_irq(irq));
1567}
1568
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001569static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001570{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001571 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001572
1573 if (VALID_EVTCHN(evtchn))
1574 unmask_evtchn(evtchn);
1575}
1576
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001577static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001578{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001579 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001580
1581 if (VALID_EVTCHN(evtchn))
1582 mask_evtchn(evtchn);
1583}
1584
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001585static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001586{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001587 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001588
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001589 irq_move_irq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001590
1591 if (VALID_EVTCHN(evtchn))
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001592 clear_evtchn(evtchn);
1593}
1594
1595static void mask_ack_dynirq(struct irq_data *data)
1596{
1597 disable_dynirq(data);
1598 ack_dynirq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001599}
1600
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001601static int retrigger_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001602{
David Vrabel87295182013-03-12 18:28:04 +00001603 return retrigger_evtchn(evtchn_from_irq(data->irq));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001604}
1605
Ian Campbell0a852262011-03-10 16:08:06 +00001606static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001607{
1608 int pirq, rc, irq, gsi;
1609 struct physdev_map_pirq map_irq;
Ian Campbell69c358c2011-03-10 16:08:13 +00001610 struct irq_info *info;
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001611
Ian Campbell69c358c2011-03-10 16:08:13 +00001612 list_for_each_entry(info, &xen_irq_list_head, list) {
1613 if (info->type != IRQT_PIRQ)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001614 continue;
1615
Ian Campbell69c358c2011-03-10 16:08:13 +00001616 pirq = info->u.pirq.pirq;
1617 gsi = info->u.pirq.gsi;
1618 irq = info->irq;
1619
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001620 /* save/restore of PT devices doesn't work, so at this point the
1621 * only devices present are GSI based emulated devices */
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001622 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 Perches283c0972013-06-28 03:21:41 -07001632 pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1633 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001634 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001635 continue;
1636 }
1637
1638 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1639
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001640 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001641 }
1642}
1643
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001644static 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 Fitzhardingeced40d02009-02-06 14:09:44 -08001653 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001654
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 Campbell3d4cfa32011-03-10 16:08:10 +00001664 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001665 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001666 }
1667}
1668
1669static 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 Fitzhardingeced40d02009-02-06 14:09:44 -08001678 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001679
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 Campbell3d4cfa32011-03-10 16:08:10 +00001688 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001689 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001690 }
1691}
1692
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001693/* Clear an irq's pending state, in preparation for polling on it */
1694void 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 Wilkd9a88142009-11-05 16:33:09 -05001701EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001702void 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
1710bool 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 Wilkd9a88142009-11-05 16:33:09 -05001721/* 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. */
1723void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001724{
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 Wilkd9a88142009-11-05 16:33:09 -05001731 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001732 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001733
1734 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1735 BUG();
1736 }
1737}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001738EXPORT_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. */
1741void xen_poll_irq(int irq)
1742{
1743 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1744}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001745
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001746/* Check whether the IRQ line is shared with other guests. */
1747int xen_test_irq_shared(int irq)
1748{
1749 struct irq_info *info = info_for_irq(irq);
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001750 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 Wilkc7c2c3a2010-11-08 14:26:36 -05001756
1757 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1758 return 0;
1759 return !(irq_status.flags & XENIRQSTAT_shared);
1760}
1761EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1762
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001763void xen_irq_resume(void)
1764{
Ian Campbell6cb65372011-03-10 16:08:11 +00001765 unsigned int cpu, evtchn;
1766 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001767
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001768 /* 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 Campbell6cb65372011-03-10 16:08:11 +00001773 list_for_each_entry(info, &xen_irq_list_head, list)
1774 info->evtchn = 0; /* zap event-channel binding */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001775
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 Campbell69035912010-11-01 16:30:09 +00001783
Ian Campbell0a852262011-03-10 16:08:06 +00001784 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001785}
1786
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001787static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001788 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001789
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001790 .irq_disable = disable_dynirq,
1791 .irq_mask = disable_dynirq,
1792 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001793
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001794 .irq_ack = ack_dynirq,
1795 .irq_mask_ack = mask_ack_dynirq,
1796
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001797 .irq_set_affinity = set_affinity_irq,
1798 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001799};
1800
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001801static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001802 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001803
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001804 .irq_startup = startup_pirq,
1805 .irq_shutdown = shutdown_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001806 .irq_enable = enable_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001807 .irq_disable = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001808
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001809 .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 Fitzhardinged46a78b2010-10-01 12:20:09 -04001815
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001816 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001817
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001818 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001819};
1820
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001821static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001822 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001823
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001824 .irq_disable = disable_dynirq,
1825 .irq_mask = disable_dynirq,
1826 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001827
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001828 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001829};
1830
Sheng Yang38e20b02010-05-14 12:40:51 +01001831int 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}
1839EXPORT_SYMBOL_GPL(xen_set_callback_via);
1840
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001841#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001842/* 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. */
1845void xen_callback_vector(void)
1846{
1847 int rc;
1848 uint64_t callback_via;
1849 if (xen_have_vector_callback) {
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001850 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
Sheng Yang38e20b02010-05-14 12:40:51 +01001851 rc = xen_set_callback_via(callback_via);
1852 if (rc) {
Joe Perches283c0972013-06-28 03:21:41 -07001853 pr_err("Request for Xen HVM callback vector failed\n");
Sheng Yang38e20b02010-05-14 12:40:51 +01001854 xen_have_vector_callback = 0;
1855 return;
1856 }
Joe Perches283c0972013-06-28 03:21:41 -07001857 pr_info("Xen HVM callback vector for event delivery is enabled\n");
Sheng Yang38e20b02010-05-14 12:40:51 +01001858 /* in the restore case the vector has already been allocated */
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001859 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
1860 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
1861 xen_hvm_callback_vector);
Sheng Yang38e20b02010-05-14 12:40:51 +01001862 }
1863}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001864#else
1865void xen_callback_vector(void) {}
1866#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001867
Stefano Stabellini2e3d8862012-10-02 15:57:57 +01001868void __init xen_init_IRQ(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001869{
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001870 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001871
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001872 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1873 GFP_KERNEL);
Konrad Rzeszutek Wilk9d093e22011-09-29 13:31:21 -04001874 BUG_ON(!evtchn_to_irq);
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001875 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1876 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001877
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001878 /* No event channels are 'live' right now. */
1879 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1880 mask_evtchn(i);
1881
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001882 pirq_needs_eoi = pirq_needs_eoi_flag;
1883
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001884#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001885 if (xen_hvm_domain()) {
1886 xen_callback_vector();
1887 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001888 /* 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 Yang38e20b02010-05-14 12:40:51 +01001891 } else {
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001892 int rc;
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001893 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1894
Sheng Yang38e20b02010-05-14 12:40:51 +01001895 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001896 if (xen_initial_domain())
Konrad Rzeszutek Wilka0ee0562011-06-09 09:49:13 -04001897 pci_xen_initial_domain();
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001898
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 Yang38e20b02010-05-14 12:40:51 +01001907 }
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001908#endif
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001909}