blob: 1e2c74bcd0c8a4fecd0f3cdaa0a8d9a9b3a27db3 [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
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700355
356/**
357 * notify_remote_via_irq - send event to remote end of event channel via irq
358 * @irq: irq of event channel to send event to
359 *
360 * Unlike notify_remote_via_evtchn(), this is safe to use across
361 * save/restore. Notifications on a broken connection are silently
362 * dropped.
363 */
364void notify_remote_via_irq(int irq)
365{
366 int evtchn = evtchn_from_irq(irq);
367
368 if (VALID_EVTCHN(evtchn))
369 notify_remote_via_evtchn(evtchn);
370}
371EXPORT_SYMBOL_GPL(notify_remote_via_irq);
372
373static void mask_evtchn(int port)
374{
375 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000376 sync_set_bit(port, BM(&s->evtchn_mask[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700377}
378
379static void unmask_evtchn(int port)
380{
381 struct shared_info *s = HYPERVISOR_shared_info;
382 unsigned int cpu = get_cpu();
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100383 int do_hypercall = 0, evtchn_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700384
385 BUG_ON(!irqs_disabled());
386
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100387 if (unlikely((cpu != cpu_from_evtchn(port))))
388 do_hypercall = 1;
David Vrabelc26377e2013-03-25 14:11:19 +0000389 else {
390 /*
391 * Need to clear the mask before checking pending to
392 * avoid a race with an event becoming pending.
393 *
394 * EVTCHNOP_unmask will only trigger an upcall if the
395 * mask bit was set, so if a hypercall is needed
396 * remask the event.
397 */
398 sync_clear_bit(port, BM(&s->evtchn_mask[0]));
Ian Campbellc81611c2013-02-20 11:48:06 +0000399 evtchn_pending = sync_test_bit(port, BM(&s->evtchn_pending[0]));
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100400
David Vrabelc26377e2013-03-25 14:11:19 +0000401 if (unlikely(evtchn_pending && xen_hvm_domain())) {
402 sync_set_bit(port, BM(&s->evtchn_mask[0]));
403 do_hypercall = 1;
404 }
405 }
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100406
407 /* Slow path (hypercall) if this is a non-local port or if this is
408 * an hvm domain and an event is pending (hvm domains don't have
409 * their own implementation of irq_enable). */
410 if (do_hypercall) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700411 struct evtchn_unmask unmask = { .port = port };
412 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
413 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600414 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700415
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700416 /*
417 * The following is basically the equivalent of
418 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
419 * the interrupt edge' if the channel is masked.
420 */
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100421 if (evtchn_pending &&
Ian Campbellc81611c2013-02-20 11:48:06 +0000422 !sync_test_and_set_bit(port / BITS_PER_EVTCHN_WORD,
423 BM(&vcpu_info->evtchn_pending_sel)))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700424 vcpu_info->evtchn_upcall_pending = 1;
425 }
426
427 put_cpu();
428}
429
Ian Campbell6cb65372011-03-10 16:08:11 +0000430static void xen_irq_init(unsigned irq)
431{
432 struct irq_info *info;
Konrad Rzeszutek Wilkb5328cd2011-06-15 14:24:29 -0400433#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000434 struct irq_desc *desc = irq_to_desc(irq);
435
436 /* By default all event channels notify CPU#0. */
437 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Konrad Rzeszutek Wilk44626e42011-03-15 16:40:33 -0400438#endif
Ian Campbell6cb65372011-03-10 16:08:11 +0000439
Ian Campbellca62ce82011-03-10 16:08:12 +0000440 info = kzalloc(sizeof(*info), GFP_KERNEL);
441 if (info == NULL)
442 panic("Unable to allocate metadata for IRQ%d\n", irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000443
444 info->type = IRQT_UNBOUND;
Daniel De Graaf420eb552011-10-27 17:58:47 -0400445 info->refcnt = -1;
Ian Campbell6cb65372011-03-10 16:08:11 +0000446
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100447 irq_set_handler_data(irq, info);
Ian Campbellca62ce82011-03-10 16:08:12 +0000448
Ian Campbell6cb65372011-03-10 16:08:11 +0000449 list_add_tail(&info->list, &xen_irq_list_head);
450}
451
Ian Campbell7bee9762011-03-10 16:08:15 +0000452static int __must_check xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700453{
Ian Campbell89911502011-03-03 11:57:44 -0500454 int first = 0;
455 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700456
Ian Campbell89911502011-03-03 11:57:44 -0500457#ifdef CONFIG_X86_IO_APIC
458 /*
459 * For an HVM guest or domain 0 which see "real" (emulated or
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300460 * actual respectively) GSIs we allocate dynamic IRQs
Ian Campbell89911502011-03-03 11:57:44 -0500461 * e.g. those corresponding to event channels or MSIs
462 * etc. from the range above those "real" GSIs to avoid
463 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500464 */
Ian Campbell89911502011-03-03 11:57:44 -0500465 if (xen_initial_domain() || xen_hvm_domain())
466 first = get_nr_irqs_gsi();
467#endif
468
Ian Campbell89911502011-03-03 11:57:44 -0500469 irq = irq_alloc_desc_from(first, -1);
470
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400471 if (irq >= 0)
472 xen_irq_init(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800473
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700474 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400475}
476
Ian Campbell7bee9762011-03-10 16:08:15 +0000477static int __must_check xen_allocate_irq_gsi(unsigned gsi)
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000478{
479 int irq;
480
Ian Campbell89911502011-03-03 11:57:44 -0500481 /*
482 * A PV guest has no concept of a GSI (since it has no ACPI
483 * nor access to/knowledge of the physical APICs). Therefore
484 * all IRQs are dynamically allocated from the entire IRQ
485 * space.
486 */
487 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000488 return xen_allocate_irq_dynamic();
489
490 /* Legacy IRQ descriptors are already allocated by the arch. */
491 if (gsi < NR_IRQS_LEGACY)
Ian Campbell6cb65372011-03-10 16:08:11 +0000492 irq = gsi;
493 else
494 irq = irq_alloc_desc_at(gsi, -1);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000495
Ian Campbell6cb65372011-03-10 16:08:11 +0000496 xen_irq_init(irq);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000497
498 return irq;
499}
500
501static void xen_free_irq(unsigned irq)
502{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100503 struct irq_info *info = irq_get_handler_data(irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000504
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -0400505 if (WARN_ON(!info))
506 return;
507
Ian Campbell6cb65372011-03-10 16:08:11 +0000508 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000509
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100510 irq_set_handler_data(irq, NULL);
Ian Campbellca62ce82011-03-10 16:08:12 +0000511
Daniel De Graaf420eb552011-10-27 17:58:47 -0400512 WARN_ON(info->refcnt > 0);
513
Ian Campbellca62ce82011-03-10 16:08:12 +0000514 kfree(info);
515
Ian Campbell72146102011-02-03 09:49:35 +0000516 /* Legacy IRQ descriptors are managed by the arch. */
517 if (irq < NR_IRQS_LEGACY)
518 return;
519
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000520 irq_free_desc(irq);
521}
522
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400523static void pirq_query_unmask(int irq)
524{
525 struct physdev_irq_status_query irq_status;
526 struct irq_info *info = info_for_irq(irq);
527
528 BUG_ON(info->type != IRQT_PIRQ);
529
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100530 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400531 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
532 irq_status.flags = 0;
533
534 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
535 if (irq_status.flags & XENIRQSTAT_needs_eoi)
536 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
537}
538
539static bool probing_irq(int irq)
540{
541 struct irq_desc *desc = irq_to_desc(irq);
542
543 return desc && desc->action == NULL;
544}
545
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100546static void eoi_pirq(struct irq_data *data)
547{
548 int evtchn = evtchn_from_irq(data->irq);
549 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
550 int rc = 0;
551
552 irq_move_irq(data);
553
554 if (VALID_EVTCHN(evtchn))
555 clear_evtchn(evtchn);
556
557 if (pirq_needs_eoi(data->irq)) {
558 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
559 WARN_ON(rc);
560 }
561}
562
563static void mask_ack_pirq(struct irq_data *data)
564{
565 disable_dynirq(data);
566 eoi_pirq(data);
567}
568
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000569static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400570{
571 struct evtchn_bind_pirq bind_pirq;
572 struct irq_info *info = info_for_irq(irq);
573 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400574 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400575
576 BUG_ON(info->type != IRQT_PIRQ);
577
578 if (VALID_EVTCHN(evtchn))
579 goto out;
580
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100581 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400582 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400583 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
584 BIND_PIRQ__WILL_SHARE : 0;
585 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
586 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400587 if (!probing_irq(irq))
Joe Perches283c0972013-06-28 03:21:41 -0700588 pr_info("Failed to obtain physical IRQ %d\n", irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400589 return 0;
590 }
591 evtchn = bind_pirq.port;
592
593 pirq_query_unmask(irq);
594
595 evtchn_to_irq[evtchn] = irq;
596 bind_evtchn_to_cpu(evtchn, 0);
597 info->evtchn = evtchn;
598
599out:
600 unmask_evtchn(evtchn);
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100601 eoi_pirq(irq_get_irq_data(irq));
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400602
603 return 0;
604}
605
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000606static unsigned int startup_pirq(struct irq_data *data)
607{
608 return __startup_pirq(data->irq);
609}
610
611static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400612{
613 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000614 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400615 struct irq_info *info = info_for_irq(irq);
616 int evtchn = evtchn_from_irq(irq);
617
618 BUG_ON(info->type != IRQT_PIRQ);
619
620 if (!VALID_EVTCHN(evtchn))
621 return;
622
623 mask_evtchn(evtchn);
624
625 close.port = evtchn;
626 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
627 BUG();
628
629 bind_evtchn_to_cpu(evtchn, 0);
630 evtchn_to_irq[evtchn] = -1;
631 info->evtchn = 0;
632}
633
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000634static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400635{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000636 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400637}
638
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000639static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400640{
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100641 disable_dynirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400642}
643
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100644int xen_irq_from_gsi(unsigned gsi)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400645{
Ian Campbell6cb65372011-03-10 16:08:11 +0000646 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400647
Ian Campbell6cb65372011-03-10 16:08:11 +0000648 list_for_each_entry(info, &xen_irq_list_head, list) {
649 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400650 continue;
651
Ian Campbell6cb65372011-03-10 16:08:11 +0000652 if (info->u.pirq.gsi == gsi)
653 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400654 }
655
656 return -1;
657}
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100658EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400659
Ian Campbell653378a2011-03-10 16:08:04 +0000660/*
661 * Do not make any assumptions regarding the relationship between the
662 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100663 *
664 * Note: We don't assign an event channel until the irq actually started
665 * up. Return an existing irq if we've already got one for the gsi.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100666 *
667 * Shareable implies level triggered, not shareable implies edge
668 * triggered here.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400669 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000670int xen_bind_pirq_gsi_to_irq(unsigned gsi,
671 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400672{
Ian Campbella0e18112011-03-10 16:08:03 +0000673 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400674 struct physdev_irq irq_op;
675
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400676 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400677
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100678 irq = xen_irq_from_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400679 if (irq != -1) {
Joe Perches283c0972013-06-28 03:21:41 -0700680 pr_info("%s: returning irq %d for gsi %u\n",
681 __func__, irq, gsi);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400682 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400683 }
684
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000685 irq = xen_allocate_irq_gsi(gsi);
Ian Campbell7bee9762011-03-10 16:08:15 +0000686 if (irq < 0)
687 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400688
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400689 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400690 irq_op.vector = 0;
691
692 /* Only the privileged domain can do this. For non-priv, the pcifront
693 * driver provides a PCI bus that does the call to do exactly
694 * this in the priv domain. */
695 if (xen_initial_domain() &&
696 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000697 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400698 irq = -ENOSPC;
699 goto out;
700 }
701
Jan Beulichdec02de2013-04-03 15:52:50 +0100702 xen_irq_info_pirq_init(irq, 0, pirq, gsi, DOMID_SELF,
Ian Campbell9158c352011-03-10 16:08:09 +0000703 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400704
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100705 pirq_query_unmask(irq);
706 /* We try to use the handler with the appropriate semantic for the
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100707 * type of interrupt: if the interrupt is an edge triggered
708 * interrupt we use handle_edge_irq.
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100709 *
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100710 * On the other hand if the interrupt is level triggered we use
711 * handle_fasteoi_irq like the native code does for this kind of
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100712 * interrupts.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100713 *
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100714 * Depending on the Xen version, pirq_needs_eoi might return true
715 * not only for level triggered interrupts but for edge triggered
716 * interrupts too. In any case Xen always honors the eoi mechanism,
717 * not injecting any more pirqs of the same kind if the first one
718 * hasn't received an eoi yet. Therefore using the fasteoi handler
719 * is the right choice either way.
720 */
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100721 if (shareable)
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100722 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
723 handle_fasteoi_irq, name);
724 else
725 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
726 handle_edge_irq, name);
727
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400728out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400729 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400730
731 return irq;
732}
733
Qing Hef731e3ef2010-10-11 15:30:09 +0100734#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000735int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000736{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000737 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000738 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000739
Ian Campbellbf480d92011-02-18 16:43:32 +0000740 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000741 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000742
Ian Campbell5cad61a2011-02-18 16:43:31 +0000743 WARN_ONCE(rc == -ENOSYS,
744 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
745
746 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000747}
748
Ian Campbellbf480d92011-02-18 16:43:32 +0000749int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Jan Beulichdec02de2013-04-03 15:52:50 +0100750 int pirq, const char *name, domid_t domid)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100751{
Ian Campbellbf480d92011-02-18 16:43:32 +0000752 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000753
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400754 mutex_lock(&irq_mapping_update_lock);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100755
Ian Campbell4b41df72011-02-18 16:43:29 +0000756 irq = xen_allocate_irq_dynamic();
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400757 if (irq < 0)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000758 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100759
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100760 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
761 name);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100762
Jan Beulichdec02de2013-04-03 15:52:50 +0100763 xen_irq_info_pirq_init(irq, 0, pirq, 0, domid, 0);
Linus Torvalds5f6fb452011-03-15 19:23:40 -0700764 ret = irq_set_msi_desc(irq, msidesc);
Ian Campbellbf480d92011-02-18 16:43:32 +0000765 if (ret < 0)
766 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100767out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400768 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000769 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000770error_irq:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400771 mutex_unlock(&irq_mapping_update_lock);
Ian Campbellbf480d92011-02-18 16:43:32 +0000772 xen_free_irq(irq);
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400773 return ret;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100774}
Qing Hef731e3ef2010-10-11 15:30:09 +0100775#endif
776
Alex Nixonb5401a92010-03-18 16:31:34 -0400777int xen_destroy_irq(int irq)
778{
779 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100780 struct physdev_unmap_pirq unmap_irq;
781 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400782 int rc = -ENOENT;
783
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400784 mutex_lock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400785
786 desc = irq_to_desc(irq);
787 if (!desc)
788 goto out;
789
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100790 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500791 unmap_irq.pirq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400792 unmap_irq.domid = info->u.pirq.domid;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100793 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500794 /* If another domain quits without making the pci_disable_msix
795 * call, the Xen hypervisor takes care of freeing the PIRQs
796 * (free_domain_pirqs).
797 */
798 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
Joe Perches283c0972013-06-28 03:21:41 -0700799 pr_info("domain %d does not have %d anymore\n",
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500800 info->u.pirq.domid, info->u.pirq.pirq);
801 else if (rc) {
Joe Perches283c0972013-06-28 03:21:41 -0700802 pr_warn("unmap irq failed %d\n", rc);
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100803 goto out;
804 }
805 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400806
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000807 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400808
809out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400810 mutex_unlock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400811 return rc;
812}
813
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000814int xen_irq_from_pirq(unsigned pirq)
815{
Ian Campbell69c358c2011-03-10 16:08:13 +0000816 int irq;
817
818 struct irq_info *info;
819
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400820 mutex_lock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000821
822 list_for_each_entry(info, &xen_irq_list_head, list) {
Konrad Rzeszutek Wilk9bb9efe2011-09-29 13:13:30 -0400823 if (info->type != IRQT_PIRQ)
Ian Campbell69c358c2011-03-10 16:08:13 +0000824 continue;
825 irq = info->irq;
826 if (info->u.pirq.pirq == pirq)
827 goto out;
828 }
829 irq = -1;
830out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400831 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000832
833 return irq;
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000834}
835
Konrad Rzeszutek Wilke6197ac2011-02-24 14:20:12 -0500836
837int xen_pirq_from_irq(unsigned irq)
838{
839 return pirq_from_irq(irq);
840}
841EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700842int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700843{
844 int irq;
845
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400846 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700847
848 irq = evtchn_to_irq[evtchn];
849
850 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000851 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000852 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000853 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700854
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100855 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100856 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700857
Ian Campbell9158c352011-03-10 16:08:09 +0000858 xen_irq_info_evtchn_init(irq, evtchn);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400859 } else {
860 struct irq_info *info = info_for_irq(irq);
861 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700862 }
863
Ian Campbell7bee9762011-03-10 16:08:15 +0000864out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400865 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700866
867 return irq;
868}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700869EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700870
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700871static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
872{
873 struct evtchn_bind_ipi bind_ipi;
874 int evtchn, irq;
875
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400876 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700877
878 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800879
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700880 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000881 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700882 if (irq < 0)
883 goto out;
884
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100885 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700886 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700887
888 bind_ipi.vcpu = cpu;
889 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
890 &bind_ipi) != 0)
891 BUG();
892 evtchn = bind_ipi.port;
893
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000894 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700895
896 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400897 } else {
898 struct irq_info *info = info_for_irq(irq);
899 WARN_ON(info == NULL || info->type != IRQT_IPI);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700900 }
901
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700902 out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400903 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700904 return irq;
905}
906
Ian Campbell2e820f52009-02-09 12:05:50 -0800907static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
908 unsigned int remote_port)
909{
910 struct evtchn_bind_interdomain bind_interdomain;
911 int err;
912
913 bind_interdomain.remote_dom = remote_domain;
914 bind_interdomain.remote_port = remote_port;
915
916 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
917 &bind_interdomain);
918
919 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
920}
921
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200922static int find_virq(unsigned int virq, unsigned int cpu)
923{
924 struct evtchn_status status;
925 int port, rc = -ENOENT;
926
927 memset(&status, 0, sizeof(status));
928 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
929 status.dom = DOMID_SELF;
930 status.port = port;
931 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
932 if (rc < 0)
933 continue;
934 if (status.status != EVTCHNSTAT_virq)
935 continue;
936 if (status.u.virq == virq && status.vcpu == cpu) {
937 rc = port;
938 break;
939 }
940 }
941 return rc;
942}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700943
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100944int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700945{
946 struct evtchn_bind_virq bind_virq;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200947 int evtchn, irq, ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700948
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400949 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700950
951 irq = per_cpu(virq_to_irq, cpu)[virq];
952
953 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000954 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000955 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000956 goto out;
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700957
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100958 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700959 handle_percpu_irq, "virq");
960
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700961 bind_virq.virq = virq;
962 bind_virq.vcpu = cpu;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200963 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
964 &bind_virq);
965 if (ret == 0)
966 evtchn = bind_virq.port;
967 else {
968 if (ret == -EEXIST)
969 ret = find_virq(virq, cpu);
970 BUG_ON(ret < 0);
971 evtchn = ret;
972 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700973
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000974 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700975
976 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400977 } else {
978 struct irq_info *info = info_for_irq(irq);
979 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700980 }
981
Ian Campbell7bee9762011-03-10 16:08:15 +0000982out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400983 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700984
985 return irq;
986}
987
988static void unbind_from_irq(unsigned int irq)
989{
990 struct evtchn_close close;
991 int evtchn = evtchn_from_irq(irq);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400992 struct irq_info *info = irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700993
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -0400994 if (WARN_ON(!info))
995 return;
996
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400997 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700998
Daniel De Graaf420eb552011-10-27 17:58:47 -0400999 if (info->refcnt > 0) {
1000 info->refcnt--;
1001 if (info->refcnt != 0)
1002 goto done;
1003 }
1004
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001005 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001006 close.port = evtchn;
1007 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
1008 BUG();
1009
1010 switch (type_from_irq(irq)) {
1011 case IRQT_VIRQ:
1012 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001013 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001014 break;
Alex Nixond68d82a2008-08-22 11:52:15 +01001015 case IRQT_IPI:
1016 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001017 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +01001018 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001019 default:
1020 break;
1021 }
1022
1023 /* Closed ports are implicitly re-bound to VCPU0. */
1024 bind_evtchn_to_cpu(evtchn, 0);
1025
1026 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +00001027 }
1028
Ian Campbellca62ce82011-03-10 16:08:12 +00001029 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001030
Ian Campbell9158c352011-03-10 16:08:09 +00001031 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001032
Daniel De Graaf420eb552011-10-27 17:58:47 -04001033 done:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001034 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001035}
1036
1037int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -04001038 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001039 unsigned long irqflags,
1040 const char *devname, void *dev_id)
1041{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001042 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001043
1044 irq = bind_evtchn_to_irq(evtchn);
Ian Campbell7bee9762011-03-10 16:08:15 +00001045 if (irq < 0)
1046 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001047 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1048 if (retval != 0) {
1049 unbind_from_irq(irq);
1050 return retval;
1051 }
1052
1053 return irq;
1054}
1055EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1056
Ian Campbell2e820f52009-02-09 12:05:50 -08001057int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1058 unsigned int remote_port,
1059 irq_handler_t handler,
1060 unsigned long irqflags,
1061 const char *devname,
1062 void *dev_id)
1063{
1064 int irq, retval;
1065
1066 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1067 if (irq < 0)
1068 return irq;
1069
1070 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1071 if (retval != 0) {
1072 unbind_from_irq(irq);
1073 return retval;
1074 }
1075
1076 return irq;
1077}
1078EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1079
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001080int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -04001081 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001082 unsigned long irqflags, const char *devname, void *dev_id)
1083{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001084 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001085
1086 irq = bind_virq_to_irq(virq, cpu);
Ian Campbell7bee9762011-03-10 16:08:15 +00001087 if (irq < 0)
1088 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001089 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1090 if (retval != 0) {
1091 unbind_from_irq(irq);
1092 return retval;
1093 }
1094
1095 return irq;
1096}
1097EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1098
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001099int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1100 unsigned int cpu,
1101 irq_handler_t handler,
1102 unsigned long irqflags,
1103 const char *devname,
1104 void *dev_id)
1105{
1106 int irq, retval;
1107
1108 irq = bind_ipi_to_irq(ipi, cpu);
1109 if (irq < 0)
1110 return irq;
1111
Ian Campbell9bab0b72011-10-03 15:37:00 +01001112 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001113 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1114 if (retval != 0) {
1115 unbind_from_irq(irq);
1116 return retval;
1117 }
1118
1119 return irq;
1120}
1121
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001122void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1123{
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001124 struct irq_info *info = irq_get_handler_data(irq);
1125
1126 if (WARN_ON(!info))
1127 return;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001128 free_irq(irq, dev_id);
1129 unbind_from_irq(irq);
1130}
1131EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1132
Daniel De Graaf420eb552011-10-27 17:58:47 -04001133int evtchn_make_refcounted(unsigned int evtchn)
1134{
1135 int irq = evtchn_to_irq[evtchn];
1136 struct irq_info *info;
1137
1138 if (irq == -1)
1139 return -ENOENT;
1140
1141 info = irq_get_handler_data(irq);
1142
1143 if (!info)
1144 return -ENOENT;
1145
1146 WARN_ON(info->refcnt != -1);
1147
1148 info->refcnt = 1;
1149
1150 return 0;
1151}
1152EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1153
1154int evtchn_get(unsigned int evtchn)
1155{
1156 int irq;
1157 struct irq_info *info;
1158 int err = -ENOENT;
1159
Daniel De Graafc3b3f162011-11-28 11:49:09 -05001160 if (evtchn >= NR_EVENT_CHANNELS)
1161 return -EINVAL;
1162
Daniel De Graaf420eb552011-10-27 17:58:47 -04001163 mutex_lock(&irq_mapping_update_lock);
1164
1165 irq = evtchn_to_irq[evtchn];
1166 if (irq == -1)
1167 goto done;
1168
1169 info = irq_get_handler_data(irq);
1170
1171 if (!info)
1172 goto done;
1173
1174 err = -EINVAL;
1175 if (info->refcnt <= 0)
1176 goto done;
1177
1178 info->refcnt++;
1179 err = 0;
1180 done:
1181 mutex_unlock(&irq_mapping_update_lock);
1182
1183 return err;
1184}
1185EXPORT_SYMBOL_GPL(evtchn_get);
1186
1187void evtchn_put(unsigned int evtchn)
1188{
1189 int irq = evtchn_to_irq[evtchn];
1190 if (WARN_ON(irq == -1))
1191 return;
1192 unbind_from_irq(irq);
1193}
1194EXPORT_SYMBOL_GPL(evtchn_put);
1195
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001196void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1197{
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -04001198 int irq;
1199
Stefano Stabellini072b2062013-08-13 16:57:06 +00001200#ifdef CONFIG_X86
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -04001201 if (unlikely(vector == XEN_NMI_VECTOR)) {
1202 int rc = HYPERVISOR_vcpu_op(VCPUOP_send_nmi, cpu, NULL);
1203 if (rc < 0)
1204 printk(KERN_WARNING "Sending nmi to CPU%d failed (rc:%d)\n", cpu, rc);
1205 return;
1206 }
Stefano Stabellini072b2062013-08-13 16:57:06 +00001207#endif
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -04001208 irq = per_cpu(ipi_to_irq, cpu)[vector];
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001209 BUG_ON(irq < 0);
1210 notify_remote_via_irq(irq);
1211}
1212
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001213irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1214{
1215 struct shared_info *sh = HYPERVISOR_shared_info;
1216 int cpu = smp_processor_id();
Ian Campbellc81611c2013-02-20 11:48:06 +00001217 xen_ulong_t *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001218 int i;
1219 unsigned long flags;
1220 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001221 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001222
1223 spin_lock_irqsave(&debug_lock, flags);
1224
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001225 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001226
1227 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001228 int pending;
1229 v = per_cpu(xen_vcpu, i);
1230 pending = (get_irq_regs() && i == cpu)
1231 ? xen_irqs_disabled(get_irq_regs())
1232 : v->evtchn_upcall_mask;
Ian Campbellc81611c2013-02-20 11:48:06 +00001233 printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong"\n ", i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001234 pending, v->evtchn_upcall_pending,
1235 (int)(sizeof(v->evtchn_pending_sel)*2),
1236 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001237 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001238 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001239
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001240 printk("\npending:\n ");
1241 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001242 printk("%0*"PRI_xen_ulong"%s",
1243 (int)sizeof(sh->evtchn_pending[0])*2,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001244 sh->evtchn_pending[i],
1245 i % 8 == 0 ? "\n " : " ");
1246 printk("\nglobal mask:\n ");
1247 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001248 printk("%0*"PRI_xen_ulong"%s",
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001249 (int)(sizeof(sh->evtchn_mask[0])*2),
1250 sh->evtchn_mask[i],
1251 i % 8 == 0 ? "\n " : " ");
1252
1253 printk("\nglobally unmasked:\n ");
1254 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001255 printk("%0*"PRI_xen_ulong"%s",
1256 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001257 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1258 i % 8 == 0 ? "\n " : " ");
1259
1260 printk("\nlocal cpu%d mask:\n ", cpu);
Ian Campbellc81611c2013-02-20 11:48:06 +00001261 for (i = (NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD)-1; i >= 0; i--)
1262 printk("%0*"PRI_xen_ulong"%s", (int)(sizeof(cpu_evtchn[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001263 cpu_evtchn[i],
1264 i % 8 == 0 ? "\n " : " ");
1265
1266 printk("\nlocally unmasked:\n ");
1267 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001268 xen_ulong_t pending = sh->evtchn_pending[i]
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001269 & ~sh->evtchn_mask[i]
1270 & cpu_evtchn[i];
Ian Campbellc81611c2013-02-20 11:48:06 +00001271 printk("%0*"PRI_xen_ulong"%s",
1272 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001273 pending, i % 8 == 0 ? "\n " : " ");
1274 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001275
1276 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001277 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001278 if (sync_test_bit(i, BM(sh->evtchn_pending))) {
1279 int word_idx = i / BITS_PER_EVTCHN_WORD;
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001280 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001281 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001282 evtchn_to_irq[i],
Ian Campbellc81611c2013-02-20 11:48:06 +00001283 sync_test_bit(word_idx, BM(&v->evtchn_pending_sel))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001284 ? "" : " l2-clear",
Ian Campbellc81611c2013-02-20 11:48:06 +00001285 !sync_test_bit(i, BM(sh->evtchn_mask))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001286 ? "" : " globally-masked",
Ian Campbellc81611c2013-02-20 11:48:06 +00001287 sync_test_bit(i, BM(cpu_evtchn))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001288 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001289 }
1290 }
1291
1292 spin_unlock_irqrestore(&debug_lock, flags);
1293
1294 return IRQ_HANDLED;
1295}
1296
Tejun Heo245b2e72009-06-24 15:13:48 +09001297static DEFINE_PER_CPU(unsigned, xed_nesting_count);
Keir Fraserada68142011-03-03 10:01:11 +00001298static DEFINE_PER_CPU(unsigned int, current_word_idx);
1299static DEFINE_PER_CPU(unsigned int, current_bit_idx);
Tejun Heo245b2e72009-06-24 15:13:48 +09001300
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001301/*
Scott Rixnerab7f8632011-03-03 09:30:08 +00001302 * Mask out the i least significant bits of w
1303 */
Ian Campbellc81611c2013-02-20 11:48:06 +00001304#define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001305
1306/*
1307 * Search the CPUs pending events bitmasks. For each one found, map
1308 * the event number to an irq, and feed it into do_IRQ() for
1309 * handling.
1310 *
1311 * Xen uses a two-level bitmap to speed searching. The first level is
1312 * a bitset of words which contain pending event bits. The second
1313 * level is a bitset of pending events themselves.
1314 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001315static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001316{
Keir Fraser24b51c22011-03-03 11:06:28 +00001317 int start_word_idx, start_bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001318 int word_idx, bit_idx;
Keir Fraserbee980d2013-03-28 10:03:36 -04001319 int i, irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001320 int cpu = get_cpu();
1321 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001322 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Ruslan Pisarev088c05a2011-07-26 14:16:13 +03001323 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001324
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001325 do {
Ian Campbellc81611c2013-02-20 11:48:06 +00001326 xen_ulong_t pending_words;
Keir Fraserbee980d2013-03-28 10:03:36 -04001327 xen_ulong_t pending_bits;
1328 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001329
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001330 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001331
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001332 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001333 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001334
Ian Campbellc81611c2013-02-20 11:48:06 +00001335 /*
1336 * Master flag must be cleared /before/ clearing
1337 * selector flag. xchg_xen_ulong must contain an
1338 * appropriate barrier.
1339 */
Keir Fraserbee980d2013-03-28 10:03:36 -04001340 if ((irq = per_cpu(virq_to_irq, cpu)[VIRQ_TIMER]) != -1) {
1341 int evtchn = evtchn_from_irq(irq);
1342 word_idx = evtchn / BITS_PER_LONG;
1343 pending_bits = evtchn % BITS_PER_LONG;
1344 if (active_evtchns(cpu, s, word_idx) & (1ULL << pending_bits)) {
1345 desc = irq_to_desc(irq);
1346 if (desc)
1347 generic_handle_irq_desc(irq, desc);
1348 }
1349 }
1350
Ian Campbellc81611c2013-02-20 11:48:06 +00001351 pending_words = xchg_xen_ulong(&vcpu_info->evtchn_pending_sel, 0);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001352
Keir Fraser24b51c22011-03-03 11:06:28 +00001353 start_word_idx = __this_cpu_read(current_word_idx);
1354 start_bit_idx = __this_cpu_read(current_bit_idx);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001355
Keir Fraser24b51c22011-03-03 11:06:28 +00001356 word_idx = start_word_idx;
1357
1358 for (i = 0; pending_words != 0; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001359 xen_ulong_t words;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001360
Scott Rixnerab7f8632011-03-03 09:30:08 +00001361 words = MASK_LSBS(pending_words, word_idx);
1362
1363 /*
Keir Fraserada68142011-03-03 10:01:11 +00001364 * If we masked out all events, wrap to beginning.
Scott Rixnerab7f8632011-03-03 09:30:08 +00001365 */
1366 if (words == 0) {
Keir Fraserada68142011-03-03 10:01:11 +00001367 word_idx = 0;
1368 bit_idx = 0;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001369 continue;
1370 }
Ian Campbellc81611c2013-02-20 11:48:06 +00001371 word_idx = EVTCHN_FIRST_BIT(words);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001372
Keir Fraser24b51c22011-03-03 11:06:28 +00001373 pending_bits = active_evtchns(cpu, s, word_idx);
1374 bit_idx = 0; /* usually scan entire word from start */
David Vrabel3ef02962013-08-15 13:21:05 +01001375 /*
1376 * We scan the starting word in two parts.
1377 *
1378 * 1st time: start in the middle, scanning the
1379 * upper bits.
1380 *
1381 * 2nd time: scan the whole word (not just the
1382 * parts skipped in the first pass) -- if an
1383 * event in the previously scanned bits is
1384 * pending again it would just be scanned on
1385 * the next loop anyway.
1386 */
Keir Fraser24b51c22011-03-03 11:06:28 +00001387 if (word_idx == start_word_idx) {
Keir Fraser24b51c22011-03-03 11:06:28 +00001388 if (i == 0)
Keir Fraser24b51c22011-03-03 11:06:28 +00001389 bit_idx = start_bit_idx;
Keir Fraser24b51c22011-03-03 11:06:28 +00001390 }
1391
Scott Rixnerab7f8632011-03-03 09:30:08 +00001392 do {
Ian Campbellc81611c2013-02-20 11:48:06 +00001393 xen_ulong_t bits;
Keir Fraserbee980d2013-03-28 10:03:36 -04001394 int port;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001395
Scott Rixnerab7f8632011-03-03 09:30:08 +00001396 bits = MASK_LSBS(pending_bits, bit_idx);
1397
1398 /* If we masked out all events, move on. */
Keir Fraserada68142011-03-03 10:01:11 +00001399 if (bits == 0)
Scott Rixnerab7f8632011-03-03 09:30:08 +00001400 break;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001401
Ian Campbellc81611c2013-02-20 11:48:06 +00001402 bit_idx = EVTCHN_FIRST_BIT(bits);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001403
1404 /* Process port. */
Ian Campbellc81611c2013-02-20 11:48:06 +00001405 port = (word_idx * BITS_PER_EVTCHN_WORD) + bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001406 irq = evtchn_to_irq[port];
1407
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001408 if (irq != -1) {
1409 desc = irq_to_desc(irq);
1410 if (desc)
1411 generic_handle_irq_desc(irq, desc);
1412 }
Scott Rixnerab7f8632011-03-03 09:30:08 +00001413
Ian Campbellc81611c2013-02-20 11:48:06 +00001414 bit_idx = (bit_idx + 1) % BITS_PER_EVTCHN_WORD;
Keir Fraserada68142011-03-03 10:01:11 +00001415
1416 /* Next caller starts at last processed + 1 */
1417 __this_cpu_write(current_word_idx,
1418 bit_idx ? word_idx :
Ian Campbellc81611c2013-02-20 11:48:06 +00001419 (word_idx+1) % BITS_PER_EVTCHN_WORD);
Keir Fraserada68142011-03-03 10:01:11 +00001420 __this_cpu_write(current_bit_idx, bit_idx);
1421 } while (bit_idx != 0);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001422
Keir Fraser24b51c22011-03-03 11:06:28 +00001423 /* Scan start_l1i twice; all others once. */
1424 if ((word_idx != start_word_idx) || (i != 0))
Scott Rixnerab7f8632011-03-03 09:30:08 +00001425 pending_words &= ~(1UL << word_idx);
Keir Fraserada68142011-03-03 10:01:11 +00001426
Ian Campbellc81611c2013-02-20 11:48:06 +00001427 word_idx = (word_idx + 1) % BITS_PER_EVTCHN_WORD;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001428 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001429
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001430 BUG_ON(!irqs_disabled());
1431
Christoph Lameter780f36d2010-12-06 11:16:29 -06001432 count = __this_cpu_read(xed_nesting_count);
1433 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001434 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001435
1436out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001437
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001438 put_cpu();
1439}
1440
Sheng Yang38e20b02010-05-14 12:40:51 +01001441void xen_evtchn_do_upcall(struct pt_regs *regs)
1442{
1443 struct pt_regs *old_regs = set_irq_regs(regs);
1444
Mojiong Qiu772aebc2012-11-06 16:08:15 +08001445 irq_enter();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001446#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001447 exit_idle();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001448#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001449
1450 __xen_evtchn_do_upcall();
1451
1452 irq_exit();
1453 set_irq_regs(old_regs);
1454}
1455
1456void xen_hvm_evtchn_do_upcall(void)
1457{
1458 __xen_evtchn_do_upcall();
1459}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001460EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001461
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001462/* Rebind a new event channel to an existing irq. */
1463void rebind_evtchn_irq(int evtchn, int irq)
1464{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001465 struct irq_info *info = info_for_irq(irq);
1466
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001467 if (WARN_ON(!info))
1468 return;
1469
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001470 /* Make sure the irq is masked, since the new event channel
1471 will also be masked. */
1472 disable_irq(irq);
1473
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001474 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001475
1476 /* After resume the irq<->evtchn mappings are all cleared out */
1477 BUG_ON(evtchn_to_irq[evtchn] != -1);
1478 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001479 so there should be a proper type */
1480 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001481
Ian Campbell9158c352011-03-10 16:08:09 +00001482 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001483
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001484 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001485
1486 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301487 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001488
1489 /* Unmask the event channel. */
1490 enable_irq(irq);
1491}
1492
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001493/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001494static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001495{
David Vrabel4704fe42013-08-15 13:21:07 +01001496 struct shared_info *s = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001497 struct evtchn_bind_vcpu bind_vcpu;
1498 int evtchn = evtchn_from_irq(irq);
David Vrabel4704fe42013-08-15 13:21:07 +01001499 int masked;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001500
Ian Campbellbe494722011-03-10 16:08:02 +00001501 if (!VALID_EVTCHN(evtchn))
1502 return -1;
1503
1504 /*
1505 * Events delivered via platform PCI interrupts are always
1506 * routed to vcpu 0 and hence cannot be rebound.
1507 */
1508 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001509 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001510
1511 /* Send future instances of this interrupt to other vcpu. */
1512 bind_vcpu.port = evtchn;
1513 bind_vcpu.vcpu = tcpu;
1514
1515 /*
David Vrabel4704fe42013-08-15 13:21:07 +01001516 * Mask the event while changing the VCPU binding to prevent
1517 * it being delivered on an unexpected VCPU.
1518 */
1519 masked = sync_test_and_set_bit(evtchn, BM(s->evtchn_mask));
1520
1521 /*
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001522 * If this fails, it usually just indicates that we're dealing with a
1523 * virq or IPI channel, which don't actually need to be rebound. Ignore
1524 * it, but don't do the xenlinux-level rebind in that case.
1525 */
1526 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1527 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001528
David Vrabel4704fe42013-08-15 13:21:07 +01001529 if (!masked)
1530 unmask_evtchn(evtchn);
1531
Yinghai Lud5dedd42009-04-27 17:59:21 -07001532 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001533}
1534
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001535static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1536 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001537{
Rusty Russell0de26522008-12-13 21:20:26 +10301538 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001539
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001540 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001541}
1542
David Vrabel87295182013-03-12 18:28:04 +00001543static int retrigger_evtchn(int evtchn)
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001544{
David Vrabel87295182013-03-12 18:28:04 +00001545 int masked;
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001546 struct shared_info *s = HYPERVISOR_shared_info;
1547
1548 if (!VALID_EVTCHN(evtchn))
David Vrabel87295182013-03-12 18:28:04 +00001549 return 0;
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001550
Ian Campbellc81611c2013-02-20 11:48:06 +00001551 masked = sync_test_and_set_bit(evtchn, BM(s->evtchn_mask));
1552 sync_set_bit(evtchn, BM(s->evtchn_pending));
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001553 if (!masked)
1554 unmask_evtchn(evtchn);
1555
1556 return 1;
1557}
1558
David Vrabel87295182013-03-12 18:28:04 +00001559int resend_irq_on_evtchn(unsigned int irq)
1560{
1561 return retrigger_evtchn(evtchn_from_irq(irq));
1562}
1563
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001564static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001565{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001566 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001567
1568 if (VALID_EVTCHN(evtchn))
1569 unmask_evtchn(evtchn);
1570}
1571
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001572static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001573{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001574 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001575
1576 if (VALID_EVTCHN(evtchn))
1577 mask_evtchn(evtchn);
1578}
1579
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001580static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001581{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001582 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001583
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001584 irq_move_irq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001585
1586 if (VALID_EVTCHN(evtchn))
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001587 clear_evtchn(evtchn);
1588}
1589
1590static void mask_ack_dynirq(struct irq_data *data)
1591{
1592 disable_dynirq(data);
1593 ack_dynirq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001594}
1595
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001596static int retrigger_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001597{
David Vrabel87295182013-03-12 18:28:04 +00001598 return retrigger_evtchn(evtchn_from_irq(data->irq));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001599}
1600
Ian Campbell0a852262011-03-10 16:08:06 +00001601static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001602{
1603 int pirq, rc, irq, gsi;
1604 struct physdev_map_pirq map_irq;
Ian Campbell69c358c2011-03-10 16:08:13 +00001605 struct irq_info *info;
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001606
Ian Campbell69c358c2011-03-10 16:08:13 +00001607 list_for_each_entry(info, &xen_irq_list_head, list) {
1608 if (info->type != IRQT_PIRQ)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001609 continue;
1610
Ian Campbell69c358c2011-03-10 16:08:13 +00001611 pirq = info->u.pirq.pirq;
1612 gsi = info->u.pirq.gsi;
1613 irq = info->irq;
1614
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001615 /* save/restore of PT devices doesn't work, so at this point the
1616 * only devices present are GSI based emulated devices */
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001617 if (!gsi)
1618 continue;
1619
1620 map_irq.domid = DOMID_SELF;
1621 map_irq.type = MAP_PIRQ_TYPE_GSI;
1622 map_irq.index = gsi;
1623 map_irq.pirq = pirq;
1624
1625 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1626 if (rc) {
Joe Perches283c0972013-06-28 03:21:41 -07001627 pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1628 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001629 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001630 continue;
1631 }
1632
1633 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1634
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001635 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001636 }
1637}
1638
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001639static void restore_cpu_virqs(unsigned int cpu)
1640{
1641 struct evtchn_bind_virq bind_virq;
1642 int virq, irq, evtchn;
1643
1644 for (virq = 0; virq < NR_VIRQS; virq++) {
1645 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1646 continue;
1647
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001648 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001649
1650 /* Get a new binding from Xen. */
1651 bind_virq.virq = virq;
1652 bind_virq.vcpu = cpu;
1653 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1654 &bind_virq) != 0)
1655 BUG();
1656 evtchn = bind_virq.port;
1657
1658 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001659 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001660 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001661 }
1662}
1663
1664static void restore_cpu_ipis(unsigned int cpu)
1665{
1666 struct evtchn_bind_ipi bind_ipi;
1667 int ipi, irq, evtchn;
1668
1669 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1670 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1671 continue;
1672
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001673 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001674
1675 /* Get a new binding from Xen. */
1676 bind_ipi.vcpu = cpu;
1677 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1678 &bind_ipi) != 0)
1679 BUG();
1680 evtchn = bind_ipi.port;
1681
1682 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001683 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001684 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001685 }
1686}
1687
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001688/* Clear an irq's pending state, in preparation for polling on it */
1689void xen_clear_irq_pending(int irq)
1690{
1691 int evtchn = evtchn_from_irq(irq);
1692
1693 if (VALID_EVTCHN(evtchn))
1694 clear_evtchn(evtchn);
1695}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001696EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001697void xen_set_irq_pending(int irq)
1698{
1699 int evtchn = evtchn_from_irq(irq);
1700
1701 if (VALID_EVTCHN(evtchn))
1702 set_evtchn(evtchn);
1703}
1704
1705bool xen_test_irq_pending(int irq)
1706{
1707 int evtchn = evtchn_from_irq(irq);
1708 bool ret = false;
1709
1710 if (VALID_EVTCHN(evtchn))
1711 ret = test_evtchn(evtchn);
1712
1713 return ret;
1714}
1715
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001716/* Poll waiting for an irq to become pending with timeout. In the usual case,
1717 * the irq will be disabled so it won't deliver an interrupt. */
1718void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001719{
1720 evtchn_port_t evtchn = evtchn_from_irq(irq);
1721
1722 if (VALID_EVTCHN(evtchn)) {
1723 struct sched_poll poll;
1724
1725 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001726 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001727 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001728
1729 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1730 BUG();
1731 }
1732}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001733EXPORT_SYMBOL(xen_poll_irq_timeout);
1734/* Poll waiting for an irq to become pending. In the usual case, the
1735 * irq will be disabled so it won't deliver an interrupt. */
1736void xen_poll_irq(int irq)
1737{
1738 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1739}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001740
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001741/* Check whether the IRQ line is shared with other guests. */
1742int xen_test_irq_shared(int irq)
1743{
1744 struct irq_info *info = info_for_irq(irq);
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001745 struct physdev_irq_status_query irq_status;
1746
1747 if (WARN_ON(!info))
1748 return -ENOENT;
1749
1750 irq_status.irq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001751
1752 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1753 return 0;
1754 return !(irq_status.flags & XENIRQSTAT_shared);
1755}
1756EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1757
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001758void xen_irq_resume(void)
1759{
Ian Campbell6cb65372011-03-10 16:08:11 +00001760 unsigned int cpu, evtchn;
1761 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001762
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001763 /* New event-channel space is not 'live' yet. */
1764 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1765 mask_evtchn(evtchn);
1766
1767 /* No IRQ <-> event-channel mappings. */
Ian Campbell6cb65372011-03-10 16:08:11 +00001768 list_for_each_entry(info, &xen_irq_list_head, list)
1769 info->evtchn = 0; /* zap event-channel binding */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001770
1771 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1772 evtchn_to_irq[evtchn] = -1;
1773
1774 for_each_possible_cpu(cpu) {
1775 restore_cpu_virqs(cpu);
1776 restore_cpu_ipis(cpu);
1777 }
Ian Campbell69035912010-11-01 16:30:09 +00001778
Ian Campbell0a852262011-03-10 16:08:06 +00001779 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001780}
1781
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001782static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001783 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001784
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001785 .irq_disable = disable_dynirq,
1786 .irq_mask = disable_dynirq,
1787 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001788
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001789 .irq_ack = ack_dynirq,
1790 .irq_mask_ack = mask_ack_dynirq,
1791
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001792 .irq_set_affinity = set_affinity_irq,
1793 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001794};
1795
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001796static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001797 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001798
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001799 .irq_startup = startup_pirq,
1800 .irq_shutdown = shutdown_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001801 .irq_enable = enable_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001802 .irq_disable = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001803
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001804 .irq_mask = disable_dynirq,
1805 .irq_unmask = enable_dynirq,
1806
1807 .irq_ack = eoi_pirq,
1808 .irq_eoi = eoi_pirq,
1809 .irq_mask_ack = mask_ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001810
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001811 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001812
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001813 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001814};
1815
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001816static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001817 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001818
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001819 .irq_disable = disable_dynirq,
1820 .irq_mask = disable_dynirq,
1821 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001822
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001823 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001824};
1825
Sheng Yang38e20b02010-05-14 12:40:51 +01001826int xen_set_callback_via(uint64_t via)
1827{
1828 struct xen_hvm_param a;
1829 a.domid = DOMID_SELF;
1830 a.index = HVM_PARAM_CALLBACK_IRQ;
1831 a.value = via;
1832 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1833}
1834EXPORT_SYMBOL_GPL(xen_set_callback_via);
1835
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001836#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001837/* Vector callbacks are better than PCI interrupts to receive event
1838 * channel notifications because we can receive vector callbacks on any
1839 * vcpu and we don't need PCI support or APIC interactions. */
1840void xen_callback_vector(void)
1841{
1842 int rc;
1843 uint64_t callback_via;
1844 if (xen_have_vector_callback) {
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001845 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
Sheng Yang38e20b02010-05-14 12:40:51 +01001846 rc = xen_set_callback_via(callback_via);
1847 if (rc) {
Joe Perches283c0972013-06-28 03:21:41 -07001848 pr_err("Request for Xen HVM callback vector failed\n");
Sheng Yang38e20b02010-05-14 12:40:51 +01001849 xen_have_vector_callback = 0;
1850 return;
1851 }
Joe Perches283c0972013-06-28 03:21:41 -07001852 pr_info("Xen HVM callback vector for event delivery is enabled\n");
Sheng Yang38e20b02010-05-14 12:40:51 +01001853 /* in the restore case the vector has already been allocated */
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001854 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
1855 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
1856 xen_hvm_callback_vector);
Sheng Yang38e20b02010-05-14 12:40:51 +01001857 }
1858}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001859#else
1860void xen_callback_vector(void) {}
1861#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001862
Stefano Stabellini2e3d8862012-10-02 15:57:57 +01001863void __init xen_init_IRQ(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001864{
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001865 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001866
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001867 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1868 GFP_KERNEL);
Konrad Rzeszutek Wilk9d093e22011-09-29 13:31:21 -04001869 BUG_ON(!evtchn_to_irq);
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001870 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1871 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001872
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001873 /* No event channels are 'live' right now. */
1874 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1875 mask_evtchn(i);
1876
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001877 pirq_needs_eoi = pirq_needs_eoi_flag;
1878
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001879#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001880 if (xen_hvm_domain()) {
1881 xen_callback_vector();
1882 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001883 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1884 * __acpi_register_gsi can point at the right function */
1885 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001886 } else {
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001887 int rc;
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001888 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1889
Sheng Yang38e20b02010-05-14 12:40:51 +01001890 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001891 if (xen_initial_domain())
Konrad Rzeszutek Wilka0ee0562011-06-09 09:49:13 -04001892 pci_xen_initial_domain();
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001893
1894 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1895 eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1896 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1897 if (rc != 0) {
1898 free_page((unsigned long) pirq_eoi_map);
1899 pirq_eoi_map = NULL;
1900 } else
1901 pirq_needs_eoi = pirq_check_eoi_map;
Sheng Yang38e20b02010-05-14 12:40:51 +01001902 }
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001903#endif
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001904}