blob: a58ac435a9a4a03f39274f8b5a41ea659d44eee7 [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>
59#include <asm/hw_irq.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070060
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070061/*
62 * This lock protects updates to the following mapping and reference-count
63 * arrays. The lock does not need to be acquired to read the mapping tables.
64 */
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -040065static DEFINE_MUTEX(irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070066
Ian Campbell6cb65372011-03-10 16:08:11 +000067static LIST_HEAD(xen_irq_list_head);
68
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070069/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090070static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070071
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070072/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090073static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070074
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080075/* Interrupt types. */
76enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080077 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070078 IRQT_PIRQ,
79 IRQT_VIRQ,
80 IRQT_IPI,
81 IRQT_EVTCHN
82};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070083
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080084/*
85 * Packed IRQ information:
86 * type - enum xen_irq_type
87 * event channel - irq->event channel mapping
88 * cpu - cpu this event channel is bound to
89 * index - type-specific information:
Jan Beulichdec02de2013-04-03 15:52:50 +010090 * PIRQ - physical IRQ, GSI, flags, and owner domain
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080091 * VIRQ - virq number
92 * IPI - IPI vector
93 * EVTCHN -
94 */
Ruslan Pisarev088c05a2011-07-26 14:16:13 +030095struct irq_info {
Ian Campbell6cb65372011-03-10 16:08:11 +000096 struct list_head list;
Daniel De Graaf420eb552011-10-27 17:58:47 -040097 int refcnt;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080098 enum xen_irq_type type; /* type */
Ian Campbell6cb65372011-03-10 16:08:11 +000099 unsigned irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800100 unsigned short evtchn; /* event channel */
101 unsigned short cpu; /* cpu bound */
102
103 union {
104 unsigned short virq;
105 enum ipi_vector ipi;
106 struct {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100107 unsigned short pirq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800108 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400109 unsigned char flags;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400110 uint16_t domid;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800111 } pirq;
112 } u;
113};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400114#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400115#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800116
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400117static int *evtchn_to_irq;
Ian Campbellbf86ad82012-10-17 09:39:12 +0100118#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000119static unsigned long *pirq_eoi_map;
Ian Campbellbf86ad82012-10-17 09:39:12 +0100120#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000121static bool (*pirq_needs_eoi)(unsigned irq);
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700122
Ian Campbellc81611c2013-02-20 11:48:06 +0000123/*
124 * Note sizeof(xen_ulong_t) can be more than sizeof(unsigned long). Be
125 * careful to only use bitops which allow for this (e.g
126 * test_bit/find_first_bit and friends but not __ffs) and to pass
127 * BITS_PER_EVTCHN_WORD as the bitmask length.
128 */
129#define BITS_PER_EVTCHN_WORD (sizeof(xen_ulong_t)*8)
130/*
131 * Make a bitmask (i.e. unsigned long *) of a xen_ulong_t
132 * array. Primarily to avoid long lines (hence the terse name).
133 */
134#define BM(x) (unsigned long *)(x)
135/* Find the first set bit in a evtchn mask */
136#define EVTCHN_FIRST_BIT(w) find_first_bit(BM(&(w)), BITS_PER_EVTCHN_WORD)
137
138static DEFINE_PER_CPU(xen_ulong_t [NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD],
Ian Campbellcb60d112011-03-10 16:08:08 +0000139 cpu_evtchn_mask);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700140
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700141/* Xen will never allocate port zero for any purpose. */
142#define VALID_EVTCHN(chn) ((chn) != 0)
143
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700144static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700145static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400146static struct irq_chip xen_pirq_chip;
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100147static void enable_dynirq(struct irq_data *data);
148static void disable_dynirq(struct irq_data *data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700149
Ian Campbell9158c352011-03-10 16:08:09 +0000150/* Get info for IRQ */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800151static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700152{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100153 return irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700154}
155
Ian Campbell9158c352011-03-10 16:08:09 +0000156/* Constructors for packed IRQ information. */
157static void xen_irq_info_common_init(struct irq_info *info,
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000158 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000159 enum xen_irq_type type,
160 unsigned short evtchn,
161 unsigned short cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700162{
Ian Campbell9158c352011-03-10 16:08:09 +0000163
164 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
165
166 info->type = type;
Ian Campbell6cb65372011-03-10 16:08:11 +0000167 info->irq = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000168 info->evtchn = evtchn;
169 info->cpu = cpu;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000170
171 evtchn_to_irq[evtchn] = irq;
Julien Grall934f5852013-04-30 18:29:13 +0100172
173 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700174}
175
Ian Campbell9158c352011-03-10 16:08:09 +0000176static void xen_irq_info_evtchn_init(unsigned irq,
177 unsigned short evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700178{
Ian Campbell9158c352011-03-10 16:08:09 +0000179 struct irq_info *info = info_for_irq(irq);
180
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000181 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700182}
183
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000184static void xen_irq_info_ipi_init(unsigned cpu,
185 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000186 unsigned short evtchn,
187 enum ipi_vector ipi)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700188{
Ian Campbell9158c352011-03-10 16:08:09 +0000189 struct irq_info *info = info_for_irq(irq);
190
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000191 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000192
193 info->u.ipi = ipi;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000194
195 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700196}
197
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000198static void xen_irq_info_virq_init(unsigned cpu,
199 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000200 unsigned short evtchn,
201 unsigned short virq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700202{
Ian Campbell9158c352011-03-10 16:08:09 +0000203 struct irq_info *info = info_for_irq(irq);
204
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000205 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000206
207 info->u.virq = virq;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000208
209 per_cpu(virq_to_irq, cpu)[virq] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000210}
211
212static void xen_irq_info_pirq_init(unsigned irq,
213 unsigned short evtchn,
214 unsigned short pirq,
215 unsigned short gsi,
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400216 uint16_t domid,
Ian Campbell9158c352011-03-10 16:08:09 +0000217 unsigned char flags)
218{
219 struct irq_info *info = info_for_irq(irq);
220
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000221 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000222
223 info->u.pirq.pirq = pirq;
224 info->u.pirq.gsi = gsi;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400225 info->u.pirq.domid = domid;
Ian Campbell9158c352011-03-10 16:08:09 +0000226 info->u.pirq.flags = flags;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700227}
228
229/*
230 * Accessors for packed IRQ information.
231 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800232static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700233{
Joe Jin110e7c72011-01-07 14:50:12 +0800234 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
235 return 0;
236
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800237 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700238}
239
Ian Campbelld4c04532009-02-06 19:20:31 -0800240unsigned irq_from_evtchn(unsigned int evtchn)
241{
242 return evtchn_to_irq[evtchn];
243}
244EXPORT_SYMBOL_GPL(irq_from_evtchn);
245
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800246static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700247{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800248 struct irq_info *info = info_for_irq(irq);
249
250 BUG_ON(info == NULL);
251 BUG_ON(info->type != IRQT_IPI);
252
253 return info->u.ipi;
254}
255
256static unsigned virq_from_irq(unsigned irq)
257{
258 struct irq_info *info = info_for_irq(irq);
259
260 BUG_ON(info == NULL);
261 BUG_ON(info->type != IRQT_VIRQ);
262
263 return info->u.virq;
264}
265
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100266static unsigned pirq_from_irq(unsigned irq)
267{
268 struct irq_info *info = info_for_irq(irq);
269
270 BUG_ON(info == NULL);
271 BUG_ON(info->type != IRQT_PIRQ);
272
273 return info->u.pirq.pirq;
274}
275
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800276static enum xen_irq_type type_from_irq(unsigned irq)
277{
278 return info_for_irq(irq)->type;
279}
280
281static unsigned cpu_from_irq(unsigned irq)
282{
283 return info_for_irq(irq)->cpu;
284}
285
286static unsigned int cpu_from_evtchn(unsigned int evtchn)
287{
288 int irq = evtchn_to_irq[evtchn];
289 unsigned ret = 0;
290
291 if (irq != -1)
292 ret = cpu_from_irq(irq);
293
294 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700295}
296
Ian Campbellbf86ad82012-10-17 09:39:12 +0100297#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000298static bool pirq_check_eoi_map(unsigned irq)
299{
Stefano Stabellini521394e2012-04-25 16:11:38 +0100300 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000301}
Ian Campbellbf86ad82012-10-17 09:39:12 +0100302#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000303
304static bool pirq_needs_eoi_flag(unsigned irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400305{
306 struct irq_info *info = info_for_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400307 BUG_ON(info->type != IRQT_PIRQ);
308
309 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
310}
311
Ian Campbellc81611c2013-02-20 11:48:06 +0000312static inline xen_ulong_t active_evtchns(unsigned int cpu,
313 struct shared_info *sh,
314 unsigned int idx)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700315{
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300316 return sh->evtchn_pending[idx] &
Ian Campbellcb60d112011-03-10 16:08:08 +0000317 per_cpu(cpu_evtchn_mask, cpu)[idx] &
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300318 ~sh->evtchn_mask[idx];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700319}
320
321static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
322{
323 int irq = evtchn_to_irq[chn];
324
325 BUG_ON(irq == -1);
326#ifdef CONFIG_SMP
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000327 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700328#endif
329
Ian Campbellc81611c2013-02-20 11:48:06 +0000330 clear_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu_from_irq(irq))));
331 set_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700332
Ian Campbellca62ce82011-03-10 16:08:12 +0000333 info_for_irq(irq)->cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700334}
335
336static void init_evtchn_cpu_bindings(void)
337{
Jan Beulich1c6969e2010-11-16 14:55:33 -0800338 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700339#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000340 struct irq_info *info;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200341
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700342 /* By default all event channels notify CPU#0. */
Ian Campbell6cb65372011-03-10 16:08:11 +0000343 list_for_each_entry(info, &xen_irq_list_head, list) {
344 struct irq_desc *desc = irq_to_desc(info->irq);
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000345 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800346 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700347#endif
348
Jan Beulich1c6969e2010-11-16 14:55:33 -0800349 for_each_possible_cpu(i)
Ian Campbellcb60d112011-03-10 16:08:08 +0000350 memset(per_cpu(cpu_evtchn_mask, i),
351 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700352}
353
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700354static inline void clear_evtchn(int port)
355{
356 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000357 sync_clear_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700358}
359
360static inline void set_evtchn(int port)
361{
362 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000363 sync_set_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700364}
365
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700366static inline int test_evtchn(int port)
367{
368 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000369 return sync_test_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700370}
371
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700372
373/**
374 * notify_remote_via_irq - send event to remote end of event channel via irq
375 * @irq: irq of event channel to send event to
376 *
377 * Unlike notify_remote_via_evtchn(), this is safe to use across
378 * save/restore. Notifications on a broken connection are silently
379 * dropped.
380 */
381void notify_remote_via_irq(int irq)
382{
383 int evtchn = evtchn_from_irq(irq);
384
385 if (VALID_EVTCHN(evtchn))
386 notify_remote_via_evtchn(evtchn);
387}
388EXPORT_SYMBOL_GPL(notify_remote_via_irq);
389
390static void mask_evtchn(int port)
391{
392 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000393 sync_set_bit(port, BM(&s->evtchn_mask[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700394}
395
396static void unmask_evtchn(int port)
397{
398 struct shared_info *s = HYPERVISOR_shared_info;
399 unsigned int cpu = get_cpu();
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100400 int do_hypercall = 0, evtchn_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700401
402 BUG_ON(!irqs_disabled());
403
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100404 if (unlikely((cpu != cpu_from_evtchn(port))))
405 do_hypercall = 1;
David Vrabelc26377e2013-03-25 14:11:19 +0000406 else {
407 /*
408 * Need to clear the mask before checking pending to
409 * avoid a race with an event becoming pending.
410 *
411 * EVTCHNOP_unmask will only trigger an upcall if the
412 * mask bit was set, so if a hypercall is needed
413 * remask the event.
414 */
415 sync_clear_bit(port, BM(&s->evtchn_mask[0]));
Ian Campbellc81611c2013-02-20 11:48:06 +0000416 evtchn_pending = sync_test_bit(port, BM(&s->evtchn_pending[0]));
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100417
David Vrabelc26377e2013-03-25 14:11:19 +0000418 if (unlikely(evtchn_pending && xen_hvm_domain())) {
419 sync_set_bit(port, BM(&s->evtchn_mask[0]));
420 do_hypercall = 1;
421 }
422 }
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100423
424 /* Slow path (hypercall) if this is a non-local port or if this is
425 * an hvm domain and an event is pending (hvm domains don't have
426 * their own implementation of irq_enable). */
427 if (do_hypercall) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700428 struct evtchn_unmask unmask = { .port = port };
429 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
430 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600431 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700432
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700433 /*
434 * The following is basically the equivalent of
435 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
436 * the interrupt edge' if the channel is masked.
437 */
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100438 if (evtchn_pending &&
Ian Campbellc81611c2013-02-20 11:48:06 +0000439 !sync_test_and_set_bit(port / BITS_PER_EVTCHN_WORD,
440 BM(&vcpu_info->evtchn_pending_sel)))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700441 vcpu_info->evtchn_upcall_pending = 1;
442 }
443
444 put_cpu();
445}
446
Ian Campbell6cb65372011-03-10 16:08:11 +0000447static void xen_irq_init(unsigned irq)
448{
449 struct irq_info *info;
Konrad Rzeszutek Wilkb5328cd2011-06-15 14:24:29 -0400450#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000451 struct irq_desc *desc = irq_to_desc(irq);
452
453 /* By default all event channels notify CPU#0. */
454 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Konrad Rzeszutek Wilk44626e42011-03-15 16:40:33 -0400455#endif
Ian Campbell6cb65372011-03-10 16:08:11 +0000456
Ian Campbellca62ce82011-03-10 16:08:12 +0000457 info = kzalloc(sizeof(*info), GFP_KERNEL);
458 if (info == NULL)
459 panic("Unable to allocate metadata for IRQ%d\n", irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000460
461 info->type = IRQT_UNBOUND;
Daniel De Graaf420eb552011-10-27 17:58:47 -0400462 info->refcnt = -1;
Ian Campbell6cb65372011-03-10 16:08:11 +0000463
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100464 irq_set_handler_data(irq, info);
Ian Campbellca62ce82011-03-10 16:08:12 +0000465
Ian Campbell6cb65372011-03-10 16:08:11 +0000466 list_add_tail(&info->list, &xen_irq_list_head);
467}
468
Ian Campbell7bee9762011-03-10 16:08:15 +0000469static int __must_check xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700470{
Ian Campbell89911502011-03-03 11:57:44 -0500471 int first = 0;
472 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700473
Ian Campbell89911502011-03-03 11:57:44 -0500474#ifdef CONFIG_X86_IO_APIC
475 /*
476 * For an HVM guest or domain 0 which see "real" (emulated or
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300477 * actual respectively) GSIs we allocate dynamic IRQs
Ian Campbell89911502011-03-03 11:57:44 -0500478 * e.g. those corresponding to event channels or MSIs
479 * etc. from the range above those "real" GSIs to avoid
480 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500481 */
Ian Campbell89911502011-03-03 11:57:44 -0500482 if (xen_initial_domain() || xen_hvm_domain())
483 first = get_nr_irqs_gsi();
484#endif
485
Ian Campbell89911502011-03-03 11:57:44 -0500486 irq = irq_alloc_desc_from(first, -1);
487
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400488 if (irq >= 0)
489 xen_irq_init(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800490
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700491 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400492}
493
Ian Campbell7bee9762011-03-10 16:08:15 +0000494static int __must_check xen_allocate_irq_gsi(unsigned gsi)
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000495{
496 int irq;
497
Ian Campbell89911502011-03-03 11:57:44 -0500498 /*
499 * A PV guest has no concept of a GSI (since it has no ACPI
500 * nor access to/knowledge of the physical APICs). Therefore
501 * all IRQs are dynamically allocated from the entire IRQ
502 * space.
503 */
504 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000505 return xen_allocate_irq_dynamic();
506
507 /* Legacy IRQ descriptors are already allocated by the arch. */
508 if (gsi < NR_IRQS_LEGACY)
Ian Campbell6cb65372011-03-10 16:08:11 +0000509 irq = gsi;
510 else
511 irq = irq_alloc_desc_at(gsi, -1);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000512
Ian Campbell6cb65372011-03-10 16:08:11 +0000513 xen_irq_init(irq);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000514
515 return irq;
516}
517
518static void xen_free_irq(unsigned irq)
519{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100520 struct irq_info *info = irq_get_handler_data(irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000521
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -0400522 if (WARN_ON(!info))
523 return;
524
Ian Campbell6cb65372011-03-10 16:08:11 +0000525 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000526
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100527 irq_set_handler_data(irq, NULL);
Ian Campbellca62ce82011-03-10 16:08:12 +0000528
Daniel De Graaf420eb552011-10-27 17:58:47 -0400529 WARN_ON(info->refcnt > 0);
530
Ian Campbellca62ce82011-03-10 16:08:12 +0000531 kfree(info);
532
Ian Campbell72146102011-02-03 09:49:35 +0000533 /* Legacy IRQ descriptors are managed by the arch. */
534 if (irq < NR_IRQS_LEGACY)
535 return;
536
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000537 irq_free_desc(irq);
538}
539
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400540static void pirq_query_unmask(int irq)
541{
542 struct physdev_irq_status_query irq_status;
543 struct irq_info *info = info_for_irq(irq);
544
545 BUG_ON(info->type != IRQT_PIRQ);
546
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100547 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400548 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
549 irq_status.flags = 0;
550
551 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
552 if (irq_status.flags & XENIRQSTAT_needs_eoi)
553 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
554}
555
556static bool probing_irq(int irq)
557{
558 struct irq_desc *desc = irq_to_desc(irq);
559
560 return desc && desc->action == NULL;
561}
562
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100563static void eoi_pirq(struct irq_data *data)
564{
565 int evtchn = evtchn_from_irq(data->irq);
566 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
567 int rc = 0;
568
569 irq_move_irq(data);
570
571 if (VALID_EVTCHN(evtchn))
572 clear_evtchn(evtchn);
573
574 if (pirq_needs_eoi(data->irq)) {
575 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
576 WARN_ON(rc);
577 }
578}
579
580static void mask_ack_pirq(struct irq_data *data)
581{
582 disable_dynirq(data);
583 eoi_pirq(data);
584}
585
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000586static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400587{
588 struct evtchn_bind_pirq bind_pirq;
589 struct irq_info *info = info_for_irq(irq);
590 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400591 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400592
593 BUG_ON(info->type != IRQT_PIRQ);
594
595 if (VALID_EVTCHN(evtchn))
596 goto out;
597
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100598 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400599 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400600 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
601 BIND_PIRQ__WILL_SHARE : 0;
602 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
603 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400604 if (!probing_irq(irq))
Joe Perches283c0972013-06-28 03:21:41 -0700605 pr_info("Failed to obtain physical IRQ %d\n", irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400606 return 0;
607 }
608 evtchn = bind_pirq.port;
609
610 pirq_query_unmask(irq);
611
612 evtchn_to_irq[evtchn] = irq;
613 bind_evtchn_to_cpu(evtchn, 0);
614 info->evtchn = evtchn;
615
616out:
617 unmask_evtchn(evtchn);
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100618 eoi_pirq(irq_get_irq_data(irq));
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400619
620 return 0;
621}
622
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000623static unsigned int startup_pirq(struct irq_data *data)
624{
625 return __startup_pirq(data->irq);
626}
627
628static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400629{
630 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000631 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400632 struct irq_info *info = info_for_irq(irq);
633 int evtchn = evtchn_from_irq(irq);
634
635 BUG_ON(info->type != IRQT_PIRQ);
636
637 if (!VALID_EVTCHN(evtchn))
638 return;
639
640 mask_evtchn(evtchn);
641
642 close.port = evtchn;
643 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
644 BUG();
645
646 bind_evtchn_to_cpu(evtchn, 0);
647 evtchn_to_irq[evtchn] = -1;
648 info->evtchn = 0;
649}
650
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000651static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400652{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000653 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400654}
655
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000656static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400657{
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100658 disable_dynirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400659}
660
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100661int xen_irq_from_gsi(unsigned gsi)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400662{
Ian Campbell6cb65372011-03-10 16:08:11 +0000663 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400664
Ian Campbell6cb65372011-03-10 16:08:11 +0000665 list_for_each_entry(info, &xen_irq_list_head, list) {
666 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400667 continue;
668
Ian Campbell6cb65372011-03-10 16:08:11 +0000669 if (info->u.pirq.gsi == gsi)
670 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400671 }
672
673 return -1;
674}
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100675EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400676
Ian Campbell653378a2011-03-10 16:08:04 +0000677/*
678 * Do not make any assumptions regarding the relationship between the
679 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100680 *
681 * Note: We don't assign an event channel until the irq actually started
682 * up. Return an existing irq if we've already got one for the gsi.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100683 *
684 * Shareable implies level triggered, not shareable implies edge
685 * triggered here.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400686 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000687int xen_bind_pirq_gsi_to_irq(unsigned gsi,
688 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400689{
Ian Campbella0e18112011-03-10 16:08:03 +0000690 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400691 struct physdev_irq irq_op;
692
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400693 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400694
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100695 irq = xen_irq_from_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400696 if (irq != -1) {
Joe Perches283c0972013-06-28 03:21:41 -0700697 pr_info("%s: returning irq %d for gsi %u\n",
698 __func__, irq, gsi);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400699 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400700 }
701
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000702 irq = xen_allocate_irq_gsi(gsi);
Ian Campbell7bee9762011-03-10 16:08:15 +0000703 if (irq < 0)
704 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400705
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400706 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400707 irq_op.vector = 0;
708
709 /* Only the privileged domain can do this. For non-priv, the pcifront
710 * driver provides a PCI bus that does the call to do exactly
711 * this in the priv domain. */
712 if (xen_initial_domain() &&
713 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000714 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400715 irq = -ENOSPC;
716 goto out;
717 }
718
Jan Beulichdec02de2013-04-03 15:52:50 +0100719 xen_irq_info_pirq_init(irq, 0, pirq, gsi, DOMID_SELF,
Ian Campbell9158c352011-03-10 16:08:09 +0000720 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400721
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100722 pirq_query_unmask(irq);
723 /* We try to use the handler with the appropriate semantic for the
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100724 * type of interrupt: if the interrupt is an edge triggered
725 * interrupt we use handle_edge_irq.
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100726 *
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100727 * On the other hand if the interrupt is level triggered we use
728 * handle_fasteoi_irq like the native code does for this kind of
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100729 * interrupts.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100730 *
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100731 * Depending on the Xen version, pirq_needs_eoi might return true
732 * not only for level triggered interrupts but for edge triggered
733 * interrupts too. In any case Xen always honors the eoi mechanism,
734 * not injecting any more pirqs of the same kind if the first one
735 * hasn't received an eoi yet. Therefore using the fasteoi handler
736 * is the right choice either way.
737 */
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100738 if (shareable)
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100739 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
740 handle_fasteoi_irq, name);
741 else
742 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
743 handle_edge_irq, name);
744
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400745out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400746 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400747
748 return irq;
749}
750
Qing Hef731e3ef2010-10-11 15:30:09 +0100751#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000752int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000753{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000754 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000755 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000756
Ian Campbellbf480d92011-02-18 16:43:32 +0000757 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000758 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000759
Ian Campbell5cad61a2011-02-18 16:43:31 +0000760 WARN_ONCE(rc == -ENOSYS,
761 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
762
763 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000764}
765
Ian Campbellbf480d92011-02-18 16:43:32 +0000766int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Jan Beulichdec02de2013-04-03 15:52:50 +0100767 int pirq, const char *name, domid_t domid)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100768{
Ian Campbellbf480d92011-02-18 16:43:32 +0000769 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000770
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400771 mutex_lock(&irq_mapping_update_lock);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100772
Ian Campbell4b41df72011-02-18 16:43:29 +0000773 irq = xen_allocate_irq_dynamic();
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400774 if (irq < 0)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000775 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100776
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100777 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
778 name);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100779
Jan Beulichdec02de2013-04-03 15:52:50 +0100780 xen_irq_info_pirq_init(irq, 0, pirq, 0, domid, 0);
Linus Torvalds5f6fb452011-03-15 19:23:40 -0700781 ret = irq_set_msi_desc(irq, msidesc);
Ian Campbellbf480d92011-02-18 16:43:32 +0000782 if (ret < 0)
783 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100784out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400785 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000786 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000787error_irq:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400788 mutex_unlock(&irq_mapping_update_lock);
Ian Campbellbf480d92011-02-18 16:43:32 +0000789 xen_free_irq(irq);
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400790 return ret;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100791}
Qing Hef731e3ef2010-10-11 15:30:09 +0100792#endif
793
Alex Nixonb5401a92010-03-18 16:31:34 -0400794int xen_destroy_irq(int irq)
795{
796 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100797 struct physdev_unmap_pirq unmap_irq;
798 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400799 int rc = -ENOENT;
800
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400801 mutex_lock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400802
803 desc = irq_to_desc(irq);
804 if (!desc)
805 goto out;
806
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100807 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500808 unmap_irq.pirq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400809 unmap_irq.domid = info->u.pirq.domid;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100810 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500811 /* If another domain quits without making the pci_disable_msix
812 * call, the Xen hypervisor takes care of freeing the PIRQs
813 * (free_domain_pirqs).
814 */
815 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
Joe Perches283c0972013-06-28 03:21:41 -0700816 pr_info("domain %d does not have %d anymore\n",
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500817 info->u.pirq.domid, info->u.pirq.pirq);
818 else if (rc) {
Joe Perches283c0972013-06-28 03:21:41 -0700819 pr_warn("unmap irq failed %d\n", rc);
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100820 goto out;
821 }
822 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400823
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000824 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400825
826out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400827 mutex_unlock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400828 return rc;
829}
830
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000831int xen_irq_from_pirq(unsigned pirq)
832{
Ian Campbell69c358c2011-03-10 16:08:13 +0000833 int irq;
834
835 struct irq_info *info;
836
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400837 mutex_lock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000838
839 list_for_each_entry(info, &xen_irq_list_head, list) {
Konrad Rzeszutek Wilk9bb9efe2011-09-29 13:13:30 -0400840 if (info->type != IRQT_PIRQ)
Ian Campbell69c358c2011-03-10 16:08:13 +0000841 continue;
842 irq = info->irq;
843 if (info->u.pirq.pirq == pirq)
844 goto out;
845 }
846 irq = -1;
847out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400848 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000849
850 return irq;
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000851}
852
Konrad Rzeszutek Wilke6197ac2011-02-24 14:20:12 -0500853
854int xen_pirq_from_irq(unsigned irq)
855{
856 return pirq_from_irq(irq);
857}
858EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700859int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700860{
861 int irq;
862
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400863 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700864
865 irq = evtchn_to_irq[evtchn];
866
867 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000868 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000869 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000870 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700871
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100872 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100873 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700874
Ian Campbell9158c352011-03-10 16:08:09 +0000875 xen_irq_info_evtchn_init(irq, evtchn);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400876 } else {
877 struct irq_info *info = info_for_irq(irq);
878 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700879 }
880
Ian Campbell7bee9762011-03-10 16:08:15 +0000881out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400882 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700883
884 return irq;
885}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700886EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700887
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700888static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
889{
890 struct evtchn_bind_ipi bind_ipi;
891 int evtchn, irq;
892
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400893 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700894
895 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800896
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700897 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000898 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700899 if (irq < 0)
900 goto out;
901
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100902 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700903 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700904
905 bind_ipi.vcpu = cpu;
906 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
907 &bind_ipi) != 0)
908 BUG();
909 evtchn = bind_ipi.port;
910
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000911 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700912
913 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400914 } else {
915 struct irq_info *info = info_for_irq(irq);
916 WARN_ON(info == NULL || info->type != IRQT_IPI);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700917 }
918
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700919 out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400920 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700921 return irq;
922}
923
Ian Campbell2e820f52009-02-09 12:05:50 -0800924static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
925 unsigned int remote_port)
926{
927 struct evtchn_bind_interdomain bind_interdomain;
928 int err;
929
930 bind_interdomain.remote_dom = remote_domain;
931 bind_interdomain.remote_port = remote_port;
932
933 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
934 &bind_interdomain);
935
936 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
937}
938
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200939static int find_virq(unsigned int virq, unsigned int cpu)
940{
941 struct evtchn_status status;
942 int port, rc = -ENOENT;
943
944 memset(&status, 0, sizeof(status));
945 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
946 status.dom = DOMID_SELF;
947 status.port = port;
948 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
949 if (rc < 0)
950 continue;
951 if (status.status != EVTCHNSTAT_virq)
952 continue;
953 if (status.u.virq == virq && status.vcpu == cpu) {
954 rc = port;
955 break;
956 }
957 }
958 return rc;
959}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700960
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100961int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700962{
963 struct evtchn_bind_virq bind_virq;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200964 int evtchn, irq, ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700965
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400966 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700967
968 irq = per_cpu(virq_to_irq, cpu)[virq];
969
970 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000971 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000972 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000973 goto out;
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700974
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100975 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700976 handle_percpu_irq, "virq");
977
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700978 bind_virq.virq = virq;
979 bind_virq.vcpu = cpu;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200980 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
981 &bind_virq);
982 if (ret == 0)
983 evtchn = bind_virq.port;
984 else {
985 if (ret == -EEXIST)
986 ret = find_virq(virq, cpu);
987 BUG_ON(ret < 0);
988 evtchn = ret;
989 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700990
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000991 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700992
993 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400994 } else {
995 struct irq_info *info = info_for_irq(irq);
996 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700997 }
998
Ian Campbell7bee9762011-03-10 16:08:15 +0000999out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001000 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001001
1002 return irq;
1003}
1004
1005static void unbind_from_irq(unsigned int irq)
1006{
1007 struct evtchn_close close;
1008 int evtchn = evtchn_from_irq(irq);
Daniel De Graaf420eb552011-10-27 17:58:47 -04001009 struct irq_info *info = irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001010
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001011 if (WARN_ON(!info))
1012 return;
1013
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001014 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001015
Daniel De Graaf420eb552011-10-27 17:58:47 -04001016 if (info->refcnt > 0) {
1017 info->refcnt--;
1018 if (info->refcnt != 0)
1019 goto done;
1020 }
1021
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001022 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001023 close.port = evtchn;
1024 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
1025 BUG();
1026
1027 switch (type_from_irq(irq)) {
1028 case IRQT_VIRQ:
1029 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001030 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001031 break;
Alex Nixond68d82a2008-08-22 11:52:15 +01001032 case IRQT_IPI:
1033 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001034 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +01001035 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001036 default:
1037 break;
1038 }
1039
1040 /* Closed ports are implicitly re-bound to VCPU0. */
1041 bind_evtchn_to_cpu(evtchn, 0);
1042
1043 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +00001044 }
1045
Ian Campbellca62ce82011-03-10 16:08:12 +00001046 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001047
Ian Campbell9158c352011-03-10 16:08:09 +00001048 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001049
Daniel De Graaf420eb552011-10-27 17:58:47 -04001050 done:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001051 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001052}
1053
1054int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -04001055 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001056 unsigned long irqflags,
1057 const char *devname, void *dev_id)
1058{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001059 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001060
1061 irq = bind_evtchn_to_irq(evtchn);
Ian Campbell7bee9762011-03-10 16:08:15 +00001062 if (irq < 0)
1063 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001064 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1065 if (retval != 0) {
1066 unbind_from_irq(irq);
1067 return retval;
1068 }
1069
1070 return irq;
1071}
1072EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1073
Ian Campbell2e820f52009-02-09 12:05:50 -08001074int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1075 unsigned int remote_port,
1076 irq_handler_t handler,
1077 unsigned long irqflags,
1078 const char *devname,
1079 void *dev_id)
1080{
1081 int irq, retval;
1082
1083 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1084 if (irq < 0)
1085 return irq;
1086
1087 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1088 if (retval != 0) {
1089 unbind_from_irq(irq);
1090 return retval;
1091 }
1092
1093 return irq;
1094}
1095EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1096
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001097int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -04001098 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001099 unsigned long irqflags, const char *devname, void *dev_id)
1100{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001101 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001102
1103 irq = bind_virq_to_irq(virq, cpu);
Ian Campbell7bee9762011-03-10 16:08:15 +00001104 if (irq < 0)
1105 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001106 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1107 if (retval != 0) {
1108 unbind_from_irq(irq);
1109 return retval;
1110 }
1111
1112 return irq;
1113}
1114EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1115
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001116int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1117 unsigned int cpu,
1118 irq_handler_t handler,
1119 unsigned long irqflags,
1120 const char *devname,
1121 void *dev_id)
1122{
1123 int irq, retval;
1124
1125 irq = bind_ipi_to_irq(ipi, cpu);
1126 if (irq < 0)
1127 return irq;
1128
Ian Campbell9bab0b72011-10-03 15:37:00 +01001129 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001130 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1131 if (retval != 0) {
1132 unbind_from_irq(irq);
1133 return retval;
1134 }
1135
1136 return irq;
1137}
1138
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001139void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1140{
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001141 struct irq_info *info = irq_get_handler_data(irq);
1142
1143 if (WARN_ON(!info))
1144 return;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001145 free_irq(irq, dev_id);
1146 unbind_from_irq(irq);
1147}
1148EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1149
Daniel De Graaf420eb552011-10-27 17:58:47 -04001150int evtchn_make_refcounted(unsigned int evtchn)
1151{
1152 int irq = evtchn_to_irq[evtchn];
1153 struct irq_info *info;
1154
1155 if (irq == -1)
1156 return -ENOENT;
1157
1158 info = irq_get_handler_data(irq);
1159
1160 if (!info)
1161 return -ENOENT;
1162
1163 WARN_ON(info->refcnt != -1);
1164
1165 info->refcnt = 1;
1166
1167 return 0;
1168}
1169EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1170
1171int evtchn_get(unsigned int evtchn)
1172{
1173 int irq;
1174 struct irq_info *info;
1175 int err = -ENOENT;
1176
Daniel De Graafc3b3f162011-11-28 11:49:09 -05001177 if (evtchn >= NR_EVENT_CHANNELS)
1178 return -EINVAL;
1179
Daniel De Graaf420eb552011-10-27 17:58:47 -04001180 mutex_lock(&irq_mapping_update_lock);
1181
1182 irq = evtchn_to_irq[evtchn];
1183 if (irq == -1)
1184 goto done;
1185
1186 info = irq_get_handler_data(irq);
1187
1188 if (!info)
1189 goto done;
1190
1191 err = -EINVAL;
1192 if (info->refcnt <= 0)
1193 goto done;
1194
1195 info->refcnt++;
1196 err = 0;
1197 done:
1198 mutex_unlock(&irq_mapping_update_lock);
1199
1200 return err;
1201}
1202EXPORT_SYMBOL_GPL(evtchn_get);
1203
1204void evtchn_put(unsigned int evtchn)
1205{
1206 int irq = evtchn_to_irq[evtchn];
1207 if (WARN_ON(irq == -1))
1208 return;
1209 unbind_from_irq(irq);
1210}
1211EXPORT_SYMBOL_GPL(evtchn_put);
1212
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001213void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1214{
1215 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1216 BUG_ON(irq < 0);
1217 notify_remote_via_irq(irq);
1218}
1219
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001220irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1221{
1222 struct shared_info *sh = HYPERVISOR_shared_info;
1223 int cpu = smp_processor_id();
Ian Campbellc81611c2013-02-20 11:48:06 +00001224 xen_ulong_t *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001225 int i;
1226 unsigned long flags;
1227 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001228 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001229
1230 spin_lock_irqsave(&debug_lock, flags);
1231
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001232 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001233
1234 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001235 int pending;
1236 v = per_cpu(xen_vcpu, i);
1237 pending = (get_irq_regs() && i == cpu)
1238 ? xen_irqs_disabled(get_irq_regs())
1239 : v->evtchn_upcall_mask;
Ian Campbellc81611c2013-02-20 11:48:06 +00001240 printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong"\n ", i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001241 pending, v->evtchn_upcall_pending,
1242 (int)(sizeof(v->evtchn_pending_sel)*2),
1243 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001244 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001245 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001246
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001247 printk("\npending:\n ");
1248 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001249 printk("%0*"PRI_xen_ulong"%s",
1250 (int)sizeof(sh->evtchn_pending[0])*2,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001251 sh->evtchn_pending[i],
1252 i % 8 == 0 ? "\n " : " ");
1253 printk("\nglobal mask:\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",
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001256 (int)(sizeof(sh->evtchn_mask[0])*2),
1257 sh->evtchn_mask[i],
1258 i % 8 == 0 ? "\n " : " ");
1259
1260 printk("\nglobally unmasked:\n ");
1261 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001262 printk("%0*"PRI_xen_ulong"%s",
1263 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001264 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1265 i % 8 == 0 ? "\n " : " ");
1266
1267 printk("\nlocal cpu%d mask:\n ", cpu);
Ian Campbellc81611c2013-02-20 11:48:06 +00001268 for (i = (NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD)-1; i >= 0; i--)
1269 printk("%0*"PRI_xen_ulong"%s", (int)(sizeof(cpu_evtchn[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001270 cpu_evtchn[i],
1271 i % 8 == 0 ? "\n " : " ");
1272
1273 printk("\nlocally unmasked:\n ");
1274 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001275 xen_ulong_t pending = sh->evtchn_pending[i]
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001276 & ~sh->evtchn_mask[i]
1277 & cpu_evtchn[i];
Ian Campbellc81611c2013-02-20 11:48:06 +00001278 printk("%0*"PRI_xen_ulong"%s",
1279 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001280 pending, i % 8 == 0 ? "\n " : " ");
1281 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001282
1283 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001284 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001285 if (sync_test_bit(i, BM(sh->evtchn_pending))) {
1286 int word_idx = i / BITS_PER_EVTCHN_WORD;
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001287 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001288 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001289 evtchn_to_irq[i],
Ian Campbellc81611c2013-02-20 11:48:06 +00001290 sync_test_bit(word_idx, BM(&v->evtchn_pending_sel))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001291 ? "" : " l2-clear",
Ian Campbellc81611c2013-02-20 11:48:06 +00001292 !sync_test_bit(i, BM(sh->evtchn_mask))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001293 ? "" : " globally-masked",
Ian Campbellc81611c2013-02-20 11:48:06 +00001294 sync_test_bit(i, BM(cpu_evtchn))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001295 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001296 }
1297 }
1298
1299 spin_unlock_irqrestore(&debug_lock, flags);
1300
1301 return IRQ_HANDLED;
1302}
1303
Tejun Heo245b2e72009-06-24 15:13:48 +09001304static DEFINE_PER_CPU(unsigned, xed_nesting_count);
Keir Fraserada68142011-03-03 10:01:11 +00001305static DEFINE_PER_CPU(unsigned int, current_word_idx);
1306static DEFINE_PER_CPU(unsigned int, current_bit_idx);
Tejun Heo245b2e72009-06-24 15:13:48 +09001307
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001308/*
Scott Rixnerab7f8632011-03-03 09:30:08 +00001309 * Mask out the i least significant bits of w
1310 */
Ian Campbellc81611c2013-02-20 11:48:06 +00001311#define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001312
1313/*
1314 * Search the CPUs pending events bitmasks. For each one found, map
1315 * the event number to an irq, and feed it into do_IRQ() for
1316 * handling.
1317 *
1318 * Xen uses a two-level bitmap to speed searching. The first level is
1319 * a bitset of words which contain pending event bits. The second
1320 * level is a bitset of pending events themselves.
1321 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001322static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001323{
Keir Fraser24b51c22011-03-03 11:06:28 +00001324 int start_word_idx, start_bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001325 int word_idx, bit_idx;
Keir Fraserbee980d2013-03-28 10:03:36 -04001326 int i, irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001327 int cpu = get_cpu();
1328 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001329 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Ruslan Pisarev088c05a2011-07-26 14:16:13 +03001330 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001331
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001332 do {
Ian Campbellc81611c2013-02-20 11:48:06 +00001333 xen_ulong_t pending_words;
Keir Fraserbee980d2013-03-28 10:03:36 -04001334 xen_ulong_t pending_bits;
1335 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001336
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001337 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001338
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001339 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001340 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001341
Ian Campbellc81611c2013-02-20 11:48:06 +00001342 /*
1343 * Master flag must be cleared /before/ clearing
1344 * selector flag. xchg_xen_ulong must contain an
1345 * appropriate barrier.
1346 */
Keir Fraserbee980d2013-03-28 10:03:36 -04001347 if ((irq = per_cpu(virq_to_irq, cpu)[VIRQ_TIMER]) != -1) {
1348 int evtchn = evtchn_from_irq(irq);
1349 word_idx = evtchn / BITS_PER_LONG;
1350 pending_bits = evtchn % BITS_PER_LONG;
1351 if (active_evtchns(cpu, s, word_idx) & (1ULL << pending_bits)) {
1352 desc = irq_to_desc(irq);
1353 if (desc)
1354 generic_handle_irq_desc(irq, desc);
1355 }
1356 }
1357
Ian Campbellc81611c2013-02-20 11:48:06 +00001358 pending_words = xchg_xen_ulong(&vcpu_info->evtchn_pending_sel, 0);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001359
Keir Fraser24b51c22011-03-03 11:06:28 +00001360 start_word_idx = __this_cpu_read(current_word_idx);
1361 start_bit_idx = __this_cpu_read(current_bit_idx);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001362
Keir Fraser24b51c22011-03-03 11:06:28 +00001363 word_idx = start_word_idx;
1364
1365 for (i = 0; pending_words != 0; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001366 xen_ulong_t words;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001367
Scott Rixnerab7f8632011-03-03 09:30:08 +00001368 words = MASK_LSBS(pending_words, word_idx);
1369
1370 /*
Keir Fraserada68142011-03-03 10:01:11 +00001371 * If we masked out all events, wrap to beginning.
Scott Rixnerab7f8632011-03-03 09:30:08 +00001372 */
1373 if (words == 0) {
Keir Fraserada68142011-03-03 10:01:11 +00001374 word_idx = 0;
1375 bit_idx = 0;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001376 continue;
1377 }
Ian Campbellc81611c2013-02-20 11:48:06 +00001378 word_idx = EVTCHN_FIRST_BIT(words);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001379
Keir Fraser24b51c22011-03-03 11:06:28 +00001380 pending_bits = active_evtchns(cpu, s, word_idx);
1381 bit_idx = 0; /* usually scan entire word from start */
1382 if (word_idx == start_word_idx) {
1383 /* We scan the starting word in two parts */
1384 if (i == 0)
1385 /* 1st time: start in the middle */
1386 bit_idx = start_bit_idx;
1387 else
1388 /* 2nd time: mask bits done already */
1389 bit_idx &= (1UL << start_bit_idx) - 1;
1390 }
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{
1496 struct evtchn_bind_vcpu bind_vcpu;
1497 int evtchn = evtchn_from_irq(irq);
1498
Ian Campbellbe494722011-03-10 16:08:02 +00001499 if (!VALID_EVTCHN(evtchn))
1500 return -1;
1501
1502 /*
1503 * Events delivered via platform PCI interrupts are always
1504 * routed to vcpu 0 and hence cannot be rebound.
1505 */
1506 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001507 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001508
1509 /* Send future instances of this interrupt to other vcpu. */
1510 bind_vcpu.port = evtchn;
1511 bind_vcpu.vcpu = tcpu;
1512
1513 /*
1514 * If this fails, it usually just indicates that we're dealing with a
1515 * virq or IPI channel, which don't actually need to be rebound. Ignore
1516 * it, but don't do the xenlinux-level rebind in that case.
1517 */
1518 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1519 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001520
1521 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001522}
1523
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001524static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1525 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001526{
Rusty Russell0de26522008-12-13 21:20:26 +10301527 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001528
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001529 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001530}
1531
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001532int resend_irq_on_evtchn(unsigned int irq)
1533{
1534 int masked, evtchn = evtchn_from_irq(irq);
1535 struct shared_info *s = HYPERVISOR_shared_info;
1536
1537 if (!VALID_EVTCHN(evtchn))
1538 return 1;
1539
Ian Campbellc81611c2013-02-20 11:48:06 +00001540 masked = sync_test_and_set_bit(evtchn, BM(s->evtchn_mask));
1541 sync_set_bit(evtchn, BM(s->evtchn_pending));
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001542 if (!masked)
1543 unmask_evtchn(evtchn);
1544
1545 return 1;
1546}
1547
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001548static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001549{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001550 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001551
1552 if (VALID_EVTCHN(evtchn))
1553 unmask_evtchn(evtchn);
1554}
1555
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001556static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001557{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001558 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001559
1560 if (VALID_EVTCHN(evtchn))
1561 mask_evtchn(evtchn);
1562}
1563
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001564static void ack_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
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001568 irq_move_irq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001569
1570 if (VALID_EVTCHN(evtchn))
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001571 clear_evtchn(evtchn);
1572}
1573
1574static void mask_ack_dynirq(struct irq_data *data)
1575{
1576 disable_dynirq(data);
1577 ack_dynirq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001578}
1579
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001580static int retrigger_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 Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001583 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001584 int ret = 0;
1585
1586 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001587 int masked;
1588
Ian Campbellc81611c2013-02-20 11:48:06 +00001589 masked = sync_test_and_set_bit(evtchn, BM(sh->evtchn_mask));
1590 sync_set_bit(evtchn, BM(sh->evtchn_pending));
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001591 if (!masked)
1592 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001593 ret = 1;
1594 }
1595
1596 return ret;
1597}
1598
Ian Campbell0a852262011-03-10 16:08:06 +00001599static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001600{
1601 int pirq, rc, irq, gsi;
1602 struct physdev_map_pirq map_irq;
Ian Campbell69c358c2011-03-10 16:08:13 +00001603 struct irq_info *info;
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001604
Ian Campbell69c358c2011-03-10 16:08:13 +00001605 list_for_each_entry(info, &xen_irq_list_head, list) {
1606 if (info->type != IRQT_PIRQ)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001607 continue;
1608
Ian Campbell69c358c2011-03-10 16:08:13 +00001609 pirq = info->u.pirq.pirq;
1610 gsi = info->u.pirq.gsi;
1611 irq = info->irq;
1612
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001613 /* save/restore of PT devices doesn't work, so at this point the
1614 * only devices present are GSI based emulated devices */
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001615 if (!gsi)
1616 continue;
1617
1618 map_irq.domid = DOMID_SELF;
1619 map_irq.type = MAP_PIRQ_TYPE_GSI;
1620 map_irq.index = gsi;
1621 map_irq.pirq = pirq;
1622
1623 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1624 if (rc) {
Joe Perches283c0972013-06-28 03:21:41 -07001625 pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1626 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001627 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001628 continue;
1629 }
1630
1631 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1632
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001633 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001634 }
1635}
1636
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001637static void restore_cpu_virqs(unsigned int cpu)
1638{
1639 struct evtchn_bind_virq bind_virq;
1640 int virq, irq, evtchn;
1641
1642 for (virq = 0; virq < NR_VIRQS; virq++) {
1643 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1644 continue;
1645
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001646 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001647
1648 /* Get a new binding from Xen. */
1649 bind_virq.virq = virq;
1650 bind_virq.vcpu = cpu;
1651 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1652 &bind_virq) != 0)
1653 BUG();
1654 evtchn = bind_virq.port;
1655
1656 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001657 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001658 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001659 }
1660}
1661
1662static void restore_cpu_ipis(unsigned int cpu)
1663{
1664 struct evtchn_bind_ipi bind_ipi;
1665 int ipi, irq, evtchn;
1666
1667 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1668 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1669 continue;
1670
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001671 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001672
1673 /* Get a new binding from Xen. */
1674 bind_ipi.vcpu = cpu;
1675 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1676 &bind_ipi) != 0)
1677 BUG();
1678 evtchn = bind_ipi.port;
1679
1680 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001681 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001682 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001683 }
1684}
1685
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001686/* Clear an irq's pending state, in preparation for polling on it */
1687void xen_clear_irq_pending(int irq)
1688{
1689 int evtchn = evtchn_from_irq(irq);
1690
1691 if (VALID_EVTCHN(evtchn))
1692 clear_evtchn(evtchn);
1693}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001694EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001695void xen_set_irq_pending(int irq)
1696{
1697 int evtchn = evtchn_from_irq(irq);
1698
1699 if (VALID_EVTCHN(evtchn))
1700 set_evtchn(evtchn);
1701}
1702
1703bool xen_test_irq_pending(int irq)
1704{
1705 int evtchn = evtchn_from_irq(irq);
1706 bool ret = false;
1707
1708 if (VALID_EVTCHN(evtchn))
1709 ret = test_evtchn(evtchn);
1710
1711 return ret;
1712}
1713
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001714/* Poll waiting for an irq to become pending with timeout. In the usual case,
1715 * the irq will be disabled so it won't deliver an interrupt. */
1716void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001717{
1718 evtchn_port_t evtchn = evtchn_from_irq(irq);
1719
1720 if (VALID_EVTCHN(evtchn)) {
1721 struct sched_poll poll;
1722
1723 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001724 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001725 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001726
1727 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1728 BUG();
1729 }
1730}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001731EXPORT_SYMBOL(xen_poll_irq_timeout);
1732/* Poll waiting for an irq to become pending. In the usual case, the
1733 * irq will be disabled so it won't deliver an interrupt. */
1734void xen_poll_irq(int irq)
1735{
1736 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1737}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001738
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001739/* Check whether the IRQ line is shared with other guests. */
1740int xen_test_irq_shared(int irq)
1741{
1742 struct irq_info *info = info_for_irq(irq);
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001743 struct physdev_irq_status_query irq_status;
1744
1745 if (WARN_ON(!info))
1746 return -ENOENT;
1747
1748 irq_status.irq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001749
1750 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1751 return 0;
1752 return !(irq_status.flags & XENIRQSTAT_shared);
1753}
1754EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1755
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001756void xen_irq_resume(void)
1757{
Ian Campbell6cb65372011-03-10 16:08:11 +00001758 unsigned int cpu, evtchn;
1759 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001760
1761 init_evtchn_cpu_bindings();
1762
1763 /* 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
1873 init_evtchn_cpu_bindings();
1874
1875 /* No event channels are 'live' right now. */
1876 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1877 mask_evtchn(i);
1878
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001879 pirq_needs_eoi = pirq_needs_eoi_flag;
1880
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001881#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001882 if (xen_hvm_domain()) {
1883 xen_callback_vector();
1884 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001885 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1886 * __acpi_register_gsi can point at the right function */
1887 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001888 } else {
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001889 int rc;
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001890 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1891
Sheng Yang38e20b02010-05-14 12:40:51 +01001892 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001893 if (xen_initial_domain())
Konrad Rzeszutek Wilka0ee0562011-06-09 09:49:13 -04001894 pci_xen_initial_domain();
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001895
1896 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1897 eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1898 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1899 if (rc != 0) {
1900 free_page((unsigned long) pirq_eoi_map);
1901 pirq_eoi_map = NULL;
1902 } else
1903 pirq_needs_eoi = pirq_check_eoi_map;
Sheng Yang38e20b02010-05-14 12:40:51 +01001904 }
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001905#endif
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001906}